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.
This commit is contained in:
Lennart Poettering
2014-07-03 12:47:40 +02:00
parent ffd488e272
commit 37520c1bec
6 changed files with 46 additions and 27 deletions

View File

@@ -857,7 +857,7 @@ ExecStart=/bin/echo $ONE $TWO ${TWO}</programlisting>
definitions can either be numeric exit
codes or termination signal names,
separated by spaces. For example:
<programlisting>SuccessExitStatus=1 2 8 <constant>SIGKILL</constant></programlisting>
<programlisting>SuccessExitStatus=1 2 8 SIGKILL</programlisting>
ensures that exit codes 1, 2, 8 and
the termination signal
<constant>SIGKILL</constant> are
@@ -897,9 +897,8 @@ ExecStart=/bin/echo $ONE $TWO ${TWO}</programlisting>
spaces. Defaults to the empty list, so
that, by default, no exit status is
excluded from the configured restart
logic. Example:
<literal>RestartPreventExitStatus=1 6
SIGABRT</literal>, ensures that exit
logic. For example:
<programlisting>RestartPreventExitStatus=1 6 SIGABRT</programlisting> ensures that exit
codes 1 and 6 and the termination
signal <constant>SIGABRT</constant> will
not result in automatic service
@@ -913,6 +912,18 @@ ExecStart=/bin/echo $ONE $TWO ${TWO}</programlisting>
effect.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>RestartForceExitStatus=</varname></term>
<listitem><para>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
<varname>Restart=</varname>. The
argument format is similar to
<varname>RestartPreventExitStatus=</varname>.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>PermissionsStartOnly=</varname></term>
<listitem><para>Takes a boolean

View File

@@ -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)',

View File

@@ -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:

View File

@@ -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 */

View File

@@ -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;
}

View File

@@ -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);