Fix a controlling-terminal bug reported by Anthony Heading: Cygwin
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Tue, 28 Aug 2012 17:42:47 +0000 (17:42 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Tue, 28 Aug 2012 17:42:47 +0000 (17:42 +0000)
doesn't have TIOCSCTTY, so my attempt to set the ctty of the child
process isn't doing anything, and only works by chance when you run
bash because bash does the thing that _will_ set the ctty, namely
opening the terminal file again without O_NOCTTY. So now we do that
too.

git-svn-id: svn://svn.tartarus.org/sgt/putty@9638 cda61777-01e9-0310-a592-d414129be87e

contrib/cygtermd/pty.c

index e30f9e0..e22cd5f 100644 (file)
@@ -122,9 +122,23 @@ int run_program_in_pty(const struct shell_data *shdata,
             close(fd);
         }
 #endif
+        /*
+         * Make the new pty our controlling terminal. On some OSes
+         * this is done with TIOCSCTTY; Cygwin doesn't have that, so
+         * instead it's done by simply opening the pty without
+         * O_NOCTTY. This code is primarily intended for Cygwin, but
+         * it's useful to have it work in other contexts for testing
+         * purposes, so I leave the TIOCSCTTY here anyway.
+         */
+        if ((fd = open(ptyname, O_RDWR)) >= 0) {
 #ifdef TIOCSCTTY
-        ioctl(0, TIOCSCTTY, &i);
+            ioctl(fd, TIOCSCTTY, &i);
 #endif
+            close(fd);
+        } else {
+            perror("slave pty: open");
+            exit(127);
+        }
        tcsetpgrp(0, getpgrp());
 
        for (i = 0; i < shdata->nenvvars; i++)