Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Allocating a pseudo-terminal, and making it the controlling tty. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 7 | * As far as I am concerned, the code I have written for this software |
| 8 | * can be used freely for any purpose. Any derived versions of this |
| 9 | * software must be clearly marked as such, and if the derived work is |
| 10 | * incompatible with the protocol description in the RFC file, it must be |
| 11 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | RCSID("$OpenBSD: pty.c,v 1.16 2000/09/07 21:13:37 markus Exp $"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 16 | |
Damien Miller | 36b339a | 1999-12-14 10:54:47 +1100 | [diff] [blame] | 17 | #ifdef HAVE_UTIL_H |
| 18 | # include <util.h> |
| 19 | #endif /* HAVE_UTIL_H */ |
| 20 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 21 | #include "pty.h" |
| 22 | #include "ssh.h" |
Damien Miller | 356a0b0 | 1999-11-08 15:30:59 +1100 | [diff] [blame] | 23 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 24 | /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */ |
| 25 | #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY) |
| 26 | #undef HAVE_DEV_PTMX |
| 27 | #endif |
| 28 | |
Damien Miller | fce1648 | 1999-12-08 08:53:52 +1100 | [diff] [blame] | 29 | #ifdef HAVE_PTY_H |
| 30 | # include <pty.h> |
| 31 | #endif |
| 32 | #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H) |
| 33 | # include <sys/stropts.h> |
| 34 | #endif |
| 35 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | #ifndef O_NOCTTY |
| 37 | #define O_NOCTTY 0 |
| 38 | #endif |
| 39 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 40 | /* |
| 41 | * Allocates and opens a pty. Returns 0 if no pty could be allocated, or |
| 42 | * nonzero if a pty was successfully allocated. On success, open file |
| 43 | * descriptors for the pty and tty sides and the name of the tty side are |
| 44 | * returned (the buffer must be able to hold at least 64 characters). |
| 45 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 46 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 47 | int |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 48 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 49 | { |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 50 | #if defined(HAVE_OPENPTY) || defined(BSD4_4) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 51 | /* openpty(3) exists in OSF/1 and some other os'es */ |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 52 | char buf[64]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 53 | int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 55 | i = openpty(ptyfd, ttyfd, buf, NULL, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 56 | if (i < 0) { |
| 57 | error("openpty: %.100s", strerror(errno)); |
| 58 | return 0; |
| 59 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 60 | strlcpy(namebuf, buf, namebuflen); /* possible truncation */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 61 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | #else /* HAVE_OPENPTY */ |
| 63 | #ifdef HAVE__GETPTY |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 64 | /* |
| 65 | * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more |
| 66 | * pty's automagically when needed |
| 67 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 68 | char *slave; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | slave = _getpty(ptyfd, O_RDWR, 0622, 0); |
| 71 | if (slave == NULL) { |
| 72 | error("_getpty: %.100s", strerror(errno)); |
| 73 | return 0; |
| 74 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 75 | strlcpy(namebuf, slave, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 76 | /* Open the slave side. */ |
| 77 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); |
| 78 | if (*ttyfd < 0) { |
| 79 | error("%.200s: %.100s", namebuf, strerror(errno)); |
| 80 | close(*ptyfd); |
| 81 | return 0; |
| 82 | } |
| 83 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 84 | #else /* HAVE__GETPTY */ |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 85 | #if defined(HAVE_DEV_PTMX) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 86 | /* |
| 87 | * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3 |
| 88 | * also has bsd-style ptys, but they simply do not work.) |
| 89 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | int ptm; |
| 91 | char *pts; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 93 | ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY); |
| 94 | if (ptm < 0) { |
| 95 | error("/dev/ptmx: %.100s", strerror(errno)); |
| 96 | return 0; |
| 97 | } |
| 98 | if (grantpt(ptm) < 0) { |
| 99 | error("grantpt: %.100s", strerror(errno)); |
| 100 | return 0; |
| 101 | } |
| 102 | if (unlockpt(ptm) < 0) { |
| 103 | error("unlockpt: %.100s", strerror(errno)); |
| 104 | return 0; |
| 105 | } |
| 106 | pts = ptsname(ptm); |
| 107 | if (pts == NULL) |
| 108 | error("Slave pty side name could not be obtained."); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 109 | strlcpy(namebuf, pts, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | *ptyfd = ptm; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | /* Open the slave side. */ |
| 113 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); |
| 114 | if (*ttyfd < 0) { |
| 115 | error("%.100s: %.100s", namebuf, strerror(errno)); |
| 116 | close(*ptyfd); |
| 117 | return 0; |
| 118 | } |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 119 | #ifndef HAVE_CYGWIN |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 120 | /* Push the appropriate streams modules, as described in Solaris pts(7). */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | if (ioctl(*ttyfd, I_PUSH, "ptem") < 0) |
| 122 | error("ioctl I_PUSH ptem: %.100s", strerror(errno)); |
| 123 | if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0) |
| 124 | error("ioctl I_PUSH ldterm: %.100s", strerror(errno)); |
Damien Miller | e37ac5a | 2000-03-17 23:58:59 +1100 | [diff] [blame] | 125 | #ifndef _HPUX_SOURCE |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 126 | if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0) |
| 127 | error("ioctl I_PUSH ttcompat: %.100s", strerror(errno)); |
Damien Miller | e37ac5a | 2000-03-17 23:58:59 +1100 | [diff] [blame] | 128 | #endif |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 129 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 131 | #else /* HAVE_DEV_PTMX */ |
| 132 | #ifdef HAVE_DEV_PTS_AND_PTC |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | /* AIX-style pty code. */ |
| 134 | const char *name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 135 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY); |
| 137 | if (*ptyfd < 0) { |
| 138 | error("Could not open /dev/ptc: %.100s", strerror(errno)); |
| 139 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | name = ttyname(*ptyfd); |
| 142 | if (!name) |
| 143 | fatal("Open of /dev/ptc returns device for which ttyname fails."); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 144 | strlcpy(namebuf, name, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | *ttyfd = open(name, O_RDWR | O_NOCTTY); |
| 146 | if (*ttyfd < 0) { |
| 147 | error("Could not open pty slave side %.100s: %.100s", |
| 148 | name, strerror(errno)); |
| 149 | close(*ptyfd); |
| 150 | return 0; |
| 151 | } |
| 152 | return 1; |
| 153 | #else /* HAVE_DEV_PTS_AND_PTC */ |
| 154 | /* BSD-style pty code. */ |
| 155 | char buf[64]; |
| 156 | int i; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 157 | const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | const char *ptyminors = "0123456789abcdef"; |
| 159 | int num_minors = strlen(ptyminors); |
| 160 | int num_ptys = strlen(ptymajors) * num_minors; |
| 161 | |
| 162 | for (i = 0; i < num_ptys; i++) { |
| 163 | snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors], |
| 164 | ptyminors[i % num_minors]); |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 165 | snprintf(namebuf, namebuflen, "/dev/tty%c%c", |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 166 | ptymajors[i / num_minors], ptyminors[i % num_minors]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | |
Damien Miller | 6d8c11f | 2000-08-29 11:52:38 +1100 | [diff] [blame] | 168 | *ptyfd = open(buf, O_RDWR | O_NOCTTY); |
| 169 | if (*ptyfd < 0) { |
| 170 | /* Try SCO style naming */ |
| 171 | snprintf(buf, sizeof buf, "/dev/ptyp%d", i); |
| 172 | snprintf(namebuf, namebuflen, "/dev/ttyp%d", i); |
| 173 | *ptyfd = open(buf, O_RDWR | O_NOCTTY); |
| 174 | if (*ptyfd < 0) |
| 175 | continue; |
| 176 | } |
| 177 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 178 | /* Open the slave side. */ |
| 179 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); |
| 180 | if (*ttyfd < 0) { |
| 181 | error("%.100s: %.100s", namebuf, strerror(errno)); |
| 182 | close(*ptyfd); |
| 183 | return 0; |
| 184 | } |
| 185 | return 1; |
| 186 | } |
| 187 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 188 | #endif /* HAVE_DEV_PTS_AND_PTC */ |
| 189 | #endif /* HAVE_DEV_PTMX */ |
| 190 | #endif /* HAVE__GETPTY */ |
| 191 | #endif /* HAVE_OPENPTY */ |
| 192 | } |
| 193 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 194 | /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 195 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 196 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 197 | pty_release(const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 198 | { |
Damien Miller | 204ad07 | 2000-03-02 23:56:12 +1100 | [diff] [blame] | 199 | if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 200 | error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); |
Damien Miller | 204ad07 | 2000-03-02 23:56:12 +1100 | [diff] [blame] | 201 | if (chmod(ttyname, (mode_t) 0666) < 0) |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 202 | error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* Makes the tty the processes controlling tty and sets it to sane modes. */ |
| 206 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 207 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 208 | pty_make_controlling_tty(int *ttyfd, const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 209 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | int fd; |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 211 | #ifdef USE_VHANGUP |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 212 | void *old; |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 213 | #endif /* USE_VHANGUP */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 214 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 215 | /* First disconnect from the old controlling tty. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 216 | #ifdef TIOCNOTTY |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 217 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
| 218 | if (fd >= 0) { |
| 219 | (void) ioctl(fd, TIOCNOTTY, NULL); |
| 220 | close(fd); |
| 221 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 222 | #endif /* TIOCNOTTY */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 223 | if (setsid() < 0) |
| 224 | error("setsid: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 225 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 226 | /* |
| 227 | * Verify that we are successfully disconnected from the controlling |
| 228 | * tty. |
| 229 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 230 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
| 231 | if (fd >= 0) { |
| 232 | error("Failed to disconnect from controlling tty."); |
| 233 | close(fd); |
| 234 | } |
| 235 | /* Make it our controlling tty. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 236 | #ifdef TIOCSCTTY |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 237 | debug("Setting controlling tty using TIOCSCTTY."); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 238 | /* |
| 239 | * We ignore errors from this, because HPSUX defines TIOCSCTTY, but |
| 240 | * returns EINVAL with these arguments, and there is absolutely no |
| 241 | * documentation. |
| 242 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 243 | ioctl(*ttyfd, TIOCSCTTY, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | #endif /* TIOCSCTTY */ |
Ben Lindstrom | b562864 | 2000-10-18 00:02:25 +0000 | [diff] [blame] | 245 | #ifdef HAVE_NEWS4 |
| 246 | if (setpgrp(0,0) < 0) |
| 247 | error("SETPGRP %s",strerror(errno)); |
| 248 | #endif /* HAVE_NEWS4 */ |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 249 | #ifdef USE_VHANGUP |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 250 | old = signal(SIGHUP, SIG_IGN); |
| 251 | vhangup(); |
| 252 | signal(SIGHUP, old); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 253 | #endif /* USE_VHANGUP */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 254 | fd = open(ttyname, O_RDWR); |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 255 | if (fd < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 256 | error("%.100s: %.100s", ttyname, strerror(errno)); |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 257 | } else { |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 258 | #ifdef USE_VHANGUP |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 259 | close(*ttyfd); |
| 260 | *ttyfd = fd; |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 261 | #else /* USE_VHANGUP */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 262 | close(fd); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 263 | #endif /* USE_VHANGUP */ |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 264 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 265 | /* Verify that we now have a controlling tty. */ |
| 266 | fd = open("/dev/tty", O_WRONLY); |
| 267 | if (fd < 0) |
| 268 | error("open /dev/tty failed - could not set controlling tty: %.100s", |
| 269 | strerror(errno)); |
| 270 | else { |
| 271 | close(fd); |
| 272 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Changes the window size associated with the pty. */ |
| 276 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 277 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 278 | pty_change_window_size(int ptyfd, int row, int col, |
| 279 | int xpixel, int ypixel) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 280 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 281 | struct winsize w; |
| 282 | w.ws_row = row; |
| 283 | w.ws_col = col; |
| 284 | w.ws_xpixel = xpixel; |
| 285 | w.ws_ypixel = ypixel; |
| 286 | (void) ioctl(ptyfd, TIOCSWINSZ, &w); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 287 | } |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 288 | |
| 289 | void |
| 290 | pty_setowner(struct passwd *pw, const char *ttyname) |
| 291 | { |
| 292 | struct group *grp; |
| 293 | gid_t gid; |
| 294 | mode_t mode; |
| 295 | |
| 296 | /* Determine the group to make the owner of the tty. */ |
| 297 | grp = getgrnam("tty"); |
| 298 | if (grp) { |
| 299 | gid = grp->gr_gid; |
| 300 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
| 301 | } else { |
| 302 | gid = pw->pw_gid; |
| 303 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
| 304 | } |
| 305 | |
| 306 | /* Change ownership of the tty. */ |
| 307 | if (chown(ttyname, pw->pw_uid, gid) < 0) |
| 308 | fatal("chown(%.100s, %d, %d) failed: %.100s", |
| 309 | ttyname, pw->pw_uid, gid, strerror(errno)); |
| 310 | if (chmod(ttyname, mode) < 0) |
| 311 | fatal("chmod(%.100s, 0%o) failed: %.100s", |
| 312 | ttyname, mode, strerror(errno)); |
| 313 | } |