ia64/xen-unstable

view tools/blktap/lib/xs_api.c @ 11343:8135621eeb7b

Fix typo.

Signed-off-by: <andy@xensource.com>
author Andrew Warfield <andy@xensource.com>
date Tue Aug 29 16:31:57 2006 -0700 (2006-08-29)
parents 7299065fac68
children a72fdb6a19a1
line source
1 /*
2 * xs_api.c
3 *
4 * blocktap interface functions to xenstore
5 *
6 * (c) 2005 Andrew Warfield and Julian Chesterfield
7 *
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 * IN THE SOFTWARE.
32 *
33 */
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <printf.h>
38 #include <string.h>
39 #include <err.h>
40 #include <stdarg.h>
41 #include <errno.h>
42 #include <xs.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <poll.h>
47 #include "blktaplib.h"
48 #include "list.h"
49 #include "xs_api.h"
51 #if 0
52 #define DPRINTF(_f, _a...) printf ( _f , ## _a )
53 #else
54 #define DPRINTF(_f, _a...) ((void)0)
55 #endif
57 static LIST_HEAD(watches);
58 #define BASE_DEV_VAL 2048
60 int xs_gather(struct xs_handle *xs, const char *dir, ...)
61 {
62 va_list ap;
63 const char *name;
64 char *path, **e;
65 int ret = 0, num,i;
66 unsigned int len;
67 xs_transaction_t xth;
69 again:
70 if ( (xth = xs_transaction_start(xs)) == XBT_NULL) {
71 DPRINTF("unable to start xs trasanction\n");
72 ret = ENOMEM;
73 return ret;
74 }
76 va_start(ap, dir);
77 while ( (ret == 0) && (name = va_arg(ap, char *)) != NULL) {
78 const char *fmt = va_arg(ap, char *);
79 void *result = va_arg(ap, void *);
80 char *p;
82 if (asprintf(&path, "%s/%s", dir, name) == -1)
83 {
84 printf("allocation error in xs_gather!\n");
85 ret = ENOMEM;
86 break;
87 }
89 p = xs_read(xs, xth, path, &len);
92 free(path);
93 if (p == NULL) {
94 ret = ENOENT;
95 break;
96 }
97 if (fmt) {
98 if (sscanf(p, fmt, result) == 0)
99 ret = EINVAL;
100 free(p);
101 } else
102 *(char **)result = p;
103 }
104 va_end(ap);
106 if (!xs_transaction_end(xs, xth, ret)) {
107 if (ret == 0 && errno == EAGAIN)
108 goto again;
109 else
110 ret = errno;
111 }
113 return ret;
114 }
117 /* Single printf and write: returns -errno or 0. */
118 int xs_printf(struct xs_handle *h, const char *dir, const char *node,
119 const char *fmt, ...)
120 {
121 char *buf, *path;
122 va_list ap;
123 int ret;
125 va_start(ap, fmt);
126 ret = vasprintf(&buf, fmt, ap);
127 va_end(ap);
129 asprintf(&path, "%s/%s", dir, node);
131 if ( (path == NULL) || (buf == NULL) )
132 return 0;
134 ret = xs_write(h, XBT_NULL, path, buf, strlen(buf)+1);
136 free(buf);
137 free(path);
139 return ret;
140 }
143 int xs_exists(struct xs_handle *h, const char *path)
144 {
145 char **d;
146 unsigned int num;
147 xs_transaction_t xth;
149 if ( (xth = xs_transaction_start(h)) == XBT_NULL) {
150 printf("unable to start xs trasanction\n");
151 return 0;
152 }
154 d = xs_directory(h, xth, path, &num);
155 xs_transaction_end(h, xth, 0);
156 if (d == NULL)
157 return 0;
158 free(d);
159 return 1;
160 }
164 /**
165 * This assumes that the domain name we are looking for is unique.
166 * Name parameter Domain-0
167 */
168 char *get_dom_domid(struct xs_handle *h, const char *name)
169 {
170 char **e, *val, *domid = NULL;
171 unsigned int num, len;
172 int i;
173 char *path;
174 xs_transaction_t xth;
176 if ( (xth = xs_transaction_start(h)) == XBT_NULL) {
177 warn("unable to start xs trasanction\n");
178 return NULL;
179 }
181 e = xs_directory(h, xth, "/local/domain", &num);
183 for (i = 0; (i < num) && (domid == NULL); i++) {
184 asprintf(&path, "/local/domain/%s/name", e[i]);
185 val = xs_read(h, xth, path, &len);
186 free(path);
187 if (val == NULL)
188 continue;
190 if (strcmp(val, name) == 0) {
191 /* match! */
192 asprintf(&path, "/local/domain/%s/domid", e[i]);
193 domid = xs_read(h, xth, path, &len);
194 free(path);
195 }
196 free(val);
197 }
198 xs_transaction_end(h, xth, 0);
200 free(e);
201 return domid;
202 }
204 int convert_dev_name_to_num(char *name) {
205 char *p_sd, *p_hd, *p_xvd, *p_plx, *p, *alpha,*ptr;
206 int majors[10] = {3,22,33,34,56,57,88,89,90,91};
207 int maj,i,ret = 0;
209 asprintf(&p_sd,"/dev/sd");
210 asprintf(&p_hd,"/dev/hd");
211 asprintf(&p_xvd,"/dev/xvd");
212 asprintf(&p_plx,"plx");
213 asprintf(&alpha,"abcdefghijklmnop");
216 if (strstr(name, p_sd) != NULL) {
217 p = name + strlen(p_sd);
218 for(i = 0, ptr = alpha; i < strlen(alpha); i++) {
219 if(*ptr == *p)
220 break;
221 *ptr++;
222 }
223 *p++;
224 ret = BASE_DEV_VAL + (16*i) + atoi(p);
225 } else if (strstr(name, p_hd) != NULL) {
226 p = name + strlen(p_hd);
227 for (i = 0, ptr = alpha; i < strlen(alpha); i++) {
228 if(*ptr == *p) break;
229 *ptr++;
230 }
231 *p++;
232 ret = (majors[i/2]*256) + atoi(p);
234 } else if (strstr(name, p_xvd) != NULL) {
235 p = name + strlen(p_xvd);
236 for(i = 0, ptr = alpha; i < strlen(alpha); i++) {
237 if(*ptr == *p) break;
238 *ptr++;
239 }
240 *p++;
241 ret = (202*256) + (16*i) + atoi(p);
243 } else if (strstr(name, p_plx) != NULL) {
244 p = name + strlen(p_plx);
245 ret = atoi(p);
247 } else {
248 DPRINTF("Unknown device type, setting to default.\n");
249 ret = BASE_DEV_VAL;
250 }
252 free(p_sd);
253 free(p_hd);
254 free(p_xvd);
255 free(p_plx);
256 free(alpha);
258 return ret;
259 }
261 /**
262 * A little paranoia: we don't just trust token.
263 */
264 static struct xenbus_watch *find_watch(const char *token)
265 {
266 struct xenbus_watch *i, *cmp;
268 cmp = (void *)strtoul(token, NULL, 16);
270 list_for_each_entry(i, &watches, list)
271 if (i == cmp)
272 return i;
273 return NULL;
274 }
276 /**
277 * Register callback to watch this node.
278 * like xs_watch, return 0 on failure
279 */
280 int register_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch)
281 {
282 /* Pointer in ascii is the token. */
283 char token[sizeof(watch) * 2 + 1];
284 int er;
286 sprintf(token, "%lX", (long)watch);
287 if (find_watch(token))
288 {
289 DPRINTF("watch collision!\n");
290 return -EINVAL;
291 }
293 er = xs_watch(h, watch->node, token);
294 if (er != 0) {
295 list_add(&watch->list, &watches);
296 }
298 return er;
299 }
301 int unregister_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch)
302 {
303 char token[sizeof(watch) * 2 + 1];
304 int er;
306 sprintf(token, "%lX", (long)watch);
307 if (!find_watch(token))
308 {
309 DPRINTF("no such watch!\n");
310 return -EINVAL;
311 }
314 er = xs_unwatch(h, watch->node, token);
315 list_del(&watch->list);
317 if (er == 0)
318 DPRINTF("XENBUS Failed to release watch %s: %i\n",
319 watch->node, er);
320 return 0;
321 }
323 /**
324 * Re-register callbacks to all watches.
325 */
326 void reregister_xenbus_watches(struct xs_handle *h)
327 {
328 struct xenbus_watch *watch;
329 char token[sizeof(watch) * 2 + 1];
331 list_for_each_entry(watch, &watches, list) {
332 sprintf(token, "%lX", (long)watch);
333 xs_watch(h, watch->node, token);
334 }
335 }
337 /**
338 * based on watch_thread()
339 */
340 int xs_fire_next_watch(struct xs_handle *h)
341 {
342 char **res;
343 char *token;
344 char *node = NULL;
345 struct xenbus_watch *w;
346 int er;
347 unsigned int num;
349 res = xs_read_watch(h, &num);
350 if (res == NULL)
351 return -EAGAIN; /* in O_NONBLOCK, read_watch returns 0... */
353 node = res[XS_WATCH_PATH];
354 token = res[XS_WATCH_TOKEN];
356 w = find_watch(token);
357 if (!w)
358 {
359 DPRINTF("unregistered watch fired\n");
360 goto done;
361 }
362 w->callback(h, w, node);
364 done:
365 free(res);
366 return 1;
367 }