ia64/xen-unstable

view tools/xenstore/xs.c @ 6538:84ee014ebd41

Merge xen-vtx-unstable.hg
author adsharma@los-vmm.sc.intel.com
date Wed Aug 17 12:34:38 2005 -0800 (2005-08-17)
parents 23979fb12c49 a9ee400a5da9
children 99914b54f7bf
line source
1 /*
2 Xen Store Daemon interface providing simple tree-like database.
3 Copyright (C) 2005 Rusty Russell IBM Corporation
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <stdio.h>
31 #include <signal.h>
32 #include <stdint.h>
33 #include <errno.h>
34 #include "xs.h"
35 #include "xenstored.h"
36 #include "xs_lib.h"
37 #include "utils.h"
39 struct xs_handle
40 {
41 int fd;
42 };
44 /* Get the socket from the store daemon handle.
45 */
46 int xs_fileno(struct xs_handle *h)
47 {
48 return h->fd;
49 }
51 static struct xs_handle *get_socket(const char *connect_to)
52 {
53 struct sockaddr_un addr;
54 int sock, saved_errno;
55 struct xs_handle *h = NULL;
57 sock = socket(PF_UNIX, SOCK_STREAM, 0);
58 if (sock < 0)
59 return NULL;
61 addr.sun_family = AF_UNIX;
62 strcpy(addr.sun_path, connect_to);
64 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
65 h = malloc(sizeof(*h));
66 if (h) {
67 h->fd = sock;
68 return h;
69 }
70 }
72 saved_errno = errno;
73 close(sock);
74 free(h);
75 errno = saved_errno;
76 return NULL;
77 }
79 struct xs_handle *xs_daemon_open(void)
80 {
81 return get_socket(xs_daemon_socket());
82 }
84 struct xs_handle *xs_daemon_open_readonly(void)
85 {
86 return get_socket(xs_daemon_socket_ro());
87 }
89 void xs_daemon_close(struct xs_handle *h)
90 {
91 if (h->fd >= 0)
92 close(h->fd);
93 free(h);
94 }
96 static bool read_all(int fd, void *data, unsigned int len)
97 {
98 while (len) {
99 int done;
101 done = read(fd, data, len);
102 if (done < 0) {
103 if (errno == EINTR)
104 continue;
105 return false;
106 }
107 if (done == 0) {
108 /* It closed fd on us? EBADF is appropriate. */
109 errno = EBADF;
110 return false;
111 }
112 data += done;
113 len -= done;
114 }
116 return true;
117 }
119 #ifdef XSTEST
120 #define read_all read_all_choice
121 #define xs_write_all write_all_choice
122 #endif
124 static int get_error(const char *errorstring)
125 {
126 unsigned int i;
128 for (i = 0; !streq(errorstring, xsd_errors[i].errstring); i++)
129 if (i == ARRAY_SIZE(xsd_errors) - 1)
130 return EINVAL;
131 return xsd_errors[i].errnum;
132 }
134 /* Adds extra nul terminator, because we generally (always?) hold strings. */
135 static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len)
136 {
137 struct xsd_sockmsg msg;
138 void *ret;
139 int saved_errno;
141 if (!read_all(fd, &msg, sizeof(msg)))
142 return NULL;
144 ret = malloc(msg.len + 1);
145 if (!ret)
146 return NULL;
148 if (!read_all(fd, ret, msg.len)) {
149 saved_errno = errno;
150 free(ret);
151 errno = saved_errno;
152 return NULL;
153 }
155 *type = msg.type;
156 if (len)
157 *len = msg.len;
158 ((char *)ret)[msg.len] = '\0';
159 return ret;
160 }
162 /* Send message to xs, get malloc'ed reply. NULL and set errno on error. */
163 static void *xs_talkv(struct xs_handle *h, enum xsd_sockmsg_type type,
164 const struct iovec *iovec, unsigned int num_vecs,
165 unsigned int *len)
166 {
167 struct xsd_sockmsg msg;
168 void *ret = NULL;
169 int saved_errno;
170 unsigned int i;
171 struct sigaction ignorepipe, oldact;
173 msg.type = type;
174 msg.len = 0;
175 for (i = 0; i < num_vecs; i++)
176 msg.len += iovec[i].iov_len;
178 ignorepipe.sa_handler = SIG_IGN;
179 sigemptyset(&ignorepipe.sa_mask);
180 ignorepipe.sa_flags = 0;
181 sigaction(SIGPIPE, &ignorepipe, &oldact);
183 if (!xs_write_all(h->fd, &msg, sizeof(msg)))
184 goto fail;
186 for (i = 0; i < num_vecs; i++)
187 if (!xs_write_all(h->fd, iovec[i].iov_base, iovec[i].iov_len))
188 goto fail;
190 /* Watches can have fired before reply comes: daemon detects
191 * and re-transmits, so we can ignore this. */
192 do {
193 free(ret);
194 ret = read_reply(h->fd, &msg.type, len);
195 if (!ret)
196 goto fail;
197 } while (msg.type == XS_WATCH_EVENT);
199 sigaction(SIGPIPE, &oldact, NULL);
200 if (msg.type == XS_ERROR) {
201 saved_errno = get_error(ret);
202 free(ret);
203 errno = saved_errno;
204 return NULL;
205 }
207 if (msg.type != type) {
208 free(ret);
209 saved_errno = EBADF;
210 goto close_fd;
212 }
213 return ret;
215 fail:
216 /* We're in a bad state, so close fd. */
217 saved_errno = errno;
218 sigaction(SIGPIPE, &oldact, NULL);
219 close_fd:
220 close(h->fd);
221 h->fd = -1;
222 errno = saved_errno;
223 return NULL;
224 }
226 /* free(), but don't change errno. */
227 static void free_no_errno(void *p)
228 {
229 int saved_errno = errno;
230 free(p);
231 errno = saved_errno;
232 }
234 /* Simplified version of xs_talkv: single message. */
235 static void *xs_single(struct xs_handle *h, enum xsd_sockmsg_type type,
236 const char *string, unsigned int *len)
237 {
238 struct iovec iovec;
240 iovec.iov_base = (void *)string;
241 iovec.iov_len = strlen(string) + 1;
242 return xs_talkv(h, type, &iovec, 1, len);
243 }
245 static bool xs_bool(char *reply)
246 {
247 if (!reply)
248 return false;
249 free(reply);
250 return true;
251 }
253 char **xs_directory(struct xs_handle *h, const char *path, unsigned int *num)
254 {
255 char *strings, *p, **ret;
256 unsigned int len;
258 strings = xs_single(h, XS_DIRECTORY, path, &len);
259 if (!strings)
260 return NULL;
262 /* Count the strings. */
263 *num = xs_count_strings(strings, len);
265 /* Transfer to one big alloc for easy freeing. */
266 ret = malloc(*num * sizeof(char *) + len);
267 if (!ret) {
268 free_no_errno(strings);
269 return NULL;
270 }
271 memcpy(&ret[*num], strings, len);
272 free_no_errno(strings);
274 strings = (char *)&ret[*num];
275 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
276 ret[(*num)++] = p;
277 return ret;
278 }
280 /* Get the value of a single file, nul terminated.
281 * Returns a malloced value: call free() on it after use.
282 * len indicates length in bytes, not including the nul.
283 */
284 void *xs_read(struct xs_handle *h, const char *path, unsigned int *len)
285 {
286 return xs_single(h, XS_READ, path, len);
287 }
289 /* Write the value of a single file.
290 * Returns false on failure. createflags can be 0, O_CREAT, or O_CREAT|O_EXCL.
291 */
292 bool xs_write(struct xs_handle *h, const char *path,
293 const void *data, unsigned int len, int createflags)
294 {
295 const char *flags;
296 struct iovec iovec[3];
298 /* Format: Flags (as string), path, data. */
299 if (createflags == 0)
300 flags = XS_WRITE_NONE;
301 else if (createflags == O_CREAT)
302 flags = XS_WRITE_CREATE;
303 else if (createflags == (O_CREAT|O_EXCL))
304 flags = XS_WRITE_CREATE_EXCL;
305 else {
306 errno = EINVAL;
307 return false;
308 }
310 iovec[0].iov_base = (void *)path;
311 iovec[0].iov_len = strlen(path) + 1;
312 iovec[1].iov_base = (void *)flags;
313 iovec[1].iov_len = strlen(flags) + 1;
314 iovec[2].iov_base = (void *)data;
315 iovec[2].iov_len = len;
317 return xs_bool(xs_talkv(h, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
318 }
320 /* Create a new directory.
321 * Returns false on failure.
322 */
323 bool xs_mkdir(struct xs_handle *h, const char *path)
324 {
325 return xs_bool(xs_single(h, XS_MKDIR, path, NULL));
326 }
328 /* Destroy a file or directory (directories must be empty).
329 * Returns false on failure.
330 */
331 bool xs_rm(struct xs_handle *h, const char *path)
332 {
333 return xs_bool(xs_single(h, XS_RM, path, NULL));
334 }
336 /* Get permissions of node (first element is owner).
337 * Returns malloced array, or NULL: call free() after use.
338 */
339 struct xs_permissions *xs_get_permissions(struct xs_handle *h,
340 const char *path, unsigned int *num)
341 {
342 char *strings;
343 unsigned int len;
344 struct xs_permissions *ret;
346 strings = xs_single(h, XS_GET_PERMS, path, &len);
347 if (!strings)
348 return NULL;
350 /* Count the strings: each one perms then domid. */
351 *num = xs_count_strings(strings, len);
353 /* Transfer to one big alloc for easy freeing. */
354 ret = malloc(*num * sizeof(struct xs_permissions));
355 if (!ret) {
356 free_no_errno(strings);
357 return NULL;
358 }
360 if (!xs_strings_to_perms(ret, *num, strings)) {
361 free_no_errno(ret);
362 ret = NULL;
363 }
365 free(strings);
366 return ret;
367 }
369 /* Set permissions of node (must be owner).
370 * Returns false on failure.
371 */
372 bool xs_set_permissions(struct xs_handle *h, const char *path,
373 struct xs_permissions *perms,
374 unsigned int num_perms)
375 {
376 unsigned int i;
377 struct iovec iov[1+num_perms];
379 iov[0].iov_base = (void *)path;
380 iov[0].iov_len = strlen(path) + 1;
382 for (i = 0; i < num_perms; i++) {
383 char buffer[MAX_STRLEN(domid_t)+1];
385 if (!xs_perm_to_string(&perms[i], buffer))
386 goto unwind;
388 iov[i+1].iov_base = strdup(buffer);
389 iov[i+1].iov_len = strlen(buffer) + 1;
390 if (!iov[i+1].iov_base)
391 goto unwind;
392 }
394 if (!xs_bool(xs_talkv(h, XS_SET_PERMS, iov, 1+num_perms, NULL)))
395 goto unwind;
396 for (i = 0; i < num_perms; i++)
397 free(iov[i+1].iov_base);
398 return true;
400 unwind:
401 num_perms = i;
402 for (i = 0; i < num_perms; i++)
403 free_no_errno(iov[i+1].iov_base);
404 return false;
405 }
407 /* Watch a node for changes (poll on fd to detect, or call read_watch()).
408 * When the node (or any child) changes, fd will become readable.
409 * Token is returned when watch is read, to allow matching.
410 * Returns false on failure.
411 */
412 bool xs_watch(struct xs_handle *h, const char *path, const char *token)
413 {
414 struct iovec iov[2];
416 iov[0].iov_base = (void *)path;
417 iov[0].iov_len = strlen(path) + 1;
418 iov[1].iov_base = (void *)token;
419 iov[1].iov_len = strlen(token) + 1;
421 return xs_bool(xs_talkv(h, XS_WATCH, iov, ARRAY_SIZE(iov), NULL));
422 }
424 /* Find out what node change was on (will block if nothing pending).
425 * Returns array of two pointers: path and token, or NULL.
426 * Call free() after use.
427 */
428 char **xs_read_watch(struct xs_handle *h)
429 {
430 struct xsd_sockmsg msg;
431 char **ret;
433 if (!read_all(h->fd, &msg, sizeof(msg)))
434 return NULL;
436 assert(msg.type == XS_WATCH_EVENT);
437 ret = malloc(sizeof(char *)*2 + msg.len);
438 if (!ret)
439 return NULL;
441 ret[0] = (char *)(ret + 2);
442 if (!read_all(h->fd, ret[0], msg.len)) {
443 free_no_errno(ret);
444 return NULL;
445 }
446 ret[1] = ret[0] + strlen(ret[0]) + 1;
447 return ret;
448 }
450 /* Acknowledge watch on node. Watches must be acknowledged before
451 * any other watches can be read.
452 * Returns false on failure.
453 */
454 bool xs_acknowledge_watch(struct xs_handle *h, const char *token)
455 {
456 return xs_bool(xs_single(h, XS_WATCH_ACK, token, NULL));
457 }
459 /* Remove a watch on a node.
460 * Returns false on failure (no watch on that node).
461 */
462 bool xs_unwatch(struct xs_handle *h, const char *path, const char *token)
463 {
464 struct iovec iov[2];
466 iov[0].iov_base = (char *)path;
467 iov[0].iov_len = strlen(path) + 1;
468 iov[1].iov_base = (char *)token;
469 iov[1].iov_len = strlen(token) + 1;
471 return xs_bool(xs_talkv(h, XS_UNWATCH, iov, ARRAY_SIZE(iov), NULL));
472 }
474 /* Start a transaction: changes by others will not be seen during this
475 * transaction, and changes will not be visible to others until end.
476 * Transaction only applies to the given subtree.
477 * You can only have one transaction at any time.
478 * Returns false on failure.
479 */
480 bool xs_transaction_start(struct xs_handle *h, const char *subtree)
481 {
482 return xs_bool(xs_single(h, XS_TRANSACTION_START, subtree, NULL));
483 }
485 /* End a transaction.
486 * If abandon is true, transaction is discarded instead of committed.
487 * Returns false on failure, which indicates an error: transactions will
488 * not fail spuriously.
489 */
490 bool xs_transaction_end(struct xs_handle *h, bool abort)
491 {
492 char abortstr[2];
494 if (abort)
495 strcpy(abortstr, "F");
496 else
497 strcpy(abortstr, "T");
498 return xs_bool(xs_single(h, XS_TRANSACTION_END, abortstr, NULL));
499 }
501 /* Introduce a new domain.
502 * This tells the store daemon about a shared memory page and event channel
503 * associated with a domain: the domain uses these to communicate.
504 */
505 bool xs_introduce_domain(struct xs_handle *h, domid_t domid, unsigned long mfn,
506 unsigned int eventchn, const char *path)
507 {
508 char domid_str[MAX_STRLEN(domid)];
509 char mfn_str[MAX_STRLEN(mfn)];
510 char eventchn_str[MAX_STRLEN(eventchn)];
511 struct iovec iov[4];
513 sprintf(domid_str, "%u", domid);
514 sprintf(mfn_str, "%lu", mfn);
515 sprintf(eventchn_str, "%u", eventchn);
517 iov[0].iov_base = domid_str;
518 iov[0].iov_len = strlen(domid_str) + 1;
519 iov[1].iov_base = mfn_str;
520 iov[1].iov_len = strlen(mfn_str) + 1;
521 iov[2].iov_base = eventchn_str;
522 iov[2].iov_len = strlen(eventchn_str) + 1;
523 iov[3].iov_base = (char *)path;
524 iov[3].iov_len = strlen(path) + 1;
526 return xs_bool(xs_talkv(h, XS_INTRODUCE, iov, ARRAY_SIZE(iov), NULL));
527 }
529 bool xs_release_domain(struct xs_handle *h, domid_t domid)
530 {
531 char domid_str[MAX_STRLEN(domid)];
533 sprintf(domid_str, "%u", domid);
535 return xs_bool(xs_single(h, XS_RELEASE, domid_str, NULL));
536 }
538 bool xs_shutdown(struct xs_handle *h)
539 {
540 bool ret = xs_bool(xs_single(h, XS_SHUTDOWN, "", NULL));
541 if (ret) {
542 char c;
543 /* Wait for it to actually shutdown. */
544 read(h->fd, &c, 1);
545 }
546 return ret;
547 }
549 /* Only useful for DEBUG versions */
550 char *xs_debug_command(struct xs_handle *h, const char *cmd,
551 void *data, unsigned int len)
552 {
553 struct iovec iov[2];
555 iov[0].iov_base = (void *)cmd;
556 iov[0].iov_len = strlen(cmd) + 1;
557 iov[1].iov_base = data;
558 iov[1].iov_len = len;
560 return xs_talkv(h, XS_DEBUG, iov, ARRAY_SIZE(iov), NULL);
561 }