objdb.c 39 KB

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