objdb.c 39 KB

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