sa-confdb.c 8.7 KB

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