config.c 13 KB

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