objdb.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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 object_handle,
  173. hdb_handle_t parent_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(object_handle,
  191. parent_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(object_instance->object_handle,
  372. object_instance->parent_handle,
  373. object_instance->object_name,
  374. object_instance->object_name_len);
  375. objdb_unlock();
  376. return (0);
  377. error_put_destroy:
  378. hdb_handle_put (&object_instance_database, *object_handle);
  379. error_destroy:
  380. hdb_handle_destroy (&object_instance_database, *object_handle);
  381. error_object_put:
  382. hdb_handle_put (&object_instance_database, parent_object_handle);
  383. error_exit:
  384. objdb_unlock();
  385. return (-1);
  386. }
  387. static int object_priv_set (
  388. hdb_handle_t object_handle,
  389. void *priv)
  390. {
  391. int res;
  392. struct object_instance *object_instance;
  393. objdb_lock();
  394. res = hdb_handle_get (&object_instance_database,
  395. object_handle, (void *)&object_instance);
  396. if (res != 0) {
  397. goto error_exit;
  398. }
  399. object_instance->priv = priv;
  400. hdb_handle_put (&object_instance_database, object_handle);
  401. objdb_unlock();
  402. return (0);
  403. error_exit:
  404. objdb_unlock();
  405. return (-1);
  406. }
  407. static int object_key_create_typed(
  408. hdb_handle_t object_handle,
  409. const char *key_name,
  410. const void *value,
  411. size_t value_len,
  412. objdb_value_types_t value_type)
  413. {
  414. struct object_instance *instance;
  415. struct object_key *object_key;
  416. unsigned int res;
  417. struct list_head *list;
  418. int found = 0;
  419. int i;
  420. size_t key_len = strlen(key_name);
  421. size_t expected_size;
  422. int test_size_by_type = CS_TRUE;
  423. objdb_lock();
  424. res = hdb_handle_get (&object_instance_database,
  425. object_handle, (void *)&instance);
  426. if (res != 0) {
  427. goto error_exit;
  428. }
  429. switch (value_type) {
  430. case OBJDB_VALUETYPE_INT16:
  431. expected_size = sizeof (int16_t);
  432. break;
  433. case OBJDB_VALUETYPE_UINT16:
  434. expected_size = sizeof (uint16_t);
  435. break;
  436. case OBJDB_VALUETYPE_INT32:
  437. expected_size = sizeof (int32_t);
  438. break;
  439. case OBJDB_VALUETYPE_UINT32:
  440. expected_size = sizeof (uint32_t);
  441. break;
  442. case OBJDB_VALUETYPE_INT64:
  443. expected_size = sizeof (int64_t);
  444. break;
  445. case OBJDB_VALUETYPE_UINT64:
  446. expected_size = sizeof (uint64_t);
  447. break;
  448. case OBJDB_VALUETYPE_FLOAT:
  449. expected_size = sizeof (float);
  450. break;
  451. case OBJDB_VALUETYPE_DOUBLE:
  452. expected_size = sizeof (double);
  453. break;
  454. case OBJDB_VALUETYPE_ANY:
  455. default:
  456. test_size_by_type = CS_FALSE;
  457. break;
  458. }
  459. if (test_size_by_type) {
  460. if (expected_size != value_len) {
  461. //printf ("%s exp:%d != len:%d\n", key_name, expected_size, value_len);
  462. goto error_put;
  463. }
  464. }
  465. /*
  466. * Do validation check if validation is configured for the parent object
  467. */
  468. if (instance->object_key_valid_list_entries) {
  469. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  470. if ((key_len ==
  471. instance->object_key_valid_list[i].key_len) &&
  472. (memcmp (key_name,
  473. instance->object_key_valid_list[i].key_name,
  474. key_len) == 0)) {
  475. found = 1;
  476. break;
  477. }
  478. }
  479. /*
  480. * Item not found in validation list
  481. */
  482. if (found == 0) {
  483. goto error_put;
  484. } else {
  485. if (instance->object_key_valid_list[i].validate_callback) {
  486. res = instance->object_key_valid_list[i].validate_callback (
  487. key_name, key_len, value, value_len);
  488. if (res != 0) {
  489. goto error_put;
  490. }
  491. }
  492. }
  493. }
  494. /* See if it already exists */
  495. found = 0;
  496. for (list = instance->key_head.next;
  497. list != &instance->key_head; list = list->next) {
  498. object_key = list_entry (list, struct object_key, list);
  499. if ((object_key->key_len == key_len) &&
  500. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  501. found = 1;
  502. break;
  503. }
  504. }
  505. if (found) {
  506. free(object_key->value);
  507. }
  508. else {
  509. object_key = malloc (sizeof (struct object_key));
  510. if (object_key == 0) {
  511. goto error_put;
  512. }
  513. object_key->key_name = malloc (key_len + 1);
  514. if (object_key->key_name == 0) {
  515. goto error_put_object;
  516. }
  517. memcpy (object_key->key_name, key_name, key_len + 1);
  518. list_init (&object_key->list);
  519. list_add_tail (&object_key->list, &instance->key_head);
  520. }
  521. object_key->value = malloc (value_len);
  522. if (object_key->value == 0) {
  523. goto error_put_key;
  524. }
  525. memcpy (object_key->value, value, value_len);
  526. object_key->key_len = key_len;
  527. object_key->value_len = value_len;
  528. object_key->value_type = value_type;
  529. object_key_changed_notification(object_handle, key_name, key_len,
  530. value, value_len, OBJECT_KEY_CREATED);
  531. hdb_handle_put (&object_instance_database, object_handle);
  532. objdb_unlock();
  533. return (0);
  534. error_put_key:
  535. free (object_key->key_name);
  536. error_put_object:
  537. free (object_key);
  538. error_put:
  539. hdb_handle_put (&object_instance_database, object_handle);
  540. error_exit:
  541. objdb_unlock();
  542. return (-1);
  543. }
  544. static int object_key_create (
  545. hdb_handle_t object_handle,
  546. const void *key_name,
  547. size_t key_len,
  548. const void *value,
  549. size_t value_len)
  550. {
  551. char *key_name_terminated = NULL;
  552. char *key_name_str = (char*)key_name;
  553. int ret;
  554. if (key_name_str[key_len-1] != '\0') {
  555. key_name_terminated = malloc (key_len + 1);
  556. memcpy (key_name_terminated, key_name, key_len);
  557. key_name_terminated[key_len] = '\0';
  558. key_name_str = key_name_terminated;
  559. }
  560. ret = object_key_create_typed (object_handle, key_name_str,
  561. value, value_len, OBJDB_VALUETYPE_ANY);
  562. if (key_name_terminated) {
  563. free (key_name_terminated);
  564. }
  565. return ret;
  566. }
  567. static int _clear_object(struct object_instance *instance)
  568. {
  569. struct list_head *list;
  570. int res;
  571. struct object_instance *find_instance = NULL;
  572. struct object_key *object_key = NULL;
  573. for (list = instance->key_head.next;
  574. list != &instance->key_head; ) {
  575. object_key = list_entry (list, struct object_key,
  576. list);
  577. list = list->next;
  578. list_del(&object_key->list);
  579. free(object_key->key_name);
  580. free(object_key->value);
  581. free(object_key);
  582. }
  583. for (list = instance->child_head.next;
  584. list != &instance->child_head; ) {
  585. find_instance = list_entry (list, struct object_instance,
  586. child_list);
  587. res = _clear_object(find_instance);
  588. if (res)
  589. return res;
  590. list = list->next;
  591. list_del(&find_instance->child_list);
  592. free(find_instance->object_name);
  593. free(find_instance);
  594. }
  595. return 0;
  596. }
  597. static int object_destroy (
  598. hdb_handle_t object_handle)
  599. {
  600. struct object_instance *instance;
  601. unsigned int res;
  602. objdb_lock();
  603. res = hdb_handle_get (&object_instance_database,
  604. object_handle, (void *)&instance);
  605. if (res != 0) {
  606. objdb_unlock();
  607. return (res);
  608. }
  609. object_pre_deletion_notification(object_handle,
  610. instance->parent_handle,
  611. instance->object_name,
  612. instance->object_name_len);
  613. /* Recursively clear sub-objects & keys */
  614. res = _clear_object(instance);
  615. list_del(&instance->child_list);
  616. free(instance->object_name);
  617. hdb_handle_put (&object_instance_database, object_handle);
  618. hdb_handle_destroy (&object_instance_database, object_handle);
  619. objdb_unlock();
  620. return (res);
  621. }
  622. static int object_valid_set (
  623. hdb_handle_t object_handle,
  624. struct object_valid *object_valid_list,
  625. size_t object_valid_list_entries)
  626. {
  627. struct object_instance *instance;
  628. unsigned int res;
  629. objdb_lock();
  630. res = hdb_handle_get (&object_instance_database,
  631. object_handle, (void *)&instance);
  632. if (res != 0) {
  633. goto error_exit;
  634. }
  635. instance->object_valid_list = object_valid_list;
  636. instance->object_valid_list_entries = object_valid_list_entries;
  637. hdb_handle_put (&object_instance_database, object_handle);
  638. objdb_unlock();
  639. return (0);
  640. error_exit:
  641. objdb_unlock();
  642. return (-1);
  643. }
  644. static int object_key_valid_set (
  645. hdb_handle_t object_handle,
  646. struct object_key_valid *object_key_valid_list,
  647. size_t object_key_valid_list_entries)
  648. {
  649. struct object_instance *instance;
  650. unsigned int res;
  651. objdb_lock();
  652. res = hdb_handle_get (&object_instance_database,
  653. object_handle, (void *)&instance);
  654. if (res != 0) {
  655. goto error_exit;
  656. }
  657. instance->object_key_valid_list = object_key_valid_list;
  658. instance->object_key_valid_list_entries = object_key_valid_list_entries;
  659. hdb_handle_put (&object_instance_database, object_handle);
  660. objdb_unlock();
  661. return (0);
  662. error_exit:
  663. objdb_unlock();
  664. return (-1);
  665. }
  666. /*
  667. * object db reading
  668. */
  669. static int object_find_create (
  670. hdb_handle_t object_handle,
  671. const void *object_name,
  672. size_t object_len,
  673. hdb_handle_t *object_find_handle)
  674. {
  675. unsigned int res;
  676. struct object_instance *object_instance;
  677. struct object_find_instance *object_find_instance;
  678. objdb_lock();
  679. res = hdb_handle_get (&object_instance_database,
  680. object_handle, (void *)&object_instance);
  681. if (res != 0) {
  682. goto error_exit;
  683. }
  684. res = hdb_handle_create (&object_find_instance_database,
  685. sizeof (struct object_find_instance), object_find_handle);
  686. if (res != 0) {
  687. goto error_put;
  688. }
  689. res = hdb_handle_get (&object_find_instance_database,
  690. *object_find_handle, (void *)&object_find_instance);
  691. if (res != 0) {
  692. goto error_destroy;
  693. }
  694. object_find_instance->find_child_list = &object_instance->child_head;
  695. object_find_instance->child_head = &object_instance->child_head;
  696. object_find_instance->object_name = (char *)object_name;
  697. object_find_instance->object_len = object_len;
  698. hdb_handle_put (&object_instance_database, object_handle);
  699. hdb_handle_put (&object_find_instance_database, *object_find_handle);
  700. objdb_unlock();
  701. return (0);
  702. error_destroy:
  703. hdb_handle_destroy (&object_instance_database, *object_find_handle);
  704. error_put:
  705. hdb_handle_put (&object_instance_database, object_handle);
  706. error_exit:
  707. objdb_unlock();
  708. return (-1);
  709. }
  710. static int object_find_next (
  711. hdb_handle_t object_find_handle,
  712. hdb_handle_t *object_handle)
  713. {
  714. unsigned int res;
  715. struct object_find_instance *object_find_instance;
  716. struct object_instance *object_instance = NULL;
  717. struct list_head *list;
  718. unsigned int found = 0;
  719. objdb_lock();
  720. res = hdb_handle_get (&object_find_instance_database,
  721. object_find_handle, (void *)&object_find_instance);
  722. if (res != 0) {
  723. goto error_exit;
  724. }
  725. res = -1;
  726. for (list = object_find_instance->find_child_list->next;
  727. list != object_find_instance->child_head; list = list->next) {
  728. object_instance = list_entry (list, struct object_instance,
  729. child_list);
  730. if (object_find_instance->object_len == 0 ||
  731. ((object_instance->object_name_len ==
  732. object_find_instance->object_len) &&
  733. (memcmp (object_instance->object_name,
  734. object_find_instance->object_name,
  735. object_find_instance->object_len) == 0))) {
  736. found = 1;
  737. break;
  738. }
  739. }
  740. object_find_instance->find_child_list = list;
  741. hdb_handle_put (&object_find_instance_database, object_find_handle);
  742. if (found) {
  743. *object_handle = object_instance->object_handle;
  744. res = 0;
  745. }
  746. objdb_unlock();
  747. return (res);
  748. error_exit:
  749. objdb_unlock();
  750. return (-1);
  751. }
  752. static int object_find_destroy (
  753. hdb_handle_t object_find_handle)
  754. {
  755. struct object_find_instance *object_find_instance;
  756. unsigned int res;
  757. objdb_lock();
  758. res = hdb_handle_get (&object_find_instance_database,
  759. object_find_handle, (void *)&object_find_instance);
  760. if (res != 0) {
  761. goto error_exit;
  762. }
  763. hdb_handle_put(&object_find_instance_database, object_find_handle);
  764. hdb_handle_destroy(&object_find_instance_database, object_find_handle);
  765. objdb_unlock();
  766. return (0);
  767. error_exit:
  768. objdb_unlock();
  769. return (-1);
  770. }
  771. static int object_key_get_typed (
  772. hdb_handle_t object_handle,
  773. const char *key_name,
  774. void **value,
  775. size_t *value_len,
  776. objdb_value_types_t * type)
  777. {
  778. unsigned int res = 0;
  779. struct object_instance *instance;
  780. struct object_key *object_key = NULL;
  781. struct list_head *list;
  782. int found = 0;
  783. size_t key_len = strlen(key_name);
  784. objdb_lock();
  785. res = hdb_handle_get (&object_instance_database,
  786. object_handle, (void *)&instance);
  787. if (res != 0) {
  788. goto error_exit;
  789. }
  790. for (list = instance->key_head.next;
  791. list != &instance->key_head; list = list->next) {
  792. object_key = list_entry (list, struct object_key, list);
  793. if ((object_key->key_len == key_len) &&
  794. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  795. found = 1;
  796. break;
  797. }
  798. }
  799. if (found) {
  800. *value = object_key->value;
  801. if (value_len) {
  802. *value_len = object_key->value_len;
  803. }
  804. *type = object_key->value_type;
  805. }
  806. else {
  807. res = -1;
  808. }
  809. hdb_handle_put (&object_instance_database, object_handle);
  810. objdb_unlock();
  811. return (res);
  812. error_exit:
  813. objdb_unlock();
  814. return (-1);
  815. }
  816. static int object_key_get (
  817. hdb_handle_t object_handle,
  818. const void *key_name,
  819. size_t key_len,
  820. void **value,
  821. size_t *value_len)
  822. {
  823. objdb_value_types_t t;
  824. int ret;
  825. char *key_name_str = (char*)key_name;
  826. char *key_name_terminated = NULL;
  827. if (key_name_str[key_len-1] != '\0') {
  828. key_name_terminated = malloc (key_len + 1);
  829. memcpy (key_name_terminated, key_name, key_len);
  830. key_name_terminated[key_len] = '\0';
  831. key_name_str = key_name_terminated;
  832. }
  833. ret = object_key_get_typed(object_handle,
  834. key_name_str,
  835. value, value_len, &t);
  836. if (key_name_terminated) {
  837. free (key_name_terminated);
  838. }
  839. return ret;
  840. }
  841. static int object_key_increment (
  842. hdb_handle_t object_handle,
  843. const void *key_name,
  844. size_t key_len,
  845. unsigned int *value)
  846. {
  847. unsigned int res = 0;
  848. struct object_instance *instance;
  849. struct object_key *object_key = NULL;
  850. struct list_head *list;
  851. int found = 0;
  852. objdb_lock();
  853. res = hdb_handle_get (&object_instance_database,
  854. object_handle, (void *)&instance);
  855. if (res != 0) {
  856. goto error_exit;
  857. }
  858. for (list = instance->key_head.next;
  859. list != &instance->key_head; list = list->next) {
  860. object_key = list_entry (list, struct object_key, list);
  861. if ((object_key->key_len == key_len) &&
  862. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  863. found = 1;
  864. break;
  865. }
  866. }
  867. if (found) {
  868. switch (object_key->value_type) {
  869. case OBJDB_VALUETYPE_INT16:
  870. (*(int16_t *)object_key->value)++;
  871. break;
  872. case OBJDB_VALUETYPE_UINT16:
  873. (*(uint16_t *)object_key->value)++;
  874. break;
  875. case OBJDB_VALUETYPE_INT32:
  876. (*(int32_t *)object_key->value)++;
  877. break;
  878. case OBJDB_VALUETYPE_UINT32:
  879. (*(uint32_t *)object_key->value)++;
  880. break;
  881. case OBJDB_VALUETYPE_INT64:
  882. (*(int64_t *)object_key->value)++;
  883. break;
  884. case OBJDB_VALUETYPE_UINT64:
  885. (*(uint64_t *)object_key->value)++;
  886. break;
  887. case OBJDB_VALUETYPE_ANY:
  888. /* for backwards compatibilty */
  889. if (object_key->value_len == sizeof(int)) {
  890. (*(int *)object_key->value)++;
  891. }
  892. else {
  893. res = -1;
  894. }
  895. break;
  896. default:
  897. res = -1;
  898. break;
  899. }
  900. if (res == 0) {
  901. /* nasty, not sure why we need to return this typed
  902. * instead of void* */
  903. *value = *(int *)object_key->value;
  904. }
  905. }
  906. else {
  907. res = -1;
  908. }
  909. hdb_handle_put (&object_instance_database, object_handle);
  910. if (res == 0) {
  911. object_key_changed_notification (object_handle, key_name, key_len,
  912. object_key->value, object_key->value_len, OBJECT_KEY_REPLACED);
  913. }
  914. objdb_unlock();
  915. return (res);
  916. error_exit:
  917. objdb_unlock();
  918. return (-1);
  919. }
  920. static int object_key_decrement (
  921. hdb_handle_t object_handle,
  922. const void *key_name,
  923. size_t key_len,
  924. unsigned int *value)
  925. {
  926. unsigned int res = 0;
  927. struct object_instance *instance;
  928. struct object_key *object_key = NULL;
  929. struct list_head *list;
  930. int found = 0;
  931. objdb_lock();
  932. res = hdb_handle_get (&object_instance_database,
  933. object_handle, (void *)&instance);
  934. if (res != 0) {
  935. goto error_exit;
  936. }
  937. for (list = instance->key_head.next;
  938. list != &instance->key_head; list = list->next) {
  939. object_key = list_entry (list, struct object_key, list);
  940. if ((object_key->key_len == key_len) &&
  941. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  942. found = 1;
  943. break;
  944. }
  945. }
  946. if (found) {
  947. switch (object_key->value_type) {
  948. case OBJDB_VALUETYPE_INT16:
  949. (*(int16_t *)object_key->value)--;
  950. break;
  951. case OBJDB_VALUETYPE_UINT16:
  952. (*(uint16_t *)object_key->value)--;
  953. break;
  954. case OBJDB_VALUETYPE_INT32:
  955. (*(int32_t *)object_key->value)--;
  956. break;
  957. case OBJDB_VALUETYPE_UINT32:
  958. (*(uint32_t *)object_key->value)--;
  959. break;
  960. case OBJDB_VALUETYPE_INT64:
  961. (*(int64_t *)object_key->value)--;
  962. break;
  963. case OBJDB_VALUETYPE_UINT64:
  964. (*(uint64_t *)object_key->value)--;
  965. break;
  966. case OBJDB_VALUETYPE_ANY:
  967. /* for backwards compatibilty */
  968. if (object_key->value_len == sizeof(int)) {
  969. (*(int *)object_key->value)--;
  970. }
  971. else {
  972. res = -1;
  973. }
  974. break;
  975. default:
  976. res = -1;
  977. break;
  978. }
  979. if (res == 0) {
  980. /* nasty, not sure why we need to return this typed
  981. * instead of void* */
  982. *value = *(int *)object_key->value;
  983. }
  984. }
  985. else {
  986. res = -1;
  987. }
  988. hdb_handle_put (&object_instance_database, object_handle);
  989. if (res == 0) {
  990. object_key_changed_notification (object_handle, key_name, key_len,
  991. object_key->value, object_key->value_len, OBJECT_KEY_REPLACED);
  992. }
  993. objdb_unlock();
  994. return (res);
  995. error_exit:
  996. objdb_unlock();
  997. return (-1);
  998. }
  999. static int object_key_delete (
  1000. hdb_handle_t object_handle,
  1001. const void *key_name,
  1002. size_t key_len)
  1003. {
  1004. unsigned int res;
  1005. int ret = 0;
  1006. struct object_instance *instance;
  1007. struct object_key *object_key = NULL;
  1008. struct list_head *list;
  1009. int found = 0;
  1010. objdb_lock();
  1011. res = hdb_handle_get (&object_instance_database,
  1012. object_handle, (void *)&instance);
  1013. if (res != 0) {
  1014. goto error_exit;
  1015. }
  1016. for (list = instance->key_head.next;
  1017. list != &instance->key_head; list = list->next) {
  1018. object_key = list_entry (list, struct object_key, list);
  1019. if ((object_key->key_len == key_len) &&
  1020. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  1021. found = 1;
  1022. break;
  1023. }
  1024. }
  1025. if (found) {
  1026. list_del(&object_key->list);
  1027. free(object_key->key_name);
  1028. free(object_key->value);
  1029. free(object_key);
  1030. }
  1031. else {
  1032. ret = -1;
  1033. errno = ENOENT;
  1034. }
  1035. hdb_handle_put (&object_instance_database, object_handle);
  1036. if (ret == 0) {
  1037. object_key_changed_notification(object_handle, key_name, key_len,
  1038. NULL, 0, OBJECT_KEY_DELETED);
  1039. }
  1040. objdb_unlock();
  1041. return (ret);
  1042. error_exit:
  1043. objdb_unlock();
  1044. return (-1);
  1045. }
  1046. static int object_key_replace (
  1047. hdb_handle_t object_handle,
  1048. const void *key_name,
  1049. size_t key_len,
  1050. const void *new_value,
  1051. size_t new_value_len)
  1052. {
  1053. unsigned int res;
  1054. int ret = 0;
  1055. struct object_instance *instance;
  1056. struct object_key *object_key = NULL;
  1057. struct list_head *list;
  1058. int found = 0;
  1059. int value_changed = 0;
  1060. objdb_lock();
  1061. res = hdb_handle_get (&object_instance_database,
  1062. object_handle, (void *)&instance);
  1063. if (res != 0) {
  1064. goto error_exit;
  1065. }
  1066. for (list = instance->key_head.next;
  1067. list != &instance->key_head; list = list->next) {
  1068. object_key = list_entry (list, struct object_key, list);
  1069. if ((object_key->key_len == key_len) &&
  1070. (memcmp (object_key->key_name, key_name, key_len) == 0)) {
  1071. found = 1;
  1072. break;
  1073. }
  1074. }
  1075. if (found) {
  1076. int i;
  1077. int found_validator = 0;
  1078. /*
  1079. * Do validation check if validation is configured for the parent object
  1080. */
  1081. if (instance->object_key_valid_list_entries) {
  1082. for (i = 0; i < instance->object_key_valid_list_entries; i++) {
  1083. if ((key_len ==
  1084. instance->object_key_valid_list[i].key_len) &&
  1085. (memcmp (key_name,
  1086. instance->object_key_valid_list[i].key_name,
  1087. key_len) == 0)) {
  1088. found_validator = 1;
  1089. break;
  1090. }
  1091. }
  1092. /*
  1093. * Item not found in validation list
  1094. */
  1095. if (found_validator == 0) {
  1096. goto error_put;
  1097. } else {
  1098. if (instance->object_key_valid_list[i].validate_callback) {
  1099. res = instance->object_key_valid_list[i].validate_callback (
  1100. key_name, key_len, new_value, new_value_len);
  1101. if (res != 0) {
  1102. goto error_put;
  1103. }
  1104. }
  1105. }
  1106. }
  1107. if (new_value_len != object_key->value_len) {
  1108. void *replacement_value;
  1109. replacement_value = malloc(new_value_len);
  1110. if (!replacement_value)
  1111. goto error_exit;
  1112. free(object_key->value);
  1113. object_key->value = replacement_value;
  1114. memset (object_key->value, 0, new_value_len);
  1115. object_key->value_len = new_value_len;
  1116. }
  1117. if (memcmp (object_key->value, new_value, new_value_len) == 0) {
  1118. value_changed = 0;
  1119. }
  1120. else {
  1121. memcpy(object_key->value, new_value, new_value_len);
  1122. object_key->value_len = new_value_len;
  1123. value_changed = 1;
  1124. }
  1125. }
  1126. else {
  1127. ret = -1;
  1128. errno = ENOENT;
  1129. }
  1130. hdb_handle_put (&object_instance_database, object_handle);
  1131. if (ret == 0 && value_changed) {
  1132. object_key_changed_notification (object_handle, key_name, key_len,
  1133. new_value, new_value_len, OBJECT_KEY_REPLACED);
  1134. }
  1135. objdb_unlock();
  1136. return (ret);
  1137. error_put:
  1138. hdb_handle_put (&object_instance_database, object_handle);
  1139. error_exit:
  1140. objdb_unlock();
  1141. return (-1);
  1142. }
  1143. static int object_priv_get (
  1144. hdb_handle_t object_handle,
  1145. void **priv)
  1146. {
  1147. int res;
  1148. struct object_instance *object_instance;
  1149. objdb_unlock();
  1150. res = hdb_handle_get (&object_instance_database,
  1151. object_handle, (void *)&object_instance);
  1152. if (res != 0) {
  1153. goto error_exit;
  1154. }
  1155. *priv = object_instance->priv;
  1156. hdb_handle_put (&object_instance_database, object_handle);
  1157. objdb_unlock();
  1158. return (0);
  1159. error_exit:
  1160. objdb_unlock();
  1161. return (-1);
  1162. }
  1163. static int _dump_object(struct object_instance *instance, FILE *file, int depth)
  1164. {
  1165. struct list_head *list;
  1166. int res;
  1167. int i;
  1168. struct object_instance *find_instance = NULL;
  1169. struct object_key *object_key = NULL;
  1170. char stringbuf1[1024];
  1171. char stringbuf2[1024];
  1172. memcpy(stringbuf1, instance->object_name, instance->object_name_len);
  1173. stringbuf1[instance->object_name_len] = '\0';
  1174. for (i=0; i<depth; i++)
  1175. fprintf(file, " ");
  1176. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1177. fprintf(file, "%s {\n", stringbuf1);
  1178. for (list = instance->key_head.next;
  1179. list != &instance->key_head; list = list->next) {
  1180. object_key = list_entry (list, struct object_key,
  1181. list);
  1182. memcpy(stringbuf1, object_key->key_name, object_key->key_len);
  1183. stringbuf1[object_key->key_len] = '\0';
  1184. switch (object_key->value_type) {
  1185. case OBJDB_VALUETYPE_INT16:
  1186. snprintf (stringbuf2, sizeof(int), "%hd",
  1187. *(unsigned int*)object_key->value);
  1188. break;
  1189. case OBJDB_VALUETYPE_UINT16:
  1190. snprintf (stringbuf2, sizeof(int), "%hu",
  1191. *(unsigned int*)object_key->value);
  1192. break;
  1193. case OBJDB_VALUETYPE_INT32:
  1194. snprintf (stringbuf2, sizeof(int), "%d",
  1195. *(int*)object_key->value);
  1196. break;
  1197. case OBJDB_VALUETYPE_UINT32:
  1198. snprintf (stringbuf2, sizeof(int), "%u",
  1199. *(unsigned int*)object_key->value);
  1200. break;
  1201. case OBJDB_VALUETYPE_INT64:
  1202. snprintf (stringbuf2, sizeof(int), "%ld",
  1203. *(long int*)object_key->value);
  1204. break;
  1205. case OBJDB_VALUETYPE_UINT64:
  1206. snprintf (stringbuf2, sizeof(int), "%lu",
  1207. *(unsigned long int*)object_key->value);
  1208. break;
  1209. default:
  1210. case OBJDB_VALUETYPE_STRING:
  1211. case OBJDB_VALUETYPE_ANY:
  1212. memcpy(stringbuf2, object_key->value, object_key->value_len);
  1213. stringbuf2[object_key->value_len] = '\0';
  1214. break;
  1215. }
  1216. for (i=0; i<depth+1; i++)
  1217. fprintf(file, " ");
  1218. fprintf(file, "%s: %s\n", stringbuf1, stringbuf2);
  1219. }
  1220. for (list = instance->child_head.next;
  1221. list != &instance->child_head; list = list->next) {
  1222. find_instance = list_entry (list, struct object_instance,
  1223. child_list);
  1224. res = _dump_object(find_instance, file, depth+1);
  1225. if (res)
  1226. return res;
  1227. }
  1228. for (i=0; i<depth; i++)
  1229. fprintf(file, " ");
  1230. if (instance->object_handle != OBJECT_PARENT_HANDLE)
  1231. fprintf(file, "}\n");
  1232. return 0;
  1233. }
  1234. static int object_key_iter_reset(hdb_handle_t object_handle)
  1235. {
  1236. unsigned int res;
  1237. struct object_instance *instance;
  1238. objdb_lock();
  1239. res = hdb_handle_get (&object_instance_database,
  1240. object_handle, (void *)&instance);
  1241. if (res != 0) {
  1242. goto error_exit;
  1243. }
  1244. instance->iter_key_list = &instance->key_head;
  1245. hdb_handle_put (&object_instance_database, object_handle);
  1246. objdb_unlock();
  1247. return (0);
  1248. error_exit:
  1249. objdb_unlock();
  1250. return (-1);
  1251. }
  1252. static int object_key_iter_typed (hdb_handle_t parent_object_handle,
  1253. char **key_name,
  1254. void **value,
  1255. size_t *value_len,
  1256. objdb_value_types_t *type)
  1257. {
  1258. unsigned int res;
  1259. struct object_instance *instance;
  1260. struct object_key *find_key = NULL;
  1261. struct list_head *list;
  1262. unsigned int found = 0;
  1263. objdb_lock();
  1264. res = hdb_handle_get (&object_instance_database,
  1265. parent_object_handle, (void *)&instance);
  1266. if (res != 0) {
  1267. goto error_exit;
  1268. }
  1269. res = -ENOENT;
  1270. list = instance->iter_key_list->next;
  1271. if (list != &instance->key_head) {
  1272. find_key = list_entry (list, struct object_key, list);
  1273. found = 1;
  1274. }
  1275. instance->iter_key_list = list;
  1276. if (found) {
  1277. *key_name = find_key->key_name;
  1278. *value = find_key->value;
  1279. *type = find_key->value_type;
  1280. if (value_len)
  1281. *value_len = find_key->value_len;
  1282. res = 0;
  1283. }
  1284. else {
  1285. res = -1;
  1286. }
  1287. hdb_handle_put (&object_instance_database, parent_object_handle);
  1288. objdb_unlock();
  1289. return (res);
  1290. error_exit:
  1291. objdb_unlock();
  1292. return (-1);
  1293. }
  1294. static int object_key_iter(hdb_handle_t parent_object_handle,
  1295. void **key_name,
  1296. size_t *key_len,
  1297. void **value,
  1298. size_t *value_len)
  1299. {
  1300. objdb_value_types_t t;
  1301. int ret;
  1302. char *str;
  1303. ret = object_key_iter_typed (parent_object_handle,
  1304. (char**)key_name, value, value_len, &t);
  1305. if (!ret) {
  1306. str = *key_name;
  1307. *key_len = strlen(str);
  1308. }
  1309. return ret;
  1310. }
  1311. static int object_key_iter_from(hdb_handle_t parent_object_handle,
  1312. hdb_handle_t start_pos,
  1313. void **key_name,
  1314. size_t *key_len,
  1315. void **value,
  1316. size_t *value_len)
  1317. {
  1318. unsigned int pos = 0;
  1319. unsigned int res;
  1320. struct object_instance *instance;
  1321. struct object_key *find_key = NULL;
  1322. struct list_head *list;
  1323. unsigned int found = 0;
  1324. objdb_lock();
  1325. res = hdb_handle_get (&object_instance_database,
  1326. parent_object_handle, (void *)&instance);
  1327. if (res != 0) {
  1328. goto error_exit;
  1329. }
  1330. res = -ENOENT;
  1331. for (list = instance->key_head.next;
  1332. list != &instance->key_head; list = list->next) {
  1333. find_key = list_entry (list, struct object_key, list);
  1334. if (pos++ == start_pos) {
  1335. found = 1;
  1336. break;
  1337. }
  1338. }
  1339. if (found) {
  1340. *key_name = find_key->key_name;
  1341. if (key_len)
  1342. *key_len = find_key->key_len;
  1343. *value = find_key->value;
  1344. if (value_len)
  1345. *value_len = find_key->value_len;
  1346. res = 0;
  1347. }
  1348. else {
  1349. res = -1;
  1350. }
  1351. hdb_handle_put (&object_instance_database, parent_object_handle);
  1352. objdb_unlock();
  1353. return (res);
  1354. error_exit:
  1355. objdb_unlock();
  1356. return (-1);
  1357. }
  1358. static int object_parent_get(hdb_handle_t object_handle,
  1359. hdb_handle_t *parent_handle)
  1360. {
  1361. struct object_instance *instance;
  1362. unsigned int res;
  1363. objdb_lock();
  1364. res = hdb_handle_get (&object_instance_database,
  1365. object_handle, (void *)&instance);
  1366. if (res != 0) {
  1367. objdb_unlock();
  1368. return (res);
  1369. }
  1370. if (object_handle == OBJECT_PARENT_HANDLE)
  1371. *parent_handle = 0;
  1372. else
  1373. *parent_handle = instance->parent_handle;
  1374. hdb_handle_put (&object_instance_database, object_handle);
  1375. objdb_unlock();
  1376. return (0);
  1377. }
  1378. static int object_name_get(hdb_handle_t object_handle,
  1379. char *object_name,
  1380. size_t *object_name_len)
  1381. {
  1382. struct object_instance *instance;
  1383. unsigned int res;
  1384. objdb_lock();
  1385. res = hdb_handle_get (&object_instance_database,
  1386. object_handle, (void *)&instance);
  1387. if (res != 0) {
  1388. objdb_unlock();
  1389. return (res);
  1390. }
  1391. memcpy(object_name, instance->object_name, instance->object_name_len);
  1392. *object_name_len = instance->object_name_len;
  1393. hdb_handle_put (&object_instance_database, object_handle);
  1394. objdb_unlock();
  1395. return (0);
  1396. }
  1397. static int object_track_start(hdb_handle_t object_handle,
  1398. object_track_depth_t depth,
  1399. object_key_change_notify_fn_t key_change_notify_fn,
  1400. object_create_notify_fn_t object_create_notify_fn,
  1401. object_destroy_notify_fn_t object_destroy_notify_fn,
  1402. object_reload_notify_fn_t object_reload_notify_fn,
  1403. void * priv_data_pt)
  1404. {
  1405. struct object_instance *instance;
  1406. unsigned int res;
  1407. struct object_tracker * tracker_pt;
  1408. res = hdb_handle_get (&object_instance_database,
  1409. object_handle, (void *)&instance);
  1410. if (res != 0) {
  1411. return (res);
  1412. }
  1413. tracker_pt = malloc(sizeof(struct object_tracker));
  1414. tracker_pt->depth = depth;
  1415. tracker_pt->object_handle = object_handle;
  1416. tracker_pt->key_change_notify_fn = key_change_notify_fn;
  1417. tracker_pt->object_create_notify_fn = object_create_notify_fn;
  1418. tracker_pt->object_destroy_notify_fn = object_destroy_notify_fn;
  1419. tracker_pt->object_reload_notify_fn = object_reload_notify_fn;
  1420. tracker_pt->data_pt = priv_data_pt;
  1421. list_init(&tracker_pt->object_list);
  1422. list_init(&tracker_pt->tracker_list);
  1423. list_add(&tracker_pt->object_list, &instance->track_head);
  1424. list_add(&tracker_pt->tracker_list, &objdb_trackers_head);
  1425. hdb_handle_put (&object_instance_database, object_handle);
  1426. return (res);
  1427. }
  1428. static void object_track_stop(object_key_change_notify_fn_t key_change_notify_fn,
  1429. object_create_notify_fn_t object_create_notify_fn,
  1430. object_destroy_notify_fn_t object_destroy_notify_fn,
  1431. object_reload_notify_fn_t object_reload_notify_fn,
  1432. void * priv_data_pt)
  1433. {
  1434. struct object_instance *instance;
  1435. struct object_tracker * tracker_pt = NULL;
  1436. struct object_tracker * obj_tracker_pt = NULL;
  1437. struct list_head *list, *tmp_list;
  1438. struct list_head *obj_list, *tmp_obj_list;
  1439. unsigned int res;
  1440. /* go through the global list and find all the trackers to stop */
  1441. for (list = objdb_trackers_head.next, tmp_list = list->next;
  1442. list != &objdb_trackers_head; list = tmp_list, tmp_list = tmp_list->next) {
  1443. tracker_pt = list_entry (list, struct object_tracker, tracker_list);
  1444. if (tracker_pt && (tracker_pt->data_pt == priv_data_pt) &&
  1445. (tracker_pt->object_create_notify_fn == object_create_notify_fn) &&
  1446. (tracker_pt->object_destroy_notify_fn == object_destroy_notify_fn) &&
  1447. (tracker_pt->object_reload_notify_fn == object_reload_notify_fn) &&
  1448. (tracker_pt->key_change_notify_fn == key_change_notify_fn)) {
  1449. /* get the object & take this tracker off of it's list. */
  1450. res = hdb_handle_get (&object_instance_database,
  1451. tracker_pt->object_handle, (void *)&instance);
  1452. if (res != 0) continue;
  1453. for (obj_list = instance->track_head.next, tmp_obj_list = obj_list->next;
  1454. obj_list != &instance->track_head; obj_list = tmp_obj_list, tmp_obj_list = tmp_obj_list->next) {
  1455. obj_tracker_pt = list_entry (obj_list, struct object_tracker, object_list);
  1456. if (obj_tracker_pt == tracker_pt) {
  1457. /* this is the tracker we are after. */
  1458. list_del(obj_list);
  1459. }
  1460. }
  1461. hdb_handle_put (&object_instance_database, tracker_pt->object_handle);
  1462. /* remove the tracker off of the global list */
  1463. list_del(list);
  1464. free(tracker_pt);
  1465. }
  1466. }
  1467. }
  1468. static int object_dump(hdb_handle_t object_handle,
  1469. FILE *file)
  1470. {
  1471. struct object_instance *instance;
  1472. unsigned int res;
  1473. objdb_lock();
  1474. res = hdb_handle_get (&object_instance_database,
  1475. object_handle, (void *)&instance);
  1476. if (res != 0) {
  1477. objdb_unlock();
  1478. return (res);
  1479. }
  1480. res = _dump_object(instance, file, -1);
  1481. hdb_handle_put (&object_instance_database, object_handle);
  1482. objdb_unlock();
  1483. return (res);
  1484. }
  1485. static int object_write_config(const char **error_string)
  1486. {
  1487. struct config_iface_ver0 **modules;
  1488. int num_modules;
  1489. int i;
  1490. int res;
  1491. main_get_config_modules(&modules, &num_modules);
  1492. objdb_lock();
  1493. for (i=0; i<num_modules; i++) {
  1494. if (modules[i]->config_writeconfig) {
  1495. res = modules[i]->config_writeconfig(&objdb_iface, error_string);
  1496. if (res) {
  1497. objdb_unlock();
  1498. return res;
  1499. }
  1500. }
  1501. }
  1502. objdb_unlock();
  1503. return 0;
  1504. }
  1505. static int object_reload_config(int flush, const char **error_string)
  1506. {
  1507. struct config_iface_ver0 **modules;
  1508. int num_modules;
  1509. int i;
  1510. int res;
  1511. main_get_config_modules(&modules, &num_modules);
  1512. object_reload_notification(OBJDB_RELOAD_NOTIFY_START, flush);
  1513. objdb_lock();
  1514. for (i=0; i<num_modules; i++) {
  1515. if (modules[i]->config_reloadconfig) {
  1516. res = modules[i]->config_reloadconfig(&objdb_iface, flush, error_string);
  1517. if (res) {
  1518. object_reload_notification(OBJDB_RELOAD_NOTIFY_FAILED, flush);
  1519. objdb_unlock();
  1520. return res;
  1521. }
  1522. }
  1523. }
  1524. objdb_unlock();
  1525. object_reload_notification(OBJDB_RELOAD_NOTIFY_END, flush);
  1526. return 0;
  1527. }
  1528. struct objdb_iface_ver0 objdb_iface = {
  1529. .objdb_init = objdb_init,
  1530. .object_create = object_create,
  1531. .object_priv_set = object_priv_set,
  1532. .object_key_create = object_key_create,
  1533. .object_key_delete = object_key_delete,
  1534. .object_key_replace = object_key_replace,
  1535. .object_destroy = object_destroy,
  1536. .object_valid_set = object_valid_set,
  1537. .object_key_valid_set = object_key_valid_set,
  1538. .object_find_create = object_find_create,
  1539. .object_find_next = object_find_next,
  1540. .object_find_destroy = object_find_destroy,
  1541. .object_key_get = object_key_get,
  1542. .object_key_iter_reset = object_key_iter_reset,
  1543. .object_key_iter = object_key_iter,
  1544. .object_key_iter_from = object_key_iter_from,
  1545. .object_priv_get = object_priv_get,
  1546. .object_parent_get = object_parent_get,
  1547. .object_name_get = object_name_get,
  1548. .object_track_start = object_track_start,
  1549. .object_track_stop = object_track_stop,
  1550. .object_dump = object_dump,
  1551. .object_write_config = object_write_config,
  1552. .object_reload_config = object_reload_config,
  1553. .object_key_increment = object_key_increment,
  1554. .object_key_decrement = object_key_decrement,
  1555. .object_key_create_typed = object_key_create_typed,
  1556. .object_key_get_typed = object_key_get_typed,
  1557. .object_key_iter_typed = object_key_iter_typed,
  1558. };
  1559. struct lcr_iface objdb_iface_ver0[1] = {
  1560. {
  1561. .name = "objdb",
  1562. .version = 0,
  1563. .versions_replace = 0,
  1564. .versions_replace_count = 0,
  1565. .dependencies = 0,
  1566. .dependency_count = 0,
  1567. .constructor = NULL,
  1568. .destructor = NULL,
  1569. .interfaces = NULL,
  1570. }
  1571. };
  1572. struct lcr_comp objdb_comp_ver0 = {
  1573. .iface_count = 1,
  1574. .ifaces = objdb_iface_ver0
  1575. };
  1576. #ifdef COROSYNC_SOLARIS
  1577. void corosync_lcr_component_register (void);
  1578. void corosync_lcr_component_register (void) {
  1579. #else
  1580. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  1581. #endif
  1582. lcr_interfaces_set (&objdb_iface_ver0[0], &objdb_iface);
  1583. lcr_component_register (&objdb_comp_ver0);
  1584. }