4
0

corosync-qdevice.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@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 Red Hat, 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. #include <err.h>
  35. #include <signal.h>
  36. #include <stdio.h>
  37. #include "dynar.h"
  38. #include "dynar-str.h"
  39. #include "dynar-getopt-lex.h"
  40. #include "log.h"
  41. #include "qdevice-advanced-settings.h"
  42. #include "qdevice-config.h"
  43. #include "qdevice-cmap.h"
  44. #include "qdevice-heuristics.h"
  45. #include "qdevice-ipc.h"
  46. #include "qdevice-log.h"
  47. #include "qdevice-model.h"
  48. #include "qdevice-pr-poll-loop.h"
  49. #include "qdevice-pr-poll-loop-cb.h"
  50. #include "qdevice-votequorum.h"
  51. #include "utils.h"
  52. #ifdef HAVE_LIBSYSTEMD
  53. #include <systemd/sd-daemon.h>
  54. #endif
  55. struct qdevice_instance *global_instance;
  56. static void
  57. signal_int_handler(int sig)
  58. {
  59. log(LOG_DEBUG, "SIGINT received - closing local unix socket");
  60. qdevice_ipc_close(global_instance);
  61. }
  62. static void
  63. signal_term_handler(int sig)
  64. {
  65. log(LOG_DEBUG, "SIGTERM received - closing local unix socket");
  66. qdevice_ipc_close(global_instance);
  67. }
  68. static void
  69. signal_handlers_register(void)
  70. {
  71. struct sigaction act;
  72. act.sa_handler = signal_int_handler;
  73. sigemptyset(&act.sa_mask);
  74. act.sa_flags = SA_RESTART;
  75. sigaction(SIGINT, &act, NULL);
  76. act.sa_handler = signal_term_handler;
  77. sigemptyset(&act.sa_mask);
  78. act.sa_flags = SA_RESTART;
  79. sigaction(SIGTERM, &act, NULL);
  80. act.sa_handler = SIG_DFL;
  81. sigemptyset(&act.sa_mask);
  82. act.sa_flags = SA_RESTART;
  83. sigaction(SIGCHLD, &act, NULL);
  84. act.sa_handler = SIG_IGN;
  85. sigemptyset(&act.sa_mask);
  86. act.sa_flags = SA_RESTART;
  87. sigaction(SIGPIPE, &act, NULL);
  88. }
  89. static int
  90. qdevice_run_main_loop(struct qdevice_instance *instance)
  91. {
  92. int res;
  93. enum qdevice_model_post_poll_loop_exit_reason exit_reason;
  94. int restart_loop;
  95. log(LOG_DEBUG, "Running QDevice main loop");
  96. restart_loop = 1;
  97. while (restart_loop) {
  98. res = qdevice_model_pre_poll_loop(instance);
  99. if (res == -1) {
  100. return (-1);
  101. }
  102. while ((res = pr_poll_loop_exec(&instance->main_poll_loop)) == 0) {
  103. }
  104. if (res == -2) {
  105. log(LOG_CRIT, "pr_poll_loop_exec returned -2 - internal error");
  106. return (-1);
  107. } else if (res == -3) {
  108. log_nss(LOG_CRIT, "pr_poll_loop_exec returned -3 - PR_Poll error");
  109. return (-1);
  110. }
  111. exit_reason = QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_MODEL;
  112. if (instance->votequorum_closed) {
  113. exit_reason = QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_VOTEQUORUM_CLOSED;
  114. } else if (instance->cmap_closed) {
  115. exit_reason = QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_CMAP_CLOSED;
  116. } else if (instance->heuristics_closed) {
  117. exit_reason = QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_HEURISTICS_CLOSED;
  118. } else if (qdevice_ipc_is_closed(instance)) {
  119. exit_reason = QDEVICE_MODEL_POST_POLL_LOOP_EXIT_REASON_IPC_SOCKET_CLOSED;
  120. }
  121. res = qdevice_model_post_poll_loop(instance, exit_reason);
  122. if (res == 0 || res == -1) {
  123. restart_loop = 0;
  124. }
  125. }
  126. return (res);
  127. }
  128. static void
  129. usage(void)
  130. {
  131. printf("usage: %s [-dfh] [-S option=value[,option2=value2,...]]\n", QDEVICE_PROGRAM_NAME);
  132. }
  133. static void
  134. cli_parse_long_opt(struct qdevice_advanced_settings *advanced_settings, const char *long_opt)
  135. {
  136. struct dynar_getopt_lex lex;
  137. struct dynar dynar_long_opt;
  138. const char *opt;
  139. const char *val;
  140. int res;
  141. dynar_init(&dynar_long_opt, strlen(long_opt) + 1);
  142. if (dynar_str_cpy(&dynar_long_opt, long_opt) != 0) {
  143. errx(EXIT_FAILURE, "Can't alloc memory for long option");
  144. }
  145. dynar_getopt_lex_init(&lex, &dynar_long_opt);
  146. while (dynar_getopt_lex_token_next(&lex) == 0 && strcmp(dynar_data(&lex.option), "") != 0) {
  147. opt = dynar_data(&lex.option);
  148. val = dynar_data(&lex.value);
  149. res = qdevice_advanced_settings_set(advanced_settings, opt, val);
  150. switch (res) {
  151. case -1:
  152. errx(EXIT_FAILURE, "Unknown option '%s'", opt);
  153. break;
  154. case -2:
  155. errx(EXIT_FAILURE, "Invalid value '%s' for option '%s'", val, opt);
  156. break;
  157. }
  158. }
  159. dynar_getopt_lex_destroy(&lex);
  160. dynar_destroy(&dynar_long_opt);
  161. }
  162. static void
  163. cli_parse(int argc, char * const argv[], int *foreground, int *force_debug, int *bump_log_priority,
  164. struct qdevice_advanced_settings *advanced_settings)
  165. {
  166. int ch;
  167. *foreground = 0;
  168. *force_debug = 0;
  169. *bump_log_priority = 0;
  170. while ((ch = getopt(argc, argv, "dfhS:")) != -1) {
  171. switch (ch) {
  172. case 'd':
  173. if (*force_debug) {
  174. *bump_log_priority = 1;
  175. }
  176. *force_debug = 1;
  177. break;
  178. case 'f':
  179. *foreground = 1;
  180. break;
  181. case 'S':
  182. cli_parse_long_opt(advanced_settings, optarg);
  183. break;
  184. case 'h':
  185. case '?':
  186. usage();
  187. exit(EXIT_FAILURE);
  188. break;
  189. }
  190. }
  191. }
  192. int
  193. main(int argc, char * const argv[])
  194. {
  195. struct qdevice_instance instance;
  196. struct qdevice_advanced_settings advanced_settings;
  197. int foreground;
  198. int force_debug;
  199. int bump_log_priority;
  200. int lock_file;
  201. int another_instance_running;
  202. int main_loop_res;
  203. if (qdevice_advanced_settings_init(&advanced_settings) != 0) {
  204. errx(EXIT_FAILURE, "Can't alloc memory for advanced settings");
  205. }
  206. cli_parse(argc, argv, &foreground, &force_debug, &bump_log_priority, &advanced_settings);
  207. qdevice_instance_init(&instance, &advanced_settings);
  208. qdevice_heuristics_init(&instance.heuristics_instance, &advanced_settings);
  209. qdevice_cmap_init(&instance);
  210. if (qdevice_log_init(&instance, foreground, force_debug, bump_log_priority) == -1) {
  211. errx(EXIT_FAILURE, "Can't initialize logging");
  212. }
  213. /*
  214. * Daemonize
  215. */
  216. if (!foreground) {
  217. utils_tty_detach();
  218. }
  219. if ((lock_file = utils_flock(advanced_settings.lock_file, getpid(),
  220. &another_instance_running)) == -1) {
  221. if (another_instance_running) {
  222. log(LOG_ERR, "Another instance is running");
  223. } else {
  224. log_err(LOG_ERR, "Can't acquire lock");
  225. }
  226. return (EXIT_FAILURE);
  227. }
  228. log(LOG_DEBUG, "Initializing votequorum");
  229. qdevice_votequorum_init(&instance);
  230. log(LOG_DEBUG, "Initializing local socket");
  231. if (qdevice_ipc_init(&instance) != 0) {
  232. return (EXIT_FAILURE);
  233. }
  234. log(LOG_DEBUG, "Registering qdevice models");
  235. qdevice_model_register_all();
  236. log(LOG_DEBUG, "Configuring qdevice");
  237. if (qdevice_instance_configure_from_cmap(&instance) != 0) {
  238. return (EXIT_FAILURE);
  239. }
  240. log(LOG_DEBUG, "Configuring master_wins");
  241. if (qdevice_votequorum_master_wins(&instance, (advanced_settings.master_wins ==
  242. QDEVICE_ADVANCED_SETTINGS_MASTER_WINS_FORCE_ON ? 1 : 0)) != 0) {
  243. return (EXIT_FAILURE);
  244. }
  245. log(LOG_DEBUG, "Getting configuration node list");
  246. if (qdevice_cmap_store_config_node_list(&instance) != 0) {
  247. return (EXIT_FAILURE);
  248. }
  249. log(LOG_DEBUG, "Initializing qdevice model");
  250. if (qdevice_model_init(&instance) != 0) {
  251. return (EXIT_FAILURE);
  252. }
  253. log(LOG_DEBUG, "Initializing cmap tracking");
  254. if (qdevice_cmap_add_track(&instance) != 0) {
  255. return (EXIT_FAILURE);
  256. }
  257. log(LOG_DEBUG, "Registering main poll loop callbacks");
  258. if (qdevice_pr_poll_loop_cb_register(&instance) != 0) {
  259. return (EXIT_FAILURE);
  260. }
  261. log(LOG_DEBUG, "Waiting for ring id");
  262. if (qdevice_votequorum_wait_for_ring_id(&instance) != 0) {
  263. return (EXIT_FAILURE);
  264. }
  265. global_instance = &instance;
  266. signal_handlers_register();
  267. log(LOG_DEBUG, "Waiting for initial heuristics exec result");
  268. if (qdevice_pr_poll_loop_wait_for_initial_heuristics_exec_result(&instance) != 0) {
  269. return (EXIT_FAILURE);
  270. }
  271. log(LOG_DEBUG, "Qdevice ready to provide service");
  272. #ifdef HAVE_LIBSYSTEMD
  273. sd_notify (0, "READY=1");
  274. #endif
  275. main_loop_res = qdevice_run_main_loop(&instance);
  276. log(LOG_DEBUG, "Removing cmap tracking");
  277. /*
  278. * Ignore error intentionally
  279. */
  280. (void)qdevice_cmap_del_track(&instance);
  281. log(LOG_DEBUG, "Destroying qdevice model");
  282. qdevice_model_destroy(&instance);
  283. log(LOG_DEBUG, "Destroying qdevice ipc");
  284. qdevice_ipc_destroy(&instance);
  285. log(LOG_DEBUG, "Destroying votequorum and cmap");
  286. qdevice_votequorum_destroy(&instance);
  287. qdevice_cmap_destroy(&instance);
  288. log(LOG_DEBUG, "Destroying heuristics");
  289. qdevice_heuristics_destroy(&instance.heuristics_instance, foreground);
  290. log(LOG_DEBUG, "Closing log");
  291. qdevice_log_close(&instance);
  292. qdevice_instance_destroy(&instance);
  293. qdevice_advanced_settings_destroy(&advanced_settings);
  294. return (main_loop_res == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  295. }