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