objdb.c 39 KB

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