confdb.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /*
  2. * Copyright (c) 2008-2009 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/mar_gen.h>
  49. #include <corosync/confdb.h>
  50. #include <corosync/ipc_confdb.h>
  51. #include <corosync/list.h>
  52. #include "sa-confdb.h"
  53. #undef MIN
  54. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  55. /* Hold the information for iterators so that
  56. callers can do recursive tree traversals.
  57. each object_handle can have its own iterator */
  58. struct iter_context {
  59. struct list_head list;
  60. hdb_handle_t parent_object_handle;
  61. hdb_handle_t find_handle;
  62. hdb_handle_t next_entry;
  63. };
  64. struct confdb_inst {
  65. void *ipc_ctx;
  66. int finalize;
  67. int standalone;
  68. confdb_callbacks_t callbacks;
  69. const void *context;
  70. pthread_mutex_t response_mutex;
  71. pthread_mutex_t dispatch_mutex;
  72. struct list_head object_find_head;
  73. struct list_head object_iter_head;
  74. struct list_head key_iter_head;
  75. };
  76. static void confdb_instance_destructor (void *instance);
  77. DECLARE_SAHDB_DATABASE(confdb_handle_t_db,confdb_instance_destructor);
  78. static cs_error_t do_find_destroy(struct confdb_inst *confdb_inst, hdb_handle_t find_handle);
  79. /* Safely tidy one iterator context list */
  80. static void free_context_list(struct confdb_inst *confdb_inst, struct list_head *list)
  81. {
  82. struct iter_context *context;
  83. struct list_head *iter, *tmp;
  84. for (iter = list->next, tmp = iter->next;
  85. iter != list; iter = tmp, tmp = iter->next) {
  86. context = list_entry (iter, struct iter_context, list);
  87. (void)do_find_destroy(confdb_inst, context->find_handle);
  88. free(context);
  89. }
  90. }
  91. /*
  92. * Clean up function for a confdb instance (confdb_initialize) handle
  93. */
  94. static void confdb_instance_destructor (void *instance)
  95. {
  96. struct confdb_inst *confdb_inst = instance;
  97. pthread_mutex_destroy (&confdb_inst->response_mutex);
  98. pthread_mutex_destroy (&confdb_inst->dispatch_mutex);
  99. }
  100. static struct iter_context *find_iter_context(struct list_head *list, hdb_handle_t object_handle)
  101. {
  102. struct iter_context *context;
  103. struct list_head *iter;
  104. for (iter = list->next;
  105. iter != list; iter = iter->next) {
  106. context = list_entry (iter, struct iter_context, list);
  107. if (context->parent_object_handle == object_handle)
  108. return context;
  109. }
  110. return NULL;
  111. }
  112. /**
  113. * @defgroup confdb_corosync
  114. * @ingroup corosync
  115. *
  116. * @{
  117. */
  118. cs_error_t confdb_initialize (
  119. confdb_handle_t *handle,
  120. confdb_callbacks_t *callbacks)
  121. {
  122. cs_error_t error;
  123. struct confdb_inst *confdb_inst;
  124. error = saHandleCreate (&confdb_handle_t_db, sizeof (struct confdb_inst), handle);
  125. if (error != CS_OK) {
  126. goto error_no_destroy;
  127. }
  128. error = saHandleInstanceGet (&confdb_handle_t_db, *handle, (void *)&confdb_inst);
  129. if (error != CS_OK) {
  130. goto error_destroy;
  131. }
  132. if (getenv("COROSYNC_DEFAULT_CONFIG_IFACE")) {
  133. error = confdb_sa_init();
  134. confdb_inst->standalone = 1;
  135. }
  136. else {
  137. error = coroipcc_service_connect (
  138. COROSYNC_SOCKET_NAME,
  139. CONFDB_SERVICE,
  140. IPC_REQUEST_SIZE,
  141. IPC_RESPONSE_SIZE,
  142. IPC_DISPATCH_SIZE,
  143. &confdb_inst->ipc_ctx);
  144. }
  145. if (error != CS_OK)
  146. goto error_put_destroy;
  147. memcpy (&confdb_inst->callbacks, callbacks, sizeof (confdb_callbacks_t));
  148. pthread_mutex_init (&confdb_inst->response_mutex, NULL);
  149. pthread_mutex_init (&confdb_inst->dispatch_mutex, NULL);
  150. list_init (&confdb_inst->object_find_head);
  151. list_init (&confdb_inst->object_iter_head);
  152. list_init (&confdb_inst->key_iter_head);
  153. (void)saHandleInstancePut (&confdb_handle_t_db, *handle);
  154. return (CS_OK);
  155. error_put_destroy:
  156. (void)saHandleInstancePut (&confdb_handle_t_db, *handle);
  157. error_destroy:
  158. (void)saHandleDestroy (&confdb_handle_t_db, *handle);
  159. error_no_destroy:
  160. return (error);
  161. }
  162. cs_error_t confdb_finalize (
  163. confdb_handle_t handle)
  164. {
  165. struct confdb_inst *confdb_inst;
  166. cs_error_t error;
  167. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  168. if (error != CS_OK) {
  169. return (error);
  170. }
  171. pthread_mutex_lock (&confdb_inst->response_mutex);
  172. /*
  173. * Another thread has already started finalizing
  174. */
  175. if (confdb_inst->finalize) {
  176. pthread_mutex_unlock (&confdb_inst->response_mutex);
  177. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  178. return (CS_ERR_BAD_HANDLE);
  179. }
  180. confdb_inst->finalize = 1;
  181. pthread_mutex_unlock (&confdb_inst->response_mutex);
  182. /* Free saved context handles */
  183. free_context_list(confdb_inst, &confdb_inst->object_find_head);
  184. free_context_list(confdb_inst, &confdb_inst->object_iter_head);
  185. free_context_list(confdb_inst, &confdb_inst->key_iter_head);
  186. if (!confdb_inst->standalone) {
  187. coroipcc_service_disconnect (confdb_inst->ipc_ctx);
  188. }
  189. (void)saHandleDestroy (&confdb_handle_t_db, handle);
  190. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  191. return (CS_OK);
  192. }
  193. cs_error_t confdb_fd_get (
  194. confdb_handle_t handle,
  195. int *fd)
  196. {
  197. cs_error_t error;
  198. struct confdb_inst *confdb_inst;
  199. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  200. if (error != CS_OK) {
  201. return (error);
  202. }
  203. *fd = coroipcc_fd_get (confdb_inst->ipc_ctx);
  204. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  205. return (CS_OK);
  206. }
  207. cs_error_t confdb_context_get (
  208. confdb_handle_t handle,
  209. const void **context)
  210. {
  211. cs_error_t error;
  212. struct confdb_inst *confdb_inst;
  213. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  214. if (error != CS_OK) {
  215. return (error);
  216. }
  217. *context = confdb_inst->context;
  218. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  219. return (CS_OK);
  220. }
  221. cs_error_t confdb_context_set (
  222. confdb_handle_t handle,
  223. const void *context)
  224. {
  225. cs_error_t error;
  226. struct confdb_inst *confdb_inst;
  227. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  228. if (error != CS_OK) {
  229. return (error);
  230. }
  231. confdb_inst->context = context;
  232. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  233. return (CS_OK);
  234. }
  235. cs_error_t confdb_dispatch (
  236. confdb_handle_t handle,
  237. cs_dispatch_flags_t dispatch_types)
  238. {
  239. int timeout = -1;
  240. cs_error_t error;
  241. int cont = 1; /* always continue do loop except when set to 0 */
  242. int dispatch_avail;
  243. struct confdb_inst *confdb_inst;
  244. confdb_callbacks_t callbacks;
  245. struct res_lib_confdb_key_change_callback *res_key_changed_pt;
  246. struct res_lib_confdb_object_create_callback *res_object_created_pt;
  247. struct res_lib_confdb_object_destroy_callback *res_object_destroyed_pt;
  248. coroipc_response_header_t *dispatch_data;
  249. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  250. if (error != CS_OK) {
  251. return (error);
  252. }
  253. if (confdb_inst->standalone) {
  254. error = CS_ERR_NOT_SUPPORTED;
  255. goto error_put;
  256. }
  257. /*
  258. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  259. * wait indefinately for SA_DISPATCH_BLOCKING
  260. */
  261. if (dispatch_types == CONFDB_DISPATCH_ALL) {
  262. timeout = 0;
  263. }
  264. do {
  265. pthread_mutex_lock (&confdb_inst->dispatch_mutex);
  266. dispatch_avail = coroipcc_dispatch_get (
  267. confdb_inst->ipc_ctx,
  268. (void **)&dispatch_data,
  269. timeout);
  270. /*
  271. * Handle has been finalized in another thread
  272. */
  273. if (confdb_inst->finalize == 1) {
  274. error = CS_OK;
  275. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  276. goto error_put;
  277. }
  278. if (dispatch_avail == 0 && dispatch_types == CONFDB_DISPATCH_ALL) {
  279. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  280. break; /* exit do while cont is 1 loop */
  281. } else
  282. if (dispatch_avail == 0) {
  283. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  284. continue; /* next poll */
  285. }
  286. /*
  287. * Make copy of callbacks, message data, unlock instance, and call callback
  288. * A risk of this dispatch method is that the callback routines may
  289. * operate at the same time that confdbFinalize has been called.
  290. */
  291. memcpy (&callbacks, &confdb_inst->callbacks, sizeof (confdb_callbacks_t));
  292. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  293. /*
  294. * Dispatch incoming message
  295. */
  296. switch (dispatch_data->id) {
  297. case MESSAGE_RES_CONFDB_KEY_CHANGE_CALLBACK:
  298. res_key_changed_pt = (struct res_lib_confdb_key_change_callback *)dispatch_data;
  299. callbacks.confdb_key_change_notify_fn(handle,
  300. res_key_changed_pt->change_type,
  301. res_key_changed_pt->object_handle,
  302. res_key_changed_pt->parent_object_handle,
  303. res_key_changed_pt->object_name.value,
  304. res_key_changed_pt->object_name.length,
  305. res_key_changed_pt->key_name.value,
  306. res_key_changed_pt->key_name.length,
  307. res_key_changed_pt->key_value.value,
  308. res_key_changed_pt->key_value.length);
  309. break;
  310. case MESSAGE_RES_CONFDB_OBJECT_CREATE_CALLBACK:
  311. res_object_created_pt = (struct res_lib_confdb_object_create_callback *)dispatch_data;
  312. callbacks.confdb_object_create_change_notify_fn(handle,
  313. res_object_created_pt->object_handle,
  314. res_object_created_pt->parent_object_handle,
  315. res_object_created_pt->name.value,
  316. res_object_created_pt->name.length);
  317. break;
  318. case MESSAGE_RES_CONFDB_OBJECT_DESTROY_CALLBACK:
  319. res_object_destroyed_pt = (struct res_lib_confdb_object_destroy_callback *)dispatch_data;
  320. callbacks.confdb_object_delete_change_notify_fn(handle,
  321. res_object_destroyed_pt->parent_object_handle,
  322. res_object_destroyed_pt->name.value,
  323. res_object_destroyed_pt->name.length);
  324. break;
  325. default:
  326. coroipcc_dispatch_put (confdb_inst->ipc_ctx);
  327. error = CS_ERR_LIBRARY;
  328. goto error_noput;
  329. break;
  330. }
  331. coroipcc_dispatch_put (confdb_inst->ipc_ctx);
  332. /*
  333. * Determine if more messages should be processed
  334. * */
  335. switch (dispatch_types) {
  336. case CONFDB_DISPATCH_ONE:
  337. cont = 0;
  338. break;
  339. case CONFDB_DISPATCH_ALL:
  340. break;
  341. case CONFDB_DISPATCH_BLOCKING:
  342. break;
  343. }
  344. } while (cont);
  345. error_put:
  346. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  347. error_noput:
  348. return (error);
  349. }
  350. cs_error_t confdb_object_create (
  351. confdb_handle_t handle,
  352. hdb_handle_t parent_object_handle,
  353. const void *object_name,
  354. size_t object_name_len,
  355. hdb_handle_t *object_handle)
  356. {
  357. cs_error_t error;
  358. struct confdb_inst *confdb_inst;
  359. struct iovec iov;
  360. struct req_lib_confdb_object_create req_lib_confdb_object_create;
  361. struct res_lib_confdb_object_create res_lib_confdb_object_create;
  362. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  363. if (error != CS_OK) {
  364. return (error);
  365. }
  366. if (confdb_inst->standalone) {
  367. error = CS_OK;
  368. if (confdb_sa_object_create(parent_object_handle,
  369. object_name, object_name_len,
  370. object_handle))
  371. error = CS_ERR_ACCESS;
  372. goto error_exit;
  373. }
  374. req_lib_confdb_object_create.header.size = sizeof (struct req_lib_confdb_object_create);
  375. req_lib_confdb_object_create.header.id = MESSAGE_REQ_CONFDB_OBJECT_CREATE;
  376. req_lib_confdb_object_create.parent_object_handle = parent_object_handle;
  377. memcpy(req_lib_confdb_object_create.object_name.value, object_name, object_name_len);
  378. req_lib_confdb_object_create.object_name.length = object_name_len;
  379. iov.iov_base = (char *)&req_lib_confdb_object_create;
  380. iov.iov_len = sizeof (struct req_lib_confdb_object_create);
  381. pthread_mutex_lock (&confdb_inst->response_mutex);
  382. error = coroipcc_msg_send_reply_receive (
  383. confdb_inst->ipc_ctx,
  384. &iov,
  385. 1,
  386. &res_lib_confdb_object_create,
  387. sizeof (struct res_lib_confdb_object_create));
  388. pthread_mutex_unlock (&confdb_inst->response_mutex);
  389. if (error != CS_OK) {
  390. goto error_exit;
  391. }
  392. error = res_lib_confdb_object_create.header.error;
  393. *object_handle = res_lib_confdb_object_create.object_handle;
  394. error_exit:
  395. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  396. return (error);
  397. }
  398. cs_error_t confdb_object_destroy (
  399. confdb_handle_t handle,
  400. hdb_handle_t object_handle)
  401. {
  402. cs_error_t error;
  403. struct confdb_inst *confdb_inst;
  404. struct iovec iov;
  405. struct req_lib_confdb_object_destroy req_lib_confdb_object_destroy;
  406. coroipc_response_header_t res;
  407. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  408. if (error != CS_OK) {
  409. return (error);
  410. }
  411. if (confdb_inst->standalone) {
  412. error = CS_OK;
  413. if (confdb_sa_object_destroy(object_handle))
  414. error = CS_ERR_ACCESS;
  415. goto error_exit;
  416. }
  417. req_lib_confdb_object_destroy.header.size = sizeof (struct req_lib_confdb_object_destroy);
  418. req_lib_confdb_object_destroy.header.id = MESSAGE_REQ_CONFDB_OBJECT_DESTROY;
  419. req_lib_confdb_object_destroy.object_handle = object_handle;
  420. iov.iov_base = (char *)&req_lib_confdb_object_destroy;
  421. iov.iov_len = sizeof (struct req_lib_confdb_object_destroy);
  422. pthread_mutex_lock (&confdb_inst->response_mutex);
  423. error = coroipcc_msg_send_reply_receive (
  424. confdb_inst->ipc_ctx,
  425. &iov,
  426. 1,
  427. &res,
  428. sizeof (coroipc_response_header_t));
  429. pthread_mutex_unlock (&confdb_inst->response_mutex);
  430. if (error != CS_OK) {
  431. goto error_exit;
  432. }
  433. error = res.error;
  434. error_exit:
  435. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  436. return (error);
  437. }
  438. cs_error_t confdb_object_parent_get (
  439. confdb_handle_t handle,
  440. hdb_handle_t object_handle,
  441. hdb_handle_t *parent_object_handle)
  442. {
  443. cs_error_t error;
  444. struct confdb_inst *confdb_inst;
  445. struct iovec iov;
  446. struct req_lib_confdb_object_parent_get req_lib_confdb_object_parent_get;
  447. struct res_lib_confdb_object_parent_get res_lib_confdb_object_parent_get;
  448. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  449. if (error != CS_OK) {
  450. return (error);
  451. }
  452. if (confdb_inst->standalone) {
  453. error = CS_OK;
  454. if (confdb_sa_object_parent_get(object_handle, parent_object_handle))
  455. error = CS_ERR_ACCESS;
  456. goto error_exit;
  457. }
  458. req_lib_confdb_object_parent_get.header.size = sizeof (struct req_lib_confdb_object_parent_get);
  459. req_lib_confdb_object_parent_get.header.id = MESSAGE_REQ_CONFDB_OBJECT_PARENT_GET;
  460. req_lib_confdb_object_parent_get.object_handle = object_handle;
  461. iov.iov_base = (char *)&req_lib_confdb_object_parent_get;
  462. iov.iov_len = sizeof (struct req_lib_confdb_object_parent_get);
  463. pthread_mutex_lock (&confdb_inst->response_mutex);
  464. error = coroipcc_msg_send_reply_receive (
  465. confdb_inst->ipc_ctx,
  466. &iov,
  467. 1,
  468. &res_lib_confdb_object_parent_get,
  469. sizeof (struct res_lib_confdb_object_parent_get));
  470. pthread_mutex_unlock (&confdb_inst->response_mutex);
  471. if (error != CS_OK) {
  472. goto error_exit;
  473. }
  474. error = res_lib_confdb_object_parent_get.header.error;
  475. *parent_object_handle = res_lib_confdb_object_parent_get.parent_object_handle;
  476. error_exit:
  477. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  478. return (error);
  479. }
  480. static cs_error_t do_find_destroy(
  481. struct confdb_inst *confdb_inst,
  482. hdb_handle_t find_handle)
  483. {
  484. cs_error_t error;
  485. struct iovec iov;
  486. struct req_lib_confdb_object_find_destroy req_lib_confdb_object_find_destroy;
  487. coroipc_response_header_t res;
  488. if (!find_handle)
  489. return CS_OK;
  490. if (confdb_inst->standalone) {
  491. error = CS_OK;
  492. if (confdb_sa_find_destroy(find_handle))
  493. error = CS_ERR_ACCESS;
  494. goto error_exit;
  495. }
  496. req_lib_confdb_object_find_destroy.header.size = sizeof (struct req_lib_confdb_object_find_destroy);
  497. req_lib_confdb_object_find_destroy.header.id = MESSAGE_REQ_CONFDB_OBJECT_FIND_DESTROY;
  498. req_lib_confdb_object_find_destroy.find_handle = find_handle;
  499. iov.iov_base = (char *)&req_lib_confdb_object_find_destroy;
  500. iov.iov_len = sizeof (struct req_lib_confdb_object_find_destroy);
  501. pthread_mutex_lock (&confdb_inst->response_mutex);
  502. error = coroipcc_msg_send_reply_receive (
  503. confdb_inst->ipc_ctx,
  504. &iov,
  505. 1,
  506. &res,
  507. sizeof (coroipc_response_header_t));
  508. pthread_mutex_unlock (&confdb_inst->response_mutex);
  509. if (error != CS_OK) {
  510. goto error_exit;
  511. }
  512. error = res.error;
  513. error_exit:
  514. return (error);
  515. }
  516. cs_error_t confdb_object_find_destroy(
  517. confdb_handle_t handle,
  518. hdb_handle_t parent_object_handle)
  519. {
  520. struct iter_context *context;
  521. cs_error_t error;
  522. struct confdb_inst *confdb_inst;
  523. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  524. if (error != CS_OK) {
  525. return (error);
  526. }
  527. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  528. error = do_find_destroy(confdb_inst, context->find_handle);
  529. if (error == CS_OK) {
  530. list_del(&context->list);
  531. free(context);
  532. }
  533. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  534. return error;
  535. }
  536. cs_error_t confdb_object_iter_destroy(
  537. confdb_handle_t handle,
  538. hdb_handle_t parent_object_handle)
  539. {
  540. struct iter_context *context;
  541. cs_error_t error;
  542. struct confdb_inst *confdb_inst;
  543. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  544. if (error != CS_OK) {
  545. return (error);
  546. }
  547. context = find_iter_context(&confdb_inst->object_iter_head, parent_object_handle);
  548. error = do_find_destroy(confdb_inst, context->find_handle);
  549. if (error == CS_OK) {
  550. list_del(&context->list);
  551. free(context);
  552. }
  553. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  554. return error;
  555. }
  556. cs_error_t confdb_key_create (
  557. confdb_handle_t handle,
  558. hdb_handle_t parent_object_handle,
  559. const void *key_name,
  560. size_t key_name_len,
  561. const void *value,
  562. size_t value_len)
  563. {
  564. cs_error_t error;
  565. struct confdb_inst *confdb_inst;
  566. struct iovec iov;
  567. struct req_lib_confdb_key_create req_lib_confdb_key_create;
  568. coroipc_response_header_t res;
  569. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  570. if (error != CS_OK) {
  571. return (error);
  572. }
  573. if (confdb_inst->standalone) {
  574. error = CS_OK;
  575. if (confdb_sa_key_create(parent_object_handle,
  576. key_name, key_name_len,
  577. value, value_len))
  578. error = CS_ERR_ACCESS;
  579. goto error_exit;
  580. }
  581. req_lib_confdb_key_create.header.size = sizeof (struct req_lib_confdb_key_create);
  582. req_lib_confdb_key_create.header.id = MESSAGE_REQ_CONFDB_KEY_CREATE;
  583. req_lib_confdb_key_create.object_handle = parent_object_handle;
  584. memcpy(req_lib_confdb_key_create.key_name.value, key_name, key_name_len);
  585. req_lib_confdb_key_create.key_name.length = key_name_len;
  586. memcpy(req_lib_confdb_key_create.value.value, value, value_len);
  587. req_lib_confdb_key_create.value.length = value_len;
  588. iov.iov_base = (char *)&req_lib_confdb_key_create;
  589. iov.iov_len = sizeof (struct req_lib_confdb_key_create);
  590. pthread_mutex_lock (&confdb_inst->response_mutex);
  591. error = coroipcc_msg_send_reply_receive (
  592. confdb_inst->ipc_ctx,
  593. &iov,
  594. 1,
  595. &res,
  596. sizeof (res));
  597. pthread_mutex_unlock (&confdb_inst->response_mutex);
  598. if (error != CS_OK) {
  599. goto error_exit;
  600. }
  601. error = res.error;
  602. error_exit:
  603. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  604. return (error);
  605. }
  606. cs_error_t confdb_key_delete (
  607. confdb_handle_t handle,
  608. hdb_handle_t parent_object_handle,
  609. const void *key_name,
  610. size_t key_name_len,
  611. const void *value,
  612. size_t value_len)
  613. {
  614. cs_error_t error;
  615. struct confdb_inst *confdb_inst;
  616. struct iovec iov;
  617. struct req_lib_confdb_key_delete req_lib_confdb_key_delete;
  618. coroipc_response_header_t res;
  619. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  620. if (error != CS_OK) {
  621. return (error);
  622. }
  623. if (confdb_inst->standalone) {
  624. error = CS_OK;
  625. if (confdb_sa_key_delete(parent_object_handle,
  626. key_name, key_name_len,
  627. value, value_len))
  628. error = CS_ERR_ACCESS;
  629. goto error_exit;
  630. }
  631. req_lib_confdb_key_delete.header.size = sizeof (struct req_lib_confdb_key_delete);
  632. req_lib_confdb_key_delete.header.id = MESSAGE_REQ_CONFDB_KEY_DELETE;
  633. req_lib_confdb_key_delete.object_handle = parent_object_handle;
  634. memcpy(req_lib_confdb_key_delete.key_name.value, key_name, key_name_len);
  635. req_lib_confdb_key_delete.key_name.length = key_name_len;
  636. memcpy(req_lib_confdb_key_delete.value.value, value, value_len);
  637. req_lib_confdb_key_delete.value.length = value_len;
  638. iov.iov_base = (char *)&req_lib_confdb_key_delete;
  639. iov.iov_len = sizeof (struct req_lib_confdb_key_delete);
  640. pthread_mutex_lock (&confdb_inst->response_mutex);
  641. error = coroipcc_msg_send_reply_receive (
  642. confdb_inst->ipc_ctx,
  643. &iov,
  644. 1,
  645. &res,
  646. sizeof (res));
  647. pthread_mutex_unlock (&confdb_inst->response_mutex);
  648. if (error != CS_OK) {
  649. goto error_exit;
  650. }
  651. error = res.error;
  652. error_exit:
  653. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  654. return (error);
  655. }
  656. cs_error_t confdb_key_get (
  657. confdb_handle_t handle,
  658. hdb_handle_t parent_object_handle,
  659. const void *key_name,
  660. size_t key_name_len,
  661. void *value,
  662. size_t *value_len)
  663. {
  664. cs_error_t error;
  665. struct confdb_inst *confdb_inst;
  666. struct iovec iov;
  667. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  668. struct res_lib_confdb_key_get res_lib_confdb_key_get;
  669. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  670. if (error != CS_OK) {
  671. return (error);
  672. }
  673. if (confdb_inst->standalone) {
  674. error = CS_OK;
  675. if (confdb_sa_key_get(parent_object_handle,
  676. key_name, key_name_len,
  677. value, value_len))
  678. error = CS_ERR_ACCESS;
  679. goto error_exit;
  680. }
  681. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  682. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_GET;
  683. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  684. memcpy(req_lib_confdb_key_get.key_name.value, key_name, key_name_len);
  685. req_lib_confdb_key_get.key_name.length = key_name_len;
  686. iov.iov_base = (char *)&req_lib_confdb_key_get;
  687. iov.iov_len = sizeof (struct req_lib_confdb_key_get);
  688. pthread_mutex_lock (&confdb_inst->response_mutex);
  689. error = coroipcc_msg_send_reply_receive (
  690. confdb_inst->ipc_ctx,
  691. &iov,
  692. 1,
  693. &res_lib_confdb_key_get,
  694. sizeof (struct res_lib_confdb_key_get));
  695. pthread_mutex_unlock (&confdb_inst->response_mutex);
  696. if (error != CS_OK) {
  697. goto error_exit;
  698. }
  699. error = res_lib_confdb_key_get.header.error;
  700. if (error == CS_OK) {
  701. *value_len = res_lib_confdb_key_get.value.length;
  702. memcpy(value, res_lib_confdb_key_get.value.value, *value_len);
  703. }
  704. error_exit:
  705. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  706. return (error);
  707. }
  708. cs_error_t confdb_key_increment (
  709. confdb_handle_t handle,
  710. hdb_handle_t parent_object_handle,
  711. const void *key_name,
  712. size_t key_name_len,
  713. unsigned int *value)
  714. {
  715. cs_error_t error;
  716. struct confdb_inst *confdb_inst;
  717. struct iovec iov;
  718. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  719. struct res_lib_confdb_key_incdec res_lib_confdb_key_incdec;
  720. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  721. if (error != CS_OK) {
  722. return (error);
  723. }
  724. if (confdb_inst->standalone) {
  725. error = CS_OK;
  726. if (confdb_sa_key_increment(parent_object_handle,
  727. key_name, key_name_len,
  728. value))
  729. error = CS_ERR_ACCESS;
  730. goto error_exit;
  731. }
  732. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  733. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_INCREMENT;
  734. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  735. memcpy(req_lib_confdb_key_get.key_name.value, key_name, key_name_len);
  736. req_lib_confdb_key_get.key_name.length = key_name_len;
  737. iov.iov_base = (char *)&req_lib_confdb_key_get;
  738. iov.iov_len = sizeof (struct req_lib_confdb_key_get);
  739. pthread_mutex_lock (&confdb_inst->response_mutex);
  740. error = coroipcc_msg_send_reply_receive (
  741. confdb_inst->ipc_ctx,
  742. &iov,
  743. 1,
  744. &res_lib_confdb_key_incdec,
  745. sizeof (struct res_lib_confdb_key_incdec));
  746. pthread_mutex_unlock (&confdb_inst->response_mutex);
  747. if (error != CS_OK) {
  748. goto error_exit;
  749. }
  750. error = res_lib_confdb_key_incdec.header.error;
  751. if (error == CS_OK) {
  752. *value = res_lib_confdb_key_incdec.value;
  753. }
  754. error_exit:
  755. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  756. return (error);
  757. }
  758. cs_error_t confdb_key_decrement (
  759. confdb_handle_t handle,
  760. hdb_handle_t parent_object_handle,
  761. const void *key_name,
  762. size_t key_name_len,
  763. unsigned int *value)
  764. {
  765. cs_error_t error;
  766. struct confdb_inst *confdb_inst;
  767. struct iovec iov;
  768. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  769. struct res_lib_confdb_key_incdec res_lib_confdb_key_incdec;
  770. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  771. if (error != CS_OK) {
  772. return (error);
  773. }
  774. if (confdb_inst->standalone) {
  775. error = CS_OK;
  776. if (confdb_sa_key_decrement(parent_object_handle,
  777. key_name, key_name_len,
  778. value))
  779. error = CS_ERR_ACCESS;
  780. goto error_exit;
  781. }
  782. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  783. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_DECREMENT;
  784. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  785. memcpy(req_lib_confdb_key_get.key_name.value, key_name, key_name_len);
  786. req_lib_confdb_key_get.key_name.length = key_name_len;
  787. iov.iov_base = (char *)&req_lib_confdb_key_get;
  788. iov.iov_len = sizeof (struct req_lib_confdb_key_get);
  789. pthread_mutex_lock (&confdb_inst->response_mutex);
  790. error = coroipcc_msg_send_reply_receive (
  791. confdb_inst->ipc_ctx,
  792. &iov,
  793. 1,
  794. &res_lib_confdb_key_incdec,
  795. sizeof (struct res_lib_confdb_key_incdec));
  796. pthread_mutex_unlock (&confdb_inst->response_mutex);
  797. if (error != CS_OK) {
  798. goto error_exit;
  799. }
  800. error = res_lib_confdb_key_incdec.header.error;
  801. if (error == CS_OK) {
  802. *value = res_lib_confdb_key_incdec.value;
  803. }
  804. error_exit:
  805. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  806. return (error);
  807. }
  808. cs_error_t confdb_key_replace (
  809. confdb_handle_t handle,
  810. hdb_handle_t parent_object_handle,
  811. const void *key_name,
  812. size_t key_name_len,
  813. const void *old_value,
  814. size_t old_value_len,
  815. const void *new_value,
  816. size_t new_value_len)
  817. {
  818. cs_error_t error;
  819. struct confdb_inst *confdb_inst;
  820. struct iovec iov;
  821. struct req_lib_confdb_key_replace req_lib_confdb_key_replace;
  822. coroipc_response_header_t res;
  823. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  824. if (error != CS_OK) {
  825. return (error);
  826. }
  827. if (confdb_inst->standalone) {
  828. error = CS_OK;
  829. if (confdb_sa_key_replace(parent_object_handle,
  830. key_name, key_name_len,
  831. old_value, old_value_len,
  832. new_value, new_value_len))
  833. error = CS_ERR_ACCESS;
  834. goto error_exit;
  835. }
  836. req_lib_confdb_key_replace.header.size = sizeof (struct req_lib_confdb_key_replace);
  837. req_lib_confdb_key_replace.header.id = MESSAGE_REQ_CONFDB_KEY_REPLACE;
  838. req_lib_confdb_key_replace.object_handle = parent_object_handle;
  839. memcpy(req_lib_confdb_key_replace.key_name.value, key_name, key_name_len);
  840. req_lib_confdb_key_replace.key_name.length = key_name_len;
  841. memcpy(req_lib_confdb_key_replace.old_value.value, old_value, old_value_len);
  842. req_lib_confdb_key_replace.old_value.length = old_value_len;
  843. memcpy(req_lib_confdb_key_replace.new_value.value, new_value, new_value_len);
  844. req_lib_confdb_key_replace.new_value.length = new_value_len;
  845. iov.iov_base = (char *)&req_lib_confdb_key_replace;
  846. iov.iov_len = sizeof (struct req_lib_confdb_key_replace);
  847. pthread_mutex_lock (&confdb_inst->response_mutex);
  848. error = coroipcc_msg_send_reply_receive (
  849. confdb_inst->ipc_ctx,
  850. &iov,
  851. 1,
  852. &res,
  853. sizeof (res));
  854. pthread_mutex_unlock (&confdb_inst->response_mutex);
  855. if (error != CS_OK) {
  856. goto error_exit;
  857. }
  858. error = res.error;
  859. error_exit:
  860. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  861. return (error);
  862. }
  863. cs_error_t confdb_object_iter_start (
  864. confdb_handle_t handle,
  865. hdb_handle_t object_handle)
  866. {
  867. struct confdb_inst *confdb_inst;
  868. cs_error_t error = CS_OK;
  869. struct iter_context *context;
  870. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  871. if (error != CS_OK) {
  872. return (error);
  873. }
  874. context = find_iter_context(&confdb_inst->object_iter_head, object_handle);
  875. if (!context) {
  876. context = malloc(sizeof(struct iter_context));
  877. if (!context) {
  878. error = CS_ERR_NO_MEMORY;
  879. goto ret;
  880. }
  881. context->parent_object_handle = object_handle;
  882. context->find_handle = 0;
  883. list_add(&context->list, &confdb_inst->object_iter_head);
  884. }
  885. /* Start a new find context */
  886. if (context->find_handle) {
  887. (void)do_find_destroy(confdb_inst, context->find_handle);
  888. context->find_handle = 0;
  889. }
  890. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  891. ret:
  892. return error;
  893. }
  894. cs_error_t confdb_key_iter_start (
  895. confdb_handle_t handle,
  896. hdb_handle_t object_handle)
  897. {
  898. struct confdb_inst *confdb_inst;
  899. cs_error_t error = CS_OK;
  900. struct iter_context *context;
  901. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  902. if (error != CS_OK) {
  903. return (error);
  904. }
  905. context = find_iter_context(&confdb_inst->key_iter_head, object_handle);
  906. if (!context) {
  907. context = malloc(sizeof(struct iter_context));
  908. if (!context) {
  909. error = CS_ERR_NO_MEMORY;
  910. goto ret;
  911. }
  912. context->parent_object_handle = object_handle;
  913. list_add(&context->list, &confdb_inst->key_iter_head);
  914. }
  915. context->find_handle = 0;
  916. context->next_entry = 0;
  917. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  918. ret:
  919. return error;
  920. }
  921. cs_error_t confdb_object_find_start (
  922. confdb_handle_t handle,
  923. hdb_handle_t parent_object_handle)
  924. {
  925. struct confdb_inst *confdb_inst;
  926. cs_error_t error = CS_OK;
  927. struct iter_context *context;
  928. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  929. if (error != CS_OK) {
  930. return (error);
  931. }
  932. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  933. if (!context) {
  934. context = malloc(sizeof(struct iter_context));
  935. if (!context) {
  936. error = CS_ERR_NO_MEMORY;
  937. goto ret;
  938. }
  939. context->find_handle = 0;
  940. context->parent_object_handle = parent_object_handle;
  941. list_add(&context->list, &confdb_inst->object_find_head);
  942. }
  943. /* Start a new find context */
  944. if (context->find_handle) {
  945. (void)do_find_destroy(confdb_inst, context->find_handle);
  946. context->find_handle = 0;
  947. }
  948. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  949. ret:
  950. return error;
  951. }
  952. cs_error_t confdb_object_find (
  953. confdb_handle_t handle,
  954. hdb_handle_t parent_object_handle,
  955. const void *object_name,
  956. size_t object_name_len,
  957. hdb_handle_t *object_handle)
  958. {
  959. cs_error_t error;
  960. struct confdb_inst *confdb_inst;
  961. struct iovec iov;
  962. struct iter_context *context;
  963. struct req_lib_confdb_object_find req_lib_confdb_object_find;
  964. struct res_lib_confdb_object_find res_lib_confdb_object_find;
  965. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  966. if (error != CS_OK) {
  967. return (error);
  968. }
  969. /* You MUST call confdb_object_find_start first */
  970. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  971. if (!context) {
  972. error = CS_ERR_CONTEXT_NOT_FOUND;
  973. goto error_exit;
  974. }
  975. if (confdb_inst->standalone) {
  976. error = CS_OK;
  977. if (confdb_sa_object_find(parent_object_handle,
  978. &context->find_handle,
  979. object_handle,
  980. object_name, object_name_len))
  981. error = CS_ERR_ACCESS;
  982. goto error_exit;
  983. }
  984. req_lib_confdb_object_find.header.size = sizeof (struct req_lib_confdb_object_find);
  985. req_lib_confdb_object_find.header.id = MESSAGE_REQ_CONFDB_OBJECT_FIND;
  986. req_lib_confdb_object_find.parent_object_handle = parent_object_handle;
  987. req_lib_confdb_object_find.find_handle = context->find_handle;
  988. memcpy(req_lib_confdb_object_find.object_name.value, object_name, object_name_len);
  989. req_lib_confdb_object_find.object_name.length = object_name_len;
  990. iov.iov_base = (char *)&req_lib_confdb_object_find;
  991. iov.iov_len = sizeof (struct req_lib_confdb_object_find);
  992. pthread_mutex_lock (&confdb_inst->response_mutex);
  993. error = coroipcc_msg_send_reply_receive (
  994. confdb_inst->ipc_ctx,
  995. &iov,
  996. 1,
  997. &res_lib_confdb_object_find,
  998. sizeof (struct res_lib_confdb_object_find));
  999. pthread_mutex_unlock (&confdb_inst->response_mutex);
  1000. if (error != CS_OK) {
  1001. goto error_exit;
  1002. }
  1003. error = res_lib_confdb_object_find.header.error;
  1004. *object_handle = res_lib_confdb_object_find.object_handle;
  1005. context->find_handle = res_lib_confdb_object_find.find_handle;
  1006. error_exit:
  1007. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  1008. return (error);
  1009. }
  1010. cs_error_t confdb_object_iter (
  1011. confdb_handle_t handle,
  1012. hdb_handle_t parent_object_handle,
  1013. hdb_handle_t *object_handle,
  1014. void *object_name,
  1015. size_t *object_name_len)
  1016. {
  1017. cs_error_t error;
  1018. struct confdb_inst *confdb_inst;
  1019. struct iovec iov;
  1020. struct iter_context *context;
  1021. struct req_lib_confdb_object_iter req_lib_confdb_object_iter;
  1022. struct res_lib_confdb_object_iter res_lib_confdb_object_iter;
  1023. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  1024. if (error != CS_OK) {
  1025. return (error);
  1026. }
  1027. /* You MUST call confdb_object_iter_start first */
  1028. context = find_iter_context(&confdb_inst->object_iter_head, parent_object_handle);
  1029. if (!context) {
  1030. error = CS_ERR_CONTEXT_NOT_FOUND;
  1031. goto error_exit;
  1032. }
  1033. if (confdb_inst->standalone) {
  1034. error = CS_OK;
  1035. *object_name_len = 0;
  1036. if (confdb_sa_object_iter(parent_object_handle,
  1037. &context->find_handle,
  1038. object_handle,
  1039. NULL, 0,
  1040. object_name, object_name_len))
  1041. error = CS_ERR_ACCESS;
  1042. goto sa_exit;
  1043. }
  1044. req_lib_confdb_object_iter.header.size = sizeof (struct req_lib_confdb_object_iter);
  1045. req_lib_confdb_object_iter.header.id = MESSAGE_REQ_CONFDB_OBJECT_ITER;
  1046. req_lib_confdb_object_iter.parent_object_handle = parent_object_handle;
  1047. req_lib_confdb_object_iter.find_handle = context->find_handle;
  1048. iov.iov_base = (char *)&req_lib_confdb_object_iter;
  1049. iov.iov_len = sizeof (struct req_lib_confdb_object_iter);
  1050. pthread_mutex_lock (&confdb_inst->response_mutex);
  1051. error = coroipcc_msg_send_reply_receive (
  1052. confdb_inst->ipc_ctx,
  1053. &iov,
  1054. 1,
  1055. &res_lib_confdb_object_iter,
  1056. sizeof (struct res_lib_confdb_object_iter));
  1057. pthread_mutex_unlock (&confdb_inst->response_mutex);
  1058. if (error != CS_OK) {
  1059. goto error_exit;
  1060. }
  1061. error = res_lib_confdb_object_iter.header.error;
  1062. if (error == CS_OK) {
  1063. *object_name_len = res_lib_confdb_object_iter.object_name.length;
  1064. memcpy(object_name, res_lib_confdb_object_iter.object_name.value, *object_name_len);
  1065. *object_handle = res_lib_confdb_object_iter.object_handle;
  1066. context->find_handle = res_lib_confdb_object_iter.find_handle;
  1067. }
  1068. sa_exit:
  1069. error_exit:
  1070. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  1071. return (error);
  1072. }
  1073. cs_error_t confdb_key_iter (
  1074. confdb_handle_t handle,
  1075. hdb_handle_t parent_object_handle,
  1076. void *key_name,
  1077. size_t *key_name_len,
  1078. void *value,
  1079. size_t *value_len)
  1080. {
  1081. cs_error_t error;
  1082. struct confdb_inst *confdb_inst;
  1083. struct iovec iov;
  1084. struct iter_context *context;
  1085. struct req_lib_confdb_key_iter req_lib_confdb_key_iter;
  1086. struct res_lib_confdb_key_iter res_lib_confdb_key_iter;
  1087. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  1088. if (error != CS_OK) {
  1089. return (error);
  1090. }
  1091. /* You MUST call confdb_key_iter_start first */
  1092. context = find_iter_context(&confdb_inst->key_iter_head, parent_object_handle);
  1093. if (!context) {
  1094. error = CS_ERR_CONTEXT_NOT_FOUND;
  1095. goto error_exit;
  1096. }
  1097. if (confdb_inst->standalone) {
  1098. error = CS_OK;
  1099. if (confdb_sa_key_iter(parent_object_handle,
  1100. context->next_entry,
  1101. key_name, key_name_len,
  1102. value, value_len))
  1103. error = CS_ERR_ACCESS;
  1104. goto sa_exit;
  1105. }
  1106. req_lib_confdb_key_iter.header.size = sizeof (struct req_lib_confdb_key_iter);
  1107. req_lib_confdb_key_iter.header.id = MESSAGE_REQ_CONFDB_KEY_ITER;
  1108. req_lib_confdb_key_iter.parent_object_handle = parent_object_handle;
  1109. req_lib_confdb_key_iter.next_entry= context->next_entry;
  1110. iov.iov_base = (char *)&req_lib_confdb_key_iter;
  1111. iov.iov_len = sizeof (struct req_lib_confdb_key_iter);
  1112. pthread_mutex_lock (&confdb_inst->response_mutex);
  1113. error = coroipcc_msg_send_reply_receive (
  1114. confdb_inst->ipc_ctx,
  1115. &iov,
  1116. 1,
  1117. &res_lib_confdb_key_iter,
  1118. sizeof (struct res_lib_confdb_key_iter));
  1119. pthread_mutex_unlock (&confdb_inst->response_mutex);
  1120. if (error != CS_OK) {
  1121. goto error_exit;
  1122. }
  1123. error = res_lib_confdb_key_iter.header.error;
  1124. if (error == CS_OK) {
  1125. *key_name_len = res_lib_confdb_key_iter.key_name.length;
  1126. memcpy(key_name, res_lib_confdb_key_iter.key_name.value, *key_name_len);
  1127. *value_len = res_lib_confdb_key_iter.value.length;
  1128. memcpy(value, res_lib_confdb_key_iter.value.value, *value_len);
  1129. }
  1130. sa_exit:
  1131. context->next_entry++;
  1132. error_exit:
  1133. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  1134. return (error);
  1135. }
  1136. cs_error_t confdb_write (
  1137. confdb_handle_t handle,
  1138. char *error_text,
  1139. size_t errbuf_len)
  1140. {
  1141. cs_error_t error;
  1142. struct confdb_inst *confdb_inst;
  1143. struct iovec iov;
  1144. coroipc_request_header_t req;
  1145. struct res_lib_confdb_write res_lib_confdb_write;
  1146. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  1147. if (error != CS_OK) {
  1148. /* FIXME: set error_text */
  1149. return (error);
  1150. }
  1151. if (confdb_inst->standalone) {
  1152. error = CS_OK;
  1153. if (confdb_sa_write(error_text, errbuf_len))
  1154. error = CS_ERR_ACCESS;
  1155. goto error_exit;
  1156. }
  1157. req.size = sizeof (coroipc_request_header_t);
  1158. req.id = MESSAGE_REQ_CONFDB_WRITE;
  1159. iov.iov_base = (char *)&req;
  1160. iov.iov_len = sizeof (coroipc_request_header_t);
  1161. pthread_mutex_lock (&confdb_inst->response_mutex);
  1162. error = coroipcc_msg_send_reply_receive (
  1163. confdb_inst->ipc_ctx,
  1164. &iov,
  1165. 1,
  1166. &res_lib_confdb_write,
  1167. sizeof (struct res_lib_confdb_write));
  1168. pthread_mutex_unlock (&confdb_inst->response_mutex);
  1169. if (error != CS_OK) {
  1170. /* FIXME: set error_text */
  1171. goto error_exit;
  1172. }
  1173. error = res_lib_confdb_write.header.error;
  1174. if (res_lib_confdb_write.error.length) {
  1175. memcpy(error_text, res_lib_confdb_write.error.value,
  1176. MIN(res_lib_confdb_write.error.length,errbuf_len));
  1177. error_text[errbuf_len-1] = '\0';
  1178. }
  1179. error_exit:
  1180. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  1181. return (error);
  1182. }
  1183. cs_error_t confdb_reload (
  1184. confdb_handle_t handle,
  1185. int flush,
  1186. char *error_text,
  1187. size_t errbuf_len)
  1188. {
  1189. cs_error_t error;
  1190. struct confdb_inst *confdb_inst;
  1191. struct iovec iov;
  1192. struct res_lib_confdb_reload res_lib_confdb_reload;
  1193. struct req_lib_confdb_reload req_lib_confdb_reload;
  1194. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  1195. if (error != CS_OK) {
  1196. /* FIXME: set error_text */
  1197. return (error);
  1198. }
  1199. if (confdb_inst->standalone) {
  1200. error = CS_OK;
  1201. if (confdb_sa_reload(flush, error_text, errbuf_len))
  1202. error = CS_ERR_ACCESS;
  1203. goto error_exit;
  1204. }
  1205. req_lib_confdb_reload.header.size = sizeof (req_lib_confdb_reload);
  1206. req_lib_confdb_reload.header.id = MESSAGE_REQ_CONFDB_RELOAD;
  1207. req_lib_confdb_reload.flush = flush;
  1208. iov.iov_base = (char *)&req_lib_confdb_reload;
  1209. iov.iov_len = sizeof (req_lib_confdb_reload);
  1210. pthread_mutex_lock (&confdb_inst->response_mutex);
  1211. error = coroipcc_msg_send_reply_receive (
  1212. confdb_inst->ipc_ctx,
  1213. &iov,
  1214. 1,
  1215. &res_lib_confdb_reload,
  1216. sizeof (struct res_lib_confdb_reload));
  1217. pthread_mutex_unlock (&confdb_inst->response_mutex);
  1218. if (error != CS_OK) {
  1219. /* FIXME: set error_text */
  1220. goto error_exit;
  1221. }
  1222. error = res_lib_confdb_reload.header.error;
  1223. if(res_lib_confdb_reload.error.length) {
  1224. memcpy(error_text, res_lib_confdb_reload.error.value,
  1225. MIN(res_lib_confdb_reload.error.length,errbuf_len));
  1226. error_text[errbuf_len-1] = '\0';
  1227. }
  1228. error_exit:
  1229. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  1230. return (error);
  1231. }
  1232. cs_error_t confdb_track_changes (
  1233. confdb_handle_t handle,
  1234. hdb_handle_t object_handle,
  1235. unsigned int flags)
  1236. {
  1237. cs_error_t error;
  1238. struct confdb_inst *confdb_inst;
  1239. struct iovec iov;
  1240. struct req_lib_confdb_object_track_start req;
  1241. coroipc_response_header_t res;
  1242. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  1243. if (error != CS_OK) {
  1244. return (error);
  1245. }
  1246. if (confdb_inst->standalone) {
  1247. error = CS_ERR_NOT_SUPPORTED;
  1248. goto error_exit;
  1249. }
  1250. req.header.size = sizeof (struct req_lib_confdb_object_track_start);
  1251. req.header.id = MESSAGE_REQ_CONFDB_TRACK_START;
  1252. req.object_handle = object_handle;
  1253. req.flags = flags;
  1254. iov.iov_base = (char *)&req;
  1255. iov.iov_len = sizeof (struct req_lib_confdb_object_track_start);
  1256. pthread_mutex_lock (&confdb_inst->response_mutex);
  1257. error = coroipcc_msg_send_reply_receive (
  1258. confdb_inst->ipc_ctx,
  1259. &iov,
  1260. 1,
  1261. &res,
  1262. sizeof (coroipc_response_header_t));
  1263. pthread_mutex_unlock (&confdb_inst->response_mutex);
  1264. if (error != CS_OK) {
  1265. goto error_exit;
  1266. }
  1267. error = res.error;
  1268. error_exit:
  1269. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  1270. return (error);
  1271. }
  1272. cs_error_t confdb_stop_track_changes (confdb_handle_t handle)
  1273. {
  1274. cs_error_t error;
  1275. struct confdb_inst *confdb_inst;
  1276. struct iovec iov;
  1277. coroipc_request_header_t req;
  1278. coroipc_response_header_t res;
  1279. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  1280. if (error != CS_OK) {
  1281. return (error);
  1282. }
  1283. if (confdb_inst->standalone) {
  1284. error = CS_ERR_NOT_SUPPORTED;
  1285. goto error_exit;
  1286. }
  1287. req.size = sizeof (coroipc_request_header_t);
  1288. req.id = MESSAGE_REQ_CONFDB_TRACK_STOP;
  1289. iov.iov_base = (char *)&req;
  1290. iov.iov_len = sizeof (coroipc_request_header_t);
  1291. pthread_mutex_lock (&confdb_inst->response_mutex);
  1292. error = coroipcc_msg_send_reply_receive (
  1293. confdb_inst->ipc_ctx,
  1294. &iov,
  1295. 1,
  1296. &res,
  1297. sizeof (coroipc_response_header_t));
  1298. pthread_mutex_unlock (&confdb_inst->response_mutex);
  1299. if (error != CS_OK) {
  1300. goto error_exit;
  1301. }
  1302. error = res.error;
  1303. error_exit:
  1304. (void)saHandleInstancePut (&confdb_handle_t_db, handle);
  1305. return (error);
  1306. }