From 4e0c16abd5af87de05f9719b03379f4535e29556 Mon Sep 17 00:00:00 2001 From: Mark Hindley Date: Sat, 14 Sep 2024 16:27:31 +0100 Subject: [PATCH] openrc-run: Use SIGUSR1 to skip marking. The current code for mark_service comments that a service marking itself should make openrc-run skip marking it based on the exit code, but openrc-run does not respect the signal. --- src/mark_service/mark_service.c | 2 +- src/openrc-run/openrc-run.c | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/mark_service/mark_service.c b/src/mark_service/mark_service.c index 7d4d9d976..e9239c04d 100644 --- a/src/mark_service/mark_service.c +++ b/src/mark_service/mark_service.c @@ -63,7 +63,7 @@ int main(int argc, char **argv) if (ok && svcname && strcmp(svcname, service) == 0) { openrc_pid = getenv("RC_OPENRC_PID"); if (openrc_pid && sscanf(openrc_pid, "%d", &pid) == 1) - if (kill(pid, SIGHUP) != 0) + if (kill(pid, SIGUSR1) != 0) eerror("%s: failed to signal parent %d: %s", applet, pid, strerror(errno)); diff --git a/src/openrc-run/openrc-run.c b/src/openrc-run/openrc-run.c index 7c5d50a1b..da223368c 100644 --- a/src/openrc-run/openrc-run.c +++ b/src/openrc-run/openrc-run.c @@ -92,7 +92,7 @@ static RC_STRINGLIST *use_services; static RC_STRINGLIST *want_services; static RC_HOOK hook_out; static int exclusive_fd = -1, master_tty = -1; -static bool sighup, in_background, deps, dry_run; +static bool sighup, skip_mark, in_background, deps, dry_run; static pid_t service_pid; static int signal_pipe[2] = { -1, -1 }; @@ -117,6 +117,10 @@ handle_signal(int sig) sighup = true; break; + case SIGUSR1: + skip_mark = true; + break; + case SIGCHLD: while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { if (signal_pipe[1] > -1 && pid == service_pid) { @@ -802,6 +806,7 @@ static void svc_start_real(void) setenv("IN_BACKGROUND", ibsave, 1); hook_out = RC_HOOK_SERVICE_START_DONE; rc_plugin_run(RC_HOOK_SERVICE_START_NOW, applet); + skip_mark = false; started = (svc_exec("start", NULL) == 0); if (ibsave) unsetenv("IN_BACKGROUND"); @@ -811,7 +816,8 @@ static void svc_start_real(void) else if (!started) eerrorx("ERROR: %s failed to start", applet); - rc_service_mark(service, RC_SERVICE_STARTED); + if (!skip_mark) + rc_service_mark(service, RC_SERVICE_STARTED); exclusive_fd = svc_unlock(applet, exclusive_fd); hook_out = RC_HOOK_SERVICE_START_OUT; rc_plugin_run(RC_HOOK_SERVICE_START_DONE, applet); @@ -1008,6 +1014,7 @@ svc_stop_real(void) setenv("IN_BACKGROUND", ibsave, 1); hook_out = RC_HOOK_SERVICE_STOP_DONE; rc_plugin_run(RC_HOOK_SERVICE_STOP_NOW, applet); + skip_mark = false; stopped = (svc_exec("stop", NULL) == 0); if (ibsave) unsetenv("IN_BACKGROUND"); @@ -1015,11 +1022,12 @@ svc_stop_real(void) if (!stopped) eerrorx("ERROR: %s failed to stop", applet); - if (in_background) - rc_service_mark(service, RC_SERVICE_INACTIVE); - else - rc_service_mark(service, RC_SERVICE_STOPPED); - + if (!skip_mark) { + if (in_background) + rc_service_mark(service, RC_SERVICE_INACTIVE); + else + rc_service_mark(service, RC_SERVICE_STOPPED); + } hook_out = RC_HOOK_SERVICE_STOP_OUT; rc_plugin_run(RC_HOOK_SERVICE_STOP_DONE, applet); hook_out = 0; @@ -1275,6 +1283,7 @@ int main(int argc, char **argv) /* Setup a signal handler */ signal_setup(SIGHUP, handle_signal); + signal_setup(SIGUSR1, handle_signal); signal_setup(SIGINT, handle_signal); signal_setup(SIGQUIT, handle_signal); signal_setup(SIGTERM, handle_signal);