blob: f7ef19ca74c522a1f096cafef5defb648b777f0c [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 Millera34a28b1999-12-14 10:47:15 +110017RCSID("$Id: pty.c,v 1.9 1999/12/13 23:47:16 damien Exp $");
Damien Miller95def091999-11-25 00:26:21 +110018
Damien Millera34a28b1999-12-14 10:47:15 +110019#include <util.h>
Damien Miller95def091999-11-25 00:26:21 +110020#include "pty.h"
21#include "ssh.h"
Damien Miller356a0b01999-11-08 15:30:59 +110022
Damien Millerbf1c9b21999-12-09 10:16:54 +110023#ifdef HAVE_DEV_PTMX
24#include <sys/stropts.h>
25#endif /* HAVE_DEV_PTMX */
26
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
28#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
29#undef HAVE_DEV_PTMX
30#endif
31
Damien Millerfce16481999-12-08 08:53:52 +110032#ifdef HAVE_PTY_H
33# include <pty.h>
34#endif
35#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
36# include <sys/stropts.h>
37#endif
38
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#ifndef O_NOCTTY
40#define O_NOCTTY 0
41#endif
42
Damien Miller5428f641999-11-25 11:54:57 +110043/*
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).
48 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
Damien Miller95def091999-11-25 00:26:21 +110050int
Damien Miller037a0dc1999-12-07 15:38:31 +110051pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052{
Damien Miller037a0dc1999-12-07 15:38:31 +110053#if defined(HAVE_OPENPTY) || defined(BSD4_4)
Damien Miller95def091999-11-25 00:26:21 +110054 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller037a0dc1999-12-07 15:38:31 +110055 char buf[64];
Damien Miller95def091999-11-25 00:26:21 +110056 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Damien Miller037a0dc1999-12-07 15:38:31 +110058 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110059 if (i < 0) {
60 error("openpty: %.100s", strerror(errno));
61 return 0;
62 }
Damien Miller037a0dc1999-12-07 15:38:31 +110063 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110064 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#else /* HAVE_OPENPTY */
66#ifdef HAVE__GETPTY
Damien Miller5428f641999-11-25 11:54:57 +110067 /*
68 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
69 * pty's automagically when needed
70 */
Damien Miller95def091999-11-25 00:26:21 +110071 char *slave;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller95def091999-11-25 00:26:21 +110073 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
74 if (slave == NULL) {
75 error("_getpty: %.100s", strerror(errno));
76 return 0;
77 }
Damien Miller037a0dc1999-12-07 15:38:31 +110078 strlcpy(namebuf, slave, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +110079 /* Open the slave side. */
80 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
81 if (*ttyfd < 0) {
82 error("%.200s: %.100s", namebuf, strerror(errno));
83 close(*ptyfd);
84 return 0;
85 }
86 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087#else /* HAVE__GETPTY */
88#ifdef HAVE_DEV_PTMX
Damien Miller5428f641999-11-25 11:54:57 +110089 /*
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.)
92 */
Damien Miller95def091999-11-25 00:26:21 +110093 int ptm;
94 char *pts;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller95def091999-11-25 00:26:21 +110096 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
97 if (ptm < 0) {
98 error("/dev/ptmx: %.100s", strerror(errno));
99 return 0;
100 }
101 if (grantpt(ptm) < 0) {
102 error("grantpt: %.100s", strerror(errno));
103 return 0;
104 }
105 if (unlockpt(ptm) < 0) {
106 error("unlockpt: %.100s", strerror(errno));
107 return 0;
108 }
109 pts = ptsname(ptm);
110 if (pts == NULL)
111 error("Slave pty side name could not be obtained.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100112 strlcpy(namebuf, pts, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100113 *ptyfd = ptm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115 /* Open the slave side. */
116 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
117 if (*ttyfd < 0) {
118 error("%.100s: %.100s", namebuf, strerror(errno));
119 close(*ptyfd);
120 return 0;
121 }
Damien Miller5428f641999-11-25 11:54:57 +1100122 /* Push the appropriate streams modules, as described in Solaris pts(7). */
Damien Miller95def091999-11-25 00:26:21 +1100123 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));
129 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130#else /* HAVE_DEV_PTMX */
131#ifdef HAVE_DEV_PTS_AND_PTC
Damien Miller95def091999-11-25 00:26:21 +1100132 /* AIX-style pty code. */
133 const char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Miller95def091999-11-25 00:26:21 +1100135 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
136 if (*ptyfd < 0) {
137 error("Could not open /dev/ptc: %.100s", strerror(errno));
138 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139 }
Damien Miller95def091999-11-25 00:26:21 +1100140 name = ttyname(*ptyfd);
141 if (!name)
142 fatal("Open of /dev/ptc returns device for which ttyname fails.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100143 strlcpy(namebuf, name, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100144 *ttyfd = open(name, O_RDWR | O_NOCTTY);
145 if (*ttyfd < 0) {
146 error("Could not open pty slave side %.100s: %.100s",
147 name, strerror(errno));
148 close(*ptyfd);
149 return 0;
150 }
151 return 1;
152#else /* HAVE_DEV_PTS_AND_PTC */
153 /* BSD-style pty code. */
154 char buf[64];
155 int i;
Damien Miller5428f641999-11-25 11:54:57 +1100156 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
Damien Miller95def091999-11-25 00:26:21 +1100157 const char *ptyminors = "0123456789abcdef";
158 int num_minors = strlen(ptyminors);
159 int num_ptys = strlen(ptymajors) * num_minors;
160
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);
165 if (*ptyfd < 0)
166 continue;
Damien Millera34a28b1999-12-14 10:47:15 +1100167 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
Damien Miller037a0dc1999-12-07 15:38:31 +1100168 ptymajors[i / num_minors], ptyminors[i % num_minors]);
Damien Miller95def091999-11-25 00:26:21 +1100169
170 /* Open the slave side. */
171 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
172 if (*ttyfd < 0) {
173 error("%.100s: %.100s", namebuf, strerror(errno));
174 close(*ptyfd);
175 return 0;
176 }
177 return 1;
178 }
179 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180#endif /* HAVE_DEV_PTS_AND_PTC */
181#endif /* HAVE_DEV_PTMX */
182#endif /* HAVE__GETPTY */
183#endif /* HAVE_OPENPTY */
184}
185
Damien Miller95def091999-11-25 00:26:21 +1100186/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187
Damien Miller95def091999-11-25 00:26:21 +1100188void
189pty_release(const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190{
Damien Miller95def091999-11-25 00:26:21 +1100191 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));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195}
196
197/* Makes the tty the processes controlling tty and sets it to sane modes. */
198
Damien Miller95def091999-11-25 00:26:21 +1100199void
200pty_make_controlling_tty(int *ttyfd, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201{
Damien Miller95def091999-11-25 00:26:21 +1100202 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Miller95def091999-11-25 00:26:21 +1100204 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205#ifdef TIOCNOTTY
Damien Miller95def091999-11-25 00:26:21 +1100206 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
207 if (fd >= 0) {
208 (void) ioctl(fd, TIOCNOTTY, NULL);
209 close(fd);
210 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211#endif /* TIOCNOTTY */
Damien Miller95def091999-11-25 00:26:21 +1100212 if (setsid() < 0)
213 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Miller5428f641999-11-25 11:54:57 +1100215 /*
216 * Verify that we are successfully disconnected from the controlling
217 * tty.
218 */
Damien Miller95def091999-11-25 00:26:21 +1100219 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
220 if (fd >= 0) {
221 error("Failed to disconnect from controlling tty.");
222 close(fd);
223 }
224 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100226 debug("Setting controlling tty using TIOCSCTTY.");
Damien Miller5428f641999-11-25 11:54:57 +1100227 /*
228 * We ignore errors from this, because HPSUX defines TIOCSCTTY, but
229 * returns EINVAL with these arguments, and there is absolutely no
230 * documentation.
231 */
Damien Miller95def091999-11-25 00:26:21 +1100232 ioctl(*ttyfd, TIOCSCTTY, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233#endif /* TIOCSCTTY */
Damien Miller95def091999-11-25 00:26:21 +1100234 fd = open(ttyname, O_RDWR);
235 if (fd < 0)
236 error("%.100s: %.100s", ttyname, strerror(errno));
237 else
238 close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Miller95def091999-11-25 00:26:21 +1100240 /* Verify that we now have a controlling tty. */
241 fd = open("/dev/tty", O_WRONLY);
242 if (fd < 0)
243 error("open /dev/tty failed - could not set controlling tty: %.100s",
244 strerror(errno));
245 else {
246 close(fd);
247 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248}
249
250/* Changes the window size associated with the pty. */
251
Damien Miller95def091999-11-25 00:26:21 +1100252void
253pty_change_window_size(int ptyfd, int row, int col,
254 int xpixel, int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255{
Damien Miller95def091999-11-25 00:26:21 +1100256 struct winsize w;
257 w.ws_row = row;
258 w.ws_col = col;
259 w.ws_xpixel = xpixel;
260 w.ws_ypixel = ypixel;
261 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262}