objdb.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*
  2. * Copyright (c) 2006 MontaVista Software, Inc.
  3. * Copyright (c) 2007-2008 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <stdio.h>
  36. #include <errno.h>
  37. #include <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. return (0);
  677. }
  678. static int object_key_get (
  679. unsigned int object_handle,
  680. void *key_name,
  681. int key_len,
  682. void **value,
  683. int *value_len)
  684. {
  685. unsigned int res = 0;
  686. struct object_instance *instance;
  687. struct object_key *object_key = NULL;
  688. struct list_head *list;
  689. int found = 0;
  690. objdb_rdlock();
  691. res = hdb_handle_get (&object_instance_database,
  692. object_handle, (void *)&instance);
  693. if (res != 0) {
  694. goto error_exit;
  695. }
  696. for (list = instance->key_head.next;
  697. list != &instance->key_head; list = list->next) {
  698. object_key = list_entry (list, struct object_key, list);
  699. if ((object_key->key_len == key_len) &&
  700. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  701. found = 1;
  702. break;
  703. }
  704. }
  705. if (found) {
  706. *value = object_key->value;
  707. if (value_len) {
  708. *value_len = object_key->value_len;
  709. }
  710. }
  711. else {
  712. res = -1;
  713. }
  714. hdb_handle_put (&object_instance_database, object_handle);
  715. objdb_rdunlock();
  716. return (res);
  717. error_exit:
  718. objdb_rdunlock();
  719. return (-1);
  720. }
  721. static int object_key_increment (
  722. unsigned int object_handle,
  723. void *key_name,
  724. int key_len,
  725. unsigned int *value)
  726. {
  727. unsigned int res = 0;
  728. struct object_instance *instance;
  729. struct object_key *object_key = NULL;
  730. struct list_head *list;
  731. int found = 0;
  732. objdb_rdlock();
  733. res = hdb_handle_get (&object_instance_database,
  734. object_handle, (void *)&instance);
  735. if (res != 0) {
  736. goto error_exit;
  737. }
  738. for (list = instance->key_head.next;
  739. list != &instance->key_head; list = list->next) {
  740. object_key = list_entry (list, struct object_key, list);
  741. if ((object_key->key_len == key_len) &&
  742. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  743. found = 1;
  744. break;
  745. }
  746. }
  747. if (found && object_key->value_len == sizeof(int)) {
  748. (*(int *)object_key->value)++;
  749. *value = *(int *)object_key->value;
  750. }
  751. else {
  752. res = -1;
  753. }
  754. hdb_handle_put (&object_instance_database, object_handle);
  755. objdb_rdunlock();
  756. return (res);
  757. error_exit:
  758. objdb_rdunlock();
  759. return (-1);
  760. }
  761. static int object_key_decrement (
  762. unsigned int object_handle,
  763. void *key_name,
  764. int key_len,
  765. unsigned int *value)
  766. {
  767. unsigned int res = 0;
  768. struct object_instance *instance;
  769. struct object_key *object_key = NULL;
  770. struct list_head *list;
  771. int found = 0;
  772. objdb_rdlock();
  773. res = hdb_handle_get (&object_instance_database,
  774. object_handle, (void *)&instance);
  775. if (res != 0) {
  776. goto error_exit;
  777. }
  778. for (list = instance->key_head.next;
  779. list != &instance->key_head; list = list->next) {
  780. object_key = list_entry (list, struct object_key, list);
  781. if ((object_key->key_len == key_len) &&
  782. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  783. found = 1;
  784. break;
  785. }
  786. }
  787. if (found && object_key->value_len == sizeof(int)) {
  788. (*(int *)object_key->value)--;
  789. *value = *(int *)object_key->value;
  790. }
  791. else {
  792. res = -1;
  793. }
  794. hdb_handle_put (&object_instance_database, object_handle);
  795. objdb_rdunlock();
  796. return (res);
  797. error_exit:
  798. objdb_rdunlock();
  799. return (-1);
  800. }
  801. static int object_key_delete (
  802. unsigned int object_handle,
  803. void *key_name,
  804. int key_len,
  805. void *value,
  806. int value_len)
  807. {
  808. unsigned int res;
  809. int ret = 0;
  810. struct object_instance *instance;
  811. struct object_key *object_key = NULL;
  812. struct list_head *list;
  813. int found = 0;
  814. objdb_rdlock();
  815. res = hdb_handle_get (&object_instance_database,
  816. object_handle, (void *)&instance);
  817. if (res != 0) {
  818. goto error_exit;
  819. }
  820. for (list = instance->key_head.next;
  821. list != &instance->key_head; list = list->next) {
  822. object_key = list_entry (list, struct object_key, list);
  823. if ((object_key->key_len == key_len) &&
  824. (memcmp (object_key->key_name, key_name, key_len) == 0) &&
  825. (value == NULL ||
  826. (object_key->value_len == value_len &&
  827. (memcmp (object_key->value, value, value_len) == 0)))) {
  828. found = 1;
  829. break;
  830. }
  831. }
  832. if (found) {
  833. list_del(&object_key->list);
  834. free(object_key->key_name);
  835. free(object_key->value);
  836. free(object_key);
  837. }
  838. else {
  839. ret = -1;
  840. errno = ENOENT;
  841. }
  842. hdb_handle_put (&object_instance_database, object_handle);
  843. if (ret == 0)
  844. object_key_changed_notification(object_handle, key_name, key_len,
  845. value, value_len, OBJECT_KEY_DELETED);
  846. objdb_rdunlock();
  847. return (ret);
  848. error_exit:
  849. objdb_rdunlock();
  850. return (-1);
  851. }
  852. static int object_key_replace (
  853. unsigned int object_handle,
  854. void *key_name,
  855. int key_len,
  856. void *old_value,
  857. int old_value_len,
  858. void *new_value,
  859. int new_value_len)
  860. {
  861. unsigned int res;
  862. int ret = 0;
  863. struct object_instance *instance;
  864. struct object_key *object_key = NULL;
  865. struct list_head *list;
  866. int found = 0;
  867. objdb_rdlock();
  868. res = hdb_handle_get (&object_instance_database,
  869. object_handle, (void *)&instance);
  870. if (res != 0) {
  871. goto error_exit;
  872. }
  873. for (list = instance->key_head.next;
  874. list != &instance->key_head; list = list->next) {
  875. object_key = list_entry (list, struct object_key, list);
  876. if ((object_key->key_len == key_len) &&
  877. (memcmp (object_key->key_name, key_name, key_len) == 0) &&
  878. (old_value == NULL ||
  879. (object_key->value_len == old_value_len &&
  880. (memcmp (object_key->value, old_value, old_value_len) == 0)))) {
  881. found = 1;
  882. break;
  883. }
  884. }
  885. if (found) {
  886. int i;
  887. /*
  888. * Do validation check if validation is configured for the parent object
  889. */
  890. if (instance->object_key_valid_list_entries) {
  891. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  892. if ((key_len ==
  893. instance->object_key_valid_list[i].key_len) &&
  894. (memcmp (key_name,
  895. instance->object_key_valid_list[i].key_name,
  896. key_len) == 0)) {
  897. found = 1;
  898. break;
  899. }
  900. }
  901. /*
  902. * Item not found in validation list
  903. */
  904. if (found == 0) {
  905. goto error_put;
  906. } else {
  907. if (instance->object_key_valid_list[i].validate_callback) {
  908. res = instance->object_key_valid_list[i].validate_callback (
  909. key_name, key_len, new_value, new_value_len);
  910. if (res != 0) {
  911. goto error_put;
  912. }
  913. }
  914. }
  915. }
  916. if (new_value_len <= object_key->value_len) {
  917. void *replacement_value;
  918. replacement_value = malloc(new_value_len);
  919. if (!replacement_value)
  920. goto error_exit;
  921. free(object_key->value);
  922. object_key->value = replacement_value;
  923. }
  924. memcpy(object_key->value, new_value, new_value_len);
  925. object_key->value_len = new_value_len;
  926. }
  927. else {
  928. ret = -1;
  929. errno = ENOENT;
  930. }
  931. hdb_handle_put (&object_instance_database, object_handle);
  932. if (ret == 0)
  933. object_key_changed_notification(object_handle, key_name, key_len,
  934. new_value, new_value_len, OBJECT_KEY_REPLACED);
  935. objdb_rdunlock();
  936. return (ret);
  937. error_put:
  938. hdb_handle_put (&object_instance_database, object_handle);
  939. error_exit:
  940. objdb_rdunlock();
  941. return (-1);
  942. }
  943. static int object_priv_get (
  944. unsigned int object_handle,
  945. void **priv)
  946. {
  947. int res;
  948. struct object_instance *object_instance;
  949. objdb_rdunlock();
  950. res = hdb_handle_get (&object_instance_database,
  951. object_handle, (void *)&object_instance);
  952. if (res != 0) {
  953. goto error_exit;
  954. }
  955. *priv = object_instance->priv;
  956. hdb_handle_put (&object_instance_database, object_handle);
  957. objdb_rdunlock();
  958. return (0);
  959. error_exit:
  960. objdb_rdunlock();
  961. return (-1);
  962. }
  963. static int _dump_object(struct object_instance *instance, FILE *file, int depth)
  964. {
  965. struct list_head *list;
  966. int res;
  967. int i;
  968. struct object_instance *find_instance = NULL;
  969. struct object_key *object_key = NULL;
  970. char stringbuf1[1024];
  971. char stringbuf2[1024];
  972. memcpy(stringbuf1, instance->object_name, instance->object_name_len);
  973. stringbuf1[instance->object_name_len] = '\0';
  974. for (i=0; i<depth; i++)
  975. fprintf(file, " ");
  976. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  977. fprintf(file, "%s {\n", stringbuf1);
  978. for (list = instance->key_head.next;
  979. list != &instance->key_head; list = list->next) {
  980. object_key = list_entry (list, struct object_key,
  981. list);
  982. memcpy(stringbuf1, object_key->key_name, object_key->key_len);
  983. stringbuf1[object_key->key_len] = '\0';
  984. memcpy(stringbuf2, object_key->value, object_key->value_len);
  985. stringbuf2[object_key->value_len] = '\0';
  986. for (i=0; i<depth+1; i++)
  987. fprintf(file, " ");
  988. fprintf(file, "%s: %s\n", stringbuf1, stringbuf2);
  989. }
  990. for (list = instance->child_head.next;
  991. list != &instance->child_head; list = list->next) {
  992. find_instance = list_entry (list, struct object_instance,
  993. child_list);
  994. res = _dump_object(find_instance, file, depth+1);
  995. if (res)
  996. return res;
  997. }
  998. for (i=0; i<depth; i++)
  999. fprintf(file, " ");
  1000. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1001. fprintf(file, "}\n");
  1002. return 0;
  1003. }
  1004. static int object_key_iter_reset(unsigned int object_handle)
  1005. {
  1006. unsigned int res;
  1007. struct object_instance *instance;
  1008. objdb_rdlock();
  1009. res = hdb_handle_get (&object_instance_database,
  1010. object_handle, (void *)&instance);
  1011. if (res != 0) {
  1012. goto error_exit;
  1013. }
  1014. instance->iter_key_list = &instance->key_head;
  1015. hdb_handle_put (&object_instance_database, object_handle);
  1016. objdb_rdunlock();
  1017. return (0);
  1018. error_exit:
  1019. objdb_rdunlock();
  1020. return (-1);
  1021. }
  1022. static int object_key_iter(unsigned int parent_object_handle,
  1023. void **key_name,
  1024. int *key_len,
  1025. void **value,
  1026. int *value_len)
  1027. {
  1028. unsigned int res;
  1029. struct object_instance *instance;
  1030. struct object_key *find_key = NULL;
  1031. struct list_head *list;
  1032. unsigned int found = 0;
  1033. objdb_rdlock();
  1034. res = hdb_handle_get (&object_instance_database,
  1035. parent_object_handle, (void *)&instance);
  1036. if (res != 0) {
  1037. goto error_exit;
  1038. }
  1039. res = -ENOENT;
  1040. list = instance->iter_key_list->next;
  1041. if (list != &instance->key_head) {
  1042. find_key = list_entry (list, struct object_key, list);
  1043. found = 1;
  1044. }
  1045. instance->iter_key_list = list;
  1046. if (found) {
  1047. *key_name = find_key->key_name;
  1048. if (key_len)
  1049. *key_len = find_key->key_len;
  1050. *value = find_key->value;
  1051. if (value_len)
  1052. *value_len = find_key->value_len;
  1053. res = 0;
  1054. }
  1055. else {
  1056. res = -1;
  1057. }
  1058. hdb_handle_put (&object_instance_database, parent_object_handle);
  1059. objdb_rdunlock();
  1060. return (res);
  1061. error_exit:
  1062. objdb_rdunlock();
  1063. return (-1);
  1064. }
  1065. static int object_key_iter_from(unsigned int parent_object_handle,
  1066. unsigned int start_pos,
  1067. void **key_name,
  1068. int *key_len,
  1069. void **value,
  1070. int *value_len)
  1071. {
  1072. unsigned int pos = 0;
  1073. unsigned int res;
  1074. struct object_instance *instance;
  1075. struct object_key *find_key = NULL;
  1076. struct list_head *list;
  1077. unsigned int found = 0;
  1078. objdb_rdlock();
  1079. res = hdb_handle_get (&object_instance_database,
  1080. parent_object_handle, (void *)&instance);
  1081. if (res != 0) {
  1082. goto error_exit;
  1083. }
  1084. res = -ENOENT;
  1085. for (list = instance->key_head.next;
  1086. list != &instance->key_head; list = list->next) {
  1087. find_key = list_entry (list, struct object_key, list);
  1088. if (pos++ == start_pos) {
  1089. found = 1;
  1090. break;
  1091. }
  1092. }
  1093. if (found) {
  1094. *key_name = find_key->key_name;
  1095. if (key_len)
  1096. *key_len = find_key->key_len;
  1097. *value = find_key->value;
  1098. if (value_len)
  1099. *value_len = find_key->value_len;
  1100. res = 0;
  1101. }
  1102. else {
  1103. res = -1;
  1104. }
  1105. hdb_handle_put (&object_instance_database, parent_object_handle);
  1106. objdb_rdunlock();
  1107. return (res);
  1108. error_exit:
  1109. objdb_rdunlock();
  1110. return (-1);
  1111. }
  1112. static int object_parent_get(unsigned int object_handle,
  1113. unsigned int *parent_handle)
  1114. {
  1115. struct object_instance *instance;
  1116. unsigned int res;
  1117. objdb_rdlock();
  1118. res = hdb_handle_get (&object_instance_database,
  1119. object_handle, (void *)&instance);
  1120. if (res != 0) {
  1121. objdb_rdunlock();
  1122. return (res);
  1123. }
  1124. if (object_handle == OBJECT_PARENT_HANDLE)
  1125. *parent_handle = 0;
  1126. else
  1127. *parent_handle = instance->parent_handle;
  1128. hdb_handle_put (&object_instance_database, object_handle);
  1129. objdb_rdunlock();
  1130. return (0);
  1131. }
  1132. static int object_name_get(unsigned int object_handle,
  1133. char *object_name,
  1134. int *object_name_len)
  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. memcpy(object_name, instance->object_name, instance->object_name_len);
  1146. *object_name_len = instance->object_name_len;
  1147. hdb_handle_put (&object_instance_database, object_handle);
  1148. objdb_rdunlock();
  1149. return (0);
  1150. }
  1151. static int object_track_start(unsigned int object_handle,
  1152. object_track_depth_t depth,
  1153. object_key_change_notify_fn_t key_change_notify_fn,
  1154. object_create_notify_fn_t object_create_notify_fn,
  1155. object_destroy_notify_fn_t object_destroy_notify_fn,
  1156. object_reload_notify_fn_t object_reload_notify_fn,
  1157. void * priv_data_pt)
  1158. {
  1159. struct object_instance *instance;
  1160. unsigned int res;
  1161. struct object_tracker * tracker_pt;
  1162. res = hdb_handle_get (&object_instance_database,
  1163. object_handle, (void *)&instance);
  1164. if (res != 0) {
  1165. return (res);
  1166. }
  1167. tracker_pt = malloc(sizeof(struct object_tracker));
  1168. tracker_pt->depth = depth;
  1169. tracker_pt->object_handle = object_handle;
  1170. tracker_pt->key_change_notify_fn = key_change_notify_fn;
  1171. tracker_pt->object_create_notify_fn = object_create_notify_fn;
  1172. tracker_pt->object_destroy_notify_fn = object_destroy_notify_fn;
  1173. tracker_pt->object_reload_notify_fn = object_reload_notify_fn;
  1174. tracker_pt->data_pt = priv_data_pt;
  1175. list_init(&tracker_pt->object_list);
  1176. list_init(&tracker_pt->tracker_list);
  1177. list_add(&tracker_pt->object_list, &instance->track_head);
  1178. list_add(&tracker_pt->tracker_list, &objdb_trackers_head);
  1179. hdb_handle_put (&object_instance_database, object_handle);
  1180. return (res);
  1181. }
  1182. static void object_track_stop(object_key_change_notify_fn_t key_change_notify_fn,
  1183. object_create_notify_fn_t object_create_notify_fn,
  1184. object_destroy_notify_fn_t object_destroy_notify_fn,
  1185. object_reload_notify_fn_t object_reload_notify_fn,
  1186. void * priv_data_pt)
  1187. {
  1188. struct object_instance *instance;
  1189. struct object_tracker * tracker_pt = NULL;
  1190. struct object_tracker * obj_tracker_pt = NULL;
  1191. struct list_head *list, *tmp_list;
  1192. struct list_head *obj_list, *tmp_obj_list;
  1193. unsigned int res;
  1194. /* go through the global list and find all the trackers to stop */
  1195. for (list = objdb_trackers_head.next, tmp_list = list->next;
  1196. list != &objdb_trackers_head; list = tmp_list, tmp_list = tmp_list->next) {
  1197. tracker_pt = list_entry (list, struct object_tracker, tracker_list);
  1198. if (tracker_pt && (tracker_pt->data_pt == priv_data_pt) &&
  1199. (tracker_pt->object_create_notify_fn == object_create_notify_fn) &&
  1200. (tracker_pt->object_destroy_notify_fn == object_destroy_notify_fn) &&
  1201. (tracker_pt->object_reload_notify_fn == object_reload_notify_fn) &&
  1202. (tracker_pt->key_change_notify_fn == key_change_notify_fn)) {
  1203. /* get the object & take this tracker off of it's list. */
  1204. res = hdb_handle_get (&object_instance_database,
  1205. tracker_pt->object_handle, (void *)&instance);
  1206. if (res != 0) continue;
  1207. for (obj_list = instance->track_head.next, tmp_obj_list = obj_list->next;
  1208. obj_list != &instance->track_head; obj_list = tmp_obj_list, tmp_obj_list = tmp_obj_list->next) {
  1209. obj_tracker_pt = list_entry (obj_list, struct object_tracker, object_list);
  1210. if (obj_tracker_pt == tracker_pt) {
  1211. /* this is the tracker we are after. */
  1212. list_del(obj_list);
  1213. }
  1214. }
  1215. hdb_handle_put (&object_instance_database, tracker_pt->object_handle);
  1216. /* remove the tracker off of the global list */
  1217. list_del(list);
  1218. free(tracker_pt);
  1219. }
  1220. }
  1221. }
  1222. static int object_dump(unsigned int object_handle,
  1223. FILE *file)
  1224. {
  1225. struct object_instance *instance;
  1226. unsigned int res;
  1227. objdb_rdlock();
  1228. res = hdb_handle_get (&object_instance_database,
  1229. object_handle, (void *)&instance);
  1230. if (res != 0) {
  1231. objdb_rdunlock();
  1232. return (res);
  1233. }
  1234. res = _dump_object(instance, file, -1);
  1235. hdb_handle_put (&object_instance_database, object_handle);
  1236. objdb_rdunlock();
  1237. return (res);
  1238. }
  1239. static int object_write_config(char **error_string)
  1240. {
  1241. struct config_iface_ver0 **modules;
  1242. int num_modules;
  1243. int i;
  1244. int res;
  1245. main_get_config_modules(&modules, &num_modules);
  1246. objdb_wrlock();
  1247. for (i=0; i<num_modules; i++) {
  1248. if (modules[i]->config_writeconfig) {
  1249. res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  1250. if (res) {
  1251. objdb_wrunlock();
  1252. return res;
  1253. }
  1254. }
  1255. }
  1256. objdb_wrunlock();
  1257. return 0;
  1258. }
  1259. static int object_reload_config(int flush, char **error_string)
  1260. {
  1261. struct config_iface_ver0 **modules;
  1262. int num_modules;
  1263. int i;
  1264. int res;
  1265. main_get_config_modules(&modules, &num_modules);
  1266. object_reload_notification(OBJDB_RELOAD_NOTIFY_START, flush);
  1267. objdb_wrlock();
  1268. for (i=0; i<num_modules; i++) {
  1269. if (modules[i]->config_reloadconfig) {
  1270. res = modules[i]->config_reloadconfig(&objdb_iface, flush, error_string);
  1271. if (res) {
  1272. object_reload_notification(OBJDB_RELOAD_NOTIFY_FAILED, flush);
  1273. objdb_wrunlock();
  1274. return res;
  1275. }
  1276. }
  1277. }
  1278. objdb_wrunlock();
  1279. object_reload_notification(OBJDB_RELOAD_NOTIFY_END, flush);
  1280. return 0;
  1281. }
  1282. struct objdb_iface_ver0 objdb_iface = {
  1283. .objdb_init = objdb_init,
  1284. .object_create = object_create,
  1285. .object_priv_set = object_priv_set,
  1286. .object_key_create = object_key_create,
  1287. .object_key_delete = object_key_delete,
  1288. .object_key_replace = object_key_replace,
  1289. .object_destroy = object_destroy,
  1290. .object_valid_set = object_valid_set,
  1291. .object_key_valid_set = object_key_valid_set,
  1292. .object_find_create = object_find_create,
  1293. .object_find_next = object_find_next,
  1294. .object_find_destroy = object_find_destroy,
  1295. .object_key_get = object_key_get,
  1296. .object_key_iter_reset = object_key_iter_reset,
  1297. .object_key_iter = object_key_iter,
  1298. .object_key_iter_from = object_key_iter_from,
  1299. .object_priv_get = object_priv_get,
  1300. .object_parent_get = object_parent_get,
  1301. .object_name_get = object_name_get,
  1302. .object_track_start = object_track_start,
  1303. .object_track_stop = object_track_stop,
  1304. .object_dump = object_dump,
  1305. .object_write_config = object_write_config,
  1306. .object_reload_config = object_reload_config,
  1307. .object_key_increment = object_key_increment,
  1308. .object_key_decrement = object_key_decrement,
  1309. };
  1310. struct lcr_iface objdb_iface_ver0[1] = {
  1311. {
  1312. .name = "objdb",
  1313. .version = 0,
  1314. .versions_replace = 0,
  1315. .versions_replace_count = 0,
  1316. .dependencies = 0,
  1317. .dependency_count = 0,
  1318. .constructor = NULL,
  1319. .destructor = NULL,
  1320. .interfaces = NULL,
  1321. }
  1322. };
  1323. struct lcr_comp objdb_comp_ver0 = {
  1324. .iface_count = 1,
  1325. .ifaces = objdb_iface_ver0
  1326. };
  1327. __attribute__ ((constructor)) static void objdb_comp_register (void) {
  1328. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  1329. lcr_component_register (&objdb_comp_ver0);
  1330. }