objdb.c 45 KB

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