1 /*
2  * Copyright (C) 2009      Citrix Ltd.
3  * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; version 2.1 only. with the special
8  * exception on linking described in file LICENSE.
9  *
10  * This program 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
13  * GNU Lesser General Public License for more details.
14  */
15 
16 #include "libxl_osdeps.h" /* must come before any other headers */
17 
18 #include "libxl_internal.h"
19 
libxl__xs_kvs_of_flexarray(libxl__gc * gc,flexarray_t * array)20 char **libxl__xs_kvs_of_flexarray(libxl__gc *gc, flexarray_t *array)
21 {
22     char **kvs;
23     int i, length;
24 
25     if (!array)
26         return NULL;
27 
28     length = array->count;
29     if (!length)
30         return NULL;
31 
32     kvs = libxl__calloc(gc, length + 2, sizeof(char *));
33     if (kvs) {
34         for (i = 0; i < length; i += 2) {
35             void *ptr;
36 
37             flexarray_get(array, i, &ptr);
38             kvs[i] = (char *) ptr;
39             flexarray_get(array, i + 1, &ptr);
40             kvs[i + 1] = (char *) ptr;
41         }
42         kvs[i] = NULL;
43         kvs[i + 1] = NULL;
44     }
45     return kvs;
46 }
47 
libxl__xs_writev_perms(libxl__gc * gc,xs_transaction_t t,const char * dir,char * kvs[],struct xs_permissions * perms,unsigned int num_perms)48 int libxl__xs_writev_perms(libxl__gc *gc, xs_transaction_t t,
49                            const char *dir, char *kvs[],
50                            struct xs_permissions *perms,
51                            unsigned int num_perms)
52 {
53     libxl_ctx *ctx = libxl__gc_owner(gc);
54     char *path;
55     int i;
56 
57     if (!kvs)
58         return 0;
59 
60     for (i = 0; kvs[i] != NULL; i += 2) {
61         path = GCSPRINTF("%s/%s", dir, kvs[i]);
62         if (path && kvs[i + 1]) {
63             int length = strlen(kvs[i + 1]);
64             xs_write(ctx->xsh, t, path, kvs[i + 1], length);
65             if (perms)
66                 xs_set_permissions(ctx->xsh, t, path, perms, num_perms);
67         }
68     }
69     return 0;
70 }
71 
libxl__xs_writev(libxl__gc * gc,xs_transaction_t t,const char * dir,char * kvs[])72 int libxl__xs_writev(libxl__gc *gc, xs_transaction_t t,
73                      const char *dir, char *kvs[])
74 {
75     return libxl__xs_writev_perms(gc, t, dir, kvs, NULL, 0);
76 }
77 
libxl__xs_writev_atonce(libxl__gc * gc,const char * dir,char * kvs[])78 int libxl__xs_writev_atonce(libxl__gc *gc,
79                             const char *dir, char *kvs[])
80 {
81     int rc;
82     xs_transaction_t t = XBT_NULL;
83 
84     for (;;) {
85         rc = libxl__xs_transaction_start(gc, &t);
86         if (rc) goto out;
87 
88         rc = libxl__xs_writev(gc, t, dir, kvs);
89         if (rc) goto out;
90 
91         rc = libxl__xs_transaction_commit(gc, &t);
92         if (!rc) break;
93         if (rc<0) goto out;
94     }
95 
96 out:
97     libxl__xs_transaction_abort(gc, &t);
98 
99     return rc;
100 
101 }
102 
libxl__xs_vprintf(libxl__gc * gc,xs_transaction_t t,const char * path,const char * fmt,va_list ap)103 int libxl__xs_vprintf(libxl__gc *gc, xs_transaction_t t,
104                       const char *path, const char *fmt, va_list ap)
105 {
106     libxl_ctx *ctx = libxl__gc_owner(gc);
107     char *s;
108     bool ok;
109 
110     s = libxl__vsprintf(gc, fmt, ap);
111 
112     ok = xs_write(ctx->xsh, t, path, s, strlen(s));
113     if (!ok) {
114         LOGE(ERROR, "xenstore write failed: `%s' = `%s'", path, s);
115         return ERROR_FAIL;
116     }
117 
118     return 0;
119 }
120 
libxl__xs_printf(libxl__gc * gc,xs_transaction_t t,const char * path,const char * fmt,...)121 int libxl__xs_printf(libxl__gc *gc, xs_transaction_t t,
122                      const char *path, const char *fmt, ...)
123 {
124     va_list ap;
125     int rc;
126 
127     va_start(ap, fmt);
128     rc = libxl__xs_vprintf(gc, t, path, fmt, ap);
129     va_end(ap);
130 
131     return rc;
132 }
133 
libxl__xs_read(libxl__gc * gc,xs_transaction_t t,const char * path)134 char * libxl__xs_read(libxl__gc *gc, xs_transaction_t t, const char *path)
135 {
136     libxl_ctx *ctx = libxl__gc_owner(gc);
137     char *ptr;
138 
139     ptr = xs_read(ctx->xsh, t, path, NULL);
140     libxl__ptr_add(gc, ptr);
141     return ptr;
142 }
143 
libxl__xs_get_dompath(libxl__gc * gc,uint32_t domid)144 char *libxl__xs_get_dompath(libxl__gc *gc, uint32_t domid)
145 {
146     libxl_ctx *ctx = libxl__gc_owner(gc);
147     char *s = xs_get_domain_path(ctx->xsh, domid);
148     if (!s) {
149         LOGED(ERROR, domid, "Failed to get dompath");
150         return NULL;
151     }
152     libxl__ptr_add(gc, s);
153     return s;
154 }
155 
libxl__xs_directory(libxl__gc * gc,xs_transaction_t t,const char * path,unsigned int * nb)156 char **libxl__xs_directory(libxl__gc *gc, xs_transaction_t t,
157                            const char *path, unsigned int *nb)
158 {
159     libxl_ctx *ctx = libxl__gc_owner(gc);
160     char **ret = NULL;
161     ret = xs_directory(ctx->xsh, t, path, nb);
162     libxl__ptr_add(gc, ret);
163     return ret;
164 }
165 
libxl__xs_mknod(libxl__gc * gc,xs_transaction_t t,const char * path,struct xs_permissions * perms,unsigned int num_perms)166 int libxl__xs_mknod(libxl__gc *gc, xs_transaction_t t,
167                     const char *path, struct xs_permissions *perms,
168                     unsigned int num_perms)
169 {
170     libxl_ctx *ctx = libxl__gc_owner(gc);
171     bool ok;
172 
173     ok = xs_write(ctx->xsh, t, path, "", 0);
174     if (!ok) {
175         LOGE(ERROR, "xenstore write failed: `%s' = ''", path);
176         return ERROR_FAIL;
177     }
178 
179     ok = xs_set_permissions(ctx->xsh, t, path, perms, num_perms);
180     if (!ok) {
181         LOGE(ERROR, "xenstore set permissions failed on `%s'", path);
182         return ERROR_FAIL;
183     }
184 
185     return 0;
186 }
187 
libxl__xs_libxl_path(libxl__gc * gc,uint32_t domid)188 char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
189 {
190     char *s = GCSPRINTF("/libxl/%i", domid);
191     if (!s)
192         LOGD(ERROR, domid, "cannot allocate create paths");
193     return s;
194 }
195 
libxl__xs_read_mandatory(libxl__gc * gc,xs_transaction_t t,const char * path,const char ** result_out)196 int libxl__xs_read_mandatory(libxl__gc *gc, xs_transaction_t t,
197                              const char *path, const char **result_out)
198 {
199     char *result = libxl__xs_read(gc, t, path);
200     if (!result) {
201         LOGE(ERROR, "xenstore read failed: `%s'", path);
202         return ERROR_FAIL;
203     }
204     *result_out = result;
205     return 0;
206 }
207 
libxl__xs_read_checked(libxl__gc * gc,xs_transaction_t t,const char * path,const char ** result_out)208 int libxl__xs_read_checked(libxl__gc *gc, xs_transaction_t t,
209                            const char *path, const char **result_out)
210 {
211     char *result = libxl__xs_read(gc, t, path);
212     if (!result && errno != ENOENT) {
213         LOGE(ERROR, "xenstore read failed: `%s'", path);
214         return ERROR_FAIL;
215     }
216     *result_out = result;
217     return 0;
218 }
219 
libxl__xs_write_checked(libxl__gc * gc,xs_transaction_t t,const char * path,const char * string)220 int libxl__xs_write_checked(libxl__gc *gc, xs_transaction_t t,
221                             const char *path, const char *string)
222 {
223     size_t length = strlen(string);
224     if (!xs_write(CTX->xsh, t, path, string, length)) {
225         LOGE(ERROR, "xenstore write failed: `%s' = `%s'", path, string);
226         return ERROR_FAIL;
227     }
228     return 0;
229 }
230 
libxl__xs_rm_checked(libxl__gc * gc,xs_transaction_t t,const char * path)231 int libxl__xs_rm_checked(libxl__gc *gc, xs_transaction_t t, const char *path)
232 {
233     if (!xs_rm(CTX->xsh, t, path)) {
234         if (errno == ENOENT)
235             return 0;
236 
237         LOGE(ERROR, "xenstore rm failed: `%s'", path);
238         return ERROR_FAIL;
239     }
240     return 0;
241 }
242 
libxl__xs_transaction_start(libxl__gc * gc,xs_transaction_t * t)243 int libxl__xs_transaction_start(libxl__gc *gc, xs_transaction_t *t)
244 {
245     assert(!*t);
246     *t = xs_transaction_start(CTX->xsh);
247     if (!*t) {
248         LOGE(ERROR, "could not create xenstore transaction");
249         return ERROR_FAIL;
250     }
251     return 0;
252 }
253 
libxl__xs_transaction_commit(libxl__gc * gc,xs_transaction_t * t)254 int libxl__xs_transaction_commit(libxl__gc *gc, xs_transaction_t *t)
255 {
256     assert(*t);
257 
258     if (!xs_transaction_end(CTX->xsh, *t, 0)) {
259         *t = 0;
260         if (errno == EAGAIN)
261             return +1;
262 
263         LOGE(ERROR, "could not commit xenstore transaction");
264         return ERROR_FAIL;
265     }
266 
267     *t = 0;
268     return 0;
269 }
270 
libxl__xs_transaction_abort(libxl__gc * gc,xs_transaction_t * t)271 void libxl__xs_transaction_abort(libxl__gc *gc, xs_transaction_t *t)
272 {
273     if (!*t)
274         return;
275 
276     if (!xs_transaction_end(CTX->xsh, *t, 1))
277         LOGE(ERROR, "could not abort xenstore transaction");
278 
279     *t = 0;
280 }
281 
libxl__xs_path_cleanup(libxl__gc * gc,xs_transaction_t t,const char * user_path)282 int libxl__xs_path_cleanup(libxl__gc *gc, xs_transaction_t t,
283                            const char *user_path)
284 {
285     unsigned int nb = 0;
286     char *path, *last, *val;
287     int rc;
288 
289     /* A path and transaction must be provided by the caller */
290     assert(user_path && t);
291 
292     path = libxl__strdup(gc, user_path);
293     if (!xs_rm(CTX->xsh, t, path)) {
294         if (errno != ENOENT)
295             LOGE(DEBUG, "unable to remove path %s", path);
296         rc = ERROR_FAIL;
297         goto out;
298     }
299 
300     for (last = strrchr(path, '/'); last != NULL; last = strrchr(path, '/')) {
301         *last = '\0';
302 
303         if (!strlen(path)) break;
304 
305         val = libxl__xs_read(gc, t, path);
306         if (!val || strlen(val) != 0) break;
307 
308         if (!libxl__xs_directory(gc, t, path, &nb) || nb != 0) break;
309 
310         if (!xs_rm(CTX->xsh, t, path)) {
311             if (errno != ENOENT)
312                 LOGE(DEBUG, "unable to remove path %s", path);
313             rc = ERROR_FAIL;
314             goto out;
315         }
316     }
317     rc = 0;
318 
319 out:
320     return rc;
321 }
322 
323 /*
324  * Local variables:
325  * mode: C
326  * c-basic-offset: 4
327  * indent-tabs-mode: nil
328  * End:
329  */
330