6e9587b9e0fb0e1abc2108980fcb09d272893d3e
[project/uci.git] / libuci.c
1 /*
2  * libuci - Library for the Unified Configuration Interface
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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 version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 /*
16  * This file contains some common code for the uci library
17  */
18
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <dlfcn.h>
26 #include <glob.h>
27 #include "uci.h"
28
29 static const char *uci_confdir = UCI_CONFDIR;
30 static const char *uci_savedir = UCI_SAVEDIR;
31
32 static const char *uci_errstr[] = {
33         [UCI_OK] =            "Success",
34         [UCI_ERR_MEM] =       "Out of memory",
35         [UCI_ERR_INVAL] =     "Invalid argument",
36         [UCI_ERR_NOTFOUND] =  "Entry not found",
37         [UCI_ERR_IO] =        "I/O error",
38         [UCI_ERR_PARSE] =     "Parse error",
39         [UCI_ERR_DUPLICATE] = "Duplicate entry",
40         [UCI_ERR_UNKNOWN] =   "Unknown error",
41 };
42
43 static void uci_unload_plugin(struct uci_context *ctx, struct uci_plugin *p);
44
45 #include "uci_internal.h"
46 #include "util.c"
47 #include "list.c"
48 #include "history.c"
49
50 /* exported functions */
51 struct uci_context *uci_alloc_context(void)
52 {
53         struct uci_context *ctx;
54
55         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
56         if (!ctx)
57                 return NULL;
58
59         memset(ctx, 0, sizeof(struct uci_context));
60         uci_list_init(&ctx->root);
61         uci_list_init(&ctx->history_path);
62         uci_list_init(&ctx->backends);
63         uci_list_init(&ctx->hooks);
64         uci_list_init(&ctx->plugins);
65         ctx->flags = UCI_FLAG_STRICT | UCI_FLAG_SAVED_HISTORY;
66
67         ctx->confdir = (char *) uci_confdir;
68         ctx->savedir = (char *) uci_savedir;
69
70         uci_list_add(&ctx->backends, &uci_file_backend.e.list);
71         ctx->backend = &uci_file_backend;
72
73         return ctx;
74 }
75
76 void uci_free_context(struct uci_context *ctx)
77 {
78         struct uci_element *e, *tmp;
79
80         if (ctx->confdir != uci_confdir)
81                 free(ctx->confdir);
82         if (ctx->savedir != uci_savedir)
83                 free(ctx->savedir);
84
85         uci_cleanup(ctx);
86         UCI_TRAP_SAVE(ctx, ignore);
87         uci_foreach_element_safe(&ctx->root, tmp, e) {
88                 struct uci_package *p = uci_to_package(e);
89                 uci_free_package(&p);
90         }
91         uci_foreach_element_safe(&ctx->history_path, tmp, e) {
92                 uci_free_element(e);
93         }
94         UCI_TRAP_RESTORE(ctx);
95         uci_foreach_element_safe(&ctx->root, tmp, e) {
96                 uci_unload_plugin(ctx, uci_to_plugin(e));
97         }
98         free(ctx);
99
100 ignore:
101         return;
102 }
103
104 int uci_set_confdir(struct uci_context *ctx, const char *dir)
105 {
106         char *cdir;
107
108         UCI_HANDLE_ERR(ctx);
109         UCI_ASSERT(ctx, dir != NULL);
110
111         cdir = uci_strdup(ctx, dir);
112         if (ctx->confdir != uci_confdir)
113                 free(ctx->confdir);
114         ctx->confdir = cdir;
115         return 0;
116 }
117
118 __private void uci_cleanup(struct uci_context *ctx)
119 {
120         struct uci_parse_context *pctx;
121
122         if (ctx->buf) {
123                 free(ctx->buf);
124                 ctx->buf = NULL;
125                 ctx->bufsz = 0;
126         }
127
128         pctx = ctx->pctx;
129         if (!pctx)
130                 return;
131
132         ctx->pctx = NULL;
133         if (pctx->package)
134                 uci_free_package(&pctx->package);
135
136         if (pctx->buf)
137                 free(pctx->buf);
138
139         free(pctx);
140 }
141
142 void
143 uci_perror(struct uci_context *ctx, const char *str)
144 {
145         uci_get_errorstr(ctx, NULL, str);
146 }
147
148 void
149 uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
150 {
151         static char error_info[128];
152         int err;
153         const char *format =
154                 "%s%s" /* prefix */
155                 "%s%s" /* function */
156                 "%s" /* error */
157                 "%s"; /* details */
158
159         error_info[0] = 0;
160
161         if (!ctx)
162                 err = UCI_ERR_INVAL;
163         else
164                 err = ctx->err;
165
166         if ((err < 0) || (err >= UCI_ERR_LAST))
167                 err = UCI_ERR_UNKNOWN;
168
169         switch (err) {
170         case UCI_ERR_PARSE:
171                 if (ctx->pctx) {
172                         snprintf(error_info, sizeof(error_info) - 1, " (%s) at line %d, byte %d", (ctx->pctx->reason ? ctx->pctx->reason : "unknown"), ctx->pctx->line, ctx->pctx->byte);
173                         break;
174                 }
175                 break;
176         default:
177                 break;
178         }
179         if (dest) {
180                 err = asprintf(dest, format,
181                         (prefix ? prefix : ""), (prefix ? ": " : ""),
182                         (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""),
183                         uci_errstr[err],
184                         error_info);
185                 if (err < 0)
186                         *dest = NULL;
187         } else {
188                 strcat(error_info, "\n");
189                 fprintf(stderr, format,
190                         (prefix ? prefix : ""), (prefix ? ": " : ""),
191                         (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""),
192                         uci_errstr[err],
193                         error_info);
194         }
195 }
196
197 int uci_list_configs(struct uci_context *ctx, char ***list)
198 {
199         UCI_HANDLE_ERR(ctx);
200         UCI_ASSERT(ctx, list != NULL);
201         UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
202         *list = ctx->backend->list_configs(ctx);
203         return 0;
204 }
205
206 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
207 {
208         struct uci_package *p;
209         UCI_HANDLE_ERR(ctx);
210         UCI_ASSERT(ctx, package != NULL);
211         p = *package;
212         UCI_ASSERT(ctx, p != NULL);
213         UCI_ASSERT(ctx, p->backend && p->backend->commit);
214         p->backend->commit(ctx, package, overwrite);
215         return 0;
216 }
217
218 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
219 {
220         struct uci_package *p;
221         struct uci_element *e;
222
223         UCI_HANDLE_ERR(ctx);
224         UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
225         p = ctx->backend->load(ctx, name);
226         uci_foreach_element(&ctx->hooks, e) {
227                 struct uci_hook *h = uci_to_hook(e);
228                 if (h->ops->load)
229                         h->ops->load(h->ops, p);
230         }
231         if (package)
232                 *package = p;
233
234         return 0;
235 }
236
237 #ifdef UCI_PLUGIN_SUPPORT
238
239 __plugin int uci_add_backend(struct uci_context *ctx, struct uci_backend *b)
240 {
241         struct uci_element *e;
242         UCI_HANDLE_ERR(ctx);
243
244         e = uci_lookup_list(&ctx->backends, b->e.name);
245         if (e)
246                 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
247
248         e = uci_malloc(ctx, sizeof(struct uci_backend));
249         memcpy(e, b, sizeof(struct uci_backend));
250
251         uci_list_add(&ctx->backends, &e->list);
252         return 0;
253 }
254
255 __plugin int uci_del_backend(struct uci_context *ctx, struct uci_backend *b)
256 {
257         struct uci_element *e, *tmp;
258
259         UCI_HANDLE_ERR(ctx);
260
261         e = uci_lookup_list(&ctx->backends, b->e.name);
262         if (!e || uci_to_backend(e)->ptr != b->ptr)
263                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
264         b = uci_to_backend(e);
265
266         if (ctx->backend && ctx->backend->ptr == b->ptr)
267                 ctx->backend = &uci_file_backend;
268
269         uci_foreach_element_safe(&ctx->root, tmp, e) {
270                 struct uci_package *p = uci_to_package(e);
271
272                 if (!p->backend)
273                         continue;
274
275                 if (p->backend->ptr == b->ptr)
276                         UCI_INTERNAL(uci_unload, ctx, p);
277         }
278
279         uci_list_del(&b->e.list);
280         free(b);
281
282         return 0;
283 }
284
285 #endif
286
287 int uci_set_backend(struct uci_context *ctx, const char *name)
288 {
289         struct uci_element *e;
290
291         UCI_HANDLE_ERR(ctx);
292         UCI_ASSERT(ctx, name != NULL);
293         e = uci_lookup_list(&ctx->backends, name);
294         if (!e)
295                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
296         ctx->backend = uci_to_backend(e);
297         return 0;
298 }
299
300 int uci_add_hook(struct uci_context *ctx, const struct uci_hook_ops *ops)
301 {
302         struct uci_element *e;
303         struct uci_hook *h;
304
305         UCI_HANDLE_ERR(ctx);
306
307         /* check for duplicate elements */
308         uci_foreach_element(&ctx->hooks, e) {
309                 h = uci_to_hook(e);
310                 if (h->ops == ops)
311                         return UCI_ERR_INVAL;
312         }
313
314         h = uci_alloc_element(ctx, hook, "", 0);
315         h->ops = ops;
316         uci_list_init(&h->e.list);
317         uci_list_add(&ctx->hooks, &h->e.list);
318
319         return 0;
320 }
321
322 int uci_remove_hook(struct uci_context *ctx, const struct uci_hook_ops *ops)
323 {
324         struct uci_element *e;
325
326         uci_foreach_element(&ctx->hooks, e) {
327                 struct uci_hook *h = uci_to_hook(e);
328                 if (h->ops == ops) {
329                         uci_list_del(&e->list);
330                         return 0;
331                 }
332         }
333         return UCI_ERR_NOTFOUND;
334 }
335
336 int uci_load_plugin(struct uci_context *ctx, const char *filename)
337 {
338         struct uci_plugin *p;
339         const struct uci_plugin_ops *ops;
340         void *dlh;
341
342         UCI_HANDLE_ERR(ctx);
343         dlh = dlopen(filename, RTLD_GLOBAL|RTLD_NOW);
344         if (!dlh)
345                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
346
347         ops = dlsym(dlh, "uci_plugin");
348         if (!ops || !ops->attach || (ops->attach(ctx) != 0)) {
349                 if (!ops)
350                         fprintf(stderr, "No ops\n");
351                 else if (!ops->attach)
352                         fprintf(stderr, "No attach\n");
353                 else
354                         fprintf(stderr, "Other weirdness\n");
355                 dlclose(dlh);
356                 UCI_THROW(ctx, UCI_ERR_INVAL);
357         }
358
359         p = uci_alloc_element(ctx, plugin, filename, 0);
360         p->dlh = dlh;
361         p->ops = ops;
362         uci_list_add(&ctx->plugins, &p->e.list);
363
364         return 0;
365 }
366
367 static void uci_unload_plugin(struct uci_context *ctx, struct uci_plugin *p)
368 {
369         if (p->ops->detach)
370                 p->ops->detach(ctx);
371         dlclose(p->dlh);
372         uci_free_element(&p->e);
373 }
374
375 int uci_load_plugins(struct uci_context *ctx, const char *pattern)
376 {
377         glob_t gl;
378         int i;
379
380         if (!pattern)
381                 pattern = UCI_PREFIX "/lib/uci_*.so";
382
383         memset(&gl, 0, sizeof(gl));
384         glob(pattern, 0, NULL, &gl);
385         for (i = 0; i < gl.gl_pathc; i++)
386                 uci_load_plugin(ctx, gl.gl_pathv[i]);
387
388         return 0;
389 }