objdb.c 39 KB

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