corosync-qdevice.c 6.7 KB

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