sa-confdb.c 8.3 KB

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