sa-confdb.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2008, 2009 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 stand-alone access to data in the corosync object database
  36. * when aisexec is not running.
  37. */
  38. #include <config.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <pthread.h>
  43. #include <sys/types.h>
  44. #include <errno.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/coroipcc.h>
  47. #include <corosync/engine/objdb.h>
  48. #include <corosync/engine/config.h>
  49. #include <corosync/engine/logsys.h>
  50. #include <corosync/lcr/lcr_comp.h>
  51. #include <corosync/lcr/lcr_ifact.h>
  52. #include "sa-confdb.h"
  53. static struct objdb_iface_ver0 *objdb;
  54. static int num_config_modules;
  55. static struct config_iface_ver0 *config_modules[128];
  56. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num);
  57. static int load_objdb(void)
  58. {
  59. hdb_handle_t objdb_handle;
  60. void *objdb_p;
  61. int res;
  62. /*
  63. * Load the object database interface
  64. */
  65. res = lcr_ifact_reference (
  66. &objdb_handle,
  67. "objdb",
  68. 0,
  69. &objdb_p,
  70. (void *)0);
  71. if (res == -1) {
  72. return -1;
  73. }
  74. objdb = (struct objdb_iface_ver0 *)objdb_p;
  75. objdb->objdb_init ();
  76. return CS_OK;
  77. }
  78. static int load_config(void)
  79. {
  80. char *config_iface;
  81. char *iface;
  82. int res;
  83. hdb_handle_t config_handle;
  84. hdb_handle_t config_version = 0;
  85. void *config_p;
  86. struct config_iface_ver0 *config;
  87. const char *error_string;
  88. /* User's bootstrap config service */
  89. config_iface = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  90. if (!config_iface) {
  91. if ((config_iface = strdup("corosync_parser")) == NULL) {
  92. return -1;
  93. }
  94. }
  95. /* Make a copy so we can deface it with strtok */
  96. if ((config_iface = strdup(config_iface)) == NULL) {
  97. return -1;
  98. }
  99. iface = strtok(config_iface, ":");
  100. while (iface)
  101. {
  102. res = lcr_ifact_reference (
  103. &config_handle,
  104. iface,
  105. config_version,
  106. &config_p,
  107. 0);
  108. config = (struct config_iface_ver0 *)config_p;
  109. if (res == -1) {
  110. return -1;
  111. }
  112. res = config->config_readconfig(objdb, &error_string);
  113. if (res == -1) {
  114. return -1;
  115. }
  116. config_modules[num_config_modules++] = config;
  117. iface = strtok(NULL, ":");
  118. }
  119. free(config_iface);
  120. return CS_OK;
  121. }
  122. /* Needed by objdb when it writes back the configuration */
  123. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  124. {
  125. *modules = config_modules;
  126. *num = num_config_modules;
  127. }
  128. int confdb_sa_init (void)
  129. {
  130. int res;
  131. res = load_objdb();
  132. if (res != CS_OK)
  133. return res;
  134. res = load_config();
  135. return res;
  136. }
  137. int confdb_sa_object_create (
  138. hdb_handle_t parent_object_handle,
  139. const void *object_name,
  140. size_t object_name_len,
  141. hdb_handle_t *object_handle)
  142. {
  143. return objdb->object_create(parent_object_handle,
  144. object_handle,
  145. object_name, object_name_len);
  146. }
  147. int confdb_sa_object_destroy (
  148. hdb_handle_t object_handle)
  149. {
  150. return objdb->object_destroy(object_handle);
  151. }
  152. int confdb_sa_object_parent_get (
  153. hdb_handle_t object_handle,
  154. hdb_handle_t *parent_object_handle)
  155. {
  156. return objdb->object_parent_get(object_handle, parent_object_handle);
  157. }
  158. int confdb_sa_key_create (
  159. hdb_handle_t parent_object_handle,
  160. const void *key_name,
  161. size_t key_name_len,
  162. const void *value,
  163. size_t value_len)
  164. {
  165. return objdb->object_key_create(parent_object_handle,
  166. key_name, key_name_len,
  167. value, value_len);
  168. }
  169. int confdb_sa_key_delete (
  170. hdb_handle_t parent_object_handle,
  171. const void *key_name,
  172. size_t key_name_len,
  173. const void *value,
  174. size_t value_len)
  175. {
  176. return objdb->object_key_delete(parent_object_handle,
  177. key_name, key_name_len);
  178. }
  179. int confdb_sa_key_get (
  180. hdb_handle_t parent_object_handle,
  181. const void *key_name,
  182. size_t key_name_len,
  183. void *value,
  184. size_t *value_len)
  185. {
  186. int res;
  187. void *kvalue;
  188. res = objdb->object_key_get(parent_object_handle,
  189. key_name, key_name_len,
  190. &kvalue, value_len);
  191. if (!res) {
  192. memcpy(value, kvalue, *value_len);
  193. }
  194. return res;
  195. }
  196. int confdb_sa_key_increment (
  197. hdb_handle_t parent_object_handle,
  198. const void *key_name,
  199. size_t key_name_len,
  200. unsigned int *value)
  201. {
  202. int res;
  203. res = objdb->object_key_increment(parent_object_handle,
  204. key_name, key_name_len,
  205. value);
  206. return res;
  207. }
  208. int confdb_sa_key_decrement (
  209. hdb_handle_t parent_object_handle,
  210. const void *key_name,
  211. size_t key_name_len,
  212. unsigned int *value)
  213. {
  214. int res;
  215. res = objdb->object_key_decrement(parent_object_handle,
  216. key_name, key_name_len,
  217. value);
  218. return res;
  219. }
  220. int confdb_sa_key_replace (
  221. hdb_handle_t parent_object_handle,
  222. const void *key_name,
  223. size_t key_name_len,
  224. const void *old_value,
  225. size_t old_value_len,
  226. const void *new_value,
  227. size_t new_value_len)
  228. {
  229. return objdb->object_key_replace(parent_object_handle,
  230. key_name, key_name_len,
  231. new_value, new_value_len);
  232. }
  233. int confdb_sa_write (char *error_text, size_t errbuf_len)
  234. {
  235. const char *errtext;
  236. int ret;
  237. ret = objdb->object_write_config(&errtext);
  238. if (!ret) {
  239. strncpy(error_text, errtext, errbuf_len);
  240. if (errbuf_len > 0)
  241. error_text[errbuf_len-1] = '\0';
  242. }
  243. return ret;
  244. }
  245. int confdb_sa_reload (
  246. int flush,
  247. char *error_text,
  248. size_t errbuf_len)
  249. {
  250. char *errtext;
  251. int ret;
  252. ret = objdb->object_reload_config(flush, (const char **) &errtext);
  253. if (!ret) {
  254. strncpy(error_text, errtext, errbuf_len);
  255. if (errbuf_len > 0)
  256. error_text[errbuf_len-1] = '\0';
  257. }
  258. return ret;
  259. }
  260. int confdb_sa_object_find (
  261. hdb_handle_t parent_object_handle,
  262. hdb_handle_t *find_handle,
  263. hdb_handle_t *object_handle,
  264. const void *object_name,
  265. size_t object_name_len)
  266. {
  267. int res;
  268. if (!*find_handle) {
  269. objdb->object_find_create(parent_object_handle,
  270. object_name, object_name_len,
  271. find_handle);
  272. }
  273. res = objdb->object_find_next(*find_handle,
  274. object_handle);
  275. return res;
  276. }
  277. int confdb_sa_object_iter (
  278. hdb_handle_t parent_object_handle,
  279. hdb_handle_t *find_handle,
  280. hdb_handle_t *object_handle,
  281. const void *object_name,
  282. size_t object_name_len,
  283. void *found_object_name,
  284. size_t *found_object_name_len)
  285. {
  286. int res;
  287. if (!*find_handle) {
  288. objdb->object_find_create(parent_object_handle,
  289. object_name, object_name_len,
  290. find_handle);
  291. }
  292. res = objdb->object_find_next(*find_handle,
  293. object_handle);
  294. /* Return object name if we were called as _iter */
  295. if (!res) {
  296. objdb->object_name_get(*object_handle,
  297. found_object_name, found_object_name_len);
  298. }
  299. return res;
  300. }
  301. int confdb_sa_key_iter (
  302. hdb_handle_t parent_object_handle,
  303. hdb_handle_t start_pos,
  304. void *key_name,
  305. size_t *key_name_len,
  306. void *value,
  307. size_t *value_len)
  308. {
  309. int res;
  310. void *kname, *kvalue;
  311. res = objdb->object_key_iter_from(parent_object_handle,
  312. start_pos,
  313. &kname, key_name_len,
  314. &kvalue, value_len);
  315. if (!res) {
  316. memcpy(key_name, kname, *key_name_len);
  317. memcpy(value, kvalue, *value_len);
  318. }
  319. return res;
  320. }
  321. int confdb_sa_find_destroy(hdb_handle_t find_handle)
  322. {
  323. return objdb->object_find_destroy(find_handle);
  324. }