objdb.c 45 KB

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