4
0

sa-confdb.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 openais 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/saAis.h>
  45. #include <corosync/ais_util.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 SA_AIS_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 SA_AIS_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 != SA_AIS_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_replace (
  217. unsigned int parent_object_handle,
  218. void *key_name,
  219. int key_name_len,
  220. void *old_value,
  221. int old_value_len,
  222. void *new_value,
  223. int new_value_len)
  224. {
  225. return objdb->object_key_replace(parent_object_handle,
  226. key_name, key_name_len,
  227. old_value, old_value_len,
  228. new_value, new_value_len);
  229. }
  230. int confdb_sa_object_find (
  231. unsigned int parent_object_handle,
  232. unsigned int start_pos,
  233. void *object_name,
  234. int object_name_len,
  235. unsigned int *object_handle,
  236. unsigned int *next_pos)
  237. {
  238. return objdb->object_find_from(parent_object_handle,
  239. start_pos,
  240. object_name,
  241. object_name_len,
  242. object_handle,
  243. next_pos);
  244. }
  245. int confdb_sa_write (
  246. unsigned int parent_object_handle,
  247. char *error_text)
  248. {
  249. char *errtext;
  250. int ret;
  251. ret = objdb->object_write_config(&errtext);
  252. if (!ret)
  253. strcpy(error_text, errtext);
  254. return ret;
  255. }
  256. int confdb_sa_reload (
  257. unsigned int parent_object_handle,
  258. int flush,
  259. char *error_text)
  260. {
  261. char *errtext;
  262. int ret;
  263. ret = objdb->object_reload_config(flush, &errtext);
  264. if (!ret)
  265. strcpy(error_text, errtext);
  266. return ret;
  267. }
  268. int confdb_sa_object_iter (
  269. unsigned int parent_object_handle,
  270. unsigned int start_pos,
  271. unsigned int *object_handle,
  272. void *object_name,
  273. int *object_name_len)
  274. {
  275. int res;
  276. void *objname;
  277. res = objdb->object_iter_from(parent_object_handle,
  278. start_pos,
  279. &objname, object_name_len,
  280. object_handle);
  281. if (!res) {
  282. memcpy(object_name, objname, *object_name_len);
  283. }
  284. return res;
  285. }
  286. int confdb_sa_key_iter (
  287. unsigned int parent_object_handle,
  288. unsigned int start_pos,
  289. void *key_name,
  290. int *key_name_len,
  291. void *value,
  292. int *value_len)
  293. {
  294. int res;
  295. void *kname, *kvalue;
  296. res = objdb->object_key_iter_from(parent_object_handle,
  297. start_pos,
  298. &kname, key_name_len,
  299. &kvalue, value_len);
  300. if (!res) {
  301. memcpy(key_name, kname, *key_name_len);
  302. memcpy(value, kvalue, *value_len);
  303. }
  304. return res;
  305. }