4
0

objdb.c 47 KB

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