objdb.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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_delete (
  643. unsigned int object_handle,
  644. void *key_name,
  645. int key_len,
  646. void *value,
  647. int value_len)
  648. {
  649. unsigned int res;
  650. int ret = 0;
  651. struct object_instance *instance;
  652. struct object_key *object_key = NULL;
  653. struct list_head *list;
  654. int found = 0;
  655. res = hdb_handle_get (&object_instance_database,
  656. object_handle, (void *)&instance);
  657. if (res != 0) {
  658. goto error_exit;
  659. }
  660. for (list = instance->key_head.next;
  661. list != &instance->key_head; list = list->next) {
  662. object_key = list_entry (list, struct object_key, list);
  663. if ((object_key->key_len == key_len) &&
  664. (memcmp (object_key->key_name, key_name, key_len) == 0) &&
  665. (value == NULL ||
  666. (object_key->value_len == value_len &&
  667. (memcmp (object_key->value, value, value_len) == 0)))) {
  668. found = 1;
  669. break;
  670. }
  671. }
  672. if (found) {
  673. list_del(&object_key->list);
  674. free(object_key->key_name);
  675. free(object_key->value);
  676. free(object_key);
  677. }
  678. else {
  679. ret = -1;
  680. errno = ENOENT;
  681. }
  682. hdb_handle_put (&object_instance_database, object_handle);
  683. if (ret == 0)
  684. object_key_changed_notification(object_handle, key_name, key_len,
  685. value, value_len, OBJECT_KEY_DELETED);
  686. return (ret);
  687. error_exit:
  688. return (-1);
  689. }
  690. static int object_key_replace (
  691. unsigned int object_handle,
  692. void *key_name,
  693. int key_len,
  694. void *old_value,
  695. int old_value_len,
  696. void *new_value,
  697. int new_value_len)
  698. {
  699. unsigned int res;
  700. int ret = 0;
  701. struct object_instance *instance;
  702. struct object_key *object_key = NULL;
  703. struct list_head *list;
  704. int found = 0;
  705. res = hdb_handle_get (&object_instance_database,
  706. object_handle, (void *)&instance);
  707. if (res != 0) {
  708. goto error_exit;
  709. }
  710. for (list = instance->key_head.next;
  711. list != &instance->key_head; list = list->next) {
  712. object_key = list_entry (list, struct object_key, list);
  713. if ((object_key->key_len == key_len) &&
  714. (memcmp (object_key->key_name, key_name, key_len) == 0) &&
  715. (old_value == NULL ||
  716. (object_key->value_len == old_value_len &&
  717. (memcmp (object_key->value, old_value, old_value_len) == 0)))) {
  718. found = 1;
  719. break;
  720. }
  721. }
  722. if (found) {
  723. int i;
  724. /*
  725. * Do validation check if validation is configured for the parent object
  726. */
  727. if (instance->object_key_valid_list_entries) {
  728. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  729. if ((key_len ==
  730. instance->object_key_valid_list[i].key_len) &&
  731. (memcmp (key_name,
  732. instance->object_key_valid_list[i].key_name,
  733. key_len) == 0)) {
  734. found = 1;
  735. break;
  736. }
  737. }
  738. /*
  739. * Item not found in validation list
  740. */
  741. if (found == 0) {
  742. goto error_put;
  743. } else {
  744. if (instance->object_key_valid_list[i].validate_callback) {
  745. res = instance->object_key_valid_list[i].validate_callback (
  746. key_name, key_len, new_value, new_value_len);
  747. if (res != 0) {
  748. goto error_put;
  749. }
  750. }
  751. }
  752. }
  753. if (new_value_len <= object_key->value_len) {
  754. void *replacement_value;
  755. replacement_value = malloc(new_value_len);
  756. if (!replacement_value)
  757. goto error_exit;
  758. free(object_key->value);
  759. object_key->value = replacement_value;
  760. }
  761. memcpy(object_key->value, new_value, new_value_len);
  762. object_key->value_len = new_value_len;
  763. }
  764. else {
  765. ret = -1;
  766. errno = ENOENT;
  767. }
  768. hdb_handle_put (&object_instance_database, object_handle);
  769. if (ret == 0)
  770. object_key_changed_notification(object_handle, key_name, key_len,
  771. new_value, new_value_len, OBJECT_KEY_REPLACED);
  772. return (ret);
  773. error_put:
  774. hdb_handle_put (&object_instance_database, object_handle);
  775. error_exit:
  776. return (-1);
  777. }
  778. static int object_priv_get (
  779. unsigned int object_handle,
  780. void **priv)
  781. {
  782. int res;
  783. struct object_instance *object_instance;
  784. res = hdb_handle_get (&object_instance_database,
  785. object_handle, (void *)&object_instance);
  786. if (res != 0) {
  787. goto error_exit;
  788. }
  789. *priv = object_instance->priv;
  790. hdb_handle_put (&object_instance_database, object_handle);
  791. return (0);
  792. error_exit:
  793. return (-1);
  794. }
  795. static int _dump_object(struct object_instance *instance, FILE *file, int depth)
  796. {
  797. struct list_head *list;
  798. int res;
  799. int i;
  800. struct object_instance *find_instance = NULL;
  801. struct object_key *object_key = NULL;
  802. char stringbuf1[1024];
  803. char stringbuf2[1024];
  804. memcpy(stringbuf1, instance->object_name, instance->object_name_len);
  805. stringbuf1[instance->object_name_len] = '\0';
  806. for (i=0; i<depth; i++)
  807. fprintf(file, " ");
  808. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  809. fprintf(file, "%s {\n", stringbuf1);
  810. for (list = instance->key_head.next;
  811. list != &instance->key_head; list = list->next) {
  812. object_key = list_entry (list, struct object_key,
  813. list);
  814. memcpy(stringbuf1, object_key->key_name, object_key->key_len);
  815. stringbuf1[object_key->key_len] = '\0';
  816. memcpy(stringbuf2, object_key->value, object_key->value_len);
  817. stringbuf2[object_key->value_len] = '\0';
  818. for (i=0; i<depth+1; i++)
  819. fprintf(file, " ");
  820. fprintf(file, "%s: %s\n", stringbuf1, stringbuf2);
  821. }
  822. for (list = instance->child_head.next;
  823. list != &instance->child_head; list = list->next) {
  824. find_instance = list_entry (list, struct object_instance,
  825. child_list);
  826. res = _dump_object(find_instance, file, depth+1);
  827. if (res)
  828. return res;
  829. }
  830. for (i=0; i<depth; i++)
  831. fprintf(file, " ");
  832. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  833. fprintf(file, "}\n");
  834. return 0;
  835. }
  836. static int object_key_iter(unsigned int parent_object_handle,
  837. void **key_name,
  838. int *key_len,
  839. void **value,
  840. int *value_len)
  841. {
  842. unsigned int res;
  843. struct object_instance *instance;
  844. struct object_key *find_key = NULL;
  845. struct list_head *list;
  846. unsigned int found = 0;
  847. res = hdb_handle_get (&object_instance_database,
  848. parent_object_handle, (void *)&instance);
  849. if (res != 0) {
  850. goto error_exit;
  851. }
  852. res = -ENOENT;
  853. list = instance->iter_key_list->next;
  854. if (list != &instance->key_head) {
  855. find_key = list_entry (list, struct object_key, list);
  856. found = 1;
  857. }
  858. instance->iter_key_list = list;
  859. if (found) {
  860. *key_name = find_key->key_name;
  861. if (key_len)
  862. *key_len = find_key->key_len;
  863. *value = find_key->value;
  864. if (value_len)
  865. *value_len = find_key->value_len;
  866. res = 0;
  867. }
  868. else {
  869. res = -1;
  870. }
  871. hdb_handle_put (&object_instance_database, parent_object_handle);
  872. return (res);
  873. error_exit:
  874. return (-1);
  875. }
  876. static int object_key_iter_from(unsigned int parent_object_handle,
  877. unsigned int start_pos,
  878. void **key_name,
  879. int *key_len,
  880. void **value,
  881. int *value_len)
  882. {
  883. unsigned int pos = 0;
  884. unsigned int res;
  885. struct object_instance *instance;
  886. struct object_key *find_key = NULL;
  887. struct list_head *list;
  888. unsigned int found = 0;
  889. res = hdb_handle_get (&object_instance_database,
  890. parent_object_handle, (void *)&instance);
  891. if (res != 0) {
  892. goto error_exit;
  893. }
  894. res = -ENOENT;
  895. for (list = instance->key_head.next;
  896. list != &instance->key_head; list = list->next) {
  897. find_key = list_entry (list, struct object_key, list);
  898. if (pos++ == start_pos) {
  899. found = 1;
  900. break;
  901. }
  902. }
  903. if (found) {
  904. *key_name = find_key->key_name;
  905. if (key_len)
  906. *key_len = find_key->key_len;
  907. *value = find_key->value;
  908. if (value_len)
  909. *value_len = find_key->value_len;
  910. res = 0;
  911. }
  912. else {
  913. res = -1;
  914. }
  915. hdb_handle_put (&object_instance_database, parent_object_handle);
  916. return (res);
  917. error_exit:
  918. return (-1);
  919. }
  920. static int object_parent_get(unsigned int object_handle,
  921. unsigned int *parent_handle)
  922. {
  923. struct object_instance *instance;
  924. unsigned int res;
  925. res = hdb_handle_get (&object_instance_database,
  926. object_handle, (void *)&instance);
  927. if (res != 0) {
  928. return (res);
  929. }
  930. if (object_handle == OBJECT_PARENT_HANDLE)
  931. *parent_handle = 0;
  932. else
  933. *parent_handle = instance->parent_handle;
  934. hdb_handle_put (&object_instance_database, object_handle);
  935. return (0);
  936. }
  937. static int object_name_get(unsigned int object_handle,
  938. char *object_name,
  939. int *object_name_len)
  940. {
  941. struct object_instance *instance;
  942. unsigned int res;
  943. res = hdb_handle_get (&object_instance_database,
  944. object_handle, (void *)&instance);
  945. if (res != 0) {
  946. return (res);
  947. }
  948. memcpy(object_name, instance->object_name, instance->object_name_len);
  949. *object_name_len = instance->object_name_len;
  950. hdb_handle_put (&object_instance_database, object_handle);
  951. return (0);
  952. }
  953. static int object_track_start(unsigned int object_handle,
  954. object_track_depth_t depth,
  955. object_key_change_notify_fn_t key_change_notify_fn,
  956. object_create_notify_fn_t object_create_notify_fn,
  957. object_destroy_notify_fn_t object_destroy_notify_fn,
  958. void * priv_data_pt)
  959. {
  960. struct object_instance *instance;
  961. unsigned int res;
  962. struct object_tracker * tracker_pt;
  963. res = hdb_handle_get (&object_instance_database,
  964. object_handle, (void *)&instance);
  965. if (res != 0) {
  966. return (res);
  967. }
  968. tracker_pt = malloc(sizeof(struct object_tracker));
  969. tracker_pt->depth = depth;
  970. tracker_pt->object_handle = object_handle;
  971. tracker_pt->key_change_notify_fn = key_change_notify_fn;
  972. tracker_pt->object_create_notify_fn = object_create_notify_fn;
  973. tracker_pt->object_destroy_notify_fn = object_destroy_notify_fn;
  974. tracker_pt->data_pt = priv_data_pt;
  975. list_init(&tracker_pt->object_list);
  976. list_init(&tracker_pt->tracker_list);
  977. list_add(&tracker_pt->object_list, &instance->track_head);
  978. list_add(&tracker_pt->tracker_list, &objdb_trackers_head);
  979. hdb_handle_put (&object_instance_database, object_handle);
  980. return (res);
  981. }
  982. static void object_track_stop(object_key_change_notify_fn_t key_change_notify_fn,
  983. object_create_notify_fn_t object_create_notify_fn,
  984. object_destroy_notify_fn_t object_destroy_notify_fn,
  985. void * priv_data_pt)
  986. {
  987. struct object_instance *instance;
  988. struct object_tracker * tracker_pt = NULL;
  989. struct object_tracker * obj_tracker_pt = NULL;
  990. struct list_head *list, *tmp_list;
  991. struct list_head *obj_list, *tmp_obj_list;
  992. unsigned int res;
  993. /* go through the global list and find all the trackers to stop */
  994. for (list = objdb_trackers_head.next, tmp_list = list->next;
  995. list != &objdb_trackers_head; list = tmp_list, tmp_list = tmp_list->next) {
  996. tracker_pt = list_entry (list, struct object_tracker, tracker_list);
  997. if (tracker_pt && (tracker_pt->data_pt == priv_data_pt) &&
  998. (tracker_pt->object_create_notify_fn == object_create_notify_fn) &&
  999. (tracker_pt->object_destroy_notify_fn == object_destroy_notify_fn) &&
  1000. (tracker_pt->key_change_notify_fn == key_change_notify_fn)) {
  1001. /* get the object & take this tracker off of it's list. */
  1002. res = hdb_handle_get (&object_instance_database,
  1003. tracker_pt->object_handle, (void *)&instance);
  1004. if (res != 0) continue;
  1005. for (obj_list = instance->track_head.next, tmp_obj_list = obj_list->next;
  1006. obj_list != &instance->track_head; obj_list = tmp_obj_list, tmp_obj_list = tmp_obj_list->next) {
  1007. obj_tracker_pt = list_entry (obj_list, struct object_tracker, object_list);
  1008. if (obj_tracker_pt == tracker_pt) {
  1009. /* this is the tracker we are after. */
  1010. list_del(obj_list);
  1011. }
  1012. }
  1013. hdb_handle_put (&object_instance_database, tracker_pt->object_handle);
  1014. /* remove the tracker off of the global list */
  1015. list_del(list);
  1016. free(tracker_pt);
  1017. }
  1018. }
  1019. }
  1020. static int object_dump(unsigned int object_handle,
  1021. FILE *file)
  1022. {
  1023. struct object_instance *instance;
  1024. unsigned int res;
  1025. res = hdb_handle_get (&object_instance_database,
  1026. object_handle, (void *)&instance);
  1027. if (res != 0) {
  1028. return (res);
  1029. }
  1030. res = _dump_object(instance, file, -1);
  1031. hdb_handle_put (&object_instance_database, object_handle);
  1032. return (res);
  1033. }
  1034. static int object_write_config(char **error_string)
  1035. {
  1036. struct config_iface_ver0 **modules;
  1037. int num_modules;
  1038. int i;
  1039. int res;
  1040. main_get_config_modules(&modules, &num_modules);
  1041. for (i=0; i<num_modules; i++) {
  1042. if (modules[i]->config_writeconfig) {
  1043. res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  1044. if (res)
  1045. return res;
  1046. }
  1047. }
  1048. return 0;
  1049. }
  1050. static int object_reload_config(int flush, char **error_string)
  1051. {
  1052. struct config_iface_ver0 **modules;
  1053. int num_modules;
  1054. int i;
  1055. int res;
  1056. main_get_config_modules(&modules, &num_modules);
  1057. for (i=0; i<num_modules; i++) {
  1058. if (modules[i]->config_reloadconfig) {
  1059. res = modules[i]->config_reloadconfig(&objdb_iface, flush, error_string);
  1060. if (res)
  1061. return res;
  1062. }
  1063. }
  1064. return 0;
  1065. }
  1066. struct objdb_iface_ver0 objdb_iface = {
  1067. .objdb_init = objdb_init,
  1068. .object_create = object_create,
  1069. .object_priv_set = object_priv_set,
  1070. .object_key_create = object_key_create,
  1071. .object_key_delete = object_key_delete,
  1072. .object_key_replace = object_key_replace,
  1073. .object_destroy = object_destroy,
  1074. .object_valid_set = object_valid_set,
  1075. .object_key_valid_set = object_key_valid_set,
  1076. .object_find_create = object_find_create,
  1077. .object_find_next = object_find_next,
  1078. .object_find_destroy = object_find_destroy,
  1079. .object_key_get = object_key_get,
  1080. .object_key_iter = object_key_iter,
  1081. .object_key_iter_from = object_key_iter_from,
  1082. .object_priv_get = object_priv_get,
  1083. .object_parent_get = object_parent_get,
  1084. .object_name_get = object_name_get,
  1085. .object_track_start = object_track_start,
  1086. .object_track_stop = object_track_stop,
  1087. .object_dump = object_dump,
  1088. .object_write_config = object_write_config,
  1089. .object_reload_config = object_reload_config,
  1090. };
  1091. struct lcr_iface objdb_iface_ver0[1] = {
  1092. {
  1093. .name = "objdb",
  1094. .version = 0,
  1095. .versions_replace = 0,
  1096. .versions_replace_count = 0,
  1097. .dependencies = 0,
  1098. .dependency_count = 0,
  1099. .constructor = NULL,
  1100. .destructor = NULL,
  1101. .interfaces = NULL,
  1102. }
  1103. };
  1104. struct lcr_comp objdb_comp_ver0 = {
  1105. .iface_count = 1,
  1106. .ifaces = objdb_iface_ver0
  1107. };
  1108. __attribute__ ((constructor)) static void objdb_comp_register (void) {
  1109. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  1110. lcr_component_register (&objdb_comp_ver0);
  1111. }