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