objdb.c 23 KB

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