objdb.c 39 KB

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