objdb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /*
  2. * Copyright (c) 2006 MontaVista Software, Inc.
  3. * Copyright (c) 2008 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <stdio.h>
  36. #include <errno.h>
  37. #include "objdb.h"
  38. #include "config.h"
  39. #include "main.h"
  40. #include "../lcr/lcr_comp.h"
  41. #include "../include/hdb.h"
  42. #include "../include/list.h"
  43. struct object_key {
  44. void *key_name;
  45. int key_len;
  46. void *value;
  47. int value_len;
  48. struct list_head list;
  49. };
  50. struct object_instance {
  51. void *object_name;
  52. int object_name_len;
  53. unsigned int object_handle;
  54. unsigned int parent_handle;
  55. struct list_head key_head;
  56. struct list_head child_head;
  57. struct list_head child_list;
  58. struct list_head *find_child_list;
  59. struct list_head *iter_key_list;
  60. struct list_head *iter_list;
  61. void *priv;
  62. struct object_valid *object_valid_list;
  63. int object_valid_list_entries;
  64. struct object_key_valid *object_key_valid_list;
  65. int object_key_valid_list_entries;
  66. };
  67. struct objdb_iface_ver0 objdb_iface;
  68. static struct hdb_handle_database object_instance_database = {
  69. .handle_count = 0,
  70. .handles = 0,
  71. .iterator = 0,
  72. .mutex = PTHREAD_MUTEX_INITIALIZER
  73. };
  74. static int objdb_init (void)
  75. {
  76. unsigned int handle;
  77. struct object_instance *instance;
  78. unsigned int res;
  79. res = hdb_handle_create (&object_instance_database,
  80. sizeof (struct object_instance), &handle);
  81. if (res != 0) {
  82. goto error_exit;
  83. }
  84. res = hdb_handle_get (&object_instance_database,
  85. handle, (void *)&instance);
  86. if (res != 0) {
  87. goto error_destroy;
  88. }
  89. instance->find_child_list = &instance->child_head;
  90. instance->object_name = "parent";
  91. instance->object_name_len = strlen ("parent");
  92. instance->object_handle = handle;
  93. instance->priv = NULL;
  94. instance->object_valid_list = NULL;
  95. instance->object_valid_list_entries = 0;
  96. list_init (&instance->key_head);
  97. list_init (&instance->child_head);
  98. list_init (&instance->child_list);
  99. hdb_handle_put (&object_instance_database, handle);
  100. return (0);
  101. error_destroy:
  102. hdb_handle_destroy (&object_instance_database, handle);
  103. error_exit:
  104. return (-1);
  105. }
  106. /*
  107. * object db create/destroy/set
  108. */
  109. static int object_create (
  110. unsigned int parent_object_handle,
  111. unsigned int *object_handle,
  112. void *object_name,
  113. unsigned int object_name_len)
  114. {
  115. struct object_instance *object_instance;
  116. struct object_instance *parent_instance;
  117. unsigned int res;
  118. int found = 0;
  119. int i;
  120. res = hdb_handle_get (&object_instance_database,
  121. parent_object_handle, (void *)&parent_instance);
  122. if (res != 0) {
  123. goto error_exit;
  124. }
  125. /*
  126. * Do validation check if validation is configured for the parent object
  127. */
  128. if (parent_instance->object_valid_list_entries) {
  129. for (i = 0; i < parent_instance->object_valid_list_entries; i++) {
  130. if ((object_name_len ==
  131. parent_instance->object_valid_list[i].object_len) &&
  132. (memcmp (object_name,
  133. parent_instance->object_valid_list[i].object_name,
  134. object_name_len) == 0)) {
  135. found = 1;
  136. break;
  137. }
  138. }
  139. /*
  140. * Item not found in validation list
  141. */
  142. if (found == 0) {
  143. goto error_object_put;
  144. }
  145. }
  146. res = hdb_handle_create (&object_instance_database,
  147. sizeof (struct object_instance), object_handle);
  148. if (res != 0) {
  149. goto error_object_put;
  150. }
  151. res = hdb_handle_get (&object_instance_database,
  152. *object_handle, (void *)&object_instance);
  153. if (res != 0) {
  154. goto error_destroy;
  155. }
  156. list_init (&object_instance->key_head);
  157. list_init (&object_instance->child_head);
  158. list_init (&object_instance->child_list);
  159. object_instance->object_name = malloc (object_name_len);
  160. if (object_instance->object_name == 0) {
  161. goto error_put_destroy;
  162. }
  163. memcpy (object_instance->object_name, object_name, object_name_len);
  164. object_instance->object_name_len = object_name_len;
  165. list_add (&object_instance->child_list, &parent_instance->child_head);
  166. object_instance->object_handle = *object_handle;
  167. object_instance->find_child_list = &object_instance->child_head;
  168. object_instance->iter_key_list = &object_instance->key_head;
  169. object_instance->iter_list = &object_instance->child_head;
  170. object_instance->priv = NULL;
  171. object_instance->object_valid_list = NULL;
  172. object_instance->object_valid_list_entries = 0;
  173. object_instance->parent_handle = parent_object_handle;
  174. hdb_handle_put (&object_instance_database, *object_handle);
  175. hdb_handle_put (&object_instance_database, parent_object_handle);
  176. return (0);
  177. error_put_destroy:
  178. hdb_handle_put (&object_instance_database, *object_handle);
  179. error_destroy:
  180. hdb_handle_destroy (&object_instance_database, *object_handle);
  181. error_object_put:
  182. hdb_handle_put (&object_instance_database, parent_object_handle);
  183. error_exit:
  184. return (-1);
  185. }
  186. static int object_priv_set (
  187. unsigned int object_handle,
  188. void *priv)
  189. {
  190. int res;
  191. struct object_instance *object_instance;
  192. res = hdb_handle_get (&object_instance_database,
  193. object_handle, (void *)&object_instance);
  194. if (res != 0) {
  195. goto error_exit;
  196. }
  197. object_instance->priv = priv;
  198. hdb_handle_put (&object_instance_database, object_handle);
  199. return (0);
  200. error_exit:
  201. return (-1);
  202. }
  203. static int object_key_create (
  204. unsigned int object_handle,
  205. void *key_name,
  206. int key_len,
  207. void *value,
  208. int value_len)
  209. {
  210. struct object_instance *instance;
  211. struct object_key *object_key;
  212. unsigned int res;
  213. int found = 0;
  214. int i;
  215. unsigned int val;
  216. res = hdb_handle_get (&object_instance_database,
  217. object_handle, (void *)&instance);
  218. if (res != 0) {
  219. goto error_exit;
  220. }
  221. /*
  222. * Do validation check if validation is configured for the parent object
  223. */
  224. if (instance->object_key_valid_list_entries) {
  225. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  226. if ((key_len ==
  227. instance->object_key_valid_list[i].key_len) &&
  228. (memcmp (key_name,
  229. instance->object_key_valid_list[i].key_name,
  230. key_len) == 0)) {
  231. found = 1;
  232. break;
  233. }
  234. }
  235. /*
  236. * Item not found in validation list
  237. */
  238. if (found == 0) {
  239. goto error_put;
  240. } else {
  241. if (instance->object_key_valid_list[i].validate_callback) {
  242. res = instance->object_key_valid_list[i].validate_callback (
  243. key_name, key_len, value, value_len);
  244. if (res != 0) {
  245. goto error_put;
  246. }
  247. }
  248. }
  249. }
  250. object_key = malloc (sizeof (struct object_key));
  251. if (object_key == 0) {
  252. goto error_put;
  253. }
  254. object_key->key_name = malloc (key_len);
  255. if (object_key->key_name == 0) {
  256. goto error_put_object;
  257. }
  258. memcpy (&val, value, 4);
  259. object_key->value = malloc (value_len);
  260. if (object_key->value == 0) {
  261. goto error_put_key;
  262. }
  263. memcpy (object_key->key_name, key_name, key_len);
  264. memcpy (object_key->value, value, value_len);
  265. object_key->key_len = key_len;
  266. object_key->value_len = value_len;
  267. list_init (&object_key->list);
  268. list_add (&object_key->list, &instance->key_head);
  269. return (0);
  270. error_put_key:
  271. free (object_key->key_name);
  272. error_put_object:
  273. free (object_key);
  274. error_put:
  275. hdb_handle_put (&object_instance_database, object_handle);
  276. error_exit:
  277. return (-1);
  278. }
  279. static int _clear_object(struct object_instance *instance)
  280. {
  281. struct list_head *list;
  282. int res;
  283. struct object_instance *find_instance = NULL;
  284. struct object_key *object_key = NULL;
  285. for (list = instance->key_head.next;
  286. list != &instance->key_head; ) {
  287. object_key = list_entry (list, struct object_key,
  288. list);
  289. list = list->next;
  290. list_del(&object_key->list);
  291. free(object_key->key_name);
  292. free(object_key->value);
  293. }
  294. for (list = instance->child_head.next;
  295. list != &instance->child_head; ) {
  296. find_instance = list_entry (list, struct object_instance,
  297. child_list);
  298. res = _clear_object(find_instance);
  299. if (res)
  300. return res;
  301. list = list->next;
  302. list_del(&find_instance->child_list);
  303. free(find_instance->object_name);
  304. free(find_instance);
  305. }
  306. return 0;
  307. }
  308. static int object_destroy (
  309. unsigned int object_handle)
  310. {
  311. struct object_instance *instance;
  312. unsigned int res;
  313. res = hdb_handle_get (&object_instance_database,
  314. object_handle, (void *)&instance);
  315. if (res != 0) {
  316. return (res);
  317. }
  318. /* Recursively clear sub-objects & keys */
  319. res = _clear_object(instance);
  320. list_del(&instance->child_list);
  321. free(instance->object_name);
  322. free(instance);
  323. return (res);
  324. }
  325. static int object_valid_set (
  326. unsigned int object_handle,
  327. struct object_valid *object_valid_list,
  328. unsigned int object_valid_list_entries)
  329. {
  330. struct object_instance *instance;
  331. unsigned int res;
  332. res = hdb_handle_get (&object_instance_database,
  333. object_handle, (void *)&instance);
  334. if (res != 0) {
  335. goto error_exit;
  336. }
  337. instance->object_valid_list = object_valid_list;
  338. instance->object_valid_list_entries = object_valid_list_entries;
  339. hdb_handle_put (&object_instance_database, object_handle);
  340. return (0);
  341. error_exit:
  342. return (-1);
  343. }
  344. static int object_key_valid_set (
  345. unsigned int object_handle,
  346. struct object_key_valid *object_key_valid_list,
  347. unsigned int object_key_valid_list_entries)
  348. {
  349. struct object_instance *instance;
  350. unsigned int res;
  351. res = hdb_handle_get (&object_instance_database,
  352. object_handle, (void *)&instance);
  353. if (res != 0) {
  354. goto error_exit;
  355. }
  356. instance->object_key_valid_list = object_key_valid_list;
  357. instance->object_key_valid_list_entries = object_key_valid_list_entries;
  358. hdb_handle_put (&object_instance_database, object_handle);
  359. return (0);
  360. error_exit:
  361. return (-1);
  362. }
  363. /*
  364. * object db reading
  365. */
  366. static int object_find_reset (
  367. unsigned int object_handle)
  368. {
  369. unsigned int res;
  370. struct object_instance *instance;
  371. res = hdb_handle_get (&object_instance_database,
  372. object_handle, (void *)&instance);
  373. if (res != 0) {
  374. goto error_exit;
  375. }
  376. instance->find_child_list = &instance->child_head;
  377. hdb_handle_put (&object_instance_database, object_handle);
  378. return (0);
  379. error_exit:
  380. return (-1);
  381. }
  382. static int object_find (
  383. unsigned int parent_object_handle,
  384. void *object_name,
  385. int object_name_len,
  386. unsigned int *object_handle)
  387. {
  388. unsigned int res;
  389. struct object_instance *instance;
  390. struct object_instance *find_instance = NULL;
  391. struct list_head *list;
  392. unsigned int found = 0;
  393. res = hdb_handle_get (&object_instance_database,
  394. parent_object_handle, (void *)&instance);
  395. if (res != 0) {
  396. goto error_exit;
  397. }
  398. res = -ENOENT;
  399. for (list = instance->find_child_list->next;
  400. list != &instance->child_head; list = list->next) {
  401. find_instance = list_entry (list, struct object_instance,
  402. child_list);
  403. if ((find_instance->object_name_len == object_name_len) &&
  404. (memcmp (find_instance->object_name, object_name,
  405. object_name_len) == 0)) {
  406. found = 1;
  407. break;
  408. }
  409. }
  410. instance->find_child_list = list;
  411. hdb_handle_put (&object_instance_database, parent_object_handle);
  412. if (found) {
  413. *object_handle = find_instance->object_handle;
  414. res = 0;
  415. }
  416. return (res);
  417. error_exit:
  418. return (-1);
  419. }
  420. static int object_key_get (
  421. unsigned int object_handle,
  422. void *key_name,
  423. int key_len,
  424. void **value,
  425. int *value_len)
  426. {
  427. unsigned int res = 0;
  428. struct object_instance *instance;
  429. struct object_key *object_key = NULL;
  430. struct list_head *list;
  431. int found = 0;
  432. res = hdb_handle_get (&object_instance_database,
  433. object_handle, (void *)&instance);
  434. if (res != 0) {
  435. goto error_exit;
  436. }
  437. for (list = instance->key_head.next;
  438. list != &instance->key_head; list = list->next) {
  439. object_key = list_entry (list, struct object_key, list);
  440. if ((object_key->key_len == key_len) &&
  441. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  442. found = 1;
  443. break;
  444. }
  445. }
  446. if (found) {
  447. *value = object_key->value;
  448. if (value_len) {
  449. *value_len = object_key->value_len;
  450. }
  451. }
  452. else {
  453. res = -1;
  454. }
  455. hdb_handle_put (&object_instance_database, object_handle);
  456. return (res);
  457. error_exit:
  458. return (-1);
  459. }
  460. static int object_key_delete (
  461. unsigned int object_handle,
  462. void *key_name,
  463. int key_len,
  464. void *value,
  465. int value_len)
  466. {
  467. unsigned int res;
  468. int ret = 0;
  469. struct object_instance *instance;
  470. struct object_key *object_key = NULL;
  471. struct list_head *list;
  472. int found = 0;
  473. res = hdb_handle_get (&object_instance_database,
  474. object_handle, (void *)&instance);
  475. if (res != 0) {
  476. goto error_exit;
  477. }
  478. for (list = instance->key_head.next;
  479. list != &instance->key_head; list = list->next) {
  480. object_key = list_entry (list, struct object_key, list);
  481. if ((object_key->key_len == key_len) &&
  482. (memcmp (object_key->key_name, key_name, key_len) == 0) &&
  483. (value == NULL ||
  484. (object_key->value_len == value_len &&
  485. (memcmp (object_key->value, value, value_len) == 0)))) {
  486. found = 1;
  487. break;
  488. }
  489. }
  490. if (found) {
  491. list_del(&object_key->list);
  492. free(object_key->key_name);
  493. free(object_key->value);
  494. free(object_key);
  495. }
  496. else {
  497. ret = -1;
  498. errno = ENOENT;
  499. }
  500. hdb_handle_put (&object_instance_database, object_handle);
  501. return (ret);
  502. error_exit:
  503. return (-1);
  504. }
  505. static int object_key_replace (
  506. unsigned int object_handle,
  507. void *key_name,
  508. int key_len,
  509. void *old_value,
  510. int old_value_len,
  511. void *new_value,
  512. int new_value_len)
  513. {
  514. unsigned int res;
  515. int ret = 0;
  516. struct object_instance *instance;
  517. struct object_key *object_key = NULL;
  518. struct list_head *list;
  519. int found = 0;
  520. res = hdb_handle_get (&object_instance_database,
  521. object_handle, (void *)&instance);
  522. if (res != 0) {
  523. goto error_exit;
  524. }
  525. for (list = instance->key_head.next;
  526. list != &instance->key_head; list = list->next) {
  527. object_key = list_entry (list, struct object_key, list);
  528. if ((object_key->key_len == key_len) &&
  529. (memcmp (object_key->key_name, key_name, key_len) == 0) &&
  530. (old_value == NULL ||
  531. (object_key->value_len == old_value_len &&
  532. (memcmp (object_key->value, old_value, old_value_len) == 0)))) {
  533. found = 1;
  534. break;
  535. }
  536. }
  537. if (found) {
  538. int i;
  539. /*
  540. * Do validation check if validation is configured for the parent object
  541. */
  542. if (instance->object_key_valid_list_entries) {
  543. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  544. if ((key_len ==
  545. instance->object_key_valid_list[i].key_len) &&
  546. (memcmp (key_name,
  547. instance->object_key_valid_list[i].key_name,
  548. key_len) == 0)) {
  549. found = 1;
  550. break;
  551. }
  552. }
  553. /*
  554. * Item not found in validation list
  555. */
  556. if (found == 0) {
  557. goto error_put;
  558. } else {
  559. if (instance->object_key_valid_list[i].validate_callback) {
  560. res = instance->object_key_valid_list[i].validate_callback (
  561. key_name, key_len, new_value, new_value_len);
  562. if (res != 0) {
  563. goto error_put;
  564. }
  565. }
  566. }
  567. }
  568. if (new_value_len <= object_key->value_len) {
  569. void *replacement_value;
  570. replacement_value = malloc(new_value_len);
  571. if (!replacement_value)
  572. goto error_exit;
  573. free(object_key->value);
  574. object_key->value = replacement_value;
  575. }
  576. memcpy(object_key->value, new_value, new_value_len);
  577. object_key->value_len = new_value_len;
  578. }
  579. else {
  580. ret = -1;
  581. errno = ENOENT;
  582. }
  583. hdb_handle_put (&object_instance_database, object_handle);
  584. return (ret);
  585. error_put:
  586. hdb_handle_put (&object_instance_database, object_handle);
  587. error_exit:
  588. return (-1);
  589. }
  590. static int object_priv_get (
  591. unsigned int object_handle,
  592. void **priv)
  593. {
  594. int res;
  595. struct object_instance *object_instance;
  596. res = hdb_handle_get (&object_instance_database,
  597. object_handle, (void *)&object_instance);
  598. if (res != 0) {
  599. goto error_exit;
  600. }
  601. *priv = object_instance->priv;
  602. hdb_handle_put (&object_instance_database, object_handle);
  603. return (0);
  604. error_exit:
  605. return (-1);
  606. }
  607. static int _dump_object(struct object_instance *instance, FILE *file, int depth)
  608. {
  609. struct list_head *list;
  610. int res;
  611. int i;
  612. struct object_instance *find_instance = NULL;
  613. struct object_key *object_key = NULL;
  614. char stringbuf1[1024];
  615. char stringbuf2[1024];
  616. memcpy(stringbuf1, instance->object_name, instance->object_name_len);
  617. stringbuf1[instance->object_name_len] = '\0';
  618. for (i=0; i<depth; i++)
  619. fprintf(file, " ");
  620. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  621. fprintf(file, "%s {\n", stringbuf1);
  622. for (list = instance->key_head.next;
  623. list != &instance->key_head; list = list->next) {
  624. object_key = list_entry (list, struct object_key,
  625. list);
  626. memcpy(stringbuf1, object_key->key_name, object_key->key_len);
  627. stringbuf1[object_key->key_len] = '\0';
  628. memcpy(stringbuf2, object_key->value, object_key->value_len);
  629. stringbuf2[object_key->value_len] = '\0';
  630. for (i=0; i<depth+1; i++)
  631. fprintf(file, " ");
  632. fprintf(file, "%s: %s\n", stringbuf1, stringbuf2);
  633. }
  634. for (list = instance->child_head.next;
  635. list != &instance->child_head; list = list->next) {
  636. find_instance = list_entry (list, struct object_instance,
  637. child_list);
  638. res = _dump_object(find_instance, file, depth+1);
  639. if (res)
  640. return res;
  641. }
  642. for (i=0; i<depth; i++)
  643. fprintf(file, " ");
  644. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  645. fprintf(file, "}\n");
  646. return 0;
  647. }
  648. static int object_key_iter_reset(unsigned int object_handle)
  649. {
  650. unsigned int res;
  651. struct object_instance *instance;
  652. res = hdb_handle_get (&object_instance_database,
  653. object_handle, (void *)&instance);
  654. if (res != 0) {
  655. goto error_exit;
  656. }
  657. instance->iter_key_list = &instance->key_head;
  658. hdb_handle_put (&object_instance_database, object_handle);
  659. return (0);
  660. error_exit:
  661. return (-1);
  662. }
  663. static int object_key_iter(unsigned int parent_object_handle,
  664. void **key_name,
  665. int *key_len,
  666. void **value,
  667. int *value_len)
  668. {
  669. unsigned int res;
  670. struct object_instance *instance;
  671. struct object_key *find_key = NULL;
  672. struct list_head *list;
  673. unsigned int found = 0;
  674. res = hdb_handle_get (&object_instance_database,
  675. parent_object_handle, (void *)&instance);
  676. if (res != 0) {
  677. goto error_exit;
  678. }
  679. res = -ENOENT;
  680. list = instance->iter_key_list->next;
  681. if (list != &instance->key_head) {
  682. find_key = list_entry (list, struct object_key, list);
  683. found = 1;
  684. }
  685. instance->iter_key_list = list;
  686. if (found) {
  687. *key_name = find_key->key_name;
  688. if (key_len)
  689. *key_len = find_key->key_len;
  690. *value = find_key->value;
  691. if (value_len)
  692. *value_len = find_key->value_len;
  693. res = 0;
  694. }
  695. else {
  696. res = -1;
  697. }
  698. hdb_handle_put (&object_instance_database, parent_object_handle);
  699. return (res);
  700. error_exit:
  701. return (-1);
  702. }
  703. static int object_iter_reset(unsigned int parent_object_handle)
  704. {
  705. unsigned int res;
  706. struct object_instance *instance;
  707. res = hdb_handle_get (&object_instance_database,
  708. parent_object_handle, (void *)&instance);
  709. if (res != 0) {
  710. goto error_exit;
  711. }
  712. instance->iter_list = &instance->child_head;
  713. hdb_handle_put (&object_instance_database, parent_object_handle);
  714. return (0);
  715. error_exit:
  716. return (-1);
  717. }
  718. static int object_iter(unsigned int parent_object_handle,
  719. void **object_name,
  720. int *name_len,
  721. unsigned int *object_handle)
  722. {
  723. unsigned int res;
  724. struct object_instance *instance;
  725. struct object_instance *find_instance = NULL;
  726. struct list_head *list;
  727. unsigned int found = 0;
  728. res = hdb_handle_get (&object_instance_database,
  729. parent_object_handle, (void *)&instance);
  730. if (res != 0) {
  731. goto error_exit;
  732. }
  733. res = -ENOENT;
  734. list = instance->iter_list->next;
  735. if (list != &instance->child_head) {
  736. find_instance = list_entry (list, struct object_instance,
  737. child_list);
  738. found = 1;
  739. }
  740. instance->iter_list = list;
  741. if (found) {
  742. *object_handle = find_instance->object_handle;
  743. *object_name = find_instance->object_name;
  744. *name_len = find_instance->object_name_len;
  745. res = 0;
  746. }
  747. else {
  748. res = -1;
  749. }
  750. return (res);
  751. error_exit:
  752. return (-1);
  753. }
  754. static int object_find_from(unsigned int parent_object_handle,
  755. unsigned int start_pos,
  756. void *object_name,
  757. int object_name_len,
  758. unsigned int *object_handle,
  759. unsigned int *next_pos)
  760. {
  761. unsigned int res;
  762. unsigned int pos = 0;
  763. struct object_instance *instance;
  764. struct object_instance *find_instance = NULL;
  765. struct list_head *list;
  766. unsigned int found = 0;
  767. res = hdb_handle_get (&object_instance_database,
  768. parent_object_handle, (void *)&instance);
  769. if (res != 0) {
  770. goto error_exit;
  771. }
  772. res = -ENOENT;
  773. for (list = instance->child_head.next;
  774. list != &instance->child_head; list = list->next) {
  775. find_instance = list_entry (list, struct object_instance,
  776. child_list);
  777. if ((find_instance->object_name_len == object_name_len) &&
  778. (memcmp (find_instance->object_name, object_name,
  779. object_name_len) == 0)) {
  780. if (pos++ == start_pos) {
  781. found = 1;
  782. break;
  783. }
  784. }
  785. }
  786. hdb_handle_put (&object_instance_database, parent_object_handle);
  787. if (found) {
  788. *object_handle = find_instance->object_handle;
  789. res = 0;
  790. }
  791. *next_pos = pos;
  792. return (res);
  793. error_exit:
  794. return (-1);
  795. }
  796. static int object_iter_from(unsigned int parent_object_handle,
  797. unsigned int start_pos,
  798. void **object_name,
  799. int *name_len,
  800. unsigned int *object_handle)
  801. {
  802. unsigned int res;
  803. unsigned int pos = 0;
  804. struct object_instance *instance;
  805. struct object_instance *find_instance = NULL;
  806. struct list_head *list;
  807. unsigned int found = 0;
  808. res = hdb_handle_get (&object_instance_database,
  809. parent_object_handle, (void *)&instance);
  810. if (res != 0) {
  811. goto error_exit;
  812. }
  813. res = -ENOENT;
  814. for (list = instance->child_head.next;
  815. list != &instance->child_head; list = list->next) {
  816. find_instance = list_entry (list, struct object_instance,
  817. child_list);
  818. if (pos++ == start_pos) {
  819. found = 1;
  820. break;
  821. }
  822. }
  823. if (found) {
  824. *object_handle = find_instance->object_handle;
  825. *object_name = find_instance->object_name;
  826. *name_len = find_instance->object_name_len;
  827. res = 0;
  828. }
  829. else {
  830. res = -1;
  831. }
  832. return (res);
  833. error_exit:
  834. return (-1);
  835. }
  836. static int object_key_iter_from(unsigned int parent_object_handle,
  837. unsigned int start_pos,
  838. void **key_name,
  839. int *key_len,
  840. void **value,
  841. int *value_len)
  842. {
  843. unsigned int pos = 0;
  844. unsigned int res;
  845. struct object_instance *instance;
  846. struct object_key *find_key = NULL;
  847. struct list_head *list;
  848. unsigned int found = 0;
  849. res = hdb_handle_get (&object_instance_database,
  850. parent_object_handle, (void *)&instance);
  851. if (res != 0) {
  852. goto error_exit;
  853. }
  854. res = -ENOENT;
  855. for (list = instance->key_head.next;
  856. list != &instance->key_head; list = list->next) {
  857. find_key = list_entry (list, struct object_key, list);
  858. if (pos++ == start_pos) {
  859. found = 1;
  860. break;
  861. }
  862. }
  863. if (found) {
  864. *key_name = find_key->key_name;
  865. if (key_len)
  866. *key_len = find_key->key_len;
  867. *value = find_key->value;
  868. if (value_len)
  869. *value_len = find_key->value_len;
  870. res = 0;
  871. }
  872. else {
  873. res = -1;
  874. }
  875. hdb_handle_put (&object_instance_database, parent_object_handle);
  876. return (res);
  877. error_exit:
  878. return (-1);
  879. }
  880. static int object_parent_get(unsigned int object_handle,
  881. unsigned int *parent_handle)
  882. {
  883. struct object_instance *instance;
  884. unsigned int res;
  885. res = hdb_handle_get (&object_instance_database,
  886. object_handle, (void *)&instance);
  887. if (res != 0) {
  888. return (res);
  889. }
  890. if (object_handle == OBJECT_PARENT_HANDLE)
  891. *parent_handle = 0;
  892. else
  893. *parent_handle = instance->parent_handle;
  894. hdb_handle_put (&object_instance_database, object_handle);
  895. return (0);
  896. }
  897. static int object_dump(unsigned int object_handle,
  898. FILE *file)
  899. {
  900. struct object_instance *instance;
  901. unsigned int res;
  902. res = hdb_handle_get (&object_instance_database,
  903. object_handle, (void *)&instance);
  904. if (res != 0) {
  905. return (res);
  906. }
  907. res = _dump_object(instance, file, -1);
  908. hdb_handle_put (&object_instance_database, object_handle);
  909. return (res);
  910. }
  911. static int object_write_config(char **error_string)
  912. {
  913. struct config_iface_ver0 **modules;
  914. int num_modules;
  915. int i;
  916. int res;
  917. main_get_config_modules(&modules, &num_modules);
  918. for (i=0; i<num_modules; i++) {
  919. if (modules[i]->config_writeconfig) {
  920. res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  921. if (res)
  922. return res;
  923. }
  924. }
  925. return 0;
  926. }
  927. struct objdb_iface_ver0 objdb_iface = {
  928. .objdb_init = objdb_init,
  929. .object_create = object_create,
  930. .object_priv_set = object_priv_set,
  931. .object_key_create = object_key_create,
  932. .object_key_delete = object_key_delete,
  933. .object_key_replace = object_key_replace,
  934. .object_destroy = object_destroy,
  935. .object_valid_set = object_valid_set,
  936. .object_key_valid_set = object_key_valid_set,
  937. .object_find_reset = object_find_reset,
  938. .object_find = object_find,
  939. .object_find_from = object_find_from,
  940. .object_key_get = object_key_get,
  941. .object_key_iter = object_key_iter,
  942. .object_key_iter_reset = object_key_iter_reset,
  943. .object_key_iter_from = object_key_iter_from,
  944. .object_iter = object_iter,
  945. .object_iter_reset = object_iter_reset,
  946. .object_iter_from = object_iter_from,
  947. .object_priv_get = object_priv_get,
  948. .object_parent_get = object_parent_get,
  949. .object_dump = object_dump,
  950. .object_write_config = object_write_config,
  951. };
  952. struct lcr_iface objdb_iface_ver0[1] = {
  953. {
  954. .name = "objdb",
  955. .version = 0,
  956. .versions_replace = 0,
  957. .versions_replace_count = 0,
  958. .dependencies = 0,
  959. .dependency_count = 0,
  960. .constructor = NULL,
  961. .destructor = NULL,
  962. .interfaces = NULL,
  963. }
  964. };
  965. struct lcr_comp objdb_comp_ver0 = {
  966. .iface_count = 1,
  967. .ifaces = objdb_iface_ver0
  968. };
  969. __attribute__ ((constructor)) static void objdb_comp_register (void) {
  970. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  971. lcr_component_register (&objdb_comp_ver0);
  972. }