objdb.c 40 KB

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