objdb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. struct list_head key_head;
  52. struct list_head child_head;
  53. struct list_head child_list;
  54. struct list_head *find_child_list;
  55. void *priv;
  56. struct object_valid *object_valid_list;
  57. int object_valid_list_entries;
  58. struct object_key_valid *object_key_valid_list;
  59. int object_key_valid_list_entries;
  60. };
  61. static struct hdb_handle_database object_instance_database = {
  62. .handle_count = 0,
  63. .handles = 0,
  64. .iterator = 0
  65. };
  66. static int objdb_init (void)
  67. {
  68. unsigned int handle;
  69. struct object_instance *instance;
  70. unsigned int res;
  71. res = hdb_handle_create (&object_instance_database,
  72. sizeof (struct object_instance), &handle);
  73. if (res != 0) {
  74. goto error_exit;
  75. }
  76. res = hdb_handle_get (&object_instance_database,
  77. handle, (void *)&instance);
  78. if (res != 0) {
  79. goto error_destroy;
  80. }
  81. instance->find_child_list = &instance->child_head;
  82. instance->object_name = "parent";
  83. instance->object_name_len = strlen ("parent");
  84. instance->object_handle = handle;
  85. instance->priv = NULL;
  86. instance->object_valid_list = NULL;
  87. instance->object_valid_list_entries = 0;
  88. list_init (&instance->key_head);
  89. list_init (&instance->child_head);
  90. list_init (&instance->child_list);
  91. hdb_handle_put (&object_instance_database, handle);
  92. return (0);
  93. error_destroy:
  94. hdb_handle_destroy (&object_instance_database, handle);
  95. error_exit:
  96. return (-1);
  97. }
  98. /*
  99. * object db create/destroy/set
  100. */
  101. static int object_create (
  102. unsigned int parent_object_handle,
  103. unsigned int *object_handle,
  104. void *object_name,
  105. unsigned int object_name_len)
  106. {
  107. struct object_instance *object_instance;
  108. struct object_instance *parent_instance;
  109. unsigned int res;
  110. int found = 0;
  111. int i;
  112. res = hdb_handle_get (&object_instance_database,
  113. parent_object_handle, (void *)&parent_instance);
  114. if (res != 0) {
  115. goto error_exit;
  116. }
  117. /*
  118. * Do validation check if validation is configured for the parent object
  119. */
  120. if (parent_instance->object_valid_list_entries) {
  121. for (i = 0; i < parent_instance->object_valid_list_entries; i++) {
  122. if ((object_name_len ==
  123. parent_instance->object_valid_list[i].object_len) &&
  124. (memcmp (object_name,
  125. parent_instance->object_valid_list[i].object_name,
  126. object_name_len) == 0)) {
  127. found = 1;
  128. break;
  129. }
  130. }
  131. /*
  132. * Item not found in validation list
  133. */
  134. if (found == 0) {
  135. goto error_object_put;
  136. }
  137. }
  138. res = hdb_handle_create (&object_instance_database,
  139. sizeof (struct object_instance), object_handle);
  140. if (res != 0) {
  141. goto error_object_put;
  142. }
  143. res = hdb_handle_get (&object_instance_database,
  144. *object_handle, (void *)&object_instance);
  145. if (res != 0) {
  146. goto error_destroy;
  147. }
  148. list_init (&object_instance->key_head);
  149. list_init (&object_instance->child_head);
  150. list_init (&object_instance->child_list);
  151. object_instance->object_name = malloc (object_name_len);
  152. if (object_instance->object_name == 0) {
  153. goto error_put_destroy;
  154. }
  155. memcpy (object_instance->object_name, object_name, object_name_len);
  156. object_instance->object_name_len = object_name_len;
  157. list_add (&object_instance->child_list, &parent_instance->child_head);
  158. object_instance->object_handle = *object_handle;
  159. object_instance->find_child_list = &object_instance->child_head;
  160. object_instance->priv = NULL;
  161. object_instance->object_valid_list = NULL;
  162. object_instance->object_valid_list_entries = 0;
  163. hdb_handle_put (&object_instance_database, *object_handle);
  164. hdb_handle_put (&object_instance_database, parent_object_handle);
  165. return (0);
  166. error_put_destroy:
  167. hdb_handle_put (&object_instance_database, *object_handle);
  168. error_destroy:
  169. hdb_handle_destroy (&object_instance_database, *object_handle);
  170. error_object_put:
  171. hdb_handle_put (&object_instance_database, parent_object_handle);
  172. error_exit:
  173. return (-1);
  174. }
  175. static int object_priv_set (
  176. unsigned int object_handle,
  177. void *priv)
  178. {
  179. int res;
  180. struct object_instance *object_instance;
  181. res = hdb_handle_get (&object_instance_database,
  182. object_handle, (void *)&object_instance);
  183. if (res != 0) {
  184. goto error_exit;
  185. }
  186. object_instance->priv = priv;
  187. hdb_handle_put (&object_instance_database, object_handle);
  188. return (0);
  189. error_exit:
  190. return (-1);
  191. }
  192. static int object_key_create (
  193. unsigned int object_handle,
  194. void *key_name,
  195. int key_len,
  196. void *value,
  197. int value_len)
  198. {
  199. struct object_instance *instance;
  200. struct object_key *object_key;
  201. unsigned int res;
  202. int found = 0;
  203. int i;
  204. unsigned int val;
  205. res = hdb_handle_get (&object_instance_database,
  206. object_handle, (void *)&instance);
  207. if (res != 0) {
  208. goto error_exit;
  209. }
  210. /*
  211. * Do validation check if validation is configured for the parent object
  212. */
  213. if (instance->object_key_valid_list_entries) {
  214. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  215. if ((key_len ==
  216. instance->object_key_valid_list[i].key_len) &&
  217. (memcmp (key_name,
  218. instance->object_key_valid_list[i].key_name,
  219. key_len) == 0)) {
  220. found = 1;
  221. break;
  222. }
  223. }
  224. /*
  225. * Item not found in validation list
  226. */
  227. if (found == 0) {
  228. goto error_put;
  229. } else {
  230. if (instance->object_key_valid_list[i].validate_callback) {
  231. res = instance->object_key_valid_list[i].validate_callback (
  232. key_name, key_len, value, value_len);
  233. if (res != 0) {
  234. goto error_put;
  235. }
  236. }
  237. }
  238. }
  239. object_key = malloc (sizeof (struct object_key));
  240. if (object_key == 0) {
  241. goto error_put;
  242. }
  243. object_key->key_name = malloc (key_len);
  244. if (object_key->key_name == 0) {
  245. goto error_put_object;
  246. }
  247. memcpy (&val, value, 4);
  248. object_key->value = malloc (value_len);
  249. if (object_key->value == 0) {
  250. goto error_put_key;
  251. }
  252. memcpy (object_key->key_name, key_name, key_len);
  253. memcpy (object_key->value, value, value_len);
  254. object_key->key_len = key_len;
  255. object_key->value_len = value_len;
  256. list_init (&object_key->list);
  257. list_add (&object_key->list, &instance->key_head);
  258. return (0);
  259. error_put_key:
  260. free (object_key->key_name);
  261. error_put_object:
  262. free (object_key);
  263. error_put:
  264. hdb_handle_put (&object_instance_database, object_handle);
  265. error_exit:
  266. return (-1);
  267. }
  268. static int object_destroy (
  269. unsigned int object_handle)
  270. {
  271. return (0);
  272. }
  273. static int object_valid_set (
  274. unsigned int object_handle,
  275. struct object_valid *object_valid_list,
  276. unsigned int object_valid_list_entries)
  277. {
  278. struct object_instance *instance;
  279. unsigned int res;
  280. res = hdb_handle_get (&object_instance_database,
  281. object_handle, (void *)&instance);
  282. if (res != 0) {
  283. goto error_exit;
  284. }
  285. instance->object_valid_list = object_valid_list;
  286. instance->object_valid_list_entries = object_valid_list_entries;
  287. hdb_handle_put (&object_instance_database, object_handle);
  288. return (0);
  289. error_exit:
  290. return (-1);
  291. }
  292. static int object_key_valid_set (
  293. unsigned int object_handle,
  294. struct object_key_valid *object_key_valid_list,
  295. unsigned int object_key_valid_list_entries)
  296. {
  297. struct object_instance *instance;
  298. unsigned int res;
  299. res = hdb_handle_get (&object_instance_database,
  300. object_handle, (void *)&instance);
  301. if (res != 0) {
  302. goto error_exit;
  303. }
  304. instance->object_key_valid_list = object_key_valid_list;
  305. instance->object_key_valid_list_entries = object_key_valid_list_entries;
  306. hdb_handle_put (&object_instance_database, object_handle);
  307. return (0);
  308. error_exit:
  309. return (-1);
  310. }
  311. /*
  312. * object db reading
  313. */
  314. static int object_find_reset (
  315. unsigned int object_handle)
  316. {
  317. unsigned int res;
  318. struct object_instance *instance;
  319. res = hdb_handle_get (&object_instance_database,
  320. object_handle, (void *)&instance);
  321. if (res != 0) {
  322. goto error_exit;
  323. }
  324. instance->find_child_list = &instance->child_head;
  325. hdb_handle_put (&object_instance_database, object_handle);
  326. return (0);
  327. error_exit:
  328. return (-1);
  329. }
  330. static int object_find (
  331. unsigned int parent_object_handle,
  332. void *object_name,
  333. int object_name_len,
  334. unsigned int *object_handle)
  335. {
  336. unsigned int res;
  337. struct object_instance *instance;
  338. struct object_instance *find_instance = NULL;
  339. struct list_head *list;
  340. unsigned int found = 0;
  341. res = hdb_handle_get (&object_instance_database,
  342. parent_object_handle, (void *)&instance);
  343. if (res != 0) {
  344. goto error_exit;
  345. }
  346. res = -ENOENT;
  347. for (list = instance->find_child_list->next;
  348. list != &instance->child_head; list = list->next) {
  349. find_instance = list_entry (list, struct object_instance,
  350. child_list);
  351. if ((find_instance->object_name_len == object_name_len) &&
  352. (memcmp (find_instance->object_name, object_name,
  353. object_name_len) == 0)) {
  354. found = 1;
  355. break;
  356. }
  357. }
  358. instance->find_child_list = list;
  359. hdb_handle_put (&object_instance_database, parent_object_handle);
  360. if (found) {
  361. *object_handle = find_instance->object_handle;
  362. res = 0;
  363. }
  364. return (res);
  365. error_exit:
  366. return (-1);
  367. }
  368. static int object_key_get (
  369. unsigned int object_handle,
  370. void *key_name,
  371. int key_len,
  372. void **value,
  373. int *value_len)
  374. {
  375. unsigned int res;
  376. struct object_instance *instance;
  377. struct object_key *object_key = NULL;
  378. struct list_head *list;
  379. int found = 0;
  380. res = hdb_handle_get (&object_instance_database,
  381. object_handle, (void *)&instance);
  382. if (res != 0) {
  383. goto error_exit;
  384. }
  385. for (list = instance->key_head.next;
  386. list != &instance->key_head; list = list->next) {
  387. object_key = list_entry (list, struct object_key, list);
  388. if ((object_key->key_len == key_len) &&
  389. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  390. found = 1;
  391. break;
  392. }
  393. }
  394. if (found) {
  395. *value = object_key->value;
  396. if (value_len) {
  397. *value_len = object_key->value_len;
  398. }
  399. }
  400. hdb_handle_put (&object_instance_database, object_handle);
  401. return (0);
  402. error_exit:
  403. return (-1);
  404. }
  405. static int object_priv_get (
  406. unsigned int object_handle,
  407. void **priv)
  408. {
  409. int res;
  410. struct object_instance *object_instance;
  411. res = hdb_handle_get (&object_instance_database,
  412. object_handle, (void *)&object_instance);
  413. if (res != 0) {
  414. goto error_exit;
  415. }
  416. *priv = object_instance->priv;
  417. hdb_handle_put (&object_instance_database, object_handle);
  418. return (0);
  419. error_exit:
  420. return (-1);
  421. }
  422. struct objdb_iface_ver0 objdb_iface = {
  423. .objdb_init = objdb_init,
  424. .object_create = object_create,
  425. .object_priv_set = object_priv_set,
  426. .object_key_create = object_key_create,
  427. .object_destroy = object_destroy,
  428. .object_valid_set = object_valid_set,
  429. .object_key_valid_set = object_key_valid_set,
  430. .object_find_reset = object_find_reset,
  431. .object_find = object_find,
  432. .object_key_get = object_key_get,
  433. .object_priv_get = object_priv_set
  434. };
  435. struct lcr_iface objdb_iface_ver0[1] = {
  436. {
  437. .name = "objdb",
  438. .version = 0,
  439. .versions_replace = 0,
  440. .versions_replace_count = 0,
  441. .dependencies = 0,
  442. .dependency_count = 0,
  443. .constructor = NULL,
  444. .destructor = NULL,
  445. .interfaces = (void **)&objdb_iface,
  446. }
  447. };
  448. struct lcr_comp objdb_comp_ver0 = {
  449. .iface_count = 1,
  450. .ifaces = objdb_iface_ver0
  451. };
  452. __attribute__ ((constructor)) static void objdb_comp_register (void) {
  453. lcr_component_register (&objdb_comp_ver0);
  454. }