From 49b63281a09b61ab608f51a771f124f14c1ee1ad Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 15 Jan 2019 08:49:24 +0100 Subject: [PATCH] Make it possible for a process to switch controlling terminals. Via the TIOCSCTTY and TIOCNOTTY ioctls. --- Kernel/Process.h | 1 + Kernel/SlavePTY.cpp | 1 + Kernel/TTY.cpp | 8 +++++++- LibC/sys/ioctl_numbers.h | 2 ++ Terminal/main.cpp | 14 +++++++++++++- Userland/tst.cpp | 9 +++++++-- 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Kernel/Process.h b/Kernel/Process.h index 85ae1c6bda..c696244fb7 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -206,6 +206,7 @@ public: static int reap(Process&) WARN_UNUSED_RESULT; const TTY* tty() const { return m_tty; } + void set_tty(TTY* tty) { m_tty = tty; } size_t regionCount() const { return m_regions.size(); } const Vector>& regions() const { return m_regions; } diff --git a/Kernel/SlavePTY.cpp b/Kernel/SlavePTY.cpp index 3d3a3c439e..a693a8ce58 100644 --- a/Kernel/SlavePTY.cpp +++ b/Kernel/SlavePTY.cpp @@ -5,6 +5,7 @@ SlavePTY::SlavePTY(unsigned index) : TTY(11, index) , m_index(index) { + set_size(80, 25); } SlavePTY::~SlavePTY() diff --git a/Kernel/TTY.cpp b/Kernel/TTY.cpp index 5261e251e1..75941250be 100644 --- a/Kernel/TTY.cpp +++ b/Kernel/TTY.cpp @@ -107,7 +107,7 @@ int TTY::ioctl(Process& process, unsigned request, unsigned arg) Unix::termios* tp; Unix::winsize* ws; - if (process.tty() != this) + if (process.tty() && process.tty() != this) return -ENOTTY; switch (request) { case TIOCGPGRP: @@ -140,6 +140,12 @@ int TTY::ioctl(Process& process, unsigned request, unsigned arg) ws->ws_row = m_rows; ws->ws_col = m_columns; return 0; + case TIOCSCTTY: + process.set_tty(this); + return 0; + case TIOCNOTTY: + process.set_tty(nullptr); + return 0; } ASSERT_NOT_REACHED(); return -EINVAL; diff --git a/LibC/sys/ioctl_numbers.h b/LibC/sys/ioctl_numbers.h index bc4a6dae23..1ed0a9c934 100644 --- a/LibC/sys/ioctl_numbers.h +++ b/LibC/sys/ioctl_numbers.h @@ -8,5 +8,7 @@ enum IOCtlNumber { TCSETSW, TCSETSF, TIOCGWINSZ, + TIOCSCTTY, + TIOCNOTTY, }; diff --git a/Terminal/main.cpp b/Terminal/main.cpp index 96bf34a443..78f0ffd873 100644 --- a/Terminal/main.cpp +++ b/Terminal/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "Terminal.h" @@ -20,9 +21,15 @@ static void make_shell(int ptm_fd) perror("ptsname"); exit(1); } + int rc = 0; close(ptm_fd); int pts_fd = open(tty_name, O_RDWR); dbgprintf("*** In child (%d), opening slave pty %s, pts_fd=%d\n", getpid(), tty_name, pts_fd); + rc = ioctl(0, TIOCNOTTY); + if (rc < 0) { + perror("ioctl(TIOCNOTTY)"); + exit(1); + } close(0); close(1); close(2); @@ -30,7 +37,12 @@ static void make_shell(int ptm_fd) dup2(pts_fd, 1); dup2(pts_fd, 2); close(pts_fd); - int rc = execve("/bin/sh", nullptr, nullptr); + rc = ioctl(0, TIOCSCTTY); + if (rc < 0) { + perror("ioctl(TIOCSCTTY)"); + exit(1); + } + rc = execve("/bin/sh", nullptr, nullptr); if (rc < 0) { perror("execve"); exit(1); diff --git a/Userland/tst.cpp b/Userland/tst.cpp index 14463f691f..bea9c63e61 100644 --- a/Userland/tst.cpp +++ b/Userland/tst.cpp @@ -1,4 +1,5 @@ #include +#include #include int main(int argc, char** argv) @@ -7,8 +8,12 @@ int main(int argc, char** argv) (void) argv; struct winsize ws; - ioctl(0, TIOCGWINSZ, &ws); - printf("Terminal is %ux%u\n", ws.ws_col, ws.ws_row); + int rc = ioctl(0, TIOCGWINSZ, &ws); + if (rc < 0) { + perror("ioctl(TIOCGWINSZ)"); + } + printf("TTY is %s\n", ttyname(0)); + printf("Terminal size is %ux%u\n", ws.ws_col, ws.ws_row); printf("Counting to 100000: \033[s"); for (unsigned i = 0; i <= 100000; ++i) {