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.
24 #include <sys/stropts.h>
25 #endif /* HAVE_DEV_PTMX */
27 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
28 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
35 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
36 # include <sys/stropts.h>
44 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
45 * nonzero if a pty was successfully allocated. On success, open file
46 * descriptors for the pty and tty sides and the name of the tty side are
47 * returned (the buffer must be able to hold at least 64 characters).
51 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
53 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
54 /* openpty(3) exists in OSF/1 and some other os'es */
58 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
60 error("openpty: %.100s", strerror(errno));
63 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
65 #else /* HAVE_OPENPTY */
68 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
69 * pty's automagically when needed
73 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
75 error("_getpty: %.100s", strerror(errno));
78 strlcpy(namebuf, slave, namebuflen);
79 /* Open the slave side. */
80 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
82 error("%.200s: %.100s", namebuf, strerror(errno));
87 #else /* HAVE__GETPTY */
90 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
91 * also has bsd-style ptys, but they simply do not work.)
96 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
98 error("/dev/ptmx: %.100s", strerror(errno));
101 if (grantpt(ptm) < 0) {
102 error("grantpt: %.100s", strerror(errno));
105 if (unlockpt(ptm) < 0) {
106 error("unlockpt: %.100s", strerror(errno));
111 error("Slave pty side name could not be obtained.");
112 strlcpy(namebuf, pts, namebuflen);
115 /* Open the slave side. */
116 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
118 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));
127 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
128 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
130 #else /* HAVE_DEV_PTMX */
131 #ifdef HAVE_DEV_PTS_AND_PTC
132 /* AIX-style pty code. */
135 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
137 error("Could not open /dev/ptc: %.100s", strerror(errno));
140 name = ttyname(*ptyfd);
142 fatal("Open of /dev/ptc returns device for which ttyname fails.");
143 strlcpy(namebuf, name, namebuflen);
144 *ttyfd = open(name, O_RDWR | O_NOCTTY);
146 error("Could not open pty slave side %.100s: %.100s",
147 name, strerror(errno));
152 #else /* HAVE_DEV_PTS_AND_PTC */
153 /* BSD-style pty code. */
156 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
157 const char *ptyminors = "0123456789abcdef";
158 int num_minors = strlen(ptyminors);
159 int num_ptys = strlen(ptymajors) * num_minors;
161 for (i = 0; i < num_ptys; i++) {
162 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
163 ptyminors[i % num_minors]);
164 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
167 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
168 ptymajors[i / num_minors], ptyminors[i % num_minors]);
170 /* Open the slave side. */
171 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
173 error("%.100s: %.100s", namebuf, strerror(errno));
180 #endif /* HAVE_DEV_PTS_AND_PTC */
181 #endif /* HAVE_DEV_PTMX */
182 #endif /* HAVE__GETPTY */
183 #endif /* HAVE_OPENPTY */
186 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
189 pty_release(const char *ttyname)
191 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
192 debug("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
193 if (chmod(ttyname, (mode_t) 0666) < 0)
194 debug("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
197 /* Makes the tty the processes controlling tty and sets it to sane modes. */
200 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
204 /* First disconnect from the old controlling tty. */
206 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
208 (void) ioctl(fd, TIOCNOTTY, NULL);
211 #endif /* TIOCNOTTY */
213 error("setsid: %.100s", strerror(errno));
216 * Verify that we are successfully disconnected from the controlling
219 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
221 error("Failed to disconnect from controlling tty.");
224 /* Make it our controlling tty. */
226 debug("Setting controlling tty using TIOCSCTTY.");
228 * We ignore errors from this, because HPSUX defines TIOCSCTTY, but
229 * returns EINVAL with these arguments, and there is absolutely no
232 ioctl(*ttyfd, TIOCSCTTY, NULL);
233 #endif /* TIOCSCTTY */
234 fd = open(ttyname, O_RDWR);
236 error("%.100s: %.100s", ttyname, strerror(errno));
240 /* Verify that we now have a controlling tty. */
241 fd = open("/dev/tty", O_WRONLY);
243 error("open /dev/tty failed - could not set controlling tty: %.100s",
250 /* Changes the window size associated with the pty. */
253 pty_change_window_size(int ptyfd, int row, int col,
254 int xpixel, int ypixel)
259 w.ws_xpixel = xpixel;
260 w.ws_ypixel = ypixel;
261 (void) ioctl(ptyfd, TIOCSWINSZ, &w);