confdb.c 46 KB

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