Merge pull request #32428 from poettering/sd-notify-reboot-param

pid1: send shutdown type and reboot argument to supervisor via sd_notify()
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2024-04-23 13:31:40 +02:00 committed by GitHub
commit 1b47cfab7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 47 additions and 7 deletions

3
TODO
View file

@ -130,6 +130,9 @@ Deprecations and removals:
Features:
* Clean up "reboot argument" handling, i.e. set it through some IPC service
instead of directly via /run/, so that it can be sensible set remotely.
* userdb: add concept for user "aliases", to cover for cases where you can log
in under the name lennart@somenetworkfsserver, and it would automatically
generate a local user, and from the one both names can be used to allow

View file

@ -1257,6 +1257,19 @@
details.</para>
<xi:include href="version-info.xml" xpointer="v256"/></listitem>
<listitem><para>An <varname>X_SYSTEMD_SHUTDOWN=…</varname> message will be sent out very shortly before
the system shuts down. The value is one of the strings <literal>reboot</literal>,
<literal>halt</literal>, <literal>poweroff</literal>, <literal>kexec</literal> and indicates which kind
of shutdown is being executed.</para>
<xi:include href="version-info.xml" xpointer="v256"/></listitem>
<listitem><para>An <varname>X_SYSTEMD_REBOOT_PARAMETER=…</varname> message will also be sent out very
shortly before the system shuts down. Its value is the reboot argument as configured with
<command>systemctl --reboot-argument=…</command>.</para>
<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</itemizedlist>
<para>Note that these extension fields are sent in addition to the regular <literal>READY=1</literal> and

View file

@ -333,6 +333,26 @@ static void init_watchdog(void) {
}
}
static void notify_supervisor(void) {
/* Notify VMM/container manager of the desired mode of reboot and the boot parameter */
_cleanup_free_ char *reboot_parameter = NULL;
int r;
r = read_reboot_parameter(&reboot_parameter);
if (r < 0 && r != -ENOENT)
log_debug_errno(r, "Failed to read reboot parameter, ignoring: %m");
if (reboot_parameter)
(void) sd_notifyf(/* unset_environment= */ false,
"X_SYSTEMD_SHUTDOWN=%s\n"
"X_SYSTEMD_REBOOT_PARAMETER=%s",
arg_verb, reboot_parameter);
else
(void) sd_notifyf(/* unset_environment= */ false,
"X_SYSTEMD_SHUTDOWN=%s",
arg_verb);
}
int main(int argc, char *argv[]) {
static const char* const dirs[] = {
SYSTEM_SHUTDOWN_PATH,
@ -589,6 +609,8 @@ int main(int argc, char *argv[]) {
if (!in_container)
sync_with_progress();
notify_supervisor();
if (streq(arg_verb, "exit")) {
if (in_container) {
log_info("Exiting container.");

View file

@ -155,14 +155,16 @@ int verb_start_special(int argc, char *argv[], void *userdata) {
return r;
}
if (a == ACTION_REBOOT) {
if (arg_reboot_argument) {
r = update_reboot_parameter_and_warn(arg_reboot_argument, false);
if (r < 0)
return r;
}
if (arg_reboot_argument && IN_SET(a, ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT, ACTION_KEXEC)) {
/* If we are going through an action that involves systemd-shutdown, let's set the reboot
* parameter, even if it's not a regular reboot. After all we nowadays send the string to
* our supervisor via sd_notify() too. */
r = update_reboot_parameter_and_warn(arg_reboot_argument, /* keep= */ false);
if (r < 0)
return r;
}
} else if (a == ACTION_KEXEC) {
if (a == ACTION_KEXEC) {
r = load_kexec_kernel();
if (r < 0 && arg_force >= 1)
log_notice("Failed to load kexec kernel, continuing without.");