From 37520c1bec9a92adbe02fceaece588a7aa2fea2b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 3 Jul 2014 12:47:40 +0200 Subject: [PATCH] core: introduce new RestartForceExitStatus= service setting This does the inverse of RestartPreventExitStatus=: it forces a restart of a service when a certain exit status is returned by a service process. --- man/systemd.service.xml | 19 ++++++++++++++---- src/core/load-fragment-gperf.gperf.m4 | 3 ++- src/core/service.c | 38 ++++++++++++++++------------------- src/core/service.h | 3 ++- src/shared/exit-status.c | 8 ++++++++ src/shared/exit-status.h | 2 ++ 6 files changed, 46 insertions(+), 27 deletions(-) diff --git a/man/systemd.service.xml b/man/systemd.service.xml index 1d80480..7f7d49e 100644 --- a/man/systemd.service.xml +++ b/man/systemd.service.xml @@ -857,7 +857,7 @@ ExecStart=/bin/echo $ONE $TWO ${TWO} definitions can either be numeric exit codes or termination signal names, separated by spaces. For example: - SuccessExitStatus=1 2 8 SIGKILL + SuccessExitStatus=1 2 8 SIGKILL ensures that exit codes 1, 2, 8 and the termination signal SIGKILL are @@ -897,9 +897,8 @@ ExecStart=/bin/echo $ONE $TWO ${TWO} spaces. Defaults to the empty list, so that, by default, no exit status is excluded from the configured restart - logic. Example: - RestartPreventExitStatus=1 6 - SIGABRT, ensures that exit + logic. For example: + RestartPreventExitStatus=1 6 SIGABRT ensures that exit codes 1 and 6 and the termination signal SIGABRT will not result in automatic service @@ -914,6 +913,18 @@ ExecStart=/bin/echo $ONE $TWO ${TWO} + RestartForceExitStatus= + Takes a list of exit + status definitions that when returned + by the main service process will force + automatic service restarts, regardless + of the restart setting configured with + Restart=. The + argument format is similar to + RestartPreventExitStatus=. + + + PermissionsStartOnly= Takes a boolean argument. If true, the permission-related diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4 index 4f3731b..a7c4469 100644 --- a/src/core/load-fragment-gperf.gperf.m4 +++ b/src/core/load-fragment-gperf.gperf.m4 @@ -194,7 +194,8 @@ Service.PermissionsStartOnly, config_parse_bool, 0, Service.RootDirectoryStartOnly, config_parse_bool, 0, offsetof(Service, root_directory_start_only) Service.RemainAfterExit, config_parse_bool, 0, offsetof(Service, remain_after_exit) Service.GuessMainPID, config_parse_bool, 0, offsetof(Service, guess_main_pid) -Service.RestartPreventExitStatus, config_parse_set_status, 0, offsetof(Service, restart_ignore_status) +Service.RestartPreventExitStatus, config_parse_set_status, 0, offsetof(Service, restart_prevent_status) +Service.RestartForceExitStatus, config_parse_set_status, 0, offsetof(Service, restart_force_status) Service.SuccessExitStatus, config_parse_set_status, 0, offsetof(Service, success_status) m4_ifdef(`HAVE_SYSV_COMPAT', `Service.SysVStartPriority, config_parse_sysv_priority, 0, offsetof(Service, sysv_start_priority)', diff --git a/src/core/service.c b/src/core/service.c index 10dc79c..e221899 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -266,15 +266,9 @@ static void service_done(Unit *u) { s->control_command = NULL; s->main_command = NULL; - set_free(s->restart_ignore_status.code); - s->restart_ignore_status.code = NULL; - set_free(s->restart_ignore_status.signal); - s->restart_ignore_status.signal = NULL; - - set_free(s->success_status.code); - s->success_status.code = NULL; - set_free(s->success_status.signal); - s->success_status.signal = NULL; + exit_status_set_free(&s->restart_prevent_status); + exit_status_set_free(&s->restart_force_status); + exit_status_set_free(&s->success_status); /* This will leak a process, but at least no memory or any of * our resources */ @@ -337,7 +331,12 @@ static int service_verify(Service *s) { } if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) { - log_error_unit(UNIT(s)->id, "%s has Restart setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id); + log_error_unit(UNIT(s)->id, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id); + return -EINVAL; + } + + if (s->type == SERVICE_ONESHOT && !(set_isempty(s->restart_force_status.signal) && set_isempty(s->restart_force_status.code))) { + log_error_unit(UNIT(s)->id, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id); return -EINVAL; } @@ -1071,11 +1070,11 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) || (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) || (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) || - (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP))) && - (s->result != SERVICE_FAILURE_EXIT_CODE || - !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) && - (s->result != SERVICE_FAILURE_SIGNAL || - !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))) { + (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) || + (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.code, INT_TO_PTR(s->main_exec_status.status))) || + (IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) && set_contains(s->restart_force_status.signal, INT_TO_PTR(s->main_exec_status.status)))) && + (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.code, INT_TO_PTR(s->main_exec_status.status))) && + (!IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) || !set_contains(s->restart_prevent_status.signal, INT_TO_PTR(s->main_exec_status.status)))) { r = service_arm_timer(s, s->restart_usec); if (r < 0) @@ -1421,8 +1420,7 @@ static void service_enter_start_pre(Service *s) { return; fail: - log_warning_unit(UNIT(s)->id, - "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r)); service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); } @@ -1434,8 +1432,7 @@ static void service_enter_restart(Service *s) { if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) { /* Don't restart things if we are going down anyway */ - log_info_unit(UNIT(s)->id, - "Stop job pending for unit, delaying automatic restart."); + log_info_unit(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart."); r = service_arm_timer(s, s->restart_usec); if (r < 0) @@ -1456,8 +1453,7 @@ static void service_enter_restart(Service *s) { * it will be canceled as part of the service_stop() call that * is executed as part of JOB_RESTART. */ - log_debug_unit(UNIT(s)->id, - "%s scheduled restart job.", UNIT(s)->id); + log_debug_unit(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id); return; fail: diff --git a/src/core/service.h b/src/core/service.h index b8f0e0c..7406d90 100644 --- a/src/core/service.h +++ b/src/core/service.h @@ -118,7 +118,8 @@ struct Service { ServiceType type; ServiceRestart restart; - ExitStatusSet restart_ignore_status; + ExitStatusSet restart_prevent_status; + ExitStatusSet restart_force_status; ExitStatusSet success_status; /* If set we'll read the main daemon PID from this file */ diff --git a/src/shared/exit-status.c b/src/shared/exit-status.c index ce1f1bd..38d71e1 100644 --- a/src/shared/exit-status.c +++ b/src/shared/exit-status.c @@ -208,3 +208,11 @@ bool is_clean_exit_lsb(int code, int status, ExitStatusSet *success_status) { code == CLD_EXITED && (status == EXIT_NOTINSTALLED || status == EXIT_NOTCONFIGURED); } + +void exit_status_set_free(ExitStatusSet *x) { + assert(x); + + set_free(x->code); + set_free(x->signal); + x->code = x->signal = NULL; +} diff --git a/src/shared/exit-status.h b/src/shared/exit-status.h index 57d066f..93abf7f 100644 --- a/src/shared/exit-status.h +++ b/src/shared/exit-status.h @@ -95,3 +95,5 @@ const char* exit_status_to_string(ExitStatus status, ExitStatusLevel level) _con bool is_clean_exit(int code, int status, ExitStatusSet *success_status); bool is_clean_exit_lsb(int code, int status, ExitStatusSet *success_status); + +void exit_status_set_free(ExitStatusSet *x); -- 2.7.4