Skip to content

Commit a400bd8

Browse files
committed
units: allow and use SuccessAction=exit-force in system systemd-exit.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'
1 parent afa6206 commit a400bd8

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

man/systemd.unit.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,8 @@
872872
Takes one of <option>none</option>, <option>reboot</option>, <option>reboot-force</option>,
873873
<option>reboot-immediate</option>, <option>poweroff</option>, <option>poweroff-force</option>,
874874
<option>poweroff-immediate</option>, <option>exit</option>, and <option>exit-force</option>. In system mode,
875-
all options except <option>exit</option> and <option>exit-force</option> are allowed. In user mode, only
876-
<option>none</option>, <option>exit</option>, and <option>exit-force</option> are allowed. Both options default
877-
to <option>none</option>.</para>
875+
all options are allowed. In user mode, only <option>none</option>, <option>exit</option>, and
876+
<option>exit-force</option> are allowed. Both options default to <option>none</option>.</para>
878877

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

src/core/emergency-action.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "special.h"
1111
#include "string-table.h"
1212
#include "terminal-util.h"
13+
#include "virt.h"
1314

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

7374
case EMERGENCY_ACTION_EXIT:
74-
assert(MANAGER_IS_USER(m));
75-
76-
log_and_status(m, "Exiting", reason);
75+
if (MANAGER_IS_USER(m) || detect_container() > 0) {
76+
log_and_status(m, "Exiting", reason);
77+
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL);
78+
break;
79+
}
7780

78-
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL);
79-
break;
81+
log_notice("Doing \"poweroff\" action instead of an \"exit\" emergency action.");
82+
_fallthrough_;
8083

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

8689
case EMERGENCY_ACTION_EXIT_FORCE:
87-
assert(MANAGER_IS_USER(m));
90+
if (MANAGER_IS_USER(m) || detect_container() > 0) {
91+
log_and_status(m, "Exiting immediately", reason);
92+
m->objective = MANAGER_EXIT;
93+
break;
94+
}
8895

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

9399
case EMERGENCY_ACTION_POWEROFF_FORCE:
94100
log_and_status(m, "Forcibly powering off", reason);
@@ -135,8 +141,7 @@ int parse_emergency_action(
135141
if (x < 0)
136142
return -EINVAL;
137143

138-
if ((system && x >= _EMERGENCY_ACTION_FIRST_USER_ACTION) ||
139-
(!system && x != EMERGENCY_ACTION_NONE && x < _EMERGENCY_ACTION_FIRST_USER_ACTION))
144+
if (!system && x != EMERGENCY_ACTION_NONE && x < _EMERGENCY_ACTION_FIRST_USER_ACTION)
140145
return -EOPNOTSUPP;
141146

142147
*ret = x;

src/test/test-emergency-action.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ static void test_parse_emergency_action(void) {
3636
assert_se(parse_emergency_action("poweroff-force", true, &x) == 0);
3737
assert_se(x == EMERGENCY_ACTION_POWEROFF_FORCE);
3838
assert_se(parse_emergency_action("poweroff-immediate", true, &x) == 0);
39-
assert_se(parse_emergency_action("exit", true, &x) == -EOPNOTSUPP);
40-
assert_se(parse_emergency_action("exit-force", true, &x) == -EOPNOTSUPP);
39+
assert_se(parse_emergency_action("exit", true, &x) == 0);
40+
assert_se(parse_emergency_action("exit-force", true, &x) == 0);
4141
assert_se(parse_emergency_action("exit-forcee", true, &x) == -EINVAL);
42-
assert_se(x == EMERGENCY_ACTION_POWEROFF_IMMEDIATE);
42+
assert_se(x == EMERGENCY_ACTION_EXIT_FORCE);
4343
}
4444

4545
int main(int argc, char **argv) {

units/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ units = [
8585
'multi-user.target.wants/'],
8686
['systemd-coredump.socket', 'ENABLE_COREDUMP',
8787
'sockets.target.wants/'],
88+
['systemd-exit.service', ''],
8889
['systemd-initctl.socket', '',
8990
'sockets.target.wants/'],
9091
['systemd-journal-gatewayd.socket', 'ENABLE_REMOTE HAVE_MICROHTTPD'],
@@ -135,7 +136,6 @@ in_units = [
135136
['systemd-binfmt.service', 'ENABLE_BINFMT',
136137
'sysinit.target.wants/'],
137138
['systemd-coredump@.service', 'ENABLE_COREDUMP'],
138-
['systemd-exit.service', ''],
139139
['systemd-firstboot.service', 'ENABLE_FIRSTBOOT',
140140
'sysinit.target.wants/'],
141141
['systemd-fsck-root.service', ''],
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,4 @@ Documentation=man:systemd.special(7)
1313
DefaultDependencies=no
1414
Requires=shutdown.target
1515
After=shutdown.target
16-
17-
[Service]
18-
Type=oneshot
19-
ExecStart=@SYSTEMCTL@ --force exit
16+
SuccessAction=exit

0 commit comments

Comments
 (0)