]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ptyfwd: always write additional line break on stop
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 18 Dec 2024 02:12:57 +0000 (11:12 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 18 Dec 2024 11:26:58 +0000 (20:26 +0900)
Currently we do that in the user of PTY forwarder, e.g. nspawn.
But, let's do that unconditionally in the PTY forwarder.

src/machine/machinectl.c
src/nspawn/nspawn.c
src/run/run.c
src/shared/ptyfwd.c
src/shared/ptyfwd.h

index 7843b9aa630d71fd2a4dcc7a238d3dd51511a790..1f477e682b453598ca6591e1343726a7cfa71331 100644 (file)
@@ -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);
index ec3bb40c3da080f924fb5309a3ff6caea99e820c..ee04ef8a2b6fbeaa1501691bd573664d5cac69fb 100644 (file)
@@ -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);
index e989934c741069ee05e9b5ce143b8a9c99bb4940..6a50b40c159e51391b9fe6e9acd8bd6e44a09c50 100644 (file)
@@ -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
index 9fc8e30b0c0a27a0fee829fb72befb5cfc49d9bc..401ff8fce58e9e9ed68c066124a414a8fe370e3c 100644 (file)
@@ -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;
 
index ad020a7ced01edd2fd9ba3698b1245aa5e240534..aae8b0b57ac45fcee96294d5cf86d526a271eb85 100644 (file)
@@ -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);