anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
803fb7
From 98e5c02b1602eaaac5c63045fa7a06e40249445e Mon Sep 17 00:00:00 2001
803fb7
From: Lennart Poettering <lennart@poettering.net>
803fb7
Date: Wed, 18 Feb 2015 23:32:55 +0100
803fb7
Subject: [PATCH] nspawn: when connected to pipes for stdin/stdout, pass them
803fb7
 as-is to PID 1
803fb7
803fb7
Previously we always invoked the container PID 1 on /dev/console of the
803fb7
container. With this change we do so only if nspawn was invoked
803fb7
interactively (i.e. its stdin/stdout was connected to a TTY). In all other
803fb7
cases we directly pass through the fds unmodified.
803fb7
803fb7
This has the benefit that nspawn can be added into shell pipelines.
803fb7
803fb7
https://bugs.freedesktop.org/show_bug.cgi?id=87732
803fb7
803fb7
Cherry-picked from: 9c857b9d160c10b4454fc9f83442c1878343422f
803fb7
Resolves: #1307080
803fb7
---
803fb7
 src/machine/machinectl.c |  2 +-
de8967
 src/nspawn/nspawn.c      | 48 ++++++++++++++-----------
803fb7
 src/run/run.c            |  2 +-
de8967
 src/shared/ptyfwd.c      | 75 +++++++++++++++++++++++-----------------
803fb7
 src/shared/ptyfwd.h      |  2 +-
803fb7
 5 files changed, 74 insertions(+), 55 deletions(-)
