confdb.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /*
  2. * Copyright (c) 2008 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 openais object database
  36. */
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include <pthread.h>
  41. #include <sys/types.h>
  42. #include <errno.h>
  43. #include <saAis.h>
  44. #include <confdb.h>
  45. #include <ipc_confdb.h>
  46. #include <mar_gen.h>
  47. #include <ais_util.h>
  48. #include <list.h>
  49. #include "sa-confdb.h"
  50. /* Hold the information for iterators so that
  51. callers can do recursive tree traversals.
  52. each object_handle can have its own iterator */
  53. struct iter_context {
  54. struct list_head list;
  55. uint32_t parent_object_handle;
  56. uint32_t context;
  57. };
  58. struct confdb_inst {
  59. int response_fd;
  60. int dispatch_fd;
  61. int finalize;
  62. int standalone;
  63. confdb_callbacks_t callbacks;
  64. void *context;
  65. pthread_mutex_t response_mutex;
  66. pthread_mutex_t dispatch_mutex;
  67. struct list_head object_find_head;
  68. struct list_head object_iter_head;
  69. struct list_head key_iter_head;
  70. };
  71. static void confdb_instance_destructor (void *instance);
  72. static struct saHandleDatabase confdb_handle_t_db = {
  73. .handleCount = 0,
  74. .handles = 0,
  75. .mutex = PTHREAD_MUTEX_INITIALIZER,
  76. .handleInstanceDestructor = confdb_instance_destructor
  77. };
  78. /* Safely tidy one iterator context list */
  79. static void free_context_list(struct list_head *list)
  80. {
  81. struct iter_context *context;
  82. struct list_head *iter, *tmp;
  83. for (iter = list->next, tmp = iter->next;
  84. iter != list; iter = tmp, tmp = iter->next) {
  85. context = list_entry (iter, struct iter_context, list);
  86. free(context);
  87. }
  88. }
  89. /*
  90. * Clean up function for a confdb instance (confdb_initialize) handle
  91. */
  92. static void confdb_instance_destructor (void *instance)
  93. {
  94. struct confdb_inst *confdb_inst = instance;
  95. pthread_mutex_destroy (&confdb_inst->response_mutex);
  96. pthread_mutex_destroy (&confdb_inst->dispatch_mutex);
  97. }
  98. static struct iter_context *find_iter_context(struct list_head *list, unsigned int object_handle)
  99. {
  100. struct iter_context *context;
  101. struct list_head *iter;
  102. for (iter = list->next;
  103. iter != list; iter = iter->next) {
  104. context = list_entry (iter, struct iter_context, list);
  105. if (context->parent_object_handle == object_handle)
  106. return context;
  107. }
  108. return NULL;
  109. }
  110. /**
  111. * @defgroup confdb_openais
  112. * @ingroup openais
  113. *
  114. * @{
  115. */
  116. confdb_error_t confdb_initialize (
  117. confdb_handle_t *handle,
  118. confdb_callbacks_t *callbacks)
  119. {
  120. SaAisErrorT error;
  121. struct confdb_inst *confdb_inst;
  122. error = saHandleCreate (&confdb_handle_t_db, sizeof (struct confdb_inst), handle);
  123. if (error != SA_AIS_OK) {
  124. goto error_no_destroy;
  125. }
  126. error = saHandleInstanceGet (&confdb_handle_t_db, *handle, (void *)&confdb_inst);
  127. if (error != SA_AIS_OK) {
  128. goto error_destroy;
  129. }
  130. if (getenv("OPENAIS_DEFAULT_CONFIG_IFACE")) {
  131. error = confdb_sa_init();
  132. confdb_inst->standalone = 1;
  133. }
  134. else {
  135. error = saServiceConnect (&confdb_inst->dispatch_fd,
  136. &confdb_inst->response_fd,
  137. CONFDB_SERVICE);
  138. }
  139. if (error != SA_AIS_OK)
  140. goto error_put_destroy;
  141. memcpy (&confdb_inst->callbacks, callbacks, sizeof (confdb_callbacks_t));
  142. pthread_mutex_init (&confdb_inst->response_mutex, NULL);
  143. pthread_mutex_init (&confdb_inst->dispatch_mutex, NULL);
  144. list_init (&confdb_inst->object_find_head);
  145. list_init (&confdb_inst->object_iter_head);
  146. list_init (&confdb_inst->key_iter_head);
  147. saHandleInstancePut (&confdb_handle_t_db, *handle);
  148. return (SA_AIS_OK);
  149. error_put_destroy:
  150. saHandleInstancePut (&confdb_handle_t_db, *handle);
  151. error_destroy:
  152. saHandleDestroy (&confdb_handle_t_db, *handle);
  153. error_no_destroy:
  154. return (error);
  155. }
  156. confdb_error_t confdb_finalize (
  157. confdb_handle_t handle)
  158. {
  159. struct confdb_inst *confdb_inst;
  160. SaAisErrorT error;
  161. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  162. if (error != SA_AIS_OK) {
  163. return (error);
  164. }
  165. pthread_mutex_lock (&confdb_inst->response_mutex);
  166. /*
  167. * Another thread has already started finalizing
  168. */
  169. if (confdb_inst->finalize) {
  170. pthread_mutex_unlock (&confdb_inst->response_mutex);
  171. saHandleInstancePut (&confdb_handle_t_db, handle);
  172. return (CONFDB_ERR_BAD_HANDLE);
  173. }
  174. confdb_inst->finalize = 1;
  175. pthread_mutex_unlock (&confdb_inst->response_mutex);
  176. saHandleDestroy (&confdb_handle_t_db, handle);
  177. /* Free saved context handles */
  178. free_context_list(&confdb_inst->object_find_head);
  179. free_context_list(&confdb_inst->object_iter_head);
  180. free_context_list(&confdb_inst->key_iter_head);
  181. if (!confdb_inst->standalone) {
  182. /*
  183. * Disconnect from the server
  184. */
  185. if (confdb_inst->response_fd != -1) {
  186. shutdown(confdb_inst->response_fd, 0);
  187. close(confdb_inst->response_fd);
  188. }
  189. if (confdb_inst->dispatch_fd != -1) {
  190. shutdown(confdb_inst->dispatch_fd, 0);
  191. close(confdb_inst->dispatch_fd);
  192. }
  193. }
  194. saHandleInstancePut (&confdb_handle_t_db, handle);
  195. return (CONFDB_OK);
  196. }
  197. confdb_error_t confdb_fd_get (
  198. confdb_handle_t handle,
  199. int *fd)
  200. {
  201. SaAisErrorT error;
  202. struct confdb_inst *confdb_inst;
  203. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  204. if (error != SA_AIS_OK) {
  205. return (error);
  206. }
  207. *fd = confdb_inst->dispatch_fd;
  208. saHandleInstancePut (&confdb_handle_t_db, handle);
  209. return (SA_AIS_OK);
  210. }
  211. confdb_error_t confdb_context_get (
  212. confdb_handle_t handle,
  213. void **context)
  214. {
  215. SaAisErrorT error;
  216. struct confdb_inst *confdb_inst;
  217. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  218. if (error != SA_AIS_OK) {
  219. return (error);
  220. }
  221. *context = confdb_inst->context;
  222. saHandleInstancePut (&confdb_handle_t_db, handle);
  223. return (SA_AIS_OK);
  224. }
  225. confdb_error_t confdb_context_set (
  226. confdb_handle_t handle,
  227. void *context)
  228. {
  229. SaAisErrorT error;
  230. struct confdb_inst *confdb_inst;
  231. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  232. if (error != SA_AIS_OK) {
  233. return (error);
  234. }
  235. confdb_inst->context = context;
  236. saHandleInstancePut (&confdb_handle_t_db, handle);
  237. return (SA_AIS_OK);
  238. }
  239. struct res_overlay {
  240. mar_res_header_t header __attribute__((aligned(8)));
  241. char data[512000];
  242. };
  243. confdb_error_t confdb_dispatch (
  244. confdb_handle_t handle,
  245. confdb_dispatch_t dispatch_types)
  246. {
  247. struct pollfd ufds;
  248. int timeout = -1;
  249. SaAisErrorT error;
  250. int cont = 1; /* always continue do loop except when set to 0 */
  251. int dispatch_avail;
  252. struct confdb_inst *confdb_inst;
  253. struct res_lib_confdb_change_callback *res_confdb_change_callback;
  254. confdb_callbacks_t callbacks;
  255. struct res_overlay dispatch_data;
  256. int ignore_dispatch = 0;
  257. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  258. if (error != SA_AIS_OK) {
  259. return (error);
  260. }
  261. if (confdb_inst->standalone) {
  262. error = CONFDB_ERR_NOT_SUPPORTED;
  263. goto error_unlock;
  264. }
  265. /*
  266. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  267. * wait indefinately for SA_DISPATCH_BLOCKING
  268. */
  269. if (dispatch_types == CONFDB_DISPATCH_ALL) {
  270. timeout = 0;
  271. }
  272. do {
  273. ufds.fd = confdb_inst->dispatch_fd;
  274. ufds.events = POLLIN;
  275. ufds.revents = 0;
  276. error = saPollRetry (&ufds, 1, timeout);
  277. if (error != SA_AIS_OK) {
  278. goto error_nounlock;
  279. }
  280. pthread_mutex_lock (&confdb_inst->dispatch_mutex);
  281. /*
  282. * Regather poll data in case ufds has changed since taking lock
  283. */
  284. error = saPollRetry (&ufds, 1, timeout);
  285. if (error != SA_AIS_OK) {
  286. goto error_nounlock;
  287. }
  288. /*
  289. * Handle has been finalized in another thread
  290. */
  291. if (confdb_inst->finalize == 1) {
  292. error = CONFDB_OK;
  293. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  294. goto error_unlock;
  295. }
  296. dispatch_avail = ufds.revents & POLLIN;
  297. if (dispatch_avail == 0 && dispatch_types == CONFDB_DISPATCH_ALL) {
  298. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  299. break; /* exit do while cont is 1 loop */
  300. } else
  301. if (dispatch_avail == 0) {
  302. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  303. continue; /* next poll */
  304. }
  305. if (ufds.revents & POLLIN) {
  306. /*
  307. * Queue empty, read response from socket
  308. */
  309. error = saRecvRetry (confdb_inst->dispatch_fd, &dispatch_data.header,
  310. sizeof (mar_res_header_t));
  311. if (error != SA_AIS_OK) {
  312. goto error_unlock;
  313. }
  314. if (dispatch_data.header.size > sizeof (mar_res_header_t)) {
  315. error = saRecvRetry (confdb_inst->dispatch_fd, &dispatch_data.data,
  316. dispatch_data.header.size - sizeof (mar_res_header_t));
  317. if (error != SA_AIS_OK) {
  318. goto error_unlock;
  319. }
  320. }
  321. } else {
  322. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  323. continue;
  324. }
  325. /*
  326. * Make copy of callbacks, message data, unlock instance, and call callback
  327. * A risk of this dispatch method is that the callback routines may
  328. * operate at the same time that confdbFinalize has been called.
  329. */
  330. memcpy (&callbacks, &confdb_inst->callbacks, sizeof (confdb_callbacks_t));
  331. pthread_mutex_unlock (&confdb_inst->dispatch_mutex);
  332. /*
  333. * Dispatch incoming message
  334. */
  335. switch (dispatch_data.header.id) {
  336. case MESSAGE_RES_CONFDB_CHANGE_CALLBACK:
  337. res_confdb_change_callback = (struct res_lib_confdb_change_callback *)&dispatch_data;
  338. callbacks.confdb_change_notify_fn (handle,
  339. res_confdb_change_callback->parent_object_handle,
  340. res_confdb_change_callback->object_handle,
  341. res_confdb_change_callback->object_name.value,
  342. res_confdb_change_callback->object_name.length,
  343. res_confdb_change_callback->key_name.value,
  344. res_confdb_change_callback->key_name.length,
  345. res_confdb_change_callback->key_value.value,
  346. res_confdb_change_callback->key_value.length);
  347. break;
  348. default:
  349. error = SA_AIS_ERR_LIBRARY;
  350. goto error_nounlock;
  351. break;
  352. }
  353. /*
  354. * Determine if more messages should be processed
  355. * */
  356. switch (dispatch_types) {
  357. case CONFDB_DISPATCH_ONE:
  358. if (ignore_dispatch) {
  359. ignore_dispatch = 0;
  360. } else {
  361. cont = 0;
  362. }
  363. break;
  364. case CONFDB_DISPATCH_ALL:
  365. if (ignore_dispatch) {
  366. ignore_dispatch = 0;
  367. }
  368. break;
  369. case CONFDB_DISPATCH_BLOCKING:
  370. break;
  371. }
  372. } while (cont);
  373. error_unlock:
  374. saHandleInstancePut (&confdb_handle_t_db, handle);
  375. error_nounlock:
  376. return (error);
  377. }
  378. confdb_error_t confdb_object_create (
  379. confdb_handle_t handle,
  380. unsigned int parent_object_handle,
  381. void *object_name,
  382. int object_name_len,
  383. unsigned int *object_handle)
  384. {
  385. confdb_error_t error;
  386. struct confdb_inst *confdb_inst;
  387. struct iovec iov[2];
  388. struct req_lib_confdb_object_create req_lib_confdb_object_create;
  389. struct res_lib_confdb_object_create res_lib_confdb_object_create;
  390. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  391. if (error != SA_AIS_OK) {
  392. return (error);
  393. }
  394. if (confdb_inst->standalone) {
  395. error = SA_AIS_OK;
  396. if (confdb_sa_object_create(parent_object_handle,
  397. object_name, object_name_len,
  398. object_handle))
  399. error = SA_AIS_ERR_ACCESS;
  400. goto error_exit;
  401. }
  402. req_lib_confdb_object_create.header.size = sizeof (struct req_lib_confdb_object_create);
  403. req_lib_confdb_object_create.header.id = MESSAGE_REQ_CONFDB_OBJECT_CREATE;
  404. req_lib_confdb_object_create.parent_object_handle = parent_object_handle;
  405. memcpy(req_lib_confdb_object_create.object_name.value, object_name, object_name_len);
  406. req_lib_confdb_object_create.object_name.length = object_name_len;
  407. iov[0].iov_base = (char *)&req_lib_confdb_object_create;
  408. iov[0].iov_len = sizeof (struct req_lib_confdb_object_create);
  409. pthread_mutex_lock (&confdb_inst->response_mutex);
  410. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  411. &res_lib_confdb_object_create, sizeof (struct res_lib_confdb_object_create));
  412. pthread_mutex_unlock (&confdb_inst->response_mutex);
  413. if (error != SA_AIS_OK) {
  414. goto error_exit;
  415. }
  416. error = res_lib_confdb_object_create.header.error;
  417. *object_handle = res_lib_confdb_object_create.object_handle;
  418. error_exit:
  419. saHandleInstancePut (&confdb_handle_t_db, handle);
  420. return (error);
  421. }
  422. confdb_error_t confdb_object_destroy (
  423. confdb_handle_t handle,
  424. unsigned int object_handle)
  425. {
  426. confdb_error_t error;
  427. struct confdb_inst *confdb_inst;
  428. struct iovec iov[2];
  429. struct req_lib_confdb_object_destroy req_lib_confdb_object_destroy;
  430. mar_res_header_t res;
  431. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  432. if (error != SA_AIS_OK) {
  433. return (error);
  434. }
  435. if (confdb_inst->standalone) {
  436. error = SA_AIS_OK;
  437. if (confdb_sa_object_destroy(object_handle))
  438. error = SA_AIS_ERR_ACCESS;
  439. goto error_exit;
  440. }
  441. req_lib_confdb_object_destroy.header.size = sizeof (struct req_lib_confdb_object_destroy);
  442. req_lib_confdb_object_destroy.header.id = MESSAGE_REQ_CONFDB_OBJECT_DESTROY;
  443. req_lib_confdb_object_destroy.object_handle = object_handle;
  444. iov[0].iov_base = (char *)&req_lib_confdb_object_destroy;
  445. iov[0].iov_len = sizeof (struct req_lib_confdb_object_destroy);
  446. pthread_mutex_lock (&confdb_inst->response_mutex);
  447. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  448. &res, sizeof ( mar_res_header_t));
  449. pthread_mutex_unlock (&confdb_inst->response_mutex);
  450. if (error != SA_AIS_OK) {
  451. goto error_exit;
  452. }
  453. error = res.error;
  454. error_exit:
  455. saHandleInstancePut (&confdb_handle_t_db, handle);
  456. return (error);
  457. }
  458. confdb_error_t confdb_object_parent_get (
  459. confdb_handle_t handle,
  460. unsigned int object_handle,
  461. unsigned int *parent_object_handle)
  462. {
  463. confdb_error_t error;
  464. struct confdb_inst *confdb_inst;
  465. struct iovec iov[2];
  466. struct req_lib_confdb_object_parent_get req_lib_confdb_object_parent_get;
  467. struct res_lib_confdb_object_parent_get res_lib_confdb_object_parent_get;
  468. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  469. if (error != SA_AIS_OK) {
  470. return (error);
  471. }
  472. if (confdb_inst->standalone) {
  473. error = SA_AIS_OK;
  474. if (confdb_sa_object_parent_get(object_handle, parent_object_handle))
  475. error = SA_AIS_ERR_ACCESS;
  476. goto error_exit;
  477. }
  478. req_lib_confdb_object_parent_get.header.size = sizeof (struct req_lib_confdb_object_parent_get);
  479. req_lib_confdb_object_parent_get.header.id = MESSAGE_REQ_CONFDB_OBJECT_PARENT_GET;
  480. req_lib_confdb_object_parent_get.object_handle = object_handle;
  481. iov[0].iov_base = (char *)&req_lib_confdb_object_parent_get;
  482. iov[0].iov_len = sizeof (struct req_lib_confdb_object_parent_get);
  483. pthread_mutex_lock (&confdb_inst->response_mutex);
  484. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  485. &res_lib_confdb_object_parent_get, sizeof (struct res_lib_confdb_object_parent_get));
  486. pthread_mutex_unlock (&confdb_inst->response_mutex);
  487. if (error != SA_AIS_OK) {
  488. goto error_exit;
  489. }
  490. error = res_lib_confdb_object_parent_get.header.error;
  491. *parent_object_handle = res_lib_confdb_object_parent_get.parent_object_handle;
  492. error_exit:
  493. saHandleInstancePut (&confdb_handle_t_db, handle);
  494. return (error);
  495. }
  496. confdb_error_t confdb_key_create (
  497. confdb_handle_t handle,
  498. unsigned int parent_object_handle,
  499. void *key_name,
  500. int key_name_len,
  501. void *value,
  502. int value_len)
  503. {
  504. confdb_error_t error;
  505. struct confdb_inst *confdb_inst;
  506. struct iovec iov[2];
  507. struct req_lib_confdb_key_create req_lib_confdb_key_create;
  508. mar_res_header_t res;
  509. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  510. if (error != SA_AIS_OK) {
  511. return (error);
  512. }
  513. if (confdb_inst->standalone) {
  514. error = SA_AIS_OK;
  515. if (confdb_sa_key_create(parent_object_handle,
  516. key_name, key_name_len,
  517. value, value_len))
  518. error = SA_AIS_ERR_ACCESS;
  519. goto error_exit;
  520. }
  521. req_lib_confdb_key_create.header.size = sizeof (struct req_lib_confdb_key_create);
  522. req_lib_confdb_key_create.header.id = MESSAGE_REQ_CONFDB_KEY_CREATE;
  523. req_lib_confdb_key_create.object_handle = parent_object_handle;
  524. memcpy(req_lib_confdb_key_create.key_name.value, key_name, key_name_len);
  525. req_lib_confdb_key_create.key_name.length = key_name_len;
  526. memcpy(req_lib_confdb_key_create.value.value, value, value_len);
  527. req_lib_confdb_key_create.value.length = value_len;
  528. iov[0].iov_base = (char *)&req_lib_confdb_key_create;
  529. iov[0].iov_len = sizeof (struct req_lib_confdb_key_create);
  530. pthread_mutex_lock (&confdb_inst->response_mutex);
  531. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  532. &res, sizeof (res));
  533. pthread_mutex_unlock (&confdb_inst->response_mutex);
  534. if (error != SA_AIS_OK) {
  535. goto error_exit;
  536. }
  537. error = res.error;
  538. error_exit:
  539. saHandleInstancePut (&confdb_handle_t_db, handle);
  540. return (error);
  541. }
  542. confdb_error_t confdb_key_get (
  543. confdb_handle_t handle,
  544. unsigned int parent_object_handle,
  545. void *key_name,
  546. int key_name_len,
  547. void *value,
  548. int *value_len)
  549. {
  550. confdb_error_t error;
  551. struct confdb_inst *confdb_inst;
  552. struct iovec iov[2];
  553. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  554. struct res_lib_confdb_key_get res_lib_confdb_key_get;
  555. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  556. if (error != SA_AIS_OK) {
  557. return (error);
  558. }
  559. if (confdb_inst->standalone) {
  560. error = SA_AIS_OK;
  561. if (confdb_sa_key_get(parent_object_handle,
  562. key_name, key_name_len,
  563. value, value_len))
  564. error = SA_AIS_ERR_ACCESS;
  565. goto error_exit;
  566. }
  567. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  568. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_GET;
  569. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  570. memcpy(req_lib_confdb_key_get.key_name.value, key_name, key_name_len);
  571. req_lib_confdb_key_get.key_name.length = key_name_len;
  572. iov[0].iov_base = (char *)&req_lib_confdb_key_get;
  573. iov[0].iov_len = sizeof (struct req_lib_confdb_key_get);
  574. pthread_mutex_lock (&confdb_inst->response_mutex);
  575. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  576. &res_lib_confdb_key_get, sizeof (struct res_lib_confdb_key_get));
  577. pthread_mutex_unlock (&confdb_inst->response_mutex);
  578. if (error != SA_AIS_OK) {
  579. goto error_exit;
  580. }
  581. error = res_lib_confdb_key_get.header.error;
  582. if (error == SA_AIS_OK) {
  583. *value_len = res_lib_confdb_key_get.value.length;
  584. memcpy(value, res_lib_confdb_key_get.value.value, *value_len);
  585. }
  586. error_exit:
  587. saHandleInstancePut (&confdb_handle_t_db, handle);
  588. return (error);
  589. }
  590. confdb_error_t confdb_key_replace (
  591. confdb_handle_t handle,
  592. unsigned int parent_object_handle,
  593. void *key_name,
  594. int key_name_len,
  595. void *old_value,
  596. int old_value_len,
  597. void *new_value,
  598. int new_value_len)
  599. {
  600. confdb_error_t error;
  601. struct confdb_inst *confdb_inst;
  602. struct iovec iov[2];
  603. struct req_lib_confdb_key_replace req_lib_confdb_key_replace;
  604. mar_res_header_t res;
  605. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  606. if (error != SA_AIS_OK) {
  607. return (error);
  608. }
  609. if (confdb_inst->standalone) {
  610. error = SA_AIS_OK;
  611. if (confdb_sa_key_replace(parent_object_handle,
  612. key_name, key_name_len,
  613. old_value, old_value_len,
  614. new_value, new_value_len))
  615. error = SA_AIS_ERR_ACCESS;
  616. goto error_exit;
  617. }
  618. req_lib_confdb_key_replace.header.size = sizeof (struct req_lib_confdb_key_replace);
  619. req_lib_confdb_key_replace.header.id = MESSAGE_REQ_CONFDB_KEY_REPLACE;
  620. req_lib_confdb_key_replace.object_handle = parent_object_handle;
  621. memcpy(req_lib_confdb_key_replace.key_name.value, key_name, key_name_len);
  622. req_lib_confdb_key_replace.key_name.length = key_name_len;
  623. memcpy(req_lib_confdb_key_replace.old_value.value, old_value, old_value_len);
  624. req_lib_confdb_key_replace.old_value.length = old_value_len;
  625. memcpy(req_lib_confdb_key_replace.new_value.value, new_value, new_value_len);
  626. req_lib_confdb_key_replace.new_value.length = new_value_len;
  627. iov[0].iov_base = (char *)&req_lib_confdb_key_replace;
  628. iov[0].iov_len = sizeof (struct req_lib_confdb_key_replace);
  629. pthread_mutex_lock (&confdb_inst->response_mutex);
  630. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  631. &res, sizeof (res));
  632. pthread_mutex_unlock (&confdb_inst->response_mutex);
  633. if (error != SA_AIS_OK) {
  634. goto error_exit;
  635. }
  636. error = res.error;
  637. error_exit:
  638. saHandleInstancePut (&confdb_handle_t_db, handle);
  639. return (error);
  640. }
  641. confdb_error_t confdb_object_iter_start (
  642. confdb_handle_t handle,
  643. unsigned int object_handle)
  644. {
  645. struct confdb_inst *confdb_inst;
  646. confdb_error_t error = SA_AIS_OK;
  647. struct iter_context *context;
  648. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  649. if (error != SA_AIS_OK) {
  650. return (error);
  651. }
  652. context = find_iter_context(&confdb_inst->object_iter_head, object_handle);
  653. if (!context) {
  654. context = malloc(sizeof(struct iter_context));
  655. if (!context) {
  656. error = CONFDB_ERR_NO_MEMORY;
  657. goto ret;
  658. }
  659. context->parent_object_handle = object_handle;
  660. list_add(&context->list, &confdb_inst->object_iter_head);
  661. }
  662. context->context = 0;
  663. saHandleInstancePut (&confdb_handle_t_db, handle);
  664. ret:
  665. return error;
  666. }
  667. confdb_error_t confdb_key_iter_start (
  668. confdb_handle_t handle,
  669. unsigned int object_handle)
  670. {
  671. struct confdb_inst *confdb_inst;
  672. confdb_error_t error = SA_AIS_OK;
  673. struct iter_context *context;
  674. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  675. if (error != SA_AIS_OK) {
  676. return (error);
  677. }
  678. context = find_iter_context(&confdb_inst->key_iter_head, object_handle);
  679. if (!context) {
  680. context = malloc(sizeof(struct iter_context));
  681. if (!context) {
  682. error = CONFDB_ERR_NO_MEMORY;
  683. goto ret;
  684. }
  685. context->parent_object_handle = object_handle;
  686. list_add(&context->list, &confdb_inst->key_iter_head);
  687. }
  688. context->context = 0;
  689. saHandleInstancePut (&confdb_handle_t_db, handle);
  690. ret:
  691. return error;
  692. }
  693. confdb_error_t confdb_object_find_start (
  694. confdb_handle_t handle,
  695. unsigned int parent_object_handle)
  696. {
  697. struct confdb_inst *confdb_inst;
  698. confdb_error_t error = SA_AIS_OK;
  699. struct iter_context *context;
  700. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  701. if (error != SA_AIS_OK) {
  702. return (error);
  703. }
  704. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  705. if (!context) {
  706. context = malloc(sizeof(struct iter_context));
  707. if (!context) {
  708. error = CONFDB_ERR_NO_MEMORY;
  709. goto ret;
  710. }
  711. context->parent_object_handle = parent_object_handle;
  712. list_add(&context->list, &confdb_inst->object_find_head);
  713. }
  714. context->context = 0;
  715. saHandleInstancePut (&confdb_handle_t_db, handle);
  716. ret:
  717. return error;
  718. }
  719. confdb_error_t confdb_object_find (
  720. confdb_handle_t handle,
  721. unsigned int parent_object_handle,
  722. void *object_name,
  723. int object_name_len,
  724. unsigned int *object_handle)
  725. {
  726. confdb_error_t error;
  727. struct confdb_inst *confdb_inst;
  728. struct iovec iov[2];
  729. struct iter_context *context;
  730. struct req_lib_confdb_object_find req_lib_confdb_object_find;
  731. struct res_lib_confdb_object_find res_lib_confdb_object_find;
  732. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  733. if (error != SA_AIS_OK) {
  734. return (error);
  735. }
  736. /* You MUST call confdb_object_find_start first */
  737. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  738. if (!context) {
  739. error = CONFDB_ERR_CONTEXT_NOT_FOUND;
  740. goto error_exit;
  741. }
  742. if (confdb_inst->standalone) {
  743. error = SA_AIS_OK;
  744. if (confdb_sa_object_find(parent_object_handle,
  745. context->context,
  746. object_name, object_name_len,
  747. object_handle,
  748. &context->context))
  749. error = SA_AIS_ERR_ACCESS;
  750. goto error_exit;
  751. }
  752. req_lib_confdb_object_find.header.size = sizeof (struct req_lib_confdb_object_find);
  753. req_lib_confdb_object_find.header.id = MESSAGE_REQ_CONFDB_OBJECT_FIND;
  754. req_lib_confdb_object_find.parent_object_handle = parent_object_handle;
  755. req_lib_confdb_object_find.next_entry = context->context;
  756. memcpy(req_lib_confdb_object_find.object_name.value, object_name, object_name_len);
  757. req_lib_confdb_object_find.object_name.length = object_name_len;
  758. iov[0].iov_base = (char *)&req_lib_confdb_object_find;
  759. iov[0].iov_len = sizeof (struct req_lib_confdb_object_find);
  760. pthread_mutex_lock (&confdb_inst->response_mutex);
  761. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  762. &res_lib_confdb_object_find, sizeof (struct res_lib_confdb_object_find));
  763. pthread_mutex_unlock (&confdb_inst->response_mutex);
  764. if (error != SA_AIS_OK) {
  765. goto error_exit;
  766. }
  767. error = res_lib_confdb_object_find.header.error;
  768. *object_handle = res_lib_confdb_object_find.object_handle;
  769. context->context = res_lib_confdb_object_find.next_entry;
  770. error_exit:
  771. saHandleInstancePut (&confdb_handle_t_db, handle);
  772. return (error);
  773. }
  774. confdb_error_t confdb_object_iter (
  775. confdb_handle_t handle,
  776. unsigned int parent_object_handle,
  777. unsigned int *object_handle,
  778. void *object_name,
  779. int *object_name_len)
  780. {
  781. confdb_error_t error;
  782. struct confdb_inst *confdb_inst;
  783. struct iovec iov[2];
  784. struct iter_context *context;
  785. struct req_lib_confdb_object_iter req_lib_confdb_object_iter;
  786. struct res_lib_confdb_object_iter res_lib_confdb_object_iter;
  787. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  788. if (error != SA_AIS_OK) {
  789. return (error);
  790. }
  791. /* You MUST call confdb_object_iter_start first */
  792. context = find_iter_context(&confdb_inst->object_iter_head, parent_object_handle);
  793. if (!context) {
  794. error = CONFDB_ERR_CONTEXT_NOT_FOUND;
  795. goto error_exit;
  796. }
  797. if (confdb_inst->standalone) {
  798. error = SA_AIS_OK;
  799. if (confdb_sa_object_iter(parent_object_handle,
  800. context->context,
  801. object_handle,
  802. object_name, object_name_len))
  803. error = SA_AIS_ERR_ACCESS;
  804. goto sa_exit;
  805. }
  806. req_lib_confdb_object_iter.header.size = sizeof (struct req_lib_confdb_object_iter);
  807. req_lib_confdb_object_iter.header.id = MESSAGE_REQ_CONFDB_OBJECT_ITER;
  808. req_lib_confdb_object_iter.parent_object_handle = parent_object_handle;
  809. req_lib_confdb_object_iter.next_entry = context->context;
  810. iov[0].iov_base = (char *)&req_lib_confdb_object_iter;
  811. iov[0].iov_len = sizeof (struct req_lib_confdb_object_iter);
  812. pthread_mutex_lock (&confdb_inst->response_mutex);
  813. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  814. &res_lib_confdb_object_iter, sizeof (struct res_lib_confdb_object_iter));
  815. pthread_mutex_unlock (&confdb_inst->response_mutex);
  816. if (error != SA_AIS_OK) {
  817. goto error_exit;
  818. }
  819. error = res_lib_confdb_object_iter.header.error;
  820. if (error == SA_AIS_OK) {
  821. *object_name_len = res_lib_confdb_object_iter.object_name.length;
  822. memcpy(object_name, res_lib_confdb_object_iter.object_name.value, *object_name_len);
  823. *object_handle = res_lib_confdb_object_iter.object_handle;
  824. }
  825. sa_exit:
  826. context->context++;
  827. error_exit:
  828. saHandleInstancePut (&confdb_handle_t_db, handle);
  829. return (error);
  830. }
  831. confdb_error_t confdb_key_iter (
  832. confdb_handle_t handle,
  833. unsigned int parent_object_handle,
  834. void *key_name,
  835. int *key_name_len,
  836. void *value,
  837. int *value_len)
  838. {
  839. confdb_error_t error;
  840. struct confdb_inst *confdb_inst;
  841. struct iovec iov[2];
  842. struct iter_context *context;
  843. struct req_lib_confdb_key_iter req_lib_confdb_key_iter;
  844. struct res_lib_confdb_key_iter res_lib_confdb_key_iter;
  845. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  846. if (error != SA_AIS_OK) {
  847. return (error);
  848. }
  849. /* You MUST call confdb_key_iter_start first */
  850. context = find_iter_context(&confdb_inst->key_iter_head, parent_object_handle);
  851. if (!context) {
  852. error = CONFDB_ERR_CONTEXT_NOT_FOUND;
  853. goto error_exit;
  854. }
  855. if (confdb_inst->standalone) {
  856. error = SA_AIS_OK;
  857. if (confdb_sa_key_iter(parent_object_handle,
  858. context->context,
  859. key_name, key_name_len,
  860. value, value_len))
  861. error = SA_AIS_ERR_ACCESS;
  862. goto sa_exit;
  863. }
  864. req_lib_confdb_key_iter.header.size = sizeof (struct req_lib_confdb_key_iter);
  865. req_lib_confdb_key_iter.header.id = MESSAGE_REQ_CONFDB_KEY_ITER;
  866. req_lib_confdb_key_iter.parent_object_handle = parent_object_handle;
  867. req_lib_confdb_key_iter.next_entry = context->context;
  868. iov[0].iov_base = (char *)&req_lib_confdb_key_iter;
  869. iov[0].iov_len = sizeof (struct req_lib_confdb_key_iter);
  870. pthread_mutex_lock (&confdb_inst->response_mutex);
  871. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  872. &res_lib_confdb_key_iter, sizeof (struct res_lib_confdb_key_iter));
  873. pthread_mutex_unlock (&confdb_inst->response_mutex);
  874. if (error != SA_AIS_OK) {
  875. goto error_exit;
  876. }
  877. error = res_lib_confdb_key_iter.header.error;
  878. if (error == SA_AIS_OK) {
  879. *key_name_len = res_lib_confdb_key_iter.key_name.length;
  880. memcpy(key_name, res_lib_confdb_key_iter.key_name.value, *key_name_len);
  881. *value_len = res_lib_confdb_key_iter.value.length;
  882. memcpy(value, res_lib_confdb_key_iter.value.value, *value_len);
  883. }
  884. sa_exit:
  885. context->context++;
  886. error_exit:
  887. saHandleInstancePut (&confdb_handle_t_db, handle);
  888. return (error);
  889. }
  890. confdb_error_t confdb_write (
  891. confdb_handle_t handle,
  892. char *error_text)
  893. {
  894. confdb_error_t error;
  895. struct confdb_inst *confdb_inst;
  896. struct iovec iov[2];
  897. mar_req_header_t req;
  898. struct res_lib_confdb_write res_lib_confdb_write;
  899. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  900. if (error != SA_AIS_OK) {
  901. return (error);
  902. }
  903. if (confdb_inst->standalone) {
  904. error = SA_AIS_OK;
  905. if (confdb_sa_write(error_text))
  906. error = SA_AIS_ERR_ACCESS;
  907. goto error_exit;
  908. }
  909. req.size = sizeof (mar_req_header_t);
  910. req.id = MESSAGE_REQ_CONFDB_WRITE;
  911. iov[0].iov_base = (char *)&req;
  912. iov[0].iov_len = sizeof (mar_req_header_t);
  913. pthread_mutex_lock (&confdb_inst->response_mutex);
  914. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  915. &res_lib_confdb_write, sizeof ( struct res_lib_confdb_write));
  916. pthread_mutex_unlock (&confdb_inst->response_mutex);
  917. if (error != SA_AIS_OK) {
  918. goto error_exit;
  919. }
  920. error = res_lib_confdb_write.header.error;
  921. if (res_lib_confdb_write.error.length)
  922. memcpy(error_text, res_lib_confdb_write.error.value, res_lib_confdb_write.error.length);
  923. error_exit:
  924. saHandleInstancePut (&confdb_handle_t_db, handle);
  925. return (error);
  926. }