objdb.c 42 KB

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