Bladeren bron

Make it possible for a process to switch controlling terminals.

Via the TIOCSCTTY and TIOCNOTTY ioctls.
Andreas Kling 6 jaren geleden
bovenliggende
commit
49b63281a0
6 gewijzigde bestanden met toevoegingen van 31 en 4 verwijderingen
  1. 1 0
      Kernel/Process.h
  2. 1 0
      Kernel/SlavePTY.cpp
  3. 7 1
      Kernel/TTY.cpp
  4. 2 0
      LibC/sys/ioctl_numbers.h
  5. 13 1
      Terminal/main.cpp
  6. 7 2
      Userland/tst.cpp

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

+ 1 - 0
Kernel/SlavePTY.cpp

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

+ 7 - 1
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;

+ 2 - 0
LibC/sys/ioctl_numbers.h

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

+ 13 - 1
Terminal/main.cpp

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

+ 7 - 2
Userland/tst.cpp

@@ -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) {