objdb.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  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. objdb_rdunlock();
  929. return (res);
  930. error_exit:
  931. objdb_rdunlock();
  932. return (-1);
  933. }
  934. static int object_key_decrement (
  935. hdb_handle_t object_handle,
  936. const void *key_name,
  937. size_t key_len,
  938. unsigned int *value)
  939. {
  940. unsigned int res = 0;
  941. struct object_instance *instance;
  942. struct object_key *object_key = NULL;
  943. struct list_head *list;
  944. int found = 0;
  945. objdb_rdlock();
  946. res = hdb_handle_get (&object_instance_database,
  947. object_handle, (void *)&instance);
  948. if (res != 0) {
  949. goto error_exit;
  950. }
  951. for (list = instance->key_head.next;
  952. list != &instance->key_head; list = list->next) {
  953. object_key = list_entry (list, struct object_key, list);
  954. if ((object_key->key_len == key_len) &&
  955. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  956. found = 1;
  957. break;
  958. }
  959. }
  960. if (found) {
  961. switch (object_key->value_type) {
  962. case OBJDB_VALUETYPE_INT16:
  963. (*(int16_t *)object_key->value)--;
  964. break;
  965. case OBJDB_VALUETYPE_UINT16:
  966. (*(uint16_t *)object_key->value)--;
  967. break;
  968. case OBJDB_VALUETYPE_INT32:
  969. (*(int32_t *)object_key->value)--;
  970. break;
  971. case OBJDB_VALUETYPE_UINT32:
  972. (*(uint32_t *)object_key->value)--;
  973. break;
  974. case OBJDB_VALUETYPE_INT64:
  975. (*(int64_t *)object_key->value)--;
  976. break;
  977. case OBJDB_VALUETYPE_UINT64:
  978. (*(uint64_t *)object_key->value)--;
  979. break;
  980. case OBJDB_VALUETYPE_ANY:
  981. /* for backwards compatibilty */
  982. if (object_key->value_len == sizeof(int)) {
  983. (*(int *)object_key->value)--;
  984. }
  985. else {
  986. res = -1;
  987. }
  988. break;
  989. default:
  990. res = -1;
  991. break;
  992. }
  993. if (res == 0) {
  994. /* nasty, not sure why we need to return this typed
  995. * instead of void* */
  996. *value = *(int *)object_key->value;
  997. }
  998. }
  999. else {
  1000. res = -1;
  1001. }
  1002. hdb_handle_put (&object_instance_database, object_handle);
  1003. objdb_rdunlock();
  1004. return (res);
  1005. error_exit:
  1006. objdb_rdunlock();
  1007. return (-1);
  1008. }
  1009. static int object_key_delete (
  1010. hdb_handle_t object_handle,
  1011. const void *key_name,
  1012. size_t key_len)
  1013. {
  1014. unsigned int res;
  1015. int ret = 0;
  1016. struct object_instance *instance;
  1017. struct object_key *object_key = NULL;
  1018. struct list_head *list;
  1019. int found = 0;
  1020. objdb_rdlock();
  1021. res = hdb_handle_get (&object_instance_database,
  1022. object_handle, (void *)&instance);
  1023. if (res != 0) {
  1024. goto error_exit;
  1025. }
  1026. for (list = instance->key_head.next;
  1027. list != &instance->key_head; list = list->next) {
  1028. object_key = list_entry (list, struct object_key, list);
  1029. if ((object_key->key_len == key_len) &&
  1030. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  1031. found = 1;
  1032. break;
  1033. }
  1034. }
  1035. if (found) {
  1036. list_del(&object_key->list);
  1037. free(object_key->key_name);
  1038. free(object_key->value);
  1039. free(object_key);
  1040. }
  1041. else {
  1042. ret = -1;
  1043. errno = ENOENT;
  1044. }
  1045. hdb_handle_put (&object_instance_database, object_handle);
  1046. if (ret == 0) {
  1047. object_key_changed_notification(object_handle, key_name, key_len,
  1048. NULL, 0, OBJECT_KEY_DELETED);
  1049. }
  1050. objdb_rdunlock();
  1051. return (ret);
  1052. error_exit:
  1053. objdb_rdunlock();
  1054. return (-1);
  1055. }
  1056. static int object_key_replace (
  1057. hdb_handle_t object_handle,
  1058. const void *key_name,
  1059. size_t key_len,
  1060. const void *new_value,
  1061. size_t new_value_len)
  1062. {
  1063. unsigned int res;
  1064. int ret = 0;
  1065. struct object_instance *instance;
  1066. struct object_key *object_key = NULL;
  1067. struct list_head *list;
  1068. int found = 0;
  1069. objdb_rdlock();
  1070. res = hdb_handle_get (&object_instance_database,
  1071. object_handle, (void *)&instance);
  1072. if (res != 0) {
  1073. goto error_exit;
  1074. }
  1075. for (list = instance->key_head.next;
  1076. list != &instance->key_head; list = list->next) {
  1077. object_key = list_entry (list, struct object_key, list);
  1078. if ((object_key->key_len == key_len) &&
  1079. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  1080. found = 1;
  1081. break;
  1082. }
  1083. }
  1084. if (found) {
  1085. int i;
  1086. int found_validator = 0;
  1087. /*
  1088. * Do validation check if validation is configured for the parent object
  1089. */
  1090. if (instance->object_key_valid_list_entries) {
  1091. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  1092. if ((key_len ==
  1093. instance->object_key_valid_list[i].key_len) &&
  1094. (memcmp (key_name,
  1095. instance->object_key_valid_list[i].key_name,
  1096. key_len) == 0)) {
  1097. found_validator = 1;
  1098. break;
  1099. }
  1100. }
  1101. /*
  1102. * Item not found in validation list
  1103. */
  1104. if (found_validator == 0) {
  1105. goto error_put;
  1106. } else {
  1107. if (instance->object_key_valid_list[i].validate_callback) {
  1108. res = instance->object_key_valid_list[i].validate_callback (
  1109. key_name, key_len, new_value, new_value_len);
  1110. if (res != 0) {
  1111. goto error_put;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. if (new_value_len != object_key->value_len) {
  1117. void *replacement_value;
  1118. replacement_value = malloc(new_value_len);
  1119. if (!replacement_value)
  1120. goto error_exit;
  1121. free(object_key->value);
  1122. object_key->value = replacement_value;
  1123. }
  1124. memcpy(object_key->value, new_value, new_value_len);
  1125. object_key->value_len = new_value_len;
  1126. }
  1127. else {
  1128. ret = -1;
  1129. errno = ENOENT;
  1130. }
  1131. hdb_handle_put (&object_instance_database, object_handle);
  1132. if (ret == 0) {
  1133. object_key_changed_notification (object_handle, key_name, key_len,
  1134. new_value, new_value_len, OBJECT_KEY_REPLACED);
  1135. }
  1136. objdb_rdunlock();
  1137. return (ret);
  1138. error_put:
  1139. hdb_handle_put (&object_instance_database, object_handle);
  1140. error_exit:
  1141. objdb_rdunlock();
  1142. return (-1);
  1143. }
  1144. static int object_priv_get (
  1145. hdb_handle_t object_handle,
  1146. void **priv)
  1147. {
  1148. int res;
  1149. struct object_instance *object_instance;
  1150. objdb_rdunlock();
  1151. res = hdb_handle_get (&object_instance_database,
  1152. object_handle, (void *)&object_instance);
  1153. if (res != 0) {
  1154. goto error_exit;
  1155. }
  1156. *priv = object_instance->priv;
  1157. hdb_handle_put (&object_instance_database, object_handle);
  1158. objdb_rdunlock();
  1159. return (0);
  1160. error_exit:
  1161. objdb_rdunlock();
  1162. return (-1);
  1163. }
  1164. static int _dump_object(struct object_instance *instance, FILE *file, int depth)
  1165. {
  1166. struct list_head *list;
  1167. int res;
  1168. int i;
  1169. struct object_instance *find_instance = NULL;
  1170. struct object_key *object_key = NULL;
  1171. char stringbuf1[1024];
  1172. char stringbuf2[1024];
  1173. memcpy(stringbuf1, instance->object_name, instance->object_name_len);
  1174. stringbuf1[instance->object_name_len] = '\0';
  1175. for (i=0; i<depth; i++)
  1176. fprintf(file, " ");
  1177. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1178. fprintf(file, "%s {\n", stringbuf1);
  1179. for (list = instance->key_head.next;
  1180. list != &instance->key_head; list = list->next) {
  1181. object_key = list_entry (list, struct object_key,
  1182. list);
  1183. memcpy(stringbuf1, object_key->key_name, object_key->key_len);
  1184. stringbuf1[object_key->key_len] = '\0';
  1185. switch (object_key->value_type) {
  1186. case OBJDB_VALUETYPE_INT16:
  1187. snprintf (stringbuf2, sizeof(int), "%hd",
  1188. *(unsigned int*)object_key->value);
  1189. break;
  1190. case OBJDB_VALUETYPE_UINT16:
  1191. snprintf (stringbuf2, sizeof(int), "%hu",
  1192. *(unsigned int*)object_key->value);
  1193. break;
  1194. case OBJDB_VALUETYPE_INT32:
  1195. snprintf (stringbuf2, sizeof(int), "%d",
  1196. *(int*)object_key->value);
  1197. break;
  1198. case OBJDB_VALUETYPE_UINT32:
  1199. snprintf (stringbuf2, sizeof(int), "%u",
  1200. *(unsigned int*)object_key->value);
  1201. break;
  1202. case OBJDB_VALUETYPE_INT64:
  1203. snprintf (stringbuf2, sizeof(int), "%ld",
  1204. *(long int*)object_key->value);
  1205. break;
  1206. case OBJDB_VALUETYPE_UINT64:
  1207. snprintf (stringbuf2, sizeof(int), "%lu",
  1208. *(unsigned long int*)object_key->value);
  1209. break;
  1210. default:
  1211. case OBJDB_VALUETYPE_STRING:
  1212. case OBJDB_VALUETYPE_ANY:
  1213. memcpy(stringbuf2, object_key->value, object_key->value_len);
  1214. stringbuf2[object_key->value_len] = '\0';
  1215. break;
  1216. }
  1217. for (i=0; i<depth+1; i++)
  1218. fprintf(file, " ");
  1219. fprintf(file, "%s: %s\n", stringbuf1, stringbuf2);
  1220. }
  1221. for (list = instance->child_head.next;
  1222. list != &instance->child_head; list = list->next) {
  1223. find_instance = list_entry (list, struct object_instance,
  1224. child_list);
  1225. res = _dump_object(find_instance, file, depth+1);
  1226. if (res)
  1227. return res;
  1228. }
  1229. for (i=0; i<depth; i++)
  1230. fprintf(file, " ");
  1231. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1232. fprintf(file, "}\n");
  1233. return 0;
  1234. }
  1235. static int object_key_iter_reset(hdb_handle_t object_handle)
  1236. {
  1237. unsigned int res;
  1238. struct object_instance *instance;
  1239. objdb_rdlock();
  1240. res = hdb_handle_get (&object_instance_database,
  1241. object_handle, (void *)&instance);
  1242. if (res != 0) {
  1243. goto error_exit;
  1244. }
  1245. instance->iter_key_list = &instance->key_head;
  1246. hdb_handle_put (&object_instance_database, object_handle);
  1247. objdb_rdunlock();
  1248. return (0);
  1249. error_exit:
  1250. objdb_rdunlock();
  1251. return (-1);
  1252. }
  1253. static int object_key_iter_typed (hdb_handle_t parent_object_handle,
  1254. char **key_name,
  1255. void **value,
  1256. size_t *value_len,
  1257. objdb_value_types_t *type)
  1258. {
  1259. unsigned int res;
  1260. struct object_instance *instance;
  1261. struct object_key *find_key = NULL;
  1262. struct list_head *list;
  1263. unsigned int found = 0;
  1264. objdb_rdlock();
  1265. res = hdb_handle_get (&object_instance_database,
  1266. parent_object_handle, (void *)&instance);
  1267. if (res != 0) {
  1268. goto error_exit;
  1269. }
  1270. res = -ENOENT;
  1271. list = instance->iter_key_list->next;
  1272. if (list != &instance->key_head) {
  1273. find_key = list_entry (list, struct object_key, list);
  1274. found = 1;
  1275. }
  1276. instance->iter_key_list = list;
  1277. if (found) {
  1278. *key_name = find_key->key_name;
  1279. *value = find_key->value;
  1280. *type = find_key->value_type;
  1281. if (value_len)
  1282. *value_len = find_key->value_len;
  1283. res = 0;
  1284. }
  1285. else {
  1286. res = -1;
  1287. }
  1288. hdb_handle_put (&object_instance_database, parent_object_handle);
  1289. objdb_rdunlock();
  1290. return (res);
  1291. error_exit:
  1292. objdb_rdunlock();
  1293. return (-1);
  1294. }
  1295. static int object_key_iter(hdb_handle_t parent_object_handle,
  1296. void **key_name,
  1297. size_t *key_len,
  1298. void **value,
  1299. size_t *value_len)
  1300. {
  1301. objdb_value_types_t t;
  1302. int ret;
  1303. char *str;
  1304. ret = object_key_iter_typed (parent_object_handle,
  1305. (char**)key_name, value, value_len, &t);
  1306. str = *key_name;
  1307. *key_len = strlen(str);
  1308. return ret;
  1309. }
  1310. static int object_key_iter_from(hdb_handle_t parent_object_handle,
  1311. hdb_handle_t start_pos,
  1312. void **key_name,
  1313. size_t *key_len,
  1314. void **value,
  1315. size_t *value_len)
  1316. {
  1317. unsigned int pos = 0;
  1318. unsigned int res;
  1319. struct object_instance *instance;
  1320. struct object_key *find_key = NULL;
  1321. struct list_head *list;
  1322. unsigned int found = 0;
  1323. objdb_rdlock();
  1324. res = hdb_handle_get (&object_instance_database,
  1325. parent_object_handle, (void *)&instance);
  1326. if (res != 0) {
  1327. goto error_exit;
  1328. }
  1329. res = -ENOENT;
  1330. for (list = instance->key_head.next;
  1331. list != &instance->key_head; list = list->next) {
  1332. find_key = list_entry (list, struct object_key, list);
  1333. if (pos++ == start_pos) {
  1334. found = 1;
  1335. break;
  1336. }
  1337. }
  1338. if (found) {
  1339. *key_name = find_key->key_name;
  1340. if (key_len)
  1341. *key_len = find_key->key_len;
  1342. *value = find_key->value;
  1343. if (value_len)
  1344. *value_len = find_key->value_len;
  1345. res = 0;
  1346. }
  1347. else {
  1348. res = -1;
  1349. }
  1350. hdb_handle_put (&object_instance_database, parent_object_handle);
  1351. objdb_rdunlock();
  1352. return (res);
  1353. error_exit:
  1354. objdb_rdunlock();
  1355. return (-1);
  1356. }
  1357. static int object_parent_get(hdb_handle_t object_handle,
  1358. hdb_handle_t *parent_handle)
  1359. {
  1360. struct object_instance *instance;
  1361. unsigned int res;
  1362. objdb_rdlock();
  1363. res = hdb_handle_get (&object_instance_database,
  1364. object_handle, (void *)&instance);
  1365. if (res != 0) {
  1366. objdb_rdunlock();
  1367. return (res);
  1368. }
  1369. if (object_handle == OBJECT_PARENT_HANDLE)
  1370. *parent_handle = 0;
  1371. else
  1372. *parent_handle = instance->parent_handle;
  1373. hdb_handle_put (&object_instance_database, object_handle);
  1374. objdb_rdunlock();
  1375. return (0);
  1376. }
  1377. static int object_name_get(hdb_handle_t object_handle,
  1378. char *object_name,
  1379. size_t *object_name_len)
  1380. {
  1381. struct object_instance *instance;
  1382. unsigned int res;
  1383. objdb_rdlock();
  1384. res = hdb_handle_get (&object_instance_database,
  1385. object_handle, (void *)&instance);
  1386. if (res != 0) {
  1387. objdb_rdunlock();
  1388. return (res);
  1389. }
  1390. memcpy(object_name, instance->object_name, instance->object_name_len);
  1391. *object_name_len = instance->object_name_len;
  1392. hdb_handle_put (&object_instance_database, object_handle);
  1393. objdb_rdunlock();
  1394. return (0);
  1395. }
  1396. static int object_track_start(hdb_handle_t object_handle,
  1397. object_track_depth_t depth,
  1398. object_key_change_notify_fn_t key_change_notify_fn,
  1399. object_create_notify_fn_t object_create_notify_fn,
  1400. object_destroy_notify_fn_t object_destroy_notify_fn,
  1401. object_reload_notify_fn_t object_reload_notify_fn,
  1402. void * priv_data_pt)
  1403. {
  1404. struct object_instance *instance;
  1405. unsigned int res;
  1406. struct object_tracker * tracker_pt;
  1407. res = hdb_handle_get (&object_instance_database,
  1408. object_handle, (void *)&instance);
  1409. if (res != 0) {
  1410. return (res);
  1411. }
  1412. tracker_pt = malloc(sizeof(struct object_tracker));
  1413. tracker_pt->depth = depth;
  1414. tracker_pt->object_handle = object_handle;
  1415. tracker_pt->key_change_notify_fn = key_change_notify_fn;
  1416. tracker_pt->object_create_notify_fn = object_create_notify_fn;
  1417. tracker_pt->object_destroy_notify_fn = object_destroy_notify_fn;
  1418. tracker_pt->object_reload_notify_fn = object_reload_notify_fn;
  1419. tracker_pt->data_pt = priv_data_pt;
  1420. list_init(&tracker_pt->object_list);
  1421. list_init(&tracker_pt->tracker_list);
  1422. list_add(&tracker_pt->object_list, &instance->track_head);
  1423. list_add(&tracker_pt->tracker_list, &objdb_trackers_head);
  1424. hdb_handle_put (&object_instance_database, object_handle);
  1425. return (res);
  1426. }
  1427. static void object_track_stop(object_key_change_notify_fn_t key_change_notify_fn,
  1428. object_create_notify_fn_t object_create_notify_fn,
  1429. object_destroy_notify_fn_t object_destroy_notify_fn,
  1430. object_reload_notify_fn_t object_reload_notify_fn,
  1431. void * priv_data_pt)
  1432. {
  1433. struct object_instance *instance;
  1434. struct object_tracker * tracker_pt = NULL;
  1435. struct object_tracker * obj_tracker_pt = NULL;
  1436. struct list_head *list, *tmp_list;
  1437. struct list_head *obj_list, *tmp_obj_list;
  1438. unsigned int res;
  1439. /* go through the global list and find all the trackers to stop */
  1440. for (list = objdb_trackers_head.next, tmp_list = list->next;
  1441. list != &objdb_trackers_head; list = tmp_list, tmp_list = tmp_list->next) {
  1442. tracker_pt = list_entry (list, struct object_tracker, tracker_list);
  1443. if (tracker_pt && (tracker_pt->data_pt == priv_data_pt) &&
  1444. (tracker_pt->object_create_notify_fn == object_create_notify_fn) &&
  1445. (tracker_pt->object_destroy_notify_fn == object_destroy_notify_fn) &&
  1446. (tracker_pt->object_reload_notify_fn == object_reload_notify_fn) &&
  1447. (tracker_pt->key_change_notify_fn == key_change_notify_fn)) {
  1448. /* get the object & take this tracker off of it's list. */
  1449. res = hdb_handle_get (&object_instance_database,
  1450. tracker_pt->object_handle, (void *)&instance);
  1451. if (res != 0) continue;
  1452. for (obj_list = instance->track_head.next, tmp_obj_list = obj_list->next;
  1453. obj_list != &instance->track_head; obj_list = tmp_obj_list, tmp_obj_list = tmp_obj_list->next) {
  1454. obj_tracker_pt = list_entry (obj_list, struct object_tracker, object_list);
  1455. if (obj_tracker_pt == tracker_pt) {
  1456. /* this is the tracker we are after. */
  1457. list_del(obj_list);
  1458. }
  1459. }
  1460. hdb_handle_put (&object_instance_database, tracker_pt->object_handle);
  1461. /* remove the tracker off of the global list */
  1462. list_del(list);
  1463. free(tracker_pt);
  1464. }
  1465. }
  1466. }
  1467. static int object_dump(hdb_handle_t object_handle,
  1468. FILE *file)
  1469. {
  1470. struct object_instance *instance;
  1471. unsigned int res;
  1472. objdb_rdlock();
  1473. res = hdb_handle_get (&object_instance_database,
  1474. object_handle, (void *)&instance);
  1475. if (res != 0) {
  1476. objdb_rdunlock();
  1477. return (res);
  1478. }
  1479. res = _dump_object(instance, file, -1);
  1480. hdb_handle_put (&object_instance_database, object_handle);
  1481. objdb_rdunlock();
  1482. return (res);
  1483. }
  1484. static int object_write_config(const char **error_string)
  1485. {
  1486. struct config_iface_ver0 **modules;
  1487. int num_modules;
  1488. int i;
  1489. int res;
  1490. main_get_config_modules(&modules, &num_modules);
  1491. objdb_wrlock();
  1492. for (i=0; i<num_modules; i++) {
  1493. if (modules[i]->config_writeconfig) {
  1494. res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  1495. if (res) {
  1496. objdb_wrunlock();
  1497. return res;
  1498. }
  1499. }
  1500. }
  1501. objdb_wrunlock();
  1502. return 0;
  1503. }
  1504. static int object_reload_config(int flush, const char **error_string)
  1505. {
  1506. struct config_iface_ver0 **modules;
  1507. int num_modules;
  1508. int i;
  1509. int res;
  1510. main_get_config_modules(&modules, &num_modules);
  1511. object_reload_notification(OBJDB_RELOAD_NOTIFY_START, flush);
  1512. objdb_wrlock();
  1513. for (i=0; i<num_modules; i++) {
  1514. if (modules[i]->config_reloadconfig) {
  1515. res = modules[i]->config_reloadconfig(&objdb_iface, flush, error_string);
  1516. if (res) {
  1517. object_reload_notification(OBJDB_RELOAD_NOTIFY_FAILED, flush);
  1518. objdb_wrunlock();
  1519. return res;
  1520. }
  1521. }
  1522. }
  1523. objdb_wrunlock();
  1524. object_reload_notification(OBJDB_RELOAD_NOTIFY_END, flush);
  1525. return 0;
  1526. }
  1527. struct objdb_iface_ver0 objdb_iface = {
  1528. .objdb_init = objdb_init,
  1529. .object_create = object_create,
  1530. .object_priv_set = object_priv_set,
  1531. .object_key_create = object_key_create,
  1532. .object_key_delete = object_key_delete,
  1533. .object_key_replace = object_key_replace,
  1534. .object_destroy = object_destroy,
  1535. .object_valid_set = object_valid_set,
  1536. .object_key_valid_set = object_key_valid_set,
  1537. .object_find_create = object_find_create,
  1538. .object_find_next = object_find_next,
  1539. .object_find_destroy = object_find_destroy,
  1540. .object_key_get = object_key_get,
  1541. .object_key_iter_reset = object_key_iter_reset,
  1542. .object_key_iter = object_key_iter,
  1543. .object_key_iter_from = object_key_iter_from,
  1544. .object_priv_get = object_priv_get,
  1545. .object_parent_get = object_parent_get,
  1546. .object_name_get = object_name_get,
  1547. .object_track_start = object_track_start,
  1548. .object_track_stop = object_track_stop,
  1549. .object_dump = object_dump,
  1550. .object_write_config = object_write_config,
  1551. .object_reload_config = object_reload_config,
  1552. .object_key_increment = object_key_increment,
  1553. .object_key_decrement = object_key_decrement,
  1554. .object_key_create_typed = object_key_create_typed,
  1555. .object_key_get_typed = object_key_get_typed,
  1556. .object_key_iter_typed = object_key_iter_typed,
  1557. };
  1558. struct lcr_iface objdb_iface_ver0[1] = {
  1559. {
  1560. .name = "objdb",
  1561. .version = 0,
  1562. .versions_replace = 0,
  1563. .versions_replace_count = 0,
  1564. .dependencies = 0,
  1565. .dependency_count = 0,
  1566. .constructor = NULL,
  1567. .destructor = NULL,
  1568. .interfaces = NULL,
  1569. }
  1570. };
  1571. struct lcr_comp objdb_comp_ver0 = {
  1572. .iface_count = 1,
  1573. .ifaces = objdb_iface_ver0
  1574. };
  1575. #ifdef COROSYNC_SOLARIS
  1576. void corosync_lcr_component_register (void);
  1577. void corosync_lcr_component_register (void) {
  1578. #else
  1579. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  1580. #endif
  1581. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  1582. lcr_component_register (&objdb_comp_ver0);
  1583. }