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) {
213         if (errno != ENOENT) {
214             LOGE(ERROR, "xenstore read failed: `%s'", path);
215             return ERROR_FAIL;
216         }
217     }
218     *result_out = result;
219     return 0;
220 }
221 
libxl__xs_write_checked(libxl__gc * gc,xs_transaction_t t,const char * path,const char * string)222 int libxl__xs_write_checked(libxl__gc *gc, xs_transaction_t t,
223                             const char *path, const char *string)
224 {
225     size_t length = strlen(string);
226     if (!xs_write(CTX->xsh, t, path, string, length)) {
227         LOGE(ERROR, "xenstore write failed: `%s' = `%s'", path, string);
228         return ERROR_FAIL;
229     }
230     return 0;
231 }
232 
libxl__xs_rm_checked(libxl__gc * gc,xs_transaction_t t,const char * path)233 int libxl__xs_rm_checked(libxl__gc *gc, xs_transaction_t t, const char *path)
234 {
235     if (!xs_rm(CTX->xsh, t, path)) {
236         if (errno == ENOENT)
237             return 0;
238 
239         LOGE(ERROR, "xenstore rm failed: `%s'", path);
240         return ERROR_FAIL;
241     }
242     return 0;
243 }
244 
libxl__xs_transaction_start(libxl__gc * gc,xs_transaction_t * t)245 int libxl__xs_transaction_start(libxl__gc *gc, xs_transaction_t *t)
246 {
247     assert(!*t);
248     *t = xs_transaction_start(CTX->xsh);
249     if (!*t) {
250         LOGE(ERROR, "could not create xenstore transaction");
251         return ERROR_FAIL;
252     }
253     return 0;
254 }
255 
libxl__xs_transaction_commit(libxl__gc * gc,xs_transaction_t * t)256 int libxl__xs_transaction_commit(libxl__gc *gc, xs_transaction_t *t)
257 {
258     assert(*t);
259 
260     if (!xs_transaction_end(CTX->xsh, *t, 0)) {
261         *t = 0;
262         if (errno == EAGAIN)
263             return +1;
264 
265         LOGE(ERROR, "could not commit xenstore transaction");
266         return ERROR_FAIL;
267     }
268 
269     *t = 0;
270     return 0;
271 }
272 
libxl__xs_transaction_abort(libxl__gc * gc,xs_transaction_t * t)273 void libxl__xs_transaction_abort(libxl__gc *gc, xs_transaction_t *t)
274 {
275     if (!*t)
276         return;
277 
278     if (!xs_transaction_end(CTX->xsh, *t, 1))
279         LOGE(ERROR, "could not abort xenstore transaction");
280 
281     *t = 0;
282 }
283 
libxl__xs_path_cleanup(libxl__gc * gc,xs_transaction_t t,const char * user_path)284 int libxl__xs_path_cleanup(libxl__gc *gc, xs_transaction_t t,
285                            const char *user_path)
286 {
287     unsigned int nb = 0;
288     char *path, *last, *val;
289     int rc;
290 
291     /* A path and transaction must be provided by the caller */
292     assert(user_path && t);
293 
294     path = libxl__strdup(gc, user_path);
295     if (!xs_rm(CTX->xsh, t, path)) {
296         if (errno != ENOENT)
297             LOGE(DEBUG, "unable to remove path %s", path);
298         rc = ERROR_FAIL;
299         goto out;
300     }
301 
302     for (last = strrchr(path, '/'); last != NULL; last = strrchr(path, '/')) {
303         *last = '\0';
304 
305         if (!strlen(path)) break;
306 
307         val = libxl__xs_read(gc, t, path);
308         if (!val || strlen(val) != 0) break;
309 
310         if (!libxl__xs_directory(gc, t, path, &nb) || nb != 0) break;
311 
312         if (!xs_rm(CTX->xsh, t, path)) {
313             if (errno != ENOENT)
314                 LOGE(DEBUG, "unable to remove path %s", path);
315             rc = ERROR_FAIL;
316             goto out;
317         }
318     }
319     rc = 0;
320 
321 out:
322     return rc;
323 }
324 
325 /*
326  * Local variables:
327  * mode: C
328  * c-basic-offset: 4
329  * indent-tabs-mode: nil
330  * End:
331  */
332