]> andersk Git - openssh.git/blame - pty.c
Third time lucky
[openssh.git] / pty.c
CommitLineData
8efc0c15 1/*
5260325f 2 *
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 */
8efc0c15 15
16#include "includes.h"
17RCSID("$Id$");
18
5260325f 19#include "pty.h"
20#include "ssh.h"
21
e1a9c08d 22#ifdef HAVE_PTY_H
e1a9c08d 23#include <pty.h>
24#endif /* HAVE_PTY_H */
25
8efc0c15 26/* 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
aa3378df 35/*
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 */
8efc0c15 41
5260325f 42int
a408af76 43pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
8efc0c15 44{
a408af76 45#if defined(HAVE_OPENPTY) || defined(BSD4_4)
5260325f 46 /* openpty(3) exists in OSF/1 and some other os'es */
a408af76 47 char buf[64];
5260325f 48 int i;
8efc0c15 49
a408af76 50 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
5260325f 51 if (i < 0) {
52 error("openpty: %.100s", strerror(errno));
53 return 0;
54 }
a408af76 55 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
5260325f 56 return 1;
8efc0c15 57#else /* HAVE_OPENPTY */
58#ifdef HAVE__GETPTY
aa3378df 59 /*
60 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
61 * pty's automagically when needed
62 */
5260325f 63 char *slave;
64
65 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
66 if (slave == NULL) {
67 error("_getpty: %.100s", strerror(errno));
68 return 0;
69 }
a408af76 70 strlcpy(namebuf, slave, namebuflen);
5260325f 71 /* 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;
8efc0c15 79#else /* HAVE__GETPTY */
80#ifdef HAVE_DEV_PTMX
aa3378df 81 /*
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 */
5260325f 85 int ptm;
86 char *pts;
87
88 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.");
a408af76 104 strlcpy(namebuf, pts, namebuflen);
5260325f 105 *ptyfd = ptm;
106
107 /* 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 }
aa3378df 114 /* Push the appropriate streams modules, as described in Solaris pts(7). */
5260325f 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));
121 return 1;
8efc0c15 122#else /* HAVE_DEV_PTMX */
123#ifdef HAVE_DEV_PTS_AND_PTC
5260325f 124 /* AIX-style pty code. */
125 const char *name;
8efc0c15 126
5260325f 127 *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;
131 }
132 name = ttyname(*ptyfd);
133 if (!name)
134 fatal("Open of /dev/ptc returns device for which ttyname fails.");
a408af76 135 strlcpy(namebuf, name, namebuflen);
5260325f 136 *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;
8efc0c15 144#else /* HAVE_DEV_PTS_AND_PTC */
5260325f 145 /* BSD-style pty code. */
146 char buf[64];
147 int i;
aa3378df 148 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
5260325f 149 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;
a408af76 159 snprintf(namebuf, sizeof namebuflen, "/dev/tty%c%c",
160 ptymajors[i / num_minors], ptyminors[i % num_minors]);
5260325f 161
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;
8efc0c15 170 }
5260325f 171 return 0;
8efc0c15 172#endif /* HAVE_DEV_PTS_AND_PTC */
173#endif /* HAVE_DEV_PTMX */
174#endif /* HAVE__GETPTY */
175#endif /* HAVE_OPENPTY */
176}
177
5260325f 178/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
8efc0c15 179
5260325f 180void
181pty_release(const char *ttyname)
8efc0c15 182{
5260325f 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));
8efc0c15 187}
188
189/* Makes the tty the processes controlling tty and sets it to sane modes. */
190
5260325f 191void
192pty_make_controlling_tty(int *ttyfd, const char *ttyname)
8efc0c15 193{
5260325f 194 int fd;
8efc0c15 195
5260325f 196 /* First disconnect from the old controlling tty. */
8efc0c15 197#ifdef TIOCNOTTY
5260325f 198 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
199 if (fd >= 0) {
200 (void) ioctl(fd, TIOCNOTTY, NULL);
201 close(fd);
202 }
8efc0c15 203#endif /* TIOCNOTTY */
5260325f 204 if (setsid() < 0)
205 error("setsid: %.100s", strerror(errno));
206
aa3378df 207 /*
208 * Verify that we are successfully disconnected from the controlling
209 * tty.
210 */
5260325f 211 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. */
8efc0c15 217#ifdef TIOCSCTTY
5260325f 218 debug("Setting controlling tty using TIOCSCTTY.");
aa3378df 219 /*
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 */
5260325f 224 ioctl(*ttyfd, TIOCSCTTY, NULL);
8efc0c15 225#endif /* TIOCSCTTY */
5260325f 226 fd = open(ttyname, O_RDWR);
227 if (fd < 0)
228 error("%.100s: %.100s", ttyname, strerror(errno));
229 else
230 close(fd);
231
232 /* 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 }
8efc0c15 240}
241
242/* Changes the window size associated with the pty. */
243
5260325f 244void
245pty_change_window_size(int ptyfd, int row, int col,
246 int xpixel, int ypixel)
8efc0c15 247{
5260325f 248 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);
8efc0c15 254}
This page took 0.095004 seconds and 5 git commands to generate.