confdb.c 42 KB

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