803fb7
803fb7
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
803fb7
index cfd3162fb..1a58aeaf1 100644
803fb7
--- a/src/machine/machinectl.c
803fb7
+++ b/src/machine/machinectl.c
803fb7
@@ -1427,7 +1427,7 @@ static int login_machine(int argc, char *argv[], void *userdata) {
803fb7
         sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
803fb7
         sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
803fb7
 
803fb7
-        r = pty_forward_new(event, master, true, &forward);
803fb7
+        r = pty_forward_new(event, master, true, false, &forward);
803fb7
         if (r < 0)
803fb7
                 return log_error_errno(r, "Failed to create PTY forwarder: %m");
803fb7
 
803fb7
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
803fb7
index 78bd58483..a37b64094 100644
803fb7
--- a/src/nspawn/nspawn.c
803fb7
+++ b/src/nspawn/nspawn.c
803fb7
@@ -3581,6 +3581,7 @@ int main(int argc, char *argv[]) {
803fb7
         int ret = EXIT_SUCCESS;
803fb7
         union in_addr_union exposed = {};
803fb7
         _cleanup_release_lock_file_ LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
803fb7
+        bool interactive;
803fb7
 
803fb7
         log_parse_environment();
803fb7
         log_open();
803fb7
@@ -3754,6 +3755,8 @@ int main(int argc, char *argv[]) {
803fb7
                         goto finish;
803fb7
         }
803fb7
 
803fb7
+        interactive = isatty(STDIN_FILENO) > 0 && isatty(STDOUT_FILENO) > 0;
803fb7
+
803fb7
         master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
803fb7
         if (master < 0) {
803fb7
                 r = log_error_errno(errno, "Failed to acquire pseudo tty: %m");
803fb7
@@ -3766,15 +3769,15 @@ int main(int argc, char *argv[]) {
803fb7
                 goto finish;
803fb7
         }
803fb7
 
803fb7
-        if (!arg_quiet)
803fb7
-                log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
803fb7
-                         arg_machine, arg_image ?: arg_directory);
803fb7
-
803fb7
         if (unlockpt(master) < 0) {
803fb7
                 r = log_error_errno(errno, "Failed to unlock tty: %m");
803fb7
                 goto finish;
803fb7
         }
803fb7
 
803fb7
+        if (!arg_quiet)
803fb7
+                log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
803fb7
+                         arg_machine, arg_image ?: arg_directory);
803fb7
+
803fb7
         assert_se(sigemptyset(&mask) == 0);
803fb7
         sigset_add_many(&mask, SIGCHLD, SIGWINCH, SIGTERM, SIGINT, -1);
803fb7
         assert_se(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
803fb7
@@ -3860,9 +3863,6 @@ int main(int argc, char *argv[]) {
803fb7
 
803fb7
                         master = safe_close(master);
803fb7
 
803fb7
-                        close_nointr(STDIN_FILENO);
803fb7
-                        close_nointr(STDOUT_FILENO);
803fb7
-                        close_nointr(STDERR_FILENO);
803fb7
 
803fb7
                         kmsg_socket_pair[0] = safe_close(kmsg_socket_pair[0]);
803fb7
                         rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
803fb7
@@ -3870,21 +3870,27 @@ int main(int argc, char *argv[]) {
803fb7
                         reset_all_signal_handlers();
803fb7
                         reset_signal_mask();
803fb7
 
803fb7
-                        r = open_terminal(console, O_RDWR);
803fb7
-                        if (r != STDIN_FILENO) {
803fb7
-                                if (r >= 0) {
803fb7
-                                        safe_close(r);
803fb7
-                                        r = -EINVAL;
803fb7
-                                }
803fb7
+                        if (interactive) {
803fb7
+                                close_nointr(STDIN_FILENO);
803fb7
+                                close_nointr(STDOUT_FILENO);
803fb7
+                                close_nointr(STDERR_FILENO);
803fb7
 
803fb7
-                                log_error_errno(r, "Failed to open console: %m");
803fb7
-                                _exit(EXIT_FAILURE);
803fb7
-                        }
803fb7
+                                r = open_terminal(console, O_RDWR);
803fb7
+                                if (r != STDIN_FILENO) {
803fb7
+                                        if (r >= 0) {
803fb7
+                                                safe_close(r);
803fb7
+                                                r = -EINVAL;
803fb7
+                                        }
803fb7
 
803fb7
-                        if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO ||
803fb7
-                            dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) {
803fb7
-                                log_error_errno(errno, "Failed to duplicate console: %m");
803fb7
-                                _exit(EXIT_FAILURE);
803fb7
+                                        log_error_errno(r, "Failed to open console: %m");
803fb7
+                                        _exit(EXIT_FAILURE);
803fb7
+                                }
803fb7
+
803fb7
+                                if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO ||
803fb7
+                                    dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) {
803fb7
+                                        log_error_errno(errno, "Failed to duplicate console: %m");
803fb7
+                                        _exit(EXIT_FAILURE);
803fb7
+                                }
803fb7
                         }
803fb7
 
803fb7
                         if (setsid() < 0) {
803fb7
@@ -4227,7 +4233,7 @@ int main(int argc, char *argv[]) {
803fb7
 
803fb7
                                 rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
803fb7
 
803fb7
-                                r = pty_forward_new(event, master, true, &forward);
803fb7
+                                r = pty_forward_new(event, master, true, !interactive, &forward);
803fb7
                                 if (r < 0) {
803fb7
                                         log_error_errno(r, "Failed to create PTY forwarder: %m");
803fb7
                                         goto finish;
803fb7
diff --git a/src/run/run.c b/src/run/run.c
803fb7
index dd1338f3b..468034284 100644
803fb7
--- a/src/run/run.c
803fb7
+++ b/src/run/run.c
803fb7
@@ -780,7 +780,7 @@ static int start_transient_service(
803fb7
                 if (!arg_quiet)
803fb7
                         log_info("Running as unit %s.\nPress ^] three times within 1s to disconnect TTY.", service);
803fb7
 
803fb7
-                r = pty_forward_new(event, master, false, &forward);
803fb7
+                r = pty_forward_new(event, master, false, false, &forward);
803fb7
                 if (r < 0)
803fb7
                         return log_error_errno(r, "Failed to create PTY forwarder: %m");
803fb7
 
803fb7
diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c
803fb7
index 88b3f4e3c..4402af123 100644
803fb7
--- a/src/shared/ptyfwd.c
803fb7
+++ b/src/shared/ptyfwd.c
803fb7
@@ -42,6 +42,8 @@ struct PTYForward {
803fb7
         struct termios saved_stdin_attr;
803fb7
         struct termios saved_stdout_attr;
803fb7
 
803fb7
+        bool read_only:1;
803fb7
+
803fb7
         bool saved_stdin:1;
803fb7
         bool saved_stdout:1;
803fb7
 
803fb7
@@ -298,7 +300,13 @@ static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
-int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward **ret) {
803fb7
+int pty_forward_new(
803fb7
+                sd_event *event,
803fb7
+                int master,
803fb7
+                bool ignore_vhangup,
803fb7
+                bool read_only,
803fb7
+                PTYForward **ret) {
803fb7
+
803fb7
         _cleanup_(pty_forward_freep) PTYForward *f = NULL;
803fb7
         struct winsize ws;
803fb7
         int r;
803fb7
@@ -307,6 +315,7 @@ int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward
803fb7
         if (!f)
803fb7
                 return -ENOMEM;
803fb7
 
803fb7
+        f->read_only = read_only;
803fb7
         f->ignore_vhangup = ignore_vhangup;
803fb7
 
803fb7
         if (event)
803fb7
@@ -317,13 +326,15 @@ int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward
803fb7
                         return r;
803fb7
         }
803fb7
 
803fb7
-        r = fd_nonblock(STDIN_FILENO, true);
803fb7
-        if (r < 0)
803fb7
-                return r;
803fb7
+        if (!read_only) {
803fb7
+                r = fd_nonblock(STDIN_FILENO, true);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
 
803fb7
-        r = fd_nonblock(STDOUT_FILENO, true);
803fb7
-        if (r < 0)
803fb7
-                return r;
803fb7
+                r = fd_nonblock(STDOUT_FILENO, true);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+        }
803fb7
 
803fb7
         r = fd_nonblock(master, true);
803fb7
         if (r < 0)
803fb7
@@ -334,36 +345,34 @@ int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward
803fb7
         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
803fb7
                 (void)ioctl(master, TIOCSWINSZ, &ws);
803fb7
 
803fb7
-        if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
803fb7
-                struct termios raw_stdin_attr;
803fb7
-
803fb7
-                f->saved_stdin = true;
803fb7
+        if (!read_only) {
803fb7
+                if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
803fb7
+                        struct termios raw_stdin_attr;
803fb7
 
803fb7
-                raw_stdin_attr = f->saved_stdin_attr;
803fb7
-                cfmakeraw(&raw_stdin_attr);
803fb7
-                raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
803fb7
-                tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr);
803fb7
-        }
803fb7
+                        f->saved_stdin = true;
803fb7
 
803fb7
-        if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) {
803fb7
-                struct termios raw_stdout_attr;
803fb7
+                        raw_stdin_attr = f->saved_stdin_attr;
803fb7
+                        cfmakeraw(&raw_stdin_attr);
803fb7
+                        raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
803fb7
+                        tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr);
803fb7
+                }
803fb7
 
803fb7
-                f->saved_stdout = true;
803fb7
+                if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) {
803fb7
+                        struct termios raw_stdout_attr;
803fb7
 
803fb7
-                raw_stdout_attr = f->saved_stdout_attr;
803fb7
-                cfmakeraw(&raw_stdout_attr);
803fb7
-                raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
803fb7
-                raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
803fb7
-                tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr);
803fb7
-        }
803fb7
+                        f->saved_stdout = true;
803fb7
 
803fb7
-        r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
803fb7
-        if (r < 0)
803fb7
-                return r;
803fb7
+                        raw_stdout_attr = f->saved_stdout_attr;
803fb7
+                        cfmakeraw(&raw_stdout_attr);
803fb7
+                        raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
803fb7
+                        raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
803fb7
+                        tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr);
803fb7
+                }
803fb7
 
803fb7
-        r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f);
803fb7
-        if (r < 0 && r != -EPERM)
803fb7
-                return r;
803fb7
+                r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f);
803fb7
+                if (r < 0 && r != -EPERM)
803fb7
+                        return r;
803fb7
+        }
803fb7
 
803fb7
         r = sd_event_add_io(f->event, &f->stdout_event_source, STDOUT_FILENO, EPOLLOUT|EPOLLET, on_stdout_event, f);
803fb7
         if (r == -EPERM)
803fb7
@@ -372,6 +381,10 @@ int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward
803fb7
         else if (r < 0)
803fb7
                 return r;
803fb7
 
803fb7
+        r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
803fb7
+        if (r < 0)
803fb7
+                return r;
803fb7
+
803fb7
         r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f);
803fb7
         if (r < 0)
803fb7
                 return r;
803fb7
diff --git a/src/shared/ptyfwd.h b/src/shared/ptyfwd.h
803fb7
index d3e229bd7..6208a543d 100644
803fb7
--- a/src/shared/ptyfwd.h
803fb7
+++ b/src/shared/ptyfwd.h
803fb7
@@ -30,7 +30,7 @@
803fb7
 
803fb7
 typedef struct PTYForward PTYForward;
803fb7
 
803fb7
-int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward **f);
803fb7
+int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, bool read_only, PTYForward **f);
803fb7
 PTYForward *pty_forward_free(PTYForward *f);
803fb7
 
803fb7
 int pty_forward_get_last_char(PTYForward *f, char *ch);