objdb.c 47 KB

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