confdb.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. memcpy(error_text, res_lib_confdb_write.error.value, res_lib_confdb_write.error.length);
  922. error_exit:
  923. saHandleInstancePut (&confdb_handle_t_db, handle);
  924. return (error);
  925. }