objdb.c 26 KB

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