corosync-qdevice.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <signal.h>
  35. #include "qdevice-config.h"
  36. #include "qdevice-cmap.h"
  37. #include "qdevice-ipc.h"
  38. #include "qdevice-log.h"
  39. #include "qdevice-model.h"
  40. #include "qdevice-votequorum.h"
  41. #include "utils.h"
  42. struct qdevice_instance *global_instance;
  43. static void
  44. signal_int_handler(int sig)
  45. {
  46. qdevice_log(LOG_DEBUG, "SIGINT received - closing local unix socket");
  47. qdevice_ipc_destroy(global_instance);
  48. }
  49. static void
  50. signal_term_handler(int sig)
  51. {
  52. qdevice_log(LOG_DEBUG, "SIGTERM received - closing server socket");
  53. qdevice_ipc_destroy(global_instance);
  54. }
  55. static void
  56. signal_handlers_register(void)
  57. {
  58. struct sigaction act;
  59. act.sa_handler = signal_int_handler;
  60. sigemptyset(&act.sa_mask);
  61. act.sa_flags = SA_RESTART;
  62. sigaction(SIGINT, &act, NULL);
  63. act.sa_handler = signal_term_handler;
  64. sigemptyset(&act.sa_mask);
  65. act.sa_flags = SA_RESTART;
  66. sigaction(SIGTERM, &act, NULL);
  67. }
  68. static void
  69. usage(void)
  70. {
  71. printf("usage: %s [-dfh]\n", QDEVICE_PROGRAM_NAME);
  72. }
  73. static void
  74. cli_parse(int argc, char * const argv[], int *foreground, int *force_debug)
  75. {
  76. int ch;
  77. *foreground = 0;
  78. *force_debug = 0;
  79. while ((ch = getopt(argc, argv, "dfh")) != -1) {
  80. switch (ch) {
  81. case 'd':
  82. *force_debug = 1;
  83. break;
  84. case 'f':
  85. *foreground = 1;
  86. break;
  87. case 'h':
  88. case '?':
  89. usage();
  90. exit(1);
  91. break;
  92. }
  93. }
  94. }
  95. int
  96. main(int argc, char * const argv[])
  97. {
  98. struct qdevice_instance instance;
  99. int foreground;
  100. int force_debug;
  101. int lock_file;
  102. int another_instance_running;
  103. cli_parse(argc, argv, &foreground, &force_debug);
  104. qdevice_instance_init(&instance);
  105. qdevice_cmap_init(&instance);
  106. qdevice_log_init(&instance, force_debug);
  107. /*
  108. * Daemonize
  109. */
  110. if (!foreground) {
  111. utils_tty_detach();
  112. }
  113. if ((lock_file = utils_flock(QDEVICE_LOCK_FILE, getpid(), &another_instance_running)) == -1) {
  114. if (another_instance_running) {
  115. qdevice_log(LOG_ERR, "Another instance is running");
  116. } else {
  117. qdevice_log_err(LOG_ERR, "Can't aquire lock");
  118. }
  119. exit(1);
  120. }
  121. qdevice_log(LOG_DEBUG, "Initializing votequorum");
  122. qdevice_votequorum_init(&instance);
  123. qdevice_log(LOG_DEBUG, "Initializing local socket");
  124. if (qdevice_ipc_init(&instance) != 0) {
  125. return (1);
  126. }
  127. global_instance = &instance;
  128. signal_handlers_register();
  129. qdevice_log(LOG_DEBUG, "Registering qdevice models");
  130. qdevice_model_register_all();
  131. qdevice_log(LOG_DEBUG, "Configuring qdevice");
  132. if (qdevice_instance_configure_from_cmap(&instance) != 0) {
  133. return (1);
  134. }
  135. qdevice_log(LOG_DEBUG, "Getting configuration node list");
  136. if (qdevice_cmap_store_config_node_list(&instance) != 0) {
  137. return (1);
  138. }
  139. qdevice_log(LOG_DEBUG, "Initializing qdevice model");
  140. if (qdevice_model_init(&instance) != 0) {
  141. return (1);
  142. }
  143. qdevice_log(LOG_DEBUG, "Initializing cmap tracking");
  144. if (qdevice_cmap_add_track(&instance) != 0) {
  145. return (1);
  146. }
  147. qdevice_log(LOG_DEBUG, "Running qdevice model");
  148. if (qdevice_model_run(&instance) != 0) {
  149. return (1);
  150. }
  151. qdevice_log(LOG_DEBUG, "Removing cmap tracking");
  152. if (qdevice_cmap_del_track(&instance) != 0) {
  153. return (1);
  154. }
  155. qdevice_log(LOG_DEBUG, "Destorying qdevice model");
  156. qdevice_model_destroy(&instance);
  157. qdevice_ipc_destroy(&instance);
  158. qdevice_votequorum_destroy(&instance);
  159. qdevice_cmap_destroy(&instance);
  160. qdevice_log_close(&instance);
  161. qdevice_instance_destroy(&instance);
  162. return (0);
  163. }