4
0

sa-confdb.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. /* User's bootstrap config service */
  88. config_iface = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  89. if (!config_iface) {
  90. if ((config_iface = strdup("corosync_parser")) == NULL) {
  91. return -1;
  92. }
  93. }
  94. /* Make a copy so we can deface it with strtok */
  95. if ((config_iface = strdup(config_iface)) == NULL) {
  96. return -1;
  97. }
  98. iface = strtok(config_iface, ":");
  99. while (iface)
  100. {
  101. res = lcr_ifact_reference (
  102. &config_handle,
  103. iface,
  104. config_version,
  105. &config_p,
  106. 0);
  107. config = (struct config_iface_ver0 *)config_p;
  108. if (res == -1) {
  109. return -1;
  110. }
  111. res = config->config_readconfig(objdb, &error_string);
  112. if (res == -1) {
  113. return -1;
  114. }
  115. config_modules[num_config_modules++] = config;
  116. iface = strtok(NULL, ":");
  117. }
  118. free(config_iface);
  119. return CS_OK;
  120. }
  121. /* Needed by objdb when it writes back the configuration */
  122. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  123. {
  124. *modules = config_modules;
  125. *num = num_config_modules;
  126. }
  127. int confdb_sa_init (void)
  128. {
  129. int res;
  130. res = load_objdb();
  131. if (res != CS_OK)
  132. return res;
  133. res = load_config();
  134. return res;
  135. }
  136. int confdb_sa_object_create (
  137. hdb_handle_t parent_object_handle,
  138. const void *object_name,
  139. size_t object_name_len,
  140. hdb_handle_t *object_handle)
  141. {
  142. return objdb->object_create(parent_object_handle,
  143. object_handle,
  144. object_name, object_name_len);
  145. }
  146. int confdb_sa_object_destroy (
  147. hdb_handle_t object_handle)
  148. {
  149. return objdb->object_destroy(object_handle);
  150. }
  151. int confdb_sa_object_parent_get (
  152. hdb_handle_t object_handle,
  153. hdb_handle_t *parent_object_handle)
  154. {
  155. return objdb->object_parent_get(object_handle, parent_object_handle);
  156. }
  157. int confdb_sa_key_create (
  158. hdb_handle_t parent_object_handle,
  159. const void *key_name,
  160. size_t key_name_len,
  161. const void *value,
  162. size_t value_len)
  163. {
  164. return objdb->object_key_create(parent_object_handle,
  165. key_name, key_name_len,
  166. value, value_len);
  167. }
  168. int confdb_sa_key_create_typed (
  169. hdb_handle_t parent_object_handle,
  170. const char *key_name,
  171. const void *value,
  172. size_t value_len,
  173. int type)
  174. {
  175. return objdb->object_key_create_typed(parent_object_handle,
  176. key_name,
  177. value, value_len, type);
  178. }
  179. int confdb_sa_key_delete (
  180. hdb_handle_t parent_object_handle,
  181. const void *key_name,
  182. size_t key_name_len,
  183. const void *value,
  184. size_t value_len)
  185. {
  186. return objdb->object_key_delete(parent_object_handle,
  187. key_name, key_name_len);
  188. }
  189. int confdb_sa_key_get (
  190. hdb_handle_t parent_object_handle,
  191. const void *key_name,
  192. size_t key_name_len,
  193. void *value,
  194. size_t *value_len)
  195. {
  196. int res;
  197. void *kvalue;
  198. res = objdb->object_key_get(parent_object_handle,
  199. key_name, key_name_len,
  200. &kvalue, value_len);
  201. if (!res) {
  202. memcpy(value, kvalue, *value_len);
  203. }
  204. return res;
  205. }
  206. int confdb_sa_key_get_typed (
  207. hdb_handle_t parent_object_handle,
  208. const char *key_name,
  209. void *value,
  210. size_t *value_len,
  211. int *type)
  212. {
  213. int res;
  214. void *kvalue;
  215. res = objdb->object_key_get_typed(parent_object_handle,
  216. key_name,
  217. &kvalue, value_len, (objdb_value_types_t*)type);
  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. size_t 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. size_t 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. size_t key_name_len,
  251. const void *old_value,
  252. size_t old_value_len,
  253. const void *new_value,
  254. size_t 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 (char *error_text, size_t errbuf_len)
  261. {
  262. const char *errtext;
  263. int ret;
  264. ret = objdb->object_write_config(&errtext);
  265. if (!ret) {
  266. strncpy(error_text, errtext, errbuf_len);
  267. if (errbuf_len > 0)
  268. error_text[errbuf_len-1] = '\0';
  269. }
  270. return ret;
  271. }
  272. int confdb_sa_reload (
  273. int flush,
  274. char *error_text,
  275. size_t errbuf_len)
  276. {
  277. char *errtext;
  278. int ret;
  279. ret = objdb->object_reload_config(flush, (const char **) &errtext);
  280. if (!ret) {
  281. strncpy(error_text, errtext, errbuf_len);
  282. if (errbuf_len > 0)
  283. error_text[errbuf_len-1] = '\0';
  284. }
  285. return ret;
  286. }
  287. int confdb_sa_object_find (
  288. hdb_handle_t parent_object_handle,
  289. hdb_handle_t *find_handle,
  290. hdb_handle_t *object_handle,
  291. const void *object_name,
  292. size_t object_name_len)
  293. {
  294. int res;
  295. if (!*find_handle) {
  296. objdb->object_find_create(parent_object_handle,
  297. object_name, object_name_len,
  298. find_handle);
  299. }
  300. res = objdb->object_find_next(*find_handle,
  301. object_handle);
  302. return res;
  303. }
  304. int confdb_sa_object_iter (
  305. hdb_handle_t parent_object_handle,
  306. hdb_handle_t *find_handle,
  307. hdb_handle_t *object_handle,
  308. const void *object_name,
  309. size_t object_name_len,
  310. void *found_object_name,
  311. size_t *found_object_name_len)
  312. {
  313. int res;
  314. if (!*find_handle) {
  315. objdb->object_find_create(parent_object_handle,
  316. object_name, object_name_len,
  317. find_handle);
  318. }
  319. res = objdb->object_find_next(*find_handle,
  320. object_handle);
  321. /* Return object name if we were called as _iter */
  322. if (!res) {
  323. objdb->object_name_get(*object_handle,
  324. found_object_name, found_object_name_len);
  325. }
  326. return res;
  327. }
  328. int confdb_sa_key_iter (
  329. hdb_handle_t parent_object_handle,
  330. hdb_handle_t start_pos,
  331. void *key_name,
  332. size_t *key_name_len,
  333. void *value,
  334. size_t *value_len)
  335. {
  336. int res;
  337. void *kname, *kvalue;
  338. res = objdb->object_key_iter_from(parent_object_handle,
  339. start_pos,
  340. &kname, key_name_len,
  341. &kvalue, value_len);
  342. if (!res) {
  343. memcpy(key_name, kname, *key_name_len);
  344. memcpy(value, kvalue, *value_len);
  345. }
  346. return res;
  347. }
  348. int confdb_sa_key_iter_typed (
  349. hdb_handle_t parent_object_handle,
  350. hdb_handle_t start_pos,
  351. char *key_name,
  352. void *value,
  353. size_t *value_len,
  354. int *type)
  355. {
  356. int res;
  357. void *kname;
  358. void *kvalue;
  359. size_t key_name_len;
  360. res = objdb->object_key_iter_from(parent_object_handle,
  361. start_pos,
  362. &kname, &key_name_len,
  363. &kvalue, value_len);
  364. if (!res) {
  365. memcpy(key_name, kname, key_name_len);
  366. memcpy(value, kvalue, *value_len);
  367. objdb->object_key_get_typed(parent_object_handle,
  368. key_name,
  369. &kvalue, value_len, (objdb_value_types_t*)type);
  370. }
  371. return res;
  372. }
  373. int confdb_sa_find_destroy(hdb_handle_t find_handle)
  374. {
  375. return objdb->object_find_destroy(find_handle);
  376. }