objdb.c 46 KB

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