objdb.c 35 KB

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