blob: acd81c9ba491ae25188fa63f673db8e89e071acb [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 Millerfce16481999-12-08 08:53:52 +110017RCSID("$Id: pty.c,v 1.7 1999/12/07 21:53:52 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
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
23#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
24#undef HAVE_DEV_PTMX
25#endif
26
Damien Millerfce16481999-12-08 08:53:52 +110027#ifdef HAVE_PTY_H
28# include <pty.h>
29#endif
30#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
31# include <sys/stropts.h>
32#endif
33
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034#ifndef O_NOCTTY
35#define O_NOCTTY 0
36#endif
37
Damien Miller5428f641999-11-25 11:54:57 +110038/*
39 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
40 * nonzero if a pty was successfully allocated. On success, open file
41 * descriptors for the pty and tty sides and the name of the tty side are
42 * returned (the buffer must be able to hold at least 64 characters).
43 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Miller95def091999-11-25 00:26:21 +110045int
Damien Miller037a0dc1999-12-07 15:38:31 +110046pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047{
Damien Miller037a0dc1999-12-07 15:38:31 +110048#if defined(HAVE_OPENPTY) || defined(BSD4_4)
Damien Miller95def091999-11-25 00:26:21 +110049 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller037a0dc1999-12-07 15:38:31 +110050 char buf[64];
Damien Miller95def091999-11-25 00:26:21 +110051 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller037a0dc1999-12-07 15:38:31 +110053 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110054 if (i < 0) {
55 error("openpty: %.100s", strerror(errno));
56 return 0;
57 }
Damien Miller037a0dc1999-12-07 15:38:31 +110058 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110059 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060#else /* HAVE_OPENPTY */
61#ifdef HAVE__GETPTY
Damien Miller5428f641999-11-25 11:54:57 +110062 /*
63 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
64 * pty's automagically when needed
65 */
Damien Miller95def091999-11-25 00:26:21 +110066 char *slave;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Damien Miller95def091999-11-25 00:26:21 +110068 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
69 if (slave == NULL) {
70 error("_getpty: %.100s", strerror(errno));
71 return 0;
72 }
Damien Miller037a0dc1999-12-07 15:38:31 +110073 strlcpy(namebuf, slave, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +110074 /* Open the slave side. */
75 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
76 if (*ttyfd < 0) {
77 error("%.200s: %.100s", namebuf, strerror(errno));
78 close(*ptyfd);
79 return 0;
80 }
81 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082#else /* HAVE__GETPTY */
83#ifdef HAVE_DEV_PTMX
Damien Miller5428f641999-11-25 11:54:57 +110084 /*
85 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
86 * also has bsd-style ptys, but they simply do not work.)
87 */
Damien Miller95def091999-11-25 00:26:21 +110088 int ptm;
89 char *pts;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller95def091999-11-25 00:26:21 +110091 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
92 if (ptm < 0) {
93 error("/dev/ptmx: %.100s", strerror(errno));
94 return 0;
95 }
96 if (grantpt(ptm) < 0) {
97 error("grantpt: %.100s", strerror(errno));
98 return 0;
99 }
100 if (unlockpt(ptm) < 0) {
101 error("unlockpt: %.100s", strerror(errno));
102 return 0;
103 }
104 pts = ptsname(ptm);
105 if (pts == NULL)
106 error("Slave pty side name could not be obtained.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100107 strlcpy(namebuf, pts, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100108 *ptyfd = ptm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller95def091999-11-25 00:26:21 +1100110 /* Open the slave side. */
111 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
112 if (*ttyfd < 0) {
113 error("%.100s: %.100s", namebuf, strerror(errno));
114 close(*ptyfd);
115 return 0;
116 }
Damien Miller5428f641999-11-25 11:54:57 +1100117 /* Push the appropriate streams modules, as described in Solaris pts(7). */
Damien Miller95def091999-11-25 00:26:21 +1100118 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
119 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
120 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
121 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
122 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
123 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
124 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125#else /* HAVE_DEV_PTMX */
126#ifdef HAVE_DEV_PTS_AND_PTC
Damien Miller95def091999-11-25 00:26:21 +1100127 /* AIX-style pty code. */
128 const char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Damien Miller95def091999-11-25 00:26:21 +1100130 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
131 if (*ptyfd < 0) {
132 error("Could not open /dev/ptc: %.100s", strerror(errno));
133 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134 }
Damien Miller95def091999-11-25 00:26:21 +1100135 name = ttyname(*ptyfd);
136 if (!name)
137 fatal("Open of /dev/ptc returns device for which ttyname fails.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100138 strlcpy(namebuf, name, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100139 *ttyfd = open(name, O_RDWR | O_NOCTTY);
140 if (*ttyfd < 0) {
141 error("Could not open pty slave side %.100s: %.100s",
142 name, strerror(errno));
143 close(*ptyfd);
144 return 0;
145 }
146 return 1;
147#else /* HAVE_DEV_PTS_AND_PTC */
148 /* BSD-style pty code. */
149 char buf[64];
150 int i;
Damien Miller5428f641999-11-25 11:54:57 +1100151 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
Damien Miller95def091999-11-25 00:26:21 +1100152 const char *ptyminors = "0123456789abcdef";
153 int num_minors = strlen(ptyminors);
154 int num_ptys = strlen(ptymajors) * num_minors;
155
156 for (i = 0; i < num_ptys; i++) {
157 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
158 ptyminors[i % num_minors]);
159 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
160 if (*ptyfd < 0)
161 continue;
Damien Miller037a0dc1999-12-07 15:38:31 +1100162 snprintf(namebuf, sizeof namebuflen, "/dev/tty%c%c",
163 ptymajors[i / num_minors], ptyminors[i % num_minors]);
Damien Miller95def091999-11-25 00:26:21 +1100164
165 /* Open the slave side. */
166 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
167 if (*ttyfd < 0) {
168 error("%.100s: %.100s", namebuf, strerror(errno));
169 close(*ptyfd);
170 return 0;
171 }
172 return 1;
173 }
174 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175#endif /* HAVE_DEV_PTS_AND_PTC */
176#endif /* HAVE_DEV_PTMX */
177#endif /* HAVE__GETPTY */
178#endif /* HAVE_OPENPTY */
179}
180
Damien Miller95def091999-11-25 00:26:21 +1100181/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Damien Miller95def091999-11-25 00:26:21 +1100183void
184pty_release(const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Damien Miller95def091999-11-25 00:26:21 +1100186 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
187 debug("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
188 if (chmod(ttyname, (mode_t) 0666) < 0)
189 debug("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
192/* Makes the tty the processes controlling tty and sets it to sane modes. */
193
Damien Miller95def091999-11-25 00:26:21 +1100194void
195pty_make_controlling_tty(int *ttyfd, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196{
Damien Miller95def091999-11-25 00:26:21 +1100197 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198
Damien Miller95def091999-11-25 00:26:21 +1100199 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200#ifdef TIOCNOTTY
Damien Miller95def091999-11-25 00:26:21 +1100201 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
202 if (fd >= 0) {
203 (void) ioctl(fd, TIOCNOTTY, NULL);
204 close(fd);
205 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206#endif /* TIOCNOTTY */
Damien Miller95def091999-11-25 00:26:21 +1100207 if (setsid() < 0)
208 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209
Damien Miller5428f641999-11-25 11:54:57 +1100210 /*
211 * Verify that we are successfully disconnected from the controlling
212 * tty.
213 */
Damien Miller95def091999-11-25 00:26:21 +1100214 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
215 if (fd >= 0) {
216 error("Failed to disconnect from controlling tty.");
217 close(fd);
218 }
219 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100221 debug("Setting controlling tty using TIOCSCTTY.");
Damien Miller5428f641999-11-25 11:54:57 +1100222 /*
223 * We ignore errors from this, because HPSUX defines TIOCSCTTY, but
224 * returns EINVAL with these arguments, and there is absolutely no
225 * documentation.
226 */
Damien Miller95def091999-11-25 00:26:21 +1100227 ioctl(*ttyfd, TIOCSCTTY, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228#endif /* TIOCSCTTY */
Damien Miller95def091999-11-25 00:26:21 +1100229 fd = open(ttyname, O_RDWR);
230 if (fd < 0)
231 error("%.100s: %.100s", ttyname, strerror(errno));
232 else
233 close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234
Damien Miller95def091999-11-25 00:26:21 +1100235 /* Verify that we now have a controlling tty. */
236 fd = open("/dev/tty", O_WRONLY);
237 if (fd < 0)
238 error("open /dev/tty failed - could not set controlling tty: %.100s",
239 strerror(errno));
240 else {
241 close(fd);
242 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}
244
245/* Changes the window size associated with the pty. */
246
Damien Miller95def091999-11-25 00:26:21 +1100247void
248pty_change_window_size(int ptyfd, int row, int col,
249 int xpixel, int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 struct winsize w;
252 w.ws_row = row;
253 w.ws_col = col;
254 w.ws_xpixel = xpixel;
255 w.ws_ypixel = ypixel;
256 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257}