ryantimwilson / rpms / systemd

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