diff --git a/doc/config.md b/doc/config.md index 4cba1e22..dec554cd 100644 --- a/doc/config.md +++ b/doc/config.md @@ -913,9 +913,20 @@ configurable with the following options: endless restarts. Useful in many use-cases, but not what `service` was originally designed for so not the default behavior * `oncrash:reboot` -- when all retries have failed, and the service - has *crashed*, if this option is set the system is rebooted. + has *crashed*, if this option is set the system is rebooted * `oncrash:script` -- similarly, but instead of rebooting, call the - `post:script` action if set, see below. + `post:script` action with exit code `crashed`, see below + * `reload:script [args]` -- some services do not support `SIGHUP` but + may have other ways to update the configuration of a running daemon. + When `reload:script` is defined it is preferred over `SIGHUP` + * `stop:script [args]` -- some services may require alternate methods + to be stopped. When `stop:script` is defined it is preferred over + `SIGTERM` and `stop`, for `service` and `sysv` stanzas, respectively + +> [!CAUTION] +> Both `reload:script` and `stop:script` are called as PID 1, without +> any timeout! Meaning, it is up to you to ensure the script is not +> blocking for seconds at a time or never terminates. When stopping a service (run/task/sysv/service), either manually or when moving to another runlevel, Finit starts by sending `SIGTERM`, to allow diff --git a/src/service.c b/src/service.c index e3080b99..a0b0bd33 100644 --- a/src/service.c +++ b/src/service.c @@ -1002,6 +1002,7 @@ static void service_cleanup(svc_t *svc) */ int service_stop(svc_t *svc) { + const char *id = svc_ident(svc, NULL, 0); char cmdline[CMD_SIZE] = ""; int do_progress = 1; int rc = 0; @@ -1014,11 +1015,11 @@ int service_stop(svc_t *svc) service_timeout_cancel(svc); - compose_cmdline(svc, cmdline, sizeof(cmdline)); - if (!svc_is_sysv(svc)) { + if (svc->stop_script[0]) { + logit(LOG_CONSOLE | LOG_NOTICE, "%s[%d], calling stop:%s ...", id, svc->pid, svc->stop_script); + } else if (!svc_is_sysv(svc)) { char *nm = pid_get_name(svc->pid, NULL, 0); const char *sig = sig_name(svc->sighalt); - char *id = svc_ident(svc, NULL, 0); if (svc->pid <= 1) return 1; @@ -1030,9 +1031,10 @@ int service_stop(svc_t *svc) } dbg("Sending %s to pid:%d name:%s(%s)", sig, svc->pid, id, nm); - logit(LOG_CONSOLE | LOG_NOTICE, "Stopping %s[%d], sending %s ...", id, svc->pid, sig); + logit(LOG_CONSOLE | LOG_NOTICE, "%s[%d], stopping, sending %s ...", id, svc->pid, sig); } else { - logit(LOG_CONSOLE | LOG_NOTICE, "Calling '%s stop' ...", cmdline); + compose_cmdline(svc, cmdline, sizeof(cmdline)); + logit(LOG_CONSOLE | LOG_NOTICE, "%s[%d], calling '%s stop' ...", id, svc->pid, cmdline); } /* @@ -1060,7 +1062,9 @@ int service_stop(svc_t *svc) if (runlevel != 1 && do_progress && svc_is_daemon(svc)) print_desc("Stopping ", svc->desc); - if (!svc_is_sysv(svc)) { + if (svc->stop_script[0]) { + rc = run(svc->stop_script, NULL); + } else if (!svc_is_sysv(svc)) { if (svc->pid > 1) { /* * Send SIGTERM to parent process of process group, not to the @@ -1071,11 +1075,8 @@ int service_stop(svc_t *svc) rc = kill(svc->pid, svc->sighalt); dbg("kill(%d, %d) => rc %d, errno %d", svc->pid, svc->sighalt, rc, errno); /* PID lost or forking process never really started */ - if (rc == -1 && (errno == ESRCH || errno == ENOENT)) { - service_cleanup(svc); - svc_set_state(svc, SVC_HALTED_STATE); - } else - svc_set_state(svc, SVC_STOPPING_STATE); + if (rc == -1 && (errno == ESRCH || errno == ENOENT)) + rc = 1; } else { service_cleanup(svc); svc_set_state(svc, SVC_HALTED_STATE); @@ -1083,7 +1084,6 @@ int service_stop(svc_t *svc) } else { char *args[MAX_NUM_SVC_ARGS + 1]; size_t i = 0, j; - pid_t pid; args[i++] = svc->cmd; /* this handles, e.g., bridge-stop br0 stop */ @@ -1095,8 +1095,7 @@ int service_stop(svc_t *svc) args[i++] = "stop"; args[i] = NULL; - pid = fork(); - switch (pid) { + switch (fork()) { case 0: setsid(); redirect(svc); @@ -1108,11 +1107,16 @@ int service_stop(svc_t *svc) rc = 1; break; default: - svc_set_state(svc, SVC_STOPPING_STATE); break; } } + if (rc == 1) { + service_cleanup(svc); + svc_set_state(svc, SVC_HALTED_STATE); + } else + svc_set_state(svc, SVC_STOPPING_STATE); + if (runlevel != 1 && do_progress && svc_is_daemon(svc)) print_result(rc); @@ -1120,34 +1124,27 @@ int service_stop(svc_t *svc) } /** - * service_restart - Restart a service by sending %SIGHUP + * service_reload - Reload a service * @svc: Service to reload * * This function does some basic checks of the runtime state of Finit - * and a sanity check of the @svc before sending %SIGHUP. + * and a sanity check of the @svc before sending %SIGHUP or calling + * the reload:script command. * * Returns: * POSIX OK(0) or non-zero on error. */ -static int service_restart(svc_t *svc) +static int service_reload(svc_t *svc) { + const char *id = svc_ident(svc, NULL, 0); int do_progress = 1; pid_t lost = 0; int rc; - /* Ignore if finit is SIGSTOP'ed */ - if (is_norespawn()) + /* Ignore if service is invalid or finit is SIGSTOP'ed */ + if (!svc || is_norespawn()) return 1; - if (!svc || !svc->sighup) - return 1; - - if (svc->pid <= 1) { - dbg("%s: bad PID %d for %s, SIGHUP", svc_ident(svc, NULL, 0), svc->pid, svc->cmd); - svc->start_time = svc->pid = 0; - return 1; - } - /* Skip progress if desc disabled or bootstrap task */ if (!svc->desc[0] || svc_in_runlevel(svc, INIT_LEVEL)) do_progress = 0; @@ -1155,14 +1152,28 @@ static int service_restart(svc_t *svc) if (do_progress) print_desc("Restarting ", svc->desc); - dbg("Sending SIGHUP to PID %d", svc->pid); - logit(LOG_CONSOLE | LOG_NOTICE, "Restarting %s[%d], sending SIGHUP ...", - svc_ident(svc, NULL, 0), svc->pid); - rc = kill(svc->pid, SIGHUP); - if (rc == -1 && (errno == ESRCH || errno == ENOENT)) { - /* nobody home, reset internal state machine */ - lost = svc->pid; + if (svc->reload_script[0]) { + logit(LOG_CONSOLE | LOG_NOTICE, "%s[%d], calling reload:%s ...", id, svc->pid, svc->reload_script); + rc = run(svc->reload_script, NULL); + } else if (svc->sighup) { + if (svc->pid <= 1) { + dbg("%s[%d]: bad PID, cannot reload service", id, svc->pid); + svc->start_time = svc->pid = 0; + goto done; + } + dbg("%s[%d], sending SIGHUP", id, svc->pid); + logit(LOG_CONSOLE | LOG_NOTICE, "%s[%d], sending SIGHUP ...", id, svc->pid); + rc = kill(svc->pid, SIGHUP); + if (rc == -1 && (errno == ESRCH || errno == ENOENT)) { + /* nobody home, reset internal state machine */ + lost = svc->pid; + } } else { + warnx("%s: neither HUP nor reload:script defined, no action.", id); + rc = 1; + } + + if (!rc) { /* Declare we're waiting for svc to re-assert/touch its pidfile */ svc_starting(svc); @@ -1172,7 +1183,7 @@ static int service_restart(svc_t *svc) touch(pid_file(svc)); } } - +done: if (do_progress) print_result(rc); @@ -1386,26 +1397,36 @@ static void parse_script(svc_t *svc, char *type, char *script, int *tmo, char *b if (errstr) { errx(1, "%s: tmo %s is %s (0-3600)", svc_ident(svc, NULL, 0), script, errstr); - return; + goto err; } *tmo = (int)(sec * 1000); } else { path = script; - *tmo = svc->killdelay; + if (tmo) + *tmo = svc->killdelay; + } + + if (unquote(&path, NULL)) { + errx(1, "Syntax error, unterminated quote in %s:%s", type, path); + goto err; } found = which(path); if (!found) { - /* Not in $PATH, check if path exists and is executable */ - if (access(path, X_OK)) { - logit(LOG_WARNING, "%s:%s is missing or not executable, skipping.", - type, path); - return; - } - } else - free(found); + logit(LOG_WARNING, "%s:%s is missing or not executable, skipping.", type, path); + goto err; + } + free(found); + + if (strlen(path) >= len) { + errx(1, "Command too long in %s:%s", type, path); + goto err; + } strlcpy(buf, path, len); + return; +err: + memset(buf, 0, len); } /* @@ -1591,6 +1612,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) char *id = NULL, *env = NULL, *cgroup = NULL; char *pre_script = NULL, *post_script = NULL; char *ready_script = NULL, *conflict = NULL; + char *reload_script = NULL, *stop_script = NULL; char *cleanup_script = NULL; char ident[MAX_IDENT_LEN]; char *ifstmt = NULL; @@ -1701,6 +1723,10 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) ready_script = arg; else if (MATCH_CMD(cmd, "cleanup:", arg)) cleanup_script = arg; + else if (MATCH_CMD(cmd, "reload:", arg)) + reload_script = arg; + else if (MATCH_CMD(cmd, "stop:", arg)) + stop_script = arg; else if (MATCH_CMD(cmd, "env:", arg)) env = arg; /* catch both cgroup: and cgroup. handled in parse_cgroup() */ @@ -1896,6 +1922,16 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) else memset(svc->cleanup_script, 0, sizeof(svc->cleanup_script)); + if (reload_script) + parse_script(svc, "reload", reload_script, NULL, svc->reload_script, sizeof(svc->reload_script)); + else + memset(svc->reload_script, 0, sizeof(svc->reload_script)); + + if (stop_script) + parse_script(svc, "stop", stop_script, NULL, svc->stop_script, sizeof(svc->stop_script)); + else + memset(svc->stop_script, 0, sizeof(svc->stop_script)); + if (!svc_is_tty(svc)) { if (log) parse_log(svc, log); @@ -2141,7 +2177,7 @@ void service_update_rdeps(void) continue; /* Service supports reloading conf without stop/start */ - if (!svc_is_nohup(svc)) + if (!svc_is_noreload(svc)) continue; /* Yup, no need to stop start rdeps */ svc_mark_affected(mkcond(svc, cond, sizeof(cond))); @@ -2776,7 +2812,11 @@ restart: case COND_ON: if (svc_is_changed(svc)) { - if (svc_nohup(svc) || !svc_is_daemon(svc)) { + /* + * If service does not suport reload, or its command line + * arguments have been modified, we need to stop-start it. + */ + if (svc_is_noreload(svc) || svc->args_dirty) { service_stop(svc); } else { /* @@ -2786,7 +2826,7 @@ restart: if (sm_in_reload()) break; - service_restart(svc); + service_reload(svc); } svc_mark_clean(svc); diff --git a/src/svc.h b/src/svc.h index c855a5ec..b3c218ce 100644 --- a/src/svc.h +++ b/src/svc.h @@ -212,6 +212,12 @@ typedef struct svc { char cleanup_script[MAX_CMD_LEN]; int cleanup_tmo; + /* When set, used instead of SIGHUP or stop-start */ + char reload_script[MAX_CMD_LEN]; + + /* When set, used instead of SIGTERM or sysv 'stop' */ + char stop_script[MAX_CMD_LEN]; + /* * Used to forcefully kill services that won't shutdown on * termination and to delay restarts of crashing services. @@ -272,10 +278,9 @@ static inline int svc_is_tty (svc_t *svc) { return svc && SVC_TYPE_TTY static inline int svc_is_runtask (svc_t *svc) { return svc && (SVC_TYPE_RUNTASK & svc->type);} static inline int svc_is_forking (svc_t *svc) { return svc && svc->forking; } static inline int svc_is_manual (svc_t *svc) { return svc && svc->manual; } -static inline int svc_is_nohup (svc_t *svc) { return svc && (0 == svc->sighup); } +static inline int svc_is_noreload (svc_t *svc) { return svc && (0 == svc->sighup && 0 == svc->reload_script[0]); } static inline int svc_in_runlevel (svc_t *svc, int runlevel) { return svc && ISSET(svc->runlevels, runlevel); } -static inline int svc_nohup (svc_t *svc) { return svc && (0 == svc->sighup || 0 != svc->args_dirty); } static inline int svc_has_pidfile (svc_t *svc) { return svc_is_daemon(svc) && svc->pidfile[0] != 0 && svc->pidfile[0] != '!'; } static inline int svc_has_pre (svc_t *svc) { return svc->pre_script[0]; } static inline int svc_has_post (svc_t *svc) { return svc->post_script[0]; } diff --git a/src/tmpfiles.c b/src/tmpfiles.c index f8bdcbd2..5425f423 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -642,6 +642,7 @@ int main(int argc, char *argv[]) continue; tmpfiles(line); + free(line); } fclose(fp); diff --git a/test/add-remove-dynamic-service-sub-config.sh b/test/add-remove-dynamic-service-sub-config.sh index a0e3a709..958dd8fe 100755 --- a/test/add-remove-dynamic-service-sub-config.sh +++ b/test/add-remove-dynamic-service-sub-config.sh @@ -15,8 +15,6 @@ test_teardown() # shellcheck source=/dev/null . "$TEST_DIR/lib/setup.sh" -say "Test start $(date)" - say "Add a dynamic service in $FINIT_RCSD/service.conf" run "echo 'service [2345] kill:20 log service.sh -- Dyn serv' > $FINIT_RCSD/service.conf" diff --git a/test/add-remove-dynamic-service.sh b/test/add-remove-dynamic-service.sh index df6cb029..159a26bb 100755 --- a/test/add-remove-dynamic-service.sh +++ b/test/add-remove-dynamic-service.sh @@ -15,8 +15,6 @@ test_teardown() # shellcheck source=/dev/null . "$TEST_DIR/lib/setup.sh" -say "Test start $(date)" - say "Add a dynamic service in $FINIT_CONF" run "echo 'service [2345] kill:20 log service.sh -- Dyn service' > $FINIT_CONF" diff --git a/test/bootstrap-crash.sh b/test/bootstrap-crash.sh index e843e021..36c9897f 100755 --- a/test/bootstrap-crash.sh +++ b/test/bootstrap-crash.sh @@ -11,11 +11,6 @@ BOOTSTRAP="run [S] crasher.sh -- Party crasher" # shellcheck source=/dev/null . "$TEST_DIR/lib/setup.sh" -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/cond-start-task.sh b/test/cond-start-task.sh index 3f472807..8a194020 100755 --- a/test/cond-start-task.sh +++ b/test/cond-start-task.sh @@ -7,11 +7,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/crashing.sh b/test/crashing.sh index 7f154583..e19cf59c 100755 --- a/test/crashing.sh +++ b/test/crashing.sh @@ -5,11 +5,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/depserv.sh b/test/depserv.sh index 17200893..20c3f61d 100755 --- a/test/depserv.sh +++ b/test/depserv.sh @@ -14,11 +14,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/devmon.sh b/test/devmon.sh index edd6e142..9261ee20 100755 --- a/test/devmon.sh +++ b/test/devmon.sh @@ -7,11 +7,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/failing-sysv.sh b/test/failing-sysv.sh index 377cf03a..11ea4f9f 100755 --- a/test/failing-sysv.sh +++ b/test/failing-sysv.sh @@ -8,7 +8,6 @@ TEST_DIR=$(dirname "$0") test_setup() { - say "Test start $(date)" run "mkdir -p /etc/default" } diff --git a/test/global-envs.sh b/test/global-envs.sh index e6ffbe80..548afb96 100755 --- a/test/global-envs.sh +++ b/test/global-envs.sh @@ -8,11 +8,6 @@ set COLORTERM=yes\n\ b aq=oroszoe\n\ baz=qux" -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/initctl-status-subset.sh b/test/initctl-status-subset.sh index 863acb05..eba86f58 100755 --- a/test/initctl-status-subset.sh +++ b/test/initctl-status-subset.sh @@ -7,11 +7,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/lib/setup.sh b/test/lib/setup.sh index 98d652ad..33bed403 100755 --- a/test/lib/setup.sh +++ b/test/lib/setup.sh @@ -362,6 +362,8 @@ echo "Finit running as PID $finit_pid" #tty=/dev/$(texec cat /sys/class/tty/console/active) #texec cat "$tty" & +sep "Test start $(date)" + # Allow Finit to start up properly before launching the test i=0 set +u diff --git a/test/notify.sh b/test/notify.sh index 9baa37f4..0cbe6391 100755 --- a/test/notify.sh +++ b/test/notify.sh @@ -8,7 +8,6 @@ TEST_DIR=$(dirname "$0") test_setup() { - say "Test start $(date)" run "mkdir -p /etc/default" } diff --git a/test/pidfile.sh b/test/pidfile.sh index 737fc31f..a2eabdf1 100755 --- a/test/pidfile.sh +++ b/test/pidfile.sh @@ -5,11 +5,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/pre-post-serv.sh b/test/pre-post-serv.sh index 6355bea9..0b907465 100755 --- a/test/pre-post-serv.sh +++ b/test/pre-post-serv.sh @@ -9,11 +9,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/process-depends.sh b/test/process-depends.sh index cc3738a5..1eeebab3 100755 --- a/test/process-depends.sh +++ b/test/process-depends.sh @@ -8,7 +8,6 @@ TEST_DIR=$(dirname "$0") test_setup() { - say "Test start $(date)" run "mkdir -p /etc/default" } diff --git a/test/rclocal.sh b/test/rclocal.sh index e43bd221..d7bc6d3d 100755 --- a/test/rclocal.sh +++ b/test/rclocal.sh @@ -1,26 +1,20 @@ #!/bin/sh # Regression test for basic /etc/rc.local functionality -# shellcheck disable=SC2317 +# shellcheck disable=2317,2034 set -eu +RCLOCAL="echo 'KILROY WAS HERE' > /tmp/rclocal" TEST_DIR=$(dirname "$0") #DEBUG=1 -# shellcheck disable=SC2034 -RCLOCAL="echo 'KILROY WAS HERE' > /tmp/rclocal" - -# shellcheck source=/dev/null -. "$TEST_DIR/lib/setup.sh" - -test_setup() -{ - say "Test start $(date)" -} test_teardown() { say "Test done $(date)" } +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + sleep 1 assert_file_contains /tmp/rclocal "KILROY WAS HERE" diff --git a/test/ready-serv.sh b/test/ready-serv.sh index dbadb626..ce91081f 100755 --- a/test/ready-serv.sh +++ b/test/ready-serv.sh @@ -4,11 +4,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/restart-self.sh b/test/restart-self.sh index 633d9a7f..d8ceb0d9 100755 --- a/test/restart-self.sh +++ b/test/restart-self.sh @@ -6,7 +6,6 @@ TEST_DIR=$(dirname "$0") test_setup() { - say "Test start $(date)" run "mkdir -p /etc/default" } diff --git a/test/run-restart-forever.sh b/test/run-restart-forever.sh index 90c95638..9fc28133 100755 --- a/test/run-restart-forever.sh +++ b/test/run-restart-forever.sh @@ -9,7 +9,6 @@ TEST_DIR=$(dirname "$0") test_setup() { - say "Test start $(date)" run "mkdir -p /etc/default" export TEST_TIMEOUT=5 } diff --git a/test/run-task-tricks.sh b/test/run-task-tricks.sh index 8b738803..f569cc32 100755 --- a/test/run-task-tricks.sh +++ b/test/run-task-tricks.sh @@ -21,19 +21,14 @@ run [4] name:four initctl restart service.sh -- Restart service" # shellcheck disable=SC2034 RCLOCAL="echo \"$0 ==============\" &1>2" -# shellcheck source=/dev/null -. "$TEST_DIR/lib/setup.sh" - -test_setup() -{ - say "Test start $(date)" - } - test_teardown() { say "Test done $(date)" } +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + say "$FINIT_CONF:" run "cat $FINIT_CONF" say "/etc/rc.local:" diff --git a/test/runlevel.sh b/test/runlevel.sh index 4b3e33c9..22580aae 100755 --- a/test/runlevel.sh +++ b/test/runlevel.sh @@ -1,17 +1,11 @@ #!/bin/sh # Verify selecting alternate runlevel from cmdline +# shellcheck disable=SC2034 set -eu TEST_DIR=$(dirname "$0") - -# shellcheck disable=SC2034 FINIT_RUNLEVEL=9 -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/runparts.sh b/test/runparts.sh index c88ed433..8bae27b7 100755 --- a/test/runparts.sh +++ b/test/runparts.sh @@ -8,26 +8,20 @@ # - SNNfoo should be called with a 'start' argument # - KNNfoo should be called with a 'stop' argument # +# shellcheck disable=SC2034 +BOOTSTRAP="runparts /etc/rc.d" TEST_DIR=$(dirname "$0") #DEBUG=1 -# shellcheck disable=SC2034 -BOOTSTRAP="runparts /etc/rc.d" - -# shellcheck source=/dev/null -. "$TEST_DIR/lib/setup.sh" - -test_setup() -{ - say "Test start $(date)" - } - test_teardown() { say "Test done $(date)" } +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + while true; do lvl=$(texec initctl runlevel) say "Current runlevel $lvl" diff --git a/test/signal-service.sh b/test/signal-service.sh index 8b9be367..245ea8da 100755 --- a/test/signal-service.sh +++ b/test/signal-service.sh @@ -16,8 +16,6 @@ test_teardown() # shellcheck source=/dev/null . "$TEST_DIR/lib/setup.sh" -say "Test start $(date)" - say 'Ensure file system is cleared' run "rm -f /tmp/usr1.log" diff --git a/test/skel/etc/init.d/S01-service.sh b/test/skel/etc/init.d/S01-service.sh index 23b0dde7..4211b842 100755 --- a/test/skel/etc/init.d/S01-service.sh +++ b/test/skel/etc/init.d/S01-service.sh @@ -6,46 +6,54 @@ PIDFILE="/var/run/service.pid" start() { - printf 'Starting %s: ' "$DAEMON" - start-stop-daemon -S -b -q -p "$PIDFILE" -x "$DAEMON" + printf 'Starting %s: ' "$DAEMON" + start-stop-daemon -S -b -q -p "$PIDFILE" -x "$DAEMON" - status=$? - if [ "$status" -eq 0 ]; then - echo "OK" - else - echo "FAIL" - fi + status=$? + if [ "$status" -eq 0 ]; then + echo "OK" + else + echo "FAIL" + fi - return "$status" + return "$status" } stop() { - printf 'Stopping %s: ' "$DAEMON" - start-stop-daemon -K -q -p "$PIDFILE" + printf 'Stopping %s: ' "$DAEMON" + start-stop-daemon -K -q -p "$PIDFILE" - status=$? - if [ "$status" -eq 0 ]; then - echo "OK" - else - echo "FAIL" - fi + status=$? + if [ "$status" -eq 0 ]; then + echo "OK" + else + echo "FAIL" + fi - return "$status" + return "$status" +} + +reload() +{ + if [ -f "$PIDFILE" ]; then + PID=$(cat "$PIDFILE") + kill -HUP "$PID" + fi } restart() { - stop - sleep 1 - start + stop + sleep 1 + start } case "$1" in - start|stop|restart) - "$1" - ;; - *) - echo "Usage: $0 {start|stop|restart}, got $0 $1" - exit 1 + start|stop|reload|restart) + "$1" + ;; + *) + echo "Usage: $0 {start|stop|reload|restart}, got $0 $1" + exit 1 esac diff --git a/test/skel/sbin/service.sh b/test/skel/sbin/service.sh index 74898bea..1ad4df90 100755 --- a/test/skel/sbin/service.sh +++ b/test/skel/sbin/service.sh @@ -2,23 +2,33 @@ cleanup() { - echo "Got signal, stopping ..." - rm -f /run/service.pid - exit 0 + echo "Got signal, stopping ..." + rm -f /run/service.pid + exit 0 +} + +reload() +{ + echo + echo "Ready." + echo + sleep 1 + echo $$ > /run/service.pid } # Hook SIGUSR1 and dump trace to file system # shellcheck disable=SC2172 trap 'echo USR1 > /tmp/usr1.log' USR1 +trap reload HUP trap cleanup INT trap cleanup TERM trap cleanup QUIT trap cleanup EXIT -echo $$ > /run/service.pid +reload # sleep may exit on known signal, so # we cannot use 'set -e' while true; do - sleep 1 + sleep 1 done diff --git a/test/start-kill-service.sh b/test/start-kill-service.sh index 042e1c4d..4cad2b92 100755 --- a/test/start-kill-service.sh +++ b/test/start-kill-service.sh @@ -17,7 +17,6 @@ test_teardown() # shellcheck source=/dev/null . "$TEST_DIR/lib/setup.sh" -say "Test start $(date)" rm -f "$SYSROOT"/oldpid say "Add service stanza in $FINIT_CONF" diff --git a/test/start-kill-stop.sh b/test/start-kill-stop.sh index fc5aaafe..2e4f4507 100755 --- a/test/start-kill-stop.sh +++ b/test/start-kill-stop.sh @@ -24,7 +24,6 @@ test_teardown() say "Check deps ..." check_dep jq -say "Test start $(date)" rm -f "$SYSROOT"/oldpid say "Add service stanza in $FINIT_CONF" diff --git a/test/start-stop-serv.sh b/test/start-stop-serv.sh index de18387a..bc56cf4b 100755 --- a/test/start-stop-serv.sh +++ b/test/start-stop-serv.sh @@ -3,11 +3,6 @@ set -eu TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/start-stop-service-sub-config.sh b/test/start-stop-service-sub-config.sh index 65b8cc83..0271f6a4 100755 --- a/test/start-stop-service-sub-config.sh +++ b/test/start-stop-service-sub-config.sh @@ -15,8 +15,6 @@ test_teardown() # shellcheck source=/dev/null . "$TEST_DIR/lib/setup.sh" -say "Test start $(date)" - say "Add service stanza in $FINIT_RCSD/service.conf" run "echo 'service [2345] kill:20 log service.sh -- Subserv' > $FINIT_RCSD/service.conf" diff --git a/test/start-stop-service.sh b/test/start-stop-service.sh index dcc4131f..b854f40b 100755 --- a/test/start-stop-service.sh +++ b/test/start-stop-service.sh @@ -15,8 +15,6 @@ test_teardown() # shellcheck source=/dev/null . "$TEST_DIR/lib/setup.sh" -say "Test start $(date)" - say "Add service stanza in $FINIT_CONF" run "echo 'service [2345] kill:20 log service.sh -- Test service' > $FINIT_CONF" diff --git a/test/start-stop-sysv.sh b/test/start-stop-sysv.sh index f20300ee..8060fc17 100755 --- a/test/start-stop-sysv.sh +++ b/test/start-stop-sysv.sh @@ -1,38 +1,60 @@ #!/bin/sh +# shellcheck disable=2034 source=/dev/null set -eu +STANZA1="sysv [2345] pid:!/run/service.pid name:service.sh log:stdout \ + /etc/init.d/S01-service.sh -- SysV test service" +STANZA2="sysv [2345] pid:!/run/service.pid name:service.sh log:stdout \ + reload:'/etc/init.d/S01-service.sh reload' \ + stop:'/etc/init.d/S01-service.sh stop' \ + /etc/init.d/S01-service.sh -- SysV test service" TEST_DIR=$(dirname "$0") +#DEBUG=1 test_teardown() { - say "Test done $(date)" + sep "Test done $(date)" + assert "Running test teardown." "run rm -f $FINIT_RCSD/service.conf" +} - say "Running test teardown." - run "rm -f $FINIT_CONF" +test_one() +{ + say "Add sysv stanza #1 in $FINIT_CONF" + run "echo \"$1\" > $FINIT_RCSD/service.conf" + + say "Reload Finit" + run "initctl reload" + + retry 'assert_num_children 1 service.sh' + retry 'assert_cond service/service.sh/ready' + + sep 'Stop the sysv service' + run "initctl stop service.sh" + + retry 'assert_num_children 0 service.sh' + + sep 'Start the sysv service again' + run "initctl start service.sh" + + retry 'assert_num_children 1 service.sh' + retry 'assert_cond service/service.sh/ready' + + sep 'Touch the sysv service' + run "initctl touch service.conf" + run "initctl reload" + #run "initctl cond dump" + + retry 'assert_num_children 1 service.sh' + retry 'assert_cond service/service.sh/ready' } -# shellcheck source=/dev/null . "$TEST_DIR/lib/setup.sh" -say "Test start $(date)" +sep "―― 1) Basic sysv daemon" +test_one "$STANZA1" -say "Add sysv stanza in $FINIT_CONF" -run "echo 'sysv [2345] pid:!/run/service.pid name:service.sh /etc/init.d/S01-service.sh -- SysV test service' > $FINIT_CONF" - -say 'Reload Finit' -run "initctl reload" - -retry 'assert_num_children 1 service.sh' - -say 'Stop the sysv service' -run "initctl stop service.sh" - -retry 'assert_num_children 0 service.sh' - -say 'Start the sysv service again' -run "initctl start service.sh" - -retry 'assert_num_children 1 service.sh' +sep "―― 2) Custom reload/start script for sysv daemon" +test_one "$STANZA2" return 0 diff --git a/test/svc-env.sh b/test/svc-env.sh index 537342e5..49f8228d 100755 --- a/test/svc-env.sh +++ b/test/svc-env.sh @@ -4,11 +4,6 @@ set -e TEST_DIR=$(dirname "$0") -test_setup() -{ - say "Test start $(date)" -} - test_teardown() { say "Test done $(date)" diff --git a/test/sysvparts.sh b/test/sysvparts.sh index 31c94218..213d8670 100755 --- a/test/sysvparts.sh +++ b/test/sysvparts.sh @@ -8,27 +8,20 @@ # - SNNfoo should be called with a 'start' argument # - KNNfoo should be called with a 'stop' argument # - -TEST_DIR=$(dirname "$0") # shellcheck disable=2034 -DEBUG=1 -# shellcheck disable=SC2034 BOOTSTRAP="runparts sysv /etc/rcS.d" - -# shellcheck source=/dev/null -. "$TEST_DIR/lib/setup.sh" - -test_setup() -{ - say "Test start $(date)" - } +TEST_DIR=$(dirname "$0") +DEBUG=1 test_teardown() { say "Test done $(date)" } +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + while true; do lvl=$(texec initctl runlevel) say "Current runlevel $lvl" diff --git a/test/unexpected-restart.sh b/test/unexpected-restart.sh index d415d8d6..51297aad 100755 --- a/test/unexpected-restart.sh +++ b/test/unexpected-restart.sh @@ -20,7 +20,6 @@ service log:stdout notify:s6 name:C serv -np -i C -N %n service log:stdout notify:none name:D type:forking serv -i D -- D needs A (forking) task name:allup initctl cond set allup -- Everything is up EOF - say "Test start $(date)" } test_teardown()