Make it possible for a process to switch controlling terminals.

Via the TIOCSCTTY and TIOCNOTTY ioctls.
This commit is contained in:
Andreas Kling 2019-01-15 08:49:24 +01:00
parent 10308c6434
commit 49b63281a0
Notes: sideshowbarker 2024-07-19 16:02:10 +09:00
6 changed files with 31 additions and 4 deletions

View File

@ -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<RetainPtr<Region>>& regions() const { return m_regions; }

View File

@ -5,6 +5,7 @@ SlavePTY::SlavePTY(unsigned index)
: TTY(11, index)
, m_index(index)
{
set_size(80, 25);
}
SlavePTY::~SlavePTY()

View File

@ -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;

View File

@ -8,5 +8,7 @@ enum IOCtlNumber {
TCSETSW,
TCSETSF,
TIOCGWINSZ,
TIOCSCTTY,
TIOCNOTTY,
};

View File

@ -8,6 +8,7 @@
#include <Widgets/Font.h>
#include <Widgets/GraphicsBitmap.h>
#include <Widgets/Painter.h>
#include <sys/ioctl.h>
#include <gui.h>
#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);

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
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) {