From 14a40a6d1c9c4199b6056c1524b3832dbc79cb06 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 2 Mar 2025 15:11:29 +0100 Subject: [PATCH] core/main: don't write shutdown OSC context outside of pid1 Follow-up for 98c283131cda67c98946ef373e3bb33aa52de59a --- src/core/main.c | 68 ++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index bee1d356a46..e5b18b164cb 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1514,6 +1514,38 @@ static int write_container_id(void) { return 1; } +static int write_boot_or_shutdown_osc(const char *type) { + int r; + + assert(STRPTR_IN_SET(type, "boot", "shutdown")); + + if (getenv_terminal_is_dumb()) + return 0; + + _cleanup_close_ int fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); + if (fd < 0) + return log_debug_errno(fd, "Failed to open /dev/console to print %s OSC, ignoring: %m", type); + + _cleanup_free_ char *seq = NULL; + if (streq(type, "boot")) + r = osc_context_open_boot(&seq); + else + r = osc_context_close(SD_ID128_ALLF, &seq); + if (r < 0) + return log_debug_errno(r, "Failed to acquire %s OSC sequence, ignoring: %m", type); + + r = loop_write(fd, seq, SIZE_MAX); + if (r < 0) + return log_debug_errno(r, "Failed to write %s OSC sequence, ignoring: %m", type); + + if (DEBUG_LOGGING) { + _cleanup_free_ char *h = cescape(seq); + log_debug("OSC sequence for %s successfully written: %s", type, strna(h)); + } + + return 0; +} + static int bump_unix_max_dgram_qlen(void) { _cleanup_free_ char *qlen = NULL; unsigned long v; @@ -1707,6 +1739,8 @@ static int become_shutdown(int objective, int retval) { if (detect_container() <= 0) (void) cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER); + (void) write_boot_or_shutdown_osc("shutdown"); + execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block); return -errno; } @@ -2385,38 +2419,6 @@ static void log_execution_mode(bool *ret_first_boot) { *ret_first_boot = first_boot; } -static int write_boot_or_shutdown_osc(const char *type) { - int r; - - assert(STRPTR_IN_SET(type, "boot", "shutdown")); - - if (getenv_terminal_is_dumb()) - return 0; - - _cleanup_close_ int fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); - if (fd < 0) - return log_debug_errno(fd, "Failed to open /dev/console to print %s OSC, ignoring: %m", type); - - _cleanup_free_ char *seq = NULL; - if (streq(type, "boot")) - r = osc_context_open_boot(&seq); - else - r = osc_context_close(SD_ID128_ALLF, &seq); - if (r < 0) - return log_debug_errno(r, "Failed to acquire %s OSC sequence, ignoring: %m", type); - - r = loop_write(fd, seq, SIZE_MAX); - if (r < 0) - return log_debug_errno(r, "Failed to write %s OSC sequence, ignoring: %m", type); - - if (DEBUG_LOGGING) { - _cleanup_free_ char *h = cescape(seq); - log_debug("OSC sequence for %s successfully written: %s", type, strna(h)); - } - - return 0; -} - static int initialize_runtime( bool skip_setup, bool first_boot, @@ -3459,8 +3461,6 @@ finish: } #endif - (void) write_boot_or_shutdown_osc("shutdown"); - if (r < 0) (void) sd_notifyf(/* unset_environment= */ false, "ERRNO=%i", -r); -- 2.47.3