4
0

objdb.c 39 KB

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