confdb.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  1. /*
  2. * Copyright (c) 2008-2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Provides access to data in the corosync object database
  36. */
  37. #include <config.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <pthread.h>
  42. #include <sys/types.h>
  43. #include <errno.h>
  44. #include <corosync/corotypes.h>
  45. #include <corosync/coroipc_types.h>
  46. #include <corosync/coroipcc.h>
  47. #include <corosync/corodefs.h>
  48. #include <corosync/mar_gen.h>
  49. #include <corosync/confdb.h>
  50. #include <corosync/ipc_confdb.h>
  51. #include <corosync/list.h>
  52. #include "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. }