Debian Codegraph @

tools/xs-clients/xenstore_client.c

raw
1 /*
2 * This file is subject to the terms and conditions of the GNU General
3 * Public License. See the file "COPYING" in the main directory of
4 * this archive for more details.
5 *
6 * Copyright (C) 2005 by Christian Limpach
7 * Copyright (C) 2005 XenSource Ltd.
8 *
9 */
10
11 #include <assert.h>
12 #include <err.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <getopt.h>
16 #include <stdarg.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <termios.h>
21 #include <unistd.h>
22 #include <xenstore.h>
23
24 #include <sys/ioctl.h>
25
26 #include <xen-tools/xenstore-common.h>
27
28 #define PATH_SEP '/'
29 #define MAX_PATH_LEN 256
30
31
32 enum mode {
33 MODE_unknown,
34 MODE_chmod,
35 MODE_exists,
36 MODE_list,
37 MODE_ls,
38 MODE_read,
39 MODE_rm,
40 MODE_write,
41 MODE_watch,
42 };
43
44 /* Sanitising (quoting) possibly-binary strings. */
45 struct expanding_buffer {
46 char *buf;
47 int avail;
48 };
49
50 static char *output_buf = NULL;
51 static int output_pos = 0;
52 static struct expanding_buffer ebuf;
53
54 static int output_size = 0;
55
56 /* Ensure that given expanding buffer has at least min_avail characters. */
57 static char *expanding_buffer_ensure(struct expanding_buffer *ebuf,
58 int min_avail)
59 {
60 int want;
61 char *got;
62
63 if ( ebuf->avail >= min_avail )
64 return ebuf->buf;
65
66 if ( min_avail >= INT_MAX/3 )
67 return 0;
68
69 want = ebuf->avail + min_avail + 10;
70 got = realloc(ebuf->buf, want);
71 if ( !got )
72 return 0;
73
74 ebuf->buf = got;
75 ebuf->avail = want;
76 return ebuf->buf;
77 }
78
79 /* sanitise_value() may return NULL if malloc fails. */
80 static char *sanitise_value(struct expanding_buffer *ebuf,
81 const char *val, unsigned len)
82 {
83 int used, remain, c;
84 unsigned char *ip;
85
86 #define ADD(c) (ebuf->buf[used++] = (c))
87 #define ADDF(f,c) (used += sprintf(ebuf->buf+used, (f), (c)))
88
89 assert(len < INT_MAX/5);
90
91 ip = (unsigned char *)val;
92 used = 0;
93 remain = len;
94
95 if ( !expanding_buffer_ensure(ebuf, remain + 1) )
96 return NULL;
97
98 while ( remain-- > 0 )
99 {
100 c= *ip++;
101
102 if ( c >= ' ' && c <= '~' && c != '\\' )
103 {
104 ADD(c);
105 continue;
106 }
107
108 if ( !expanding_buffer_ensure(ebuf, used + remain + 5) )
109 /* for "<used>\\nnn<remain>\0" */
110 return 0;
111
112 ADD('\\');
113 switch (c)
114 {
115 case '\t': ADD('t'); break;
116 case '\n': ADD('n'); break;
117 case '\r': ADD('r'); break;
118 case '\\': ADD('\\'); break;
119 default:
120 if ( c < 010 ) ADDF("%03o", c);
121 else ADDF("x%02x", c);
122 }
123 }
124
125 ADD(0);
126 assert(used <= ebuf->avail);
127 return ebuf->buf;
128
129 #undef ADD
130 #undef ADDF
131 }
132
133 /* *out_len_r on entry is ignored; out must be at least strlen(in)+1 bytes. */
134 static void unsanitise_value(char *out, unsigned *out_len_r, const char *in)
135 {
136 const char *ip;
137 char *op;
138 unsigned c;
139 int n;
140
141 for ( ip = in, op = out; (c = *ip++); *op++ = c )
142 {
143 if ( c == '\\' )
144 {
145 c = *ip++;
146
147 #define GETF(f) do \
148 { \
149 n = 0; \
150 sscanf(ip, f "%n", &c, &n); \
151 ip += n; \
152 } while ( 0 )
153
154 switch ( c )
155 {
156 case 't': c= '\t'; break;
157 case 'n': c= '\n'; break;
158 case 'r': c= '\r'; break;
159 case '\\': c= '\\'; break;
160 case 'x': GETF("%2x"); break;
161 case '0': case '4':
162 case '1': case '5':
163 case '2': case '6':
164 case '3': case '7': --ip; GETF("%3o"); break;
165 case 0: --ip; break;
166 default:;
167 }
168 #undef GETF
169 }
170 }
171
172 *op = 0;
173
174 if ( out_len_r )
175 *out_len_r = op - out;
176 }
177
178 /* make sure there is at least 'len' more space in output_buf */
179 static void expand_buffer(size_t len)
180 {
181 if (output_pos + len > output_size) {
182 output_size += len + 1024;
183 output_buf = realloc(output_buf, output_size);
184 if (output_buf == NULL)
185 err(1, "malloc");
186 }
187 }
188
189 static void
190 output(const char *fmt, ...) {
191 va_list ap;
192 int len;
193 char buf[1];
194
195 va_start(ap, fmt);
196 len = vsnprintf(buf, 1, fmt, ap);
197 if (len < 0)
198 err(1, "output");
199 va_end(ap);
200 expand_buffer(len + 1);
201 va_start(ap, fmt);
202 if (vsnprintf(&output_buf[output_pos], len + 1, fmt, ap) != len)
203 err(1, "output");
204 va_end(ap);
205 output_pos += len;
206 }
207
208 static void
209 output_raw(const char *data, int len)
210 {
211 expand_buffer(len);
212 memcpy(&output_buf[output_pos], data, len);
213 output_pos += len;
214 }
215
216 static void
217 usage(enum mode mode, int incl_mode, const char *progname)
218 {
219 const char *mstr = NULL;
220
221 switch (mode) {
222 case MODE_unknown:
223 errx(1, "Usage: %s <mode> [-h] [...]", progname);
224 case MODE_read:
225 mstr = incl_mode ? "read " : "";
226 errx(1, "Usage: %s %s[-h] [-p] [-R] key [...]", progname, mstr);
227 case MODE_write:
228 mstr = incl_mode ? "write " : "";
229 errx(1, "Usage: %s %s[-h] [-R] key value [...]", progname, mstr);
230 case MODE_rm:
231 mstr = incl_mode ? "rm " : "";
232 errx(1, "Usage: %s %s[-h] [-t] key [...]", progname, mstr);
233 case MODE_exists:
234 mstr = incl_mode ? "exists " : "";
235 /* fallthrough */
236 case MODE_list:
237 mstr = mstr ? : incl_mode ? "list " : "";
238 errx(1, "Usage: %s %s[-h] [-p] key [...]", progname, mstr);
239 case MODE_ls:
240 mstr = mstr ? : incl_mode ? "ls " : "";
241 errx(1, "Usage: %s %s[-h] [-f] [-p] [path]", progname, mstr);
242 case MODE_chmod:
243 mstr = incl_mode ? "chmod " : "";
244 errx(1, "Usage: %s %s[-h] [-u] [-r] key <mode [modes...]>", progname, mstr);
245 case MODE_watch:
246 mstr = incl_mode ? "watch " : "";
247 errx(1, "Usage: %s %s[-h] [-n NR] key", progname, mstr);
248 }
249 }
250
251
252 static int
253 do_rm(char *path, struct xs_handle *xsh, xs_transaction_t xth)
254 {
255 if (xs_rm(xsh, xth, path)) {
256 return 0;
257 }
258 else {
259 warnx("could not remove path %s", path);
260 return 1;
261 }
262 }
263
264 #define STRING_MAX XENSTORE_ABS_PATH_MAX+1024
265 static int max_width = 80;
266 static int desired_width = 60;
267 static int show_whole_path = 0;
268
269 #define TAG " = \"...\""
270 #define TAG_LEN strlen(TAG)
271
272 #define MIN(a, b) (((a) < (b))? (a) : (b))
273
274 static void do_ls(struct xs_handle *h, char *path, int cur_depth, int show_perms)
275 {
276 char **e;
277 char *newpath, *val;
278 int newpath_len;
279 int i;
280 unsigned int num, len;
281
282 e = xs_directory(h, XBT_NULL, path, &num);
283 if (e == NULL) {
284 if (cur_depth && errno == ENOENT) {
285 /* If a node disappears while recursing, silently move on. */
286 return;
287 }
288
289 err(1, "xs_directory (%s)", path);
290 }
291
292 newpath = malloc(STRING_MAX);
293 if (!newpath)
294 err(1, "malloc in do_ls");
295
296 for (i = 0; i<num; i++) {
297 char buf[MAX_STRLEN(unsigned int)+1];
298 struct xs_permissions *perms;
299 unsigned int nperms;
300 int linewid;
301
302 /* Compose fullpath */
303 newpath_len = snprintf(newpath, STRING_MAX, "%s%s%s", path,
304 path[strlen(path)-1] == '/' ? "" : "/",
305 e[i]);
306
307 /* Print indent and path basename */
308 linewid = 0;
309 if (show_whole_path) {
310 fputs(newpath, stdout);
311 } else {
312 for (; linewid<cur_depth; linewid++) {
313 putchar(' ');
314 }
315 linewid += printf("%.*s",
316 (int) (max_width - TAG_LEN - linewid), e[i]);
317 }
318
319 /* Fetch value */
320 if ( newpath_len < STRING_MAX ) {
321 val = xs_read(h, XBT_NULL, newpath, &len);
322 }
323 else {
324 /* Path was truncated and thus invalid */
325 val = NULL;
326 len = 0;
327 }
328
329 /* Print value */
330 if (val == NULL) {
331 printf(":\n");
332 }
333 else {
334 if (max_width < (linewid + len + TAG_LEN)) {
335 printf(" = \"%.*s\\...\"",
336 (int)(max_width - TAG_LEN - linewid),
337 sanitise_value(&ebuf, val, len));
338 }
339 else {
340 linewid += printf(" = \"%s\"",
341 sanitise_value(&ebuf, val, len));
342 if (show_perms) {
343 putchar(' ');
344 for (linewid++;
345 linewid < MIN(desired_width, max_width);
346 linewid++)
347 putchar((linewid & 1)? '.' : ' ');
348 }
349 }
350 }
351 free(val);
352
353 if (show_perms) {
354 perms = xs_get_permissions(h, XBT_NULL, newpath, &nperms);
355 if (perms == NULL) {
356 warn("\ncould not access permissions for %s", e[i]);
357 }
358 else {
359 int i;
360 fputs(" (", stdout);
361 for (i = 0; i < nperms; i++) {
362 if (i)
363 putchar(',');
364 xenstore_perm_to_string(perms+i, buf, sizeof(buf));
365 fputs(buf, stdout);
366 }
367 putchar(')');
368 }
369 }
370
371 putchar('\n');
372
373 do_ls(h, newpath, cur_depth+1, show_perms);
374 }
375 free(e);
376 free(newpath);
377 }
378
379 static void
380 do_chmod(char *path, struct xs_permissions *perms, int nperms, int upto,
381 int recurse, struct xs_handle *xsh, xs_transaction_t xth)
382 {
383 int ret;
384
385 if (!path[0])
386 return;
387
388 ret = xs_set_permissions(xsh, xth, path, perms, nperms);
389 if (!ret)
390 err(1, "Error occurred setting permissions on '%s'", path);
391
392 if (upto) {
393 /* apply same permissions to all parent entries: */
394 char *path_sep_ptr = strrchr(path, PATH_SEP);
395 if (!path_sep_ptr)
396 errx(1, "Unable to locate path separator '%c' in '%s'",
397 PATH_SEP, path);
398
399 *path_sep_ptr = '\0'; /* truncate path */
400
401 do_chmod(path, perms, nperms, 1, 0, xsh, xth);
402
403 *path_sep_ptr = PATH_SEP;
404 }
405
406 if (recurse) {
407 char buf[MAX_PATH_LEN];
408
409 /* apply same permissions to all child entries: */
410 unsigned int xsval_n;
411 char **xsval = xs_directory(xsh, xth, path, &xsval_n);
412
413 if (xsval) {
414 int i;
415 for (i = 0; i < xsval_n; i++) {
416 snprintf(buf, MAX_PATH_LEN, "%s/%s", path, xsval[i]);
417
418 do_chmod(buf, perms, nperms, 0, 1, xsh, xth);
419 }
420
421 free(xsval);
422 }
423 }
424 }
425
426 static void
427 do_watch(struct xs_handle *xsh, int max_events)
428 {
429 int count = 0;
430 char **vec = NULL;
431
432 for ( count = 0; max_events == -1 || count < max_events; count++ ) {
433 unsigned int num;
434
435 vec = xs_read_watch(xsh, &num);
436 if (vec == NULL)
437 continue;
438
439 printf("%s\n", vec[XS_WATCH_PATH]);
440 fflush(stdout);
441 free(vec);
442 }
443 }
444
445 static int
446 perform(enum mode mode, int optind, int argc, char **argv, struct xs_handle *xsh,
447 xs_transaction_t xth, int prefix, int tidy, int upto, int recurse, int nr_watches,
448 int raw)
449 {
450 switch (mode) {
451 case MODE_ls:
452 if (optind == argc)
453 {
454 optind=0;
455 argc=1;
456 argv[0] = "/";
457 }
458 break;
459 default:
460 break;
461 }
462
463 while (optind < argc) {
464 switch (mode) {
465 case MODE_unknown:
466 /* CANNOT BE REACHED */
467 errx(1, "invalid mode %d", mode);
468 case MODE_read: {
469 unsigned len;
470 char *val = xs_read(xsh, xth, argv[optind], &len);
471 if (val == NULL) {
472 warnx("couldn't read path %s", argv[optind]);
473 return 1;
474 }
475 if (prefix)
476 output("%s: ", argv[optind]);
477 if (raw)
478 output_raw(val, len);
479 else
480 output("%s\n", sanitise_value(&ebuf, val, len));
481 free(val);
482 optind++;
483 break;
484 }
485 case MODE_write: {
486 char *val_spec = argv[optind + 1];
487 char *val;
488 unsigned len;
489 if (raw) {
490 val = val_spec;
491 len = strlen(val_spec);
492 } else {
493 expanding_buffer_ensure(&ebuf, strlen(val_spec)+1);
494 unsanitise_value(ebuf.buf, &len, val_spec);
495 val = ebuf.buf;
496 }
497 if (!xs_write(xsh, xth, argv[optind], val, len)) {
498 warnx("could not write path %s", argv[optind]);
499 return 1;
500 }
501 optind += 2;
502 } break;
503 case MODE_rm: {
504 /* Remove the specified path. If the tidy flag is set, then also
505 remove any containing directories that are both empty and have no
506 value attached, and repeat, recursing all the way up to the root if
507 necessary.
508 */
509
510 char *slash, *path = argv[optind];
511
512 if (tidy) {
513 /* Copy path, because we can't modify argv because we will need it
514 again if xs_transaction_end gives us EAGAIN. */
515 char *p = malloc(strlen(path) + 1);
516 if (!p)
517 return 1;
518 strcpy(p, path);
519 path = p;
520
521 again:
522 if (do_rm(path, xsh, xth)) {
523 free(path);
524 return 1;
525 }
526
527 slash = strrchr(p, '/');
528 if (slash) {
529 char *val;
530 unsigned len;
531 *slash = '\0';
532 val = xs_read(xsh, xth, p, &len);
533 if (val && len == 0) {
534 unsigned int num;
535 char ** list = xs_directory(xsh, xth, p, &num);
536
537 if (list) {
538 free(list);
539 if (num == 0){
540 free(val);
541 goto again;
542 }
543 }
544 }
545 free(val);
546 }
547
548 free(path);
549 }
550 else {
551 if (do_rm(path, xsh, xth)) {
552 return 1;
553 }
554 }
555
556 optind++;
557 break;
558 }
559 case MODE_exists: {
560 char *val = xs_read(xsh, xth, argv[optind], NULL);
561 if (val == NULL) {
562 return 1;
563 }
564 free(val);
565 optind++;
566 break;
567 }
568 case MODE_list: {
569 unsigned int i, num;
570 char **list = xs_directory(xsh, xth, argv[optind], &num);
571 if (list == NULL) {
572 warnx("could not list path %s", argv[optind]);
573 return 1;
574 }
575 for (i = 0; i < num; i++) {
576 if (prefix)
577 output("%s/", argv[optind]);
578 output("%s\n", list[i]);
579 }
580 free(list);
581 optind++;
582 break;
583 }
584 case MODE_ls: {
585 do_ls(xsh, argv[optind], 0, prefix);
586 optind++;
587 break;
588 }
589 case MODE_chmod: {
590 /* save path pointer: */
591 char *path = argv[optind++];
592 int nperms = argc - optind;
593 struct xs_permissions perms[nperms];
594 int i;
595 for (i = 0; argv[optind]; optind++, i++)
596 {
597 perms[i].id = atoi(argv[optind]+1);
598
599 switch (argv[optind][0])
600 {
601 case 'n':
602 perms[i].perms = XS_PERM_NONE;
603 break;
604 case 'r':
605 perms[i].perms = XS_PERM_READ;
606 break;
607 case 'w':
608 perms[i].perms = XS_PERM_WRITE;
609 break;
610 case 'b':
611 perms[i].perms = XS_PERM_READ | XS_PERM_WRITE;
612 break;
613 default:
614 errx(1, "Invalid permission specification: '%c'",
615 argv[optind][0]);
616 }
617 }
618
619 do_chmod(path, perms, nperms, upto, recurse, xsh, xth);
620 break;
621 }
622 case MODE_watch: {
623 for (; argv[optind]; optind++) {
624 const char *w = argv[optind];
625
626 if (!xs_watch(xsh, w, w))
627 errx(1, "Unable to add watch on %s\n", w);
628 }
629 do_watch(xsh, nr_watches);
630 }
631 }
632 }
633
634 return 0;
635 }
636
637 static enum mode lookup_mode(const char *m)
638 {
639 if (strcmp(m, "read") == 0)
640 return MODE_read;
641 else if (strcmp(m, "chmod") == 0)
642 return MODE_chmod;
643 else if (strcmp(m, "exists") == 0)
644 return MODE_exists;
645 else if (strcmp(m, "list") == 0)
646 return MODE_list;
647 else if (strcmp(m, "ls") == 0)
648 return MODE_ls;
649 else if (strcmp(m, "rm") == 0)
650 return MODE_rm;
651 else if (strcmp(m, "write") == 0)
652 return MODE_write;
653 else if (strcmp(m, "read") == 0)
654 return MODE_read;
655 else if (strcmp(m, "watch") == 0)
656 return MODE_watch;
657
658 errx(1, "unknown mode %s\n", m);
659 return 0;
660 }
661
662 int
663 main(int argc, char **argv)
664 {
665 struct xs_handle *xsh;
666 xs_transaction_t xth = XBT_NULL;
667 int ret = 0;
668 int prefix = 0;
669 int tidy = 0;
670 int upto = 0;
671 int recurse = 0;
672 int nr_watches = -1;
673 int transaction;
674 int raw = 0;
675 struct winsize ws;
676 enum mode mode;
677
678 const char *_command = strrchr(argv[0], '/');
679 const char *command = _command ? &_command[1] : argv[0];
680 int switch_argv = -1; /* which element of argv did we switch on */
681
682 if (strncmp(command, "xenstore-", strlen("xenstore-")) == 0)
683 {
684 switch_argv = 0;
685 command = command + strlen("xenstore-");
686 }
687 else if (argc < 2)
688 usage(MODE_unknown, 0, argv[0]);
689 else
690 {
691 command = argv[1];
692 switch_argv = 1;
693 }
694
695 mode = lookup_mode(command);
696
697 while (1) {
698 int c, index = 0;
699 static struct option long_options[] = {
700 {"help", 0, 0, 'h'},
701 {"flat", 0, 0, 'f'}, /* MODE_ls */
702 {"prefix", 0, 0, 'p'}, /* MODE_read || MODE_list || MODE_ls */
703 {"tidy", 0, 0, 't'}, /* MODE_rm */
704 {"upto", 0, 0, 'u'}, /* MODE_chmod */
705 {"recurse", 0, 0, 'r'}, /* MODE_chmod */
706 {"number", 1, 0, 'n'}, /* MODE_watch */
707 {"raw", 0, 0, 'R'}, /* MODE_read || MODE_write */
708 {0, 0, 0, 0}
709 };
710
711 c = getopt_long(argc - switch_argv, argv + switch_argv, "hfspturn:R",
712 long_options, &index);
713 if (c == -1)
714 break;
715
716 switch (c) {
717 case 'h':
718 usage(mode, switch_argv, argv[0]);
719 /* NOTREACHED */
720 case 'f':
721 if ( mode == MODE_ls ) {
722 max_width = INT_MAX/2;
723 desired_width = 0;
724 show_whole_path = 1;
725 } else {
726 usage(mode, switch_argv, argv[0]);
727 }
728 break;
729 case 'p':
730 if ( mode == MODE_read || mode == MODE_list || mode == MODE_ls )
731 prefix = 1;
732 else
733 usage(mode, switch_argv, argv[0]);
734 break;
735 case 't':
736 if ( mode == MODE_rm )
737 tidy = 1;
738 else
739 usage(mode, switch_argv, argv[0]);
740 break;
741 case 'u':
742 if ( mode == MODE_chmod )
743 upto = 1;
744 else
745 usage(mode, switch_argv, argv[0]);
746 break;
747 case 'r':
748 if ( mode == MODE_chmod )
749 recurse = 1;
750 else
751 usage(mode, switch_argv, argv[0]);
752 break;
753 case 'n':
754 if ( mode == MODE_watch )
755 nr_watches = atoi(optarg);
756 else
757 usage(mode, switch_argv, argv[0]);
758 break;
759 case 'R':
760 if ( mode == MODE_read || mode == MODE_write )
761 raw = 1;
762 else
763 usage(mode, switch_argv, argv[0]);
764 break;
765 }
766 }
767
768 switch (mode) {
769 case MODE_ls:
770 break;
771 case MODE_write:
772 if ((argc - switch_argv - optind) % 2 == 1) {
773 usage(mode, switch_argv, argv[0]);
774 /* NOTREACHED */
775 }
776 /* DROP-THRU */
777 default:
778 if (optind == argc - switch_argv) {
779 usage(mode, switch_argv, argv[0]);
780 /* NOTREACHED */
781 }
782 }
783
784 switch (mode) {
785 case MODE_read:
786 transaction = (argc - switch_argv - optind) > 1;
787 break;
788 case MODE_write:
789 transaction = (argc - switch_argv - optind) > 2;
790 break;
791 case MODE_ls:
792 case MODE_watch:
793 transaction = 0;
794 break;
795 default:
796 transaction = 1;
797 break;
798 }
799
800 if ( mode == MODE_ls )
801 {
802 memset(&ws, 0, sizeof(ws));
803 ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
804 if (!ret)
805 max_width = ws.ws_col - 2;
806 }
807
808 xsh = xs_open(0);
809 if (xsh == NULL) err(1, "xs_open");
810
811 again:
812 if (transaction) {
813 xth = xs_transaction_start(xsh);
814 if (xth == XBT_NULL)
815 errx(1, "couldn't start transaction");
816 }
817
818 ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse, nr_watches, raw);
819
820 if (transaction && !xs_transaction_end(xsh, xth, ret)) {
821 if (ret == 0 && errno == EAGAIN) {
822 output_pos = 0;
823 goto again;
824 }
825 errx(1, "couldn't end transaction");
826 }
827
828 if (output_pos)
829 fwrite(output_buf, 1, output_pos, stdout);
830
831 free(output_buf);
832 free(ebuf.buf);
833
834 if (xsh)
835 xs_close(xsh);
836
837 return ret;
838 }