objdb.c 28 KB

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