confdb.c 42 KB

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