From: Yu Watanabe Date: Wed, 18 Dec 2024 02:12:57 +0000 (+0900) Subject: ptyfwd: always write additional line break on stop X-Git-Tag: v258-rc1~1817^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a4e7ec0e99e7f7004bf5cdcd1c715785ff16311;p=thirdparty%2Fsystemd.git ptyfwd: always write additional line break on stop Currently we do that in the user of PTY forwarder, e.g. nspawn. But, let's do that unconditionally in the PTY forwarder. --- diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 7843b9aa630..1f477e682b4 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -1212,7 +1212,6 @@ static int on_machine_removed(sd_bus_message *m, void *userdata, sd_bus_error *r } static int process_forward(sd_event *event, PTYForward **forward, int master, PTYForwardFlags flags, const char *name) { - char last_char = 0; bool machine_died; int r; @@ -1239,17 +1238,12 @@ static int process_forward(sd_event *event, PTYForward **forward, int master, PT if (r < 0) return log_error_errno(r, "Failed to run event loop: %m"); - pty_forward_get_last_char(*forward, &last_char); - machine_died = (flags & PTY_FORWARD_IGNORE_VHANGUP) && pty_forward_get_ignore_vhangup(*forward) == 0; *forward = pty_forward_free(*forward); - if (last_char != '\n') - fputc('\n', stdout); - if (!arg_quiet) { if (machine_died) log_info("Machine %s terminated.", name); diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index ec3bb40c3da..ee04ef8a2b6 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -5740,16 +5740,9 @@ static int run_container( if (r < 0) return log_error_errno(r, "Failed to run event loop: %m"); - if (forward) { - char last_char = 0; - - (void) pty_forward_get_last_char(forward, &last_char); + if (forward) forward = pty_forward_free(forward); - if (!arg_quiet && last_char != '\n') - putc('\n', stdout); - } - /* Kill if it is not dead yet anyway */ if (!arg_register && !arg_keep_unit && bus) terminate_scope(bus, arg_machine); diff --git a/src/run/run.c b/src/run/run.c index e989934c741..6a50b40c159 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -2094,14 +2094,6 @@ static int start_transient_service(sd_bus *bus) { if (r < 0) return log_error_errno(r, "Failed to run event loop: %m"); - if (c.forward) { - char last_char = 0; - - r = pty_forward_get_last_char(c.forward, &last_char); - if (r >= 0 && !arg_quiet && last_char != '\n') - fputc('\n', stdout); - } - if (arg_wait && !arg_quiet) { /* Explicitly destroy the PTY forwarder, so that the PTY device is usable again, with its diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c index 9fc8e30b0c0..401ff8fce58 100644 --- a/src/shared/ptyfwd.c +++ b/src/shared/ptyfwd.c @@ -136,6 +136,19 @@ static void pty_forward_disconnect(PTYForward *f) { (void) loop_write(f->output_fd, ANSI_WINDOW_TITLE_POP, SIZE_MAX); } + if (f->last_char_set && f->last_char != '\n') { + const char *s; + + if (isatty_safe(f->output_fd) && f->last_char != '\r') + s = "\r\n"; + else + s = "\n"; + (void) loop_write(f->output_fd, s, SIZE_MAX); + + f->last_char_set = true; + f->last_char = '\n'; + } + if (f->close_output_fd) f->output_fd = safe_close(f->output_fd); } @@ -994,17 +1007,6 @@ PTYForward* pty_forward_free(PTYForward *f) { return mfree(f); } -int pty_forward_get_last_char(PTYForward *f, char *ret) { - assert(f); - assert(ret); - - if (!f->last_char_set) - return -ENXIO; - - *ret = f->last_char; - return 0; -} - int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) { int r; diff --git a/src/shared/ptyfwd.h b/src/shared/ptyfwd.h index ad020a7ced0..aae8b0b57ac 100644 --- a/src/shared/ptyfwd.h +++ b/src/shared/ptyfwd.h @@ -28,8 +28,6 @@ typedef int (*PTYForwardHandler)(PTYForward *f, int rcode, void *userdata); int pty_forward_new(sd_event *event, int master, PTYForwardFlags flags, PTYForward **ret); PTYForward* pty_forward_free(PTYForward *f); -int pty_forward_get_last_char(PTYForward *f, char *ret); - int pty_forward_set_ignore_vhangup(PTYForward *f, bool ignore_vhangup); bool pty_forward_get_ignore_vhangup(PTYForward *f);