objdb.c 39 KB

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