objdb.c 39 KB

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