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 #endif /* HAVE_PTY_H */
26 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
27 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
36 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
37 * nonzero if a pty was successfully allocated. On success, open file
38 * descriptors for the pty and tty sides and the name of the tty side are
39 * returned (the buffer must be able to hold at least 64 characters).
43 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
45 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
46 /* openpty(3) exists in OSF/1 and some other os'es */
50 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
52 error("openpty: %.100s", strerror(errno));
55 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
57 #else /* HAVE_OPENPTY */
60 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
61 * pty's automagically when needed
65 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
67 error("_getpty: %.100s", strerror(errno));
70 strlcpy(namebuf, slave, namebuflen);
71 /* Open the slave side. */
72 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
74 error("%.200s: %.100s", namebuf, strerror(errno));
79 #else /* HAVE__GETPTY */
82 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
83 * also has bsd-style ptys, but they simply do not work.)
88 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
90 error("/dev/ptmx: %.100s", strerror(errno));
93 if (grantpt(ptm) < 0) {
94 error("grantpt: %.100s", strerror(errno));
97 if (unlockpt(ptm) < 0) {
98 error("unlockpt: %.100s", strerror(errno));
103 error("Slave pty side name could not be obtained.");
104 strlcpy(namebuf, pts, namebuflen);
107 /* Open the slave side. */
108 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
110 error("%.100s: %.100s", namebuf, strerror(errno));
114 /* Push the appropriate streams modules, as described in Solaris pts(7). */
115 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
116 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
117 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
118 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
119 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
120 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
122 #else /* HAVE_DEV_PTMX */
123 #ifdef HAVE_DEV_PTS_AND_PTC
124 /* AIX-style pty code. */
127 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
129 error("Could not open /dev/ptc: %.100s", strerror(errno));
132 name = ttyname(*ptyfd);
134 fatal("Open of /dev/ptc returns device for which ttyname fails.");
135 strlcpy(namebuf, name, namebuflen);
136 *ttyfd = open(name, O_RDWR | O_NOCTTY);
138 error("Could not open pty slave side %.100s: %.100s",
139 name, strerror(errno));
144 #else /* HAVE_DEV_PTS_AND_PTC */
145 /* BSD-style pty code. */
148 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
149 const char *ptyminors = "0123456789abcdef";
150 int num_minors = strlen(ptyminors);
151 int num_ptys = strlen(ptymajors) * num_minors;
153 for (i = 0; i < num_ptys; i++) {
154 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
155 ptyminors[i % num_minors]);
156 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
159 snprintf(namebuf, sizeof namebuflen, "/dev/tty%c%c",
160 ptymajors[i / num_minors], ptyminors[i % num_minors]);
162 /* Open the slave side. */
163 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
165 error("%.100s: %.100s", namebuf, strerror(errno));
172 #endif /* HAVE_DEV_PTS_AND_PTC */
173 #endif /* HAVE_DEV_PTMX */
174 #endif /* HAVE__GETPTY */
175 #endif /* HAVE_OPENPTY */
178 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
181 pty_release(const char *ttyname)
183 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
184 debug("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
185 if (chmod(ttyname, (mode_t) 0666) < 0)
186 debug("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
189 /* Makes the tty the processes controlling tty and sets it to sane modes. */
192 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
196 /* First disconnect from the old controlling tty. */
198 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
200 (void) ioctl(fd, TIOCNOTTY, NULL);
203 #endif /* TIOCNOTTY */
205 error("setsid: %.100s", strerror(errno));
208 * Verify that we are successfully disconnected from the controlling
211 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
213 error("Failed to disconnect from controlling tty.");
216 /* Make it our controlling tty. */
218 debug("Setting controlling tty using TIOCSCTTY.");
220 * We ignore errors from this, because HPSUX defines TIOCSCTTY, but
221 * returns EINVAL with these arguments, and there is absolutely no
224 ioctl(*ttyfd, TIOCSCTTY, NULL);
225 #endif /* TIOCSCTTY */
226 fd = open(ttyname, O_RDWR);
228 error("%.100s: %.100s", ttyname, strerror(errno));
232 /* Verify that we now have a controlling tty. */
233 fd = open("/dev/tty", O_WRONLY);
235 error("open /dev/tty failed - could not set controlling tty: %.100s",
242 /* Changes the window size associated with the pty. */
245 pty_change_window_size(int ptyfd, int row, int col,
246 int xpixel, int ypixel)
251 w.ws_xpixel = xpixel;
252 w.ws_ypixel = ypixel;
253 (void) ioctl(ptyfd, TIOCSWINSZ, &w);