ia64/xen-unstable

view tools/xenstore/xenstore_client.c @ 7329:74d56b7ff46c

Merged
author djm@kirby.fc.hp.com
date Tue Oct 11 16:57:44 2005 -0600 (2005-10-11)
parents 61b3b357d827 7a48bfd1aba6
children 41c970cf82ba
line source
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 *
8 */
10 #include <err.h>
11 #include <fcntl.h>
12 #include <getopt.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <xs.h>
17 #include <errno.h>
19 static void
20 usage(const char *progname)
21 {
22 #if defined(CLIENT_read)
23 errx(1, "Usage: %s [-h] [-p] [-s] key [...]", progname);
24 #elif defined(CLIENT_write)
25 errx(1, "Usage: %s [-h] [-s] key value [...]", progname);
26 #elif defined(CLIENT_rm) || defined(CLIENT_exists) || defined(CLIENT_list)
27 errx(1, "Usage: %s [-h] [-s] key [...]", progname);
28 #endif
29 }
31 int
32 main(int argc, char **argv)
33 {
34 struct xs_handle *xsh;
35 struct xs_transaction_handle *xth;
36 bool success;
37 int ret = 0, socket = 0;
38 #if defined(CLIENT_read) || defined(CLIENT_list)
39 int prefix = 0;
40 #endif
42 while (1) {
43 int c, index = 0;
44 static struct option long_options[] = {
45 {"help", 0, 0, 'h'},
46 #if defined(CLIENT_read) || defined(CLIENT_list)
47 {"prefix", 0, 0, 'p'},
48 #endif
49 {"socket", 0, 0, 's'},
50 {0, 0, 0, 0}
51 };
53 c = getopt_long(argc, argv, "hs"
54 #if defined(CLIENT_read) || defined(CLIENT_list)
55 "p"
56 #endif
57 , long_options, &index);
58 if (c == -1)
59 break;
61 switch (c) {
62 case 'h':
63 usage(argv[0]);
64 /* NOTREACHED */
65 case 's':
66 socket = 1;
67 break;
68 #if defined(CLIENT_read) || defined(CLIENT_list)
69 case 'p':
70 prefix = 1;
71 break;
72 #endif
73 }
74 }
76 if (optind == argc) {
77 usage(argv[0]);
78 /* NOTREACHED */
79 }
80 #if defined(CLIENT_write)
81 if ((argc - optind) % 2 == 1) {
82 usage(argv[0]);
83 /* NOTREACHED */
84 }
85 #endif
87 xsh = socket ? xs_daemon_open() : xs_domain_open();
88 if (xsh == NULL)
89 err(1, socket ? "xs_daemon_open" : "xs_domain_open");
91 again:
92 xth = xs_transaction_start(xsh);
93 if (xth == NULL)
94 errx(1, "couldn't start transaction");
96 while (optind < argc) {
97 #if defined(CLIENT_read)
98 char *val = xs_read(xsh, xth, argv[optind], NULL);
99 if (val == NULL) {
100 warnx("couldn't read path %s", argv[optind]);
101 ret = 1;
102 goto out;
103 }
104 if (prefix)
105 printf("%s: ", argv[optind]);
106 printf("%s\n", val);
107 free(val);
108 optind++;
109 #elif defined(CLIENT_write)
110 success = xs_write(xsh, xth, argv[optind], argv[optind + 1],
111 strlen(argv[optind + 1]));
112 if (!success) {
113 warnx("could not write path %s", argv[optind]);
114 ret = 1;
115 goto out;
116 }
117 optind += 2;
118 #elif defined(CLIENT_rm)
119 success = xs_rm(xsh, xth, argv[optind]);
120 if (!success) {
121 warnx("could not remove path %s", argv[optind]);
122 ret = 1;
123 goto out;
124 }
125 optind++;
126 #elif defined(CLIENT_exists)
127 char *val = xs_read(xsh, xth, argv[optind], NULL);
128 if (val == NULL) {
129 ret = 1;
130 goto out;
131 }
132 free(val);
133 optind++;
134 #elif defined(CLIENT_list)
135 unsigned int i, num;
136 char **list = xs_directory(xsh, xth, argv[optind], &num);
137 if (list == NULL) {
138 warnx("could not list path %s", argv[optind]);
139 ret = 1;
140 goto out;
141 }
142 for (i = 0; i < num; i++) {
143 if (prefix)
144 printf("%s/", argv[optind]);
145 printf("%s\n", list[i]);
146 }
147 free(list);
148 optind++;
149 #endif
150 }
152 out:
153 success = xs_transaction_end(xsh, xth, ret ? true : false);
154 if (!success) {
155 if (ret == 0 && errno == EAGAIN)
156 goto again;
157 errx(1, "couldn't end transaction");
158 }
159 return ret;
160 }