1 /*
2 * Copyright (C) 2013 Lars Marowsky-Bree <lmb@suse.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <crm/common/util.h>
20 #include "sbd.h"
21 #define LOCKSTRLEN 11
22
23 static struct servants_list_item *servants_leader = NULL;
24
25 int disk_priority = 1;
26 int check_pcmk = 1;
27 int check_cluster = 1;
28 int has_check_pcmk_env = false;
29 int disk_count = 0;
30 int servant_count = 0;
31 int servant_restart_interval = 5;
32 int servant_restart_count = 1;
33 int start_mode = 0;
34 char* pidfile = NULL;
35 bool do_flush = true;
36 char timeout_sysrq_char = 'b';
37 bool move_to_root_cgroup = true;
38 bool enforce_moving_to_root_cgroup = false;
39 bool sync_resource_startup = false;
40
41 int parse_device_line(const char *line);
42
43 static int
44 sanitize_numeric_option_value(const char *value)
45 {
46 char *end = NULL;
47 long int result = -1;
48
49 if (value == NULL) {
50 return -1;
51 }
52
53 errno = 0;
54
55 result = strtol(value, &end, 10);
56 if (result <= INT_MIN || result >= INT_MAX || errno != 0) {
57 result = -1;
58 } else if (*end != '\0') {
59 result = -1;
60 }
61
62 return (int)result;
63 }
64
65 static const char *
66 sanitize_option_value(const char *value)
67 {
68 size_t max = 0;
69 size_t lpc = 0;
70
71 if (value == NULL) {
72 return NULL;
73 }
74
75 max = strlen(value);
76
77 for (lpc = 0; lpc < max; lpc++) {
78 if (!isspace(value[lpc])) {
79 break;
80 }
81 }
82
83 return (strlen(value + lpc) > 0 ? (value + lpc) : NULL);
84 }
85
86 static const char *
87 get_env_option(const char *option)
88 {
89 const char *value = getenv(option);
90
91 return sanitize_option_value(value);
92 }
93
94 static int
95 recruit_servant(const char *devname, pid_t pid)
96 {
97 struct servants_list_item *s = servants_leader;
98 struct servants_list_item *newbie;
99
100 if (lookup_servant_by_dev(devname)) {
101 cl_log(LOG_DEBUG, "Servant %s already exists", devname);
102 return 0;
103 }
104
105 newbie = malloc(sizeof(*newbie));
106 if (newbie) {
107 memset(newbie, 0, sizeof(*newbie));
108 newbie->devname = strdup(devname);
109 newbie->pid = pid;
110 newbie->first_start = 1;
111 }
112 if (!newbie || !newbie->devname) {
113 fprintf(stderr, "heap allocation failed in recruit_servant.\n");
114 exit(1);
115 }
116
117 /* some sanity-check on our newbie */
118 if (sbd_is_disk(newbie)) {
119 cl_log(LOG_INFO, "Monitoring %s", devname);
120 disk_count++;
121 } else if (sbd_is_pcmk(newbie) || sbd_is_cluster(newbie)) {
122 /* alive just after pcmk and cluster servants have shown up */
123 newbie->outdated = 1;
124 } else {
125 /* toss our newbie */
126 cl_log(LOG_ERR, "Refusing to recruit unrecognized servant %s", devname);
127 free((void *) newbie->devname);
128 free(newbie);
129 return -1;
130 }
131
132 if (!s) {
133 servants_leader = newbie;
134 } else {
135 while (s->next)
136 s = s->next;
137 s->next = newbie;
138 }
139
140 servant_count++;
141
142 return 0;
143 }
144
145 int assign_servant(const char* devname, functionp_t functionp, int mode, const void* argp)
146 {
147 pid_t pid = 0;
148 int rc = 0;
149
150 pid = fork();
151 if (pid == 0) { /* child */
152 maximize_priority();
153 sbd_set_format_string(QB_LOG_SYSLOG, devname);
154 rc = (*functionp)(devname, mode, argp);
155 if (rc == -1)
156 exit(1);
157 else
158 exit(0);
159 } else if (pid != -1) { /* parent */
160 return pid;
161 } else {
162 cl_log(LOG_ERR,"Failed to fork servant");
163 exit(1);
164 }
165 }
166
167 struct servants_list_item *lookup_servant_by_dev(const char *devname)
168 {
169 struct servants_list_item *s;
170
171 for (s = servants_leader; s; s = s->next) {
172 if (strcasecmp(s->devname, devname) == 0)
173 break;
174 }
175 return s;
176 }
177
178 struct servants_list_item *lookup_servant_by_pid(pid_t pid)
179 {
180 struct servants_list_item *s;
181
182 for (s = servants_leader; s; s = s->next) {
183 if (s->pid == pid)
184 break;
185 }
186 return s;
187 }
188
189 int check_all_dead(void)
190 {
191 struct servants_list_item *s;
192 int r = 0;
193
194 for (s = servants_leader; s; s = s->next) {
195 if (s->pid != 0) {
196 r = sigqueue_zero(s->pid, 0);
197 if (r == -1 && errno == ESRCH)
198 continue;
199 return 0;
200 }
201 }
202 return 1;
203 }
204
205 void servant_start(struct servants_list_item *s)
206 {
207 int r = 0;
208
209 if (s->pid != 0) {
210 r = sigqueue_zero(s->pid, 0);
211 if ((r != -1 || errno != ESRCH))
212 return;
213 }
214 s->restarts++;
215 if (sbd_is_disk(s)) {
216 #if SUPPORT_SHARED_DISK
217 DBGLOG(LOG_INFO, "Starting servant for device %s", s->devname);
218 s->pid = assign_servant(s->devname, servant_md, start_mode, s);
219 #else
220 cl_log(LOG_ERR, "Shared disk functionality not supported");
221 return;
222 #endif
223 } else if(sbd_is_pcmk(s)) {
224 DBGLOG(LOG_INFO, "Starting Pacemaker servant");
225 s->pid = assign_servant(s->devname, servant_pcmk, start_mode, NULL);
226
227 } else if(sbd_is_cluster(s)) {
228 DBGLOG(LOG_INFO, "Starting Cluster servant");
229 s->pid = assign_servant(s->devname, servant_cluster, start_mode, NULL);
230
231 } else {
232 cl_log(LOG_ERR, "Unrecognized servant: %s", s->devname);
233 }
234
235 clock_gettime(CLOCK_MONOTONIC, &s->t_started);
236 return;
237 }
238
239 void servants_start(void)
240 {
241 struct servants_list_item *s;
242
243 for (s = servants_leader; s; s = s->next) {
244 s->restarts = 0;
245 servant_start(s);
246 }
247 }
248
249 void servants_kill(void)
250 {
251 struct servants_list_item *s;
252
253 for (s = servants_leader; s; s = s->next) {
254 if (s->pid != 0) {
255 sigqueue_zero(s->pid, SIGKILL);
256 }
257 }
258 }
259
260 static inline void cleanup_servant_by_pid(pid_t pid)
261 {
262 struct servants_list_item* s;
263
264 s = lookup_servant_by_pid(pid);
265 if (s) {
266 cl_log(LOG_WARNING, "Servant for %s (pid: %i) has terminated",
267 s->devname, s->pid);
268 s->pid = 0;
269 } else {
270 /* This most likely is a stray signal from somewhere, or
271 * a SIGCHLD for a process that has previously
272 * explicitly disconnected. */
273 DBGLOG(LOG_INFO, "cleanup_servant: Nothing known about pid %i",
274 pid);
275 }
276 }
277
278 int inquisitor_decouple(void)
279 {
280 pid_t ppid = getppid();
281
282 /* During start-up, we only arm the watchdog once we've got
283 * quorum at least once. */
284 if (watchdog_use) {
285 if (watchdog_init() < 0) {
286 return -1;
287 }
288 }
289
290 if (ppid > 1) {
291 sigqueue_zero(ppid, SIG_LIVENESS);
292 }
293 return 0;
294 }
295
296 static int sbd_lock_running(long pid)
297 {
298 int rc = 0;
299 long mypid;
300 int running = 0;
301 char proc_path[PATH_MAX], exe_path[PATH_MAX], myexe_path[PATH_MAX];
302
303 /* check if pid is running */
304 if (kill(pid, 0) < 0 && errno == ESRCH) {
305 goto bail;
306 }
307
308 #ifndef HAVE_PROC_PID
309 return 1;
310 #endif
311
312 /* check to make sure pid hasn't been reused by another process */
313 snprintf(proc_path, sizeof(proc_path), "/proc/%lu/exe", pid);
314 rc = readlink(proc_path, exe_path, PATH_MAX-1);
315 if(rc < 0) {
316 cl_perror("Could not read from %s", proc_path);
317 goto bail;
318 }
319 exe_path[rc] = 0;
320 mypid = (unsigned long) getpid();
321 snprintf(proc_path, sizeof(proc_path), "/proc/%lu/exe", mypid);
322 rc = readlink(proc_path, myexe_path, PATH_MAX-1);
323 if(rc < 0) {
324 cl_perror("Could not read from %s", proc_path);
325 goto bail;
326 }
327 myexe_path[rc] = 0;
328
329 if(strcmp(exe_path, myexe_path) == 0) {
330 running = 1;
331 }
332
333 bail:
334 return running;
335 }
336
337 static int
338 sbd_lock_pidfile(const char *filename)
339 {
340 char lf_name[256], tf_name[256], buf[LOCKSTRLEN+1];
341 int fd;
342 long pid, mypid;
343 int rc;
344 struct stat sbuf;
345
|
(1) Event path: |
Condition "filename == NULL", taking false branch. |
346 if (filename == NULL) {
347 errno = EFAULT;
348 return -1;
349 }
350
351 mypid = (unsigned long) getpid();
352 snprintf(lf_name, sizeof(lf_name), "%s",filename);
353 snprintf(tf_name, sizeof(tf_name), "%s.%lu",
354 filename, mypid);
355
|
(2) Event path: |
Condition "(fd = open(lf_name, 0)) >= 0", taking true branch. |
356 if ((fd = open(lf_name, O_RDONLY)) >= 0) {
|
(3) Event path: |
Condition "fstat(fd, &sbuf) >= 0", taking true branch. |
|
(4) Event path: |
Condition "sbuf.st_size < 11", taking true branch. |
357 if (fstat(fd, &sbuf) >= 0 && sbuf.st_size < LOCKSTRLEN) {
358 sleep(1); /* if someone was about to create one,
359 * give'm a sec to do so
360 * Though if they follow our protocol,
361 * this won't happen. They should really
362 * put the pid in, then link, not the
363 * other way around.
364 */
365 }
|
(5) Event path: |
Condition "read(fd, buf, 12UL /* sizeof (buf) */) < 1", taking true branch. |
366 if (read(fd, buf, sizeof(buf)) < 1) {
367 /* lockfile empty -> rm it and go on */;
|
(6) Event path: |
Falling through to end of if statement. |
368 } else {
369 if (sscanf(buf, "%ld", &pid) < 1) {
370 /* lockfile screwed up -> rm it and go on */
371 } else {
372 if (pid > 1 && (getpid() != pid)
373 && sbd_lock_running(pid)) {
374 /* is locked by existing process
375 * -> give up */
376 close(fd);
377 return -1;
378 } else {
379 /* stale lockfile -> rm it and go on */
380 }
381 }
382 }
383 unlink(lf_name);
384 close(fd);
385 }
|
(7) Event path: |
Condition "(fd = open(tf_name, 193 /* (0x40 | 1) | 0x80 */, 420)) < 0", taking false branch. |
386 if ((fd = open(tf_name, O_CREAT | O_WRONLY | O_EXCL, 0644)) < 0) {
387 /* Hmmh, why did we fail? Anyway, nothing we can do about it */
388 return -3;
389 }
390
391 /* Slight overkill with the %*d format ;-) */
392 snprintf(buf, sizeof(buf), "%*lu\n", LOCKSTRLEN-1, mypid);
393
|
(8) Event path: |
Condition "write(fd, buf, 11) != 11", taking false branch. |
394 if (write(fd, buf, LOCKSTRLEN) != LOCKSTRLEN) {
395 /* Again, nothing we can do about this */
396 rc = -3;
397 close(fd);
398 goto out;
399 }
400 close(fd);
401
|
(9) Event path: |
Switch case value "0". |
402 switch (link(tf_name, lf_name)) {
403 case 0:
|
CID (unavailable; MK=3c3576ccc8602f1de0283b799a040306) (#1 of 1): Time of check time of use (TOCTOU): |
|
(10) Event fs_check_call: |
Calling function "stat" to perform check on "tf_name". |
|
(11) Event path: |
Condition "stat(tf_name, &sbuf) < 0", taking false branch. |
| Also see events: |
[toctou] |
404 if (stat(tf_name, &sbuf) < 0) {
405 /* something weird happened - Loss of tf_name
406 * makes unlink via out pointless.
407 * Return directly to avoid Coverity falsely
408 * pairing this stat with the unlink as TOCTOU.
409 */
410 return -3;
411 }
|
(12) Event path: |
Condition "sbuf.st_nlink < 2", taking true branch. |
412 if (sbuf.st_nlink < 2) {
413 /* somehow, it didn't get through - NFS trouble? */
414 rc = -2;
|
(13) Event path: |
Breaking from switch. |
415 break;
416 }
417 rc = 0;
418 break;
419 case EEXIST:
420 rc = -1;
421 break;
422 default:
423 rc = -3;
424 }
425 out:
|
(14) Event toctou: |
Calling function "unlink" that uses "tf_name" after a check function. This can cause a time-of-check, time-of-use race condition. |
| Also see events: |
[fs_check_call] |
426 unlink(tf_name);
427 return rc;
428 }
429
430
431 /*
432 * Unlock a file (remove its lockfile)
433 * do we need to check, if its (still) ours? No, IMHO, if someone else
434 * locked our line, it's his fault -tho
435 * returns 0 on success
436 * <0 if some failure occured
437 */
438
439 static int
440 sbd_unlock_pidfile(const char *filename)
441 {
442 char lf_name[256];
443
444 if (filename == NULL) {
445 errno = EFAULT;
446 return -1;
447 }
448
449 snprintf(lf_name, sizeof(lf_name), "%s", filename);
450
451 return unlink(lf_name);
452 }
453
454 int cluster_alive(bool all)
455 {
456 int alive = 1;
457 struct servants_list_item* s;
458
459 if(servant_count == disk_count) {
460 return 0;
461 }
462
463 for (s = servants_leader; s; s = s->next) {
464 if (sbd_is_cluster(s) || sbd_is_pcmk(s)) {
465 if(s->outdated) {
466 alive = 0;
467 } else if(all == false) {
468 return 1;
469 }
470 }
471 }
472
473 return alive;
474 }
475
476 int quorum_read(int good_servants)
477 {
478 if (disk_count > 2)
479 return (good_servants > disk_count/2);
480 else
481 return (good_servants > 0);
482 }
483
484 void inquisitor_child(void)
485 {
486 int sig, pid;
487 sigset_t procmask;
488 siginfo_t sinfo;
489 int status;
490 struct timespec timeout;
491 int exiting = 0;
492 int decoupled = 0;
493 int cluster_appeared = 0;
494 int pcmk_override = 0;
495 int latency;
496 struct timespec t_last_tickle, t_now;
497 struct servants_list_item* s;
498
499 if (debug_mode) {
500 cl_log(LOG_ERR, "DEBUG MODE %d IS ACTIVE - DO NOT RUN IN PRODUCTION!", debug_mode);
501 }
502
503 set_proc_title("sbd: inquisitor");
504
505 if (pidfile) {
506 if (sbd_lock_pidfile(pidfile) < 0) {
507 exit(1);
508 }
509 }
510
511 sigemptyset(&procmask);
512 sigaddset(&procmask, SIGCHLD);
513 sigaddset(&procmask, SIGTERM);
514 sigaddset(&procmask, SIG_LIVENESS);
515 sigaddset(&procmask, SIG_EXITREQ);
516 sigaddset(&procmask, SIG_TEST);
517 sigaddset(&procmask, SIG_PCMK_UNHEALTHY);
518 sigaddset(&procmask, SIG_RESTART);
519 sigaddset(&procmask, SIGUSR1);
520 sigaddset(&procmask, SIGUSR2);
521 sigprocmask(SIG_BLOCK, &procmask, NULL);
522
523 servants_start();
524
525 timeout.tv_sec = timeout_loop;
526 timeout.tv_nsec = 0;
527 clock_gettime(CLOCK_MONOTONIC, &t_last_tickle);
528
529 while (1) {
530 bool tickle = 0;
531 bool can_detach = 0;
532 int good_servants = 0;
533
534 sig = sigtimedwait(&procmask, &sinfo, &timeout);
535
536 clock_gettime(CLOCK_MONOTONIC, &t_now);
537
538 if (sig == SIG_EXITREQ || sig == SIGTERM) {
539 servants_kill();
540 watchdog_close(true);
541 exiting = 1;
542 } else if (sig == SIGCHLD) {
543 while ((pid = waitpid(-1, &status, WNOHANG))) {
544 if (pid == -1 && errno == ECHILD) {
545 break;
546 } else {
547 s = lookup_servant_by_pid(pid);
548 if (sbd_is_disk(s)) {
549 if (WIFEXITED(status)) {
550 switch(WEXITSTATUS(status)) {
551 case EXIT_MD_SERVANT_IO_FAIL:
552 DBGLOG(LOG_INFO, "Servant for %s requests to be disowned",
553 s->devname);
554 break;
555 case EXIT_MD_SERVANT_REQUEST_RESET:
556 cl_log(LOG_WARNING, "%s requested a reset", s->devname);
557 do_reset();
558 break;
559 case EXIT_MD_SERVANT_REQUEST_SHUTOFF:
560 cl_log(LOG_WARNING, "%s requested a shutoff", s->devname);
561 do_off();
562 break;
563 case EXIT_MD_SERVANT_REQUEST_CRASHDUMP:
564 cl_log(LOG_WARNING, "%s requested a crashdump", s->devname);
565 do_crashdump();
566 break;
567 default:
568 break;
569 }
570 }
571 } else if (sbd_is_pcmk(s)) {
572 if (WIFEXITED(status)) {
573 switch(WEXITSTATUS(status)) {
574 case EXIT_PCMK_SERVANT_GRACEFUL_SHUTDOWN:
575 DBGLOG(LOG_INFO, "PCMK-Servant has exited gracefully");
576 /* revert to state prior to pacemaker-detection */
577 s->restarts = 0;
578 s->restart_blocked = 0;
579 cluster_appeared = 0;
580 s->outdated = 1;
581 s->t_last.tv_sec = 0;
582 break;
583 default:
584 break;
585 }
586 }
587 }
588 cleanup_servant_by_pid(pid);
589 }
590 }
591 } else if (sig == SIG_PCMK_UNHEALTHY) {
592 s = lookup_servant_by_pid(sinfo.si_pid);
593 if (sbd_is_cluster(s) || sbd_is_pcmk(s)) {
594 if (s->outdated == 0) {
595 cl_log(LOG_WARNING, "%s health check: UNHEALTHY", s->devname);
596 }
597 s->t_last.tv_sec = 1;
598 } else {
599 cl_log(LOG_WARNING, "Ignoring SIG_PCMK_UNHEALTHY from unknown source");
600 }
601 } else if (sig == SIG_LIVENESS) {
602 s = lookup_servant_by_pid(sinfo.si_pid);
603 if (s) {
604 s->first_start = 0;
605 clock_gettime(CLOCK_MONOTONIC, &s->t_last);
606 }
607
608 } else if (sig == SIG_TEST) {
609 } else if (sig == SIGUSR1) {
610 if (exiting)
611 continue;
612 servants_start();
613 }
614
615 if (exiting) {
616 if (check_all_dead()) {
617 if (pidfile) {
618 sbd_unlock_pidfile(pidfile);
619 }
620 exit(0);
621 } else
622 continue;
623 }
624
625 good_servants = 0;
626 for (s = servants_leader; s; s = s->next) {
627 int age = seconds_diff_timespec(&t_now, &(s->t_last));
628
629 if (!s->t_last.tv_sec)
630 continue;
631
632 if (age < timeout_io+timeout_loop) {
633 if (sbd_is_disk(s)) {
634 good_servants++;
635 }
636 if (s->outdated) {
637 cl_log(LOG_NOTICE, "Servant %s is healthy (age: %d)", s->devname, age);
638 }
639 s->outdated = 0;
640
641 } else if (!s->outdated) {
642 if (!s->restart_blocked) {
643 cl_log(LOG_WARNING, "Servant %s is outdated (age: %d)", s->devname, age);
644 }
645 s->outdated = 1;
646 }
647 }
648
649 if(disk_count == 0) {
650 /* NO disks, everything is up to the cluster */
651
652 if(cluster_alive(true)) {
653 /* We LIVE! */
654 if(cluster_appeared == false) {
655 cl_log(LOG_INFO, "Active cluster detected");
656 }
657 tickle = 1;
658 can_detach = 1;
659 cluster_appeared = 1;
660
661 } else if(cluster_alive(false)) {
662 if(!decoupled) {
663 /* On the way up, detach and arm the watchdog */
664 cl_log(LOG_INFO, "Partial cluster detected, detaching");
665 }
666
667 can_detach = 1;
668 tickle = !cluster_appeared;
669
670 } else if(!decoupled) {
671 /* Stay alive until the cluster comes up */
672 tickle = !cluster_appeared;
673 }
674
675 } else if(disk_priority == 1 || servant_count == disk_count) {
676 if (quorum_read(good_servants)) {
677 /* There are disks and we're connected to the majority of them */
678 tickle = 1;
679 can_detach = 1;
680 pcmk_override = 0;
681
682 } else if (servant_count > disk_count && cluster_alive(true)) {
683 tickle = 1;
684
685 if(!pcmk_override) {
686 cl_log(LOG_WARNING, "Majority of devices lost - surviving on pacemaker");
687 pcmk_override = 1; /* Only log this message once */
688 }
689 }
690
691 } else if(cluster_alive(true) && quorum_read(good_servants)) {
692 /* Both disk and cluster servants are healthy */
693 tickle = 1;
694 can_detach = 1;
695 cluster_appeared = 1;
696
697 } else if(quorum_read(good_servants)) {
698 /* The cluster takes priority but only once
699 * connected for the first time.
700 *
701 * Until then, we tickle based on disk quorum.
702 */
703 can_detach = 1;
704 tickle = !cluster_appeared;
705 }
706
707 /* cl_log(LOG_DEBUG, "Tickle: q=%d, g=%d, p=%d, s=%d", */
708 /* quorum_read(good_servants), good_servants, tickle, disk_count); */
709
710 if(tickle) {
711 watchdog_tickle();
712 clock_gettime(CLOCK_MONOTONIC, &t_last_tickle);
713 }
714
715 if (!decoupled && can_detach) {
716 /* We only do this at the point either the disk or
717 * cluster servants become healthy
718 */
719 cl_log(LOG_DEBUG, "Decoupling");
720 if (inquisitor_decouple() < 0) {
721 servants_kill();
722 exiting = 1;
723 continue;
724 } else {
725 decoupled = 1;
726 }
727 }
728
729 /* Note that this can actually be negative, since we set
730 * last_tickle after we set now. */
731 latency = seconds_diff_timespec(&t_now, &t_last_tickle);
732 if (timeout_watchdog && (latency > timeout_watchdog)) {
733 if (!decoupled) {
734 /* We're still being watched by our
735 * parent. We don't fence, but exit. */
736 cl_log(LOG_ERR, "SBD: Not enough votes to proceed. Aborting start-up.");
737 servants_kill();
738 exiting = 1;
739 continue;
740 }
741 if (debug_mode < 2) {
742 /* At level 2 or above, we do nothing, but expect
743 * things to eventually return to
744 * normal. */
745 do_timeout_action();
746 } else {
747 cl_log(LOG_ERR, "SBD: DEBUG MODE: Would have fenced due to timeout!");
748 }
749 }
750
751 if (timeout_watchdog_warn && (latency > timeout_watchdog_warn)) {
752 cl_log(LOG_WARNING,
753 "Latency: No liveness for %ds exceeds watchdog warning timeout of %ds (healthy servants: %d)",
754 latency, timeout_watchdog_warn, good_servants);
755
756 if (debug_mode && watchdog_use) {
757 /* In debug mode, trigger a reset before the watchdog can panic the machine */
758 do_timeout_action();
759 }
760 }
761
762 for (s = servants_leader; s; s = s->next) {
763 int age = seconds_diff_timespec(&t_now, &(s->t_started));
764
765 if (age > servant_restart_interval) {
766 s->restarts = 0;
767 s->restart_blocked = 0;
768 }
769
770 if (servant_restart_count
771 && (s->restarts >= servant_restart_count)
772 && !s->restart_blocked) {
773 if (servant_restart_count > 1) {
774 cl_log(LOG_WARNING, "Max retry count (%d) reached: not restarting servant for %s",
775 (int)servant_restart_count, s->devname);
776 }
777 s->restart_blocked = 1;
778 }
779
780 if (!s->restart_blocked) {
781 servant_start(s);
782 }
783 }
784 }
785 }
786
787 int inquisitor(void)
788 {
789 int sig, pid, inquisitor_pid;
790 int status;
791 sigset_t procmask;
792 siginfo_t sinfo;
793
794 /* Where's the best place for sysrq init ?*/
795 sysrq_init();
796
797 sigemptyset(&procmask);
798 sigaddset(&procmask, SIGCHLD);
799 sigaddset(&procmask, SIG_LIVENESS);
800 sigprocmask(SIG_BLOCK, &procmask, NULL);
801
802 inquisitor_pid = make_daemon();
803 if (inquisitor_pid == 0) {
804 inquisitor_child();
805 }
806
807 /* We're the parent. Wait for a happy signal from our child
808 * before we proceed - we either get "SIG_LIVENESS" when the
809 * inquisitor has completed the first successful round, or
810 * ECHLD when it exits with an error. */
811
812 while (1) {
813 sig = sigwaitinfo(&procmask, &sinfo);
814 if (sig == SIGCHLD) {
815 while ((pid = waitpid(-1, &status, WNOHANG))) {
816 if (pid == -1 && errno == ECHILD) {
817 break;
818 }
819 /* We got here because the inquisitor
820 * did not succeed. */
821 return -1;
822 }
823 } else if (sig == SIG_LIVENESS) {
824 /* Inquisitor started up properly. */
825 return 0;
826 } else {
827 fprintf(stderr, "Nobody expected the spanish inquisition!\n");
828 continue;
829 }
830 }
831 /* not reached */
832 return -1;
833 }
834
835
836 int
837 parse_device_line(const char *line)
838 {
839 size_t lpc = 0;
840 size_t last = 0;
841 size_t max = 0;
842 int found = 0;
843 bool skip_space = true;
844 int space_run = 0;
845
846 if (!line) {
847 return 0;
848 }
849
850 max = strlen(line);
851
852 cl_log(LOG_DEBUG, "Processing %d bytes: [%s]", (int) max, line);
853
854 for (lpc = 0; lpc <= max; lpc++) {
855 if (isspace(line[lpc])) {
856 if (skip_space) {
857 last = lpc + 1;
858 } else {
859 space_run++;
860 }
861 continue;
862 }
863 skip_space = false;
864 if (line[lpc] == ';' || line[lpc] == 0) {
865 int rc = 0;
866 char *entry = calloc(1, 1 + lpc - last);
867
868 if (entry) {
869 rc = sscanf(line + last, "%[^;]", entry);
870 } else {
871 fprintf(stderr, "Heap allocation failed parsing device-line.\n");
872 exit(1);
873 }
874
875 if (rc != 1) {
876 cl_log(LOG_WARNING, "Could not parse: '%s'", line + last);
877 } else {
878 entry[strlen(entry)-space_run] = '\0';
879 cl_log(LOG_DEBUG, "Adding '%s'", entry);
880 if (recruit_servant(entry, 0) != 0) {
881 free(entry);
882 // sbd should refuse to start if any of the configured device names is invalid.
883 return -1;
884 }
885 found++;
886 }
887
888 free(entry);
889 skip_space = true;
890 last = lpc + 1;
891 }
892 space_run = 0;
893 }
894 return found;
895 }
896
897 #define SBD_SOURCE_FILES "sbd-cluster.c,sbd-common.c,sbd-inquisitor.c,sbd-md.c,sbd-pacemaker.c,sbd-watchdog.c,setproctitle.c"
898
899 static void
900 sbd_log_filter_ctl(const char *files, uint8_t priority)
901 {
902 if (files == NULL) {
903 files = SBD_SOURCE_FILES;
904 }
905
906 qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, files, priority);
907 qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, files, priority);
908 }
909
910 int
911 arg_enabled(int arg_count)
912 {
913 return arg_count % 2;
914 }
915
916 int main(int argc, char **argv, char **envp)
917 {
918 int exit_status = 0;
919 int c;
920 int W_count = 0;
921 int c_count = 0;
922 int P_count = 0;
923 int qb_facility;
924 const char *value = NULL;
925 bool delay_start = false;
926 long delay = 0;
927 char *timeout_action = NULL;
928
929 if ((cmdname = strrchr(argv[0], '/')) == NULL) {
930 cmdname = argv[0];
931 } else {
932 ++cmdname;
933 }
934
935 watchdogdev = strdup("/dev/watchdog");
936 watchdogdev_is_default = true;
937 qb_facility = qb_log_facility2int("daemon");
938 qb_log_init(cmdname, qb_facility, LOG_WARNING);
939 sbd_set_format_string(QB_LOG_SYSLOG, "sbd");
940
941 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
942 qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
943 sbd_log_filter_ctl(NULL, LOG_NOTICE);
944
945 sbd_get_uname();
946
947 value = get_env_option("SBD_PACEMAKER");
948 if(value) {
949 check_pcmk = crm_is_true(value);
950 check_cluster = crm_is_true(value);
951
952 has_check_pcmk_env = true;
953 }
954 cl_log(LOG_INFO, "SBD_PACEMAKER set to: %d (%s)", (int)check_pcmk, value?value:"default");
955
956 value = get_env_option("SBD_STARTMODE");
957 if(value == NULL) {
958 } else if(strcmp(value, "clean") == 0) {
959 start_mode = 1;
960 } else if(strcmp(value, "always") == 0) {
961 start_mode = 0;
962 }
963 cl_log(LOG_INFO, "Start mode set to: %d (%s)", (int)start_mode, value?value:"default");
964
965 value = get_env_option("SBD_WATCHDOG_DEV");
966 if(value) {
967 free(watchdogdev);
968 watchdogdev = strdup(value);
969 watchdogdev_is_default = false;
970 }
971
972 /* SBD_WATCHDOG has been dropped from sbd.sysconfig example.
973 * This is for backward compatibility. */
974 value = get_env_option("SBD_WATCHDOG");
975 if(value) {
976 watchdog_use = crm_is_true(value);
977 }
978
979 value = get_env_option("SBD_WATCHDOG_TIMEOUT");
980 if(value) {
981 timeout_watchdog = crm_get_msec(value) / 1000;
982 }
983
984 value = get_env_option("SBD_PIDFILE");
985 if(value) {
986 pidfile = strdup(value);
987 cl_log(LOG_INFO, "pidfile set to %s", pidfile);
988 }
989
990 value = get_env_option("SBD_DELAY_START");
991 if(value) {
992 if (crm_str_to_boolean(value, (int *) &delay_start) != 1) {
993 delay = crm_get_msec(value) / 1000;
994 if (delay > 0) {
995 delay_start = true;
996 }
997 }
998 }
999
1000 value = get_env_option("SBD_TIMEOUT_ACTION");
1001 if(value) {
1002 timeout_action = strdup(value);
1003 }
1004
1005 value = get_env_option("SBD_MOVE_TO_ROOT_CGROUP");
1006 if(value) {
1007 move_to_root_cgroup = crm_is_true(value);
1008
1009 if (move_to_root_cgroup) {
1010 enforce_moving_to_root_cgroup = true;
1011 } else {
1012 if (strcmp(value, "auto") == 0) {
1013 move_to_root_cgroup = true;
1014 }
1015 }
1016 }
1017
1018 while ((c = getopt(argc, argv, "czC:DPRTWZhvw:d:n:p:1:2:3:4:5:t:I:F:S:s:r:")) != -1) {
1019 int sanitized_num_optarg = 0;
1020 /* Call it before checking optarg for NULL to make coverity happy */
1021 const char *sanitized_optarg = sanitize_option_value(optarg);
1022
1023 if (optarg && ((sanitized_optarg == NULL) ||
1024 (strchr("SsC12345tIF", c) &&
1025 (sanitized_num_optarg = sanitize_numeric_option_value(sanitized_optarg)) < 0))) {
1026 fprintf(stderr, "Invalid value \"%s\" for option -%c\n", optarg, c);
1027 exit_status = -2;
1028 goto out;
1029 }
1030
1031 switch (c) {
1032 case 'D':
1033 break;
1034 case 'Z':
1035 debug_mode++;
1036 cl_log(LOG_INFO, "Debug mode now at level %d", (int)debug_mode);
1037 break;
1038 case 'R':
1039 skip_rt = 1;
1040 cl_log(LOG_INFO, "Realtime mode deactivated.");
1041 break;
1042 case 'S':
1043 start_mode = sanitized_num_optarg;
1044 cl_log(LOG_INFO, "Start mode set to: %d", (int)start_mode);
1045 break;
1046 case 's':
1047 timeout_startup = sanitized_num_optarg;
1048 cl_log(LOG_INFO, "Start timeout set to: %d", (int)timeout_startup);
1049 break;
1050 case 'v':
1051 debug++;
1052 if(debug == 1) {
1053 sbd_log_filter_ctl(NULL, LOG_INFO);
1054 cl_log(LOG_INFO, "Verbose mode enabled.");
1055
1056 } else if(debug == 2) {
1057 sbd_log_filter_ctl(NULL, LOG_DEBUG);
1058 cl_log(LOG_INFO, "Debug mode enabled.");
1059
1060 } else if(debug == 3) {
1061 /* Go nuts, turn on pacemaker's logging too */
1062 sbd_log_filter_ctl("*", LOG_DEBUG);
1063 cl_log(LOG_INFO, "Debug library mode enabled.");
1064 }
1065 break;
1066 case 'T':
1067 watchdog_set_timeout = 0;
1068 cl_log(LOG_INFO, "Setting watchdog timeout disabled; using defaults.");
1069 break;
1070 case 'W':
1071 W_count++;
1072 break;
1073 case 'w':
1074 free(watchdogdev);
1075 watchdogdev = strdup(sanitized_optarg);
1076 watchdogdev_is_default = false;
1077 cl_log(LOG_NOTICE, "Using watchdog device '%s'", watchdogdev);
1078 break;
1079 case 'd':
1080 #if SUPPORT_SHARED_DISK
1081 if (recruit_servant(sanitized_optarg, 0) != 0) {
1082 fprintf(stderr, "Invalid device: %s\n", optarg);
1083 exit_status = -1;
1084 goto out;
1085 }
1086 #else
1087 fprintf(stderr, "Shared disk functionality not supported\n");
1088 exit_status = -2;
1089 goto out;
1090 #endif
1091 break;
1092 case 'c':
1093 c_count++;
1094 break;
1095 case 'P':
1096 P_count++;
1097 break;
1098 case 'z':
1099 disk_priority = 0;
1100 break;
1101 case 'n':
1102 local_uname = strdup(sanitized_optarg);
1103 cl_log(LOG_INFO, "Overriding local hostname to %s", local_uname);
1104 break;
1105 case 'p':
1106 pidfile = strdup(sanitized_optarg);
1107 cl_log(LOG_INFO, "pidfile set to %s", pidfile);
1108 break;
1109 case 'C':
1110 timeout_watchdog_crashdump = sanitized_num_optarg;
1111 cl_log(LOG_INFO, "Setting crashdump watchdog timeout to %d",
1112 timeout_watchdog_crashdump);
1113 break;
1114 case '1':
1115 timeout_watchdog = sanitized_num_optarg;
1116 break;
1117 case '2':
1118 timeout_allocate = sanitized_num_optarg;
1119 break;
1120 case '3':
1121 timeout_loop = sanitized_num_optarg;
1122 break;
1123 case '4':
1124 timeout_msgwait = sanitized_num_optarg;
1125 break;
1126 case '5':
1127 timeout_watchdog_warn = sanitized_num_optarg;
1128 do_calculate_timeout_watchdog_warn = false;
1129 cl_log(LOG_INFO, "Setting latency warning to %d",
1130 timeout_watchdog_warn);
1131 break;
1132 case 't':
1133 servant_restart_interval = sanitized_num_optarg;
1134 cl_log(LOG_INFO, "Setting servant restart interval to %d",
1135 (int)servant_restart_interval);
1136 break;
1137 case 'I':
1138 timeout_io = sanitized_num_optarg;
1139 cl_log(LOG_INFO, "Setting IO timeout to %d",
1140 (int)timeout_io);
1141 break;
1142 case 'F':
1143 servant_restart_count = sanitized_num_optarg;
1144 cl_log(LOG_INFO, "Servant restart count set to %d",
1145 (int)servant_restart_count);
1146 break;
1147 case 'r':
1148 if (timeout_action) {
1149 free(timeout_action);
1150 }
1151 timeout_action = strdup(sanitized_optarg);
1152 break;
1153 case 'h':
1154 usage();
1155 goto out;
1156 break;
1157 default:
1158 exit_status = -2;
1159 goto out;
1160 break;
1161 }
1162 }
1163
1164 if (disk_count == 0) {
1165 /* if we already have disks from commandline
1166 then it is probably undesirable to add those
1167 from environment (general rule cmdline has precedence)
1168 */
1169 value = get_env_option("SBD_DEVICE");
1170 if ((value) && strlen(value)) {
1171 #if SUPPORT_SHARED_DISK
1172 int devices = parse_device_line(value);
1173 if(devices < 1) {
1174 fprintf(stderr, "Invalid device line: %s\n", value);
1175 exit_status = -1;
1176 goto out;
1177 }
1178 #else
1179 fprintf(stderr, "Shared disk functionality not supported\n");
1180 exit_status = -2;
1181 goto out;
1182 #endif
1183 }
1184 }
1185
1186 if (watchdogdev == NULL || strcmp(watchdogdev, "/dev/null") == 0) {
1187 watchdog_use = 0;
1188
1189 } else if (W_count > 0) {
1190 watchdog_use = arg_enabled(W_count);
1191 }
1192
1193 if (watchdog_use) {
1194 cl_log(LOG_INFO, "Watchdog enabled.");
1195 } else {
1196 cl_log(LOG_INFO, "Watchdog disabled.");
1197 }
1198
1199 if (c_count > 0) {
1200 check_cluster = arg_enabled(c_count);
1201 }
1202
1203 if (P_count > 0) {
1204 int check_pcmk_arg = arg_enabled(P_count);
1205
1206 if (has_check_pcmk_env && check_pcmk_arg != check_pcmk) {
1207 cl_log(LOG_WARNING, "Pacemaker integration is %s: "
1208 "SBD_PACEMAKER=%s is overridden by %s option. "
1209 "It's recommended to only use SBD_PACEMAKER.",
1210 check_pcmk_arg? "enabled" : "disabled",
1211 check_pcmk? "yes" : "no",
1212 check_pcmk_arg? "-P" : "-PP");
1213 }
1214 check_pcmk = check_pcmk_arg;
1215 }
1216
1217 if ((disk_count > 0) && (strlen(local_uname) > SECTOR_NAME_MAX)) {
1218 fprintf(stderr, "Node name mustn't be longer than %d chars.\n",
1219 SECTOR_NAME_MAX);
1220 fprintf(stderr, "If uname is longer define a name to be used by sbd.\n");
1221 exit_status = -1;
1222 goto out;
1223 }
1224
1225 if (disk_count > 3) {
1226 fprintf(stderr, "You can specify up to 3 devices via the -d option.\n");
1227 exit_status = -1;
1228 goto out;
1229 }
1230
1231 /* There must at least be one command following the options: */
1232 if ((argc - optind) < 1) {
1233 fprintf(stderr, "Not enough arguments.\n");
1234 exit_status = -2;
1235 goto out;
1236 }
1237
1238 if (init_set_proc_title(argc, argv, envp) < 0) {
1239 fprintf(stderr, "Allocation of proc title failed.\n");
1240 exit_status = -1;
1241 goto out;
1242 }
1243
1244 if (timeout_action) {
1245 char *p[2];
1246 int i;
1247 char c;
1248 int nrflags = sscanf(timeout_action, "%m[a-z],%m[a-z]%c", &p[0], &p[1], &c);
1249 bool parse_error = (nrflags < 1) || (nrflags > 2);
1250
1251 for (i = 0; (i < nrflags) && (i < 2); i++) {
1252 if (!strcmp(p[i], "reboot")) {
1253 timeout_sysrq_char = 'b';
1254 } else if (!strcmp(p[i], "crashdump")) {
1255 timeout_sysrq_char = 'c';
1256 } else if (!strcmp(p[i], "off")) {
1257 timeout_sysrq_char = 'o';
1258 } else if (!strcmp(p[i], "flush")) {
1259 do_flush = true;
1260 } else if (!strcmp(p[i], "noflush")) {
1261 do_flush = false;
1262 } else {
1263 parse_error = true;
1264 }
1265 free(p[i]);
1266 }
1267 if (parse_error) {
1268 fprintf(stderr, "Failed to parse timeout-action \"%s\".\n",
1269 timeout_action);
1270 exit_status = -1;
1271 goto out;
1272 }
1273 }
1274
1275 if (strcmp(argv[optind], "watch") == 0) {
1276 value = get_env_option("SBD_SYNC_RESOURCE_STARTUP");
1277 sync_resource_startup =
1278 crm_is_true(value?value:SBD_SYNC_RESOURCE_STARTUP_DEFAULT);
1279
1280 #if !USE_PACEMAKERD_API
1281 if (sync_resource_startup) {
1282 fprintf(stderr, "Failed to sync resource-startup as "
1283 "SBD was built against pacemaker not supporting pacemakerd-API.\n");
1284 exit_status = -1;
1285 goto out;
1286 }
1287 #else
1288 if (check_pcmk && !sync_resource_startup) {
1289 cl_log(LOG_WARNING, "SBD built against pacemaker supporting "
1290 "pacemakerd-API. Should think about enabling "
1291 "SBD_SYNC_RESOURCE_STARTUP.");
1292
1293 } else if (!check_pcmk && sync_resource_startup) {
1294 fprintf(stderr, "Set SBD_PACEMAKER=yes to allow resource startup syncing. "
1295 "Otherwise explicitly set SBD_SYNC_RESOURCE_STARTUP=no if to intentionally disable.\n");
1296 exit_status = -1;
1297 goto out;
1298 }
1299 #endif
1300 }
1301
1302 #if SUPPORT_SHARED_DISK
1303 if (strcmp(argv[optind], "create") == 0) {
1304 exit_status = init_devices(servants_leader);
1305
1306 } else if (strcmp(argv[optind], "dump") == 0) {
1307 exit_status = dump_headers(servants_leader);
1308
1309 } else if (strcmp(argv[optind], "allocate") == 0) {
1310 exit_status = allocate_slots(argv[optind + 1], servants_leader);
1311
1312 } else if (strcmp(argv[optind], "list") == 0) {
1313 exit_status = list_slots(servants_leader);
1314
1315 } else if (strcmp(argv[optind], "message") == 0) {
1316 exit_status = messenger(argv[optind + 1], argv[optind + 2], servants_leader);
1317
1318 } else if (strcmp(argv[optind], "ping") == 0) {
1319 exit_status = ping_via_slots(argv[optind + 1], servants_leader);
1320
1321 } else
1322 #endif
1323 if (strcmp(argv[optind], "query-watchdog") == 0) {
1324 exit_status = watchdog_info();
1325 } else if (strcmp(argv[optind], "test-watchdog") == 0) {
1326 exit_status = watchdog_test();
1327 } else if (strcmp(argv[optind], "watch") == 0) {
1328 /* sleep $(sbd $SBD_DEVICE_ARGS dump | grep -m 1 msgwait | awk '{print $4}') 2>/dev/null */
1329
1330 const char *delay_source = delay ? "SBD_DELAY_START" : "";
1331
1332 #if SUPPORT_SHARED_DISK
1333 if(disk_count > 0) {
1334 /* If no devices are specified, its not an error to be unable to find one */
1335 open_any_device(servants_leader);
1336
1337 if (delay_start && delay <= 0) {
1338 delay = get_first_msgwait(servants_leader);
1339
1340 if (delay > 0) {
1341 delay_source = "msgwait";
1342 } else {
1343 cl_log(LOG_WARNING, "No 'msgwait' value from disk, using '2 * watchdog-timeout' for 'delay' starting");
1344 }
1345 }
1346 }
1347 #endif
1348 /* Re-calculate timeout_watchdog_warn based on any timeout_watchdog from:
1349 * SBD_WATCHDOG_TIMEOUT, -1 option or on-disk setting read with open_any_device() */
1350 if (do_calculate_timeout_watchdog_warn) {
1351 timeout_watchdog_warn = calculate_timeout_watchdog_warn(timeout_watchdog);
1352 }
1353
1354 if (delay_start) {
1355 /* diskless mode or disk read issues causing get_first_msgwait() to return a 0 for delay */
1356 if (delay <= 0) {
1357 delay = 2 * timeout_watchdog;
1358 delay_source = "watchdog-timeout * 2";
1359 }
1360
1361 cl_log(LOG_DEBUG, "Delay start (yes), (delay: %ld), (delay source: %s)", delay, delay_source);
1362
1363 sleep((unsigned long) delay);
1364
1365 } else {
1366 cl_log(LOG_DEBUG, "Delay start (no)");
1367 }
1368
1369 /* We only want this to have an effect during watch right now;
1370 * pinging and fencing would be too confused */
1371 cl_log(LOG_INFO, "Turning on pacemaker checks: %d", check_pcmk);
1372 if (check_pcmk) {
1373 recruit_servant("pcmk", 0);
1374 #if SUPPORT_PLUGIN
1375 check_cluster = 1;
1376 #endif
1377 }
1378
1379 cl_log(LOG_INFO, "Turning on cluster checks: %d", check_cluster);
1380 if (check_cluster) {
1381 recruit_servant("cluster", 0);
1382 }
1383
1384 cl_log(LOG_NOTICE, "%s flush + write \'%c\' to sysrq in case of timeout",
1385 do_flush?"Do":"Skip", timeout_sysrq_char);
1386 exit_status = inquisitor();
1387 } else {
1388 exit_status = -2;
1389 }
1390
1391 out:
1392 if (timeout_action) {
1393 free(timeout_action);
1394 }
1395 if (exit_status < 0) {
1396 if (exit_status == -2) {
1397 usage();
1398 } else {
1399 fprintf(stderr, "sbd failed; please check the logs.\n");
1400 }
1401 return (1);
1402 }
1403 return (0);
1404 }
1405