corosync-qdevice.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 2015-2019 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-votequorum.h"
  49. #include "utils.h"
  50. struct qdevice_instance *global_instance;
  51. static void
  52. signal_int_handler(int sig)
  53. {
  54. log(LOG_DEBUG, "SIGINT received - closing local unix socket");
  55. qdevice_ipc_close(global_instance);
  56. }
  57. static void
  58. signal_term_handler(int sig)
  59. {
  60. log(LOG_DEBUG, "SIGTERM received - closing local unix socket");
  61. qdevice_ipc_close(global_instance);
  62. }
  63. static void
  64. signal_handlers_register(void)
  65. {
  66. struct sigaction act;
  67. act.sa_handler = signal_int_handler;
  68. sigemptyset(&act.sa_mask);
  69. act.sa_flags = SA_RESTART;
  70. sigaction(SIGINT, &act, NULL);
  71. act.sa_handler = signal_term_handler;
  72. sigemptyset(&act.sa_mask);
  73. act.sa_flags = SA_RESTART;
  74. sigaction(SIGTERM, &act, NULL);
  75. act.sa_handler = SIG_DFL;
  76. sigemptyset(&act.sa_mask);
  77. act.sa_flags = SA_RESTART;
  78. sigaction(SIGCHLD, &act, NULL);
  79. act.sa_handler = SIG_IGN;
  80. sigemptyset(&act.sa_mask);
  81. act.sa_flags = SA_RESTART;
  82. sigaction(SIGPIPE, &act, NULL);
  83. }
  84. static void
  85. usage(void)
  86. {
  87. printf("usage: %s [-dfh] [-S option=value[,option2=value2,...]]\n", QDEVICE_PROGRAM_NAME);
  88. }
  89. static void
  90. cli_parse_long_opt(struct qdevice_advanced_settings *advanced_settings, const char *long_opt)
  91. {
  92. struct dynar_getopt_lex lex;
  93. struct dynar dynar_long_opt;
  94. const char *opt;
  95. const char *val;
  96. int res;
  97. dynar_init(&dynar_long_opt, strlen(long_opt) + 1);
  98. if (dynar_str_cpy(&dynar_long_opt, long_opt) != 0) {
  99. errx(1, "Can't alloc memory for long option");
  100. }
  101. dynar_getopt_lex_init(&lex, &dynar_long_opt);
  102. while (dynar_getopt_lex_token_next(&lex) == 0 && strcmp(dynar_data(&lex.option), "") != 0) {
  103. opt = dynar_data(&lex.option);
  104. val = dynar_data(&lex.value);
  105. res = qdevice_advanced_settings_set(advanced_settings, opt, val);
  106. switch (res) {
  107. case -1:
  108. errx(1, "Unknown option '%s'", opt);
  109. break;
  110. case -2:
  111. errx(1, "Invalid value '%s' for option '%s'", val, opt);
  112. break;
  113. }
  114. }
  115. dynar_getopt_lex_destroy(&lex);
  116. dynar_destroy(&dynar_long_opt);
  117. }
  118. static void
  119. cli_parse(int argc, char * const argv[], int *foreground, int *force_debug, int *bump_log_priority,
  120. struct qdevice_advanced_settings *advanced_settings)
  121. {
  122. int ch;
  123. *foreground = 0;
  124. *force_debug = 0;
  125. *bump_log_priority = 0;
  126. while ((ch = getopt(argc, argv, "dfhS:")) != -1) {
  127. switch (ch) {
  128. case 'd':
  129. if (*force_debug) {
  130. *bump_log_priority = 1;
  131. }
  132. *force_debug = 1;
  133. break;
  134. case 'f':
  135. *foreground = 1;
  136. break;
  137. case 'S':
  138. cli_parse_long_opt(advanced_settings, optarg);
  139. break;
  140. case 'h':
  141. case '?':
  142. usage();
  143. exit(1);
  144. break;
  145. }
  146. }
  147. }
  148. int
  149. main(int argc, char * const argv[])
  150. {
  151. struct qdevice_instance instance;
  152. struct qdevice_advanced_settings advanced_settings;
  153. int foreground;
  154. int force_debug;
  155. int bump_log_priority;
  156. int lock_file;
  157. int another_instance_running;
  158. int model_run_res;
  159. if (qdevice_advanced_settings_init(&advanced_settings) != 0) {
  160. errx(1, "Can't alloc memory for advanced settings");
  161. }
  162. cli_parse(argc, argv, &foreground, &force_debug, &bump_log_priority, &advanced_settings);
  163. qdevice_instance_init(&instance, &advanced_settings);
  164. qdevice_heuristics_init(&instance.heuristics_instance, &advanced_settings);
  165. instance.heuristics_instance.qdevice_instance_ptr = &instance;
  166. qdevice_cmap_init(&instance);
  167. if (qdevice_log_init(&instance, foreground, force_debug, bump_log_priority) == -1) {
  168. errx(1, "Can't initialize logging");
  169. }
  170. /*
  171. * Daemonize
  172. */
  173. if (!foreground) {
  174. utils_tty_detach();
  175. }
  176. if ((lock_file = utils_flock(advanced_settings.lock_file, getpid(),
  177. &another_instance_running)) == -1) {
  178. if (another_instance_running) {
  179. log(LOG_ERR, "Another instance is running");
  180. } else {
  181. log_err(LOG_ERR, "Can't acquire lock");
  182. }
  183. exit(1);
  184. }
  185. log(LOG_DEBUG, "Initializing votequorum");
  186. qdevice_votequorum_init(&instance);
  187. log(LOG_DEBUG, "Initializing local socket");
  188. if (qdevice_ipc_init(&instance) != 0) {
  189. return (1);
  190. }
  191. log(LOG_DEBUG, "Registering qdevice models");
  192. qdevice_model_register_all();
  193. log(LOG_DEBUG, "Configuring qdevice");
  194. if (qdevice_instance_configure_from_cmap(&instance) != 0) {
  195. return (1);
  196. }
  197. log(LOG_DEBUG, "Configuring master_wins");
  198. if (qdevice_votequorum_master_wins(&instance, (advanced_settings.master_wins ==
  199. QDEVICE_ADVANCED_SETTINGS_MASTER_WINS_FORCE_ON ? 1 : 0)) != 0) {
  200. return (1);
  201. }
  202. log(LOG_DEBUG, "Getting configuration node list");
  203. if (qdevice_cmap_store_config_node_list(&instance) != 0) {
  204. return (1);
  205. }
  206. log(LOG_DEBUG, "Initializing qdevice model");
  207. if (qdevice_model_init(&instance) != 0) {
  208. return (1);
  209. }
  210. log(LOG_DEBUG, "Initializing cmap tracking");
  211. if (qdevice_cmap_add_track(&instance) != 0) {
  212. return (1);
  213. }
  214. log(LOG_DEBUG, "Waiting for ring id");
  215. if (qdevice_votequorum_wait_for_ring_id(&instance) != 0) {
  216. return (1);
  217. }
  218. log(LOG_DEBUG, "Waiting for initial heuristics exec result");
  219. if (qdevice_heuristics_wait_for_initial_exec_result(&instance.heuristics_instance) != 0) {
  220. return (1);
  221. }
  222. global_instance = &instance;
  223. signal_handlers_register();
  224. log(LOG_DEBUG, "Running qdevice model");
  225. model_run_res = qdevice_model_run(&instance);
  226. log(LOG_DEBUG, "Removing cmap tracking");
  227. /*
  228. * Ignore error intentionally
  229. */
  230. (void)qdevice_cmap_del_track(&instance);
  231. log(LOG_DEBUG, "Destroying qdevice model");
  232. qdevice_model_destroy(&instance);
  233. log(LOG_DEBUG, "Destroying qdevice ipc");
  234. qdevice_ipc_destroy(&instance);
  235. log(LOG_DEBUG, "Destroying votequorum and cmap");
  236. qdevice_votequorum_destroy(&instance);
  237. qdevice_cmap_destroy(&instance);
  238. log(LOG_DEBUG, "Destroying heuristics");
  239. qdevice_heuristics_destroy(&instance.heuristics_instance, foreground);
  240. log(LOG_DEBUG, "Closing log");
  241. qdevice_log_close(&instance);
  242. qdevice_instance_destroy(&instance);
  243. qdevice_advanced_settings_destroy(&advanced_settings);
  244. return (model_run_res == 0 ? 0 : 2);
  245. }