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