sa-confdb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (c) 2008, 2012 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_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. if (!*value) {
  228. *value = malloc(*value_len + 1);
  229. if (!*value) {
  230. res = CS_ERR_NO_MEMORY;
  231. }
  232. }
  233. if (*value) {
  234. memcpy(*value, kvalue, *value_len);
  235. }
  236. }
  237. return res;
  238. }
  239. int confdb_sa_key_increment (
  240. hdb_handle_t parent_object_handle,
  241. const void *key_name,
  242. size_t key_name_len,
  243. unsigned int *value)
  244. {
  245. int res;
  246. res = objdb->object_key_increment(parent_object_handle,
  247. key_name, key_name_len,
  248. value);
  249. return res;
  250. }
  251. int confdb_sa_key_decrement (
  252. hdb_handle_t parent_object_handle,
  253. const void *key_name,
  254. size_t key_name_len,
  255. unsigned int *value)
  256. {
  257. int res;
  258. res = objdb->object_key_decrement(parent_object_handle,
  259. key_name, key_name_len,
  260. value);
  261. return res;
  262. }
  263. int confdb_sa_key_replace (
  264. hdb_handle_t parent_object_handle,
  265. const void *key_name,
  266. size_t key_name_len,
  267. const void *old_value,
  268. size_t old_value_len,
  269. const void *new_value,
  270. size_t new_value_len)
  271. {
  272. return objdb->object_key_replace(parent_object_handle,
  273. key_name, key_name_len,
  274. new_value, new_value_len);
  275. }
  276. int confdb_sa_write (char *error_text, size_t errbuf_len)
  277. {
  278. const char *errtext;
  279. int ret;
  280. ret = objdb->object_write_config(&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_reload (
  289. int flush,
  290. char *error_text,
  291. size_t errbuf_len)
  292. {
  293. char *errtext;
  294. int ret;
  295. ret = objdb->object_reload_config(flush, (const char **) &errtext);
  296. if (!ret) {
  297. strncpy(error_text, errtext, errbuf_len);
  298. if (errbuf_len > 0)
  299. error_text[errbuf_len-1] = '\0';
  300. }
  301. return ret;
  302. }
  303. int confdb_sa_object_find (
  304. hdb_handle_t parent_object_handle,
  305. hdb_handle_t *find_handle,
  306. hdb_handle_t *object_handle,
  307. const void *object_name,
  308. size_t object_name_len)
  309. {
  310. int res;
  311. if (!*find_handle) {
  312. objdb->object_find_create(parent_object_handle,
  313. object_name, object_name_len,
  314. find_handle);
  315. }
  316. res = objdb->object_find_next(*find_handle,
  317. object_handle);
  318. return res;
  319. }
  320. int confdb_sa_object_iter (
  321. hdb_handle_t parent_object_handle,
  322. hdb_handle_t *find_handle,
  323. hdb_handle_t *object_handle,
  324. const void *object_name,
  325. size_t object_name_len,
  326. void *found_object_name,
  327. size_t *found_object_name_len)
  328. {
  329. int res;
  330. if (!*find_handle) {
  331. objdb->object_find_create(parent_object_handle,
  332. object_name, object_name_len,
  333. find_handle);
  334. }
  335. res = objdb->object_find_next(*find_handle,
  336. object_handle);
  337. /* Return object name if we were called as _iter */
  338. if (!res) {
  339. objdb->object_name_get(*object_handle,
  340. found_object_name, found_object_name_len);
  341. }
  342. return res;
  343. }
  344. int confdb_sa_key_iter (
  345. hdb_handle_t parent_object_handle,
  346. hdb_handle_t start_pos,
  347. void *key_name,
  348. size_t *key_name_len,
  349. void *value,
  350. size_t *value_len)
  351. {
  352. int res;
  353. void *kname, *kvalue;
  354. res = objdb->object_key_iter_from(parent_object_handle,
  355. start_pos,
  356. &kname, key_name_len,
  357. &kvalue, value_len);
  358. if (!res) {
  359. memcpy(key_name, kname, *key_name_len);
  360. memcpy(value, kvalue, *value_len);
  361. }
  362. return res;
  363. }
  364. int confdb_sa_key_iter_typed (
  365. hdb_handle_t parent_object_handle,
  366. hdb_handle_t start_pos,
  367. char *key_name,
  368. void **value,
  369. size_t *value_len,
  370. int *type)
  371. {
  372. int res;
  373. void *kname;
  374. void *kvalue;
  375. size_t key_name_len;
  376. res = objdb->object_key_iter_from(parent_object_handle,
  377. start_pos,
  378. &kname, &key_name_len,
  379. &kvalue, value_len);
  380. if (!res) {
  381. memcpy(key_name, kname, key_name_len);
  382. key_name[key_name_len] = '\0';
  383. if (!*value) {
  384. *value = malloc(*value_len + 1);
  385. if (!*value) {
  386. res = CS_ERR_NO_MEMORY;
  387. }
  388. }
  389. if (*value) {
  390. memcpy(*value, kvalue, *value_len);
  391. objdb->object_key_get_typed(parent_object_handle,
  392. key_name,
  393. &kvalue, value_len, (objdb_value_types_t*)type);
  394. }
  395. }
  396. return res;
  397. }
  398. int confdb_sa_find_destroy(hdb_handle_t find_handle)
  399. {
  400. return objdb->object_find_destroy(find_handle);
  401. }