4
0

objdb.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  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. size_t expected_size;
  438. int test_size_by_type = CS_TRUE;
  439. objdb_rdlock();
  440. res = hdb_handle_get (&object_instance_database,
  441. object_handle, (void *)&instance);
  442. if (res != 0) {
  443. goto error_exit;
  444. }
  445. switch (value_type) {
  446. case OBJDB_VALUETYPE_INT16:
  447. expected_size = sizeof (int16_t);
  448. break;
  449. case OBJDB_VALUETYPE_UINT16:
  450. expected_size = sizeof (uint16_t);
  451. break;
  452. case OBJDB_VALUETYPE_INT32:
  453. expected_size = sizeof (int32_t);
  454. break;
  455. case OBJDB_VALUETYPE_UINT32:
  456. expected_size = sizeof (uint32_t);
  457. break;
  458. case OBJDB_VALUETYPE_INT64:
  459. expected_size = sizeof (int64_t);
  460. break;
  461. case OBJDB_VALUETYPE_UINT64:
  462. expected_size = sizeof (uint64_t);
  463. break;
  464. case OBJDB_VALUETYPE_FLOAT:
  465. expected_size = sizeof (float);
  466. break;
  467. case OBJDB_VALUETYPE_DOUBLE:
  468. expected_size = sizeof (double);
  469. break;
  470. case OBJDB_VALUETYPE_ANY:
  471. default:
  472. test_size_by_type = CS_FALSE;
  473. break;
  474. }
  475. if (test_size_by_type) {
  476. if (expected_size != value_len) {
  477. //printf ("%s exp:%d != len:%d\n", key_name, expected_size, value_len);
  478. goto error_put;
  479. }
  480. }
  481. /*
  482. * Do validation check if validation is configured for the parent object
  483. */
  484. if (instance->object_key_valid_list_entries) {
  485. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  486. if ((key_len ==
  487. instance->object_key_valid_list[i].key_len) &&
  488. (memcmp (key_name,
  489. instance->object_key_valid_list[i].key_name,
  490. key_len) == 0)) {
  491. found = 1;
  492. break;
  493. }
  494. }
  495. /*
  496. * Item not found in validation list
  497. */
  498. if (found == 0) {
  499. goto error_put;
  500. } else {
  501. if (instance->object_key_valid_list[i].validate_callback) {
  502. res = instance->object_key_valid_list[i].validate_callback (
  503. key_name, key_len, value, value_len);
  504. if (res != 0) {
  505. goto error_put;
  506. }
  507. }
  508. }
  509. }
  510. /* See if it already exists */
  511. found = 0;
  512. for (list = instance->key_head.next;
  513. list != &instance->key_head; list = list->next) {
  514. object_key = list_entry (list, struct object_key, list);
  515. if ((object_key->key_len == key_len) &&
  516. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  517. found = 1;
  518. break;
  519. }
  520. }
  521. if (found) {
  522. free(object_key->value);
  523. }
  524. else {
  525. object_key = malloc (sizeof (struct object_key));
  526. if (object_key == 0) {
  527. goto error_put;
  528. }
  529. object_key->key_name = malloc (key_len + 1);
  530. if (object_key->key_name == 0) {
  531. goto error_put_object;
  532. }
  533. memcpy (object_key->key_name, key_name, key_len + 1);
  534. list_init (&object_key->list);
  535. list_add_tail (&object_key->list, &instance->key_head);
  536. }
  537. object_key->value = malloc (value_len);
  538. if (object_key->value == 0) {
  539. goto error_put_key;
  540. }
  541. memcpy (object_key->value, value, value_len);
  542. object_key->key_len = key_len;
  543. object_key->value_len = value_len;
  544. object_key->value_type = value_type;
  545. object_key_changed_notification(object_handle, key_name, key_len,
  546. value, value_len, OBJECT_KEY_CREATED);
  547. objdb_rdunlock();
  548. return (0);
  549. error_put_key:
  550. free (object_key->key_name);
  551. error_put_object:
  552. free (object_key);
  553. error_put:
  554. hdb_handle_put (&object_instance_database, object_handle);
  555. error_exit:
  556. objdb_rdunlock();
  557. return (-1);
  558. }
  559. static int object_key_create (
  560. hdb_handle_t object_handle,
  561. const void *key_name,
  562. size_t key_len,
  563. const void *value,
  564. size_t value_len)
  565. {
  566. return object_key_create_typed (object_handle, key_name,
  567. value, value_len, OBJDB_VALUETYPE_ANY);
  568. }
  569. static int _clear_object(struct object_instance *instance)
  570. {
  571. struct list_head *list;
  572. int res;
  573. struct object_instance *find_instance = NULL;
  574. struct object_key *object_key = NULL;
  575. for (list = instance->key_head.next;
  576. list != &instance->key_head; ) {
  577. object_key = list_entry (list, struct object_key,
  578. list);
  579. list = list->next;
  580. list_del(&object_key->list);
  581. free(object_key->key_name);
  582. free(object_key->value);
  583. }
  584. for (list = instance->child_head.next;
  585. list != &instance->child_head; ) {
  586. find_instance = list_entry (list, struct object_instance,
  587. child_list);
  588. res = _clear_object(find_instance);
  589. if (res)
  590. return res;
  591. list = list->next;
  592. list_del(&find_instance->child_list);
  593. free(find_instance->object_name);
  594. free(find_instance);
  595. }
  596. return 0;
  597. }
  598. static int object_destroy (
  599. hdb_handle_t object_handle)
  600. {
  601. struct object_instance *instance;
  602. unsigned int res;
  603. objdb_rdlock();
  604. res = hdb_handle_get (&object_instance_database,
  605. object_handle, (void *)&instance);
  606. if (res != 0) {
  607. objdb_rdunlock();
  608. return (res);
  609. }
  610. object_pre_deletion_notification(object_handle,
  611. instance->parent_handle,
  612. instance->object_name,
  613. instance->object_name_len);
  614. /* Recursively clear sub-objects & keys */
  615. res = _clear_object(instance);
  616. list_del(&instance->child_list);
  617. free(instance->object_name);
  618. free(instance);
  619. objdb_rdunlock();
  620. return (res);
  621. }
  622. static int object_valid_set (
  623. hdb_handle_t object_handle,
  624. struct object_valid *object_valid_list,
  625. size_t object_valid_list_entries)
  626. {
  627. struct object_instance *instance;
  628. unsigned int res;
  629. objdb_rdlock();
  630. res = hdb_handle_get (&object_instance_database,
  631. object_handle, (void *)&instance);
  632. if (res != 0) {
  633. goto error_exit;
  634. }
  635. instance->object_valid_list = object_valid_list;
  636. instance->object_valid_list_entries = object_valid_list_entries;
  637. hdb_handle_put (&object_instance_database, object_handle);
  638. objdb_rdunlock();
  639. return (0);
  640. error_exit:
  641. objdb_rdunlock();
  642. return (-1);
  643. }
  644. static int object_key_valid_set (
  645. hdb_handle_t object_handle,
  646. struct object_key_valid *object_key_valid_list,
  647. size_t object_key_valid_list_entries)
  648. {
  649. struct object_instance *instance;
  650. unsigned int res;
  651. objdb_rdlock();
  652. res = hdb_handle_get (&object_instance_database,
  653. object_handle, (void *)&instance);
  654. if (res != 0) {
  655. goto error_exit;
  656. }
  657. instance->object_key_valid_list = object_key_valid_list;
  658. instance->object_key_valid_list_entries = object_key_valid_list_entries;
  659. hdb_handle_put (&object_instance_database, object_handle);
  660. objdb_rdunlock();
  661. return (0);
  662. error_exit:
  663. objdb_rdunlock();
  664. return (-1);
  665. }
  666. /*
  667. * object db reading
  668. */
  669. static int object_find_create (
  670. hdb_handle_t object_handle,
  671. const void *object_name,
  672. size_t object_len,
  673. hdb_handle_t *object_find_handle)
  674. {
  675. unsigned int res;
  676. struct object_instance *object_instance;
  677. struct object_find_instance *object_find_instance;
  678. objdb_rdlock();
  679. res = hdb_handle_get (&object_instance_database,
  680. object_handle, (void *)&object_instance);
  681. if (res != 0) {
  682. goto error_exit;
  683. }
  684. res = hdb_handle_create (&object_find_instance_database,
  685. sizeof (struct object_find_instance), object_find_handle);
  686. if (res != 0) {
  687. goto error_put;
  688. }
  689. res = hdb_handle_get (&object_find_instance_database,
  690. *object_find_handle, (void *)&object_find_instance);
  691. if (res != 0) {
  692. goto error_destroy;
  693. }
  694. object_find_instance->find_child_list = &object_instance->child_head;
  695. object_find_instance->child_head = &object_instance->child_head;
  696. object_find_instance->object_name = (char *)object_name;
  697. object_find_instance->object_len = object_len;
  698. hdb_handle_put (&object_instance_database, object_handle);
  699. hdb_handle_put (&object_find_instance_database, *object_find_handle);
  700. objdb_rdunlock();
  701. return (0);
  702. error_destroy:
  703. hdb_handle_destroy (&object_instance_database, *object_find_handle);
  704. error_put:
  705. hdb_handle_put (&object_instance_database, object_handle);
  706. error_exit:
  707. objdb_rdunlock();
  708. return (-1);
  709. }
  710. static int object_find_next (
  711. hdb_handle_t object_find_handle,
  712. hdb_handle_t *object_handle)
  713. {
  714. unsigned int res;
  715. struct object_find_instance *object_find_instance;
  716. struct object_instance *object_instance = NULL;
  717. struct list_head *list;
  718. unsigned int found = 0;
  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. res = -1;
  726. for (list = object_find_instance->find_child_list->next;
  727. list != object_find_instance->child_head; list = list->next) {
  728. object_instance = list_entry (list, struct object_instance,
  729. child_list);
  730. if (object_find_instance->object_len == 0 ||
  731. ((object_instance->object_name_len ==
  732. object_find_instance->object_len) &&
  733. (memcmp (object_instance->object_name,
  734. object_find_instance->object_name,
  735. object_find_instance->object_len) == 0))) {
  736. found = 1;
  737. break;
  738. }
  739. }
  740. object_find_instance->find_child_list = list;
  741. hdb_handle_put (&object_find_instance_database, object_find_handle);
  742. if (found) {
  743. *object_handle = object_instance->object_handle;
  744. res = 0;
  745. }
  746. objdb_rdunlock();
  747. return (res);
  748. error_exit:
  749. objdb_rdunlock();
  750. return (-1);
  751. }
  752. static int object_find_destroy (
  753. hdb_handle_t object_find_handle)
  754. {
  755. struct object_find_instance *object_find_instance;
  756. unsigned int res;
  757. objdb_rdlock();
  758. res = hdb_handle_get (&object_find_instance_database,
  759. object_find_handle, (void *)&object_find_instance);
  760. if (res != 0) {
  761. goto error_exit;
  762. }
  763. hdb_handle_put(&object_find_instance_database, object_find_handle);
  764. hdb_handle_destroy(&object_find_instance_database, object_find_handle);
  765. objdb_rdunlock();
  766. return (0);
  767. error_exit:
  768. objdb_rdunlock();
  769. return (-1);
  770. }
  771. static int object_key_get_typed (
  772. hdb_handle_t object_handle,
  773. const char *key_name,
  774. void **value,
  775. size_t *value_len,
  776. objdb_value_types_t * type)
  777. {
  778. unsigned int res = 0;
  779. struct object_instance *instance;
  780. struct object_key *object_key = NULL;
  781. struct list_head *list;
  782. int found = 0;
  783. size_t key_len = strlen(key_name);
  784. objdb_rdlock();
  785. res = hdb_handle_get (&object_instance_database,
  786. object_handle, (void *)&instance);
  787. if (res != 0) {
  788. goto error_exit;
  789. }
  790. for (list = instance->key_head.next;
  791. list != &instance->key_head; list = list->next) {
  792. object_key = list_entry (list, struct object_key, list);
  793. if ((object_key->key_len == key_len) &&
  794. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  795. found = 1;
  796. break;
  797. }
  798. }
  799. if (found) {
  800. *value = object_key->value;
  801. if (value_len) {
  802. *value_len = object_key->value_len;
  803. }
  804. *type = object_key->value_type;
  805. }
  806. else {
  807. res = -1;
  808. }
  809. hdb_handle_put (&object_instance_database, object_handle);
  810. objdb_rdunlock();
  811. return (res);
  812. error_exit:
  813. objdb_rdunlock();
  814. return (-1);
  815. }
  816. static int object_key_get (
  817. hdb_handle_t object_handle,
  818. const void *key_name,
  819. size_t key_len,
  820. void **value,
  821. size_t *value_len)
  822. {
  823. objdb_value_types_t t;
  824. return object_key_get_typed(object_handle,
  825. key_name,
  826. value, value_len, &t);
  827. }
  828. static int object_key_increment (
  829. hdb_handle_t object_handle,
  830. const void *key_name,
  831. size_t key_len,
  832. unsigned int *value)
  833. {
  834. unsigned int res = 0;
  835. struct object_instance *instance;
  836. struct object_key *object_key = NULL;
  837. struct list_head *list;
  838. int found = 0;
  839. objdb_rdlock();
  840. res = hdb_handle_get (&object_instance_database,
  841. object_handle, (void *)&instance);
  842. if (res != 0) {
  843. goto error_exit;
  844. }
  845. for (list = instance->key_head.next;
  846. list != &instance->key_head; list = list->next) {
  847. object_key = list_entry (list, struct object_key, list);
  848. if ((object_key->key_len == key_len) &&
  849. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  850. found = 1;
  851. break;
  852. }
  853. }
  854. if (found) {
  855. switch (object_key->value_type) {
  856. case OBJDB_VALUETYPE_INT16:
  857. (*(int16_t *)object_key->value)++;
  858. break;
  859. case OBJDB_VALUETYPE_UINT16:
  860. (*(uint16_t *)object_key->value)++;
  861. break;
  862. case OBJDB_VALUETYPE_INT32:
  863. (*(int32_t *)object_key->value)++;
  864. break;
  865. case OBJDB_VALUETYPE_UINT32:
  866. (*(uint32_t *)object_key->value)++;
  867. break;
  868. case OBJDB_VALUETYPE_INT64:
  869. (*(int64_t *)object_key->value)++;
  870. break;
  871. case OBJDB_VALUETYPE_UINT64:
  872. (*(uint64_t *)object_key->value)++;
  873. break;
  874. case OBJDB_VALUETYPE_ANY:
  875. /* for backwards compatibilty */
  876. if (object_key->value_len == sizeof(int)) {
  877. (*(int *)object_key->value)++;
  878. }
  879. else {
  880. res = -1;
  881. }
  882. break;
  883. default:
  884. res = -1;
  885. break;
  886. }
  887. if (res == 0) {
  888. /* nasty, not sure why we need to return this typed
  889. * instead of void* */
  890. *value = *(int *)object_key->value;
  891. }
  892. }
  893. else {
  894. res = -1;
  895. }
  896. hdb_handle_put (&object_instance_database, object_handle);
  897. objdb_rdunlock();
  898. return (res);
  899. error_exit:
  900. objdb_rdunlock();
  901. return (-1);
  902. }
  903. static int object_key_decrement (
  904. hdb_handle_t object_handle,
  905. const void *key_name,
  906. size_t key_len,
  907. unsigned int *value)
  908. {
  909. unsigned int res = 0;
  910. struct object_instance *instance;
  911. struct object_key *object_key = NULL;
  912. struct list_head *list;
  913. int found = 0;
  914. objdb_rdlock();
  915. res = hdb_handle_get (&object_instance_database,
  916. object_handle, (void *)&instance);
  917. if (res != 0) {
  918. goto error_exit;
  919. }
  920. for (list = instance->key_head.next;
  921. list != &instance->key_head; list = list->next) {
  922. object_key = list_entry (list, struct object_key, list);
  923. if ((object_key->key_len == key_len) &&
  924. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  925. found = 1;
  926. break;
  927. }
  928. }
  929. if (found) {
  930. switch (object_key->value_type) {
  931. case OBJDB_VALUETYPE_INT16:
  932. (*(int16_t *)object_key->value)--;
  933. break;
  934. case OBJDB_VALUETYPE_UINT16:
  935. (*(uint16_t *)object_key->value)--;
  936. break;
  937. case OBJDB_VALUETYPE_INT32:
  938. (*(int32_t *)object_key->value)--;
  939. break;
  940. case OBJDB_VALUETYPE_UINT32:
  941. (*(uint32_t *)object_key->value)--;
  942. break;
  943. case OBJDB_VALUETYPE_INT64:
  944. (*(int64_t *)object_key->value)--;
  945. break;
  946. case OBJDB_VALUETYPE_UINT64:
  947. (*(uint64_t *)object_key->value)--;
  948. break;
  949. case OBJDB_VALUETYPE_ANY:
  950. /* for backwards compatibilty */
  951. if (object_key->value_len == sizeof(int)) {
  952. (*(int *)object_key->value)--;
  953. }
  954. else {
  955. res = -1;
  956. }
  957. break;
  958. default:
  959. res = -1;
  960. break;
  961. }
  962. if (res == 0) {
  963. /* nasty, not sure why we need to return this typed
  964. * instead of void* */
  965. *value = *(int *)object_key->value;
  966. }
  967. }
  968. else {
  969. res = -1;
  970. }
  971. hdb_handle_put (&object_instance_database, object_handle);
  972. objdb_rdunlock();
  973. return (res);
  974. error_exit:
  975. objdb_rdunlock();
  976. return (-1);
  977. }
  978. static int object_key_delete (
  979. hdb_handle_t object_handle,
  980. const void *key_name,
  981. size_t key_len)
  982. {
  983. unsigned int res;
  984. int ret = 0;
  985. struct object_instance *instance;
  986. struct object_key *object_key = NULL;
  987. struct list_head *list;
  988. int found = 0;
  989. objdb_rdlock();
  990. res = hdb_handle_get (&object_instance_database,
  991. object_handle, (void *)&instance);
  992. if (res != 0) {
  993. goto error_exit;
  994. }
  995. for (list = instance->key_head.next;
  996. list != &instance->key_head; list = list->next) {
  997. object_key = list_entry (list, struct object_key, list);
  998. if ((object_key->key_len == key_len) &&
  999. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  1000. found = 1;
  1001. break;
  1002. }
  1003. }
  1004. if (found) {
  1005. list_del(&object_key->list);
  1006. free(object_key->key_name);
  1007. free(object_key->value);
  1008. free(object_key);
  1009. }
  1010. else {
  1011. ret = -1;
  1012. errno = ENOENT;
  1013. }
  1014. hdb_handle_put (&object_instance_database, object_handle);
  1015. if (ret == 0)
  1016. object_key_changed_notification(object_handle, key_name, key_len,
  1017. NULL, 0, OBJECT_KEY_DELETED);
  1018. objdb_rdunlock();
  1019. return (ret);
  1020. error_exit:
  1021. objdb_rdunlock();
  1022. return (-1);
  1023. }
  1024. static int object_key_replace (
  1025. hdb_handle_t object_handle,
  1026. const void *key_name,
  1027. size_t key_len,
  1028. const void *new_value,
  1029. size_t new_value_len)
  1030. {
  1031. unsigned int res;
  1032. int ret = 0;
  1033. struct object_instance *instance;
  1034. struct object_key *object_key = NULL;
  1035. struct list_head *list;
  1036. int found = 0;
  1037. objdb_rdlock();
  1038. res = hdb_handle_get (&object_instance_database,
  1039. object_handle, (void *)&instance);
  1040. if (res != 0) {
  1041. goto error_exit;
  1042. }
  1043. for (list = instance->key_head.next;
  1044. list != &instance->key_head; list = list->next) {
  1045. object_key = list_entry (list, struct object_key, list);
  1046. if ((object_key->key_len == key_len) &&
  1047. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  1048. found = 1;
  1049. break;
  1050. }
  1051. }
  1052. if (found) {
  1053. int i;
  1054. int found_validator = 0;
  1055. /*
  1056. * Do validation check if validation is configured for the parent object
  1057. */
  1058. if (instance->object_key_valid_list_entries) {
  1059. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  1060. if ((key_len ==
  1061. instance->object_key_valid_list[i].key_len) &&
  1062. (memcmp (key_name,
  1063. instance->object_key_valid_list[i].key_name,
  1064. key_len) == 0)) {
  1065. found_validator = 1;
  1066. break;
  1067. }
  1068. }
  1069. /*
  1070. * Item not found in validation list
  1071. */
  1072. if (found_validator == 0) {
  1073. goto error_put;
  1074. } else {
  1075. if (instance->object_key_valid_list[i].validate_callback) {
  1076. res = instance->object_key_valid_list[i].validate_callback (
  1077. key_name, key_len, new_value, new_value_len);
  1078. if (res != 0) {
  1079. goto error_put;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. if (new_value_len != object_key->value_len) {
  1085. void *replacement_value;
  1086. replacement_value = malloc(new_value_len);
  1087. if (!replacement_value)
  1088. goto error_exit;
  1089. free(object_key->value);
  1090. object_key->value = replacement_value;
  1091. }
  1092. memcpy(object_key->value, new_value, new_value_len);
  1093. object_key->value_len = new_value_len;
  1094. }
  1095. else {
  1096. ret = -1;
  1097. errno = ENOENT;
  1098. }
  1099. hdb_handle_put (&object_instance_database, object_handle);
  1100. if (ret == 0)
  1101. object_key_changed_notification(object_handle, key_name, key_len,
  1102. new_value, new_value_len, OBJECT_KEY_REPLACED);
  1103. objdb_rdunlock();
  1104. return (ret);
  1105. error_put:
  1106. hdb_handle_put (&object_instance_database, object_handle);
  1107. error_exit:
  1108. objdb_rdunlock();
  1109. return (-1);
  1110. }
  1111. static int object_priv_get (
  1112. hdb_handle_t object_handle,
  1113. void **priv)
  1114. {
  1115. int res;
  1116. struct object_instance *object_instance;
  1117. objdb_rdunlock();
  1118. res = hdb_handle_get (&object_instance_database,
  1119. object_handle, (void *)&object_instance);
  1120. if (res != 0) {
  1121. goto error_exit;
  1122. }
  1123. *priv = object_instance->priv;
  1124. hdb_handle_put (&object_instance_database, object_handle);
  1125. objdb_rdunlock();
  1126. return (0);
  1127. error_exit:
  1128. objdb_rdunlock();
  1129. return (-1);
  1130. }
  1131. static int _dump_object(struct object_instance *instance, FILE *file, int depth)
  1132. {
  1133. struct list_head *list;
  1134. int res;
  1135. int i;
  1136. struct object_instance *find_instance = NULL;
  1137. struct object_key *object_key = NULL;
  1138. char stringbuf1[1024];
  1139. char stringbuf2[1024];
  1140. memcpy(stringbuf1, instance->object_name, instance->object_name_len);
  1141. stringbuf1[instance->object_name_len] = '\0';
  1142. for (i=0; i<depth; i++)
  1143. fprintf(file, " ");
  1144. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1145. fprintf(file, "%s {\n", stringbuf1);
  1146. for (list = instance->key_head.next;
  1147. list != &instance->key_head; list = list->next) {
  1148. object_key = list_entry (list, struct object_key,
  1149. list);
  1150. memcpy(stringbuf1, object_key->key_name, object_key->key_len);
  1151. stringbuf1[object_key->key_len] = '\0';
  1152. switch (object_key->value_type) {
  1153. case OBJDB_VALUETYPE_INT16:
  1154. snprintf (stringbuf2, sizeof(int), "%hd",
  1155. *(unsigned int*)object_key->value);
  1156. break;
  1157. case OBJDB_VALUETYPE_UINT16:
  1158. snprintf (stringbuf2, sizeof(int), "%hu",
  1159. *(unsigned int*)object_key->value);
  1160. break;
  1161. case OBJDB_VALUETYPE_INT32:
  1162. snprintf (stringbuf2, sizeof(int), "%d",
  1163. *(int*)object_key->value);
  1164. break;
  1165. case OBJDB_VALUETYPE_UINT32:
  1166. snprintf (stringbuf2, sizeof(int), "%u",
  1167. *(unsigned int*)object_key->value);
  1168. break;
  1169. case OBJDB_VALUETYPE_INT64:
  1170. snprintf (stringbuf2, sizeof(int), "%ld",
  1171. *(long int*)object_key->value);
  1172. break;
  1173. case OBJDB_VALUETYPE_UINT64:
  1174. snprintf (stringbuf2, sizeof(int), "%lu",
  1175. *(unsigned long int*)object_key->value);
  1176. break;
  1177. default:
  1178. case OBJDB_VALUETYPE_STRING:
  1179. case OBJDB_VALUETYPE_ANY:
  1180. memcpy(stringbuf2, object_key->value, object_key->value_len);
  1181. stringbuf2[object_key->value_len] = '\0';
  1182. break;
  1183. }
  1184. for (i=0; i<depth+1; i++)
  1185. fprintf(file, " ");
  1186. fprintf(file, "%s: %s\n", stringbuf1, stringbuf2);
  1187. }
  1188. for (list = instance->child_head.next;
  1189. list != &instance->child_head; list = list->next) {
  1190. find_instance = list_entry (list, struct object_instance,
  1191. child_list);
  1192. res = _dump_object(find_instance, file, depth+1);
  1193. if (res)
  1194. return res;
  1195. }
  1196. for (i=0; i<depth; i++)
  1197. fprintf(file, " ");
  1198. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1199. fprintf(file, "}\n");
  1200. return 0;
  1201. }
  1202. static int object_key_iter_reset(hdb_handle_t object_handle)
  1203. {
  1204. unsigned int res;
  1205. struct object_instance *instance;
  1206. objdb_rdlock();
  1207. res = hdb_handle_get (&object_instance_database,
  1208. object_handle, (void *)&instance);
  1209. if (res != 0) {
  1210. goto error_exit;
  1211. }
  1212. instance->iter_key_list = &instance->key_head;
  1213. hdb_handle_put (&object_instance_database, object_handle);
  1214. objdb_rdunlock();
  1215. return (0);
  1216. error_exit:
  1217. objdb_rdunlock();
  1218. return (-1);
  1219. }
  1220. static int object_key_iter_typed (hdb_handle_t parent_object_handle,
  1221. char **key_name,
  1222. void **value,
  1223. size_t *value_len,
  1224. objdb_value_types_t *type)
  1225. {
  1226. unsigned int res;
  1227. struct object_instance *instance;
  1228. struct object_key *find_key = NULL;
  1229. struct list_head *list;
  1230. unsigned int found = 0;
  1231. objdb_rdlock();
  1232. res = hdb_handle_get (&object_instance_database,
  1233. parent_object_handle, (void *)&instance);
  1234. if (res != 0) {
  1235. goto error_exit;
  1236. }
  1237. res = -ENOENT;
  1238. list = instance->iter_key_list->next;
  1239. if (list != &instance->key_head) {
  1240. find_key = list_entry (list, struct object_key, list);
  1241. found = 1;
  1242. }
  1243. instance->iter_key_list = list;
  1244. if (found) {
  1245. *key_name = find_key->key_name;
  1246. *value = find_key->value;
  1247. *type = find_key->value_type;
  1248. if (value_len)
  1249. *value_len = find_key->value_len;
  1250. res = 0;
  1251. }
  1252. else {
  1253. res = -1;
  1254. }
  1255. hdb_handle_put (&object_instance_database, parent_object_handle);
  1256. objdb_rdunlock();
  1257. return (res);
  1258. error_exit:
  1259. objdb_rdunlock();
  1260. return (-1);
  1261. }
  1262. static int object_key_iter(hdb_handle_t parent_object_handle,
  1263. void **key_name,
  1264. size_t *key_len,
  1265. void **value,
  1266. size_t *value_len)
  1267. {
  1268. objdb_value_types_t t;
  1269. int ret;
  1270. char *str;
  1271. ret = object_key_iter_typed (parent_object_handle, (char**)key_name, value, value_len, &t);
  1272. str = *key_name;
  1273. *key_len = strlen(str);
  1274. return ret;
  1275. }
  1276. static int object_key_iter_from(hdb_handle_t parent_object_handle,
  1277. hdb_handle_t start_pos,
  1278. void **key_name,
  1279. size_t *key_len,
  1280. void **value,
  1281. size_t *value_len)
  1282. {
  1283. unsigned int pos = 0;
  1284. unsigned int res;
  1285. struct object_instance *instance;
  1286. struct object_key *find_key = NULL;
  1287. struct list_head *list;
  1288. unsigned int found = 0;
  1289. objdb_rdlock();
  1290. res = hdb_handle_get (&object_instance_database,
  1291. parent_object_handle, (void *)&instance);
  1292. if (res != 0) {
  1293. goto error_exit;
  1294. }
  1295. res = -ENOENT;
  1296. for (list = instance->key_head.next;
  1297. list != &instance->key_head; list = list->next) {
  1298. find_key = list_entry (list, struct object_key, list);
  1299. if (pos++ == start_pos) {
  1300. found = 1;
  1301. break;
  1302. }
  1303. }
  1304. if (found) {
  1305. *key_name = find_key->key_name;
  1306. if (key_len)
  1307. *key_len = find_key->key_len;
  1308. *value = find_key->value;
  1309. if (value_len)
  1310. *value_len = find_key->value_len;
  1311. res = 0;
  1312. }
  1313. else {
  1314. res = -1;
  1315. }
  1316. hdb_handle_put (&object_instance_database, parent_object_handle);
  1317. objdb_rdunlock();
  1318. return (res);
  1319. error_exit:
  1320. objdb_rdunlock();
  1321. return (-1);
  1322. }
  1323. static int object_parent_get(hdb_handle_t object_handle,
  1324. hdb_handle_t *parent_handle)
  1325. {
  1326. struct object_instance *instance;
  1327. unsigned int res;
  1328. objdb_rdlock();
  1329. res = hdb_handle_get (&object_instance_database,
  1330. object_handle, (void *)&instance);
  1331. if (res != 0) {
  1332. objdb_rdunlock();
  1333. return (res);
  1334. }
  1335. if (object_handle == OBJECT_PARENT_HANDLE)
  1336. *parent_handle = 0;
  1337. else
  1338. *parent_handle = instance->parent_handle;
  1339. hdb_handle_put (&object_instance_database, object_handle);
  1340. objdb_rdunlock();
  1341. return (0);
  1342. }
  1343. static int object_name_get(hdb_handle_t object_handle,
  1344. char *object_name,
  1345. size_t *object_name_len)
  1346. {
  1347. struct object_instance *instance;
  1348. unsigned int res;
  1349. objdb_rdlock();
  1350. res = hdb_handle_get (&object_instance_database,
  1351. object_handle, (void *)&instance);
  1352. if (res != 0) {
  1353. objdb_rdunlock();
  1354. return (res);
  1355. }
  1356. memcpy(object_name, instance->object_name, instance->object_name_len);
  1357. *object_name_len = instance->object_name_len;
  1358. hdb_handle_put (&object_instance_database, object_handle);
  1359. objdb_rdunlock();
  1360. return (0);
  1361. }
  1362. static int object_track_start(hdb_handle_t object_handle,
  1363. object_track_depth_t depth,
  1364. object_key_change_notify_fn_t key_change_notify_fn,
  1365. object_create_notify_fn_t object_create_notify_fn,
  1366. object_destroy_notify_fn_t object_destroy_notify_fn,
  1367. object_reload_notify_fn_t object_reload_notify_fn,
  1368. void * priv_data_pt)
  1369. {
  1370. struct object_instance *instance;
  1371. unsigned int res;
  1372. struct object_tracker * tracker_pt;
  1373. res = hdb_handle_get (&object_instance_database,
  1374. object_handle, (void *)&instance);
  1375. if (res != 0) {
  1376. return (res);
  1377. }
  1378. tracker_pt = malloc(sizeof(struct object_tracker));
  1379. tracker_pt->depth = depth;
  1380. tracker_pt->object_handle = object_handle;
  1381. tracker_pt->key_change_notify_fn = key_change_notify_fn;
  1382. tracker_pt->object_create_notify_fn = object_create_notify_fn;
  1383. tracker_pt->object_destroy_notify_fn = object_destroy_notify_fn;
  1384. tracker_pt->object_reload_notify_fn = object_reload_notify_fn;
  1385. tracker_pt->data_pt = priv_data_pt;
  1386. list_init(&tracker_pt->object_list);
  1387. list_init(&tracker_pt->tracker_list);
  1388. list_add(&tracker_pt->object_list, &instance->track_head);
  1389. list_add(&tracker_pt->tracker_list, &objdb_trackers_head);
  1390. hdb_handle_put (&object_instance_database, object_handle);
  1391. return (res);
  1392. }
  1393. static void object_track_stop(object_key_change_notify_fn_t key_change_notify_fn,
  1394. object_create_notify_fn_t object_create_notify_fn,
  1395. object_destroy_notify_fn_t object_destroy_notify_fn,
  1396. object_reload_notify_fn_t object_reload_notify_fn,
  1397. void * priv_data_pt)
  1398. {
  1399. struct object_instance *instance;
  1400. struct object_tracker * tracker_pt = NULL;
  1401. struct object_tracker * obj_tracker_pt = NULL;
  1402. struct list_head *list, *tmp_list;
  1403. struct list_head *obj_list, *tmp_obj_list;
  1404. unsigned int res;
  1405. /* go through the global list and find all the trackers to stop */
  1406. for (list = objdb_trackers_head.next, tmp_list = list->next;
  1407. list != &objdb_trackers_head; list = tmp_list, tmp_list = tmp_list->next) {
  1408. tracker_pt = list_entry (list, struct object_tracker, tracker_list);
  1409. if (tracker_pt && (tracker_pt->data_pt == priv_data_pt) &&
  1410. (tracker_pt->object_create_notify_fn == object_create_notify_fn) &&
  1411. (tracker_pt->object_destroy_notify_fn == object_destroy_notify_fn) &&
  1412. (tracker_pt->object_reload_notify_fn == object_reload_notify_fn) &&
  1413. (tracker_pt->key_change_notify_fn == key_change_notify_fn)) {
  1414. /* get the object & take this tracker off of it's list. */
  1415. res = hdb_handle_get (&object_instance_database,
  1416. tracker_pt->object_handle, (void *)&instance);
  1417. if (res != 0) continue;
  1418. for (obj_list = instance->track_head.next, tmp_obj_list = obj_list->next;
  1419. obj_list != &instance->track_head; obj_list = tmp_obj_list, tmp_obj_list = tmp_obj_list->next) {
  1420. obj_tracker_pt = list_entry (obj_list, struct object_tracker, object_list);
  1421. if (obj_tracker_pt == tracker_pt) {
  1422. /* this is the tracker we are after. */
  1423. list_del(obj_list);
  1424. }
  1425. }
  1426. hdb_handle_put (&object_instance_database, tracker_pt->object_handle);
  1427. /* remove the tracker off of the global list */
  1428. list_del(list);
  1429. free(tracker_pt);
  1430. }
  1431. }
  1432. }
  1433. static int object_dump(hdb_handle_t object_handle,
  1434. FILE *file)
  1435. {
  1436. struct object_instance *instance;
  1437. unsigned int res;
  1438. objdb_rdlock();
  1439. res = hdb_handle_get (&object_instance_database,
  1440. object_handle, (void *)&instance);
  1441. if (res != 0) {
  1442. objdb_rdunlock();
  1443. return (res);
  1444. }
  1445. res = _dump_object(instance, file, -1);
  1446. hdb_handle_put (&object_instance_database, object_handle);
  1447. objdb_rdunlock();
  1448. return (res);
  1449. }
  1450. static int object_write_config(const char **error_string)
  1451. {
  1452. struct config_iface_ver0 **modules;
  1453. int num_modules;
  1454. int i;
  1455. int res;
  1456. main_get_config_modules(&modules, &num_modules);
  1457. objdb_wrlock();
  1458. for (i=0; i<num_modules; i++) {
  1459. if (modules[i]->config_writeconfig) {
  1460. res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  1461. if (res) {
  1462. objdb_wrunlock();
  1463. return res;
  1464. }
  1465. }
  1466. }
  1467. objdb_wrunlock();
  1468. return 0;
  1469. }
  1470. static int object_reload_config(int flush, const char **error_string)
  1471. {
  1472. struct config_iface_ver0 **modules;
  1473. int num_modules;
  1474. int i;
  1475. int res;
  1476. main_get_config_modules(&modules, &num_modules);
  1477. object_reload_notification(OBJDB_RELOAD_NOTIFY_START, flush);
  1478. objdb_wrlock();
  1479. for (i=0; i<num_modules; i++) {
  1480. if (modules[i]->config_reloadconfig) {
  1481. res = modules[i]->config_reloadconfig(&objdb_iface, flush, error_string);
  1482. if (res) {
  1483. object_reload_notification(OBJDB_RELOAD_NOTIFY_FAILED, flush);
  1484. objdb_wrunlock();
  1485. return res;
  1486. }
  1487. }
  1488. }
  1489. objdb_wrunlock();
  1490. object_reload_notification(OBJDB_RELOAD_NOTIFY_END, flush);
  1491. return 0;
  1492. }
  1493. struct objdb_iface_ver0 objdb_iface = {
  1494. .objdb_init = objdb_init,
  1495. .object_create = object_create,
  1496. .object_priv_set = object_priv_set,
  1497. .object_key_create = object_key_create,
  1498. .object_key_delete = object_key_delete,
  1499. .object_key_replace = object_key_replace,
  1500. .object_destroy = object_destroy,
  1501. .object_valid_set = object_valid_set,
  1502. .object_key_valid_set = object_key_valid_set,
  1503. .object_find_create = object_find_create,
  1504. .object_find_next = object_find_next,
  1505. .object_find_destroy = object_find_destroy,
  1506. .object_key_get = object_key_get,
  1507. .object_key_iter_reset = object_key_iter_reset,
  1508. .object_key_iter = object_key_iter,
  1509. .object_key_iter_from = object_key_iter_from,
  1510. .object_priv_get = object_priv_get,
  1511. .object_parent_get = object_parent_get,
  1512. .object_name_get = object_name_get,
  1513. .object_track_start = object_track_start,
  1514. .object_track_stop = object_track_stop,
  1515. .object_dump = object_dump,
  1516. .object_write_config = object_write_config,
  1517. .object_reload_config = object_reload_config,
  1518. .object_key_increment = object_key_increment,
  1519. .object_key_decrement = object_key_decrement,
  1520. .object_key_create_typed = object_key_create_typed,
  1521. .object_key_get_typed = object_key_get_typed,
  1522. .object_key_iter_typed = object_key_iter_typed,
  1523. };
  1524. struct lcr_iface objdb_iface_ver0[1] = {
  1525. {
  1526. .name = "objdb",
  1527. .version = 0,
  1528. .versions_replace = 0,
  1529. .versions_replace_count = 0,
  1530. .dependencies = 0,
  1531. .dependency_count = 0,
  1532. .constructor = NULL,
  1533. .destructor = NULL,
  1534. .interfaces = NULL,
  1535. }
  1536. };
  1537. struct lcr_comp objdb_comp_ver0 = {
  1538. .iface_count = 1,
  1539. .ifaces = objdb_iface_ver0
  1540. };
  1541. #ifdef COROSYNC_SOLARIS
  1542. void corosync_lcr_component_register (void);
  1543. void corosync_lcr_component_register (void) {
  1544. #else
  1545. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  1546. #endif
  1547. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  1548. lcr_component_register (&objdb_comp_ver0);
  1549. }