objdb.c 45 KB

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