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