]>
Commit | Line | Data |
---|---|---|
8b886ca7 | 1 | /* |
896014f4 DL |
2 | * Monitor status of frr daemons and restart if necessary. |
3 | * | |
4 | * Copyright (C) 2004 Andrew J. Schorr | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
8b886ca7 | 19 | */ |
20 | ||
a365534f | 21 | #include <zebra.h> |
8b886ca7 | 22 | #include <thread.h> |
23 | #include <log.h> | |
52e66296 | 24 | #include <network.h> |
8b886ca7 | 25 | #include <sigevent.h> |
a365534f | 26 | #include <lib/version.h> |
95c4aff2 | 27 | #include "command.h" |
87f44e2f | 28 | #include "memory_vty.h" |
4f04a76b | 29 | #include "libfrr.h" |
95c4aff2 | 30 | |
6f594023 | 31 | #include <getopt.h> |
a365534f | 32 | #include <sys/un.h> |
33 | #include <sys/wait.h> | |
837d16cc | 34 | #include <memory.h> |
651415bd | 35 | #include <systemd.h> |
8b886ca7 | 36 | |
9473e340 | 37 | #include "watchfrr.h" |
95c4aff2 | 38 | |
8b886ca7 | 39 | #ifndef MIN |
40 | #define MIN(X,Y) (((X) <= (Y)) ? (X) : (Y)) | |
41 | #endif | |
42 | ||
43 | /* Macros to help randomize timers. */ | |
44 | #define JITTER(X) ((random() % ((X)+1))-((X)/2)) | |
45 | #define FUZZY(X) ((X)+JITTER((X)/20)) | |
46 | ||
47 | #define DEFAULT_PERIOD 5 | |
48 | #define DEFAULT_TIMEOUT 10 | |
49 | #define DEFAULT_RESTART_TIMEOUT 20 | |
50 | #define DEFAULT_LOGLEVEL LOG_INFO | |
51 | #define DEFAULT_MIN_RESTART 60 | |
52 | #define DEFAULT_MAX_RESTART 600 | |
8b886ca7 | 53 | |
54 | #define PING_TOKEN "PING" | |
55 | ||
55c72803 | 56 | /* Needs to be global, referenced somewhere inside libfrr. */ |
8b886ca7 | 57 | struct thread_master *master; |
64a249ad | 58 | static char pidfile_default[256]; |
8b886ca7 | 59 | |
f168b713 | 60 | static bool watch_only = false; |
8b886ca7 | 61 | |
a6810074 DL |
62 | typedef enum { |
63 | PHASE_NONE = 0, | |
64 | PHASE_STOPS_PENDING, | |
65 | PHASE_WAITING_DOWN, | |
66 | PHASE_ZEBRA_RESTART_PENDING, | |
67 | PHASE_WAITING_ZEBRA_UP | |
8b886ca7 | 68 | } restart_phase_t; |
69 | ||
a6810074 DL |
70 | static const char *phase_str[] = { |
71 | "None", | |
72 | "Stop jobs running", | |
73 | "Waiting for other daemons to come down", | |
74 | "Zebra restart job running", | |
75 | "Waiting for zebra to come up", | |
76 | "Start jobs running", | |
8b886ca7 | 77 | }; |
78 | ||
79 | #define PHASE_TIMEOUT (3*gs.restart_timeout) | |
80 | ||
a6810074 DL |
81 | struct restart_info { |
82 | const char *name; | |
83 | const char *what; | |
84 | pid_t pid; | |
85 | struct timeval time; | |
86 | long interval; | |
87 | struct thread *t_kill; | |
88 | int kills; | |
098e240f | 89 | }; |
90 | ||
a6810074 | 91 | static struct global_state { |
a6810074 DL |
92 | restart_phase_t phase; |
93 | struct thread *t_phase_hanging; | |
94 | const char *vtydir; | |
95 | long period; | |
96 | long timeout; | |
97 | long restart_timeout; | |
98 | long min_restart_interval; | |
99 | long max_restart_interval; | |
a6810074 DL |
100 | struct daemon *daemons; |
101 | const char *restart_command; | |
102 | const char *start_command; | |
103 | const char *stop_command; | |
104 | struct restart_info restart; | |
a6810074 | 105 | int loglevel; |
d62a17ae | 106 | struct daemon *special; /* points to zebra when doing phased restart */ |
a6810074 DL |
107 | int numdaemons; |
108 | int numpids; | |
d62a17ae | 109 | int numdown; /* # of daemons that are not UP or UNRESPONSIVE */ |
8b886ca7 | 110 | } gs = { |
d62a17ae | 111 | .phase = PHASE_NONE, |
64a249ad | 112 | .vtydir = frr_vtydir, |
d62a17ae | 113 | .period = 1000 * DEFAULT_PERIOD, |
114 | .timeout = DEFAULT_TIMEOUT, | |
115 | .restart_timeout = DEFAULT_RESTART_TIMEOUT, | |
116 | .loglevel = DEFAULT_LOGLEVEL, | |
117 | .min_restart_interval = DEFAULT_MIN_RESTART, | |
118 | .max_restart_interval = DEFAULT_MAX_RESTART, | |
d62a17ae | 119 | }; |
a6810074 DL |
120 | |
121 | typedef enum { | |
122 | DAEMON_INIT, | |
123 | DAEMON_DOWN, | |
124 | DAEMON_CONNECTING, | |
125 | DAEMON_UP, | |
126 | DAEMON_UNRESPONSIVE | |
8b886ca7 | 127 | } daemon_state_t; |
128 | ||
d62a17ae | 129 | #define IS_UP(DMN) \ |
130 | (((DMN)->state == DAEMON_UP) || ((DMN)->state == DAEMON_UNRESPONSIVE)) | |
8b886ca7 | 131 | |
a6810074 | 132 | static const char *state_str[] = { |
d62a17ae | 133 | "Init", "Down", "Connecting", "Up", "Unresponsive", |
8b886ca7 | 134 | }; |
135 | ||
136 | struct daemon { | |
a6810074 DL |
137 | const char *name; |
138 | daemon_state_t state; | |
139 | int fd; | |
140 | struct timeval echo_sent; | |
141 | u_int connect_tries; | |
142 | struct thread *t_wakeup; | |
143 | struct thread *t_read; | |
144 | struct thread *t_write; | |
145 | struct daemon *next; | |
146 | struct restart_info restart; | |
8b886ca7 | 147 | }; |
148 | ||
9272302b DL |
149 | #define OPTION_MINRESTART 2000 |
150 | #define OPTION_MAXRESTART 2001 | |
f168b713 | 151 | #define OPTION_DRY 2002 |
9272302b | 152 | |
a6810074 DL |
153 | static const struct option longopts[] = { |
154 | {"daemon", no_argument, NULL, 'd'}, | |
155 | {"statedir", required_argument, NULL, 'S'}, | |
a6810074 DL |
156 | {"loglevel", required_argument, NULL, 'l'}, |
157 | {"interval", required_argument, NULL, 'i'}, | |
158 | {"timeout", required_argument, NULL, 't'}, | |
159 | {"restart-timeout", required_argument, NULL, 'T'}, | |
160 | {"restart", required_argument, NULL, 'r'}, | |
161 | {"start-command", required_argument, NULL, 's'}, | |
162 | {"kill-command", required_argument, NULL, 'k'}, | |
f168b713 | 163 | {"dry", no_argument, NULL, OPTION_DRY}, |
d62a17ae | 164 | {"min-restart-interval", required_argument, NULL, OPTION_MINRESTART}, |
165 | {"max-restart-interval", required_argument, NULL, OPTION_MAXRESTART}, | |
a6810074 DL |
166 | {"pid-file", required_argument, NULL, 'p'}, |
167 | {"blank-string", required_argument, NULL, 'b'}, | |
168 | {"help", no_argument, NULL, 'h'}, | |
169 | {"version", no_argument, NULL, 'v'}, | |
d62a17ae | 170 | {NULL, 0, NULL, 0}}; |
8b886ca7 | 171 | |
172 | static int try_connect(struct daemon *dmn); | |
173 | static int wakeup_send_echo(struct thread *t_wakeup); | |
174 | static void try_restart(struct daemon *dmn); | |
175 | static void phase_check(void); | |
176 | ||
4f04a76b DL |
177 | static const char *progname; |
178 | static void printhelp(FILE *target) | |
8b886ca7 | 179 | { |
d62a17ae | 180 | fprintf(target, |
181 | "Usage : %s [OPTION...] <daemon name> ...\n\n\ | |
9473e340 | 182 | Watchdog program to monitor status of frr daemons and try to restart\n\ |
8b886ca7 | 183 | them if they are down or unresponsive. It determines whether a daemon is\n\ |
184 | up based on whether it can connect to the daemon's vty unix stream socket.\n\ | |
185 | It then repeatedly sends echo commands over that socket to determine whether\n\ | |
186 | the daemon is responsive. If the daemon crashes, we will receive an EOF\n\ | |
187 | on the socket connection and know immediately that the daemon is down.\n\n\ | |
188 | The daemons to be monitored should be listed on the command line.\n\n\ | |
8b886ca7 | 189 | In order to avoid attempting to restart the daemons in a fast loop,\n\ |
190 | the -m and -M options allow you to control the minimum delay between\n\ | |
191 | restart commands. The minimum restart delay is recalculated each time\n\ | |
192 | a restart is attempted: if the time since the last restart attempt exceeds\n\ | |
193 | twice the -M value, then the restart delay is set to the -m value.\n\ | |
d62a17ae | 194 | Otherwise, the interval is doubled (but capped at the -M value).\n\n", |
f168b713 | 195 | progname); |
e757c940 | 196 | |
d62a17ae | 197 | fprintf(target, |
198 | "Options:\n\ | |
8b886ca7 | 199 | -d, --daemon Run in daemon mode. In this mode, error messages are sent\n\ |
200 | to syslog instead of stdout.\n\ | |
201 | -S, --statedir Set the vty socket directory (default is %s)\n\ | |
8b886ca7 | 202 | -l, --loglevel Set the logging level (default is %d).\n\ |
203 | The value should range from %d (LOG_EMERG) to %d (LOG_DEBUG),\n\ | |
204 | but it can be set higher than %d if extra-verbose debugging\n\ | |
205 | messages are desired.\n\ | |
9272302b | 206 | --min-restart-interval\n\ |
8b886ca7 | 207 | Set the minimum seconds to wait between invocations of daemon\n\ |
208 | restart commands (default is %d).\n\ | |
9272302b | 209 | --max-restart-interval\n\ |
8b886ca7 | 210 | Set the maximum seconds to wait between invocations of daemon\n\ |
211 | restart commands (default is %d).\n\ | |
212 | -i, --interval Set the status polling interval in seconds (default is %d)\n\ | |
213 | -t, --timeout Set the unresponsiveness timeout in seconds (default is %d)\n\ | |
214 | -T, --restart-timeout\n\ | |
215 | Set the restart (kill) timeout in seconds (default is %d).\n\ | |
216 | If any background jobs are still running after this much\n\ | |
217 | time has elapsed, they will be killed.\n\ | |
218 | -r, --restart Supply a Bourne shell command to use to restart a single\n\ | |
219 | daemon. The command string should include '%%s' where the\n\ | |
220 | name of the daemon should be substituted.\n\ | |
8b886ca7 | 221 | -s, --start-command\n\ |
222 | Supply a Bourne shell to command to use to start a single\n\ | |
223 | daemon. The command string should include '%%s' where the\n\ | |
224 | name of the daemon should be substituted.\n\ | |
225 | -k, --kill-command\n\ | |
226 | Supply a Bourne shell to command to use to stop a single\n\ | |
227 | daemon. The command string should include '%%s' where the\n\ | |
228 | name of the daemon should be substituted.\n\ | |
f168b713 | 229 | --dry Do not start or restart anything, just log.\n\ |
8b886ca7 | 230 | -p, --pid-file Set process identifier file name\n\ |
231 | (default is %s).\n\ | |
c8b40f86 | 232 | -b, --blank-string\n\ |
233 | When the supplied argument string is found in any of the\n\ | |
f168b713 | 234 | various shell command arguments (-r, -s, or -k), replace\n\ |
c8b40f86 | 235 | it with a space. This is an ugly hack to circumvent problems\n\ |
236 | passing command-line arguments with embedded spaces.\n\ | |
8b886ca7 | 237 | -v, --version Print program version\n\ |
d62a17ae | 238 | -h, --help Display this help and exit\n", |
64a249ad | 239 | frr_vtydir, DEFAULT_LOGLEVEL, LOG_EMERG, LOG_DEBUG, LOG_DEBUG, |
d62a17ae | 240 | DEFAULT_MIN_RESTART, DEFAULT_MAX_RESTART, DEFAULT_PERIOD, |
64a249ad | 241 | DEFAULT_TIMEOUT, DEFAULT_RESTART_TIMEOUT, pidfile_default); |
8b886ca7 | 242 | } |
243 | ||
a6810074 | 244 | static pid_t run_background(char *shell_cmd) |
8b886ca7 | 245 | { |
a6810074 DL |
246 | pid_t child; |
247 | ||
248 | switch (child = fork()) { | |
249 | case -1: | |
d62a17ae | 250 | zlog_err("fork failed, cannot run command [%s]: %s", shell_cmd, |
251 | safe_strerror(errno)); | |
a6810074 DL |
252 | return -1; |
253 | case 0: | |
254 | /* Child process. */ | |
d62a17ae | 255 | /* Use separate process group so child processes can be killed |
256 | * easily. */ | |
a6810074 DL |
257 | if (setpgid(0, 0) < 0) |
258 | zlog_warn("warning: setpgid(0,0) failed: %s", | |
259 | safe_strerror(errno)); | |
260 | { | |
261 | char shell[] = "sh"; | |
262 | char dashc[] = "-c"; | |
d62a17ae | 263 | char *const argv[4] = {shell, dashc, shell_cmd, NULL}; |
a6810074 | 264 | execv("/bin/sh", argv); |
d62a17ae | 265 | zlog_err("execv(/bin/sh -c '%s') failed: %s", shell_cmd, |
266 | safe_strerror(errno)); | |
a6810074 DL |
267 | _exit(127); |
268 | } | |
269 | default: | |
270 | /* Parent process: we will reap the child later. */ | |
271 | zlog_err("Forked background command [pid %d]: %s", (int)child, | |
272 | shell_cmd); | |
273 | return child; | |
274 | } | |
8b886ca7 | 275 | } |
276 | ||
a6810074 DL |
277 | static struct timeval *time_elapsed(struct timeval *result, |
278 | const struct timeval *start_time) | |
8b886ca7 | 279 | { |
a6810074 DL |
280 | gettimeofday(result, NULL); |
281 | result->tv_sec -= start_time->tv_sec; | |
282 | result->tv_usec -= start_time->tv_usec; | |
283 | while (result->tv_usec < 0) { | |
284 | result->tv_usec += 1000000L; | |
285 | result->tv_sec--; | |
286 | } | |
287 | return result; | |
8b886ca7 | 288 | } |
289 | ||
a6810074 | 290 | static int restart_kill(struct thread *t_kill) |
8b886ca7 | 291 | { |
a6810074 DL |
292 | struct restart_info *restart = THREAD_ARG(t_kill); |
293 | struct timeval delay; | |
294 | ||
295 | time_elapsed(&delay, &restart->time); | |
d62a17ae | 296 | zlog_warn( |
297 | "Warning: %s %s child process %d still running after " | |
298 | "%ld seconds, sending signal %d", | |
299 | restart->what, restart->name, (int)restart->pid, | |
300 | (long)delay.tv_sec, (restart->kills ? SIGKILL : SIGTERM)); | |
a6810074 DL |
301 | kill(-restart->pid, (restart->kills ? SIGKILL : SIGTERM)); |
302 | restart->kills++; | |
66e78ae6 QY |
303 | restart->t_kill = NULL; |
304 | thread_add_timer(master, restart_kill, restart, gs.restart_timeout, | |
305 | &restart->t_kill); | |
a6810074 | 306 | return 0; |
8b886ca7 | 307 | } |
308 | ||
a6810074 | 309 | static struct restart_info *find_child(pid_t child) |
8b886ca7 | 310 | { |
f168b713 DL |
311 | struct daemon *dmn; |
312 | for (dmn = gs.daemons; dmn; dmn = dmn->next) { | |
313 | if (dmn->restart.pid == child) | |
314 | return &dmn->restart; | |
a6810074 DL |
315 | } |
316 | return NULL; | |
8b886ca7 | 317 | } |
318 | ||
a6810074 | 319 | static void sigchild(void) |
8b886ca7 | 320 | { |
a6810074 DL |
321 | pid_t child; |
322 | int status; | |
323 | const char *name; | |
324 | const char *what; | |
325 | struct restart_info *restart; | |
326 | ||
327 | switch (child = waitpid(-1, &status, WNOHANG)) { | |
328 | case -1: | |
329 | zlog_err("waitpid failed: %s", safe_strerror(errno)); | |
330 | return; | |
331 | case 0: | |
332 | zlog_warn("SIGCHLD received, but waitpid did not reap a child"); | |
333 | return; | |
334 | } | |
335 | ||
336 | if (child == integrated_write_pid) { | |
337 | integrated_write_sigchld(status); | |
338 | return; | |
339 | } | |
340 | ||
341 | if ((restart = find_child(child)) != NULL) { | |
342 | name = restart->name; | |
343 | what = restart->what; | |
344 | restart->pid = 0; | |
345 | gs.numpids--; | |
346 | thread_cancel(restart->t_kill); | |
347 | restart->t_kill = NULL; | |
d62a17ae | 348 | /* Update restart time to reflect the time the command |
349 | * completed. */ | |
a6810074 DL |
350 | gettimeofday(&restart->time, NULL); |
351 | } else { | |
d62a17ae | 352 | zlog_err( |
353 | "waitpid returned status for an unknown child process %d", | |
354 | (int)child); | |
a6810074 DL |
355 | name = "(unknown)"; |
356 | what = "background"; | |
357 | } | |
358 | if (WIFSTOPPED(status)) | |
d62a17ae | 359 | zlog_warn("warning: %s %s process %d is stopped", what, name, |
360 | (int)child); | |
a6810074 | 361 | else if (WIFSIGNALED(status)) |
d62a17ae | 362 | zlog_warn("%s %s process %d terminated due to signal %d", what, |
363 | name, (int)child, WTERMSIG(status)); | |
a6810074 DL |
364 | else if (WIFEXITED(status)) { |
365 | if (WEXITSTATUS(status) != 0) | |
d62a17ae | 366 | zlog_warn( |
367 | "%s %s process %d exited with non-zero status %d", | |
368 | what, name, (int)child, WEXITSTATUS(status)); | |
a6810074 DL |
369 | else |
370 | zlog_debug("%s %s process %d exited normally", what, | |
371 | name, (int)child); | |
372 | } else | |
373 | zlog_err("cannot interpret %s %s process %d wait status 0x%x", | |
374 | what, name, (int)child, status); | |
375 | phase_check(); | |
8b886ca7 | 376 | } |
377 | ||
d62a17ae | 378 | static int run_job(struct restart_info *restart, const char *cmdtype, |
379 | const char *command, int force, int update_interval) | |
8b886ca7 | 380 | { |
a6810074 DL |
381 | struct timeval delay; |
382 | ||
383 | if (gs.loglevel > LOG_DEBUG + 1) | |
384 | zlog_debug("attempting to %s %s", cmdtype, restart->name); | |
385 | ||
386 | if (restart->pid) { | |
387 | if (gs.loglevel > LOG_DEBUG + 1) | |
d62a17ae | 388 | zlog_debug( |
389 | "cannot %s %s, previous pid %d still running", | |
390 | cmdtype, restart->name, (int)restart->pid); | |
a6810074 DL |
391 | return -1; |
392 | } | |
393 | ||
d62a17ae | 394 | /* Note: time_elapsed test must come before the force test, since we |
395 | need | |
a6810074 DL |
396 | to make sure that delay is initialized for use below in updating the |
397 | restart interval. */ | |
398 | if ((time_elapsed(&delay, &restart->time)->tv_sec < restart->interval) | |
399 | && !force) { | |
400 | if (gs.loglevel > LOG_DEBUG + 1) | |
d62a17ae | 401 | zlog_debug( |
402 | "postponing %s %s: " | |
403 | "elapsed time %ld < retry interval %ld", | |
404 | cmdtype, restart->name, (long)delay.tv_sec, | |
405 | restart->interval); | |
a6810074 DL |
406 | return -1; |
407 | } | |
408 | ||
409 | gettimeofday(&restart->time, NULL); | |
410 | restart->kills = 0; | |
411 | { | |
412 | char cmd[strlen(command) + strlen(restart->name) + 1]; | |
413 | snprintf(cmd, sizeof(cmd), command, restart->name); | |
414 | if ((restart->pid = run_background(cmd)) > 0) { | |
66e78ae6 | 415 | restart->t_kill = NULL; |
d62a17ae | 416 | thread_add_timer(master, restart_kill, restart, |
417 | gs.restart_timeout, &restart->t_kill); | |
a6810074 DL |
418 | restart->what = cmdtype; |
419 | gs.numpids++; | |
420 | } else | |
421 | restart->pid = 0; | |
422 | } | |
423 | ||
424 | /* Calculate the new restart interval. */ | |
425 | if (update_interval) { | |
426 | if (delay.tv_sec > 2 * gs.max_restart_interval) | |
427 | restart->interval = gs.min_restart_interval; | |
428 | else if ((restart->interval *= 2) > gs.max_restart_interval) | |
429 | restart->interval = gs.max_restart_interval; | |
430 | if (gs.loglevel > LOG_DEBUG + 1) | |
431 | zlog_debug("restart %s interval is now %ld", | |
432 | restart->name, restart->interval); | |
433 | } | |
434 | return restart->pid; | |
8b886ca7 | 435 | } |
436 | ||
d62a17ae | 437 | #define SET_READ_HANDLER(DMN) \ |
438 | do { \ | |
439 | (DMN)->t_read = NULL; \ | |
440 | thread_add_read(master, handle_read, (DMN), (DMN)->fd, \ | |
441 | &(DMN)->t_read); \ | |
442 | } while (0); | |
443 | ||
444 | #define SET_WAKEUP_DOWN(DMN) \ | |
445 | do { \ | |
446 | (DMN)->t_wakeup = NULL; \ | |
447 | thread_add_timer_msec(master, wakeup_down, (DMN), \ | |
448 | FUZZY(gs.period), &(DMN)->t_wakeup); \ | |
449 | } while (0); | |
450 | ||
451 | #define SET_WAKEUP_UNRESPONSIVE(DMN) \ | |
452 | do { \ | |
453 | (DMN)->t_wakeup = NULL; \ | |
454 | thread_add_timer_msec(master, wakeup_unresponsive, (DMN), \ | |
455 | FUZZY(gs.period), &(DMN)->t_wakeup); \ | |
456 | } while (0); | |
457 | ||
458 | #define SET_WAKEUP_ECHO(DMN) \ | |
459 | do { \ | |
460 | (DMN)->t_wakeup = NULL; \ | |
461 | thread_add_timer_msec(master, wakeup_send_echo, (DMN), \ | |
462 | FUZZY(gs.period), &(DMN)->t_wakeup); \ | |
463 | } while (0); | |
8b886ca7 | 464 | |
a6810074 | 465 | static int wakeup_down(struct thread *t_wakeup) |
8b886ca7 | 466 | { |
a6810074 DL |
467 | struct daemon *dmn = THREAD_ARG(t_wakeup); |
468 | ||
469 | dmn->t_wakeup = NULL; | |
470 | if (try_connect(dmn) < 0) | |
471 | SET_WAKEUP_DOWN(dmn); | |
472 | if ((dmn->connect_tries > 1) && (dmn->state != DAEMON_UP)) | |
473 | try_restart(dmn); | |
474 | return 0; | |
8b886ca7 | 475 | } |
476 | ||
a6810074 | 477 | static int wakeup_init(struct thread *t_wakeup) |
8b886ca7 | 478 | { |
a6810074 DL |
479 | struct daemon *dmn = THREAD_ARG(t_wakeup); |
480 | ||
481 | dmn->t_wakeup = NULL; | |
482 | if (try_connect(dmn) < 0) { | |
483 | SET_WAKEUP_DOWN(dmn); | |
484 | zlog_err("%s state -> down : initial connection attempt failed", | |
485 | dmn->name); | |
486 | dmn->state = DAEMON_DOWN; | |
487 | } | |
488 | return 0; | |
8b886ca7 | 489 | } |
490 | ||
a6810074 | 491 | static void daemon_down(struct daemon *dmn, const char *why) |
8b886ca7 | 492 | { |
a6810074 DL |
493 | if (IS_UP(dmn) || (dmn->state == DAEMON_INIT)) |
494 | zlog_err("%s state -> down : %s", dmn->name, why); | |
495 | else if (gs.loglevel > LOG_DEBUG) | |
496 | zlog_debug("%s still down : %s", dmn->name, why); | |
497 | if (IS_UP(dmn)) | |
498 | gs.numdown++; | |
499 | dmn->state = DAEMON_DOWN; | |
500 | if (dmn->fd >= 0) { | |
501 | close(dmn->fd); | |
502 | dmn->fd = -1; | |
503 | } | |
504 | THREAD_OFF(dmn->t_read); | |
505 | THREAD_OFF(dmn->t_write); | |
506 | THREAD_OFF(dmn->t_wakeup); | |
507 | if (try_connect(dmn) < 0) | |
508 | SET_WAKEUP_DOWN(dmn); | |
509 | phase_check(); | |
8b886ca7 | 510 | } |
511 | ||
a6810074 | 512 | static int handle_read(struct thread *t_read) |
8b886ca7 | 513 | { |
a6810074 DL |
514 | struct daemon *dmn = THREAD_ARG(t_read); |
515 | static const char resp[sizeof(PING_TOKEN) + 4] = PING_TOKEN "\n"; | |
516 | char buf[sizeof(resp) + 100]; | |
517 | ssize_t rc; | |
518 | struct timeval delay; | |
519 | ||
520 | dmn->t_read = NULL; | |
521 | if ((rc = read(dmn->fd, buf, sizeof(buf))) < 0) { | |
522 | char why[100]; | |
523 | ||
524 | if (ERRNO_IO_RETRY(errno)) { | |
525 | /* Pretend it never happened. */ | |
526 | SET_READ_HANDLER(dmn); | |
527 | return 0; | |
528 | } | |
529 | snprintf(why, sizeof(why), "unexpected read error: %s", | |
530 | safe_strerror(errno)); | |
531 | daemon_down(dmn, why); | |
532 | return 0; | |
8b886ca7 | 533 | } |
a6810074 DL |
534 | if (rc == 0) { |
535 | daemon_down(dmn, "read returned EOF"); | |
536 | return 0; | |
537 | } | |
538 | if (!dmn->echo_sent.tv_sec) { | |
539 | char why[sizeof(buf) + 100]; | |
540 | snprintf(why, sizeof(why), | |
541 | "unexpected read returns %d bytes: %.*s", (int)rc, | |
542 | (int)rc, buf); | |
543 | daemon_down(dmn, why); | |
544 | return 0; | |
8b886ca7 | 545 | } |
a6810074 DL |
546 | |
547 | /* We are expecting an echo response: is there any chance that the | |
548 | response would not be returned entirely in the first read? That | |
549 | seems inconceivable... */ | |
550 | if ((rc != sizeof(resp)) || memcmp(buf, resp, sizeof(resp))) { | |
551 | char why[100 + sizeof(buf)]; | |
552 | snprintf(why, sizeof(why), | |
553 | "read returned bad echo response of %d bytes " | |
d62a17ae | 554 | "(expecting %u): %.*s", |
555 | (int)rc, (u_int)sizeof(resp), (int)rc, buf); | |
a6810074 DL |
556 | daemon_down(dmn, why); |
557 | return 0; | |
558 | } | |
559 | ||
560 | time_elapsed(&delay, &dmn->echo_sent); | |
561 | dmn->echo_sent.tv_sec = 0; | |
562 | if (dmn->state == DAEMON_UNRESPONSIVE) { | |
563 | if (delay.tv_sec < gs.timeout) { | |
564 | dmn->state = DAEMON_UP; | |
d62a17ae | 565 | zlog_warn( |
566 | "%s state -> up : echo response received after %ld.%06ld " | |
567 | "seconds", | |
568 | dmn->name, (long)delay.tv_sec, | |
569 | (long)delay.tv_usec); | |
a6810074 | 570 | } else |
d62a17ae | 571 | zlog_warn( |
572 | "%s: slow echo response finally received after %ld.%06ld " | |
573 | "seconds", | |
574 | dmn->name, (long)delay.tv_sec, | |
575 | (long)delay.tv_usec); | |
a6810074 DL |
576 | } else if (gs.loglevel > LOG_DEBUG + 1) |
577 | zlog_debug("%s: echo response received after %ld.%06ld seconds", | |
578 | dmn->name, (long)delay.tv_sec, (long)delay.tv_usec); | |
579 | ||
580 | SET_READ_HANDLER(dmn); | |
581 | if (dmn->t_wakeup) | |
582 | thread_cancel(dmn->t_wakeup); | |
583 | SET_WAKEUP_ECHO(dmn); | |
584 | ||
585 | return 0; | |
8b886ca7 | 586 | } |
587 | ||
207e0d7a DS |
588 | /* |
589 | * Wait till we notice that all daemons are ready before | |
590 | * we send we are ready to systemd | |
591 | */ | |
a6810074 | 592 | static void daemon_send_ready(void) |
207e0d7a | 593 | { |
a6810074 DL |
594 | static int sent = 0; |
595 | if (!sent && gs.numdown == 0) { | |
a6810074 | 596 | FILE *fp; |
207e0d7a | 597 | |
a6810074 DL |
598 | fp = fopen(DAEMON_VTY_DIR "/watchfrr.started", "w"); |
599 | fclose(fp); | |
d62a17ae | 600 | zlog_notice( |
601 | "Watchfrr: Notifying Systemd we are up and running"); | |
a6810074 DL |
602 | systemd_send_started(master, 0); |
603 | sent = 1; | |
604 | } | |
207e0d7a DS |
605 | } |
606 | ||
a6810074 | 607 | static void daemon_up(struct daemon *dmn, const char *why) |
8b886ca7 | 608 | { |
a6810074 DL |
609 | dmn->state = DAEMON_UP; |
610 | gs.numdown--; | |
611 | dmn->connect_tries = 0; | |
612 | zlog_notice("%s state -> up : %s", dmn->name, why); | |
613 | daemon_send_ready(); | |
a8cbb8b3 | 614 | SET_WAKEUP_ECHO(dmn); |
a6810074 | 615 | phase_check(); |
8b886ca7 | 616 | } |
617 | ||
a6810074 | 618 | static int check_connect(struct thread *t_write) |
8b886ca7 | 619 | { |
a6810074 DL |
620 | struct daemon *dmn = THREAD_ARG(t_write); |
621 | int sockerr; | |
622 | socklen_t reslen = sizeof(sockerr); | |
623 | ||
624 | dmn->t_write = NULL; | |
625 | if (getsockopt(dmn->fd, SOL_SOCKET, SO_ERROR, (char *)&sockerr, &reslen) | |
626 | < 0) { | |
627 | zlog_warn("%s: check_connect: getsockopt failed: %s", dmn->name, | |
628 | safe_strerror(errno)); | |
629 | daemon_down(dmn, | |
630 | "getsockopt failed checking connection success"); | |
631 | return 0; | |
632 | } | |
633 | if ((reslen == sizeof(sockerr)) && sockerr) { | |
634 | char why[100]; | |
d62a17ae | 635 | snprintf( |
636 | why, sizeof(why), | |
637 | "getsockopt reports that connection attempt failed: %s", | |
638 | safe_strerror(sockerr)); | |
a6810074 DL |
639 | daemon_down(dmn, why); |
640 | return 0; | |
641 | } | |
642 | ||
643 | daemon_up(dmn, "delayed connect succeeded"); | |
644 | return 0; | |
8b886ca7 | 645 | } |
646 | ||
a6810074 | 647 | static int wakeup_connect_hanging(struct thread *t_wakeup) |
8b886ca7 | 648 | { |
a6810074 DL |
649 | struct daemon *dmn = THREAD_ARG(t_wakeup); |
650 | char why[100]; | |
651 | ||
652 | dmn->t_wakeup = NULL; | |
653 | snprintf(why, sizeof(why), | |
654 | "connection attempt timed out after %ld seconds", gs.timeout); | |
655 | daemon_down(dmn, why); | |
656 | return 0; | |
8b886ca7 | 657 | } |
658 | ||
659 | /* Making connection to protocol daemon. */ | |
a6810074 | 660 | static int try_connect(struct daemon *dmn) |
8b886ca7 | 661 | { |
a6810074 DL |
662 | int sock; |
663 | struct sockaddr_un addr; | |
664 | socklen_t len; | |
665 | ||
666 | if (gs.loglevel > LOG_DEBUG + 1) | |
667 | zlog_debug("%s: attempting to connect", dmn->name); | |
668 | dmn->connect_tries++; | |
669 | ||
670 | memset(&addr, 0, sizeof(struct sockaddr_un)); | |
671 | addr.sun_family = AF_UNIX; | |
d62a17ae | 672 | snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s.vty", gs.vtydir, |
673 | dmn->name); | |
6f0e3f6e | 674 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
a6810074 | 675 | len = addr.sun_len = SUN_LEN(&addr); |
8b886ca7 | 676 | #else |
a6810074 | 677 | len = sizeof(addr.sun_family) + strlen(addr.sun_path); |
d62a17ae | 678 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
a6810074 DL |
679 | |
680 | /* Quick check to see if we might succeed before we go to the trouble | |
681 | of creating a socket. */ | |
682 | if (access(addr.sun_path, W_OK) < 0) { | |
683 | if (errno != ENOENT) | |
684 | zlog_err("%s: access to socket %s denied: %s", | |
685 | dmn->name, addr.sun_path, | |
686 | safe_strerror(errno)); | |
687 | return -1; | |
688 | } | |
689 | ||
690 | if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { | |
d62a17ae | 691 | zlog_err("%s(%s): cannot make socket: %s", __func__, |
692 | addr.sun_path, safe_strerror(errno)); | |
a6810074 DL |
693 | return -1; |
694 | } | |
695 | ||
696 | if (set_nonblocking(sock) < 0 || set_cloexec(sock) < 0) { | |
d62a17ae | 697 | zlog_err("%s(%s): set_nonblocking/cloexec(%d) failed", __func__, |
698 | addr.sun_path, sock); | |
a6810074 DL |
699 | close(sock); |
700 | return -1; | |
8b886ca7 | 701 | } |
a6810074 DL |
702 | |
703 | if (connect(sock, (struct sockaddr *)&addr, len) < 0) { | |
704 | if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK)) { | |
705 | if (gs.loglevel > LOG_DEBUG) | |
706 | zlog_debug("%s(%s): connect failed: %s", | |
707 | __func__, addr.sun_path, | |
708 | safe_strerror(errno)); | |
709 | close(sock); | |
710 | return -1; | |
711 | } | |
712 | if (gs.loglevel > LOG_DEBUG) | |
713 | zlog_debug("%s: connection in progress", dmn->name); | |
714 | dmn->state = DAEMON_CONNECTING; | |
715 | dmn->fd = sock; | |
66e78ae6 QY |
716 | dmn->t_write = NULL; |
717 | thread_add_write(master, check_connect, dmn, dmn->fd, | |
d62a17ae | 718 | &dmn->t_write); |
719 | dmn->t_wakeup = NULL; | |
720 | thread_add_timer(master, wakeup_connect_hanging, dmn, | |
721 | gs.timeout, &dmn->t_wakeup); | |
a6810074 DL |
722 | SET_READ_HANDLER(dmn); |
723 | return 0; | |
724 | } | |
725 | ||
726 | dmn->fd = sock; | |
727 | SET_READ_HANDLER(dmn); | |
728 | daemon_up(dmn, "connect succeeded"); | |
729 | return 1; | |
8b886ca7 | 730 | } |
731 | ||
a6810074 | 732 | static int phase_hanging(struct thread *t_hanging) |
8b886ca7 | 733 | { |
a6810074 DL |
734 | gs.t_phase_hanging = NULL; |
735 | zlog_err("Phase [%s] hanging for %ld seconds, aborting phased restart", | |
736 | phase_str[gs.phase], PHASE_TIMEOUT); | |
737 | gs.phase = PHASE_NONE; | |
738 | return 0; | |
8b886ca7 | 739 | } |
740 | ||
a6810074 | 741 | static void set_phase(restart_phase_t new_phase) |
8b886ca7 | 742 | { |
a6810074 DL |
743 | gs.phase = new_phase; |
744 | if (gs.t_phase_hanging) | |
745 | thread_cancel(gs.t_phase_hanging); | |
66e78ae6 QY |
746 | gs.t_phase_hanging = NULL; |
747 | thread_add_timer(master, phase_hanging, NULL, PHASE_TIMEOUT, | |
748 | &gs.t_phase_hanging); | |
8b886ca7 | 749 | } |
750 | ||
a6810074 | 751 | static void phase_check(void) |
8b886ca7 | 752 | { |
a6810074 DL |
753 | switch (gs.phase) { |
754 | case PHASE_NONE: | |
755 | break; | |
756 | case PHASE_STOPS_PENDING: | |
757 | if (gs.numpids) | |
758 | break; | |
d62a17ae | 759 | zlog_info( |
760 | "Phased restart: all routing daemon stop jobs have completed."); | |
a6810074 DL |
761 | set_phase(PHASE_WAITING_DOWN); |
762 | ||
d62a17ae | 763 | /*FALLTHRU*/ |
a6810074 DL |
764 | case PHASE_WAITING_DOWN: |
765 | if (gs.numdown + IS_UP(gs.special) < gs.numdaemons) | |
766 | break; | |
767 | zlog_info("Phased restart: all routing daemons now down."); | |
768 | run_job(&gs.special->restart, "restart", gs.restart_command, 1, | |
769 | 1); | |
770 | set_phase(PHASE_ZEBRA_RESTART_PENDING); | |
771 | ||
d62a17ae | 772 | /*FALLTHRU*/ |
a6810074 DL |
773 | case PHASE_ZEBRA_RESTART_PENDING: |
774 | if (gs.special->restart.pid) | |
775 | break; | |
776 | zlog_info("Phased restart: %s restart job completed.", | |
777 | gs.special->name); | |
778 | set_phase(PHASE_WAITING_ZEBRA_UP); | |
779 | ||
d62a17ae | 780 | /*FALLTHRU*/ |
a6810074 DL |
781 | case PHASE_WAITING_ZEBRA_UP: |
782 | if (!IS_UP(gs.special)) | |
783 | break; | |
784 | zlog_info("Phased restart: %s is now up.", gs.special->name); | |
785 | { | |
786 | struct daemon *dmn; | |
787 | for (dmn = gs.daemons; dmn; dmn = dmn->next) { | |
788 | if (dmn != gs.special) | |
789 | run_job(&dmn->restart, "start", | |
790 | gs.start_command, 1, 0); | |
791 | } | |
792 | } | |
793 | gs.phase = PHASE_NONE; | |
794 | THREAD_OFF(gs.t_phase_hanging); | |
795 | zlog_notice("Phased global restart has completed."); | |
796 | break; | |
797 | } | |
8b886ca7 | 798 | } |
799 | ||
a6810074 | 800 | static void try_restart(struct daemon *dmn) |
8b886ca7 | 801 | { |
f168b713 | 802 | if (watch_only) |
a6810074 | 803 | return; |
a6810074 | 804 | |
f168b713 DL |
805 | if (dmn != gs.special) { |
806 | if ((gs.special->state == DAEMON_UP) | |
807 | && (gs.phase == PHASE_NONE)) | |
808 | run_job(&dmn->restart, "restart", gs.restart_command, 0, | |
809 | 1); | |
810 | else | |
811 | zlog_debug( | |
812 | "%s: postponing restart attempt because master %s daemon " | |
813 | "not up [%s], or phased restart in progress", | |
814 | dmn->name, gs.special->name, | |
815 | state_str[gs.special->state]); | |
816 | return; | |
817 | } | |
818 | ||
819 | if ((gs.phase != PHASE_NONE) || gs.numpids) { | |
820 | if (gs.loglevel > LOG_DEBUG + 1) | |
821 | zlog_debug( | |
822 | "postponing phased global restart: restart already in " | |
823 | "progress [%s], or outstanding child processes [%d]", | |
824 | phase_str[gs.phase], gs.numpids); | |
825 | return; | |
826 | } | |
827 | /* Is it too soon for a restart? */ | |
828 | { | |
829 | struct timeval delay; | |
830 | if (time_elapsed(&delay, &gs.special->restart.time)->tv_sec | |
831 | < gs.special->restart.interval) { | |
a6810074 | 832 | if (gs.loglevel > LOG_DEBUG + 1) |
d62a17ae | 833 | zlog_debug( |
f168b713 DL |
834 | "postponing phased global restart: " |
835 | "elapsed time %ld < retry interval %ld", | |
836 | (long)delay.tv_sec, | |
837 | gs.special->restart.interval); | |
838 | return; | |
a6810074 | 839 | } |
8b886ca7 | 840 | } |
f168b713 | 841 | run_job(&gs.restart, "restart", gs.restart_command, 0, 1); |
8b886ca7 | 842 | } |
843 | ||
a6810074 | 844 | static int wakeup_unresponsive(struct thread *t_wakeup) |
8b886ca7 | 845 | { |
a6810074 DL |
846 | struct daemon *dmn = THREAD_ARG(t_wakeup); |
847 | ||
848 | dmn->t_wakeup = NULL; | |
849 | if (dmn->state != DAEMON_UNRESPONSIVE) | |
d62a17ae | 850 | zlog_err( |
851 | "%s: no longer unresponsive (now %s), " | |
852 | "wakeup should have been cancelled!", | |
853 | dmn->name, state_str[dmn->state]); | |
a6810074 DL |
854 | else { |
855 | SET_WAKEUP_UNRESPONSIVE(dmn); | |
856 | try_restart(dmn); | |
857 | } | |
858 | return 0; | |
8b886ca7 | 859 | } |
860 | ||
a6810074 | 861 | static int wakeup_no_answer(struct thread *t_wakeup) |
8b886ca7 | 862 | { |
a6810074 DL |
863 | struct daemon *dmn = THREAD_ARG(t_wakeup); |
864 | ||
865 | dmn->t_wakeup = NULL; | |
866 | dmn->state = DAEMON_UNRESPONSIVE; | |
d62a17ae | 867 | zlog_err( |
868 | "%s state -> unresponsive : no response yet to ping " | |
869 | "sent %ld seconds ago", | |
870 | dmn->name, gs.timeout); | |
71e7975a DL |
871 | SET_WAKEUP_UNRESPONSIVE(dmn); |
872 | try_restart(dmn); | |
a6810074 | 873 | return 0; |
8b886ca7 | 874 | } |
875 | ||
a6810074 | 876 | static int wakeup_send_echo(struct thread *t_wakeup) |
8b886ca7 | 877 | { |
a6810074 DL |
878 | static const char echocmd[] = "echo " PING_TOKEN; |
879 | ssize_t rc; | |
880 | struct daemon *dmn = THREAD_ARG(t_wakeup); | |
881 | ||
882 | dmn->t_wakeup = NULL; | |
d62a17ae | 883 | if (((rc = write(dmn->fd, echocmd, sizeof(echocmd))) < 0) |
884 | || ((size_t)rc != sizeof(echocmd))) { | |
a6810074 DL |
885 | char why[100 + sizeof(echocmd)]; |
886 | snprintf(why, sizeof(why), | |
887 | "write '%s' returned %d instead of %u", echocmd, | |
d62a17ae | 888 | (int)rc, (u_int)sizeof(echocmd)); |
a6810074 DL |
889 | daemon_down(dmn, why); |
890 | } else { | |
891 | gettimeofday(&dmn->echo_sent, NULL); | |
66e78ae6 QY |
892 | dmn->t_wakeup = NULL; |
893 | thread_add_timer(master, wakeup_no_answer, dmn, gs.timeout, | |
894 | &dmn->t_wakeup); | |
a6810074 DL |
895 | } |
896 | return 0; | |
8b886ca7 | 897 | } |
898 | ||
a6810074 | 899 | static void sigint(void) |
8b886ca7 | 900 | { |
a6810074 DL |
901 | zlog_notice("Terminating on signal"); |
902 | systemd_send_stopping(); | |
903 | exit(0); | |
8b886ca7 | 904 | } |
905 | ||
a6810074 | 906 | static int valid_command(const char *cmd) |
8b886ca7 | 907 | { |
a6810074 | 908 | char *p; |
8b886ca7 | 909 | |
a6810074 | 910 | return ((p = strchr(cmd, '%')) != NULL) && (*(p + 1) == 's') |
d62a17ae | 911 | && !strchr(p + 1, '%'); |
8b886ca7 | 912 | } |
913 | ||
c8b40f86 | 914 | /* This is an ugly hack to circumvent problems with passing command-line |
915 | arguments that contain spaces. The fix is to use a configuration file. */ | |
a6810074 | 916 | static char *translate_blanks(const char *cmd, const char *blankstr) |
c8b40f86 | 917 | { |
a6810074 DL |
918 | char *res; |
919 | char *p; | |
920 | size_t bslen = strlen(blankstr); | |
921 | ||
922 | if (!(res = strdup(cmd))) { | |
923 | perror("strdup"); | |
924 | exit(1); | |
925 | } | |
926 | while ((p = strstr(res, blankstr)) != NULL) { | |
927 | *p = ' '; | |
928 | if (bslen != 1) | |
929 | memmove(p + 1, p + bslen, strlen(p + bslen) + 1); | |
930 | } | |
931 | return res; | |
c8b40f86 | 932 | } |
933 | ||
a6810074 | 934 | struct zebra_privs_t watchfrr_privs = { |
95c4aff2 | 935 | #ifdef VTY_GROUP |
a6810074 | 936 | .vty_group = VTY_GROUP, |
95c4aff2 DL |
937 | #endif |
938 | }; | |
939 | ||
4f04a76b DL |
940 | static struct quagga_signal_t watchfrr_signals[] = { |
941 | { | |
942 | .signal = SIGINT, | |
943 | .handler = sigint, | |
944 | }, | |
945 | { | |
946 | .signal = SIGTERM, | |
947 | .handler = sigint, | |
948 | }, | |
949 | { | |
950 | .signal = SIGCHLD, | |
951 | .handler = sigchild, | |
952 | }, | |
953 | }; | |
954 | ||
955 | FRR_DAEMON_INFO(watchfrr, WATCHFRR, | |
d62a17ae | 956 | .flags = FRR_NO_PRIVSEP | FRR_NO_TCPVTY | FRR_LIMITED_CLI |
957 | | FRR_NO_CFG_PID_DRY | FRR_NO_ZCLIENT, | |
4f04a76b | 958 | |
d62a17ae | 959 | .printhelp = printhelp, |
960 | .copyright = "Copyright 2004 Andrew J. Schorr", | |
4f04a76b | 961 | |
d62a17ae | 962 | .signals = watchfrr_signals, |
963 | .n_signals = array_size(watchfrr_signals), | |
4f04a76b | 964 | |
d62a17ae | 965 | .privs = &watchfrr_privs, ) |
4f04a76b | 966 | |
a6810074 | 967 | int main(int argc, char **argv) |
8b886ca7 | 968 | { |
a6810074 | 969 | int opt; |
64a249ad | 970 | const char *pidfile = pidfile_default; |
a6810074 DL |
971 | const char *special = "zebra"; |
972 | const char *blankstr = NULL; | |
a6810074 | 973 | |
64a249ad DL |
974 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/watchfrr.pid", |
975 | frr_vtydir); | |
976 | ||
4f04a76b DL |
977 | frr_preinit(&watchfrr_di, argc, argv); |
978 | progname = watchfrr_di.progname; | |
979 | ||
71e7975a | 980 | frr_opt_add("b:dk:l:i:p:r:S:s:t:T:", longopts, ""); |
a6810074 DL |
981 | |
982 | gs.restart.name = "all"; | |
4f04a76b | 983 | while ((opt = frr_getopt(argc, argv, NULL)) != EOF) { |
a6810074 DL |
984 | switch (opt) { |
985 | case 0: | |
986 | break; | |
a6810074 DL |
987 | case 'b': |
988 | blankstr = optarg; | |
989 | break; | |
f168b713 DL |
990 | case OPTION_DRY: |
991 | watch_only = true; | |
992 | break; | |
a6810074 DL |
993 | case 'k': |
994 | if (!valid_command(optarg)) { | |
995 | fprintf(stderr, | |
996 | "Invalid kill command, must contain '%%s': %s\n", | |
997 | optarg); | |
4f04a76b | 998 | frr_help_exit(1); |
a6810074 DL |
999 | } |
1000 | gs.stop_command = optarg; | |
1001 | break; | |
d62a17ae | 1002 | case 'l': { |
1003 | char garbage[3]; | |
1004 | if ((sscanf(optarg, "%d%1s", &gs.loglevel, garbage) | |
1005 | != 1) | |
1006 | || (gs.loglevel < LOG_EMERG)) { | |
1007 | fprintf(stderr, | |
1008 | "Invalid loglevel argument: %s\n", | |
1009 | optarg); | |
1010 | frr_help_exit(1); | |
a6810074 | 1011 | } |
d62a17ae | 1012 | } break; |
1013 | case OPTION_MINRESTART: { | |
1014 | char garbage[3]; | |
1015 | if ((sscanf(optarg, "%ld%1s", &gs.min_restart_interval, | |
1016 | garbage) | |
1017 | != 1) | |
1018 | || (gs.min_restart_interval < 0)) { | |
1019 | fprintf(stderr, | |
1020 | "Invalid min_restart_interval argument: %s\n", | |
1021 | optarg); | |
1022 | frr_help_exit(1); | |
a6810074 | 1023 | } |
d62a17ae | 1024 | } break; |
1025 | case OPTION_MAXRESTART: { | |
1026 | char garbage[3]; | |
1027 | if ((sscanf(optarg, "%ld%1s", &gs.max_restart_interval, | |
1028 | garbage) | |
1029 | != 1) | |
1030 | || (gs.max_restart_interval < 0)) { | |
1031 | fprintf(stderr, | |
1032 | "Invalid max_restart_interval argument: %s\n", | |
1033 | optarg); | |
1034 | frr_help_exit(1); | |
a6810074 | 1035 | } |
d62a17ae | 1036 | } break; |
1037 | case 'i': { | |
1038 | char garbage[3]; | |
1039 | int period; | |
1040 | if ((sscanf(optarg, "%d%1s", &period, garbage) != 1) | |
1041 | || (gs.period < 1)) { | |
1042 | fprintf(stderr, | |
1043 | "Invalid interval argument: %s\n", | |
1044 | optarg); | |
1045 | frr_help_exit(1); | |
a6810074 | 1046 | } |
d62a17ae | 1047 | gs.period = 1000 * period; |
1048 | } break; | |
a6810074 DL |
1049 | case 'p': |
1050 | pidfile = optarg; | |
1051 | break; | |
1052 | case 'r': | |
a6810074 DL |
1053 | if (!valid_command(optarg)) { |
1054 | fprintf(stderr, | |
1055 | "Invalid restart command, must contain '%%s': %s\n", | |
1056 | optarg); | |
4f04a76b | 1057 | frr_help_exit(1); |
a6810074 DL |
1058 | } |
1059 | gs.restart_command = optarg; | |
a6810074 DL |
1060 | break; |
1061 | case 's': | |
1062 | if (!valid_command(optarg)) { | |
1063 | fprintf(stderr, | |
1064 | "Invalid start command, must contain '%%s': %s\n", | |
1065 | optarg); | |
4f04a76b | 1066 | frr_help_exit(1); |
a6810074 DL |
1067 | } |
1068 | gs.start_command = optarg; | |
1069 | break; | |
1070 | case 'S': | |
1071 | gs.vtydir = optarg; | |
1072 | break; | |
d62a17ae | 1073 | case 't': { |
1074 | char garbage[3]; | |
1075 | if ((sscanf(optarg, "%ld%1s", &gs.timeout, garbage) | |
1076 | != 1) | |
1077 | || (gs.timeout < 1)) { | |
1078 | fprintf(stderr, | |
1079 | "Invalid timeout argument: %s\n", | |
1080 | optarg); | |
1081 | frr_help_exit(1); | |
a6810074 | 1082 | } |
d62a17ae | 1083 | } break; |
1084 | case 'T': { | |
1085 | char garbage[3]; | |
1086 | if ((sscanf(optarg, "%ld%1s", &gs.restart_timeout, | |
1087 | garbage) | |
1088 | != 1) | |
1089 | || (gs.restart_timeout < 1)) { | |
1090 | fprintf(stderr, | |
1091 | "Invalid restart timeout argument: %s\n", | |
1092 | optarg); | |
1093 | frr_help_exit(1); | |
a6810074 | 1094 | } |
d62a17ae | 1095 | } break; |
a6810074 DL |
1096 | default: |
1097 | fputs("Invalid option.\n", stderr); | |
4f04a76b | 1098 | frr_help_exit(1); |
a6810074 | 1099 | } |
8b886ca7 | 1100 | } |
a6810074 | 1101 | |
71e7975a DL |
1102 | if (watch_only |
1103 | && (gs.start_command || gs.stop_command || gs.restart_command)) { | |
d87ae5cc | 1104 | fputs("Options -r/-s/-k are not used when --dry is active.\n", |
a6810074 | 1105 | stderr); |
8b886ca7 | 1106 | } |
f168b713 DL |
1107 | if (!watch_only |
1108 | && (!gs.restart_command || !gs.start_command || !gs.stop_command)) { | |
1109 | fprintf(stderr, | |
1110 | "Options -s (start), -k (kill), and -r (restart) are required.\n"); | |
1111 | frr_help_exit(1); | |
8b886ca7 | 1112 | } |
8b886ca7 | 1113 | |
a6810074 DL |
1114 | if (blankstr) { |
1115 | if (gs.restart_command) | |
1116 | gs.restart_command = | |
d62a17ae | 1117 | translate_blanks(gs.restart_command, blankstr); |
a6810074 DL |
1118 | if (gs.start_command) |
1119 | gs.start_command = | |
d62a17ae | 1120 | translate_blanks(gs.start_command, blankstr); |
a6810074 DL |
1121 | if (gs.stop_command) |
1122 | gs.stop_command = | |
d62a17ae | 1123 | translate_blanks(gs.stop_command, blankstr); |
065de903 | 1124 | } |
8b886ca7 | 1125 | |
a6810074 | 1126 | gs.restart.interval = gs.min_restart_interval; |
8b886ca7 | 1127 | |
4f04a76b DL |
1128 | master = frr_init(); |
1129 | ||
dd8376fe | 1130 | zlog_set_level(ZLOG_DEST_MONITOR, ZLOG_DISABLED); |
eb05883f | 1131 | if (watchfrr_di.daemon_mode) { |
dd8376fe | 1132 | zlog_set_level(ZLOG_DEST_SYSLOG, MIN(gs.loglevel, LOG_DEBUG)); |
d62a17ae | 1133 | if (daemon(0, 0) < 0) { |
2f4f11fa | 1134 | fprintf(stderr, "Watchfrr daemon failed: %s", |
d62a17ae | 1135 | strerror(errno)); |
1136 | exit(1); | |
4f04a76b DL |
1137 | } |
1138 | } else | |
dd8376fe | 1139 | zlog_set_level(ZLOG_DEST_STDOUT, MIN(gs.loglevel, LOG_DEBUG)); |
8b886ca7 | 1140 | |
a6810074 | 1141 | watchfrr_vty_init(); |
8b886ca7 | 1142 | |
eb05883f | 1143 | frr_vty_serv(); |
8b886ca7 | 1144 | |
8b886ca7 | 1145 | { |
a6810074 DL |
1146 | int i; |
1147 | struct daemon *tail = NULL; | |
1148 | ||
1149 | for (i = optind; i < argc; i++) { | |
1150 | struct daemon *dmn; | |
1151 | ||
1152 | if (!(dmn = (struct daemon *)calloc(1, sizeof(*dmn)))) { | |
1153 | fprintf(stderr, "calloc(1,%u) failed: %s\n", | |
d62a17ae | 1154 | (u_int)sizeof(*dmn), |
a6810074 DL |
1155 | safe_strerror(errno)); |
1156 | return 1; | |
1157 | } | |
1158 | dmn->name = dmn->restart.name = argv[i]; | |
1159 | dmn->state = DAEMON_INIT; | |
1160 | gs.numdaemons++; | |
1161 | gs.numdown++; | |
1162 | dmn->fd = -1; | |
66e78ae6 | 1163 | dmn->t_wakeup = NULL; |
d62a17ae | 1164 | thread_add_timer_msec(master, wakeup_init, dmn, |
1165 | 100 + (random() % 900), | |
66e78ae6 | 1166 | &dmn->t_wakeup); |
a6810074 DL |
1167 | dmn->restart.interval = gs.min_restart_interval; |
1168 | if (tail) | |
1169 | tail->next = dmn; | |
1170 | else | |
1171 | gs.daemons = dmn; | |
1172 | tail = dmn; | |
1173 | ||
f168b713 | 1174 | if (!strcmp(dmn->name, special)) |
a6810074 DL |
1175 | gs.special = dmn; |
1176 | } | |
1177 | } | |
1178 | if (!gs.daemons) { | |
1179 | fputs("Must specify one or more daemons to monitor.\n", stderr); | |
4f04a76b | 1180 | frr_help_exit(1); |
a6810074 | 1181 | } |
f168b713 DL |
1182 | if (!watch_only && !gs.special) { |
1183 | fprintf(stderr, "\"%s\" daemon must be in daemon list\n", | |
1184 | special); | |
4f04a76b | 1185 | frr_help_exit(1); |
8b886ca7 | 1186 | } |
8b886ca7 | 1187 | |
a6810074 DL |
1188 | /* Make sure we're not already running. */ |
1189 | pid_output(pidfile); | |
1190 | ||
1191 | /* Announce which daemons are being monitored. */ | |
1192 | { | |
1193 | struct daemon *dmn; | |
1194 | size_t len = 0; | |
1195 | ||
1196 | for (dmn = gs.daemons; dmn; dmn = dmn->next) | |
1197 | len += strlen(dmn->name) + 1; | |
1198 | ||
1199 | { | |
1200 | char buf[len + 1]; | |
1201 | char *p = buf; | |
1202 | ||
1203 | for (dmn = gs.daemons; dmn; dmn = dmn->next) { | |
1204 | if (p != buf) | |
1205 | *p++ = ' '; | |
1206 | strcpy(p, dmn->name); | |
1207 | p += strlen(p); | |
1208 | } | |
f168b713 DL |
1209 | zlog_notice("%s %s watching [%s]%s", progname, |
1210 | FRR_VERSION, buf, | |
1211 | watch_only ? ", monitor mode" : ""); | |
a6810074 DL |
1212 | } |
1213 | } | |
8b886ca7 | 1214 | |
a6810074 DL |
1215 | { |
1216 | struct thread thread; | |
1217 | ||
1218 | while (thread_fetch(master, &thread)) | |
1219 | thread_call(&thread); | |
1220 | } | |
8b886ca7 | 1221 | |
a6810074 DL |
1222 | systemd_send_stopping(); |
1223 | /* Not reached. */ | |
1224 | return 0; | |
8b886ca7 | 1225 | } |