sa-confdb.c 8.7 KB

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