sa-confdb.c 8.8 KB

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