libuci.c 8.4 KB

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