lua: add uci.foreach() error handling patch by xMff
[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_cleanup(struct uci_context *ctx);
44 static void uci_unload_plugin(struct uci_context *ctx, struct uci_plugin *p);
45
46 #include "uci_internal.h"
47 #include "util.c"
48 #include "list.c"
49 #include "history.c"
50 #include "file.c"
51
52 /* exported functions */
53 struct uci_context *uci_alloc_context(void)
54 {
55         struct uci_context *ctx;
56
57         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
58         if (!ctx)
59                 return NULL;
60
61         memset(ctx, 0, sizeof(struct uci_context));
62         uci_list_init(&ctx->root);
63         uci_list_init(&ctx->history_path);
64         uci_list_init(&ctx->backends);
65         uci_list_init(&ctx->hooks);
66         uci_list_init(&ctx->plugins);
67         ctx->flags = UCI_FLAG_STRICT | UCI_FLAG_SAVED_HISTORY;
68
69         ctx->confdir = (char *) uci_confdir;
70         ctx->savedir = (char *) uci_savedir;
71
72         uci_list_add(&ctx->backends, &uci_file_backend.e.list);
73         ctx->backend = &uci_file_backend;
74
75         return ctx;
76 }
77
78 void uci_free_context(struct uci_context *ctx)
79 {
80         struct uci_element *e, *tmp;
81
82         if (ctx->confdir != uci_confdir)
83                 free(ctx->confdir);
84         if (ctx->savedir != uci_savedir)
85                 free(ctx->savedir);
86
87         uci_cleanup(ctx);
88         UCI_TRAP_SAVE(ctx, ignore);
89         uci_foreach_element_safe(&ctx->root, tmp, e) {
90                 struct uci_package *p = uci_to_package(e);
91                 uci_free_package(&p);
92         }
93         uci_foreach_element_safe(&ctx->history_path, tmp, e) {
94                 uci_free_element(e);
95         }
96         UCI_TRAP_RESTORE(ctx);
97         uci_foreach_element_safe(&ctx->root, tmp, e) {
98                 uci_unload_plugin(ctx, uci_to_plugin(e));
99         }
100         free(ctx);
101
102 ignore:
103         return;
104 }
105
106 int uci_set_confdir(struct uci_context *ctx, const char *dir)
107 {
108         char *cdir;
109
110         UCI_HANDLE_ERR(ctx);
111         UCI_ASSERT(ctx, dir != NULL);
112
113         cdir = uci_strdup(ctx, dir);
114         if (ctx->confdir != uci_confdir)
115                 free(ctx->confdir);
116         ctx->confdir = cdir;
117         return 0;
118 }
119
120 static void uci_cleanup(struct uci_context *ctx)
121 {
122         struct uci_parse_context *pctx;
123
124         if (ctx->buf) {
125                 free(ctx->buf);
126                 ctx->buf = NULL;
127                 ctx->bufsz = 0;
128         }
129
130         pctx = ctx->pctx;
131         if (!pctx)
132                 return;
133
134         ctx->pctx = NULL;
135         if (pctx->package)
136                 uci_free_package(&pctx->package);
137
138         if (pctx->buf)
139                 free(pctx->buf);
140
141         free(pctx);
142 }
143
144 void
145 uci_perror(struct uci_context *ctx, const char *str)
146 {
147         uci_get_errorstr(ctx, NULL, str);
148 }
149
150 void
151 uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
152 {
153         static char error_info[128];
154         int err;
155         const char *format =
156                 "%s%s" /* prefix */
157                 "%s%s" /* function */
158                 "%s" /* error */
159                 "%s"; /* details */
160
161         error_info[0] = 0;
162
163         if (!ctx)
164                 err = UCI_ERR_INVAL;
165         else
166                 err = ctx->err;
167
168         if ((err < 0) || (err >= UCI_ERR_LAST))
169                 err = UCI_ERR_UNKNOWN;
170
171         switch (err) {
172         case UCI_ERR_PARSE:
173                 if (ctx->pctx) {
174                         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);
175                         break;
176                 }
177                 break;
178         default:
179                 break;
180         }
181         if (dest) {
182                 err = asprintf(dest, format,
183                         (prefix ? prefix : ""), (prefix ? ": " : ""),
184                         (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""),
185                         uci_errstr[err],
186                         error_info);
187                 if (err < 0)
188                         *dest = NULL;
189         } else {
190                 strcat(error_info, "\n");
191                 fprintf(stderr, format,
192                         (prefix ? prefix : ""), (prefix ? ": " : ""),
193                         (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""),
194                         uci_errstr[err],
195                         error_info);
196         }
197 }
198
199 int uci_list_configs(struct uci_context *ctx, char ***list)
200 {
201         UCI_HANDLE_ERR(ctx);
202         UCI_ASSERT(ctx, list != NULL);
203         UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
204         *list = ctx->backend->list_configs(ctx);
205         return 0;
206 }
207
208 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
209 {
210         struct uci_package *p;
211         UCI_HANDLE_ERR(ctx);
212         UCI_ASSERT(ctx, package != NULL);
213         p = *package;
214         UCI_ASSERT(ctx, p != NULL);
215         UCI_ASSERT(ctx, p->backend && p->backend->commit);
216         p->backend->commit(ctx, package, overwrite);
217         return 0;
218 }
219
220 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
221 {
222         struct uci_package *p;
223         struct uci_element *e;
224
225         UCI_HANDLE_ERR(ctx);
226         UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
227         p = ctx->backend->load(ctx, name);
228         uci_foreach_element(&ctx->hooks, e) {
229                 struct uci_hook *h = uci_to_hook(e);
230                 if (h->ops->load)
231                         h->ops->load(h->ops, p);
232         }
233         if (package)
234                 *package = p;
235
236         return 0;
237 }
238
239 #ifdef UCI_PLUGIN_SUPPORT
240
241 __plugin int uci_add_backend(struct uci_context *ctx, struct uci_backend *b)
242 {
243         struct uci_element *e;
244         UCI_HANDLE_ERR(ctx);
245
246         e = uci_lookup_list(&ctx->backends, b->e.name);
247         if (e)
248                 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
249
250         e = uci_malloc(ctx, sizeof(struct uci_backend));
251         memcpy(e, b, sizeof(struct uci_backend));
252
253         uci_list_add(&ctx->backends, &e->list);
254         return 0;
255 }
256
257 __plugin int uci_del_backend(struct uci_context *ctx, struct uci_backend *b)
258 {
259         struct uci_element *e, *tmp;
260
261         UCI_HANDLE_ERR(ctx);
262
263         e = uci_lookup_list(&ctx->backends, b->e.name);
264         if (!e || uci_to_backend(e)->ptr != b->ptr)
265                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
266         b = uci_to_backend(e);
267
268         if (ctx->backend && ctx->backend->ptr == b->ptr)
269                 ctx->backend = &uci_file_backend;
270
271         uci_foreach_element_safe(&ctx->root, tmp, e) {
272                 struct uci_package *p = uci_to_package(e);
273
274                 if (!p->backend)
275                         continue;
276
277                 if (p->backend->ptr == b->ptr)
278                         UCI_INTERNAL(uci_unload, ctx, p);
279         }
280
281         uci_list_del(&b->e.list);
282         free(b);
283
284         return 0;
285 }
286
287 #endif
288
289 int uci_set_backend(struct uci_context *ctx, const char *name)
290 {
291         struct uci_element *e;
292
293         UCI_HANDLE_ERR(ctx);
294         UCI_ASSERT(ctx, name != NULL);
295         e = uci_lookup_list(&ctx->backends, name);
296         if (!e)
297                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
298         ctx->backend = uci_to_backend(e);
299         return 0;
300 }
301
302 int uci_add_hook(struct uci_context *ctx, const struct uci_hook_ops *ops)
303 {
304         struct uci_element *e;
305         struct uci_hook *h;
306
307         UCI_HANDLE_ERR(ctx);
308
309         /* check for duplicate elements */
310         uci_foreach_element(&ctx->hooks, e) {
311                 h = uci_to_hook(e);
312                 if (h->ops == ops)
313                         return UCI_ERR_INVAL;
314         }
315
316         h = uci_alloc_element(ctx, hook, "", 0);
317         h->ops = ops;
318         uci_list_init(&h->e.list);
319         uci_list_add(&ctx->hooks, &h->e.list);
320
321         return 0;
322 }
323
324 int uci_remove_hook(struct uci_context *ctx, const struct uci_hook_ops *ops)
325 {
326         struct uci_element *e;
327
328         uci_foreach_element(&ctx->hooks, e) {
329                 struct uci_hook *h = uci_to_hook(e);
330                 if (h->ops == ops) {
331                         uci_list_del(&e->list);
332                         return 0;
333                 }
334         }
335         return UCI_ERR_NOTFOUND;
336 }
337
338 int uci_load_plugin(struct uci_context *ctx, const char *filename)
339 {
340         struct uci_plugin *p;
341         const struct uci_plugin_ops *ops;
342         void *dlh;
343
344         UCI_HANDLE_ERR(ctx);
345         dlh = dlopen(filename, RTLD_GLOBAL|RTLD_NOW);
346         if (!dlh)
347                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
348
349         ops = dlsym(dlh, "uci_plugin");
350         if (!ops || !ops->attach || (ops->attach(ctx) != 0)) {
351                 if (!ops)
352                         fprintf(stderr, "No ops\n");
353                 else if (!ops->attach)
354                         fprintf(stderr, "No attach\n");
355                 else
356                         fprintf(stderr, "Other weirdness\n");
357                 dlclose(dlh);
358                 UCI_THROW(ctx, UCI_ERR_INVAL);
359         }
360
361         p = uci_alloc_element(ctx, plugin, filename, 0);
362         p->dlh = dlh;
363         p->ops = ops;
364         uci_list_add(&ctx->plugins, &p->e.list);
365
366         return 0;
367 }
368
369 static void uci_unload_plugin(struct uci_context *ctx, struct uci_plugin *p)
370 {
371         if (p->ops->detach)
372                 p->ops->detach(ctx);
373         dlclose(p->dlh);
374         uci_free_element(&p->e);
375 }
376
377 int uci_load_plugins(struct uci_context *ctx, const char *pattern)
378 {
379         glob_t gl;
380         int i;
381
382         if (!pattern)
383                 pattern = UCI_PREFIX "/lib/uci_*.so";
384
385         memset(&gl, 0, sizeof(gl));
386         glob(pattern, 0, NULL, &gl);
387         for (i = 0; i < gl.gl_pathc; i++)
388                 uci_load_plugin(ctx, gl.gl_pathv[i]);
389
390         return 0;
391 }