objdb.c 44 KB

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