sa-confdb.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 <sys/types.h>
  43. #include <errno.h>
  44. #include <corosync/corotypes.h>
  45. #include <corosync/coroipcc.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. static int load_objdb(void)
  57. {
  58. hdb_handle_t objdb_handle;
  59. void *objdb_p;
  60. int res;
  61. /*
  62. * Load the object database interface
  63. */
  64. res = lcr_ifact_reference (
  65. &objdb_handle,
  66. "objdb",
  67. 0,
  68. &objdb_p,
  69. (void *)0);
  70. if (res == -1) {
  71. return -1;
  72. }
  73. objdb = (struct objdb_iface_ver0 *)objdb_p;
  74. objdb->objdb_init ();
  75. return CS_OK;
  76. }
  77. static int load_config(void)
  78. {
  79. char *config_iface;
  80. char *iface;
  81. int res;
  82. hdb_handle_t config_handle;
  83. hdb_handle_t config_version = 0;
  84. void *config_p;
  85. struct config_iface_ver0 *config;
  86. const char *error_string;
  87. char *strtok_savept;
  88. /* User's bootstrap config service */
  89. config_iface = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  90. if (!config_iface) {
  91. if ((config_iface = strdup("corosync_parser")) == NULL) {
  92. return -1;
  93. }
  94. }
  95. /* Make a copy so we can deface it with strtok */
  96. if ((config_iface = strdup(config_iface)) == NULL) {
  97. return -1;
  98. }
  99. iface = strtok_r (config_iface, ":", &strtok_savept);
  100. while (iface)
  101. {
  102. res = lcr_ifact_reference (
  103. &config_handle,
  104. iface,
  105. config_version,
  106. &config_p,
  107. 0);
  108. config = (struct config_iface_ver0 *)config_p;
  109. if (res == -1) {
  110. return -1;
  111. }
  112. res = config->config_readconfig(objdb, &error_string);
  113. if (res == -1) {
  114. return -1;
  115. }
  116. config_modules[num_config_modules++] = config;
  117. iface = strtok_r (NULL, ":", &strtok_savept);
  118. }
  119. free(config_iface);
  120. return CS_OK;
  121. }
  122. /* Needed by objdb when it writes back the configuration */
  123. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  124. {
  125. *modules = config_modules;
  126. *num = num_config_modules;
  127. }
  128. int confdb_sa_init (void)
  129. {
  130. int res;
  131. res = load_objdb();
  132. if (res != CS_OK)
  133. return res;
  134. res = load_config();
  135. return res;
  136. }
  137. int confdb_sa_object_create (
  138. hdb_handle_t parent_object_handle,
  139. const void *object_name,
  140. size_t object_name_len,
  141. hdb_handle_t *object_handle)
  142. {
  143. return objdb->object_create(parent_object_handle,
  144. object_handle,
  145. object_name, object_name_len);
  146. }
  147. int confdb_sa_object_destroy (
  148. hdb_handle_t object_handle)
  149. {
  150. return objdb->object_destroy(object_handle);
  151. }
  152. int confdb_sa_object_parent_get (
  153. hdb_handle_t object_handle,
  154. hdb_handle_t *parent_object_handle)
  155. {
  156. return objdb->object_parent_get(object_handle, parent_object_handle);
  157. }
  158. int confdb_sa_key_create (
  159. hdb_handle_t parent_object_handle,
  160. const void *key_name,
  161. size_t key_name_len,
  162. const void *value,
  163. size_t value_len)
  164. {
  165. return objdb->object_key_create(parent_object_handle,
  166. key_name, key_name_len,
  167. value, value_len);
  168. }
  169. int confdb_sa_key_create_typed (
  170. hdb_handle_t parent_object_handle,
  171. const char *key_name,
  172. const void *value,
  173. size_t value_len,
  174. int type)
  175. {
  176. return objdb->object_key_create_typed(parent_object_handle,
  177. key_name,
  178. value, value_len, type);
  179. }
  180. int confdb_sa_key_delete (
  181. hdb_handle_t parent_object_handle,
  182. const void *key_name,
  183. size_t key_name_len,
  184. const void *value,
  185. size_t value_len)
  186. {
  187. return objdb->object_key_delete(parent_object_handle,
  188. key_name, key_name_len);
  189. }
  190. int confdb_sa_key_get (
  191. hdb_handle_t parent_object_handle,
  192. const void *key_name,
  193. size_t key_name_len,
  194. void *value,
  195. size_t *value_len)
  196. {
  197. int res;
  198. void *kvalue;
  199. res = objdb->object_key_get(parent_object_handle,
  200. key_name, key_name_len,
  201. &kvalue, value_len);
  202. if (!res) {
  203. memcpy(value, kvalue, *value_len);
  204. }
  205. return res;
  206. }
  207. int confdb_sa_key_get_typed (
  208. hdb_handle_t parent_object_handle,
  209. const char *key_name,
  210. void *value,
  211. size_t *value_len,
  212. int *type)
  213. {
  214. int res;
  215. void *kvalue;
  216. res = objdb->object_key_get_typed(parent_object_handle,
  217. key_name,
  218. &kvalue, value_len, (objdb_value_types_t*)type);
  219. if (!res) {
  220. memcpy(value, kvalue, *value_len);
  221. }
  222. return res;
  223. }
  224. int confdb_sa_key_increment (
  225. hdb_handle_t parent_object_handle,
  226. const void *key_name,
  227. size_t key_name_len,
  228. unsigned int *value)
  229. {
  230. int res;
  231. res = objdb->object_key_increment(parent_object_handle,
  232. key_name, key_name_len,
  233. value);
  234. return res;
  235. }
  236. int confdb_sa_key_decrement (
  237. hdb_handle_t parent_object_handle,
  238. const void *key_name,
  239. size_t key_name_len,
  240. unsigned int *value)
  241. {
  242. int res;
  243. res = objdb->object_key_decrement(parent_object_handle,
  244. key_name, key_name_len,
  245. value);
  246. return res;
  247. }
  248. int confdb_sa_key_replace (
  249. hdb_handle_t parent_object_handle,
  250. const void *key_name,
  251. size_t key_name_len,
  252. const void *old_value,
  253. size_t old_value_len,
  254. const void *new_value,
  255. size_t new_value_len)
  256. {
  257. return objdb->object_key_replace(parent_object_handle,
  258. key_name, key_name_len,
  259. new_value, new_value_len);
  260. }
  261. int confdb_sa_write (char *error_text, size_t errbuf_len)
  262. {
  263. const char *errtext;
  264. int ret;
  265. ret = objdb->object_write_config(&errtext);
  266. if (!ret) {
  267. strncpy(error_text, errtext, errbuf_len);
  268. if (errbuf_len > 0)
  269. error_text[errbuf_len-1] = '\0';
  270. }
  271. return ret;
  272. }
  273. int confdb_sa_reload (
  274. int flush,
  275. char *error_text,
  276. size_t errbuf_len)
  277. {
  278. char *errtext;
  279. int ret;
  280. ret = objdb->object_reload_config(flush, (const char **) &errtext);
  281. if (!ret) {
  282. strncpy(error_text, errtext, errbuf_len);
  283. if (errbuf_len > 0)
  284. error_text[errbuf_len-1] = '\0';
  285. }
  286. return ret;
  287. }
  288. int confdb_sa_object_find (
  289. hdb_handle_t parent_object_handle,
  290. hdb_handle_t *find_handle,
  291. hdb_handle_t *object_handle,
  292. const void *object_name,
  293. size_t object_name_len)
  294. {
  295. int res;
  296. if (!*find_handle) {
  297. objdb->object_find_create(parent_object_handle,
  298. object_name, object_name_len,
  299. find_handle);
  300. }
  301. res = objdb->object_find_next(*find_handle,
  302. object_handle);
  303. return res;
  304. }
  305. int confdb_sa_object_iter (
  306. hdb_handle_t parent_object_handle,
  307. hdb_handle_t *find_handle,
  308. hdb_handle_t *object_handle,
  309. const void *object_name,
  310. size_t object_name_len,
  311. void *found_object_name,
  312. size_t *found_object_name_len)
  313. {
  314. int res;
  315. if (!*find_handle) {
  316. objdb->object_find_create(parent_object_handle,
  317. object_name, object_name_len,
  318. find_handle);
  319. }
  320. res = objdb->object_find_next(*find_handle,
  321. object_handle);
  322. /* Return object name if we were called as _iter */
  323. if (!res) {
  324. objdb->object_name_get(*object_handle,
  325. found_object_name, found_object_name_len);
  326. }
  327. return res;
  328. }
  329. int confdb_sa_key_iter (
  330. hdb_handle_t parent_object_handle,
  331. hdb_handle_t start_pos,
  332. void *key_name,
  333. size_t *key_name_len,
  334. void *value,
  335. size_t *value_len)
  336. {
  337. int res;
  338. void *kname, *kvalue;
  339. res = objdb->object_key_iter_from(parent_object_handle,
  340. start_pos,
  341. &kname, key_name_len,
  342. &kvalue, value_len);
  343. if (!res) {
  344. memcpy(key_name, kname, *key_name_len);
  345. memcpy(value, kvalue, *value_len);
  346. }
  347. return res;
  348. }
  349. int confdb_sa_key_iter_typed (
  350. hdb_handle_t parent_object_handle,
  351. hdb_handle_t start_pos,
  352. char *key_name,
  353. void *value,
  354. size_t *value_len,
  355. int *type)
  356. {
  357. int res;
  358. void *kname;
  359. void *kvalue;
  360. size_t key_name_len;
  361. res = objdb->object_key_iter_from(parent_object_handle,
  362. start_pos,
  363. &kname, &key_name_len,
  364. &kvalue, value_len);
  365. if (!res) {
  366. memcpy(key_name, kname, key_name_len);
  367. key_name[key_name_len] = '\0';
  368. memcpy(value, kvalue, *value_len);
  369. objdb->object_key_get_typed(parent_object_handle,
  370. key_name,
  371. &kvalue, value_len, (objdb_value_types_t*)type);
  372. }
  373. return res;
  374. }
  375. int confdb_sa_find_destroy(hdb_handle_t find_handle)
  376. {
  377. return objdb->object_find_destroy(find_handle);
  378. }