sa-confdb.c 8.1 KB

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