]>
Commit | Line | Data |
---|---|---|
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" | |
17 | RCSID("$Id$"); | |
18 | ||
95f1eccc | 19 | #include <util.h> |
5260325f | 20 | #include "pty.h" |
21 | #include "ssh.h" | |
22 | ||
8946db53 | 23 | #ifdef HAVE_DEV_PTMX |
24 | #include <sys/stropts.h> | |
25 | #endif /* HAVE_DEV_PTMX */ | |
26 | ||
8efc0c15 | 27 | /* 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 | ||
6d8c4ea4 | 32 | #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 | ||
8efc0c15 | 39 | #ifndef O_NOCTTY |
40 | #define O_NOCTTY 0 | |
41 | #endif | |
42 | ||
aa3378df | 43 | /* |
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 | */ | |
8efc0c15 | 49 | |
5260325f | 50 | int |
a408af76 | 51 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
8efc0c15 | 52 | { |
a408af76 | 53 | #if defined(HAVE_OPENPTY) || defined(BSD4_4) |
5260325f | 54 | /* openpty(3) exists in OSF/1 and some other os'es */ |
a408af76 | 55 | char buf[64]; |
5260325f | 56 | int i; |
8efc0c15 | 57 | |
a408af76 | 58 | i = openpty(ptyfd, ttyfd, buf, NULL, NULL); |
5260325f | 59 | if (i < 0) { |
60 | error("openpty: %.100s", strerror(errno)); | |
61 | return 0; | |
62 | } | |
a408af76 | 63 | strlcpy(namebuf, buf, namebuflen); /* possible truncation */ |
5260325f | 64 | return 1; |
8efc0c15 | 65 | #else /* HAVE_OPENPTY */ |
66 | #ifdef HAVE__GETPTY | |
aa3378df | 67 | /* |
68 | * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more | |
69 | * pty's automagically when needed | |
70 | */ | |
5260325f | 71 | char *slave; |
72 | ||
73 | slave = _getpty(ptyfd, O_RDWR, 0622, 0); | |
74 | if (slave == NULL) { | |
75 | error("_getpty: %.100s", strerror(errno)); | |
76 | return 0; | |
77 | } | |
a408af76 | 78 | strlcpy(namebuf, slave, namebuflen); |
5260325f | 79 | /* 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; | |
8efc0c15 | 87 | #else /* HAVE__GETPTY */ |
88 | #ifdef HAVE_DEV_PTMX | |
aa3378df | 89 | /* |
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 | */ | |
5260325f | 93 | int ptm; |
94 | char *pts; | |
95 | ||
96 | 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."); | |
a408af76 | 112 | strlcpy(namebuf, pts, namebuflen); |
5260325f | 113 | *ptyfd = ptm; |
114 | ||
115 | /* 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 | } | |
aa3378df | 122 | /* Push the appropriate streams modules, as described in Solaris pts(7). */ |
5260325f | 123 | 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; | |
8efc0c15 | 130 | #else /* HAVE_DEV_PTMX */ |
131 | #ifdef HAVE_DEV_PTS_AND_PTC | |
5260325f | 132 | /* AIX-style pty code. */ |
133 | const char *name; | |
8efc0c15 | 134 | |
5260325f | 135 | *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; | |
139 | } | |
140 | name = ttyname(*ptyfd); | |
141 | if (!name) | |
142 | fatal("Open of /dev/ptc returns device for which ttyname fails."); | |
a408af76 | 143 | strlcpy(namebuf, name, namebuflen); |
5260325f | 144 | *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; | |
8efc0c15 | 152 | #else /* HAVE_DEV_PTS_AND_PTC */ |
5260325f | 153 | /* BSD-style pty code. */ |
154 | char buf[64]; | |
155 | int i; | |
aa3378df | 156 | const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
5260325f | 157 | 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; | |
95f1eccc | 167 | snprintf(namebuf, namebuflen, "/dev/tty%c%c", |
a408af76 | 168 | ptymajors[i / num_minors], ptyminors[i % num_minors]); |
5260325f | 169 | |
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; | |
8efc0c15 | 178 | } |
5260325f | 179 | return 0; |
8efc0c15 | 180 | #endif /* HAVE_DEV_PTS_AND_PTC */ |
181 | #endif /* HAVE_DEV_PTMX */ | |
182 | #endif /* HAVE__GETPTY */ | |
183 | #endif /* HAVE_OPENPTY */ | |
184 | } | |
185 | ||
5260325f | 186 | /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ |
8efc0c15 | 187 | |
5260325f | 188 | void |
189 | pty_release(const char *ttyname) | |
8efc0c15 | 190 | { |
5260325f | 191 | 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)); | |
8efc0c15 | 195 | } |
196 | ||
197 | /* Makes the tty the processes controlling tty and sets it to sane modes. */ | |
198 | ||
5260325f | 199 | void |
200 | pty_make_controlling_tty(int *ttyfd, const char *ttyname) | |
8efc0c15 | 201 | { |
5260325f | 202 | int fd; |
8efc0c15 | 203 | |
5260325f | 204 | /* First disconnect from the old controlling tty. */ |
8efc0c15 | 205 | #ifdef TIOCNOTTY |
5260325f | 206 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
207 | if (fd >= 0) { | |
208 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
209 | close(fd); | |
210 | } | |
8efc0c15 | 211 | #endif /* TIOCNOTTY */ |
5260325f | 212 | if (setsid() < 0) |
213 | error("setsid: %.100s", strerror(errno)); | |
214 | ||
aa3378df | 215 | /* |
216 | * Verify that we are successfully disconnected from the controlling | |
217 | * tty. | |
218 | */ | |
5260325f | 219 | 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. */ | |
8efc0c15 | 225 | #ifdef TIOCSCTTY |
5260325f | 226 | debug("Setting controlling tty using TIOCSCTTY."); |
aa3378df | 227 | /* |
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 | */ | |
5260325f | 232 | ioctl(*ttyfd, TIOCSCTTY, NULL); |
8efc0c15 | 233 | #endif /* TIOCSCTTY */ |
5260325f | 234 | fd = open(ttyname, O_RDWR); |
235 | if (fd < 0) | |
236 | error("%.100s: %.100s", ttyname, strerror(errno)); | |
237 | else | |
238 | close(fd); | |
239 | ||
240 | /* 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 | } | |
8efc0c15 | 248 | } |
249 | ||
250 | /* Changes the window size associated with the pty. */ | |
251 | ||
5260325f | 252 | void |
253 | pty_change_window_size(int ptyfd, int row, int col, | |
254 | int xpixel, int ypixel) | |
8efc0c15 | 255 | { |
5260325f | 256 | 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); | |
8efc0c15 | 262 | } |