blob: 4f8fbd21b06a475aa9a51041f78ff636ae64ab5e [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * pty.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Fri Mar 17 04:37:25 1995 ylo
11 *
12 * Allocating a pseudo-terminal, and making it the controlling tty.
13 *
14 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller037a0dc1999-12-07 15:38:31 +110017RCSID("$Id: pty.c,v 1.6 1999/12/07 04:38:32 damien Exp $");
Damien Miller95def091999-11-25 00:26:21 +110018
19#include "pty.h"
20#include "ssh.h"
Damien Miller356a0b01999-11-08 15:30:59 +110021
22#ifdef HAVE_PTY_H
Damien Miller356a0b01999-11-08 15:30:59 +110023#include <pty.h>
24#endif /* HAVE_PTY_H */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
27#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
28#undef HAVE_DEV_PTMX
29#endif
30
31#ifndef O_NOCTTY
32#define O_NOCTTY 0
33#endif
34
Damien Miller5428f641999-11-25 11:54:57 +110035/*
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).
40 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
Damien Miller95def091999-11-25 00:26:21 +110042int
Damien Miller037a0dc1999-12-07 15:38:31 +110043pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044{
Damien Miller037a0dc1999-12-07 15:38:31 +110045#if defined(HAVE_OPENPTY) || defined(BSD4_4)
Damien Miller95def091999-11-25 00:26:21 +110046 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller037a0dc1999-12-07 15:38:31 +110047 char buf[64];
Damien Miller95def091999-11-25 00:26:21 +110048 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
Damien Miller037a0dc1999-12-07 15:38:31 +110050 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110051 if (i < 0) {
52 error("openpty: %.100s", strerror(errno));
53 return 0;
54 }
Damien Miller037a0dc1999-12-07 15:38:31 +110055 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110056 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#else /* HAVE_OPENPTY */
58#ifdef HAVE__GETPTY
Damien Miller5428f641999-11-25 11:54:57 +110059 /*
60 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
61 * pty's automagically when needed
62 */
Damien Miller95def091999-11-25 00:26:21 +110063 char *slave;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Damien Miller95def091999-11-25 00:26:21 +110065 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
66 if (slave == NULL) {
67 error("_getpty: %.100s", strerror(errno));
68 return 0;
69 }
Damien Miller037a0dc1999-12-07 15:38:31 +110070 strlcpy(namebuf, slave, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +110071 /* Open the slave side. */
72 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
73 if (*ttyfd < 0) {
74 error("%.200s: %.100s", namebuf, strerror(errno));
75 close(*ptyfd);
76 return 0;
77 }
78 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#else /* HAVE__GETPTY */
80#ifdef HAVE_DEV_PTMX
Damien Miller5428f641999-11-25 11:54:57 +110081 /*
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.)
84 */
Damien Miller95def091999-11-25 00:26:21 +110085 int ptm;
86 char *pts;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller95def091999-11-25 00:26:21 +110088 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
89 if (ptm < 0) {
90 error("/dev/ptmx: %.100s", strerror(errno));
91 return 0;
92 }
93 if (grantpt(ptm) < 0) {
94 error("grantpt: %.100s", strerror(errno));
95 return 0;
96 }
97 if (unlockpt(ptm) < 0) {
98 error("unlockpt: %.100s", strerror(errno));
99 return 0;
100 }
101 pts = ptsname(ptm);
102 if (pts == NULL)
103 error("Slave pty side name could not be obtained.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100104 strlcpy(namebuf, pts, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100105 *ptyfd = ptm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller95def091999-11-25 00:26:21 +1100107 /* Open the slave side. */
108 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
109 if (*ttyfd < 0) {
110 error("%.100s: %.100s", namebuf, strerror(errno));
111 close(*ptyfd);
112 return 0;
113 }
Damien Miller5428f641999-11-25 11:54:57 +1100114 /* Push the appropriate streams modules, as described in Solaris pts(7). */
Damien Miller95def091999-11-25 00:26:21 +1100115 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));
121 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122#else /* HAVE_DEV_PTMX */
123#ifdef HAVE_DEV_PTS_AND_PTC
Damien Miller95def091999-11-25 00:26:21 +1100124 /* AIX-style pty code. */
125 const char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
128 if (*ptyfd < 0) {
129 error("Could not open /dev/ptc: %.100s", strerror(errno));
130 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131 }
Damien Miller95def091999-11-25 00:26:21 +1100132 name = ttyname(*ptyfd);
133 if (!name)
134 fatal("Open of /dev/ptc returns device for which ttyname fails.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100135 strlcpy(namebuf, name, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100136 *ttyfd = open(name, O_RDWR | O_NOCTTY);
137 if (*ttyfd < 0) {
138 error("Could not open pty slave side %.100s: %.100s",
139 name, strerror(errno));
140 close(*ptyfd);
141 return 0;
142 }
143 return 1;
144#else /* HAVE_DEV_PTS_AND_PTC */
145 /* BSD-style pty code. */
146 char buf[64];
147 int i;
Damien Miller5428f641999-11-25 11:54:57 +1100148 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
Damien Miller95def091999-11-25 00:26:21 +1100149 const char *ptyminors = "0123456789abcdef";
150 int num_minors = strlen(ptyminors);
151 int num_ptys = strlen(ptymajors) * num_minors;
152
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);
157 if (*ptyfd < 0)
158 continue;
Damien Miller037a0dc1999-12-07 15:38:31 +1100159 snprintf(namebuf, sizeof namebuflen, "/dev/tty%c%c",
160 ptymajors[i / num_minors], ptyminors[i % num_minors]);
Damien Miller95def091999-11-25 00:26:21 +1100161
162 /* Open the slave side. */
163 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
164 if (*ttyfd < 0) {
165 error("%.100s: %.100s", namebuf, strerror(errno));
166 close(*ptyfd);
167 return 0;
168 }
169 return 1;
170 }
171 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172#endif /* HAVE_DEV_PTS_AND_PTC */
173#endif /* HAVE_DEV_PTMX */
174#endif /* HAVE__GETPTY */
175#endif /* HAVE_OPENPTY */
176}
177
Damien Miller95def091999-11-25 00:26:21 +1100178/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller95def091999-11-25 00:26:21 +1100180void
181pty_release(const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182{
Damien Miller95def091999-11-25 00:26:21 +1100183 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));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187}
188
189/* Makes the tty the processes controlling tty and sets it to sane modes. */
190
Damien Miller95def091999-11-25 00:26:21 +1100191void
192pty_make_controlling_tty(int *ttyfd, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193{
Damien Miller95def091999-11-25 00:26:21 +1100194 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Miller95def091999-11-25 00:26:21 +1100196 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197#ifdef TIOCNOTTY
Damien Miller95def091999-11-25 00:26:21 +1100198 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
199 if (fd >= 0) {
200 (void) ioctl(fd, TIOCNOTTY, NULL);
201 close(fd);
202 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203#endif /* TIOCNOTTY */
Damien Miller95def091999-11-25 00:26:21 +1100204 if (setsid() < 0)
205 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Miller5428f641999-11-25 11:54:57 +1100207 /*
208 * Verify that we are successfully disconnected from the controlling
209 * tty.
210 */
Damien Miller95def091999-11-25 00:26:21 +1100211 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
212 if (fd >= 0) {
213 error("Failed to disconnect from controlling tty.");
214 close(fd);
215 }
216 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100218 debug("Setting controlling tty using TIOCSCTTY.");
Damien Miller5428f641999-11-25 11:54:57 +1100219 /*
220 * We ignore errors from this, because HPSUX defines TIOCSCTTY, but
221 * returns EINVAL with these arguments, and there is absolutely no
222 * documentation.
223 */
Damien Miller95def091999-11-25 00:26:21 +1100224 ioctl(*ttyfd, TIOCSCTTY, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225#endif /* TIOCSCTTY */
Damien Miller95def091999-11-25 00:26:21 +1100226 fd = open(ttyname, O_RDWR);
227 if (fd < 0)
228 error("%.100s: %.100s", ttyname, strerror(errno));
229 else
230 close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Damien Miller95def091999-11-25 00:26:21 +1100232 /* Verify that we now have a controlling tty. */
233 fd = open("/dev/tty", O_WRONLY);
234 if (fd < 0)
235 error("open /dev/tty failed - could not set controlling tty: %.100s",
236 strerror(errno));
237 else {
238 close(fd);
239 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
242/* Changes the window size associated with the pty. */
243
Damien Miller95def091999-11-25 00:26:21 +1100244void
245pty_change_window_size(int ptyfd, int row, int col,
246 int xpixel, int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247{
Damien Miller95def091999-11-25 00:26:21 +1100248 struct winsize w;
249 w.ws_row = row;
250 w.ws_col = col;
251 w.ws_xpixel = xpixel;
252 w.ws_ypixel = ypixel;
253 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}