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