confdb.c 42 KB

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