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