Skip to content

Commit 6c1c641

Browse files
committed
Simplify the systemd unit file by reading options from the environment
1 parent 756d0e0 commit 6c1c641

File tree

5 files changed

+166
-15
lines changed

5 files changed

+166
-15
lines changed

src/sbd-inquisitor.c

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ void inquisitor_child(void)
392392

393393
sigemptyset(&procmask);
394394
sigaddset(&procmask, SIGCHLD);
395+
sigaddset(&procmask, SIGTERM);
395396
sigaddset(&procmask, SIG_LIVENESS);
396397
sigaddset(&procmask, SIG_EXITREQ);
397398
sigaddset(&procmask, SIG_TEST);
@@ -415,7 +416,7 @@ void inquisitor_child(void)
415416

416417
clock_gettime(CLOCK_MONOTONIC, &t_now);
417418

418-
if (sig == SIG_EXITREQ) {
419+
if (sig == SIG_EXITREQ || sig == SIGTERM) {
419420
servants_kill();
420421
watchdog_close(true);
421422
exiting = 1;
@@ -628,12 +629,70 @@ int inquisitor(void)
628629
return -1;
629630
}
630631

632+
633+
static int
634+
parse_device_line(const char *line)
635+
{
636+
int lpc = 0;
637+
int last = 0;
638+
int max = 0;
639+
int found = 0;
640+
641+
if(line) {
642+
max = strlen(line);
643+
}
644+
645+
if (max <= 0) {
646+
return found;
647+
}
648+
649+
crm_trace("Processing %d bytes: [%s]", max, line);
650+
/* Skip initial whitespace */
651+
for (lpc = 0; lpc <= max && isspace(line[lpc]); lpc++) {
652+
last = lpc + 1;
653+
}
654+
655+
/* Now the actual content */
656+
for (lpc = 0; lpc <= max; lpc++) {
657+
int a_space = isspace(line[lpc]);
658+
659+
if (a_space && lpc < max && isspace(line[lpc + 1])) {
660+
/* fast-forward to the end of the spaces */
661+
662+
} else if (a_space || line[lpc] == ';' || line[lpc] == 0) {
663+
int rc = 1;
664+
char *entry = NULL;
665+
666+
if (lpc != last) {
667+
entry = calloc(1, 1 + lpc - last);
668+
rc = sscanf(line + last, "%[^;]", entry);
669+
}
670+
671+
if (entry == NULL) {
672+
/* Skip */
673+
} else if (rc != 1) {
674+
crm_warn("Could not parse (%d %d): %s", last, lpc, line + last);
675+
} else {
676+
crm_trace("Adding '%s'", entry);
677+
recruit_servant(entry, 0);
678+
found++;
679+
}
680+
681+
free(entry);
682+
last = lpc + 1;
683+
}
684+
}
685+
return found;
686+
}
687+
631688
int main(int argc, char **argv, char **envp)
632689
{
633690
int exit_status = 0;
634691
int c;
635692
int w = 0;
636693
int qb_facility;
694+
const char *value = NULL;
695+
int start_delay = 0;
637696

638697
if ((cmdname = strrchr(argv[0], '/')) == NULL) {
639698
cmdname = argv[0];
@@ -649,6 +708,49 @@ int main(int argc, char **argv, char **envp)
649708

650709
sbd_get_uname();
651710

711+
value = getenv("SBD_DEVICE");
712+
if(value) {
713+
#if SUPPORT_SHARED_DISK
714+
int devices = parse_device_line(value);
715+
if(devices > 0) {
716+
fprintf(stderr, "Invalid device line: %s\n", value);
717+
exit_status = -2;
718+
goto out;
719+
}
720+
#else
721+
fprintf(stderr, "Shared disk functionality not supported\n");
722+
exit_status = -2;
723+
goto out;
724+
#endif
725+
}
726+
727+
value = getenv("SBD_PACEMAKER");
728+
if(value) {
729+
check_pcmk = crm_is_true(value);
730+
}
731+
732+
value = getenv("SBD_STARTMODE");
733+
if(value) {
734+
start_mode = crm_int_helper(value, NULL);
735+
cl_log(LOG_INFO, "Start mode set to: %d", (int)start_mode);
736+
}
737+
738+
value = getenv("SBD_WATCHDOG_DEV");
739+
if(value) {
740+
watchdogdev = value;
741+
}
742+
743+
value = getenv("SBD_PIDFILE");
744+
if(value) {
745+
pidfile = strdup(value);
746+
cl_log(LOG_INFO, "pidfile set to %s", pidfile);
747+
}
748+
749+
value = getenv("SBD_DELAY_START");
750+
if(value) {
751+
start_delay = crm_is_true(value);
752+
}
753+
652754
while ((c = getopt(argc, argv, "C:DPRTWZhvw:d:n:p:1:2:3:4:5:t:I:F:S:s:")) != -1) {
653755
switch (c) {
654756
case 'D':
@@ -681,7 +783,7 @@ int main(int argc, char **argv, char **envp)
681783
w++;
682784
break;
683785
case 'w':
684-
watchdogdev = strdup(optarg);
786+
watchdogdev = optarg;
685787
break;
686788
case 'd':
687789
#if SUPPORT_SHARED_DISK
@@ -751,8 +853,11 @@ int main(int argc, char **argv, char **envp)
751853
}
752854

753855
if (w > 0) {
754-
watchdog_use = w % 2;
755-
}
856+
watchdog_use = w % 2;
857+
858+
} else if(watchdogdev == NULL || strcmp(watchdogdev, "/dev/null") == 0) {
859+
watchdog_use = 0;
860+
}
756861

757862
if (watchdog_use) {
758863
cl_log(LOG_INFO, "Watchdog enabled.");
@@ -795,7 +900,10 @@ int main(int argc, char **argv, char **envp)
795900
} else if (strcmp(argv[optind], "ping") == 0) {
796901
exit_status = ping_via_slots(argv[optind + 1], servants_leader);
797902
} else if (strcmp(argv[optind], "watch") == 0) {
798-
open_any_device(servants_leader);
903+
if(servant_count > 0) {
904+
/* If no devices are specified, its not an error to be unable to find one */
905+
open_any_device(servants_leader);
906+
}
799907

800908
/* We only want this to have an effect during watch right now;
801909
* pinging and fencing would be too confused */
@@ -804,13 +912,21 @@ int main(int argc, char **argv, char **envp)
804912
servant_count--;
805913
}
806914

915+
if(start_delay) {
916+
unsigned long delay = get_first_msgwait(servants_leader);
917+
918+
sleep(delay);
919+
}
920+
807921
exit_status = inquisitor();
808922

809923
} else {
810924
exit_status = -2;
811925
}
812926
#else
813927
if (strcmp(argv[optind], "watch") == 0) {
928+
/* sleep $(sbd -d "$SBD_DEVICE" dump | grep -m 1 msgwait | awk '{print $4}') 2>/dev/null */
929+
814930
/* We only want this to have an effect during watch right now;
815931
* pinging and fencing would be too confused */
816932
if (check_pcmk) {

src/sbd-md.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,32 @@ int messenger(const char *name, const char *msg, struct servants_list_item *serv
897897
}
898898
}
899899

900+
unsigned long
901+
get_first_msgwait(struct servants_list_item *servants)
902+
{
903+
unsigned long msgwait = 0;
904+
struct servants_list_item *s = servants;
905+
906+
for (s = servants; s; s = s->next) {
907+
struct sbd_context *st;
908+
struct sector_header_s *s_header;
909+
st = open_device(s->devname, LOG_WARNING);
910+
if (!st) {
911+
continue;
912+
}
913+
914+
s_header = header_get(st);
915+
if (s_header != NULL) {
916+
msgwait = (unsigned long)s_header->timeout_msgwait;
917+
close_device(st);
918+
return msgwait;
919+
}
920+
921+
close_device(st);
922+
}
923+
return msgwait;
924+
}
925+
900926
int dump_headers(struct servants_list_item *servants)
901927
{
902928
int rc = 0;

src/sbd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ int allocate_slots(const char *name, struct servants_list_item *servants);
148148
int list_slots(struct servants_list_item *servants);
149149
int ping_via_slots(const char *name, struct servants_list_item *servants);
150150
int dump_headers(struct servants_list_item *servants);
151+
unsigned long get_first_msgwait(struct servants_list_item *servants);
151152
int messenger(const char *name, const char *msg, struct servants_list_item *servants);
152153
int servant(const char *diskname, int mode, const void* argp);
153154
#endif

src/sbd.service

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ RefuseManualStart=true
88

99
[Service]
1010
Type=forking
11-
ExecStart=/usr/share/sbd/sbd.sh start
12-
ExecStop=/usr/share/sbd/sbd.sh stop
13-
PIDFile=/var/run/sbd.pid
11+
EnvironmentFile=-@sysconfdir@/sysconfig/sbd
12+
ExecStart=@sbindir@/sbd $SBD_OPTS watch
13+
ExecStop=kill -TERM $MAINPID
14+
PIDFile=${SBD_PIDFILE}
15+
1416
# Could this benefit from exit codes for restart?
1517
# Does this need to be set to msgwait * 1.2?
1618
# TimeoutSec=
@@ -19,5 +21,5 @@ PIDFile=/var/run/sbd.pid
1921
Restart=on-abort
2022

2123
[Install]
22-
RequiredBy=pacemaker.service
24+
RequiredBy=corosync.service
2325

src/sbd.sysconfig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
# and to monitor. If specifying more than one path, use ";" as
66
# separator.
77
#
8-
SBD_DEVICE=""
8+
#SBD_DEVICE=""
99

1010
## Type: yesno
1111
## Default: yes
1212
#
1313
# Whether to enable the pacemaker integration.
1414
#
15-
SBD_PACEMAKER=
15+
SBD_PACEMAKER=yes
1616

1717
## Type: list(always,clean)
1818
## Default: always
@@ -21,7 +21,7 @@ SBD_PACEMAKER=
2121
# allow sbd to start if it was not previously fenced. See the -S option
2222
# in the man page.
2323
#
24-
SBD_STARTMODE=
24+
SBD_STARTMODE=clean
2525

2626
## Type: yesno
2727
## Default: no
@@ -34,19 +34,25 @@ SBD_STARTMODE=
3434
# This option may be ignored at a later point, once pacemaker handles
3535
# this case better.
3636
#
37-
SBD_DELAY_START=
37+
SBD_DELAY_START=no
3838

3939
## Type: yesno
4040
## Default: yes
4141
#
4242
# Whether to use a watchdog.
4343
#
44-
SBD_WATCHDOG=
44+
SBD_WATCHDOG_DEV=/dev/watchdog
45+
46+
## Type: string
47+
## Default: /var/run/sbd.pid
48+
#
49+
# Where to store the PID of the active process
50+
#
51+
SBD_PIDFILE=/var/run/sbd.pid
4552

4653
## Type: string
4754
## Default: ""
4855
#
4956
# Additional options for starting sbd
5057
#
5158
SBD_OPTS=
52-

0 commit comments

Comments
 (0)