objdb.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. /*
  2. * Copyright (c) 2006 MontaVista Software, Inc.
  3. * Copyright (c) 2007-2010 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. #define _XOPEN_SOURCE 600
  36. #include <config.h>
  37. #include <stdio.h>
  38. #include <errno.h>
  39. #include <string.h>
  40. #include <pthread.h>
  41. #include <corosync/list.h>
  42. #include <corosync/hdb.h>
  43. #include <corosync/lcr/lcr_comp.h>
  44. #include <corosync/engine/objdb.h>
  45. #include <corosync/engine/config.h>
  46. #include <corosync/corotypes.h>
  47. #include <qb/qbipc_common.h>
  48. #include "main.h"
  49. struct object_key {
  50. void *key_name;
  51. size_t key_len;
  52. void *value;
  53. size_t value_len;
  54. objdb_value_types_t value_type;
  55. struct list_head list;
  56. };
  57. struct object_tracker {
  58. hdb_handle_t object_handle;
  59. void * data_pt;
  60. object_track_depth_t depth;
  61. object_key_change_notify_fn_t key_change_notify_fn;
  62. object_create_notify_fn_t object_create_notify_fn;
  63. object_destroy_notify_fn_t object_destroy_notify_fn;
  64. object_reload_notify_fn_t object_reload_notify_fn;
  65. struct list_head tracker_list;
  66. struct list_head object_list;
  67. };
  68. struct object_instance {
  69. void *object_name;
  70. size_t object_name_len;
  71. hdb_handle_t object_handle;
  72. hdb_handle_t parent_handle;
  73. struct list_head key_head;
  74. struct list_head child_head;
  75. struct list_head child_list;
  76. struct list_head *find_child_list;
  77. struct list_head *iter_key_list;
  78. struct list_head *iter_list;
  79. void *priv;
  80. struct object_valid *object_valid_list;
  81. int object_valid_list_entries;
  82. struct object_key_valid *object_key_valid_list;
  83. int object_key_valid_list_entries;
  84. struct list_head track_head;
  85. };
  86. struct object_find_instance {
  87. void *object_name;
  88. size_t object_len;
  89. hdb_handle_t *handles_array;
  90. size_t handles_array_size;
  91. size_t handles_array_pos;
  92. };
  93. struct objdb_iface_ver0 objdb_iface;
  94. struct list_head objdb_trackers_head;
  95. static int object_destroy (hdb_handle_t object_handle);
  96. DECLARE_HDB_DATABASE (object_instance_database,NULL);
  97. DECLARE_HDB_DATABASE (object_find_instance_database,NULL);
  98. static int objdb_init (void)
  99. {
  100. hdb_handle_t handle;
  101. struct object_instance *instance;
  102. 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 = strdup("parent");
  115. instance->object_name_len = strlen ("parent");
  116. instance->object_handle = handle;
  117. instance->parent_handle = OBJECT_PARENT_HANDLE;
  118. instance->priv = NULL;
  119. instance->object_valid_list = NULL;
  120. instance->object_valid_list_entries = 0;
  121. list_init (&instance->key_head);
  122. list_init (&instance->child_head);
  123. list_init (&instance->child_list);
  124. list_init (&instance->track_head);
  125. list_init (&objdb_trackers_head);
  126. hdb_handle_put (&object_instance_database, handle);
  127. return (0);
  128. error_destroy:
  129. hdb_handle_destroy (&object_instance_database, handle);
  130. error_exit:
  131. return (-1);
  132. }
  133. static void objdb_fini (void)
  134. {
  135. object_destroy (OBJECT_PARENT_HANDLE);
  136. hdb_destroy (&object_instance_database);
  137. }
  138. static int _object_notify_deleted_children(struct object_instance *parent_pt)
  139. {
  140. struct list_head *list;
  141. struct list_head *notify_list;
  142. int res;
  143. struct object_instance *obj_pt = NULL;
  144. struct object_tracker * tracker_pt;
  145. for (list = parent_pt->child_head.next;
  146. list != &parent_pt->child_head; list = list->next) {
  147. obj_pt = list_entry(list, struct object_instance,
  148. child_list);
  149. res = _object_notify_deleted_children(obj_pt);
  150. if (res)
  151. return res;
  152. for (notify_list = obj_pt->track_head.next;
  153. notify_list != &obj_pt->track_head;
  154. notify_list = notify_list->next) {
  155. tracker_pt = list_entry (notify_list, struct object_tracker, object_list);
  156. if ((tracker_pt != NULL) &&
  157. (tracker_pt->object_destroy_notify_fn != NULL))
  158. tracker_pt->object_destroy_notify_fn(parent_pt->object_handle,
  159. obj_pt->object_name,
  160. obj_pt->object_name_len,
  161. tracker_pt->data_pt);
  162. }
  163. }
  164. return 0;
  165. }
  166. static void object_created_notification(
  167. hdb_handle_t parent_object_handle,
  168. hdb_handle_t object_handle,
  169. const void *name_pt, size_t name_len)
  170. {
  171. struct list_head * list;
  172. struct object_instance * obj_pt;
  173. struct object_tracker * tracker_pt;
  174. hdb_handle_t obj_handle = object_handle;
  175. do {
  176. if (hdb_handle_get (&object_instance_database,
  177. obj_handle, (void *)&obj_pt) != 0) {
  178. return;
  179. }
  180. for (list = obj_pt->track_head.next;
  181. list != &obj_pt->track_head; list = list->next) {
  182. tracker_pt = list_entry (list, struct object_tracker, object_list);
  183. if (((obj_handle == parent_object_handle) ||
  184. (tracker_pt->depth == OBJECT_TRACK_DEPTH_RECURSIVE)) &&
  185. (tracker_pt->object_create_notify_fn != NULL)) {
  186. tracker_pt->object_create_notify_fn(parent_object_handle,
  187. object_handle,
  188. name_pt, name_len,
  189. tracker_pt->data_pt);
  190. }
  191. }
  192. hdb_handle_put (&object_instance_database, obj_handle);
  193. obj_handle = obj_pt->parent_handle;
  194. } while (obj_handle != OBJECT_PARENT_HANDLE);
  195. }
  196. static void object_pre_deletion_notification(hdb_handle_t object_handle,
  197. hdb_handle_t parent_object_handle,
  198. const void *name_pt, size_t name_len)
  199. {
  200. struct list_head * list;
  201. struct object_instance * obj_pt;
  202. struct object_tracker * tracker_pt;
  203. hdb_handle_t obj_handle = object_handle;
  204. do {
  205. if (hdb_handle_get (&object_instance_database,
  206. obj_handle, (void *)&obj_pt) != 0) {
  207. return;
  208. }
  209. for (list = obj_pt->track_head.next;
  210. list != &obj_pt->track_head; list = list->next) {
  211. tracker_pt = list_entry (list, struct object_tracker, object_list);
  212. if (((obj_handle == parent_object_handle) ||
  213. (tracker_pt->depth == OBJECT_TRACK_DEPTH_RECURSIVE)) &&
  214. (tracker_pt->object_destroy_notify_fn != NULL)) {
  215. tracker_pt->object_destroy_notify_fn(
  216. parent_object_handle,
  217. name_pt, name_len,
  218. tracker_pt->data_pt);
  219. }
  220. }
  221. /* notify child object listeners */
  222. if (obj_handle == object_handle)
  223. _object_notify_deleted_children(obj_pt);
  224. obj_handle = obj_pt->parent_handle;
  225. hdb_handle_put (&object_instance_database, obj_pt->object_handle);
  226. } while (obj_handle != OBJECT_PARENT_HANDLE);
  227. }
  228. static void object_key_changed_notification(hdb_handle_t object_handle,
  229. const void *name_pt, size_t name_len,
  230. const void *value_pt, size_t value_len,
  231. object_change_type_t type)
  232. {
  233. struct list_head * list;
  234. struct object_instance * obj_pt;
  235. struct object_instance * owner_pt = NULL;
  236. struct object_tracker * tracker_pt;
  237. hdb_handle_t obj_handle = object_handle;
  238. do {
  239. if (hdb_handle_get (&object_instance_database,
  240. obj_handle, (void *)&obj_pt) != 0) {
  241. return;
  242. }
  243. if (owner_pt == NULL)
  244. owner_pt = obj_pt;
  245. for (list = obj_pt->track_head.next;
  246. list != &obj_pt->track_head; list = list->next) {
  247. tracker_pt = list_entry (list, struct object_tracker, object_list);
  248. if (((obj_handle == object_handle) ||
  249. (tracker_pt->depth == OBJECT_TRACK_DEPTH_RECURSIVE)) &&
  250. (tracker_pt->key_change_notify_fn != NULL))
  251. tracker_pt->key_change_notify_fn(type, obj_pt->parent_handle, object_handle,
  252. owner_pt->object_name, owner_pt->object_name_len,
  253. name_pt, name_len,
  254. value_pt, value_len,
  255. tracker_pt->data_pt);
  256. }
  257. obj_handle = obj_pt->parent_handle;
  258. hdb_handle_put (&object_instance_database, obj_pt->object_handle);
  259. } while (obj_handle != OBJECT_PARENT_HANDLE);
  260. }
  261. static void object_reload_notification(int startstop, int flush)
  262. {
  263. struct list_head * list, *tmp;
  264. struct list_head tmplist;
  265. struct object_instance * obj_pt;
  266. struct object_tracker * tracker_pt;
  267. struct object_tracker * tmptracker_pt;
  268. if (hdb_handle_get (&object_instance_database,
  269. OBJECT_PARENT_HANDLE, (void *)&obj_pt) != 0) {
  270. return;
  271. }
  272. /*
  273. * Make a copy of the list
  274. * so that items can be added & removed in the callbacks
  275. */
  276. list_init(&tmplist);
  277. for (list = obj_pt->track_head.next;
  278. list != &obj_pt->track_head; list = list->next) {
  279. tracker_pt = list_entry (list, struct object_tracker, object_list);
  280. if (tracker_pt->object_reload_notify_fn != NULL) {
  281. tmptracker_pt = malloc(sizeof(*tracker_pt));
  282. if (tmptracker_pt) {
  283. list_add(&tmptracker_pt->object_list, &tmplist);
  284. tmptracker_pt->object_reload_notify_fn = tracker_pt->object_reload_notify_fn;
  285. tmptracker_pt->data_pt = tracker_pt->data_pt;
  286. }
  287. }
  288. }
  289. for (list = tmplist.next, tmp = list->next;
  290. list != &tmplist; list = tmp, tmp = list->next) {
  291. tracker_pt = list_entry (list, struct object_tracker, object_list);
  292. tracker_pt->object_reload_notify_fn(startstop, flush,
  293. tracker_pt->data_pt);
  294. free(tracker_pt);
  295. }
  296. hdb_handle_put (&object_instance_database, OBJECT_PARENT_HANDLE);
  297. }
  298. /*
  299. * object db create/destroy/set
  300. */
  301. static int object_create (
  302. hdb_handle_t parent_object_handle,
  303. hdb_handle_t *object_handle,
  304. const void *object_name,
  305. size_t object_name_len)
  306. {
  307. struct object_instance *object_instance;
  308. struct object_instance *parent_instance;
  309. int res;
  310. int found = 0;
  311. int i;
  312. res = hdb_handle_get (&object_instance_database,
  313. parent_object_handle, (void *)&parent_instance);
  314. if (res != 0) {
  315. goto error_exit;
  316. }
  317. /*
  318. * Do validation check if validation is configured for the parent object
  319. */
  320. if (parent_instance->object_valid_list_entries) {
  321. for (i = 0; i < parent_instance->object_valid_list_entries; i++) {
  322. if ((object_name_len ==
  323. parent_instance->object_valid_list[i].object_len) &&
  324. (memcmp (object_name,
  325. parent_instance->object_valid_list[i].object_name,
  326. object_name_len) == 0)) {
  327. found = 1;
  328. break;
  329. }
  330. }
  331. /*
  332. * Item not found in validation list
  333. */
  334. if (found == 0) {
  335. goto error_object_put;
  336. }
  337. }
  338. res = hdb_handle_create (&object_instance_database,
  339. sizeof (struct object_instance), object_handle);
  340. if (res != 0) {
  341. goto error_object_put;
  342. }
  343. res = hdb_handle_get (&object_instance_database,
  344. *object_handle, (void *)&object_instance);
  345. if (res != 0) {
  346. goto error_destroy;
  347. }
  348. list_init (&object_instance->key_head);
  349. list_init (&object_instance->child_head);
  350. list_init (&object_instance->child_list);
  351. list_init (&object_instance->track_head);
  352. object_instance->object_name = malloc (object_name_len);
  353. if (object_instance->object_name == 0) {
  354. goto error_put_destroy;
  355. }
  356. memcpy (object_instance->object_name, object_name, object_name_len);
  357. object_instance->object_name_len = object_name_len;
  358. list_add_tail (&object_instance->child_list, &parent_instance->child_head);
  359. object_instance->object_handle = *object_handle;
  360. object_instance->find_child_list = &object_instance->child_head;
  361. object_instance->iter_key_list = &object_instance->key_head;
  362. object_instance->iter_list = &object_instance->child_head;
  363. object_instance->priv = NULL;
  364. object_instance->object_valid_list = NULL;
  365. object_instance->object_valid_list_entries = 0;
  366. object_instance->parent_handle = parent_object_handle;
  367. hdb_handle_put (&object_instance_database, *object_handle);
  368. hdb_handle_put (&object_instance_database, parent_object_handle);
  369. object_created_notification(
  370. object_instance->parent_handle,
  371. object_instance->object_handle,
  372. object_instance->object_name,
  373. object_instance->object_name_len);
  374. return (0);
  375. error_put_destroy:
  376. hdb_handle_put (&object_instance_database, *object_handle);
  377. error_destroy:
  378. hdb_handle_destroy (&object_instance_database, *object_handle);
  379. error_object_put:
  380. hdb_handle_put (&object_instance_database, parent_object_handle);
  381. error_exit:
  382. return (-1);
  383. }
  384. static int object_priv_set (
  385. hdb_handle_t object_handle,
  386. void *priv)
  387. {
  388. int res;
  389. struct object_instance *object_instance;
  390. res = hdb_handle_get (&object_instance_database,
  391. object_handle, (void *)&object_instance);
  392. if (res != 0) {
  393. goto error_exit;
  394. }
  395. object_instance->priv = priv;
  396. hdb_handle_put (&object_instance_database, object_handle);
  397. return (0);
  398. error_exit:
  399. return (-1);
  400. }
  401. static int object_key_create_typed(
  402. hdb_handle_t object_handle,
  403. const char *key_name,
  404. const void *value,
  405. size_t value_len,
  406. objdb_value_types_t value_type)
  407. {
  408. struct object_instance *instance;
  409. struct object_key *object_key;
  410. int res;
  411. struct list_head *list;
  412. int found = 0;
  413. int i;
  414. size_t key_len = strlen(key_name);
  415. size_t expected_size;
  416. int test_size_by_type = CS_TRUE;
  417. res = hdb_handle_get (&object_instance_database,
  418. object_handle, (void *)&instance);
  419. if (res != 0) {
  420. goto error_exit;
  421. }
  422. switch (value_type) {
  423. case OBJDB_VALUETYPE_INT16:
  424. expected_size = sizeof (int16_t);
  425. break;
  426. case OBJDB_VALUETYPE_UINT16:
  427. expected_size = sizeof (uint16_t);
  428. break;
  429. case OBJDB_VALUETYPE_INT32:
  430. expected_size = sizeof (int32_t);
  431. break;
  432. case OBJDB_VALUETYPE_UINT32:
  433. expected_size = sizeof (uint32_t);
  434. break;
  435. case OBJDB_VALUETYPE_INT64:
  436. expected_size = sizeof (int64_t);
  437. break;
  438. case OBJDB_VALUETYPE_UINT64:
  439. expected_size = sizeof (uint64_t);
  440. break;
  441. case OBJDB_VALUETYPE_FLOAT:
  442. expected_size = sizeof (float);
  443. break;
  444. case OBJDB_VALUETYPE_DOUBLE:
  445. expected_size = sizeof (double);
  446. break;
  447. case OBJDB_VALUETYPE_ANY:
  448. default:
  449. test_size_by_type = CS_FALSE;
  450. break;
  451. }
  452. if (test_size_by_type) {
  453. if (expected_size != value_len) {
  454. //printf ("%s exp:%d != len:%d\n", key_name, expected_size, value_len);
  455. goto error_put;
  456. }
  457. }
  458. /*
  459. * Do validation check if validation is configured for the parent object
  460. */
  461. if (instance->object_key_valid_list_entries) {
  462. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  463. if ((key_len ==
  464. instance->object_key_valid_list[i].key_len) &&
  465. (memcmp (key_name,
  466. instance->object_key_valid_list[i].key_name,
  467. key_len) == 0)) {
  468. found = 1;
  469. break;
  470. }
  471. }
  472. /*
  473. * Item not found in validation list
  474. */
  475. if (found == 0) {
  476. goto error_put;
  477. } else {
  478. if (instance->object_key_valid_list[i].validate_callback) {
  479. res = instance->object_key_valid_list[i].validate_callback (
  480. key_name, key_len, value, value_len);
  481. if (res != 0) {
  482. goto error_put;
  483. }
  484. }
  485. }
  486. }
  487. /* See if it already exists */
  488. found = 0;
  489. for (list = instance->key_head.next;
  490. list != &instance->key_head; list = list->next) {
  491. object_key = list_entry (list, struct object_key, list);
  492. if ((object_key->key_len == key_len) &&
  493. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  494. found = 1;
  495. break;
  496. }
  497. }
  498. if (found) {
  499. free(object_key->value);
  500. }
  501. else {
  502. object_key = malloc (sizeof (struct object_key));
  503. if (object_key == 0) {
  504. goto error_put;
  505. }
  506. object_key->key_name = malloc (key_len + 1);
  507. if (object_key->key_name == 0) {
  508. goto error_put_object;
  509. }
  510. memcpy (object_key->key_name, key_name, key_len + 1);
  511. list_init (&object_key->list);
  512. list_add_tail (&object_key->list, &instance->key_head);
  513. }
  514. object_key->value = malloc (value_len);
  515. if (object_key->value == 0) {
  516. goto error_put_key;
  517. }
  518. memcpy (object_key->value, value, value_len);
  519. object_key->key_len = key_len;
  520. object_key->value_len = value_len;
  521. object_key->value_type = value_type;
  522. object_key_changed_notification(object_handle, key_name, key_len,
  523. value, value_len, OBJECT_KEY_CREATED);
  524. hdb_handle_put (&object_instance_database, object_handle);
  525. return (0);
  526. error_put_key:
  527. free (object_key->key_name);
  528. error_put_object:
  529. free (object_key);
  530. error_put:
  531. hdb_handle_put (&object_instance_database, object_handle);
  532. error_exit:
  533. return (-1);
  534. }
  535. static int object_key_create (
  536. hdb_handle_t object_handle,
  537. const void *key_name,
  538. size_t key_len,
  539. const void *value,
  540. size_t value_len)
  541. {
  542. char *key_name_terminated = NULL;
  543. char *key_name_str = (char*)key_name;
  544. int ret;
  545. if (key_name_str[key_len-1] != '\0') {
  546. key_name_terminated = malloc (key_len + 1);
  547. memcpy (key_name_terminated, key_name, key_len);
  548. key_name_terminated[key_len] = '\0';
  549. key_name_str = key_name_terminated;
  550. }
  551. ret = object_key_create_typed (object_handle, key_name_str,
  552. value, value_len, OBJDB_VALUETYPE_ANY);
  553. if (key_name_terminated) {
  554. free (key_name_terminated);
  555. }
  556. return ret;
  557. }
  558. static int _clear_object(struct object_instance *instance)
  559. {
  560. struct list_head *list;
  561. int res;
  562. struct object_instance *find_instance = NULL;
  563. struct object_key *object_key = NULL;
  564. struct object_tracker *tracker_pt = NULL;
  565. for (list = instance->key_head.next;
  566. list != &instance->key_head; ) {
  567. object_key = list_entry (list, struct object_key,
  568. list);
  569. list = list->next;
  570. list_del(&object_key->list);
  571. free(object_key->key_name);
  572. free(object_key->value);
  573. free(object_key);
  574. }
  575. for (list = instance->track_head.next;
  576. list != &instance->track_head;) {
  577. tracker_pt = list_entry (list,
  578. struct object_tracker, object_list);
  579. list = list->next;
  580. list_del(&tracker_pt->tracker_list);
  581. list_del(&tracker_pt->object_list);
  582. free(tracker_pt);
  583. }
  584. for (list = instance->child_head.next;
  585. list != &instance->child_head; ) {
  586. find_instance = list_entry (list, struct object_instance,
  587. child_list);
  588. res = _clear_object(find_instance);
  589. if (res)
  590. return res;
  591. list = list->next;
  592. list_del(&find_instance->child_list);
  593. free(find_instance->object_name);
  594. hdb_handle_destroy (&object_instance_database, find_instance->object_handle);
  595. }
  596. return 0;
  597. }
  598. static int object_destroy (
  599. hdb_handle_t object_handle)
  600. {
  601. struct object_instance *instance;
  602. int res;
  603. res = hdb_handle_get (&object_instance_database,
  604. object_handle, (void *)&instance);
  605. if (res != 0) {
  606. return (res);
  607. }
  608. object_pre_deletion_notification(object_handle,
  609. instance->parent_handle,
  610. instance->object_name,
  611. instance->object_name_len);
  612. /* Recursively clear sub-objects & keys */
  613. res = _clear_object(instance);
  614. list_del(&instance->child_list);
  615. free(instance->object_name);
  616. hdb_handle_put (&object_instance_database, object_handle);
  617. hdb_handle_destroy (&object_instance_database, object_handle);
  618. return (res);
  619. }
  620. static int object_valid_set (
  621. hdb_handle_t object_handle,
  622. struct object_valid *object_valid_list,
  623. size_t object_valid_list_entries)
  624. {
  625. struct object_instance *instance;
  626. int res;
  627. res = hdb_handle_get (&object_instance_database,
  628. object_handle, (void *)&instance);
  629. if (res != 0) {
  630. goto error_exit;
  631. }
  632. instance->object_valid_list = object_valid_list;
  633. instance->object_valid_list_entries = object_valid_list_entries;
  634. hdb_handle_put (&object_instance_database, object_handle);
  635. return (0);
  636. error_exit:
  637. return (-1);
  638. }
  639. static int object_key_valid_set (
  640. hdb_handle_t object_handle,
  641. struct object_key_valid *object_key_valid_list,
  642. size_t object_key_valid_list_entries)
  643. {
  644. struct object_instance *instance;
  645. int res;
  646. res = hdb_handle_get (&object_instance_database,
  647. object_handle, (void *)&instance);
  648. if (res != 0) {
  649. goto error_exit;
  650. }
  651. instance->object_key_valid_list = object_key_valid_list;
  652. instance->object_key_valid_list_entries = object_key_valid_list_entries;
  653. hdb_handle_put (&object_instance_database, object_handle);
  654. return (0);
  655. error_exit:
  656. return (-1);
  657. }
  658. /*
  659. * object db reading
  660. */
  661. static int object_find_create (
  662. hdb_handle_t object_handle,
  663. const void *object_name,
  664. size_t object_len,
  665. hdb_handle_t *object_find_handle)
  666. {
  667. int res;
  668. struct object_instance *iter_obj_inst;
  669. struct object_instance *object_instance;
  670. struct object_find_instance *object_find_instance;
  671. struct list_head *list;
  672. hdb_handle_t *handles_array, *handles_array_realloc;
  673. size_t ha_len;
  674. size_t ha_used;
  675. res = hdb_handle_get (&object_instance_database,
  676. object_handle, (void *)&object_instance);
  677. if (res != 0) {
  678. goto error_exit;
  679. }
  680. res = hdb_handle_create (&object_find_instance_database,
  681. sizeof (struct object_find_instance), object_find_handle);
  682. if (res != 0) {
  683. goto error_put;
  684. }
  685. res = hdb_handle_get (&object_find_instance_database,
  686. *object_find_handle, (void *)&object_find_instance);
  687. if (res != 0) {
  688. goto error_destroy;
  689. }
  690. object_find_instance->object_name = (char *)object_name;
  691. object_find_instance->object_len = object_len;
  692. ha_len = ha_used = 0;
  693. handles_array = NULL;
  694. for (list = object_instance->child_head.next;
  695. list != &object_instance->child_head; list = list->next) {
  696. iter_obj_inst = list_entry (list, struct object_instance,
  697. child_list);
  698. if (object_find_instance->object_len == 0 ||
  699. ((iter_obj_inst->object_name_len ==
  700. object_find_instance->object_len) &&
  701. (memcmp (iter_obj_inst->object_name,
  702. object_find_instance->object_name,
  703. object_find_instance->object_len) == 0))) {
  704. /*
  705. * Add handle to list
  706. */
  707. if (ha_used + 1 > ha_len) {
  708. ha_len = ha_len * 2 + 1;
  709. if ((handles_array_realloc =
  710. realloc (handles_array, ha_len * sizeof (hdb_handle_t))) ==
  711. NULL) {
  712. goto error_ha_free;
  713. }
  714. handles_array = handles_array_realloc;
  715. }
  716. handles_array[ha_used] =
  717. iter_obj_inst->object_handle;
  718. ha_used++;
  719. }
  720. }
  721. object_find_instance->handles_array_size = ha_used;
  722. object_find_instance->handles_array_pos = 0;
  723. object_find_instance->handles_array = handles_array;
  724. hdb_handle_put (&object_instance_database, object_handle);
  725. hdb_handle_put (&object_find_instance_database, *object_find_handle);
  726. return (0);
  727. error_ha_free:
  728. free(handles_array);
  729. error_destroy:
  730. hdb_handle_destroy (&object_instance_database, *object_find_handle);
  731. error_put:
  732. hdb_handle_put (&object_instance_database, object_handle);
  733. error_exit:
  734. return (-1);
  735. }
  736. static int object_find_next (
  737. hdb_handle_t object_find_handle,
  738. hdb_handle_t *object_handle)
  739. {
  740. int res;
  741. struct object_find_instance *object_find_instance;
  742. struct object_instance *object_instance = NULL;
  743. int found = 0;
  744. size_t pos;
  745. res = hdb_handle_get (&object_find_instance_database,
  746. object_find_handle, (void *)&object_find_instance);
  747. if (res != 0) {
  748. goto error_exit;
  749. }
  750. for (pos = object_find_instance->handles_array_pos; !found &&
  751. pos < object_find_instance->handles_array_size; pos++) {
  752. *object_handle = object_find_instance->handles_array[pos];
  753. res = hdb_handle_get (&object_instance_database,
  754. *object_handle, (void *)&object_instance);
  755. if (res != 0) {
  756. continue;
  757. }
  758. if (object_find_instance->object_len == 0 ||
  759. ((object_instance->object_name_len ==
  760. object_find_instance->object_len) &&
  761. (memcmp (object_instance->object_name,
  762. object_find_instance->object_name,
  763. object_find_instance->object_len) == 0))) {
  764. found = 1;
  765. }
  766. hdb_handle_put (&object_instance_database, *object_handle);
  767. }
  768. object_find_instance->handles_array_pos = pos;
  769. hdb_handle_put (&object_find_instance_database, object_find_handle);
  770. if (found) {
  771. *object_handle = object_instance->object_handle;
  772. res = 0;
  773. } else {
  774. res = -1;
  775. }
  776. return (res);
  777. error_exit:
  778. return (-1);
  779. }
  780. static int object_find_destroy (
  781. hdb_handle_t object_find_handle)
  782. {
  783. struct object_find_instance *object_find_instance;
  784. int res;
  785. res = hdb_handle_get (&object_find_instance_database,
  786. object_find_handle, (void *)&object_find_instance);
  787. if (res != 0) {
  788. goto error_exit;
  789. }
  790. free(object_find_instance->handles_array);
  791. hdb_handle_put(&object_find_instance_database, object_find_handle);
  792. hdb_handle_destroy(&object_find_instance_database, object_find_handle);
  793. return (0);
  794. error_exit:
  795. return (-1);
  796. }
  797. static int object_key_get_typed (
  798. hdb_handle_t object_handle,
  799. const char *key_name,
  800. void **value,
  801. size_t *value_len,
  802. objdb_value_types_t * type)
  803. {
  804. int res = 0;
  805. struct object_instance *instance;
  806. struct object_key *object_key = NULL;
  807. struct list_head *list;
  808. int found = 0;
  809. size_t key_len = strlen(key_name);
  810. res = hdb_handle_get (&object_instance_database,
  811. object_handle, (void *)&instance);
  812. if (res != 0) {
  813. goto error_exit;
  814. }
  815. for (list = instance->key_head.next;
  816. list != &instance->key_head; list = list->next) {
  817. object_key = list_entry (list, struct object_key, list);
  818. if ((object_key->key_len == key_len) &&
  819. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  820. found = 1;
  821. break;
  822. }
  823. }
  824. if (found) {
  825. *value = object_key->value;
  826. if (value_len) {
  827. *value_len = object_key->value_len;
  828. }
  829. *type = object_key->value_type;
  830. }
  831. else {
  832. res = -1;
  833. }
  834. hdb_handle_put (&object_instance_database, object_handle);
  835. return (res);
  836. error_exit:
  837. return (-1);
  838. }
  839. static int object_key_get (
  840. hdb_handle_t object_handle,
  841. const void *key_name,
  842. size_t key_len,
  843. void **value,
  844. size_t *value_len)
  845. {
  846. objdb_value_types_t t;
  847. int ret;
  848. char *key_name_str = (char*)key_name;
  849. char *key_name_terminated = NULL;
  850. if (key_name_str[key_len-1] != '\0') {
  851. key_name_terminated = malloc (key_len + 1);
  852. memcpy (key_name_terminated, key_name, key_len);
  853. key_name_terminated[key_len] = '\0';
  854. key_name_str = key_name_terminated;
  855. }
  856. ret = object_key_get_typed(object_handle,
  857. key_name_str,
  858. value, value_len, &t);
  859. if (key_name_terminated) {
  860. free (key_name_terminated);
  861. }
  862. return ret;
  863. }
  864. static int object_key_increment (
  865. hdb_handle_t object_handle,
  866. const void *key_name,
  867. size_t key_len,
  868. unsigned int *value)
  869. {
  870. int res = 0;
  871. struct object_instance *instance;
  872. struct object_key *object_key = NULL;
  873. struct list_head *list;
  874. int found = 0;
  875. res = hdb_handle_get (&object_instance_database,
  876. object_handle, (void *)&instance);
  877. if (res != 0) {
  878. goto error_exit;
  879. }
  880. for (list = instance->key_head.next;
  881. list != &instance->key_head; list = list->next) {
  882. object_key = list_entry (list, struct object_key, list);
  883. if ((object_key->key_len == key_len) &&
  884. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  885. found = 1;
  886. break;
  887. }
  888. }
  889. if (found) {
  890. switch (object_key->value_type) {
  891. case OBJDB_VALUETYPE_INT16:
  892. (*(int16_t *)object_key->value)++;
  893. break;
  894. case OBJDB_VALUETYPE_UINT16:
  895. (*(uint16_t *)object_key->value)++;
  896. break;
  897. case OBJDB_VALUETYPE_INT32:
  898. (*(int32_t *)object_key->value)++;
  899. break;
  900. case OBJDB_VALUETYPE_UINT32:
  901. (*(uint32_t *)object_key->value)++;
  902. break;
  903. case OBJDB_VALUETYPE_INT64:
  904. (*(int64_t *)object_key->value)++;
  905. break;
  906. case OBJDB_VALUETYPE_UINT64:
  907. (*(uint64_t *)object_key->value)++;
  908. break;
  909. case OBJDB_VALUETYPE_ANY:
  910. /* for backwards compatibilty */
  911. if (object_key->value_len == sizeof(int)) {
  912. (*(int *)object_key->value)++;
  913. }
  914. else {
  915. res = -1;
  916. }
  917. break;
  918. default:
  919. res = -1;
  920. break;
  921. }
  922. if (res == 0) {
  923. /* nasty, not sure why we need to return this typed
  924. * instead of void* */
  925. *value = *(int *)object_key->value;
  926. }
  927. }
  928. else {
  929. res = -1;
  930. }
  931. hdb_handle_put (&object_instance_database, object_handle);
  932. if (res == 0) {
  933. object_key_changed_notification (object_handle, key_name, key_len,
  934. object_key->value, object_key->value_len, OBJECT_KEY_REPLACED);
  935. }
  936. return (res);
  937. error_exit:
  938. return (-1);
  939. }
  940. static int object_key_decrement (
  941. hdb_handle_t object_handle,
  942. const void *key_name,
  943. size_t key_len,
  944. unsigned int *value)
  945. {
  946. int res = 0;
  947. struct object_instance *instance;
  948. struct object_key *object_key = NULL;
  949. struct list_head *list;
  950. int found = 0;
  951. res = hdb_handle_get (&object_instance_database,
  952. object_handle, (void *)&instance);
  953. if (res != 0) {
  954. goto error_exit;
  955. }
  956. for (list = instance->key_head.next;
  957. list != &instance->key_head; list = list->next) {
  958. object_key = list_entry (list, struct object_key, list);
  959. if ((object_key->key_len == key_len) &&
  960. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  961. found = 1;
  962. break;
  963. }
  964. }
  965. if (found) {
  966. switch (object_key->value_type) {
  967. case OBJDB_VALUETYPE_INT16:
  968. (*(int16_t *)object_key->value)--;
  969. break;
  970. case OBJDB_VALUETYPE_UINT16:
  971. (*(uint16_t *)object_key->value)--;
  972. break;
  973. case OBJDB_VALUETYPE_INT32:
  974. (*(int32_t *)object_key->value)--;
  975. break;
  976. case OBJDB_VALUETYPE_UINT32:
  977. (*(uint32_t *)object_key->value)--;
  978. break;
  979. case OBJDB_VALUETYPE_INT64:
  980. (*(int64_t *)object_key->value)--;
  981. break;
  982. case OBJDB_VALUETYPE_UINT64:
  983. (*(uint64_t *)object_key->value)--;
  984. break;
  985. case OBJDB_VALUETYPE_ANY:
  986. /* for backwards compatibilty */
  987. if (object_key->value_len == sizeof(int)) {
  988. (*(int *)object_key->value)--;
  989. }
  990. else {
  991. res = -1;
  992. }
  993. break;
  994. default:
  995. res = -1;
  996. break;
  997. }
  998. if (res == 0) {
  999. /* nasty, not sure why we need to return this typed
  1000. * instead of void* */
  1001. *value = *(int *)object_key->value;
  1002. }
  1003. }
  1004. else {
  1005. res = -1;
  1006. }
  1007. hdb_handle_put (&object_instance_database, object_handle);
  1008. if (res == 0) {
  1009. object_key_changed_notification (object_handle, key_name, key_len,
  1010. object_key->value, object_key->value_len, OBJECT_KEY_REPLACED);
  1011. }
  1012. return (res);
  1013. error_exit:
  1014. return (-1);
  1015. }
  1016. static int object_key_delete (
  1017. hdb_handle_t object_handle,
  1018. const void *key_name,
  1019. size_t key_len)
  1020. {
  1021. int res;
  1022. int ret = 0;
  1023. struct object_instance *instance;
  1024. struct object_key *object_key = NULL;
  1025. struct list_head *list;
  1026. int found = 0;
  1027. res = hdb_handle_get (&object_instance_database,
  1028. object_handle, (void *)&instance);
  1029. if (res != 0) {
  1030. goto error_exit;
  1031. }
  1032. for (list = instance->key_head.next;
  1033. list != &instance->key_head; list = list->next) {
  1034. object_key = list_entry (list, struct object_key, list);
  1035. if ((object_key->key_len == key_len) &&
  1036. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  1037. found = 1;
  1038. break;
  1039. }
  1040. }
  1041. if (found) {
  1042. list_del(&object_key->list);
  1043. free(object_key->key_name);
  1044. free(object_key->value);
  1045. free(object_key);
  1046. }
  1047. else {
  1048. ret = -1;
  1049. errno = ENOENT;
  1050. }
  1051. hdb_handle_put (&object_instance_database, object_handle);
  1052. if (ret == 0) {
  1053. object_key_changed_notification(object_handle, key_name, key_len,
  1054. NULL, 0, OBJECT_KEY_DELETED);
  1055. }
  1056. return (ret);
  1057. error_exit:
  1058. return (-1);
  1059. }
  1060. static int object_key_replace (
  1061. hdb_handle_t object_handle,
  1062. const void *key_name,
  1063. size_t key_len,
  1064. const void *new_value,
  1065. size_t new_value_len)
  1066. {
  1067. int res;
  1068. int ret = 0;
  1069. struct object_instance *instance;
  1070. struct object_key *object_key = NULL;
  1071. struct list_head *list;
  1072. int found = 0;
  1073. int value_changed = 0;
  1074. res = hdb_handle_get (&object_instance_database,
  1075. object_handle, (void *)&instance);
  1076. if (res != 0) {
  1077. goto error_exit;
  1078. }
  1079. for (list = instance->key_head.next;
  1080. list != &instance->key_head; list = list->next) {
  1081. object_key = list_entry (list, struct object_key, list);
  1082. if ((object_key->key_len == key_len) &&
  1083. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  1084. found = 1;
  1085. break;
  1086. }
  1087. }
  1088. if (found) {
  1089. int i;
  1090. int found_validator = 0;
  1091. /*
  1092. * Do validation check if validation is configured for the parent object
  1093. */
  1094. if (instance->object_key_valid_list_entries) {
  1095. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  1096. if ((key_len ==
  1097. instance->object_key_valid_list[i].key_len) &&
  1098. (memcmp (key_name,
  1099. instance->object_key_valid_list[i].key_name,
  1100. key_len) == 0)) {
  1101. found_validator = 1;
  1102. break;
  1103. }
  1104. }
  1105. /*
  1106. * Item not found in validation list
  1107. */
  1108. if (found_validator == 0) {
  1109. goto error_put;
  1110. } else {
  1111. if (instance->object_key_valid_list[i].validate_callback) {
  1112. res = instance->object_key_valid_list[i].validate_callback (
  1113. key_name, key_len, new_value, new_value_len);
  1114. if (res != 0) {
  1115. goto error_put;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. if (new_value_len != object_key->value_len) {
  1121. void *replacement_value;
  1122. replacement_value = malloc(new_value_len);
  1123. if (!replacement_value)
  1124. goto error_exit;
  1125. free(object_key->value);
  1126. object_key->value = replacement_value;
  1127. memset (object_key->value, 0, new_value_len);
  1128. object_key->value_len = new_value_len;
  1129. }
  1130. if (memcmp (object_key->value, new_value, new_value_len) == 0) {
  1131. value_changed = 0;
  1132. }
  1133. else {
  1134. memcpy(object_key->value, new_value, new_value_len);
  1135. object_key->value_len = new_value_len;
  1136. value_changed = 1;
  1137. }
  1138. }
  1139. else {
  1140. ret = -1;
  1141. errno = ENOENT;
  1142. }
  1143. hdb_handle_put (&object_instance_database, object_handle);
  1144. if (ret == 0 && value_changed) {
  1145. object_key_changed_notification (object_handle, key_name, key_len,
  1146. new_value, new_value_len, OBJECT_KEY_REPLACED);
  1147. }
  1148. return (ret);
  1149. error_put:
  1150. hdb_handle_put (&object_instance_database, object_handle);
  1151. error_exit:
  1152. return (-1);
  1153. }
  1154. static int object_priv_get (
  1155. hdb_handle_t object_handle,
  1156. void **priv)
  1157. {
  1158. int res;
  1159. struct object_instance *object_instance;
  1160. res = hdb_handle_get (&object_instance_database,
  1161. object_handle, (void *)&object_instance);
  1162. if (res != 0) {
  1163. goto error_exit;
  1164. }
  1165. *priv = object_instance->priv;
  1166. hdb_handle_put (&object_instance_database, object_handle);
  1167. return (0);
  1168. error_exit:
  1169. return (-1);
  1170. }
  1171. static int _dump_object(struct object_instance *instance, FILE *file, int depth)
  1172. {
  1173. struct list_head *list;
  1174. int res;
  1175. int i;
  1176. struct object_instance *find_instance = NULL;
  1177. struct object_key *object_key = NULL;
  1178. char stringbuf1[1024];
  1179. char stringbuf2[1024];
  1180. memcpy(stringbuf1, instance->object_name, instance->object_name_len);
  1181. stringbuf1[instance->object_name_len] = '\0';
  1182. for (i=0; i<depth; i++)
  1183. fprintf(file, " ");
  1184. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1185. fprintf(file, "%s {\n", stringbuf1);
  1186. for (list = instance->key_head.next;
  1187. list != &instance->key_head; list = list->next) {
  1188. object_key = list_entry (list, struct object_key,
  1189. list);
  1190. memcpy(stringbuf1, object_key->key_name, object_key->key_len);
  1191. stringbuf1[object_key->key_len] = '\0';
  1192. switch (object_key->value_type) {
  1193. case OBJDB_VALUETYPE_INT16:
  1194. snprintf (stringbuf2, sizeof(int), "%hd",
  1195. *(unsigned int*)object_key->value);
  1196. break;
  1197. case OBJDB_VALUETYPE_UINT16:
  1198. snprintf (stringbuf2, sizeof(int), "%hu",
  1199. *(unsigned int*)object_key->value);
  1200. break;
  1201. case OBJDB_VALUETYPE_INT32:
  1202. snprintf (stringbuf2, sizeof(int), "%d",
  1203. *(int*)object_key->value);
  1204. break;
  1205. case OBJDB_VALUETYPE_UINT32:
  1206. snprintf (stringbuf2, sizeof(int), "%u",
  1207. *(unsigned int*)object_key->value);
  1208. break;
  1209. case OBJDB_VALUETYPE_INT64:
  1210. snprintf (stringbuf2, sizeof(int), "%ld",
  1211. *(long int*)object_key->value);
  1212. break;
  1213. case OBJDB_VALUETYPE_UINT64:
  1214. snprintf (stringbuf2, sizeof(int), "%lu",
  1215. *(unsigned long int*)object_key->value);
  1216. break;
  1217. default:
  1218. case OBJDB_VALUETYPE_STRING:
  1219. case OBJDB_VALUETYPE_ANY:
  1220. memcpy(stringbuf2, object_key->value, object_key->value_len);
  1221. stringbuf2[object_key->value_len] = '\0';
  1222. break;
  1223. }
  1224. for (i=0; i<depth+1; i++)
  1225. fprintf(file, " ");
  1226. fprintf(file, "%s: %s\n", stringbuf1, stringbuf2);
  1227. }
  1228. for (list = instance->child_head.next;
  1229. list != &instance->child_head; list = list->next) {
  1230. find_instance = list_entry (list, struct object_instance,
  1231. child_list);
  1232. res = _dump_object(find_instance, file, depth+1);
  1233. if (res)
  1234. return res;
  1235. }
  1236. for (i=0; i<depth; i++)
  1237. fprintf(file, " ");
  1238. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1239. fprintf(file, "}\n");
  1240. return 0;
  1241. }
  1242. static int object_key_iter_reset(hdb_handle_t object_handle)
  1243. {
  1244. int res;
  1245. struct object_instance *instance;
  1246. res = hdb_handle_get (&object_instance_database,
  1247. object_handle, (void *)&instance);
  1248. if (res != 0) {
  1249. goto error_exit;
  1250. }
  1251. instance->iter_key_list = &instance->key_head;
  1252. hdb_handle_put (&object_instance_database, object_handle);
  1253. return (0);
  1254. error_exit:
  1255. return (-1);
  1256. }
  1257. static int object_key_iter_typed (hdb_handle_t parent_object_handle,
  1258. char **key_name,
  1259. void **value,
  1260. size_t *value_len,
  1261. objdb_value_types_t *type)
  1262. {
  1263. int res;
  1264. struct object_instance *instance;
  1265. struct object_key *find_key = NULL;
  1266. struct list_head *list;
  1267. unsigned int found = 0;
  1268. res = hdb_handle_get (&object_instance_database,
  1269. parent_object_handle, (void *)&instance);
  1270. if (res != 0) {
  1271. goto error_exit;
  1272. }
  1273. res = -ENOENT;
  1274. list = instance->iter_key_list->next;
  1275. if (list != &instance->key_head) {
  1276. find_key = list_entry (list, struct object_key, list);
  1277. found = 1;
  1278. }
  1279. instance->iter_key_list = list;
  1280. if (found) {
  1281. *key_name = find_key->key_name;
  1282. *value = find_key->value;
  1283. *type = find_key->value_type;
  1284. if (value_len)
  1285. *value_len = find_key->value_len;
  1286. res = 0;
  1287. }
  1288. else {
  1289. res = -1;
  1290. }
  1291. hdb_handle_put (&object_instance_database, parent_object_handle);
  1292. return (res);
  1293. error_exit:
  1294. return (-1);
  1295. }
  1296. static int object_key_iter(hdb_handle_t parent_object_handle,
  1297. void **key_name,
  1298. size_t *key_len,
  1299. void **value,
  1300. size_t *value_len)
  1301. {
  1302. objdb_value_types_t t;
  1303. int ret;
  1304. char *str;
  1305. ret = object_key_iter_typed (parent_object_handle,
  1306. (char**)key_name, value, value_len, &t);
  1307. if (!ret) {
  1308. str = *key_name;
  1309. *key_len = strlen(str);
  1310. }
  1311. return ret;
  1312. }
  1313. static int object_key_iter_from(hdb_handle_t parent_object_handle,
  1314. hdb_handle_t start_pos,
  1315. void **key_name,
  1316. size_t *key_len,
  1317. void **value,
  1318. size_t *value_len)
  1319. {
  1320. unsigned int pos = 0;
  1321. int res;
  1322. struct object_instance *instance;
  1323. struct object_key *find_key = NULL;
  1324. struct list_head *list;
  1325. unsigned int found = 0;
  1326. res = hdb_handle_get (&object_instance_database,
  1327. parent_object_handle, (void *)&instance);
  1328. if (res != 0) {
  1329. goto error_exit;
  1330. }
  1331. res = -ENOENT;
  1332. for (list = instance->key_head.next;
  1333. list != &instance->key_head; list = list->next) {
  1334. find_key = list_entry (list, struct object_key, list);
  1335. if (pos++ == start_pos) {
  1336. found = 1;
  1337. break;
  1338. }
  1339. }
  1340. if (found) {
  1341. *key_name = find_key->key_name;
  1342. if (key_len)
  1343. *key_len = find_key->key_len;
  1344. *value = find_key->value;
  1345. if (value_len)
  1346. *value_len = find_key->value_len;
  1347. res = 0;
  1348. }
  1349. else {
  1350. res = -1;
  1351. }
  1352. hdb_handle_put (&object_instance_database, parent_object_handle);
  1353. return (res);
  1354. error_exit:
  1355. return (-1);
  1356. }
  1357. static int object_parent_get(hdb_handle_t object_handle,
  1358. hdb_handle_t *parent_handle)
  1359. {
  1360. struct object_instance *instance;
  1361. int res;
  1362. res = hdb_handle_get (&object_instance_database,
  1363. object_handle, (void *)&instance);
  1364. if (res != 0) {
  1365. return (res);
  1366. }
  1367. if (object_handle == OBJECT_PARENT_HANDLE)
  1368. *parent_handle = 0;
  1369. else
  1370. *parent_handle = instance->parent_handle;
  1371. hdb_handle_put (&object_instance_database, object_handle);
  1372. return (0);
  1373. }
  1374. static int object_name_get(hdb_handle_t object_handle,
  1375. char *object_name,
  1376. size_t *object_name_len)
  1377. {
  1378. struct object_instance *instance;
  1379. int res;
  1380. res = hdb_handle_get (&object_instance_database,
  1381. object_handle, (void *)&instance);
  1382. if (res != 0) {
  1383. return (res);
  1384. }
  1385. memcpy(object_name, instance->object_name, instance->object_name_len);
  1386. *object_name_len = instance->object_name_len;
  1387. hdb_handle_put (&object_instance_database, object_handle);
  1388. return (0);
  1389. }
  1390. static int object_track_start(hdb_handle_t object_handle,
  1391. object_track_depth_t depth,
  1392. object_key_change_notify_fn_t key_change_notify_fn,
  1393. object_create_notify_fn_t object_create_notify_fn,
  1394. object_destroy_notify_fn_t object_destroy_notify_fn,
  1395. object_reload_notify_fn_t object_reload_notify_fn,
  1396. void * priv_data_pt)
  1397. {
  1398. struct object_instance *instance;
  1399. int res;
  1400. struct object_tracker * tracker_pt;
  1401. res = hdb_handle_get (&object_instance_database,
  1402. object_handle, (void *)&instance);
  1403. if (res != 0) {
  1404. return (res);
  1405. }
  1406. tracker_pt = malloc(sizeof(struct object_tracker));
  1407. tracker_pt->depth = depth;
  1408. tracker_pt->object_handle = object_handle;
  1409. tracker_pt->key_change_notify_fn = key_change_notify_fn;
  1410. tracker_pt->object_create_notify_fn = object_create_notify_fn;
  1411. tracker_pt->object_destroy_notify_fn = object_destroy_notify_fn;
  1412. tracker_pt->object_reload_notify_fn = object_reload_notify_fn;
  1413. tracker_pt->data_pt = priv_data_pt;
  1414. list_init(&tracker_pt->object_list);
  1415. list_init(&tracker_pt->tracker_list);
  1416. list_add(&tracker_pt->object_list, &instance->track_head);
  1417. list_add(&tracker_pt->tracker_list, &objdb_trackers_head);
  1418. hdb_handle_put (&object_instance_database, object_handle);
  1419. return (res);
  1420. }
  1421. static void object_track_stop(object_key_change_notify_fn_t key_change_notify_fn,
  1422. object_create_notify_fn_t object_create_notify_fn,
  1423. object_destroy_notify_fn_t object_destroy_notify_fn,
  1424. object_reload_notify_fn_t object_reload_notify_fn,
  1425. void * priv_data_pt)
  1426. {
  1427. struct object_instance *instance;
  1428. struct object_tracker * tracker_pt = NULL;
  1429. struct object_tracker * obj_tracker_pt = NULL;
  1430. struct list_head *list, *tmp_list;
  1431. struct list_head *obj_list, *tmp_obj_list;
  1432. int res;
  1433. /* go through the global list and find all the trackers to stop */
  1434. for (list = objdb_trackers_head.next, tmp_list = list->next;
  1435. list != &objdb_trackers_head; list = tmp_list, tmp_list = tmp_list->next) {
  1436. tracker_pt = list_entry (list, struct object_tracker, tracker_list);
  1437. if (tracker_pt && (tracker_pt->data_pt == priv_data_pt) &&
  1438. (tracker_pt->object_create_notify_fn == object_create_notify_fn) &&
  1439. (tracker_pt->object_destroy_notify_fn == object_destroy_notify_fn) &&
  1440. (tracker_pt->object_reload_notify_fn == object_reload_notify_fn) &&
  1441. (tracker_pt->key_change_notify_fn == key_change_notify_fn)) {
  1442. /* get the object & take this tracker off of it's list. */
  1443. res = hdb_handle_get (&object_instance_database,
  1444. tracker_pt->object_handle, (void *)&instance);
  1445. if (res != 0) continue;
  1446. for (obj_list = instance->track_head.next, tmp_obj_list = obj_list->next;
  1447. obj_list != &instance->track_head; obj_list = tmp_obj_list, tmp_obj_list = tmp_obj_list->next) {
  1448. obj_tracker_pt = list_entry (obj_list, struct object_tracker, object_list);
  1449. if (obj_tracker_pt == tracker_pt) {
  1450. /* this is the tracker we are after. */
  1451. list_del(obj_list);
  1452. }
  1453. }
  1454. hdb_handle_put (&object_instance_database, tracker_pt->object_handle);
  1455. /* remove the tracker off of the global list */
  1456. list_del(list);
  1457. free(tracker_pt);
  1458. }
  1459. }
  1460. }
  1461. static int object_dump(hdb_handle_t object_handle,
  1462. FILE *file)
  1463. {
  1464. struct object_instance *instance;
  1465. int res;
  1466. res = hdb_handle_get (&object_instance_database,
  1467. object_handle, (void *)&instance);
  1468. if (res != 0) {
  1469. return (res);
  1470. }
  1471. res = _dump_object(instance, file, -1);
  1472. hdb_handle_put (&object_instance_database, object_handle);
  1473. return (res);
  1474. }
  1475. static int object_write_config(const char **error_string)
  1476. {
  1477. struct config_iface_ver0 **modules;
  1478. int num_modules;
  1479. int i;
  1480. // int res;
  1481. main_get_config_modules(&modules, &num_modules);
  1482. for (i=0; i<num_modules; i++) {
  1483. if (modules[i]->config_writeconfig) {
  1484. /* res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  1485. if (res) {
  1486. return res;
  1487. }*/
  1488. }
  1489. }
  1490. return 0;
  1491. }
  1492. static int object_reload_config(int flush, const char **error_string)
  1493. {
  1494. struct config_iface_ver0 **modules;
  1495. int num_modules;
  1496. int i;
  1497. /* int res = 0; */
  1498. main_get_config_modules(&modules, &num_modules);
  1499. object_reload_notification(OBJDB_RELOAD_NOTIFY_START, flush);
  1500. for (i=0; i<num_modules; i++) {
  1501. if (modules[i]->config_reloadconfig) {
  1502. /* res = modules[i]->config_reloadconfig(&objdb_iface, flush, error_string);
  1503. if (res) {
  1504. object_reload_notification(OBJDB_RELOAD_NOTIFY_FAILED, flush);
  1505. return res;
  1506. } */
  1507. }
  1508. }
  1509. object_reload_notification(OBJDB_RELOAD_NOTIFY_END, flush);
  1510. return 0;
  1511. }
  1512. struct objdb_iface_ver0 objdb_iface = {
  1513. .objdb_init = objdb_init,
  1514. .object_create = object_create,
  1515. .object_priv_set = object_priv_set,
  1516. .object_key_create = object_key_create,
  1517. .object_key_delete = object_key_delete,
  1518. .object_key_replace = object_key_replace,
  1519. .object_destroy = object_destroy,
  1520. .object_valid_set = object_valid_set,
  1521. .object_key_valid_set = object_key_valid_set,
  1522. .object_find_create = object_find_create,
  1523. .object_find_next = object_find_next,
  1524. .object_find_destroy = object_find_destroy,
  1525. .object_key_get = object_key_get,
  1526. .object_key_iter_reset = object_key_iter_reset,
  1527. .object_key_iter = object_key_iter,
  1528. .object_key_iter_from = object_key_iter_from,
  1529. .object_priv_get = object_priv_get,
  1530. .object_parent_get = object_parent_get,
  1531. .object_name_get = object_name_get,
  1532. .object_track_start = object_track_start,
  1533. .object_track_stop = object_track_stop,
  1534. .object_dump = object_dump,
  1535. .object_write_config = object_write_config,
  1536. .object_reload_config = object_reload_config,
  1537. .object_key_increment = object_key_increment,
  1538. .object_key_decrement = object_key_decrement,
  1539. .object_key_create_typed = object_key_create_typed,
  1540. .object_key_get_typed = object_key_get_typed,
  1541. .object_key_iter_typed = object_key_iter_typed,
  1542. .objdb_fini = objdb_fini,
  1543. };
  1544. struct lcr_iface objdb_iface_ver0[1] = {
  1545. {
  1546. .name = "objdb",
  1547. .version = 0,
  1548. .versions_replace = 0,
  1549. .versions_replace_count = 0,
  1550. .dependencies = 0,
  1551. .dependency_count = 0,
  1552. .constructor = NULL,
  1553. .destructor = NULL,
  1554. .interfaces = NULL,
  1555. }
  1556. };
  1557. struct lcr_comp objdb_comp_ver0 = {
  1558. .iface_count = 1,
  1559. .ifaces = objdb_iface_ver0
  1560. };
  1561. #ifdef COROSYNC_SOLARIS
  1562. void corosync_lcr_component_register (void);
  1563. void corosync_lcr_component_register (void) {
  1564. #else
  1565. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  1566. #endif
  1567. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  1568. lcr_component_register (&objdb_comp_ver0);
  1569. }