ia64/xen-unstable

view extras/mini-os/lib/xs.c @ 18811:390ef36eb596

Remove Xen-private definitions from kexec public header.

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Nov 19 13:13:39 2008 +0000 (2008-11-19)
parents a905c582a406
children
line source
1 /*
2 * libxs-compatible layer
3 *
4 * Samuel Thibault <Samuel.Thibault@eu.citrix.net>, 2007-2008
5 *
6 * Mere wrapper around xenbus_*
7 */
9 #ifdef HAVE_LIBC
10 #include <os.h>
11 #include <lib.h>
12 #include <xs.h>
13 #include <xenbus.h>
14 #include <stdlib.h>
15 #include <unistd.h>
17 static inline int _xs_fileno(struct xs_handle *h) {
18 return (intptr_t) h;
19 }
21 struct xs_handle *xs_daemon_open()
22 {
23 int fd = alloc_fd(FTYPE_XENBUS);
24 files[fd].xenbus.events = NULL;
25 printk("xs_daemon_open -> %d, %p\n", fd, &files[fd].xenbus.events);
26 return (void*)(intptr_t) fd;
27 }
29 void xs_daemon_close(struct xs_handle *h)
30 {
31 int fd = _xs_fileno(h);
32 struct xenbus_event *event;
33 for (event = files[fd].xenbus.events; event; event = event->next)
34 free(event);
35 files[fd].type = FTYPE_NONE;
36 }
38 int xs_fileno(struct xs_handle *h)
39 {
40 return _xs_fileno(h);
41 }
43 void *xs_read(struct xs_handle *h, xs_transaction_t t,
44 const char *path, unsigned int *len)
45 {
46 char *value;
47 char *msg;
49 msg = xenbus_read(t, path, &value);
50 if (msg) {
51 printk("xs_read(%s): %s\n", path, msg);
52 return NULL;
53 }
55 if (len)
56 *len = strlen(value);
57 return value;
58 }
60 bool xs_write(struct xs_handle *h, xs_transaction_t t,
61 const char *path, const void *data, unsigned int len)
62 {
63 char value[len + 1];
64 char *msg;
66 memcpy(value, data, len);
67 value[len] = 0;
69 msg = xenbus_write(t, path, value);
70 if (msg) {
71 printk("xs_write(%s): %s\n", path, msg);
72 return false;
73 }
74 return true;
75 }
77 static bool xs_bool(char *reply)
78 {
79 if (!reply)
80 return true;
81 free(reply);
82 return false;
83 }
85 bool xs_rm(struct xs_handle *h, xs_transaction_t t, const char *path)
86 {
87 return xs_bool(xenbus_rm(t, path));
88 }
90 static void *xs_talkv(struct xs_handle *h, xs_transaction_t t,
91 enum xsd_sockmsg_type type,
92 struct write_req *iovec,
93 unsigned int num_vecs,
94 unsigned int *len)
95 {
96 struct xsd_sockmsg *msg;
97 void *ret;
99 msg = xenbus_msg_reply(type, t, iovec, num_vecs);
100 ret = malloc(msg->len);
101 memcpy(ret, (char*) msg + sizeof(*msg), msg->len);
102 if (len)
103 *len = msg->len - 1;
104 free(msg);
105 return ret;
106 }
108 static void *xs_single(struct xs_handle *h, xs_transaction_t t,
109 enum xsd_sockmsg_type type,
110 const char *string,
111 unsigned int *len)
112 {
113 struct write_req iovec;
115 iovec.data = (void *)string;
116 iovec.len = strlen(string) + 1;
118 return xs_talkv(h, t, type, &iovec, 1, len);
119 }
121 char *xs_get_domain_path(struct xs_handle *h, unsigned int domid)
122 {
123 char domid_str[MAX_STRLEN(domid)];
125 sprintf(domid_str, "%u", domid);
127 return xs_single(h, XBT_NULL, XS_GET_DOMAIN_PATH, domid_str, NULL);
128 }
130 char **xs_directory(struct xs_handle *h, xs_transaction_t t,
131 const char *path, unsigned int *num)
132 {
133 char *msg;
134 char **entries, **res;
135 char *entry;
136 int i, n;
137 int size;
139 msg = xenbus_ls(t, path, &res);
140 if (msg) {
141 printk("xs_directory(%s): %s\n", path, msg);
142 return NULL;
143 }
145 size = 0;
146 for (n = 0; res[n]; n++)
147 size += strlen(res[n]) + 1;
149 entries = malloc(n * sizeof(char *) + size);
150 entry = (char *) (&entries[n]);
152 for (i = 0; i < n; i++) {
153 int l = strlen(res[i]) + 1;
154 memcpy(entry, res[i], l);
155 free(res[i]);
156 entries[i] = entry;
157 entry += l;
158 }
160 *num = n;
161 return entries;
162 }
164 bool xs_watch(struct xs_handle *h, const char *path, const char *token)
165 {
166 int fd = _xs_fileno(h);
167 printk("xs_watch(%s, %s)\n", path, token);
168 return xs_bool(xenbus_watch_path_token(XBT_NULL, path, token, &files[fd].xenbus.events));
169 }
171 char **xs_read_watch(struct xs_handle *h, unsigned int *num)
172 {
173 int fd = _xs_fileno(h);
174 struct xenbus_event *event;
175 event = files[fd].xenbus.events;
176 files[fd].xenbus.events = event->next;
177 printk("xs_read_watch() -> %s %s\n", event->path, event->token);
178 *num = 2;
179 return (char **) &event->path;
180 }
182 bool xs_unwatch(struct xs_handle *h, const char *path, const char *token)
183 {
184 printk("xs_unwatch(%s, %s)\n", path, token);
185 return xs_bool(xenbus_unwatch_path_token(XBT_NULL, path, token));
186 }
187 #endif