objdb.c 47 KB

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