objdb.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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_reset(unsigned int object_handle)
  911. {
  912. unsigned int res;
  913. struct object_instance *instance;
  914. res = hdb_handle_get (&object_instance_database,
  915. object_handle, (void *)&instance);
  916. if (res != 0) {
  917. goto error_exit;
  918. }
  919. instance->iter_key_list = &instance->key_head;
  920. hdb_handle_put (&object_instance_database, object_handle);
  921. return (0);
  922. error_exit:
  923. return (-1);
  924. }
  925. static int object_key_iter(unsigned int parent_object_handle,
  926. void **key_name,
  927. int *key_len,
  928. void **value,
  929. int *value_len)
  930. {
  931. unsigned int res;
  932. struct object_instance *instance;
  933. struct object_key *find_key = NULL;
  934. struct list_head *list;
  935. unsigned int found = 0;
  936. res = hdb_handle_get (&object_instance_database,
  937. parent_object_handle, (void *)&instance);
  938. if (res != 0) {
  939. goto error_exit;
  940. }
  941. res = -ENOENT;
  942. list = instance->iter_key_list->next;
  943. if (list != &instance->key_head) {
  944. find_key = list_entry (list, struct object_key, list);
  945. found = 1;
  946. }
  947. instance->iter_key_list = list;
  948. if (found) {
  949. *key_name = find_key->key_name;
  950. if (key_len)
  951. *key_len = find_key->key_len;
  952. *value = find_key->value;
  953. if (value_len)
  954. *value_len = find_key->value_len;
  955. res = 0;
  956. }
  957. else {
  958. res = -1;
  959. }
  960. hdb_handle_put (&object_instance_database, parent_object_handle);
  961. return (res);
  962. error_exit:
  963. return (-1);
  964. }
  965. static int object_key_iter_from(unsigned int parent_object_handle,
  966. unsigned int start_pos,
  967. void **key_name,
  968. int *key_len,
  969. void **value,
  970. int *value_len)
  971. {
  972. unsigned int pos = 0;
  973. unsigned int res;
  974. struct object_instance *instance;
  975. struct object_key *find_key = NULL;
  976. struct list_head *list;
  977. unsigned int found = 0;
  978. res = hdb_handle_get (&object_instance_database,
  979. parent_object_handle, (void *)&instance);
  980. if (res != 0) {
  981. goto error_exit;
  982. }
  983. res = -ENOENT;
  984. for (list = instance->key_head.next;
  985. list != &instance->key_head; list = list->next) {
  986. find_key = list_entry (list, struct object_key, list);
  987. if (pos++ == start_pos) {
  988. found = 1;
  989. break;
  990. }
  991. }
  992. if (found) {
  993. *key_name = find_key->key_name;
  994. if (key_len)
  995. *key_len = find_key->key_len;
  996. *value = find_key->value;
  997. if (value_len)
  998. *value_len = find_key->value_len;
  999. res = 0;
  1000. }
  1001. else {
  1002. res = -1;
  1003. }
  1004. hdb_handle_put (&object_instance_database, parent_object_handle);
  1005. return (res);
  1006. error_exit:
  1007. return (-1);
  1008. }
  1009. static int object_parent_get(unsigned int object_handle,
  1010. unsigned int *parent_handle)
  1011. {
  1012. struct object_instance *instance;
  1013. unsigned int res;
  1014. res = hdb_handle_get (&object_instance_database,
  1015. object_handle, (void *)&instance);
  1016. if (res != 0) {
  1017. return (res);
  1018. }
  1019. if (object_handle == OBJECT_PARENT_HANDLE)
  1020. *parent_handle = 0;
  1021. else
  1022. *parent_handle = instance->parent_handle;
  1023. hdb_handle_put (&object_instance_database, object_handle);
  1024. return (0);
  1025. }
  1026. static int object_name_get(unsigned int object_handle,
  1027. char *object_name,
  1028. int *object_name_len)
  1029. {
  1030. struct object_instance *instance;
  1031. unsigned int res;
  1032. res = hdb_handle_get (&object_instance_database,
  1033. object_handle, (void *)&instance);
  1034. if (res != 0) {
  1035. return (res);
  1036. }
  1037. memcpy(object_name, instance->object_name, instance->object_name_len);
  1038. *object_name_len = instance->object_name_len;
  1039. hdb_handle_put (&object_instance_database, object_handle);
  1040. return (0);
  1041. }
  1042. static int object_track_start(unsigned int object_handle,
  1043. object_track_depth_t depth,
  1044. object_key_change_notify_fn_t key_change_notify_fn,
  1045. object_create_notify_fn_t object_create_notify_fn,
  1046. object_destroy_notify_fn_t object_destroy_notify_fn,
  1047. void * priv_data_pt)
  1048. {
  1049. struct object_instance *instance;
  1050. unsigned int res;
  1051. struct object_tracker * tracker_pt;
  1052. res = hdb_handle_get (&object_instance_database,
  1053. object_handle, (void *)&instance);
  1054. if (res != 0) {
  1055. return (res);
  1056. }
  1057. tracker_pt = malloc(sizeof(struct object_tracker));
  1058. tracker_pt->depth = depth;
  1059. tracker_pt->object_handle = object_handle;
  1060. tracker_pt->key_change_notify_fn = key_change_notify_fn;
  1061. tracker_pt->object_create_notify_fn = object_create_notify_fn;
  1062. tracker_pt->object_destroy_notify_fn = object_destroy_notify_fn;
  1063. tracker_pt->data_pt = priv_data_pt;
  1064. list_init(&tracker_pt->object_list);
  1065. list_init(&tracker_pt->tracker_list);
  1066. list_add(&tracker_pt->object_list, &instance->track_head);
  1067. list_add(&tracker_pt->tracker_list, &objdb_trackers_head);
  1068. hdb_handle_put (&object_instance_database, object_handle);
  1069. return (res);
  1070. }
  1071. static void object_track_stop(object_key_change_notify_fn_t key_change_notify_fn,
  1072. object_create_notify_fn_t object_create_notify_fn,
  1073. object_destroy_notify_fn_t object_destroy_notify_fn,
  1074. void * priv_data_pt)
  1075. {
  1076. struct object_instance *instance;
  1077. struct object_tracker * tracker_pt = NULL;
  1078. struct object_tracker * obj_tracker_pt = NULL;
  1079. struct list_head *list, *tmp_list;
  1080. struct list_head *obj_list, *tmp_obj_list;
  1081. unsigned int res;
  1082. /* go through the global list and find all the trackers to stop */
  1083. for (list = objdb_trackers_head.next, tmp_list = list->next;
  1084. list != &objdb_trackers_head; list = tmp_list, tmp_list = tmp_list->next) {
  1085. tracker_pt = list_entry (list, struct object_tracker, tracker_list);
  1086. if (tracker_pt && (tracker_pt->data_pt == priv_data_pt) &&
  1087. (tracker_pt->object_create_notify_fn == object_create_notify_fn) &&
  1088. (tracker_pt->object_destroy_notify_fn == object_destroy_notify_fn) &&
  1089. (tracker_pt->key_change_notify_fn == key_change_notify_fn)) {
  1090. /* get the object & take this tracker off of it's list. */
  1091. res = hdb_handle_get (&object_instance_database,
  1092. tracker_pt->object_handle, (void *)&instance);
  1093. if (res != 0) continue;
  1094. for (obj_list = instance->track_head.next, tmp_obj_list = obj_list->next;
  1095. obj_list != &instance->track_head; obj_list = tmp_obj_list, tmp_obj_list = tmp_obj_list->next) {
  1096. obj_tracker_pt = list_entry (obj_list, struct object_tracker, object_list);
  1097. if (obj_tracker_pt == tracker_pt) {
  1098. /* this is the tracker we are after. */
  1099. list_del(obj_list);
  1100. }
  1101. }
  1102. hdb_handle_put (&object_instance_database, tracker_pt->object_handle);
  1103. /* remove the tracker off of the global list */
  1104. list_del(list);
  1105. free(tracker_pt);
  1106. }
  1107. }
  1108. }
  1109. static int object_dump(unsigned int object_handle,
  1110. FILE *file)
  1111. {
  1112. struct object_instance *instance;
  1113. unsigned int res;
  1114. res = hdb_handle_get (&object_instance_database,
  1115. object_handle, (void *)&instance);
  1116. if (res != 0) {
  1117. return (res);
  1118. }
  1119. res = _dump_object(instance, file, -1);
  1120. hdb_handle_put (&object_instance_database, object_handle);
  1121. return (res);
  1122. }
  1123. static int object_write_config(char **error_string)
  1124. {
  1125. struct config_iface_ver0 **modules;
  1126. int num_modules;
  1127. int i;
  1128. int res;
  1129. main_get_config_modules(&modules, &num_modules);
  1130. for (i=0; i<num_modules; i++) {
  1131. if (modules[i]->config_writeconfig) {
  1132. res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  1133. if (res)
  1134. return res;
  1135. }
  1136. }
  1137. return 0;
  1138. }
  1139. static int object_reload_config(int flush, char **error_string)
  1140. {
  1141. struct config_iface_ver0 **modules;
  1142. int num_modules;
  1143. int i;
  1144. int res;
  1145. main_get_config_modules(&modules, &num_modules);
  1146. for (i=0; i<num_modules; i++) {
  1147. if (modules[i]->config_reloadconfig) {
  1148. res = modules[i]->config_reloadconfig(&objdb_iface, flush, error_string);
  1149. if (res)
  1150. return res;
  1151. }
  1152. }
  1153. return 0;
  1154. }
  1155. struct objdb_iface_ver0 objdb_iface = {
  1156. .objdb_init = objdb_init,
  1157. .object_create = object_create,
  1158. .object_priv_set = object_priv_set,
  1159. .object_key_create = object_key_create,
  1160. .object_key_delete = object_key_delete,
  1161. .object_key_replace = object_key_replace,
  1162. .object_destroy = object_destroy,
  1163. .object_valid_set = object_valid_set,
  1164. .object_key_valid_set = object_key_valid_set,
  1165. .object_find_create = object_find_create,
  1166. .object_find_next = object_find_next,
  1167. .object_find_destroy = object_find_destroy,
  1168. .object_key_get = object_key_get,
  1169. .object_key_iter_reset = object_key_iter_reset,
  1170. .object_key_iter = object_key_iter,
  1171. .object_key_iter_from = object_key_iter_from,
  1172. .object_priv_get = object_priv_get,
  1173. .object_parent_get = object_parent_get,
  1174. .object_name_get = object_name_get,
  1175. .object_track_start = object_track_start,
  1176. .object_track_stop = object_track_stop,
  1177. .object_dump = object_dump,
  1178. .object_write_config = object_write_config,
  1179. .object_reload_config = object_reload_config,
  1180. .object_key_increment = object_key_increment,
  1181. .object_key_decrement = object_key_decrement,
  1182. };
  1183. struct lcr_iface objdb_iface_ver0[1] = {
  1184. {
  1185. .name = "objdb",
  1186. .version = 0,
  1187. .versions_replace = 0,
  1188. .versions_replace_count = 0,
  1189. .dependencies = 0,
  1190. .dependency_count = 0,
  1191. .constructor = NULL,
  1192. .destructor = NULL,
  1193. .interfaces = NULL,
  1194. }
  1195. };
  1196. struct lcr_comp objdb_comp_ver0 = {
  1197. .iface_count = 1,
  1198. .ifaces = objdb_iface_ver0
  1199. };
  1200. __attribute__ ((constructor)) static void objdb_comp_register (void) {
  1201. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  1202. lcr_component_register (&objdb_comp_ver0);
  1203. }