4
0

objdb.c 45 KB

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