confdb.c 27 KB

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