objdb.c 46 KB

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