objdb.c 45 KB

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