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