objdb.c 46 KB

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