confdb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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_delete (
  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_delete req_lib_confdb_key_delete;
  554. mar_res_header_t res;
  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_delete(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_delete.header.size = sizeof (struct req_lib_confdb_key_delete);
  568. req_lib_confdb_key_delete.header.id = MESSAGE_REQ_CONFDB_KEY_DELETE;
  569. req_lib_confdb_key_delete.object_handle = parent_object_handle;
  570. memcpy(req_lib_confdb_key_delete.key_name.value, key_name, key_name_len);
  571. req_lib_confdb_key_delete.key_name.length = key_name_len;
  572. memcpy(req_lib_confdb_key_delete.value.value, value, value_len);
  573. req_lib_confdb_key_delete.value.length = value_len;
  574. iov[0].iov_base = (char *)&req_lib_confdb_key_delete;
  575. iov[0].iov_len = sizeof (struct req_lib_confdb_key_delete);
  576. pthread_mutex_lock (&confdb_inst->response_mutex);
  577. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  578. &res, sizeof (res));
  579. pthread_mutex_unlock (&confdb_inst->response_mutex);
  580. if (error != SA_AIS_OK) {
  581. goto error_exit;
  582. }
  583. error = res.error;
  584. error_exit:
  585. saHandleInstancePut (&confdb_handle_t_db, handle);
  586. return (error);
  587. }
  588. confdb_error_t confdb_key_get (
  589. confdb_handle_t handle,
  590. unsigned int parent_object_handle,
  591. void *key_name,
  592. int key_name_len,
  593. void *value,
  594. int *value_len)
  595. {
  596. confdb_error_t error;
  597. struct confdb_inst *confdb_inst;
  598. struct iovec iov[2];
  599. struct req_lib_confdb_key_get req_lib_confdb_key_get;
  600. struct res_lib_confdb_key_get res_lib_confdb_key_get;
  601. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  602. if (error != SA_AIS_OK) {
  603. return (error);
  604. }
  605. if (confdb_inst->standalone) {
  606. error = SA_AIS_OK;
  607. if (confdb_sa_key_get(parent_object_handle,
  608. key_name, key_name_len,
  609. value, value_len))
  610. error = SA_AIS_ERR_ACCESS;
  611. goto error_exit;
  612. }
  613. req_lib_confdb_key_get.header.size = sizeof (struct req_lib_confdb_key_get);
  614. req_lib_confdb_key_get.header.id = MESSAGE_REQ_CONFDB_KEY_GET;
  615. req_lib_confdb_key_get.parent_object_handle = parent_object_handle;
  616. memcpy(req_lib_confdb_key_get.key_name.value, key_name, key_name_len);
  617. req_lib_confdb_key_get.key_name.length = key_name_len;
  618. iov[0].iov_base = (char *)&req_lib_confdb_key_get;
  619. iov[0].iov_len = sizeof (struct req_lib_confdb_key_get);
  620. pthread_mutex_lock (&confdb_inst->response_mutex);
  621. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  622. &res_lib_confdb_key_get, sizeof (struct res_lib_confdb_key_get));
  623. pthread_mutex_unlock (&confdb_inst->response_mutex);
  624. if (error != SA_AIS_OK) {
  625. goto error_exit;
  626. }
  627. error = res_lib_confdb_key_get.header.error;
  628. if (error == SA_AIS_OK) {
  629. *value_len = res_lib_confdb_key_get.value.length;
  630. memcpy(value, res_lib_confdb_key_get.value.value, *value_len);
  631. }
  632. error_exit:
  633. saHandleInstancePut (&confdb_handle_t_db, handle);
  634. return (error);
  635. }
  636. confdb_error_t confdb_key_replace (
  637. confdb_handle_t handle,
  638. unsigned int parent_object_handle,
  639. void *key_name,
  640. int key_name_len,
  641. void *old_value,
  642. int old_value_len,
  643. void *new_value,
  644. int new_value_len)
  645. {
  646. confdb_error_t error;
  647. struct confdb_inst *confdb_inst;
  648. struct iovec iov[2];
  649. struct req_lib_confdb_key_replace req_lib_confdb_key_replace;
  650. mar_res_header_t res;
  651. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  652. if (error != SA_AIS_OK) {
  653. return (error);
  654. }
  655. if (confdb_inst->standalone) {
  656. error = SA_AIS_OK;
  657. if (confdb_sa_key_replace(parent_object_handle,
  658. key_name, key_name_len,
  659. old_value, old_value_len,
  660. new_value, new_value_len))
  661. error = SA_AIS_ERR_ACCESS;
  662. goto error_exit;
  663. }
  664. req_lib_confdb_key_replace.header.size = sizeof (struct req_lib_confdb_key_replace);
  665. req_lib_confdb_key_replace.header.id = MESSAGE_REQ_CONFDB_KEY_REPLACE;
  666. req_lib_confdb_key_replace.object_handle = parent_object_handle;
  667. memcpy(req_lib_confdb_key_replace.key_name.value, key_name, key_name_len);
  668. req_lib_confdb_key_replace.key_name.length = key_name_len;
  669. memcpy(req_lib_confdb_key_replace.old_value.value, old_value, old_value_len);
  670. req_lib_confdb_key_replace.old_value.length = old_value_len;
  671. memcpy(req_lib_confdb_key_replace.new_value.value, new_value, new_value_len);
  672. req_lib_confdb_key_replace.new_value.length = new_value_len;
  673. iov[0].iov_base = (char *)&req_lib_confdb_key_replace;
  674. iov[0].iov_len = sizeof (struct req_lib_confdb_key_replace);
  675. pthread_mutex_lock (&confdb_inst->response_mutex);
  676. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  677. &res, sizeof (res));
  678. pthread_mutex_unlock (&confdb_inst->response_mutex);
  679. if (error != SA_AIS_OK) {
  680. goto error_exit;
  681. }
  682. error = res.error;
  683. error_exit:
  684. saHandleInstancePut (&confdb_handle_t_db, handle);
  685. return (error);
  686. }
  687. confdb_error_t confdb_object_iter_start (
  688. confdb_handle_t handle,
  689. unsigned int object_handle)
  690. {
  691. struct confdb_inst *confdb_inst;
  692. confdb_error_t error = SA_AIS_OK;
  693. struct iter_context *context;
  694. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  695. if (error != SA_AIS_OK) {
  696. return (error);
  697. }
  698. context = find_iter_context(&confdb_inst->object_iter_head, object_handle);
  699. if (!context) {
  700. context = malloc(sizeof(struct iter_context));
  701. if (!context) {
  702. error = CONFDB_ERR_NO_MEMORY;
  703. goto ret;
  704. }
  705. context->parent_object_handle = object_handle;
  706. list_add(&context->list, &confdb_inst->object_iter_head);
  707. }
  708. context->context = 0;
  709. saHandleInstancePut (&confdb_handle_t_db, handle);
  710. ret:
  711. return error;
  712. }
  713. confdb_error_t confdb_key_iter_start (
  714. confdb_handle_t handle,
  715. unsigned int object_handle)
  716. {
  717. struct confdb_inst *confdb_inst;
  718. confdb_error_t error = SA_AIS_OK;
  719. struct iter_context *context;
  720. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  721. if (error != SA_AIS_OK) {
  722. return (error);
  723. }
  724. context = find_iter_context(&confdb_inst->key_iter_head, object_handle);
  725. if (!context) {
  726. context = malloc(sizeof(struct iter_context));
  727. if (!context) {
  728. error = CONFDB_ERR_NO_MEMORY;
  729. goto ret;
  730. }
  731. context->parent_object_handle = object_handle;
  732. list_add(&context->list, &confdb_inst->key_iter_head);
  733. }
  734. context->context = 0;
  735. saHandleInstancePut (&confdb_handle_t_db, handle);
  736. ret:
  737. return error;
  738. }
  739. confdb_error_t confdb_object_find_start (
  740. confdb_handle_t handle,
  741. unsigned int parent_object_handle)
  742. {
  743. struct confdb_inst *confdb_inst;
  744. confdb_error_t error = SA_AIS_OK;
  745. struct iter_context *context;
  746. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  747. if (error != SA_AIS_OK) {
  748. return (error);
  749. }
  750. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  751. if (!context) {
  752. context = malloc(sizeof(struct iter_context));
  753. if (!context) {
  754. error = CONFDB_ERR_NO_MEMORY;
  755. goto ret;
  756. }
  757. context->parent_object_handle = parent_object_handle;
  758. list_add(&context->list, &confdb_inst->object_find_head);
  759. }
  760. context->context = 0;
  761. saHandleInstancePut (&confdb_handle_t_db, handle);
  762. ret:
  763. return error;
  764. }
  765. confdb_error_t confdb_object_find (
  766. confdb_handle_t handle,
  767. unsigned int parent_object_handle,
  768. void *object_name,
  769. int object_name_len,
  770. unsigned int *object_handle)
  771. {
  772. confdb_error_t error;
  773. struct confdb_inst *confdb_inst;
  774. struct iovec iov[2];
  775. struct iter_context *context;
  776. struct req_lib_confdb_object_find req_lib_confdb_object_find;
  777. struct res_lib_confdb_object_find res_lib_confdb_object_find;
  778. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  779. if (error != SA_AIS_OK) {
  780. return (error);
  781. }
  782. /* You MUST call confdb_object_find_start first */
  783. context = find_iter_context(&confdb_inst->object_find_head, parent_object_handle);
  784. if (!context) {
  785. error = CONFDB_ERR_CONTEXT_NOT_FOUND;
  786. goto error_exit;
  787. }
  788. if (confdb_inst->standalone) {
  789. error = SA_AIS_OK;
  790. if (confdb_sa_object_find(parent_object_handle,
  791. context->context,
  792. object_name, object_name_len,
  793. object_handle,
  794. &context->context))
  795. error = SA_AIS_ERR_ACCESS;
  796. goto error_exit;
  797. }
  798. req_lib_confdb_object_find.header.size = sizeof (struct req_lib_confdb_object_find);
  799. req_lib_confdb_object_find.header.id = MESSAGE_REQ_CONFDB_OBJECT_FIND;
  800. req_lib_confdb_object_find.parent_object_handle = parent_object_handle;
  801. req_lib_confdb_object_find.next_entry = context->context;
  802. memcpy(req_lib_confdb_object_find.object_name.value, object_name, object_name_len);
  803. req_lib_confdb_object_find.object_name.length = object_name_len;
  804. iov[0].iov_base = (char *)&req_lib_confdb_object_find;
  805. iov[0].iov_len = sizeof (struct req_lib_confdb_object_find);
  806. pthread_mutex_lock (&confdb_inst->response_mutex);
  807. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  808. &res_lib_confdb_object_find, sizeof (struct res_lib_confdb_object_find));
  809. pthread_mutex_unlock (&confdb_inst->response_mutex);
  810. if (error != SA_AIS_OK) {
  811. goto error_exit;
  812. }
  813. error = res_lib_confdb_object_find.header.error;
  814. *object_handle = res_lib_confdb_object_find.object_handle;
  815. context->context = res_lib_confdb_object_find.next_entry;
  816. error_exit:
  817. saHandleInstancePut (&confdb_handle_t_db, handle);
  818. return (error);
  819. }
  820. confdb_error_t confdb_object_iter (
  821. confdb_handle_t handle,
  822. unsigned int parent_object_handle,
  823. unsigned int *object_handle,
  824. void *object_name,
  825. int *object_name_len)
  826. {
  827. confdb_error_t error;
  828. struct confdb_inst *confdb_inst;
  829. struct iovec iov[2];
  830. struct iter_context *context;
  831. struct req_lib_confdb_object_iter req_lib_confdb_object_iter;
  832. struct res_lib_confdb_object_iter res_lib_confdb_object_iter;
  833. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  834. if (error != SA_AIS_OK) {
  835. return (error);
  836. }
  837. /* You MUST call confdb_object_iter_start first */
  838. context = find_iter_context(&confdb_inst->object_iter_head, parent_object_handle);
  839. if (!context) {
  840. error = CONFDB_ERR_CONTEXT_NOT_FOUND;
  841. goto error_exit;
  842. }
  843. if (confdb_inst->standalone) {
  844. error = SA_AIS_OK;
  845. if (confdb_sa_object_iter(parent_object_handle,
  846. context->context,
  847. object_handle,
  848. object_name, object_name_len))
  849. error = SA_AIS_ERR_ACCESS;
  850. goto sa_exit;
  851. }
  852. req_lib_confdb_object_iter.header.size = sizeof (struct req_lib_confdb_object_iter);
  853. req_lib_confdb_object_iter.header.id = MESSAGE_REQ_CONFDB_OBJECT_ITER;
  854. req_lib_confdb_object_iter.parent_object_handle = parent_object_handle;
  855. req_lib_confdb_object_iter.next_entry = context->context;
  856. iov[0].iov_base = (char *)&req_lib_confdb_object_iter;
  857. iov[0].iov_len = sizeof (struct req_lib_confdb_object_iter);
  858. pthread_mutex_lock (&confdb_inst->response_mutex);
  859. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  860. &res_lib_confdb_object_iter, sizeof (struct res_lib_confdb_object_iter));
  861. pthread_mutex_unlock (&confdb_inst->response_mutex);
  862. if (error != SA_AIS_OK) {
  863. goto error_exit;
  864. }
  865. error = res_lib_confdb_object_iter.header.error;
  866. if (error == SA_AIS_OK) {
  867. *object_name_len = res_lib_confdb_object_iter.object_name.length;
  868. memcpy(object_name, res_lib_confdb_object_iter.object_name.value, *object_name_len);
  869. *object_handle = res_lib_confdb_object_iter.object_handle;
  870. }
  871. sa_exit:
  872. context->context++;
  873. error_exit:
  874. saHandleInstancePut (&confdb_handle_t_db, handle);
  875. return (error);
  876. }
  877. confdb_error_t confdb_key_iter (
  878. confdb_handle_t handle,
  879. unsigned int parent_object_handle,
  880. void *key_name,
  881. int *key_name_len,
  882. void *value,
  883. int *value_len)
  884. {
  885. confdb_error_t error;
  886. struct confdb_inst *confdb_inst;
  887. struct iovec iov[2];
  888. struct iter_context *context;
  889. struct req_lib_confdb_key_iter req_lib_confdb_key_iter;
  890. struct res_lib_confdb_key_iter res_lib_confdb_key_iter;
  891. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  892. if (error != SA_AIS_OK) {
  893. return (error);
  894. }
  895. /* You MUST call confdb_key_iter_start first */
  896. context = find_iter_context(&confdb_inst->key_iter_head, parent_object_handle);
  897. if (!context) {
  898. error = CONFDB_ERR_CONTEXT_NOT_FOUND;
  899. goto error_exit;
  900. }
  901. if (confdb_inst->standalone) {
  902. error = SA_AIS_OK;
  903. if (confdb_sa_key_iter(parent_object_handle,
  904. context->context,
  905. key_name, key_name_len,
  906. value, value_len))
  907. error = SA_AIS_ERR_ACCESS;
  908. goto sa_exit;
  909. }
  910. req_lib_confdb_key_iter.header.size = sizeof (struct req_lib_confdb_key_iter);
  911. req_lib_confdb_key_iter.header.id = MESSAGE_REQ_CONFDB_KEY_ITER;
  912. req_lib_confdb_key_iter.parent_object_handle = parent_object_handle;
  913. req_lib_confdb_key_iter.next_entry = context->context;
  914. iov[0].iov_base = (char *)&req_lib_confdb_key_iter;
  915. iov[0].iov_len = sizeof (struct req_lib_confdb_key_iter);
  916. pthread_mutex_lock (&confdb_inst->response_mutex);
  917. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  918. &res_lib_confdb_key_iter, sizeof (struct res_lib_confdb_key_iter));
  919. pthread_mutex_unlock (&confdb_inst->response_mutex);
  920. if (error != SA_AIS_OK) {
  921. goto error_exit;
  922. }
  923. error = res_lib_confdb_key_iter.header.error;
  924. if (error == SA_AIS_OK) {
  925. *key_name_len = res_lib_confdb_key_iter.key_name.length;
  926. memcpy(key_name, res_lib_confdb_key_iter.key_name.value, *key_name_len);
  927. *value_len = res_lib_confdb_key_iter.value.length;
  928. memcpy(value, res_lib_confdb_key_iter.value.value, *value_len);
  929. }
  930. sa_exit:
  931. context->context++;
  932. error_exit:
  933. saHandleInstancePut (&confdb_handle_t_db, handle);
  934. return (error);
  935. }
  936. confdb_error_t confdb_write (
  937. confdb_handle_t handle,
  938. char *error_text)
  939. {
  940. confdb_error_t error;
  941. struct confdb_inst *confdb_inst;
  942. struct iovec iov[2];
  943. mar_req_header_t req;
  944. struct res_lib_confdb_write res_lib_confdb_write;
  945. error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
  946. if (error != SA_AIS_OK) {
  947. return (error);
  948. }
  949. if (confdb_inst->standalone) {
  950. error = SA_AIS_OK;
  951. if (confdb_sa_write(error_text))
  952. error = SA_AIS_ERR_ACCESS;
  953. goto error_exit;
  954. }
  955. req.size = sizeof (mar_req_header_t);
  956. req.id = MESSAGE_REQ_CONFDB_WRITE;
  957. iov[0].iov_base = (char *)&req;
  958. iov[0].iov_len = sizeof (mar_req_header_t);
  959. pthread_mutex_lock (&confdb_inst->response_mutex);
  960. error = saSendMsgReceiveReply (confdb_inst->response_fd, iov, 1,
  961. &res_lib_confdb_write, sizeof ( struct res_lib_confdb_write));
  962. pthread_mutex_unlock (&confdb_inst->response_mutex);
  963. if (error != SA_AIS_OK) {
  964. goto error_exit;
  965. }
  966. error = res_lib_confdb_write.header.error;
  967. if (res_lib_confdb_write.error.length)
  968. memcpy(error_text, res_lib_confdb_write.error.value, res_lib_confdb_write.error.length);
  969. error_exit:
  970. saHandleInstancePut (&confdb_handle_t_db, handle);
  971. return (error);
  972. }