confdb.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. /*
  2. * Copyright (c) 2008-2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Provides access to data in the corosync object database
  36. */
  37. #include <config.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <pthread.h>
  42. #include <sys/types.h>
  43. #include <errno.h>
  44. #include <corosync/corotypes.h>
  45. #include <corosync/coroipc_types.h>
  46. #include <corosync/coroipcc.h>
  47. #include <corosync/corodefs.h>
  48. #include <corosync/hdb.h>
  49. #include <corosync/list.h>
  50. #include <corosync/confdb.h>
  51. #include <corosync/ipc_confdb.h>
  52. #include "util.h"
  53. #include "sa-confdb.h"
  54. #undef MIN
  55. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  56. /* Hold the information for iterators so that
  57. callers can do recursive tree traversals.
  58. each object_handle can have its own iterator */
  59. struct iter_context {
  60. struct list_head list;
  61. hdb_handle_t parent_object_handle;
  62. hdb_handle_t find_handle;
  63. hdb_handle_t next_entry;
  64. };
  65. struct confdb_inst {
  66. hdb_handle_t handle;
  67. int finalize;
  68. int standalone;
  69. confdb_callbacks_t callbacks;
  70. const void *context;
  71. struct list_head object_find_head;
  72. struct list_head object_iter_head;
  73. struct list_head key_iter_head;
  74. };
  75. DECLARE_HDB_DATABASE(confdb_handle_t_db,NULL);
  76. static cs_error_t do_find_destroy(struct confdb_inst *confdb_inst, hdb_handle_t find_handle);
  77. /* Safely tidy one iterator context list */
  78. static void free_context_list(struct confdb_inst *confdb_inst, struct list_head *list)
  79. {
  80. struct iter_context *context;
  81. struct list_head *iter, *tmp;
  82. for (iter = list->next, tmp = iter->next;
  83. iter != list; iter = tmp, tmp = iter->next) {
  84. context = list_entry (iter, struct iter_context, list);
  85. (void)do_find_destroy(confdb_inst, context->find_handle);
  86. free(context);
  87. }
  88. }
  89. static struct iter_context *find_iter_context(struct list_head *list, hdb_handle_t object_handle)
  90. {
  91. struct iter_context *context;
  92. struct list_head *iter;
  93. for (iter = list->next;
  94. iter != list; iter = iter->next) {
  95. context = list_entry (iter, struct iter_context, list);
  96. if (context->parent_object_handle == object_handle)
  97. return context;
  98. }
  99. return NULL;
  100. }
  101. /**
  102. * @defgroup confdb_corosync confdb
  103. * @ingroup corosync
  104. *
  105. * @{
  106. */
  107. cs_error_t confdb_initialize (
  108. confdb_handle_t *handle,
  109. confdb_callbacks_t *callbacks)
  110. {
  111. cs_error_t error;
  112. struct confdb_inst *confdb_inst;
  113. error = hdb_error_to_cs(hdb_handle_create (&confdb_handle_t_db, sizeof (struct confdb_inst), handle));
  114. if (error != CS_OK) {
  115. goto error_no_destroy;
  116. }
  117. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, *handle, (void *)&confdb_inst));
  118. if (error != CS_OK) {
  119. goto error_destroy;
  120. }
  121. if (getenv("COROSYNC_DEFAULT_CONFIG_IFACE")) {
  122. error = confdb_sa_init();
  123. confdb_inst->standalone = 1;
  124. }
  125. else {
  126. error = coroipcc_service_connect (
  127. COROSYNC_SOCKET_NAME,
  128. CONFDB_SERVICE,
  129. IPC_REQUEST_SIZE,
  130. IPC_RESPONSE_SIZE,
  131. IPC_DISPATCH_SIZE,
  132. &confdb_inst->handle);
  133. }
  134. if (error != CS_OK)
  135. goto error_put_destroy;
  136. if (callbacks) {
  137. memcpy (&confdb_inst->callbacks, callbacks, sizeof (confdb_callbacks_t));
  138. }
  139. list_init (&confdb_inst->object_find_head);
  140. list_init (&confdb_inst->object_iter_head);
  141. list_init (&confdb_inst->key_iter_head);
  142. (void)hdb_handle_put (&confdb_handle_t_db, *handle);
  143. return (CS_OK);
  144. error_put_destroy:
  145. (void)hdb_handle_put (&confdb_handle_t_db, *handle);
  146. error_destroy:
  147. (void)hdb_handle_destroy (&confdb_handle_t_db, *handle);
  148. error_no_destroy:
  149. return (error);
  150. }
  151. cs_error_t confdb_finalize (
  152. confdb_handle_t handle)
  153. {
  154. struct confdb_inst *confdb_inst;
  155. cs_error_t error;
  156. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  157. if (error != CS_OK) {
  158. return (error);
  159. }
  160. /*
  161. * Another thread has already started finalizing
  162. */
  163. if (confdb_inst->finalize) {
  164. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  165. return (CS_ERR_BAD_HANDLE);
  166. }
  167. confdb_inst->finalize = 1;
  168. /* Free saved context handles */
  169. free_context_list(confdb_inst, &confdb_inst->object_find_head);
  170. free_context_list(confdb_inst, &confdb_inst->object_iter_head);
  171. free_context_list(confdb_inst, &confdb_inst->key_iter_head);
  172. if (!confdb_inst->standalone) {
  173. coroipcc_service_disconnect (confdb_inst->handle);
  174. }
  175. (void)hdb_handle_destroy (&confdb_handle_t_db, handle);
  176. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  177. return (CS_OK);
  178. }
  179. cs_error_t confdb_fd_get (
  180. confdb_handle_t handle,
  181. int *fd)
  182. {
  183. cs_error_t error;
  184. struct confdb_inst *confdb_inst;
  185. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  186. if (error != CS_OK) {
  187. return (error);
  188. }
  189. error = coroipcc_fd_get (confdb_inst->handle, fd);
  190. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  191. return (error);
  192. }
  193. cs_error_t confdb_context_get (
  194. confdb_handle_t handle,
  195. const void **context)
  196. {
  197. cs_error_t error;
  198. struct confdb_inst *confdb_inst;
  199. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  200. if (error != CS_OK) {
  201. return (error);
  202. }
  203. *context = confdb_inst->context;
  204. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  205. return (CS_OK);
  206. }
  207. cs_error_t confdb_context_set (
  208. confdb_handle_t handle,
  209. const void *context)
  210. {
  211. cs_error_t error;
  212. struct confdb_inst *confdb_inst;
  213. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  214. if (error != CS_OK) {
  215. return (error);
  216. }
  217. confdb_inst->context = context;
  218. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  219. return (CS_OK);
  220. }
  221. cs_error_t confdb_dispatch (
  222. confdb_handle_t handle,
  223. cs_dispatch_flags_t dispatch_types)
  224. {
  225. int timeout = -1;
  226. cs_error_t error;
  227. int cont = 1; /* always continue do loop except when set to 0 */
  228. struct confdb_inst *confdb_inst;
  229. confdb_callbacks_t callbacks;
  230. struct res_lib_confdb_key_change_callback *res_key_changed_pt;
  231. struct res_lib_confdb_object_create_callback *res_object_created_pt;
  232. struct res_lib_confdb_object_destroy_callback *res_object_destroyed_pt;
  233. struct res_lib_confdb_reload_callback *res_reload_pt;
  234. coroipc_response_header_t *dispatch_data;
  235. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  236. if (error != CS_OK) {
  237. return (error);
  238. }
  239. if (confdb_inst->standalone) {
  240. error = CS_ERR_NOT_SUPPORTED;
  241. goto error_put;
  242. }
  243. /*
  244. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  245. * wait indefinitely for CS_DISPATCH_BLOCKING
  246. */
  247. if (dispatch_types == CONFDB_DISPATCH_ALL) {
  248. timeout = 0;
  249. }
  250. do {
  251. error = coroipcc_dispatch_get (
  252. confdb_inst->handle,
  253. (void **)&dispatch_data,
  254. timeout);
  255. if (error == CS_ERR_BAD_HANDLE) {
  256. error = CS_OK;
  257. goto error_put;
  258. }
  259. if (error == CS_ERR_TRY_AGAIN) {
  260. error = CS_OK;
  261. if (dispatch_types == CONFDB_DISPATCH_ALL) {
  262. break; /* exit do while cont is 1 loop */
  263. } else {
  264. continue; /* next poll */
  265. }
  266. }
  267. if (error != CS_OK) {
  268. goto error_put;
  269. }
  270. /*
  271. * Make copy of callbacks, message data, unlock instance, and call callback
  272. * A risk of this dispatch method is that the callback routines may
  273. * operate at the same time that confdbFinalize has been called.
  274. */
  275. memcpy (&callbacks, &confdb_inst->callbacks, sizeof (confdb_callbacks_t));
  276. /*
  277. * Dispatch incoming message
  278. */
  279. switch (dispatch_data->id) {
  280. case MESSAGE_RES_CONFDB_KEY_CHANGE_CALLBACK:
  281. if (callbacks.confdb_key_change_notify_fn == NULL) {
  282. break;
  283. }
  284. res_key_changed_pt = (struct res_lib_confdb_key_change_callback *)dispatch_data;
  285. callbacks.confdb_key_change_notify_fn(handle,
  286. res_key_changed_pt->change_type,
  287. res_key_changed_pt->object_handle,
  288. res_key_changed_pt->parent_object_handle,
  289. res_key_changed_pt->object_name.value,
  290. res_key_changed_pt->object_name.length,
  291. res_key_changed_pt->key_name.value,
  292. res_key_changed_pt->key_name.length,
  293. res_key_changed_pt->key_value.value,
  294. res_key_changed_pt->key_value.length);
  295. break;
  296. case MESSAGE_RES_CONFDB_OBJECT_CREATE_CALLBACK:
  297. if (callbacks.confdb_object_create_change_notify_fn == NULL) {
  298. break;
  299. }
  300. res_object_created_pt = (struct res_lib_confdb_object_create_callback *)dispatch_data;
  301. callbacks.confdb_object_create_change_notify_fn(handle,
  302. res_object_created_pt->object_handle,
  303. res_object_created_pt->parent_object_handle,
  304. res_object_created_pt->name.value,
  305. res_object_created_pt->name.length);
  306. break;
  307. case MESSAGE_RES_CONFDB_OBJECT_DESTROY_CALLBACK:
  308. if (callbacks.confdb_object_delete_change_notify_fn == NULL) {
  309. break;
  310. }
  311. res_object_destroyed_pt = (struct res_lib_confdb_object_destroy_callback *)dispatch_data;
  312. callbacks.confdb_object_delete_change_notify_fn(handle,
  313. res_object_destroyed_pt->parent_object_handle,
  314. res_object_destroyed_pt->name.value,
  315. res_object_destroyed_pt->name.length);
  316. break;
  317. case MESSAGE_RES_CONFDB_RELOAD_CALLBACK:
  318. if (callbacks.confdb_reload_notify_fn == NULL) {
  319. break;
  320. }
  321. res_reload_pt = (struct res_lib_confdb_reload_callback *)dispatch_data;
  322. callbacks.confdb_reload_notify_fn(handle,
  323. res_reload_pt->type);
  324. break;
  325. default:
  326. coroipcc_dispatch_put (confdb_inst->handle);
  327. error = CS_ERR_LIBRARY;
  328. goto error_noput;
  329. break;
  330. }
  331. coroipcc_dispatch_put (confdb_inst->handle);
  332. /*
  333. * Determine if more messages should be processed
  334. */
  335. if (dispatch_types == CS_DISPATCH_ONE) {
  336. cont = 0;
  337. }
  338. } while (cont);
  339. error_put:
  340. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  341. error_noput:
  342. return (error);
  343. }
  344. cs_error_t confdb_object_create (
  345. confdb_handle_t handle,
  346. hdb_handle_t parent_object_handle,
  347. const void *object_name,
  348. size_t object_name_len,
  349. hdb_handle_t *object_handle)
  350. {
  351. cs_error_t error;
  352. struct confdb_inst *confdb_inst;
  353. struct iovec iov;
  354. struct req_lib_confdb_object_create req_lib_confdb_object_create;
  355. struct res_lib_confdb_object_create res_lib_confdb_object_create;
  356. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  357. if (error != CS_OK) {
  358. return (error);
  359. }
  360. if (confdb_inst->standalone) {
  361. error = CS_OK;
  362. if (confdb_sa_object_create(parent_object_handle,
  363. object_name, object_name_len,
  364. object_handle))
  365. error = CS_ERR_ACCESS;
  366. goto error_exit;
  367. }
  368. req_lib_confdb_object_create.header.size = sizeof (struct req_lib_confdb_object_create);
  369. req_lib_confdb_object_create.header.id = MESSAGE_REQ_CONFDB_OBJECT_CREATE;
  370. req_lib_confdb_object_create.parent_object_handle = parent_object_handle;
  371. memcpy(req_lib_confdb_object_create.object_name.value, object_name, object_name_len);
  372. req_lib_confdb_object_create.object_name.length = object_name_len;
  373. iov.iov_base = (char *)&req_lib_confdb_object_create;
  374. iov.iov_len = sizeof (struct req_lib_confdb_object_create);
  375. error = coroipcc_msg_send_reply_receive (
  376. confdb_inst->handle,
  377. &iov,
  378. 1,
  379. &res_lib_confdb_object_create,
  380. sizeof (struct res_lib_confdb_object_create));
  381. if (error != CS_OK) {
  382. goto error_exit;
  383. }
  384. error = res_lib_confdb_object_create.header.error;
  385. *object_handle = res_lib_confdb_object_create.object_handle;
  386. error_exit:
  387. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  388. return (error);
  389. }
  390. cs_error_t confdb_object_destroy (
  391. confdb_handle_t handle,
  392. hdb_handle_t object_handle)
  393. {
  394. cs_error_t error;
  395. struct confdb_inst *confdb_inst;
  396. struct iovec iov;
  397. struct req_lib_confdb_object_destroy req_lib_confdb_object_destroy;
  398. coroipc_response_header_t res;
  399. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  400. if (error != CS_OK) {
  401. return (error);
  402. }
  403. if (confdb_inst->standalone) {
  404. error = CS_OK;
  405. if (confdb_sa_object_destroy(object_handle))
  406. error = CS_ERR_ACCESS;
  407. goto error_exit;
  408. }
  409. req_lib_confdb_object_destroy.header.size = sizeof (struct req_lib_confdb_object_destroy);
  410. req_lib_confdb_object_destroy.header.id = MESSAGE_REQ_CONFDB_OBJECT_DESTROY;
  411. req_lib_confdb_object_destroy.object_handle = object_handle;
  412. iov.iov_base = (char *)&req_lib_confdb_object_destroy;
  413. iov.iov_len = sizeof (struct req_lib_confdb_object_destroy);
  414. error = coroipcc_msg_send_reply_receive (
  415. confdb_inst->handle,
  416. &iov,
  417. 1,
  418. &res,
  419. sizeof (coroipc_response_header_t));
  420. if (error != CS_OK) {
  421. goto error_exit;
  422. }
  423. error = res.error;
  424. error_exit:
  425. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  426. return (error);
  427. }
  428. cs_error_t confdb_object_parent_get (
  429. confdb_handle_t handle,
  430. hdb_handle_t object_handle,
  431. hdb_handle_t *parent_object_handle)
  432. {
  433. cs_error_t error;
  434. struct confdb_inst *confdb_inst;
  435. struct iovec iov;
  436. struct req_lib_confdb_object_parent_get req_lib_confdb_object_parent_get;
  437. struct res_lib_confdb_object_parent_get res_lib_confdb_object_parent_get;
  438. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  439. if (error != CS_OK) {
  440. return (error);
  441. }
  442. if (confdb_inst->standalone) {
  443. error = CS_OK;
  444. if (confdb_sa_object_parent_get(object_handle, parent_object_handle))
  445. error = CS_ERR_ACCESS;
  446. goto error_exit;
  447. }
  448. req_lib_confdb_object_parent_get.header.size = sizeof (struct req_lib_confdb_object_parent_get);
  449. req_lib_confdb_object_parent_get.header.id = MESSAGE_REQ_CONFDB_OBJECT_PARENT_GET;
  450. req_lib_confdb_object_parent_get.object_handle = object_handle;
  451. iov.iov_base = (char *)&req_lib_confdb_object_parent_get;
  452. iov.iov_len = sizeof (struct req_lib_confdb_object_parent_get);
  453. error = coroipcc_msg_send_reply_receive (
  454. confdb_inst->handle,
  455. &iov,
  456. 1,
  457. &res_lib_confdb_object_parent_get,
  458. sizeof (struct res_lib_confdb_object_parent_get));
  459. if (error != CS_OK) {
  460. goto error_exit;
  461. }
  462. error = res_lib_confdb_object_parent_get.header.error;
  463. *parent_object_handle = res_lib_confdb_object_parent_get.parent_object_handle;
  464. error_exit:
  465. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  466. return (error);
  467. }
  468. cs_error_t confdb_object_name_get (
  469. confdb_handle_t handle,
  470. hdb_handle_t object_handle,
  471. char *object_name,
  472. size_t *object_name_len)
  473. {
  474. cs_error_t error;
  475. struct confdb_inst *confdb_inst;
  476. struct iovec iov;
  477. struct req_lib_confdb_object_name_get request;
  478. struct res_lib_confdb_object_name_get response;
  479. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  480. if (error != CS_OK) {
  481. return (error);
  482. }
  483. if (confdb_inst->standalone) {
  484. error = CS_OK;
  485. if (confdb_sa_object_name_get(object_handle, object_name, object_name_len))
  486. error = CS_ERR_ACCESS;
  487. goto error_exit;
  488. }
  489. request.header.size = sizeof (struct req_lib_confdb_object_name_get);
  490. request.header.id = MESSAGE_REQ_CONFDB_OBJECT_NAME_GET;
  491. request.object_handle = object_handle;
  492. iov.iov_base = (char *)&request;
  493. iov.iov_len = sizeof (struct req_lib_confdb_object_name_get);
  494. error = coroipcc_msg_send_reply_receive (
  495. confdb_inst->handle,
  496. &iov,
  497. 1,
  498. &response,
  499. sizeof (struct res_lib_confdb_object_name_get));
  500. if (error != CS_OK) {
  501. goto error_exit;
  502. }
  503. error = response.header.error;
  504. if (error == CS_OK) {
  505. *object_name_len = response.object_name.length;
  506. memcpy(object_name, response.object_name.value, *object_name_len);
  507. }
  508. error_exit:
  509. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  510. return (error);
  511. }
  512. static cs_error_t do_find_destroy(
  513. struct confdb_inst *confdb_inst,
  514. hdb_handle_t find_handle)
  515. {
  516. cs_error_t error;
  517. struct iovec iov;
  518. struct req_lib_confdb_object_find_destroy req_lib_confdb_object_find_destroy;
  519. coroipc_response_header_t res;
  520. if (!find_handle)
  521. return CS_OK;
  522. if (confdb_inst->standalone) {
  523. error = CS_OK;
  524. if (confdb_sa_find_destroy(find_handle))
  525. error = CS_ERR_ACCESS;
  526. goto error_exit;
  527. }
  528. req_lib_confdb_object_find_destroy.header.size = sizeof (struct req_lib_confdb_object_find_destroy);
  529. req_lib_confdb_object_find_destroy.header.id = MESSAGE_REQ_CONFDB_OBJECT_FIND_DESTROY;
  530. req_lib_confdb_object_find_destroy.find_handle = find_handle;
  531. iov.iov_base = (char *)&req_lib_confdb_object_find_destroy;
  532. iov.iov_len = sizeof (struct req_lib_confdb_object_find_destroy);
  533. error = coroipcc_msg_send_reply_receive (
  534. confdb_inst->handle,
  535. &iov,
  536. 1,
  537. &res,
  538. sizeof (coroipc_response_header_t));
  539. if (error != CS_OK) {
  540. goto error_exit;
  541. }
  542. error = res.error;
  543. error_exit:
  544. return (error);
  545. }
  546. cs_error_t confdb_object_find_destroy(
  547. confdb_handle_t handle,
  548. hdb_handle_t parent_object_handle)
  549. {
  550. struct iter_context *context;
  551. cs_error_t error;
  552. struct confdb_inst *confdb_inst;
  553. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  554. if (error != CS_OK) {
  555. return (error);
  556. }
  557. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  558. error = do_find_destroy(confdb_inst, context->find_handle);
  559. if (error == CS_OK) {
  560. list_del(&context->list);
  561. free(context);
  562. }
  563. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  564. return error;
  565. }
  566. cs_error_t confdb_object_iter_destroy(
  567. confdb_handle_t handle,
  568. hdb_handle_t parent_object_handle)
  569. {
  570. struct iter_context *context;
  571. cs_error_t error;
  572. struct confdb_inst *confdb_inst;
  573. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  574. if (error != CS_OK) {
  575. return (error);
  576. }
  577. context = find_iter_context(&confdb_inst->object_iter_head, parent_object_handle);
  578. error = do_find_destroy(confdb_inst, context->find_handle);
  579. if (error == CS_OK) {
  580. list_del(&context->list);
  581. free(context);
  582. }
  583. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  584. return error;
  585. }
  586. cs_error_t confdb_key_create (
  587. confdb_handle_t handle,
  588. hdb_handle_t parent_object_handle,
  589. const void *key_name,
  590. size_t key_name_len,
  591. const void *value,
  592. size_t value_len)
  593. {
  594. cs_error_t error;
  595. struct confdb_inst *confdb_inst;
  596. struct iovec iov;
  597. struct req_lib_confdb_key_create req_lib_confdb_key_create;
  598. coroipc_response_header_t res;
  599. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  600. if (error != CS_OK) {
  601. return (error);
  602. }
  603. if (confdb_inst->standalone) {
  604. error = CS_OK;
  605. if (confdb_sa_key_create(parent_object_handle,
  606. key_name, key_name_len,
  607. value, value_len))
  608. error = CS_ERR_ACCESS;
  609. goto error_exit;
  610. }
  611. req_lib_confdb_key_create.header.size = sizeof (struct req_lib_confdb_key_create);
  612. req_lib_confdb_key_create.header.id = MESSAGE_REQ_CONFDB_KEY_CREATE;
  613. req_lib_confdb_key_create.object_handle = parent_object_handle;
  614. memcpy(req_lib_confdb_key_create.key_name.value, key_name, key_name_len);
  615. req_lib_confdb_key_create.key_name.length = key_name_len;
  616. memcpy(req_lib_confdb_key_create.value.value, value, value_len);
  617. req_lib_confdb_key_create.value.length = value_len;
  618. iov.iov_base = (char *)&req_lib_confdb_key_create;
  619. iov.iov_len = sizeof (struct req_lib_confdb_key_create);
  620. error = coroipcc_msg_send_reply_receive (
  621. confdb_inst->handle,
  622. &iov,
  623. 1,
  624. &res,
  625. sizeof (res));
  626. if (error != CS_OK) {
  627. goto error_exit;
  628. }
  629. error = res.error;
  630. error_exit:
  631. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  632. return (error);
  633. }
  634. cs_error_t confdb_key_create_typed (
  635. confdb_handle_t handle,
  636. hdb_handle_t parent_object_handle,
  637. const char *key_name,
  638. const void *value,
  639. size_t value_len,
  640. confdb_value_types_t type)
  641. {
  642. cs_error_t error;
  643. struct confdb_inst *confdb_inst;
  644. struct iovec iov;
  645. struct req_lib_confdb_key_create_typed request;
  646. coroipc_response_header_t res;
  647. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  648. if (error != CS_OK) {
  649. return (error);
  650. }
  651. if (confdb_inst->standalone) {
  652. error = CS_OK;
  653. if (confdb_sa_key_create_typed(parent_object_handle,
  654. key_name, value, value_len, type))
  655. error = CS_ERR_ACCESS;
  656. goto error_exit;
  657. }
  658. request.header.size = sizeof (struct req_lib_confdb_key_create_typed);
  659. request.header.id = MESSAGE_REQ_CONFDB_KEY_CREATE_TYPED;
  660. request.object_handle = parent_object_handle;
  661. request.key_name.length = strlen(key_name)+1;
  662. memcpy(request.key_name.value, key_name, request.key_name.length);
  663. memcpy(request.value.value, value, value_len);
  664. request.value.length = value_len;
  665. request.type = type;
  666. iov.iov_base = (char *)&request;
  667. iov.iov_len = sizeof (struct req_lib_confdb_key_create_typed);
  668. error = coroipcc_msg_send_reply_receive (
  669. confdb_inst->handle,
  670. &iov,
  671. 1,
  672. &res,
  673. sizeof (res));
  674. if (error != CS_OK) {
  675. goto error_exit;
  676. }
  677. error = res.error;
  678. error_exit:
  679. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  680. return (error);
  681. }
  682. cs_error_t confdb_key_delete (
  683. confdb_handle_t handle,
  684. hdb_handle_t parent_object_handle,
  685. const void *key_name,
  686. size_t key_name_len,
  687. const void *value,
  688. size_t value_len)
  689. {
  690. cs_error_t error;
  691. struct confdb_inst *confdb_inst;
  692. struct iovec iov;
  693. struct req_lib_confdb_key_delete req_lib_confdb_key_delete;
  694. coroipc_response_header_t res;
  695. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  696. if (error != CS_OK) {
  697. return (error);
  698. }
  699. if (confdb_inst->standalone) {
  700. error = CS_OK;
  701. if (confdb_sa_key_delete(parent_object_handle,
  702. key_name, key_name_len,
  703. value, value_len))
  704. error = CS_ERR_ACCESS;
  705. goto error_exit;
  706. }
  707. req_lib_confdb_key_delete.header.size = sizeof (struct req_lib_confdb_key_delete);
  708. req_lib_confdb_key_delete.header.id = MESSAGE_REQ_CONFDB_KEY_DELETE;
  709. req_lib_confdb_key_delete.object_handle = parent_object_handle;
  710. memcpy(req_lib_confdb_key_delete.key_name.value, key_name, key_name_len);
  711. req_lib_confdb_key_delete.key_name.length = key_name_len;
  712. memcpy(req_lib_confdb_key_delete.value.value, value, value_len);
  713. req_lib_confdb_key_delete.value.length = value_len;
  714. iov.iov_base = (char *)&req_lib_confdb_key_delete;
  715. iov.iov_len = sizeof (struct req_lib_confdb_key_delete);
  716. error = coroipcc_msg_send_reply_receive (
  717. confdb_inst->handle,
  718. &iov,
  719. 1,
  720. &res,
  721. sizeof (res));
  722. if (error != CS_OK) {
  723. goto error_exit;
  724. }
  725. error = res.error;
  726. error_exit:
  727. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  728. return (error);
  729. }
  730. cs_error_t confdb_key_get (
  731. confdb_handle_t handle,
  732. hdb_handle_t parent_object_handle,
  733. const void *key_name,
  734. size_t key_name_len,
  735. void *value,
  736. size_t *value_len)
  737. {
  738. cs_error_t error;
  739. struct confdb_inst *confdb_inst;
  740. struct iovec iov;
  741. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  742. struct res_lib_confdb_key_get res_lib_confdb_key_get;
  743. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  744. if (error != CS_OK) {
  745. return (error);
  746. }
  747. if (confdb_inst->standalone) {
  748. error = CS_OK;
  749. if (confdb_sa_key_get(parent_object_handle,
  750. key_name, key_name_len,
  751. value, value_len))
  752. error = CS_ERR_ACCESS;
  753. goto error_exit;
  754. }
  755. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  756. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_GET;
  757. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  758. memcpy(req_lib_confdb_key_get.key_name.value, key_name, key_name_len);
  759. req_lib_confdb_key_get.key_name.length = key_name_len;
  760. iov.iov_base = (char *)&req_lib_confdb_key_get;
  761. iov.iov_len = sizeof (struct req_lib_confdb_key_get);
  762. error = coroipcc_msg_send_reply_receive (
  763. confdb_inst->handle,
  764. &iov,
  765. 1,
  766. &res_lib_confdb_key_get,
  767. sizeof (struct res_lib_confdb_key_get));
  768. if (error != CS_OK) {
  769. goto error_exit;
  770. }
  771. error = res_lib_confdb_key_get.header.error;
  772. if (error == CS_OK) {
  773. *value_len = res_lib_confdb_key_get.value.length;
  774. memcpy(value, res_lib_confdb_key_get.value.value, *value_len);
  775. }
  776. error_exit:
  777. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  778. return (error);
  779. }
  780. cs_error_t confdb_key_get_typed (
  781. confdb_handle_t handle,
  782. hdb_handle_t parent_object_handle,
  783. const char *key_name,
  784. void *value,
  785. size_t *value_len,
  786. confdb_value_types_t *type)
  787. {
  788. cs_error_t error;
  789. struct confdb_inst *confdb_inst;
  790. struct iovec iov;
  791. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  792. struct res_lib_confdb_key_get_typed response;
  793. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  794. if (error != CS_OK) {
  795. return (error);
  796. }
  797. if (confdb_inst->standalone) {
  798. error = CS_OK;
  799. if (confdb_sa_key_get_typed(parent_object_handle,
  800. key_name, value, value_len, (int*)type))
  801. error = CS_ERR_ACCESS;
  802. goto error_exit;
  803. }
  804. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  805. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_GET_TYPED;
  806. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  807. req_lib_confdb_key_get.key_name.length = strlen(key_name) + 1;
  808. memcpy(req_lib_confdb_key_get.key_name.value, key_name, req_lib_confdb_key_get.key_name.length);
  809. iov.iov_base = (char *)&req_lib_confdb_key_get;
  810. iov.iov_len = sizeof (struct req_lib_confdb_key_get);
  811. error = coroipcc_msg_send_reply_receive (
  812. confdb_inst->handle,
  813. &iov,
  814. 1,
  815. &response,
  816. sizeof (struct res_lib_confdb_key_get_typed));
  817. if (error != CS_OK) {
  818. goto error_exit;
  819. }
  820. error = response.header.error;
  821. if (error == CS_OK) {
  822. *value_len = response.value.length;
  823. *type = response.type;
  824. memcpy(value, response.value.value, *value_len);
  825. }
  826. error_exit:
  827. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  828. return (error);
  829. }
  830. cs_error_t confdb_key_increment (
  831. confdb_handle_t handle,
  832. hdb_handle_t parent_object_handle,
  833. const void *key_name,
  834. size_t key_name_len,
  835. unsigned int *value)
  836. {
  837. cs_error_t error;
  838. struct confdb_inst *confdb_inst;
  839. struct iovec iov;
  840. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  841. struct res_lib_confdb_key_incdec res_lib_confdb_key_incdec;
  842. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  843. if (error != CS_OK) {
  844. return (error);
  845. }
  846. if (confdb_inst->standalone) {
  847. error = CS_OK;
  848. if (confdb_sa_key_increment(parent_object_handle,
  849. key_name, key_name_len,
  850. value))
  851. error = CS_ERR_ACCESS;
  852. goto error_exit;
  853. }
  854. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  855. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_INCREMENT;
  856. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  857. memcpy(req_lib_confdb_key_get.key_name.value, key_name, key_name_len);
  858. req_lib_confdb_key_get.key_name.length = key_name_len;
  859. iov.iov_base = (char *)&req_lib_confdb_key_get;
  860. iov.iov_len = sizeof (struct req_lib_confdb_key_get);
  861. error = coroipcc_msg_send_reply_receive (
  862. confdb_inst->handle,
  863. &iov,
  864. 1,
  865. &res_lib_confdb_key_incdec,
  866. sizeof (struct res_lib_confdb_key_incdec));
  867. if (error != CS_OK) {
  868. goto error_exit;
  869. }
  870. error = res_lib_confdb_key_incdec.header.error;
  871. if (error == CS_OK) {
  872. *value = res_lib_confdb_key_incdec.value;
  873. }
  874. error_exit:
  875. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  876. return (error);
  877. }
  878. cs_error_t confdb_key_decrement (
  879. confdb_handle_t handle,
  880. hdb_handle_t parent_object_handle,
  881. const void *key_name,
  882. size_t key_name_len,
  883. unsigned int *value)
  884. {
  885. cs_error_t error;
  886. struct confdb_inst *confdb_inst;
  887. struct iovec iov;
  888. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  889. struct res_lib_confdb_key_incdec res_lib_confdb_key_incdec;
  890. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  891. if (error != CS_OK) {
  892. return (error);
  893. }
  894. if (confdb_inst->standalone) {
  895. error = CS_OK;
  896. if (confdb_sa_key_decrement(parent_object_handle,
  897. key_name, key_name_len,
  898. value))
  899. error = CS_ERR_ACCESS;
  900. goto error_exit;
  901. }
  902. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  903. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_DECREMENT;
  904. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  905. memcpy(req_lib_confdb_key_get.key_name.value, key_name, key_name_len);
  906. req_lib_confdb_key_get.key_name.length = key_name_len;
  907. iov.iov_base = (char *)&req_lib_confdb_key_get;
  908. iov.iov_len = sizeof (struct req_lib_confdb_key_get);
  909. error = coroipcc_msg_send_reply_receive (
  910. confdb_inst->handle,
  911. &iov,
  912. 1,
  913. &res_lib_confdb_key_incdec,
  914. sizeof (struct res_lib_confdb_key_incdec));
  915. if (error != CS_OK) {
  916. goto error_exit;
  917. }
  918. error = res_lib_confdb_key_incdec.header.error;
  919. if (error == CS_OK) {
  920. *value = res_lib_confdb_key_incdec.value;
  921. }
  922. error_exit:
  923. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  924. return (error);
  925. }
  926. cs_error_t confdb_key_replace (
  927. confdb_handle_t handle,
  928. hdb_handle_t parent_object_handle,
  929. const void *key_name,
  930. size_t key_name_len,
  931. const void *old_value,
  932. size_t old_value_len,
  933. const void *new_value,
  934. size_t new_value_len)
  935. {
  936. cs_error_t error;
  937. struct confdb_inst *confdb_inst;
  938. struct iovec iov;
  939. struct req_lib_confdb_key_replace req_lib_confdb_key_replace;
  940. coroipc_response_header_t res;
  941. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  942. if (error != CS_OK) {
  943. return (error);
  944. }
  945. if (confdb_inst->standalone) {
  946. error = CS_OK;
  947. if (confdb_sa_key_replace(parent_object_handle,
  948. key_name, key_name_len,
  949. old_value, old_value_len,
  950. new_value, new_value_len))
  951. error = CS_ERR_ACCESS;
  952. goto error_exit;
  953. }
  954. req_lib_confdb_key_replace.header.size = sizeof (struct req_lib_confdb_key_replace);
  955. req_lib_confdb_key_replace.header.id = MESSAGE_REQ_CONFDB_KEY_REPLACE;
  956. req_lib_confdb_key_replace.object_handle = parent_object_handle;
  957. memcpy(req_lib_confdb_key_replace.key_name.value, key_name, key_name_len);
  958. req_lib_confdb_key_replace.key_name.length = key_name_len;
  959. memcpy(req_lib_confdb_key_replace.old_value.value, old_value, old_value_len);
  960. req_lib_confdb_key_replace.old_value.length = old_value_len;
  961. memcpy(req_lib_confdb_key_replace.new_value.value, new_value, new_value_len);
  962. req_lib_confdb_key_replace.new_value.length = new_value_len;
  963. iov.iov_base = (char *)&req_lib_confdb_key_replace;
  964. iov.iov_len = sizeof (struct req_lib_confdb_key_replace);
  965. error = coroipcc_msg_send_reply_receive (
  966. confdb_inst->handle,
  967. &iov,
  968. 1,
  969. &res,
  970. sizeof (res));
  971. if (error != CS_OK) {
  972. goto error_exit;
  973. }
  974. error = res.error;
  975. error_exit:
  976. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  977. return (error);
  978. }
  979. cs_error_t confdb_object_iter_start (
  980. confdb_handle_t handle,
  981. hdb_handle_t object_handle)
  982. {
  983. struct confdb_inst *confdb_inst;
  984. cs_error_t error = CS_OK;
  985. struct iter_context *context;
  986. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  987. if (error != CS_OK) {
  988. return (error);
  989. }
  990. context = find_iter_context(&confdb_inst->object_iter_head, object_handle);
  991. if (!context) {
  992. context = malloc(sizeof(struct iter_context));
  993. if (!context) {
  994. error = CS_ERR_NO_MEMORY;
  995. goto ret;
  996. }
  997. context->parent_object_handle = object_handle;
  998. context->find_handle = 0;
  999. list_add(&context->list, &confdb_inst->object_iter_head);
  1000. }
  1001. /* Start a new find context */
  1002. if (context->find_handle) {
  1003. (void)do_find_destroy(confdb_inst, context->find_handle);
  1004. context->find_handle = 0;
  1005. }
  1006. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1007. ret:
  1008. return error;
  1009. }
  1010. cs_error_t confdb_key_iter_start (
  1011. confdb_handle_t handle,
  1012. hdb_handle_t object_handle)
  1013. {
  1014. struct confdb_inst *confdb_inst;
  1015. cs_error_t error = CS_OK;
  1016. struct iter_context *context;
  1017. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1018. if (error != CS_OK) {
  1019. return (error);
  1020. }
  1021. context = find_iter_context(&confdb_inst->key_iter_head, object_handle);
  1022. if (!context) {
  1023. context = malloc(sizeof(struct iter_context));
  1024. if (!context) {
  1025. error = CS_ERR_NO_MEMORY;
  1026. goto ret;
  1027. }
  1028. context->parent_object_handle = object_handle;
  1029. list_add(&context->list, &confdb_inst->key_iter_head);
  1030. }
  1031. context->find_handle = 0;
  1032. context->next_entry = 0;
  1033. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1034. ret:
  1035. return error;
  1036. }
  1037. cs_error_t confdb_object_find_start (
  1038. confdb_handle_t handle,
  1039. hdb_handle_t parent_object_handle)
  1040. {
  1041. struct confdb_inst *confdb_inst;
  1042. cs_error_t error = CS_OK;
  1043. struct iter_context *context;
  1044. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1045. if (error != CS_OK) {
  1046. return (error);
  1047. }
  1048. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  1049. if (!context) {
  1050. context = malloc(sizeof(struct iter_context));
  1051. if (!context) {
  1052. error = CS_ERR_NO_MEMORY;
  1053. goto ret;
  1054. }
  1055. context->find_handle = 0;
  1056. context->parent_object_handle = parent_object_handle;
  1057. list_add(&context->list, &confdb_inst->object_find_head);
  1058. }
  1059. /* Start a new find context */
  1060. if (context->find_handle) {
  1061. (void)do_find_destroy(confdb_inst, context->find_handle);
  1062. context->find_handle = 0;
  1063. }
  1064. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1065. ret:
  1066. return error;
  1067. }
  1068. cs_error_t confdb_object_find (
  1069. confdb_handle_t handle,
  1070. hdb_handle_t parent_object_handle,
  1071. const void *object_name,
  1072. size_t object_name_len,
  1073. hdb_handle_t *object_handle)
  1074. {
  1075. cs_error_t error;
  1076. struct confdb_inst *confdb_inst;
  1077. struct iovec iov;
  1078. struct iter_context *context;
  1079. struct req_lib_confdb_object_find req_lib_confdb_object_find;
  1080. struct res_lib_confdb_object_find res_lib_confdb_object_find;
  1081. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1082. if (error != CS_OK) {
  1083. return (error);
  1084. }
  1085. /* You MUST call confdb_object_find_start first */
  1086. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  1087. if (!context) {
  1088. error = CS_ERR_CONTEXT_NOT_FOUND;
  1089. goto error_exit;
  1090. }
  1091. if (confdb_inst->standalone) {
  1092. error = CS_OK;
  1093. if (confdb_sa_object_find(parent_object_handle,
  1094. &context->find_handle,
  1095. object_handle,
  1096. object_name, object_name_len))
  1097. error = CS_ERR_ACCESS;
  1098. goto error_exit;
  1099. }
  1100. req_lib_confdb_object_find.header.size = sizeof (struct req_lib_confdb_object_find);
  1101. req_lib_confdb_object_find.header.id = MESSAGE_REQ_CONFDB_OBJECT_FIND;
  1102. req_lib_confdb_object_find.parent_object_handle = parent_object_handle;
  1103. req_lib_confdb_object_find.find_handle = context->find_handle;
  1104. memcpy(req_lib_confdb_object_find.object_name.value, object_name, object_name_len);
  1105. req_lib_confdb_object_find.object_name.length = object_name_len;
  1106. iov.iov_base = (char *)&req_lib_confdb_object_find;
  1107. iov.iov_len = sizeof (struct req_lib_confdb_object_find);
  1108. error = coroipcc_msg_send_reply_receive (
  1109. confdb_inst->handle,
  1110. &iov,
  1111. 1,
  1112. &res_lib_confdb_object_find,
  1113. sizeof (struct res_lib_confdb_object_find));
  1114. if (error != CS_OK) {
  1115. goto error_exit;
  1116. }
  1117. error = res_lib_confdb_object_find.header.error;
  1118. *object_handle = res_lib_confdb_object_find.object_handle;
  1119. context->find_handle = res_lib_confdb_object_find.find_handle;
  1120. error_exit:
  1121. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1122. return (error);
  1123. }
  1124. cs_error_t confdb_object_iter (
  1125. confdb_handle_t handle,
  1126. hdb_handle_t parent_object_handle,
  1127. hdb_handle_t *object_handle,
  1128. void *object_name,
  1129. size_t *object_name_len)
  1130. {
  1131. cs_error_t error;
  1132. struct confdb_inst *confdb_inst;
  1133. struct iovec iov;
  1134. struct iter_context *context;
  1135. struct req_lib_confdb_object_iter req_lib_confdb_object_iter;
  1136. struct res_lib_confdb_object_iter res_lib_confdb_object_iter;
  1137. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1138. if (error != CS_OK) {
  1139. return (error);
  1140. }
  1141. /* You MUST call confdb_object_iter_start first */
  1142. context = find_iter_context(&confdb_inst->object_iter_head, parent_object_handle);
  1143. if (!context) {
  1144. error = CS_ERR_CONTEXT_NOT_FOUND;
  1145. goto error_exit;
  1146. }
  1147. if (confdb_inst->standalone) {
  1148. error = CS_OK;
  1149. *object_name_len = 0;
  1150. if (confdb_sa_object_iter(parent_object_handle,
  1151. &context->find_handle,
  1152. object_handle,
  1153. NULL, 0,
  1154. object_name, object_name_len))
  1155. error = CS_ERR_ACCESS;
  1156. goto sa_exit;
  1157. }
  1158. req_lib_confdb_object_iter.header.size = sizeof (struct req_lib_confdb_object_iter);
  1159. req_lib_confdb_object_iter.header.id = MESSAGE_REQ_CONFDB_OBJECT_ITER;
  1160. req_lib_confdb_object_iter.parent_object_handle = parent_object_handle;
  1161. req_lib_confdb_object_iter.find_handle = context->find_handle;
  1162. iov.iov_base = (char *)&req_lib_confdb_object_iter;
  1163. iov.iov_len = sizeof (struct req_lib_confdb_object_iter);
  1164. error = coroipcc_msg_send_reply_receive (
  1165. confdb_inst->handle,
  1166. &iov,
  1167. 1,
  1168. &res_lib_confdb_object_iter,
  1169. sizeof (struct res_lib_confdb_object_iter));
  1170. if (error != CS_OK) {
  1171. goto error_exit;
  1172. }
  1173. error = res_lib_confdb_object_iter.header.error;
  1174. if (error == CS_OK) {
  1175. *object_name_len = res_lib_confdb_object_iter.object_name.length;
  1176. memcpy(object_name, res_lib_confdb_object_iter.object_name.value, *object_name_len);
  1177. *object_handle = res_lib_confdb_object_iter.object_handle;
  1178. context->find_handle = res_lib_confdb_object_iter.find_handle;
  1179. }
  1180. sa_exit:
  1181. error_exit:
  1182. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1183. return (error);
  1184. }
  1185. cs_error_t confdb_key_iter (
  1186. confdb_handle_t handle,
  1187. hdb_handle_t parent_object_handle,
  1188. void *key_name,
  1189. size_t *key_name_len,
  1190. void *value,
  1191. size_t *value_len)
  1192. {
  1193. cs_error_t error;
  1194. struct confdb_inst *confdb_inst;
  1195. struct iovec iov;
  1196. struct iter_context *context;
  1197. struct req_lib_confdb_key_iter req_lib_confdb_key_iter;
  1198. struct res_lib_confdb_key_iter res_lib_confdb_key_iter;
  1199. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1200. if (error != CS_OK) {
  1201. return (error);
  1202. }
  1203. /* You MUST call confdb_key_iter_start first */
  1204. context = find_iter_context(&confdb_inst->key_iter_head, parent_object_handle);
  1205. if (!context) {
  1206. error = CS_ERR_CONTEXT_NOT_FOUND;
  1207. goto error_exit;
  1208. }
  1209. if (confdb_inst->standalone) {
  1210. error = CS_OK;
  1211. if (confdb_sa_key_iter(parent_object_handle,
  1212. context->next_entry,
  1213. key_name, key_name_len,
  1214. value, value_len))
  1215. error = CS_ERR_ACCESS;
  1216. goto sa_exit;
  1217. }
  1218. req_lib_confdb_key_iter.header.size = sizeof (struct req_lib_confdb_key_iter);
  1219. req_lib_confdb_key_iter.header.id = MESSAGE_REQ_CONFDB_KEY_ITER;
  1220. req_lib_confdb_key_iter.parent_object_handle = parent_object_handle;
  1221. req_lib_confdb_key_iter.next_entry= context->next_entry;
  1222. iov.iov_base = (char *)&req_lib_confdb_key_iter;
  1223. iov.iov_len = sizeof (struct req_lib_confdb_key_iter);
  1224. error = coroipcc_msg_send_reply_receive (
  1225. confdb_inst->handle,
  1226. &iov,
  1227. 1,
  1228. &res_lib_confdb_key_iter,
  1229. sizeof (struct res_lib_confdb_key_iter));
  1230. if (error != CS_OK) {
  1231. goto error_exit;
  1232. }
  1233. error = res_lib_confdb_key_iter.header.error;
  1234. if (error == CS_OK) {
  1235. char* key_name_str = (char*)key_name;
  1236. *key_name_len = res_lib_confdb_key_iter.key_name.length;
  1237. memcpy(key_name, res_lib_confdb_key_iter.key_name.value, *key_name_len);
  1238. key_name_str[res_lib_confdb_key_iter.key_name.length] = '\0';
  1239. *value_len = res_lib_confdb_key_iter.value.length;
  1240. memcpy(value, res_lib_confdb_key_iter.value.value, *value_len);
  1241. }
  1242. sa_exit:
  1243. context->next_entry++;
  1244. error_exit:
  1245. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1246. return (error);
  1247. }
  1248. cs_error_t confdb_key_iter_typed (
  1249. confdb_handle_t handle,
  1250. hdb_handle_t parent_object_handle,
  1251. char *key_name,
  1252. void *value,
  1253. size_t *value_len,
  1254. confdb_value_types_t *type)
  1255. {
  1256. cs_error_t error;
  1257. struct confdb_inst *confdb_inst;
  1258. struct iovec iov;
  1259. struct iter_context *context;
  1260. struct req_lib_confdb_key_iter req_lib_confdb_key_iter;
  1261. struct res_lib_confdb_key_iter_typed response;
  1262. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1263. if (error != CS_OK) {
  1264. return (error);
  1265. }
  1266. /* You MUST call confdb_key_iter_start first */
  1267. context = find_iter_context(&confdb_inst->key_iter_head, parent_object_handle);
  1268. if (!context) {
  1269. error = CS_ERR_CONTEXT_NOT_FOUND;
  1270. goto error_exit;
  1271. }
  1272. if (confdb_inst->standalone) {
  1273. error = CS_OK;
  1274. if (confdb_sa_key_iter_typed(parent_object_handle,
  1275. context->next_entry,
  1276. key_name,
  1277. value, value_len, (int*)type))
  1278. error = CS_ERR_ACCESS;
  1279. goto sa_exit;
  1280. }
  1281. req_lib_confdb_key_iter.header.size = sizeof (struct req_lib_confdb_key_iter);
  1282. req_lib_confdb_key_iter.header.id = MESSAGE_REQ_CONFDB_KEY_ITER_TYPED;
  1283. req_lib_confdb_key_iter.parent_object_handle = parent_object_handle;
  1284. req_lib_confdb_key_iter.next_entry= context->next_entry;
  1285. iov.iov_base = (char *)&req_lib_confdb_key_iter;
  1286. iov.iov_len = sizeof (struct req_lib_confdb_key_iter);
  1287. error = coroipcc_msg_send_reply_receive (
  1288. confdb_inst->handle,
  1289. &iov,
  1290. 1,
  1291. &response,
  1292. sizeof (struct res_lib_confdb_key_iter_typed));
  1293. if (error != CS_OK) {
  1294. goto error_exit;
  1295. }
  1296. error = response.header.error;
  1297. if (error == CS_OK) {
  1298. memcpy(key_name, response.key_name.value, response.key_name.length);
  1299. key_name[response.key_name.length] = '\0';
  1300. *value_len = response.value.length;
  1301. memcpy(value, response.value.value, *value_len);
  1302. *type = response.type;
  1303. }
  1304. sa_exit:
  1305. context->next_entry++;
  1306. error_exit:
  1307. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1308. return (error);
  1309. }
  1310. cs_error_t confdb_write (
  1311. confdb_handle_t handle,
  1312. char *error_text,
  1313. size_t errbuf_len)
  1314. {
  1315. cs_error_t error;
  1316. struct confdb_inst *confdb_inst;
  1317. struct iovec iov;
  1318. coroipc_request_header_t req;
  1319. struct res_lib_confdb_write res_lib_confdb_write;
  1320. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1321. if (error != CS_OK) {
  1322. /* FIXME: set error_text */
  1323. return (error);
  1324. }
  1325. if (confdb_inst->standalone) {
  1326. error = CS_OK;
  1327. if (confdb_sa_write(error_text, errbuf_len))
  1328. error = CS_ERR_ACCESS;
  1329. goto error_exit;
  1330. }
  1331. req.size = sizeof (coroipc_request_header_t);
  1332. req.id = MESSAGE_REQ_CONFDB_WRITE;
  1333. iov.iov_base = (char *)&req;
  1334. iov.iov_len = sizeof (coroipc_request_header_t);
  1335. error = coroipcc_msg_send_reply_receive (
  1336. confdb_inst->handle,
  1337. &iov,
  1338. 1,
  1339. &res_lib_confdb_write,
  1340. sizeof (struct res_lib_confdb_write));
  1341. if (error != CS_OK) {
  1342. /* FIXME: set error_text */
  1343. goto error_exit;
  1344. }
  1345. error = res_lib_confdb_write.header.error;
  1346. if (res_lib_confdb_write.error.length) {
  1347. memcpy(error_text, res_lib_confdb_write.error.value,
  1348. MIN(res_lib_confdb_write.error.length,errbuf_len));
  1349. error_text[errbuf_len-1] = '\0';
  1350. }
  1351. error_exit:
  1352. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1353. return (error);
  1354. }
  1355. cs_error_t confdb_reload (
  1356. confdb_handle_t handle,
  1357. int flush,
  1358. char *error_text,
  1359. size_t errbuf_len)
  1360. {
  1361. cs_error_t error;
  1362. struct confdb_inst *confdb_inst;
  1363. struct iovec iov;
  1364. struct res_lib_confdb_reload res_lib_confdb_reload;
  1365. struct req_lib_confdb_reload req_lib_confdb_reload;
  1366. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1367. if (error != CS_OK) {
  1368. /* FIXME: set error_text */
  1369. return (error);
  1370. }
  1371. if (confdb_inst->standalone) {
  1372. error = CS_OK;
  1373. if (confdb_sa_reload(flush, error_text, errbuf_len))
  1374. error = CS_ERR_ACCESS;
  1375. goto error_exit;
  1376. }
  1377. req_lib_confdb_reload.header.size = sizeof (req_lib_confdb_reload);
  1378. req_lib_confdb_reload.header.id = MESSAGE_REQ_CONFDB_RELOAD;
  1379. req_lib_confdb_reload.flush = flush;
  1380. iov.iov_base = (char *)&req_lib_confdb_reload;
  1381. iov.iov_len = sizeof (req_lib_confdb_reload);
  1382. error = coroipcc_msg_send_reply_receive (
  1383. confdb_inst->handle,
  1384. &iov,
  1385. 1,
  1386. &res_lib_confdb_reload,
  1387. sizeof (struct res_lib_confdb_reload));
  1388. if (error != CS_OK) {
  1389. /* FIXME: set error_text */
  1390. goto error_exit;
  1391. }
  1392. error = res_lib_confdb_reload.header.error;
  1393. if(res_lib_confdb_reload.error.length) {
  1394. memcpy(error_text, res_lib_confdb_reload.error.value,
  1395. MIN(res_lib_confdb_reload.error.length,errbuf_len));
  1396. error_text[errbuf_len-1] = '\0';
  1397. }
  1398. error_exit:
  1399. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1400. return (error);
  1401. }
  1402. cs_error_t confdb_track_changes (
  1403. confdb_handle_t handle,
  1404. hdb_handle_t object_handle,
  1405. unsigned int flags)
  1406. {
  1407. cs_error_t error;
  1408. struct confdb_inst *confdb_inst;
  1409. struct iovec iov;
  1410. struct req_lib_confdb_object_track_start req;
  1411. coroipc_response_header_t res;
  1412. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1413. if (error != CS_OK) {
  1414. return (error);
  1415. }
  1416. if (confdb_inst->standalone) {
  1417. error = CS_ERR_NOT_SUPPORTED;
  1418. goto error_exit;
  1419. }
  1420. req.header.size = sizeof (struct req_lib_confdb_object_track_start);
  1421. req.header.id = MESSAGE_REQ_CONFDB_TRACK_START;
  1422. req.object_handle = object_handle;
  1423. req.flags = flags;
  1424. iov.iov_base = (char *)&req;
  1425. iov.iov_len = sizeof (struct req_lib_confdb_object_track_start);
  1426. error = coroipcc_msg_send_reply_receive (
  1427. confdb_inst->handle,
  1428. &iov,
  1429. 1,
  1430. &res,
  1431. sizeof (coroipc_response_header_t));
  1432. if (error != CS_OK) {
  1433. goto error_exit;
  1434. }
  1435. error = res.error;
  1436. error_exit:
  1437. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1438. return (error);
  1439. }
  1440. cs_error_t confdb_stop_track_changes (confdb_handle_t handle)
  1441. {
  1442. cs_error_t error;
  1443. struct confdb_inst *confdb_inst;
  1444. struct iovec iov;
  1445. coroipc_request_header_t req;
  1446. coroipc_response_header_t res;
  1447. error = hdb_error_to_cs(hdb_handle_get (&confdb_handle_t_db, handle, (void *)&confdb_inst));
  1448. if (error != CS_OK) {
  1449. return (error);
  1450. }
  1451. if (confdb_inst->standalone) {
  1452. error = CS_ERR_NOT_SUPPORTED;
  1453. goto error_exit;
  1454. }
  1455. req.size = sizeof (coroipc_request_header_t);
  1456. req.id = MESSAGE_REQ_CONFDB_TRACK_STOP;
  1457. iov.iov_base = (char *)&req;
  1458. iov.iov_len = sizeof (coroipc_request_header_t);
  1459. error = coroipcc_msg_send_reply_receive (
  1460. confdb_inst->handle,
  1461. &iov,
  1462. 1,
  1463. &res,
  1464. sizeof (coroipc_response_header_t));
  1465. if (error != CS_OK) {
  1466. goto error_exit;
  1467. }
  1468. error = res.error;
  1469. error_exit:
  1470. (void)hdb_handle_put (&confdb_handle_t_db, handle);
  1471. return (error);
  1472. }
  1473. /**
  1474. * @}
  1475. */