objdb.c 13 KB

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