xen-vtx-unstable

view tools/xenstore/xs.c @ 6749:80afc502461b

Change xenbus_dev interface from ioctl to read/write.
Check boundaries so we can recover if userspace dies.
Also simplifies libxenstore.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Mon Sep 12 21:12:16 2005 +0000 (2005-09-12)
parents d4d69c509371
children d8637529daff
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 <sys/ioctl.h>
35 #include "xs.h"
36 #include "xenstored.h"
37 #include "xs_lib.h"
38 #include "utils.h"
40 struct xs_handle
41 {
42 int fd;
43 };
45 /* Get the socket from the store daemon handle.
46 */
47 int xs_fileno(struct xs_handle *h)
48 {
49 return h->fd;
50 }
52 static struct xs_handle *get_socket(const char *connect_to)
53 {
54 struct sockaddr_un addr;
55 int sock, saved_errno;
56 struct xs_handle *h = NULL;
58 sock = socket(PF_UNIX, SOCK_STREAM, 0);
59 if (sock < 0)
60 return NULL;
62 addr.sun_family = AF_UNIX;
63 strcpy(addr.sun_path, connect_to);
65 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
66 h = malloc(sizeof(*h));
67 if (h) {
68 h->fd = sock;
69 return h;
70 }
71 }
73 saved_errno = errno;
74 close(sock);
75 errno = saved_errno;
76 return NULL;
77 }
79 static struct xs_handle *get_dev(const char *connect_to)
80 {
81 int fd, saved_errno;
82 struct xs_handle *h;
84 fd = open(connect_to, O_RDWR);
85 if (fd < 0)
86 return NULL;
88 h = malloc(sizeof(*h));
89 if (h) {
90 h->fd = fd;
91 return h;
92 }
94 saved_errno = errno;
95 close(fd);
96 errno = saved_errno;
97 return NULL;
98 }
100 struct xs_handle *xs_daemon_open(void)
101 {
102 return get_socket(xs_daemon_socket());
103 }
105 struct xs_handle *xs_daemon_open_readonly(void)
106 {
107 return get_socket(xs_daemon_socket_ro());
108 }
110 struct xs_handle *xs_domain_open(void)
111 {
112 return get_dev(xs_domain_dev());
113 }
115 void xs_daemon_close(struct xs_handle *h)
116 {
117 if (h->fd >= 0)
118 close(h->fd);
119 free(h);
120 }
122 static bool read_all(int fd, void *data, unsigned int len)
123 {
124 while (len) {
125 int done;
127 done = read(fd, data, len);
128 if (done < 0) {
129 if (errno == EINTR)
130 continue;
131 return false;
132 }
133 if (done == 0) {
134 /* It closed fd on us? EBADF is appropriate. */
135 errno = EBADF;
136 return false;
137 }
138 data += done;
139 len -= done;
140 }
142 return true;
143 }
145 #ifdef XSTEST
146 #define read_all read_all_choice
147 #define xs_write_all write_all_choice
148 #endif
150 static int get_error(const char *errorstring)
151 {
152 unsigned int i;
154 for (i = 0; !streq(errorstring, xsd_errors[i].errstring); i++)
155 if (i == ARRAY_SIZE(xsd_errors) - 1)
156 return EINVAL;
157 return xsd_errors[i].errnum;
158 }
160 /* Adds extra nul terminator, because we generally (always?) hold strings. */
161 static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len)
162 {
163 struct xsd_sockmsg msg;
164 void *ret;
165 int saved_errno;
167 if (!read_all(fd, &msg, sizeof(msg)))
168 return NULL;
170 ret = malloc(msg.len + 1);
171 if (!ret)
172 return NULL;
174 if (!read_all(fd, ret, msg.len)) {
175 saved_errno = errno;
176 free(ret);
177 errno = saved_errno;
178 return NULL;
179 }
181 *type = msg.type;
182 if (len)
183 *len = msg.len;
184 ((char *)ret)[msg.len] = '\0';
185 return ret;
186 }
188 /* Send message to xs, get malloc'ed reply. NULL and set errno on error. */
189 static void *xs_talkv(struct xs_handle *h, enum xsd_sockmsg_type type,
190 const struct iovec *iovec, unsigned int num_vecs,
191 unsigned int *len)
192 {
193 struct xsd_sockmsg msg;
194 void *ret = NULL;
195 int saved_errno;
196 unsigned int i;
197 struct sigaction ignorepipe, oldact;
199 msg.type = type;
200 msg.len = 0;
201 for (i = 0; i < num_vecs; i++)
202 msg.len += iovec[i].iov_len;
204 ignorepipe.sa_handler = SIG_IGN;
205 sigemptyset(&ignorepipe.sa_mask);
206 ignorepipe.sa_flags = 0;
207 sigaction(SIGPIPE, &ignorepipe, &oldact);
209 if (!xs_write_all(h->fd, &msg, sizeof(msg)))
210 goto fail;
212 for (i = 0; i < num_vecs; i++)
213 if (!xs_write_all(h->fd, iovec[i].iov_base, iovec[i].iov_len))
214 goto fail;
216 /* Watches can have fired before reply comes: daemon detects
217 * and re-transmits, so we can ignore this. */
218 do {
219 free(ret);
220 ret = read_reply(h->fd, &msg.type, len);
221 if (!ret)
222 goto fail;
223 } while (msg.type == XS_WATCH_EVENT);
225 sigaction(SIGPIPE, &oldact, NULL);
226 if (msg.type == XS_ERROR) {
227 saved_errno = get_error(ret);
228 free(ret);
229 errno = saved_errno;
230 return NULL;
231 }
233 if (msg.type != type) {
234 free(ret);
235 saved_errno = EBADF;
236 goto close_fd;
238 }
239 return ret;
241 fail:
242 /* We're in a bad state, so close fd. */
243 saved_errno = errno;
244 sigaction(SIGPIPE, &oldact, NULL);
245 close_fd:
246 close(h->fd);
247 h->fd = -1;
248 errno = saved_errno;
249 return NULL;
250 }
252 /* free(), but don't change errno. */
253 static void free_no_errno(void *p)
254 {
255 int saved_errno = errno;
256 free(p);
257 errno = saved_errno;
258 }
260 /* Simplified version of xs_talkv: single message. */
261 static void *xs_single(struct xs_handle *h, enum xsd_sockmsg_type type,
262 const char *string, unsigned int *len)
263 {
264 struct iovec iovec;
266 iovec.iov_base = (void *)string;
267 iovec.iov_len = strlen(string) + 1;
268 return xs_talkv(h, type, &iovec, 1, len);
269 }
271 static bool xs_bool(char *reply)
272 {
273 if (!reply)
274 return false;
275 free(reply);
276 return true;
277 }
279 char **xs_directory(struct xs_handle *h, const char *path, unsigned int *num)
280 {
281 char *strings, *p, **ret;
282 unsigned int len;
284 strings = xs_single(h, XS_DIRECTORY, path, &len);
285 if (!strings)
286 return NULL;
288 /* Count the strings. */
289 *num = xs_count_strings(strings, len);
291 /* Transfer to one big alloc for easy freeing. */
292 ret = malloc(*num * sizeof(char *) + len);
293 if (!ret) {
294 free_no_errno(strings);
295 return NULL;
296 }
297 memcpy(&ret[*num], strings, len);
298 free_no_errno(strings);
300 strings = (char *)&ret[*num];
301 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
302 ret[(*num)++] = p;
303 return ret;
304 }
306 /* Get the value of a single file, nul terminated.
307 * Returns a malloced value: call free() on it after use.
308 * len indicates length in bytes, not including the nul.
309 */
310 void *xs_read(struct xs_handle *h, const char *path, unsigned int *len)
311 {
312 return xs_single(h, XS_READ, path, len);
313 }
315 /* Write the value of a single file.
316 * Returns false on failure. createflags can be 0, O_CREAT, or O_CREAT|O_EXCL.
317 */
318 bool xs_write(struct xs_handle *h, const char *path,
319 const void *data, unsigned int len, int createflags)
320 {
321 const char *flags;
322 struct iovec iovec[3];
324 /* Format: Flags (as string), path, data. */
325 if (createflags == 0)
326 flags = XS_WRITE_NONE;
327 else if (createflags == O_CREAT)
328 flags = XS_WRITE_CREATE;
329 else if (createflags == (O_CREAT|O_EXCL))
330 flags = XS_WRITE_CREATE_EXCL;
331 else {
332 errno = EINVAL;
333 return false;
334 }
336 iovec[0].iov_base = (void *)path;
337 iovec[0].iov_len = strlen(path) + 1;
338 iovec[1].iov_base = (void *)flags;
339 iovec[1].iov_len = strlen(flags) + 1;
340 iovec[2].iov_base = (void *)data;
341 iovec[2].iov_len = len;
343 return xs_bool(xs_talkv(h, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
344 }
346 /* Create a new directory.
347 * Returns false on failure.
348 */
349 bool xs_mkdir(struct xs_handle *h, const char *path)
350 {
351 return xs_bool(xs_single(h, XS_MKDIR, path, NULL));
352 }
354 /* Destroy a file or directory (directories must be empty).
355 * Returns false on failure.
356 */
357 bool xs_rm(struct xs_handle *h, const char *path)
358 {
359 return xs_bool(xs_single(h, XS_RM, path, NULL));
360 }
362 /* Get permissions of node (first element is owner).
363 * Returns malloced array, or NULL: call free() after use.
364 */
365 struct xs_permissions *xs_get_permissions(struct xs_handle *h,
366 const char *path, unsigned int *num)
367 {
368 char *strings;
369 unsigned int len;
370 struct xs_permissions *ret;
372 strings = xs_single(h, XS_GET_PERMS, path, &len);
373 if (!strings)
374 return NULL;
376 /* Count the strings: each one perms then domid. */
377 *num = xs_count_strings(strings, len);
379 /* Transfer to one big alloc for easy freeing. */
380 ret = malloc(*num * sizeof(struct xs_permissions));
381 if (!ret) {
382 free_no_errno(strings);
383 return NULL;
384 }
386 if (!xs_strings_to_perms(ret, *num, strings)) {
387 free_no_errno(ret);
388 ret = NULL;
389 }
391 free(strings);
392 return ret;
393 }
395 /* Set permissions of node (must be owner).
396 * Returns false on failure.
397 */
398 bool xs_set_permissions(struct xs_handle *h, const char *path,
399 struct xs_permissions *perms,
400 unsigned int num_perms)
401 {
402 unsigned int i;
403 struct iovec iov[1+num_perms];
405 iov[0].iov_base = (void *)path;
406 iov[0].iov_len = strlen(path) + 1;
408 for (i = 0; i < num_perms; i++) {
409 char buffer[MAX_STRLEN(domid_t)+1];
411 if (!xs_perm_to_string(&perms[i], buffer))
412 goto unwind;
414 iov[i+1].iov_base = strdup(buffer);
415 iov[i+1].iov_len = strlen(buffer) + 1;
416 if (!iov[i+1].iov_base)
417 goto unwind;
418 }
420 if (!xs_bool(xs_talkv(h, XS_SET_PERMS, iov, 1+num_perms, NULL)))
421 goto unwind;
422 for (i = 0; i < num_perms; i++)
423 free(iov[i+1].iov_base);
424 return true;
426 unwind:
427 num_perms = i;
428 for (i = 0; i < num_perms; i++)
429 free_no_errno(iov[i+1].iov_base);
430 return false;
431 }
433 /* Watch a node for changes (poll on fd to detect, or call read_watch()).
434 * When the node (or any child) changes, fd will become readable.
435 * Token is returned when watch is read, to allow matching.
436 * Returns false on failure.
437 */
438 bool xs_watch(struct xs_handle *h, const char *path, const char *token)
439 {
440 struct iovec iov[2];
442 iov[0].iov_base = (void *)path;
443 iov[0].iov_len = strlen(path) + 1;
444 iov[1].iov_base = (void *)token;
445 iov[1].iov_len = strlen(token) + 1;
447 return xs_bool(xs_talkv(h, XS_WATCH, iov, ARRAY_SIZE(iov), NULL));
448 }
450 /* Find out what node change was on (will block if nothing pending).
451 * Returns array of two pointers: path and token, or NULL.
452 * Call free() after use.
453 */
454 char **xs_read_watch(struct xs_handle *h)
455 {
456 struct xsd_sockmsg msg;
457 char **ret;
459 if (!read_all(h->fd, &msg, sizeof(msg)))
460 return NULL;
462 assert(msg.type == XS_WATCH_EVENT);
463 ret = malloc(sizeof(char *)*2 + msg.len);
464 if (!ret)
465 return NULL;
467 ret[0] = (char *)(ret + 2);
468 if (!read_all(h->fd, ret[0], msg.len)) {
469 free_no_errno(ret);
470 return NULL;
471 }
472 ret[1] = ret[0] + strlen(ret[0]) + 1;
473 return ret;
474 }
476 /* Acknowledge watch on node. Watches must be acknowledged before
477 * any other watches can be read.
478 * Returns false on failure.
479 */
480 bool xs_acknowledge_watch(struct xs_handle *h, const char *token)
481 {
482 return xs_bool(xs_single(h, XS_WATCH_ACK, token, NULL));
483 }
485 /* Remove a watch on a node.
486 * Returns false on failure (no watch on that node).
487 */
488 bool xs_unwatch(struct xs_handle *h, const char *path, const char *token)
489 {
490 struct iovec iov[2];
492 iov[0].iov_base = (char *)path;
493 iov[0].iov_len = strlen(path) + 1;
494 iov[1].iov_base = (char *)token;
495 iov[1].iov_len = strlen(token) + 1;
497 return xs_bool(xs_talkv(h, XS_UNWATCH, iov, ARRAY_SIZE(iov), NULL));
498 }
500 /* Start a transaction: changes by others will not be seen during this
501 * transaction, and changes will not be visible to others until end.
502 * Transaction only applies to the given subtree.
503 * You can only have one transaction at any time.
504 * Returns false on failure.
505 */
506 bool xs_transaction_start(struct xs_handle *h, const char *subtree)
507 {
508 return xs_bool(xs_single(h, XS_TRANSACTION_START, subtree, NULL));
509 }
511 /* End a transaction.
512 * If abandon is true, transaction is discarded instead of committed.
513 * Returns false on failure, which indicates an error: transactions will
514 * not fail spuriously.
515 */
516 bool xs_transaction_end(struct xs_handle *h, bool abort)
517 {
518 char abortstr[2];
520 if (abort)
521 strcpy(abortstr, "F");
522 else
523 strcpy(abortstr, "T");
524 return xs_bool(xs_single(h, XS_TRANSACTION_END, abortstr, NULL));
525 }
527 /* Introduce a new domain.
528 * This tells the store daemon about a shared memory page and event channel
529 * associated with a domain: the domain uses these to communicate.
530 */
531 bool xs_introduce_domain(struct xs_handle *h, domid_t domid, unsigned long mfn,
532 unsigned int eventchn, const char *path)
533 {
534 char domid_str[MAX_STRLEN(domid)];
535 char mfn_str[MAX_STRLEN(mfn)];
536 char eventchn_str[MAX_STRLEN(eventchn)];
537 struct iovec iov[4];
539 sprintf(domid_str, "%u", domid);
540 sprintf(mfn_str, "%lu", mfn);
541 sprintf(eventchn_str, "%u", eventchn);
543 iov[0].iov_base = domid_str;
544 iov[0].iov_len = strlen(domid_str) + 1;
545 iov[1].iov_base = mfn_str;
546 iov[1].iov_len = strlen(mfn_str) + 1;
547 iov[2].iov_base = eventchn_str;
548 iov[2].iov_len = strlen(eventchn_str) + 1;
549 iov[3].iov_base = (char *)path;
550 iov[3].iov_len = strlen(path) + 1;
552 return xs_bool(xs_talkv(h, XS_INTRODUCE, iov, ARRAY_SIZE(iov), NULL));
553 }
555 bool xs_release_domain(struct xs_handle *h, domid_t domid)
556 {
557 char domid_str[MAX_STRLEN(domid)];
559 sprintf(domid_str, "%u", domid);
561 return xs_bool(xs_single(h, XS_RELEASE, domid_str, NULL));
562 }
564 char *xs_get_domain_path(struct xs_handle *h, domid_t domid)
565 {
566 char domid_str[MAX_STRLEN(domid)];
568 sprintf(domid_str, "%u", domid);
570 return xs_single(h, XS_GET_DOMAIN_PATH, domid_str, NULL);
571 }
573 bool xs_shutdown(struct xs_handle *h)
574 {
575 bool ret = xs_bool(xs_single(h, XS_SHUTDOWN, "", NULL));
576 if (ret) {
577 char c;
578 /* Wait for it to actually shutdown. */
579 while ((read(h->fd, &c, 1) < 0) && (errno == EINTR))
580 continue;
581 }
582 return ret;
583 }
585 /* Only useful for DEBUG versions */
586 char *xs_debug_command(struct xs_handle *h, const char *cmd,
587 void *data, unsigned int len)
588 {
589 struct iovec iov[2];
591 iov[0].iov_base = (void *)cmd;
592 iov[0].iov_len = strlen(cmd) + 1;
593 iov[1].iov_base = data;
594 iov[1].iov_len = len;
596 return xs_talkv(h, XS_DEBUG, iov, ARRAY_SIZE(iov), NULL);
597 }