4
0

sa-confdb.c 9.0 KB

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