objdb.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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_tracker {
  51. unsigned int object_handle;
  52. void * data_pt;
  53. object_track_depth_t depth;
  54. object_key_change_notify_fn_t key_change_notify_fn;
  55. object_create_notify_fn_t object_create_notify_fn;
  56. object_destroy_notify_fn_t object_destroy_notify_fn;
  57. struct list_head tracker_list;
  58. struct list_head object_list;
  59. };
  60. struct object_instance {
  61. void *object_name;
  62. int object_name_len;
  63. unsigned int object_handle;
  64. unsigned int parent_handle;
  65. struct list_head key_head;
  66. struct list_head child_head;
  67. struct list_head child_list;
  68. struct list_head *find_child_list;
  69. struct list_head *iter_key_list;
  70. struct list_head *iter_list;
  71. void *priv;
  72. struct object_valid *object_valid_list;
  73. int object_valid_list_entries;
  74. struct object_key_valid *object_key_valid_list;
  75. int object_key_valid_list_entries;
  76. struct list_head track_head;
  77. };
  78. struct object_find_instance {
  79. struct list_head *find_child_list;
  80. struct list_head *child_head;
  81. void *object_name;
  82. int object_len;
  83. };
  84. struct objdb_iface_ver0 objdb_iface;
  85. struct list_head objdb_trackers_head;
  86. static struct hdb_handle_database object_instance_database = {
  87. .handle_count = 0,
  88. .handles = 0,
  89. .iterator = 0,
  90. .mutex = PTHREAD_MUTEX_INITIALIZER
  91. };
  92. static struct hdb_handle_database object_find_instance_database = {
  93. .handle_count = 0,
  94. .handles = 0,
  95. .iterator = 0,
  96. .mutex = PTHREAD_MUTEX_INITIALIZER
  97. };
  98. static int objdb_init (void)
  99. {
  100. unsigned int handle;
  101. struct object_instance *instance;
  102. unsigned int res;
  103. res = hdb_handle_create (&object_instance_database,
  104. sizeof (struct object_instance), &handle);
  105. if (res != 0) {
  106. goto error_exit;
  107. }
  108. res = hdb_handle_get (&object_instance_database,
  109. handle, (void *)&instance);
  110. if (res != 0) {
  111. goto error_destroy;
  112. }
  113. instance->find_child_list = &instance->child_head;
  114. instance->object_name = "parent";
  115. instance->object_name_len = strlen ("parent");
  116. instance->object_handle = handle;
  117. instance->priv = NULL;
  118. instance->object_valid_list = NULL;
  119. instance->object_valid_list_entries = 0;
  120. list_init (&instance->key_head);
  121. list_init (&instance->child_head);
  122. list_init (&instance->child_list);
  123. list_init (&instance->track_head);
  124. list_init (&objdb_trackers_head);
  125. hdb_handle_put (&object_instance_database, handle);
  126. return (0);
  127. error_destroy:
  128. hdb_handle_destroy (&object_instance_database, handle);
  129. error_exit:
  130. return (-1);
  131. }
  132. static int _object_notify_deleted_children(struct object_instance *parent_pt)
  133. {
  134. struct list_head *list;
  135. struct list_head *notify_list;
  136. int res;
  137. struct object_instance *obj_pt = NULL;
  138. struct object_tracker * tracker_pt;
  139. for (list = parent_pt->child_head.next;
  140. list != &parent_pt->child_head; list = list->next) {
  141. obj_pt = list_entry(list, struct object_instance,
  142. child_list);
  143. res = _object_notify_deleted_children(obj_pt);
  144. if (res)
  145. return res;
  146. for (notify_list = obj_pt->track_head.next;
  147. notify_list != &obj_pt->track_head;
  148. notify_list = notify_list->next) {
  149. tracker_pt = list_entry (notify_list, struct object_tracker, object_list);
  150. if ((tracker_pt != NULL) &&
  151. (tracker_pt->object_destroy_notify_fn != NULL))
  152. tracker_pt->object_destroy_notify_fn(parent_pt->object_handle,
  153. obj_pt->object_name,
  154. obj_pt->object_name_len,
  155. tracker_pt->data_pt);
  156. }
  157. }
  158. return 0;
  159. }
  160. static void object_created_notification(unsigned int object_handle,
  161. unsigned int parent_object_handle,
  162. void *name_pt, int name_len)
  163. {
  164. struct list_head * list;
  165. struct object_instance * obj_pt;
  166. struct object_tracker * tracker_pt;
  167. unsigned int obj_handle = object_handle;
  168. unsigned int res;
  169. do {
  170. res = hdb_handle_get (&object_instance_database,
  171. obj_handle, (void *)&obj_pt);
  172. for (list = obj_pt->track_head.next;
  173. list != &obj_pt->track_head; list = list->next) {
  174. tracker_pt = list_entry (list, struct object_tracker, object_list);
  175. if (((obj_handle == parent_object_handle) ||
  176. (tracker_pt->depth == OBJECT_TRACK_DEPTH_RECURSIVE)) &&
  177. (tracker_pt->object_create_notify_fn != NULL)) {
  178. tracker_pt->object_create_notify_fn(object_handle, parent_object_handle,
  179. name_pt, name_len,
  180. tracker_pt->data_pt);
  181. }
  182. }
  183. hdb_handle_put (&object_instance_database, obj_handle);
  184. obj_handle = obj_pt->parent_handle;
  185. } while (obj_pt->object_handle != OBJECT_PARENT_HANDLE);
  186. }
  187. static void object_pre_deletion_notification(unsigned int object_handle,
  188. unsigned int parent_object_handle,
  189. void *name_pt, int name_len)
  190. {
  191. struct list_head * list;
  192. struct object_instance * obj_pt;
  193. struct object_tracker * tracker_pt;
  194. unsigned int obj_handle = object_handle;
  195. unsigned int res;
  196. do {
  197. res = hdb_handle_get (&object_instance_database,
  198. obj_handle, (void *)&obj_pt);
  199. for (list = obj_pt->track_head.next;
  200. list != &obj_pt->track_head; list = list->next) {
  201. tracker_pt = list_entry (list, struct object_tracker, object_list);
  202. if (((obj_handle == parent_object_handle) ||
  203. (tracker_pt->depth == OBJECT_TRACK_DEPTH_RECURSIVE)) &&
  204. (tracker_pt->object_destroy_notify_fn != NULL)) {
  205. tracker_pt->object_destroy_notify_fn(parent_object_handle,
  206. name_pt, name_len,
  207. tracker_pt->data_pt);
  208. }
  209. }
  210. /* notify child object listeners */
  211. if (obj_handle == object_handle)
  212. _object_notify_deleted_children(obj_pt);
  213. hdb_handle_put (&object_instance_database, obj_handle);
  214. obj_handle = obj_pt->parent_handle;
  215. } while (obj_pt->object_handle != OBJECT_PARENT_HANDLE);
  216. }
  217. static void object_key_changed_notification(unsigned int object_handle,
  218. void *name_pt, int name_len,
  219. void *value_pt, int value_len,
  220. object_change_type_t type)
  221. {
  222. struct list_head * list;
  223. struct object_instance * obj_pt;
  224. struct object_instance * owner_pt = NULL;
  225. struct object_tracker * tracker_pt;
  226. unsigned int obj_handle = object_handle;
  227. unsigned int res;
  228. do {
  229. res = hdb_handle_get (&object_instance_database,
  230. obj_handle, (void *)&obj_pt);
  231. if (owner_pt == NULL)
  232. owner_pt = obj_pt;
  233. for (list = obj_pt->track_head.next;
  234. list != &obj_pt->track_head; list = list->next) {
  235. tracker_pt = list_entry (list, struct object_tracker, object_list);
  236. if (((obj_handle == object_handle) ||
  237. (tracker_pt->depth == OBJECT_TRACK_DEPTH_RECURSIVE)) &&
  238. (tracker_pt->key_change_notify_fn != NULL))
  239. tracker_pt->key_change_notify_fn(type, obj_pt->parent_handle, object_handle,
  240. owner_pt->object_name, owner_pt->object_name_len,
  241. name_pt, name_len,
  242. value_pt, value_len,
  243. tracker_pt->data_pt);
  244. }
  245. hdb_handle_put (&object_instance_database, obj_handle);
  246. obj_handle = obj_pt->parent_handle;
  247. } while (obj_pt->object_handle != OBJECT_PARENT_HANDLE);
  248. }
  249. /*
  250. * object db create/destroy/set
  251. */
  252. static int object_create (
  253. unsigned int parent_object_handle,
  254. unsigned int *object_handle,
  255. void *object_name,
  256. unsigned int object_name_len)
  257. {
  258. struct object_instance *object_instance;
  259. struct object_instance *parent_instance;
  260. unsigned int res;
  261. int found = 0;
  262. int i;
  263. res = hdb_handle_get (&object_instance_database,
  264. parent_object_handle, (void *)&parent_instance);
  265. if (res != 0) {
  266. goto error_exit;
  267. }
  268. /*
  269. * Do validation check if validation is configured for the parent object
  270. */
  271. if (parent_instance->object_valid_list_entries) {
  272. for (i = 0; i < parent_instance->object_valid_list_entries; i++) {
  273. if ((object_name_len ==
  274. parent_instance->object_valid_list[i].object_len) &&
  275. (memcmp (object_name,
  276. parent_instance->object_valid_list[i].object_name,
  277. object_name_len) == 0)) {
  278. found = 1;
  279. break;
  280. }
  281. }
  282. /*
  283. * Item not found in validation list
  284. */
  285. if (found == 0) {
  286. goto error_object_put;
  287. }
  288. }
  289. res = hdb_handle_create (&object_instance_database,
  290. sizeof (struct object_instance), object_handle);
  291. if (res != 0) {
  292. goto error_object_put;
  293. }
  294. res = hdb_handle_get (&object_instance_database,
  295. *object_handle, (void *)&object_instance);
  296. if (res != 0) {
  297. goto error_destroy;
  298. }
  299. list_init (&object_instance->key_head);
  300. list_init (&object_instance->child_head);
  301. list_init (&object_instance->child_list);
  302. list_init (&object_instance->track_head);
  303. object_instance->object_name = malloc (object_name_len);
  304. if (object_instance->object_name == 0) {
  305. goto error_put_destroy;
  306. }
  307. memcpy (object_instance->object_name, object_name, object_name_len);
  308. object_instance->object_name_len = object_name_len;
  309. list_add (&object_instance->child_list, &parent_instance->child_head);
  310. object_instance->object_handle = *object_handle;
  311. object_instance->find_child_list = &object_instance->child_head;
  312. object_instance->iter_key_list = &object_instance->key_head;
  313. object_instance->iter_list = &object_instance->child_head;
  314. object_instance->priv = NULL;
  315. object_instance->object_valid_list = NULL;
  316. object_instance->object_valid_list_entries = 0;
  317. object_instance->parent_handle = parent_object_handle;
  318. hdb_handle_put (&object_instance_database, *object_handle);
  319. hdb_handle_put (&object_instance_database, parent_object_handle);
  320. object_created_notification(object_instance->object_handle,
  321. object_instance->parent_handle,
  322. object_instance->object_name,
  323. object_instance->object_name_len);
  324. return (0);
  325. error_put_destroy:
  326. hdb_handle_put (&object_instance_database, *object_handle);
  327. error_destroy:
  328. hdb_handle_destroy (&object_instance_database, *object_handle);
  329. error_object_put:
  330. hdb_handle_put (&object_instance_database, parent_object_handle);
  331. error_exit:
  332. return (-1);
  333. }
  334. static int object_priv_set (
  335. unsigned int object_handle,
  336. void *priv)
  337. {
  338. int res;
  339. struct object_instance *object_instance;
  340. res = hdb_handle_get (&object_instance_database,
  341. object_handle, (void *)&object_instance);
  342. if (res != 0) {
  343. goto error_exit;
  344. }
  345. object_instance->priv = priv;
  346. hdb_handle_put (&object_instance_database, object_handle);
  347. return (0);
  348. error_exit:
  349. return (-1);
  350. }
  351. static int object_key_create (
  352. unsigned int object_handle,
  353. void *key_name,
  354. int key_len,
  355. void *value,
  356. int value_len)
  357. {
  358. struct object_instance *instance;
  359. struct object_key *object_key;
  360. unsigned int res;
  361. int found = 0;
  362. int i;
  363. unsigned int val;
  364. res = hdb_handle_get (&object_instance_database,
  365. object_handle, (void *)&instance);
  366. if (res != 0) {
  367. goto error_exit;
  368. }
  369. /*
  370. * Do validation check if validation is configured for the parent object
  371. */
  372. if (instance->object_key_valid_list_entries) {
  373. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  374. if ((key_len ==
  375. instance->object_key_valid_list[i].key_len) &&
  376. (memcmp (key_name,
  377. instance->object_key_valid_list[i].key_name,
  378. key_len) == 0)) {
  379. found = 1;
  380. break;
  381. }
  382. }
  383. /*
  384. * Item not found in validation list
  385. */
  386. if (found == 0) {
  387. goto error_put;
  388. } else {
  389. if (instance->object_key_valid_list[i].validate_callback) {
  390. res = instance->object_key_valid_list[i].validate_callback (
  391. key_name, key_len, value, value_len);
  392. if (res != 0) {
  393. goto error_put;
  394. }
  395. }
  396. }
  397. }
  398. object_key = malloc (sizeof (struct object_key));
  399. if (object_key == 0) {
  400. goto error_put;
  401. }
  402. object_key->key_name = malloc (key_len);
  403. if (object_key->key_name == 0) {
  404. goto error_put_object;
  405. }
  406. memcpy (&val, value, 4);
  407. object_key->value = malloc (value_len);
  408. if (object_key->value == 0) {
  409. goto error_put_key;
  410. }
  411. memcpy (object_key->key_name, key_name, key_len);
  412. memcpy (object_key->value, value, value_len);
  413. object_key->key_len = key_len;
  414. object_key->value_len = value_len;
  415. list_init (&object_key->list);
  416. list_add (&object_key->list, &instance->key_head);
  417. object_key_changed_notification(object_handle, key_name, key_len,
  418. value, value_len, OBJECT_KEY_CREATED);
  419. return (0);
  420. error_put_key:
  421. free (object_key->key_name);
  422. error_put_object:
  423. free (object_key);
  424. error_put:
  425. hdb_handle_put (&object_instance_database, object_handle);
  426. error_exit:
  427. return (-1);
  428. }
  429. static int _clear_object(struct object_instance *instance)
  430. {
  431. struct list_head *list;
  432. int res;
  433. struct object_instance *find_instance = NULL;
  434. struct object_key *object_key = NULL;
  435. for (list = instance->key_head.next;
  436. list != &instance->key_head; ) {
  437. object_key = list_entry (list, struct object_key,
  438. list);
  439. list = list->next;
  440. list_del(&object_key->list);
  441. free(object_key->key_name);
  442. free(object_key->value);
  443. }
  444. for (list = instance->child_head.next;
  445. list != &instance->child_head; ) {
  446. find_instance = list_entry (list, struct object_instance,
  447. child_list);
  448. res = _clear_object(find_instance);
  449. if (res)
  450. return res;
  451. list = list->next;
  452. list_del(&find_instance->child_list);
  453. free(find_instance->object_name);
  454. free(find_instance);
  455. }
  456. return 0;
  457. }
  458. static int object_destroy (
  459. unsigned int object_handle)
  460. {
  461. struct object_instance *instance;
  462. unsigned int res;
  463. res = hdb_handle_get (&object_instance_database,
  464. object_handle, (void *)&instance);
  465. if (res != 0) {
  466. return (res);
  467. }
  468. object_pre_deletion_notification(object_handle,
  469. instance->parent_handle,
  470. instance->object_name,
  471. instance->object_name_len);
  472. /* Recursively clear sub-objects & keys */
  473. res = _clear_object(instance);
  474. list_del(&instance->child_list);
  475. free(instance->object_name);
  476. free(instance);
  477. return (res);
  478. }
  479. static int object_valid_set (
  480. unsigned int object_handle,
  481. struct object_valid *object_valid_list,
  482. unsigned int object_valid_list_entries)
  483. {
  484. struct object_instance *instance;
  485. unsigned int res;
  486. res = hdb_handle_get (&object_instance_database,
  487. object_handle, (void *)&instance);
  488. if (res != 0) {
  489. goto error_exit;
  490. }
  491. instance->object_valid_list = object_valid_list;
  492. instance->object_valid_list_entries = object_valid_list_entries;
  493. hdb_handle_put (&object_instance_database, object_handle);
  494. return (0);
  495. error_exit:
  496. return (-1);
  497. }
  498. static int object_key_valid_set (
  499. unsigned int object_handle,
  500. struct object_key_valid *object_key_valid_list,
  501. unsigned int object_key_valid_list_entries)
  502. {
  503. struct object_instance *instance;
  504. unsigned int res;
  505. res = hdb_handle_get (&object_instance_database,
  506. object_handle, (void *)&instance);
  507. if (res != 0) {
  508. goto error_exit;
  509. }
  510. instance->object_key_valid_list = object_key_valid_list;
  511. instance->object_key_valid_list_entries = object_key_valid_list_entries;
  512. hdb_handle_put (&object_instance_database, object_handle);
  513. return (0);
  514. error_exit:
  515. return (-1);
  516. }
  517. /*
  518. * object db reading
  519. */
  520. static int object_find_create (
  521. unsigned int object_handle,
  522. void *object_name,
  523. int object_len,
  524. unsigned int *object_find_handle)
  525. {
  526. unsigned int res;
  527. struct object_instance *object_instance;
  528. struct object_find_instance *object_find_instance;
  529. res = hdb_handle_get (&object_instance_database,
  530. object_handle, (void *)&object_instance);
  531. if (res != 0) {
  532. goto error_exit;
  533. }
  534. res = hdb_handle_create (&object_find_instance_database,
  535. sizeof (struct object_find_instance), object_find_handle);
  536. if (res != 0) {
  537. goto error_put;
  538. }
  539. res = hdb_handle_get (&object_find_instance_database,
  540. *object_find_handle, (void *)&object_find_instance);
  541. if (res != 0) {
  542. goto error_destroy;
  543. }
  544. object_find_instance->find_child_list = &object_instance->child_head;
  545. object_find_instance->child_head = &object_instance->child_head;
  546. object_find_instance->object_name = object_name;
  547. object_find_instance->object_len = object_len;
  548. hdb_handle_put (&object_instance_database, object_handle);
  549. hdb_handle_put (&object_find_instance_database, *object_find_handle);
  550. return (0);
  551. error_destroy:
  552. hdb_handle_destroy (&object_instance_database, *object_find_handle);
  553. error_put:
  554. hdb_handle_put (&object_instance_database, object_handle);
  555. error_exit:
  556. return (-1);
  557. }
  558. static int object_find_next (
  559. unsigned int object_find_handle,
  560. unsigned int *object_handle)
  561. {
  562. unsigned int res;
  563. struct object_find_instance *object_find_instance;
  564. struct object_instance *object_instance = NULL;
  565. struct list_head *list;
  566. unsigned int found = 0;
  567. res = hdb_handle_get (&object_find_instance_database,
  568. object_find_handle, (void *)&object_find_instance);
  569. if (res != 0) {
  570. goto error_exit;
  571. }
  572. res = -1;
  573. for (list = object_find_instance->find_child_list->next;
  574. list != object_find_instance->child_head; list = list->next) {
  575. object_instance = list_entry (list, struct object_instance,
  576. child_list);
  577. if ((object_instance->object_name_len ==
  578. object_find_instance->object_len) &&
  579. (memcmp (object_instance->object_name,
  580. object_find_instance->object_name,
  581. object_find_instance->object_len) == 0)) {
  582. found = 1;
  583. break;
  584. }
  585. }
  586. object_find_instance->find_child_list = list;
  587. hdb_handle_put (&object_find_instance_database, object_find_handle);
  588. if (found) {
  589. *object_handle = object_instance->object_handle;
  590. res = 0;
  591. }
  592. return (res);
  593. error_exit:
  594. return (-1);
  595. }
  596. static int object_find_destroy (
  597. unsigned int object_find_handle)
  598. {
  599. return (0);
  600. }
  601. static int object_key_get (
  602. unsigned int object_handle,
  603. void *key_name,
  604. int key_len,
  605. void **value,
  606. int *value_len)
  607. {
  608. unsigned int res = 0;
  609. struct object_instance *instance;
  610. struct object_key *object_key = NULL;
  611. struct list_head *list;
  612. int found = 0;
  613. res = hdb_handle_get (&object_instance_database,
  614. object_handle, (void *)&instance);
  615. if (res != 0) {
  616. goto error_exit;
  617. }
  618. for (list = instance->key_head.next;
  619. list != &instance->key_head; list = list->next) {
  620. object_key = list_entry (list, struct object_key, list);
  621. if ((object_key->key_len == key_len) &&
  622. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  623. found = 1;
  624. break;
  625. }
  626. }
  627. if (found) {
  628. *value = object_key->value;
  629. if (value_len) {
  630. *value_len = object_key->value_len;
  631. }
  632. }
  633. else {
  634. res = -1;
  635. }
  636. hdb_handle_put (&object_instance_database, object_handle);
  637. return (res);
  638. error_exit:
  639. return (-1);
  640. }
  641. static int object_key_delete (
  642. unsigned int object_handle,
  643. void *key_name,
  644. int key_len,
  645. void *value,
  646. int value_len)
  647. {
  648. unsigned int res;
  649. int ret = 0;
  650. struct object_instance *instance;
  651. struct object_key *object_key = NULL;
  652. struct list_head *list;
  653. int found = 0;
  654. res = hdb_handle_get (&object_instance_database,
  655. object_handle, (void *)&instance);
  656. if (res != 0) {
  657. goto error_exit;
  658. }
  659. for (list = instance->key_head.next;
  660. list != &instance->key_head; list = list->next) {
  661. object_key = list_entry (list, struct object_key, list);
  662. if ((object_key->key_len == key_len) &&
  663. (memcmp (object_key->key_name, key_name, key_len) == 0) &&
  664. (value == NULL ||
  665. (object_key->value_len == value_len &&
  666. (memcmp (object_key->value, value, value_len) == 0)))) {
  667. found = 1;
  668. break;
  669. }
  670. }
  671. if (found) {
  672. list_del(&object_key->list);
  673. free(object_key->key_name);
  674. free(object_key->value);
  675. free(object_key);
  676. }
  677. else {
  678. ret = -1;
  679. errno = ENOENT;
  680. }
  681. hdb_handle_put (&object_instance_database, object_handle);
  682. if (ret == 0)
  683. object_key_changed_notification(object_handle, key_name, key_len,
  684. value, value_len, OBJECT_KEY_DELETED);
  685. return (ret);
  686. error_exit:
  687. return (-1);
  688. }
  689. static int object_key_replace (
  690. unsigned int object_handle,
  691. void *key_name,
  692. int key_len,
  693. void *old_value,
  694. int old_value_len,
  695. void *new_value,
  696. int new_value_len)
  697. {
  698. unsigned int res;
  699. int ret = 0;
  700. struct object_instance *instance;
  701. struct object_key *object_key = NULL;
  702. struct list_head *list;
  703. int found = 0;
  704. res = hdb_handle_get (&object_instance_database,
  705. object_handle, (void *)&instance);
  706. if (res != 0) {
  707. goto error_exit;
  708. }
  709. for (list = instance->key_head.next;
  710. list != &instance->key_head; list = list->next) {
  711. object_key = list_entry (list, struct object_key, list);
  712. if ((object_key->key_len == key_len) &&
  713. (memcmp (object_key->key_name, key_name, key_len) == 0) &&
  714. (old_value == NULL ||
  715. (object_key->value_len == old_value_len &&
  716. (memcmp (object_key->value, old_value, old_value_len) == 0)))) {
  717. found = 1;
  718. break;
  719. }
  720. }
  721. if (found) {
  722. int i;
  723. /*
  724. * Do validation check if validation is configured for the parent object
  725. */
  726. if (instance->object_key_valid_list_entries) {
  727. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  728. if ((key_len ==
  729. instance->object_key_valid_list[i].key_len) &&
  730. (memcmp (key_name,
  731. instance->object_key_valid_list[i].key_name,
  732. key_len) == 0)) {
  733. found = 1;
  734. break;
  735. }
  736. }
  737. /*
  738. * Item not found in validation list
  739. */
  740. if (found == 0) {
  741. goto error_put;
  742. } else {
  743. if (instance->object_key_valid_list[i].validate_callback) {
  744. res = instance->object_key_valid_list[i].validate_callback (
  745. key_name, key_len, new_value, new_value_len);
  746. if (res != 0) {
  747. goto error_put;
  748. }
  749. }
  750. }
  751. }
  752. if (new_value_len <= object_key->value_len) {
  753. void *replacement_value;
  754. replacement_value = malloc(new_value_len);
  755. if (!replacement_value)
  756. goto error_exit;
  757. free(object_key->value);
  758. object_key->value = replacement_value;
  759. }
  760. memcpy(object_key->value, new_value, new_value_len);
  761. object_key->value_len = new_value_len;
  762. }
  763. else {
  764. ret = -1;
  765. errno = ENOENT;
  766. }
  767. hdb_handle_put (&object_instance_database, object_handle);
  768. if (ret == 0)
  769. object_key_changed_notification(object_handle, key_name, key_len,
  770. new_value, new_value_len, OBJECT_KEY_REPLACED);
  771. return (ret);
  772. error_put:
  773. hdb_handle_put (&object_instance_database, object_handle);
  774. error_exit:
  775. return (-1);
  776. }
  777. static int object_priv_get (
  778. unsigned int object_handle,
  779. void **priv)
  780. {
  781. int res;
  782. struct object_instance *object_instance;
  783. res = hdb_handle_get (&object_instance_database,
  784. object_handle, (void *)&object_instance);
  785. if (res != 0) {
  786. goto error_exit;
  787. }
  788. *priv = object_instance->priv;
  789. hdb_handle_put (&object_instance_database, object_handle);
  790. return (0);
  791. error_exit:
  792. return (-1);
  793. }
  794. static int _dump_object(struct object_instance *instance, FILE *file, int depth)
  795. {
  796. struct list_head *list;
  797. int res;
  798. int i;
  799. struct object_instance *find_instance = NULL;
  800. struct object_key *object_key = NULL;
  801. char stringbuf1[1024];
  802. char stringbuf2[1024];
  803. memcpy(stringbuf1, instance->object_name, instance->object_name_len);
  804. stringbuf1[instance->object_name_len] = '\0';
  805. for (i=0; i<depth; i++)
  806. fprintf(file, " ");
  807. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  808. fprintf(file, "%s {\n", stringbuf1);
  809. for (list = instance->key_head.next;
  810. list != &instance->key_head; list = list->next) {
  811. object_key = list_entry (list, struct object_key,
  812. list);
  813. memcpy(stringbuf1, object_key->key_name, object_key->key_len);
  814. stringbuf1[object_key->key_len] = '\0';
  815. memcpy(stringbuf2, object_key->value, object_key->value_len);
  816. stringbuf2[object_key->value_len] = '\0';
  817. for (i=0; i<depth+1; i++)
  818. fprintf(file, " ");
  819. fprintf(file, "%s: %s\n", stringbuf1, stringbuf2);
  820. }
  821. for (list = instance->child_head.next;
  822. list != &instance->child_head; list = list->next) {
  823. find_instance = list_entry (list, struct object_instance,
  824. child_list);
  825. res = _dump_object(find_instance, file, depth+1);
  826. if (res)
  827. return res;
  828. }
  829. for (i=0; i<depth; i++)
  830. fprintf(file, " ");
  831. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  832. fprintf(file, "}\n");
  833. return 0;
  834. }
  835. static int object_key_iter_reset(unsigned int object_handle)
  836. {
  837. unsigned int res;
  838. struct object_instance *instance;
  839. res = hdb_handle_get (&object_instance_database,
  840. object_handle, (void *)&instance);
  841. if (res != 0) {
  842. goto error_exit;
  843. }
  844. instance->iter_key_list = &instance->key_head;
  845. hdb_handle_put (&object_instance_database, object_handle);
  846. return (0);
  847. error_exit:
  848. return (-1);
  849. }
  850. static int object_key_iter(unsigned int parent_object_handle,
  851. void **key_name,
  852. int *key_len,
  853. void **value,
  854. int *value_len)
  855. {
  856. unsigned int res;
  857. struct object_instance *instance;
  858. struct object_key *find_key = NULL;
  859. struct list_head *list;
  860. unsigned int found = 0;
  861. res = hdb_handle_get (&object_instance_database,
  862. parent_object_handle, (void *)&instance);
  863. if (res != 0) {
  864. goto error_exit;
  865. }
  866. res = -ENOENT;
  867. list = instance->iter_key_list->next;
  868. if (list != &instance->key_head) {
  869. find_key = list_entry (list, struct object_key, list);
  870. found = 1;
  871. }
  872. instance->iter_key_list = list;
  873. if (found) {
  874. *key_name = find_key->key_name;
  875. if (key_len)
  876. *key_len = find_key->key_len;
  877. *value = find_key->value;
  878. if (value_len)
  879. *value_len = find_key->value_len;
  880. res = 0;
  881. }
  882. else {
  883. res = -1;
  884. }
  885. hdb_handle_put (&object_instance_database, parent_object_handle);
  886. return (res);
  887. error_exit:
  888. return (-1);
  889. }
  890. static int object_iter_reset(unsigned int parent_object_handle)
  891. {
  892. unsigned int res;
  893. struct object_instance *instance;
  894. res = hdb_handle_get (&object_instance_database,
  895. parent_object_handle, (void *)&instance);
  896. if (res != 0) {
  897. goto error_exit;
  898. }
  899. instance->iter_list = &instance->child_head;
  900. hdb_handle_put (&object_instance_database, parent_object_handle);
  901. return (0);
  902. error_exit:
  903. return (-1);
  904. }
  905. static int object_iter(unsigned int parent_object_handle,
  906. void **object_name,
  907. int *name_len,
  908. unsigned int *object_handle)
  909. {
  910. unsigned int res;
  911. struct object_instance *instance;
  912. struct object_instance *find_instance = NULL;
  913. struct list_head *list;
  914. unsigned int found = 0;
  915. res = hdb_handle_get (&object_instance_database,
  916. parent_object_handle, (void *)&instance);
  917. if (res != 0) {
  918. goto error_exit;
  919. }
  920. res = -ENOENT;
  921. list = instance->iter_list->next;
  922. if (list != &instance->child_head) {
  923. find_instance = list_entry (list, struct object_instance,
  924. child_list);
  925. found = 1;
  926. }
  927. instance->iter_list = list;
  928. if (found) {
  929. *object_handle = find_instance->object_handle;
  930. *object_name = find_instance->object_name;
  931. *name_len = find_instance->object_name_len;
  932. res = 0;
  933. }
  934. else {
  935. res = -1;
  936. }
  937. return (res);
  938. error_exit:
  939. return (-1);
  940. }
  941. static int object_find_from(unsigned int parent_object_handle,
  942. unsigned int start_pos,
  943. void *object_name,
  944. int object_name_len,
  945. unsigned int *object_handle,
  946. unsigned int *next_pos)
  947. {
  948. unsigned int res;
  949. unsigned int pos = 0;
  950. struct object_instance *instance;
  951. struct object_instance *find_instance = NULL;
  952. struct list_head *list;
  953. unsigned int found = 0;
  954. res = hdb_handle_get (&object_instance_database,
  955. parent_object_handle, (void *)&instance);
  956. if (res != 0) {
  957. goto error_exit;
  958. }
  959. res = -ENOENT;
  960. for (list = instance->child_head.next;
  961. list != &instance->child_head; list = list->next) {
  962. find_instance = list_entry (list, struct object_instance,
  963. child_list);
  964. if ((find_instance->object_name_len == object_name_len) &&
  965. (memcmp (find_instance->object_name, object_name,
  966. object_name_len) == 0)) {
  967. if (pos++ == start_pos) {
  968. found = 1;
  969. break;
  970. }
  971. }
  972. }
  973. hdb_handle_put (&object_instance_database, parent_object_handle);
  974. if (found) {
  975. *object_handle = find_instance->object_handle;
  976. res = 0;
  977. }
  978. *next_pos = pos;
  979. return (res);
  980. error_exit:
  981. return (-1);
  982. }
  983. static int object_iter_from(unsigned int parent_object_handle,
  984. unsigned int start_pos,
  985. void **object_name,
  986. int *name_len,
  987. unsigned int *object_handle)
  988. {
  989. unsigned int res;
  990. unsigned int pos = 0;
  991. struct object_instance *instance;
  992. struct object_instance *find_instance = NULL;
  993. struct list_head *list;
  994. unsigned int found = 0;
  995. res = hdb_handle_get (&object_instance_database,
  996. parent_object_handle, (void *)&instance);
  997. if (res != 0) {
  998. goto error_exit;
  999. }
  1000. res = -ENOENT;
  1001. for (list = instance->child_head.next;
  1002. list != &instance->child_head; list = list->next) {
  1003. find_instance = list_entry (list, struct object_instance,
  1004. child_list);
  1005. if (pos++ == start_pos) {
  1006. found = 1;
  1007. break;
  1008. }
  1009. }
  1010. if (found) {
  1011. *object_handle = find_instance->object_handle;
  1012. *object_name = find_instance->object_name;
  1013. *name_len = find_instance->object_name_len;
  1014. res = 0;
  1015. }
  1016. else {
  1017. res = -1;
  1018. }
  1019. return (res);
  1020. error_exit:
  1021. return (-1);
  1022. }
  1023. static int object_key_iter_from(unsigned int parent_object_handle,
  1024. unsigned int start_pos,
  1025. void **key_name,
  1026. int *key_len,
  1027. void **value,
  1028. int *value_len)
  1029. {
  1030. unsigned int pos = 0;
  1031. unsigned int res;
  1032. struct object_instance *instance;
  1033. struct object_key *find_key = NULL;
  1034. struct list_head *list;
  1035. unsigned int found = 0;
  1036. res = hdb_handle_get (&object_instance_database,
  1037. parent_object_handle, (void *)&instance);
  1038. if (res != 0) {
  1039. goto error_exit;
  1040. }
  1041. res = -ENOENT;
  1042. for (list = instance->key_head.next;
  1043. list != &instance->key_head; list = list->next) {
  1044. find_key = list_entry (list, struct object_key, list);
  1045. if (pos++ == start_pos) {
  1046. found = 1;
  1047. break;
  1048. }
  1049. }
  1050. if (found) {
  1051. *key_name = find_key->key_name;
  1052. if (key_len)
  1053. *key_len = find_key->key_len;
  1054. *value = find_key->value;
  1055. if (value_len)
  1056. *value_len = find_key->value_len;
  1057. res = 0;
  1058. }
  1059. else {
  1060. res = -1;
  1061. }
  1062. hdb_handle_put (&object_instance_database, parent_object_handle);
  1063. return (res);
  1064. error_exit:
  1065. return (-1);
  1066. }
  1067. static int object_parent_get(unsigned int object_handle,
  1068. unsigned int *parent_handle)
  1069. {
  1070. struct object_instance *instance;
  1071. unsigned int res;
  1072. res = hdb_handle_get (&object_instance_database,
  1073. object_handle, (void *)&instance);
  1074. if (res != 0) {
  1075. return (res);
  1076. }
  1077. if (object_handle == OBJECT_PARENT_HANDLE)
  1078. *parent_handle = 0;
  1079. else
  1080. *parent_handle = instance->parent_handle;
  1081. hdb_handle_put (&object_instance_database, object_handle);
  1082. return (0);
  1083. }
  1084. static int object_track_start(unsigned int object_handle,
  1085. object_track_depth_t depth,
  1086. object_key_change_notify_fn_t key_change_notify_fn,
  1087. object_create_notify_fn_t object_create_notify_fn,
  1088. object_destroy_notify_fn_t object_destroy_notify_fn,
  1089. void * priv_data_pt)
  1090. {
  1091. struct object_instance *instance;
  1092. unsigned int res;
  1093. struct object_tracker * tracker_pt;
  1094. res = hdb_handle_get (&object_instance_database,
  1095. object_handle, (void *)&instance);
  1096. if (res != 0) {
  1097. return (res);
  1098. }
  1099. tracker_pt = malloc(sizeof(struct object_tracker));
  1100. tracker_pt->depth = depth;
  1101. tracker_pt->object_handle = object_handle;
  1102. tracker_pt->key_change_notify_fn = key_change_notify_fn;
  1103. tracker_pt->object_create_notify_fn = object_create_notify_fn;
  1104. tracker_pt->object_destroy_notify_fn = object_destroy_notify_fn;
  1105. tracker_pt->data_pt = priv_data_pt;
  1106. list_init(&tracker_pt->object_list);
  1107. list_init(&tracker_pt->tracker_list);
  1108. list_add(&tracker_pt->object_list, &instance->track_head);
  1109. list_add(&tracker_pt->tracker_list, &objdb_trackers_head);
  1110. hdb_handle_put (&object_instance_database, object_handle);
  1111. return (res);
  1112. }
  1113. static void object_track_stop(object_key_change_notify_fn_t key_change_notify_fn,
  1114. object_create_notify_fn_t object_create_notify_fn,
  1115. object_destroy_notify_fn_t object_destroy_notify_fn,
  1116. void * priv_data_pt)
  1117. {
  1118. struct object_instance *instance;
  1119. struct object_tracker * tracker_pt = NULL;
  1120. struct object_tracker * obj_tracker_pt = NULL;
  1121. struct list_head *list, *tmp_list;
  1122. struct list_head *obj_list, *tmp_obj_list;
  1123. unsigned int res;
  1124. /* go through the global list and find all the trackers to stop */
  1125. for (list = objdb_trackers_head.next, tmp_list = list->next;
  1126. list != &objdb_trackers_head; list = tmp_list, tmp_list = tmp_list->next) {
  1127. tracker_pt = list_entry (list, struct object_tracker, tracker_list);
  1128. if (tracker_pt && (tracker_pt->data_pt == priv_data_pt) &&
  1129. (tracker_pt->object_create_notify_fn == object_create_notify_fn) &&
  1130. (tracker_pt->object_destroy_notify_fn == object_destroy_notify_fn) &&
  1131. (tracker_pt->key_change_notify_fn == key_change_notify_fn)) {
  1132. /* get the object & take this tracker off of it's list. */
  1133. res = hdb_handle_get (&object_instance_database,
  1134. tracker_pt->object_handle, (void *)&instance);
  1135. if (res != 0) continue;
  1136. for (obj_list = instance->track_head.next, tmp_obj_list = obj_list->next;
  1137. obj_list != &instance->track_head; obj_list = tmp_obj_list, tmp_obj_list = tmp_obj_list->next) {
  1138. obj_tracker_pt = list_entry (obj_list, struct object_tracker, object_list);
  1139. if (obj_tracker_pt == tracker_pt) {
  1140. /* this is the tracker we are after. */
  1141. list_del(obj_list);
  1142. }
  1143. }
  1144. hdb_handle_put (&object_instance_database, tracker_pt->object_handle);
  1145. /* remove the tracker off of the global list */
  1146. list_del(list);
  1147. free(tracker_pt);
  1148. }
  1149. }
  1150. }
  1151. static int object_dump(unsigned int object_handle,
  1152. FILE *file)
  1153. {
  1154. struct object_instance *instance;
  1155. unsigned int res;
  1156. res = hdb_handle_get (&object_instance_database,
  1157. object_handle, (void *)&instance);
  1158. if (res != 0) {
  1159. return (res);
  1160. }
  1161. res = _dump_object(instance, file, -1);
  1162. hdb_handle_put (&object_instance_database, object_handle);
  1163. return (res);
  1164. }
  1165. static int object_write_config(char **error_string)
  1166. {
  1167. struct config_iface_ver0 **modules;
  1168. int num_modules;
  1169. int i;
  1170. int res;
  1171. main_get_config_modules(&modules, &num_modules);
  1172. for (i=0; i<num_modules; i++) {
  1173. if (modules[i]->config_writeconfig) {
  1174. res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  1175. if (res)
  1176. return res;
  1177. }
  1178. }
  1179. return 0;
  1180. }
  1181. struct objdb_iface_ver0 objdb_iface = {
  1182. .objdb_init = objdb_init,
  1183. .object_create = object_create,
  1184. .object_priv_set = object_priv_set,
  1185. .object_key_create = object_key_create,
  1186. .object_key_delete = object_key_delete,
  1187. .object_key_replace = object_key_replace,
  1188. .object_destroy = object_destroy,
  1189. .object_valid_set = object_valid_set,
  1190. .object_key_valid_set = object_key_valid_set,
  1191. .object_find_create = object_find_create,
  1192. .object_find_next = object_find_next,
  1193. .object_find_destroy = object_find_destroy,
  1194. .object_find_from = object_find_from,
  1195. .object_key_get = object_key_get,
  1196. .object_key_iter = object_key_iter,
  1197. .object_key_iter_reset = object_key_iter_reset,
  1198. .object_key_iter_from = object_key_iter_from,
  1199. .object_iter = object_iter,
  1200. .object_iter_reset = object_iter_reset,
  1201. .object_iter_from = object_iter_from,
  1202. .object_priv_get = object_priv_get,
  1203. .object_parent_get = object_parent_get,
  1204. .object_track_start = object_track_start,
  1205. .object_track_stop = object_track_stop,
  1206. .object_dump = object_dump,
  1207. .object_write_config = object_write_config,
  1208. };
  1209. struct lcr_iface objdb_iface_ver0[1] = {
  1210. {
  1211. .name = "objdb",
  1212. .version = 0,
  1213. .versions_replace = 0,
  1214. .versions_replace_count = 0,
  1215. .dependencies = 0,
  1216. .dependency_count = 0,
  1217. .constructor = NULL,
  1218. .destructor = NULL,
  1219. .interfaces = NULL,
  1220. }
  1221. };
  1222. struct lcr_comp objdb_comp_ver0 = {
  1223. .iface_count = 1,
  1224. .ifaces = objdb_iface_ver0
  1225. };
  1226. __attribute__ ((constructor)) static void objdb_comp_register (void) {
  1227. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  1228. lcr_component_register (&objdb_comp_ver0);
  1229. }