Blob Blame History Raw
From 98e5c02b1602eaaac5c63045fa7a06e40249445e Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Wed, 18 Feb 2015 23:32:55 +0100
Subject: [PATCH] nspawn: when connected to pipes for stdin/stdout, pass them
 as-is to PID 1

Previously we always invoked the container PID 1 on /dev/console of the
container. With this change we do so only if nspawn was invoked
interactively (i.e. its stdin/stdout was connected to a TTY). In all other
cases we directly pass through the fds unmodified.

This has the benefit that nspawn can be added into shell pipelines.

https://bugs.freedesktop.org/show_bug.cgi?id=87732

Cherry-picked from: 9c857b9d160c10b4454fc9f83442c1878343422f
Resolves: #1307080
---
 src/machine/machinectl.c |  2 +-
 src/nspawn/nspawn.c      | 48 +++++++++++++++++--------------
 src/run/run.c            |  2 +-
 src/shared/ptyfwd.c      | 75 ++++++++++++++++++++++++++++--------------------
 src/shared/ptyfwd.h      |  2 +-
 5 files changed, 74 insertions(+), 55 deletions(-)

diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index cfd3162..1a58aea 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -1427,7 +1427,7 @@ static int login_machine(int argc, char *argv[], void *userdata) {
         sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
         sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
 
-        r = pty_forward_new(event, master, true, &forward);
+        r = pty_forward_new(event, master, true, false, &forward);
         if (r < 0)
                 return log_error_errno(r, "Failed to create PTY forwarder: %m");
 
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 78bd584..a37b640 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -3581,6 +3581,7 @@ int main(int argc, char *argv[]) {
         int ret = EXIT_SUCCESS;
         union in_addr_union exposed = {};
         _cleanup_release_lock_file_ LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
+        bool interactive;
 
         log_parse_environment();
         log_open();
@@ -3754,6 +3755,8 @@ int main(int argc, char *argv[]) {
                         goto finish;
         }
 
+        interactive = isatty(STDIN_FILENO) > 0 && isatty(STDOUT_FILENO) > 0;
+
         master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
         if (master < 0) {
                 r = log_error_errno(errno, "Failed to acquire pseudo tty: %m");
@@ -3766,15 +3769,15 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (!arg_quiet)
-                log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
-                         arg_machine, arg_image ?: arg_directory);
-
         if (unlockpt(master) < 0) {
                 r = log_error_errno(errno, "Failed to unlock tty: %m");
                 goto finish;
         }
 
+        if (!arg_quiet)
+                log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
+                         arg_machine, arg_image ?: arg_directory);
+
         assert_se(sigemptyset(&mask) == 0);
         sigset_add_many(&mask, SIGCHLD, SIGWINCH, SIGTERM, SIGINT, -1);
         assert_se(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
@@ -3860,9 +3863,6 @@ int main(int argc, char *argv[]) {
 
                         master = safe_close(master);
 
-                        close_nointr(STDIN_FILENO);
-                        close_nointr(STDOUT_FILENO);
-                        close_nointr(STDERR_FILENO);
 
                         kmsg_socket_pair[0] = safe_close(kmsg_socket_pair[0]);
                         rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
@@ -3870,21 +3870,27 @@ int main(int argc, char *argv[]) {
                         reset_all_signal_handlers();
                         reset_signal_mask();
 
-                        r = open_terminal(console, O_RDWR);
-                        if (r != STDIN_FILENO) {
-                                if (r >= 0) {
-                                        safe_close(r);
-                                        r = -EINVAL;
-                                }
+                        if (interactive) {
+                                close_nointr(STDIN_FILENO);
+                                close_nointr(STDOUT_FILENO);
+                                close_nointr(STDERR_FILENO);
 
-                                log_error_errno(r, "Failed to open console: %m");
-                                _exit(EXIT_FAILURE);
-                        }
+                                r = open_terminal(console, O_RDWR);
+                                if (r != STDIN_FILENO) {
+                                        if (r >= 0) {
+                                                safe_close(r);
+                                                r = -EINVAL;
+                                        }
 
-                        if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO ||
-                            dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) {
-                                log_error_errno(errno, "Failed to duplicate console: %m");
-                                _exit(EXIT_FAILURE);
+                                        log_error_errno(r, "Failed to open console: %m");
+                                        _exit(EXIT_FAILURE);
+                                }
+
+                                if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO ||
+                                    dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) {
+                                        log_error_errno(errno, "Failed to duplicate console: %m");
+                                        _exit(EXIT_FAILURE);
+                                }
                         }
 
                         if (setsid() < 0) {
@@ -4227,7 +4233,7 @@ int main(int argc, char *argv[]) {
 
                                 rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
 
-                                r = pty_forward_new(event, master, true, &forward);
+                                r = pty_forward_new(event, master, true, !interactive, &forward);
                                 if (r < 0) {
                                         log_error_errno(r, "Failed to create PTY forwarder: %m");
                                         goto finish;
diff --git a/src/run/run.c b/src/run/run.c
index dd1338f..4680342 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -780,7 +780,7 @@ static int start_transient_service(
                 if (!arg_quiet)
                         log_info("Running as unit %s.\nPress ^] three times within 1s to disconnect TTY.", service);
 
-                r = pty_forward_new(event, master, false, &forward);
+                r = pty_forward_new(event, master, false, false, &forward);
                 if (r < 0)
                         return log_error_errno(r, "Failed to create PTY forwarder: %m");
 
diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c
index 88b3f4e..4402af1 100644
--- a/src/shared/ptyfwd.c
+++ b/src/shared/ptyfwd.c
@@ -42,6 +42,8 @@ struct PTYForward {
         struct termios saved_stdin_attr;
         struct termios saved_stdout_attr;
 
+        bool read_only:1;
+
         bool saved_stdin:1;
         bool saved_stdout:1;
 
@@ -298,7 +300,13 @@ static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *
         return 0;
 }
 
-int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward **ret) {
+int pty_forward_new(
+                sd_event *event,
+                int master,
+                bool ignore_vhangup,
+                bool read_only,
+                PTYForward **ret) {
+
         _cleanup_(pty_forward_freep) PTYForward *f = NULL;
         struct winsize ws;
         int r;
@@ -307,6 +315,7 @@ int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward
         if (!f)
                 return -ENOMEM;
 
+        f->read_only = read_only;
         f->ignore_vhangup = ignore_vhangup;
 
         if (event)
@@ -317,13 +326,15 @@ int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward
                         return r;
         }
 
-        r = fd_nonblock(STDIN_FILENO, true);
-        if (r < 0)
-                return r;
+        if (!read_only) {
+                r = fd_nonblock(STDIN_FILENO, true);
+                if (r < 0)
+                        return r;
 
-        r = fd_nonblock(STDOUT_FILENO, true);
-        if (r < 0)
-                return r;
+                r = fd_nonblock(STDOUT_FILENO, true);
+                if (r < 0)
+                        return r;
+        }
 
         r = fd_nonblock(master, true);
         if (r < 0)
@@ -334,36 +345,34 @@ int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward
         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
                 (void)ioctl(master, TIOCSWINSZ, &ws);
 
-        if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
-                struct termios raw_stdin_attr;
-
-                f->saved_stdin = true;
+        if (!read_only) {
+                if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
+                        struct termios raw_stdin_attr;
 
-                raw_stdin_attr = f->saved_stdin_attr;
-                cfmakeraw(&raw_stdin_attr);
-                raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
-                tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr);
-        }
+                        f->saved_stdin = true;
 
-        if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) {
-                struct termios raw_stdout_attr;
+                        raw_stdin_attr = f->saved_stdin_attr;
+                        cfmakeraw(&raw_stdin_attr);
+                        raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
+                        tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr);
+                }
 
-                f->saved_stdout = true;
+                if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) {
+                        struct termios raw_stdout_attr;
 
-                raw_stdout_attr = f->saved_stdout_attr;
-                cfmakeraw(&raw_stdout_attr);
-                raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
-                raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
-                tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr);
-        }
+                        f->saved_stdout = true;
 
-        r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
-        if (r < 0)
-                return r;
+                        raw_stdout_attr = f->saved_stdout_attr;
+                        cfmakeraw(&raw_stdout_attr);
+                        raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
+                        raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
+                        tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr);
+                }
 
-        r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f);
-        if (r < 0 && r != -EPERM)
-                return r;
+                r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f);
+                if (r < 0 && r != -EPERM)
+                        return r;
+        }
 
         r = sd_event_add_io(f->event, &f->stdout_event_source, STDOUT_FILENO, EPOLLOUT|EPOLLET, on_stdout_event, f);
         if (r == -EPERM)
@@ -372,6 +381,10 @@ int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward
         else if (r < 0)
                 return r;
 
+        r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
+        if (r < 0)
+                return r;
+
         r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f);
         if (r < 0)
                 return r;
diff --git a/src/shared/ptyfwd.h b/src/shared/ptyfwd.h
index d3e229b..6208a54 100644
--- a/src/shared/ptyfwd.h
+++ b/src/shared/ptyfwd.h
@@ -30,7 +30,7 @@
 
 typedef struct PTYForward PTYForward;
 
-int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward **f);
+int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, bool read_only, PTYForward **f);
 PTYForward *pty_forward_free(PTYForward *f);
 
 int pty_forward_get_last_char(PTYForward *f, char *ch);