objdb.c 40 KB

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