config.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS 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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * config.c
  17. *
  18. * This file contains functions to read app config (manifest) file and create
  19. * a tree to lookup / access config values.
  20. */
  21. #include <list.h>
  22. #include <api.h>
  23. #include <pal_error.h>
  24. DEFINE_LIST(config);
  25. struct config {
  26. const char * key, * val;
  27. int klen, vlen;
  28. char * buf;
  29. LIST_TYPE(config) list;
  30. LISTP_TYPE(config) children;
  31. LIST_TYPE(config) siblings;
  32. };
  33. static int __add_config (struct config_store * store,
  34. const char * key, int klen,
  35. const char * val, int vlen,
  36. struct config ** entry)
  37. {
  38. LISTP_TYPE(config) * list = &store->root;
  39. struct config * e = NULL;
  40. while (klen) {
  41. if (e && e->val)
  42. return -PAL_ERROR_INVAL;
  43. const char * token = key;
  44. int len = 0;
  45. for ( ; len < klen ; len++)
  46. if (token[len] == '.')
  47. break;
  48. listp_for_each_entry(e, list, siblings)
  49. if (e->klen == len && !memcmp(e->key, token, len))
  50. goto next;
  51. e = store->malloc(sizeof(struct config));
  52. if (!e)
  53. return -PAL_ERROR_NOMEM;
  54. e->key = token;
  55. e->klen = len;
  56. e->val = NULL;
  57. e->vlen = 0;
  58. e->buf = NULL;
  59. INIT_LIST_HEAD(e, list);
  60. listp_add_tail(e, &store->entries, list);
  61. INIT_LISTP(&e->children);
  62. INIT_LIST_HEAD(e, siblings);
  63. listp_add_tail(e, list, siblings);
  64. next:
  65. if (len < klen)
  66. len++;
  67. key += len;
  68. klen -= len;
  69. list = &e->children;
  70. }
  71. if (!e || e->val || !listp_empty(&e->children))
  72. return -PAL_ERROR_INVAL;
  73. e->val = val;
  74. e->vlen = vlen;
  75. if (entry)
  76. *entry = e;
  77. return 0;
  78. }
  79. static struct config * __get_config (struct config_store * store,
  80. const char * key)
  81. {
  82. LISTP_TYPE(config) * list = &store->root;
  83. struct config * e = NULL;
  84. while (*key) {
  85. const char * token = key;
  86. int len = 0;
  87. for ( ; token[len] ; len++)
  88. if (token[len] == '.')
  89. break;
  90. listp_for_each_entry(e, list, siblings)
  91. if (e->klen == len && !memcmp(e->key, token, len))
  92. goto next;
  93. return NULL;
  94. next:
  95. if (token[len])
  96. len++;
  97. key += len;
  98. list = &e->children;
  99. }
  100. return e;
  101. }
  102. int get_config (struct config_store * store, const char * key,
  103. char * val_buf, int size)
  104. {
  105. struct config * e = __get_config(store, key);
  106. if (!e || !e->val)
  107. return -PAL_ERROR_INVAL;
  108. if (e->vlen >= size)
  109. return -PAL_ERROR_TOOLONG;
  110. memcpy(val_buf, e->val, e->vlen);
  111. val_buf[e->vlen] = 0;
  112. return e->vlen;
  113. }
  114. int get_config_entries (struct config_store * store, const char * key,
  115. char * key_buf, int size)
  116. {
  117. struct config * e = __get_config(store, key);
  118. if (!e || e->val)
  119. return -PAL_ERROR_INVAL;
  120. LISTP_TYPE(config) * children = &e->children;
  121. int nentries = 0;
  122. listp_for_each_entry(e, children, siblings) {
  123. if (e->klen >= size)
  124. return -PAL_ERROR_TOOLONG;
  125. memcpy(key_buf, e->key, e->klen);
  126. key_buf[e->klen] = 0;
  127. key_buf += e->klen + 1;
  128. size -= e->klen + 1;
  129. nentries++;
  130. }
  131. return nentries;
  132. }
  133. static int __del_config (struct config_store * store,
  134. LISTP_TYPE(config) * root, const char * key)
  135. {
  136. struct config * e, * found = NULL;
  137. int len = 0;
  138. for ( ; key[len] ; len++)
  139. if (key[len] == '.')
  140. break;
  141. listp_for_each_entry(e, root, siblings)
  142. if (e->klen == len && !memcmp(e->key, key, len)) {
  143. found = e;
  144. break;
  145. }
  146. if (!found)
  147. return -PAL_ERROR_INVAL;
  148. if (key[len]) {
  149. if (found->val)
  150. return -PAL_ERROR_INVAL;
  151. int ret = __del_config(store, &found->children, key + len + 1);
  152. if (ret < 0)
  153. return ret;
  154. if (!listp_empty(&found->children))
  155. return 0;
  156. } else {
  157. if (!found->val)
  158. return -PAL_ERROR_INVAL;
  159. }
  160. listp_del(found, root, siblings);
  161. listp_del(found, &store->entries, list);
  162. if (found->buf)
  163. store->free(found->buf);
  164. store->free(found);
  165. return 0;
  166. }
  167. int set_config (struct config_store * store, const char * key, const char * val)
  168. {
  169. if (!key)
  170. return -PAL_ERROR_INVAL;
  171. if (!val) { /* deletion */
  172. return __del_config(store, &store->root, key);
  173. }
  174. int klen = strlen(key);
  175. int vlen = strlen(val);
  176. char * buf = store->malloc(klen + vlen + 2);
  177. if (!buf)
  178. return -PAL_ERROR_NOMEM;
  179. memcpy(buf, key, klen + 1);
  180. memcpy(buf + klen + 1, val, vlen + 1);
  181. struct config * e = __get_config(store, key);
  182. if (e) {
  183. e->val = buf + klen + 1;
  184. e->vlen = vlen;
  185. e->buf = buf;
  186. } else {
  187. int ret = __add_config(store, buf, klen, buf + klen + 1, vlen, &e);
  188. if (ret < 0) {
  189. store->free(buf);
  190. return ret;
  191. }
  192. e->buf = buf;
  193. }
  194. return 0;
  195. }
  196. int read_config (struct config_store * store,
  197. int (*filter) (const char * key, int ken),
  198. const char ** errstring)
  199. {
  200. INIT_LISTP(&store->root);
  201. INIT_LISTP(&store->entries);
  202. char * ptr = store->raw_data;
  203. char * ptr_end = store->raw_data + store->raw_size;
  204. const char * err = "unknown error";
  205. #define IS_SPACE(c) ((c) == ' ' || (c) == '\t')
  206. #define IS_BREAK(c) ((c) == '\r' || (c) == '\n')
  207. #define IS_VALID(c) \
  208. (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') || \
  209. ((c) >= '0' && (c) <= '9') || (c) == '_')
  210. register int skipping = 0;
  211. #define IS_SKIP(p) \
  212. (skipping ? ({ if (IS_BREAK(*(p))) skipping = 0; 1; }) \
  213. : ((*(p)) == '#' ? ({ skipping = 1; 1; }) : IS_BREAK(*(p))))
  214. #define RANGE (ptr < ptr_end)
  215. #define GOTO_INVAL(msg) ({ err = msg; goto inval; })
  216. #define CHECK_PTR(msg) if (!RANGE) GOTO_INVAL(msg)
  217. while (RANGE) {
  218. for ( ; RANGE && (IS_SKIP(ptr) || IS_SPACE(*ptr)) ; ptr++);
  219. if (!(RANGE))
  220. break;
  221. if (!IS_VALID(*ptr))
  222. GOTO_INVAL("invalid start of key");
  223. char * key = ptr;
  224. for ( ; RANGE ; ptr++) {
  225. char * pptr = ptr;
  226. for ( ; RANGE && IS_VALID(*ptr) ; ptr++);
  227. CHECK_PTR("stream ended at key");
  228. if (pptr == ptr)
  229. GOTO_INVAL("key token with zero length");
  230. if (*ptr != '.')
  231. break;
  232. }
  233. int klen = ptr - key;
  234. for ( ; RANGE && IS_SPACE(*ptr) ; ptr++)
  235. CHECK_PTR("stream ended at key");
  236. if (*ptr != '=')
  237. GOTO_INVAL("equal mark expected");
  238. ptr++;
  239. for ( ; RANGE && IS_SPACE(*ptr) ; ptr++);
  240. CHECK_PTR("stream ended at equal mark");
  241. char * val = NULL;
  242. int vlen;
  243. if (*ptr == '"') {
  244. int shift = 0;
  245. val = (++ptr);
  246. for ( ; RANGE && *ptr != '"' ; ptr++) {
  247. if (*ptr == '\\') {
  248. shift++;
  249. ptr++;
  250. }
  251. if (shift)
  252. *(ptr - shift) = *ptr;
  253. }
  254. CHECK_PTR("stream ended without closing quote");
  255. vlen = (ptr - shift) - val;
  256. } else {
  257. val = ptr;
  258. for ( ; RANGE && !IS_SKIP(ptr) ; ptr++);
  259. vlen = ptr - val;
  260. }
  261. ptr++;
  262. if (!filter || !filter(key, klen)) {
  263. int ret = __add_config(store, key, klen, val, vlen, NULL);
  264. if (ret < 0) {
  265. if (ret == -PAL_ERROR_TOOLONG)
  266. GOTO_INVAL("key too long");
  267. if (ret == -PAL_ERROR_INVAL)
  268. GOTO_INVAL("key format invalid");
  269. GOTO_INVAL("unknown error");
  270. }
  271. }
  272. }
  273. return 0;
  274. inval:
  275. if (errstring)
  276. *errstring = err;
  277. return -PAL_ERROR_INVAL;
  278. }
  279. int free_config (struct config_store * store)
  280. {
  281. struct config * e, * n;
  282. listp_for_each_entry_safe(e, n, &store->entries, list) {
  283. if (e->buf)
  284. store->free(e->buf);
  285. store->free(e);
  286. }
  287. INIT_LISTP(&store->root);
  288. INIT_LISTP(&store->entries);
  289. return 0;
  290. }
  291. static int __dup_config (const struct config_store * ss,
  292. const LISTP_TYPE(config) * sr,
  293. struct config_store * ts,
  294. LISTP_TYPE(config) * tr,
  295. void ** data, int * size)
  296. {
  297. struct config * e, * new;
  298. listp_for_each_entry(e, sr, siblings) {
  299. char * key = NULL, * val = NULL, * buf = NULL;
  300. int need = 0;
  301. if (e->key) {
  302. if (*size > e->klen) {
  303. key = *data;
  304. *data += e->klen;
  305. *size -= e->klen;
  306. memcpy(key, e->key, e->klen);
  307. } else
  308. need += e->klen;
  309. }
  310. if (e->val) {
  311. if (*size > e->vlen) {
  312. val = *data;
  313. *data += e->vlen;
  314. *size -= e->vlen;
  315. memcpy(val, e->val, e->vlen);
  316. } else
  317. need += e->vlen;
  318. }
  319. if (need) {
  320. buf = ts->malloc(need);
  321. if (!buf)
  322. return -PAL_ERROR_NOMEM;
  323. }
  324. if (e->key && !key) {
  325. key = buf;
  326. memcpy(key, e->key, e->klen);
  327. }
  328. if (e->val && !val) {
  329. val = buf + (key == buf ? e->klen : 0);
  330. memcpy(val, e->val, e->vlen);
  331. }
  332. new = ts->malloc(sizeof(struct config));
  333. if (!new)
  334. return -PAL_ERROR_NOMEM;
  335. new->key = key;
  336. new->klen = e->klen;
  337. new->val = val;
  338. new->vlen = e->vlen;
  339. new->buf = buf;
  340. INIT_LIST_HEAD(new, list);
  341. listp_add_tail(new, &ts->entries, list);
  342. INIT_LISTP(&new->children);
  343. INIT_LIST_HEAD(new, siblings);
  344. listp_add_tail(new, tr, siblings);
  345. if (!listp_empty(&e->children)) {
  346. int ret = __dup_config(ss, &e->children,
  347. ts, &new->children,
  348. data, size);
  349. if (ret < 0)
  350. return ret;
  351. }
  352. }
  353. return 0;
  354. }
  355. int copy_config (struct config_store * store, struct config_store * new_store)
  356. {
  357. INIT_LISTP(&new_store->root);
  358. INIT_LISTP(&new_store->entries);
  359. struct config * e;
  360. int size = 0;
  361. listp_for_each_entry(e, &store->entries, list) {
  362. if (e->key)
  363. size += e->klen;
  364. if (e->val)
  365. size += e->vlen;
  366. }
  367. void * data = new_store->malloc(size);
  368. if (!data)
  369. return -PAL_ERROR_NOMEM;
  370. void * dataptr = data;
  371. int datasz = size;
  372. new_store->raw_data = data;
  373. new_store->raw_size = size;
  374. return __dup_config(store, &store->root, new_store, &new_store->root,
  375. &dataptr, &datasz);
  376. }
  377. static int __write_config (void * f, int (*write) (void *, void *, int),
  378. struct config_store * store,
  379. LISTP_TYPE(config) * root,
  380. char * keybuf, int klen,
  381. unsigned long * offset)
  382. {
  383. struct config * e;
  384. int ret;
  385. char * buf = NULL;
  386. int bufsz = 0;
  387. listp_for_each_entry(e, root, siblings)
  388. if (e->val) {
  389. int total = klen + e->klen + e->vlen + 2;
  390. while (total > bufsz) {
  391. bufsz += CONFIG_MAX;
  392. buf = __alloca(CONFIG_MAX);
  393. }
  394. memcpy(buf, keybuf, klen);
  395. memcpy(buf + klen, e->key, e->klen);
  396. buf[klen + e->klen] = '=';
  397. memcpy(buf + total - e->vlen - 1, e->val, e->vlen);
  398. buf[total - 1] = '\n';
  399. ret = write(f, buf, total);
  400. if (ret < 0)
  401. return ret;
  402. *offset += total;
  403. } else {
  404. if (klen + e->klen + 1 > CONFIG_MAX)
  405. return -PAL_ERROR_TOOLONG;
  406. memcpy(keybuf + klen, e->key, e->klen);
  407. keybuf[klen + e->klen] = '.';
  408. if ((ret = __write_config(f, write, store, &e->children, keybuf,
  409. klen + e->klen + 1, offset)) < 0)
  410. return ret;
  411. }
  412. return 0;
  413. }
  414. int write_config (void * f, int (*write) (void *, void *, int),
  415. struct config_store * store)
  416. {
  417. char buf[CONFIG_MAX];
  418. unsigned long offset = 0;
  419. return __write_config(f, write, store, &store->root, buf, 0, &offset);
  420. }