Skip to content

Commit

Permalink
units: allow and use SuccessAction=exit-force in system systemd-exit.…
Browse files Browse the repository at this point in the history
…service

C.f. 287419c: 'systemctl exit 42' can be
used to set an exit value and pulls in exit.target, which pulls in systemd-exit.service,
which calls org.fdo.Manager.Exit, which calls method_exit(), which sets the objective
to MANAGER_EXIT. Allow the same to happen through SuccessAction=exit.

v2: update for 'exit' and 'exit-force'
  • Loading branch information
keszybz committed Oct 17, 2018
1 parent afa6206 commit a400bd8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
7 changes: 3 additions & 4 deletions man/systemd.unit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -872,9 +872,8 @@
Takes one of <option>none</option>, <option>reboot</option>, <option>reboot-force</option>,
<option>reboot-immediate</option>, <option>poweroff</option>, <option>poweroff-force</option>,
<option>poweroff-immediate</option>, <option>exit</option>, and <option>exit-force</option>. In system mode,
all options except <option>exit</option> and <option>exit-force</option> are allowed. In user mode, only
<option>none</option>, <option>exit</option>, and <option>exit-force</option> are allowed. Both options default
to <option>none</option>.</para>
all options are allowed. In user mode, only <option>none</option>, <option>exit</option>, and
<option>exit-force</option> are allowed. Both options default to <option>none</option>.</para>

<para>If <option>none</option> is set, no action will be triggered. <option>reboot</option> causes a reboot
following the normal shutdown procedure (i.e. equivalent to <command>systemctl reboot</command>).
Expand All @@ -884,7 +883,7 @@
<citerefentry><refentrytitle>reboot</refentrytitle><manvolnum>2</manvolnum></citerefentry> system call, which
might result in data loss. Similarly, <option>poweroff</option>, <option>poweroff-force</option>,
<option>poweroff-immediate</option> have the effect of powering down the system with similar
semantics. <option>exit</option> causes the user manager to exit following the normal shutdown procedure, and
semantics. <option>exit</option> causes the manager to exit following the normal shutdown procedure, and
<option>exit-force</option> causes it terminate without shutting down services.</para></listitem>
</varlistentry>

Expand Down
27 changes: 16 additions & 11 deletions src/core/emergency-action.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "special.h"
#include "string-table.h"
#include "terminal-util.h"
#include "virt.h"

static void log_and_status(Manager *m, const char *message, const char *reason) {
log_warning("%s: %s", message, reason);
Expand Down Expand Up @@ -71,24 +72,29 @@ int emergency_action(
break;

case EMERGENCY_ACTION_EXIT:
assert(MANAGER_IS_USER(m));

log_and_status(m, "Exiting", reason);
if (MANAGER_IS_USER(m) || detect_container() > 0) {
log_and_status(m, "Exiting", reason);
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL);
break;
}

(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL);
break;
log_notice("Doing \"poweroff\" action instead of an \"exit\" emergency action.");
_fallthrough_;

case EMERGENCY_ACTION_POWEROFF:
log_and_status(m, "Powering off", reason);
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL);
break;

case EMERGENCY_ACTION_EXIT_FORCE:
assert(MANAGER_IS_USER(m));
if (MANAGER_IS_USER(m) || detect_container() > 0) {
log_and_status(m, "Exiting immediately", reason);
m->objective = MANAGER_EXIT;
break;
}

log_and_status(m, "Exiting immediately", reason);
m->objective = MANAGER_EXIT;
break;
log_notice("Doing \"poweroff-force\" action instead of an \"exit-force\" emergency action.");
_fallthrough_;

case EMERGENCY_ACTION_POWEROFF_FORCE:
log_and_status(m, "Forcibly powering off", reason);
Expand Down Expand Up @@ -135,8 +141,7 @@ int parse_emergency_action(
if (x < 0)
return -EINVAL;

if ((system && x >= _EMERGENCY_ACTION_FIRST_USER_ACTION) ||
(!system && x != EMERGENCY_ACTION_NONE && x < _EMERGENCY_ACTION_FIRST_USER_ACTION))
if (!system && x != EMERGENCY_ACTION_NONE && x < _EMERGENCY_ACTION_FIRST_USER_ACTION)
return -EOPNOTSUPP;

*ret = x;
Expand Down
6 changes: 3 additions & 3 deletions src/test/test-emergency-action.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ static void test_parse_emergency_action(void) {
assert_se(parse_emergency_action("poweroff-force", true, &x) == 0);
assert_se(x == EMERGENCY_ACTION_POWEROFF_FORCE);
assert_se(parse_emergency_action("poweroff-immediate", true, &x) == 0);
assert_se(parse_emergency_action("exit", true, &x) == -EOPNOTSUPP);
assert_se(parse_emergency_action("exit-force", true, &x) == -EOPNOTSUPP);
assert_se(parse_emergency_action("exit", true, &x) == 0);
assert_se(parse_emergency_action("exit-force", true, &x) == 0);
assert_se(parse_emergency_action("exit-forcee", true, &x) == -EINVAL);
assert_se(x == EMERGENCY_ACTION_POWEROFF_IMMEDIATE);
assert_se(x == EMERGENCY_ACTION_EXIT_FORCE);
}

int main(int argc, char **argv) {
Expand Down
2 changes: 1 addition & 1 deletion units/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ units = [
'multi-user.target.wants/'],
['systemd-coredump.socket', 'ENABLE_COREDUMP',
'sockets.target.wants/'],
['systemd-exit.service', ''],
['systemd-initctl.socket', '',
'sockets.target.wants/'],
['systemd-journal-gatewayd.socket', 'ENABLE_REMOTE HAVE_MICROHTTPD'],
Expand Down Expand Up @@ -135,7 +136,6 @@ in_units = [
['systemd-binfmt.service', 'ENABLE_BINFMT',
'sysinit.target.wants/'],
['systemd-coredump@.service', 'ENABLE_COREDUMP'],
['systemd-exit.service', ''],
['systemd-firstboot.service', 'ENABLE_FIRSTBOOT',
'sysinit.target.wants/'],
['systemd-fsck-root.service', ''],
Expand Down
5 changes: 1 addition & 4 deletions units/systemd-exit.service.in → units/systemd-exit.service
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,4 @@ Documentation=man:systemd.special(7)
DefaultDependencies=no
Requires=shutdown.target
After=shutdown.target

[Service]
Type=oneshot
ExecStart=@SYSTEMCTL@ --force exit
SuccessAction=exit

0 comments on commit a400bd8

Please sign in to comment.