5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Fri Mar 17 04:37:25 1995 ylo
12 * Allocating a pseudo-terminal, and making it the controlling tty.
17 RCSID("$OpenBSD: pty.c,v 1.14 2000/06/20 01:39:43 markus Exp $");
21 #endif /* HAVE_UTIL_H */
26 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
27 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
34 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
35 # include <sys/stropts.h>
43 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
44 * nonzero if a pty was successfully allocated. On success, open file
45 * descriptors for the pty and tty sides and the name of the tty side are
46 * returned (the buffer must be able to hold at least 64 characters).
50 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
52 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
53 /* openpty(3) exists in OSF/1 and some other os'es */
57 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
59 error("openpty: %.100s", strerror(errno));
62 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
64 #else /* HAVE_OPENPTY */
67 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
68 * pty's automagically when needed
72 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
74 error("_getpty: %.100s", strerror(errno));
77 strlcpy(namebuf, slave, namebuflen);
78 /* Open the slave side. */
79 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
81 error("%.200s: %.100s", namebuf, strerror(errno));
86 #else /* HAVE__GETPTY */
87 #if defined(HAVE_DEV_PTMX)
89 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
90 * also has bsd-style ptys, but they simply do not work.)
95 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
97 error("/dev/ptmx: %.100s", strerror(errno));
100 if (grantpt(ptm) < 0) {
101 error("grantpt: %.100s", strerror(errno));
104 if (unlockpt(ptm) < 0) {
105 error("unlockpt: %.100s", strerror(errno));
110 error("Slave pty side name could not be obtained.");
111 strlcpy(namebuf, pts, namebuflen);
114 /* Open the slave side. */
115 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
117 error("%.100s: %.100s", namebuf, strerror(errno));
122 /* Push the appropriate streams modules, as described in Solaris pts(7). */
123 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
124 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
125 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
126 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
128 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
129 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
133 #else /* HAVE_DEV_PTMX */
134 #ifdef HAVE_DEV_PTS_AND_PTC
135 /* AIX-style pty code. */
138 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
140 error("Could not open /dev/ptc: %.100s", strerror(errno));
143 name = ttyname(*ptyfd);
145 fatal("Open of /dev/ptc returns device for which ttyname fails.");
146 strlcpy(namebuf, name, namebuflen);
147 *ttyfd = open(name, O_RDWR | O_NOCTTY);
149 error("Could not open pty slave side %.100s: %.100s",
150 name, strerror(errno));
155 #else /* HAVE_DEV_PTS_AND_PTC */
156 /* BSD-style pty code. */
159 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
160 const char *ptyminors = "0123456789abcdef";
161 int num_minors = strlen(ptyminors);
162 int num_ptys = strlen(ptymajors) * num_minors;
164 for (i = 0; i < num_ptys; i++) {
165 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
166 ptyminors[i % num_minors]);
167 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
168 ptymajors[i / num_minors], ptyminors[i % num_minors]);
170 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
172 /* Try SCO style naming */
173 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
174 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
175 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
180 /* Open the slave side. */
181 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
183 error("%.100s: %.100s", namebuf, strerror(errno));
190 #endif /* HAVE_DEV_PTS_AND_PTC */
191 #endif /* HAVE_DEV_PTMX */
192 #endif /* HAVE__GETPTY */
193 #endif /* HAVE_OPENPTY */
196 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
199 pty_release(const char *ttyname)
201 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
202 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
203 if (chmod(ttyname, (mode_t) 0666) < 0)
204 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
207 /* Makes the tty the processes controlling tty and sets it to sane modes. */
210 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
215 #endif /* USE_VHANGUP */
217 /* First disconnect from the old controlling tty. */
219 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
221 (void) ioctl(fd, TIOCNOTTY, NULL);
224 #endif /* TIOCNOTTY */
226 error("setsid: %.100s", strerror(errno));
229 * Verify that we are successfully disconnected from the controlling
232 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
234 error("Failed to disconnect from controlling tty.");
237 /* Make it our controlling tty. */
239 debug("Setting controlling tty using TIOCSCTTY.");
241 * We ignore errors from this, because HPSUX defines TIOCSCTTY, but
242 * returns EINVAL with these arguments, and there is absolutely no
245 ioctl(*ttyfd, TIOCSCTTY, NULL);
246 #endif /* TIOCSCTTY */
248 old = signal(SIGHUP, SIG_IGN);
251 #endif /* USE_VHANGUP */
252 fd = open(ttyname, O_RDWR);
254 error("%.100s: %.100s", ttyname, strerror(errno));
259 #else /* USE_VHANGUP */
261 #endif /* USE_VHANGUP */
263 /* Verify that we now have a controlling tty. */
264 fd = open("/dev/tty", O_WRONLY);
266 error("open /dev/tty failed - could not set controlling tty: %.100s",
273 /* Changes the window size associated with the pty. */
276 pty_change_window_size(int ptyfd, int row, int col,
277 int xpixel, int ypixel)
282 w.ws_xpixel = xpixel;
283 w.ws_ypixel = ypixel;
284 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
288 pty_setowner(struct passwd *pw, const char *ttyname)
294 /* Determine the group to make the owner of the tty. */
295 grp = getgrnam("tty");
298 mode = S_IRUSR | S_IWUSR | S_IWGRP;
301 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
304 /* Change ownership of the tty. */
305 if (chown(ttyname, pw->pw_uid, gid) < 0)
306 fatal("chown(%.100s, %d, %d) failed: %.100s",
307 ttyname, pw->pw_uid, gid, strerror(errno));
308 if (chmod(ttyname, mode) < 0)
309 fatal("chmod(%.100s, 0%o) failed: %.100s",
310 ttyname, mode, strerror(errno));