sa-confdb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 <qb/qbipcc.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_object_name_get(
  159. hdb_handle_t object_handle,
  160. char *object_name,
  161. size_t *object_name_len)
  162. {
  163. return objdb->object_name_get(object_handle, object_name, object_name_len);
  164. }
  165. int confdb_sa_key_create (
  166. hdb_handle_t parent_object_handle,
  167. const void *key_name,
  168. size_t key_name_len,
  169. const void *value,
  170. size_t value_len)
  171. {
  172. return objdb->object_key_create(parent_object_handle,
  173. key_name, key_name_len,
  174. value, value_len);
  175. }
  176. int confdb_sa_key_create_typed (
  177. hdb_handle_t parent_object_handle,
  178. const char *key_name,
  179. const void *value,
  180. size_t value_len,
  181. int type)
  182. {
  183. return objdb->object_key_create_typed(parent_object_handle,
  184. key_name,
  185. value, value_len, type);
  186. }
  187. int confdb_sa_key_delete (
  188. hdb_handle_t parent_object_handle,
  189. const void *key_name,
  190. size_t key_name_len,
  191. const void *value,
  192. size_t value_len)
  193. {
  194. return objdb->object_key_delete(parent_object_handle,
  195. key_name, key_name_len);
  196. }
  197. int confdb_sa_key_get (
  198. hdb_handle_t parent_object_handle,
  199. const void *key_name,
  200. size_t key_name_len,
  201. void *value,
  202. size_t *value_len)
  203. {
  204. int res;
  205. void *kvalue;
  206. res = objdb->object_key_get(parent_object_handle,
  207. key_name, key_name_len,
  208. &kvalue, value_len);
  209. if (!res) {
  210. memcpy(value, kvalue, *value_len);
  211. }
  212. return res;
  213. }
  214. int confdb_sa_key_get_typed (
  215. hdb_handle_t parent_object_handle,
  216. const char *key_name,
  217. void *value,
  218. size_t *value_len,
  219. int *type)
  220. {
  221. int res;
  222. void *kvalue;
  223. res = objdb->object_key_get_typed(parent_object_handle,
  224. key_name,
  225. &kvalue, value_len, (objdb_value_types_t*)type);
  226. if (!res) {
  227. memcpy(value, kvalue, *value_len);
  228. }
  229. return res;
  230. }
  231. int confdb_sa_key_increment (
  232. hdb_handle_t parent_object_handle,
  233. const void *key_name,
  234. size_t key_name_len,
  235. unsigned int *value)
  236. {
  237. int res;
  238. res = objdb->object_key_increment(parent_object_handle,
  239. key_name, key_name_len,
  240. value);
  241. return res;
  242. }
  243. int confdb_sa_key_decrement (
  244. hdb_handle_t parent_object_handle,
  245. const void *key_name,
  246. size_t key_name_len,
  247. unsigned int *value)
  248. {
  249. int res;
  250. res = objdb->object_key_decrement(parent_object_handle,
  251. key_name, key_name_len,
  252. value);
  253. return res;
  254. }
  255. int confdb_sa_key_replace (
  256. hdb_handle_t parent_object_handle,
  257. const void *key_name,
  258. size_t key_name_len,
  259. const void *old_value,
  260. size_t old_value_len,
  261. const void *new_value,
  262. size_t new_value_len)
  263. {
  264. return objdb->object_key_replace(parent_object_handle,
  265. key_name, key_name_len,
  266. new_value, new_value_len);
  267. }
  268. int confdb_sa_write (char *error_text, size_t errbuf_len)
  269. {
  270. const char *errtext;
  271. int ret;
  272. ret = objdb->object_write_config(&errtext);
  273. if (!ret) {
  274. strncpy(error_text, errtext, errbuf_len);
  275. if (errbuf_len > 0)
  276. error_text[errbuf_len-1] = '\0';
  277. }
  278. return ret;
  279. }
  280. int confdb_sa_reload (
  281. int flush,
  282. char *error_text,
  283. size_t errbuf_len)
  284. {
  285. char *errtext;
  286. int ret;
  287. ret = objdb->object_reload_config(flush, (const char **) &errtext);
  288. if (!ret) {
  289. strncpy(error_text, errtext, errbuf_len);
  290. if (errbuf_len > 0)
  291. error_text[errbuf_len-1] = '\0';
  292. }
  293. return ret;
  294. }
  295. int confdb_sa_object_find (
  296. hdb_handle_t parent_object_handle,
  297. hdb_handle_t *find_handle,
  298. hdb_handle_t *object_handle,
  299. const void *object_name,
  300. size_t object_name_len)
  301. {
  302. int res;
  303. if (!*find_handle) {
  304. objdb->object_find_create(parent_object_handle,
  305. object_name, object_name_len,
  306. find_handle);
  307. }
  308. res = objdb->object_find_next(*find_handle,
  309. object_handle);
  310. return res;
  311. }
  312. int confdb_sa_object_iter (
  313. hdb_handle_t parent_object_handle,
  314. hdb_handle_t *find_handle,
  315. hdb_handle_t *object_handle,
  316. const void *object_name,
  317. size_t object_name_len,
  318. void *found_object_name,
  319. size_t *found_object_name_len)
  320. {
  321. int res;
  322. if (!*find_handle) {
  323. objdb->object_find_create(parent_object_handle,
  324. object_name, object_name_len,
  325. find_handle);
  326. }
  327. res = objdb->object_find_next(*find_handle,
  328. object_handle);
  329. /* Return object name if we were called as _iter */
  330. if (!res) {
  331. objdb->object_name_get(*object_handle,
  332. found_object_name, found_object_name_len);
  333. }
  334. return res;
  335. }
  336. int confdb_sa_key_iter (
  337. hdb_handle_t parent_object_handle,
  338. hdb_handle_t start_pos,
  339. void *key_name,
  340. size_t *key_name_len,
  341. void *value,
  342. size_t *value_len)
  343. {
  344. int res;
  345. void *kname, *kvalue;
  346. res = objdb->object_key_iter_from(parent_object_handle,
  347. start_pos,
  348. &kname, key_name_len,
  349. &kvalue, value_len);
  350. if (!res) {
  351. memcpy(key_name, kname, *key_name_len);
  352. memcpy(value, kvalue, *value_len);
  353. }
  354. return res;
  355. }
  356. int confdb_sa_key_iter_typed (
  357. hdb_handle_t parent_object_handle,
  358. hdb_handle_t start_pos,
  359. char *key_name,
  360. void *value,
  361. size_t *value_len,
  362. int *type)
  363. {
  364. int res;
  365. void *kname;
  366. void *kvalue;
  367. size_t key_name_len;
  368. res = objdb->object_key_iter_from(parent_object_handle,
  369. start_pos,
  370. &kname, &key_name_len,
  371. &kvalue, value_len);
  372. if (!res) {
  373. memcpy(key_name, kname, key_name_len);
  374. key_name[key_name_len] = '\0';
  375. memcpy(value, kvalue, *value_len);
  376. objdb->object_key_get_typed(parent_object_handle,
  377. key_name,
  378. &kvalue, value_len, (objdb_value_types_t*)type);
  379. }
  380. return res;
  381. }
  382. int confdb_sa_find_destroy(hdb_handle_t find_handle)
  383. {
  384. return objdb->object_find_destroy(find_handle);
  385. }