1 |
/*----------------------------------------------------------------------* |
2 |
* File: ptytty.C |
3 |
*----------------------------------------------------------------------* |
4 |
* |
5 |
* All portions of code are copyright by their respective author/s. |
6 |
* Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
7 |
* Copyright (c) 2004-2006 Marc Lehmann <schmorp@schmorp.de> |
8 |
* Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it> |
9 |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
12 |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* (at your option) any later version. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
23 |
*---------------------------------------------------------------------*/ |
24 |
|
25 |
#include "config.h" |
26 |
|
27 |
#include "ptytty.h" |
28 |
|
29 |
#include <cstdlib> |
30 |
#include <cstdio> |
31 |
#include <cstring> |
32 |
|
33 |
#include <sys/types.h> |
34 |
#include <sys/stat.h> |
35 |
#include <unistd.h> |
36 |
#include <fcntl.h> |
37 |
|
38 |
#ifdef HAVE_SYS_IOCTL_H |
39 |
# include <sys/ioctl.h> |
40 |
#endif |
41 |
#if defined(HAVE_SYS_STROPTS_H) |
42 |
# include <sys/stropts.h> /* for I_PUSH */ |
43 |
#endif |
44 |
#if defined(HAVE_ISASTREAM) && defined(HAVE_STROPTS_H) |
45 |
# include <stropts.h> |
46 |
#endif |
47 |
#if defined(HAVE_PTY_H) |
48 |
# include <pty.h> |
49 |
#elif defined(HAVE_LIBUTIL_H) |
50 |
# include <libutil.h> |
51 |
#elif defined(HAVE_UTIL_H) |
52 |
# include <util.h> |
53 |
#endif |
54 |
#ifdef TTY_GID_SUPPORT |
55 |
#include <grp.h> |
56 |
#endif |
57 |
|
58 |
#ifndef O_NOCTTY |
59 |
# define O_NOCTTY 0 |
60 |
#endif |
61 |
|
62 |
///////////////////////////////////////////////////////////////////////////// |
63 |
|
64 |
/* ------------------------------------------------------------------------- * |
65 |
* GET PSEUDO TELETYPE - MASTER AND SLAVE * |
66 |
* ------------------------------------------------------------------------- */ |
67 |
/* |
68 |
* Returns pty file descriptor, or -1 on failure |
69 |
* If successful, ttydev is set to the name of the slave device. |
70 |
* fd_tty _may_ also be set to an open fd to the slave device |
71 |
*/ |
72 |
#if defined(UNIX98_PTY) |
73 |
|
74 |
static int |
75 |
get_pty (int *fd_tty, char **ttydev) |
76 |
{ |
77 |
int pfd; |
78 |
|
79 |
# if defined(HAVE_GETPT) |
80 |
pfd = getpt (); |
81 |
# elif defined(HAVE_POSIX_OPENPT) |
82 |
pfd = posix_openpt (O_RDWR | O_NOCTTY); |
83 |
# else |
84 |
# ifdef _AIX |
85 |
pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0); |
86 |
# else |
87 |
pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
88 |
# endif |
89 |
# endif |
90 |
|
91 |
if (pfd >= 0) |
92 |
{ |
93 |
if (grantpt (pfd) == 0 /* change slave permissions */ |
94 |
&& unlockpt (pfd) == 0) |
95 |
{ |
96 |
/* slave now unlocked */ |
97 |
*ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
98 |
return pfd; |
99 |
} |
100 |
|
101 |
close (pfd); |
102 |
} |
103 |
|
104 |
return -1; |
105 |
} |
106 |
|
107 |
#elif defined(HAVE_OPENPTY) |
108 |
|
109 |
static int |
110 |
get_pty (int *fd_tty, char **ttydev) |
111 |
{ |
112 |
int pfd; |
113 |
int res; |
114 |
|
115 |
res = openpty (&pfd, fd_tty, NULL, NULL, NULL); |
116 |
|
117 |
if (res != -1) |
118 |
{ |
119 |
*ttydev = strdup (ttyname (*fd_tty)); |
120 |
return pfd; |
121 |
} |
122 |
|
123 |
return -1; |
124 |
} |
125 |
|
126 |
#elif defined(HAVE__GETPTY) |
127 |
|
128 |
static int |
129 |
get_pty (int *fd_tty, char **ttydev) |
130 |
{ |
131 |
int pfd; |
132 |
char *slave; |
133 |
|
134 |
slave = _getpty (&pfd, O_RDWR | O_NOCTTY, 0622, 0); |
135 |
|
136 |
if (slave != NULL) |
137 |
{ |
138 |
*ttydev = strdup (slave); |
139 |
return pfd; |
140 |
} |
141 |
|
142 |
return -1; |
143 |
} |
144 |
|
145 |
#else |
146 |
|
147 |
/* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ |
148 |
static int |
149 |
get_pty (int *fd_tty, char **ttydev) |
150 |
{ |
151 |
int pfd; |
152 |
int i; |
153 |
char pty_name[32]; |
154 |
char tty_name[32]; |
155 |
const char *majors = "pqrstuvwxyzabcde"; |
156 |
const char *minors = "0123456789abcdef"; |
157 |
|
158 |
for (i = 0; i < 256; i++) |
159 |
{ |
160 |
snprintf (pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); |
161 |
snprintf (tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); |
162 |
|
163 |
if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
164 |
{ |
165 |
snprintf (pty_name, 32, "/dev/ptyp%d", i); |
166 |
snprintf (tty_name, 32, "/dev/ttyp%d", i); |
167 |
if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
168 |
continue; |
169 |
} |
170 |
|
171 |
if (access (tty_name, R_OK | W_OK) == 0) |
172 |
{ |
173 |
*ttydev = strdup (tty_name); |
174 |
return pfd; |
175 |
} |
176 |
|
177 |
close (pfd); |
178 |
} |
179 |
|
180 |
return -1; |
181 |
} |
182 |
|
183 |
#endif |
184 |
|
185 |
/*----------------------------------------------------------------------*/ |
186 |
/* |
187 |
* Returns tty file descriptor, or -1 on failure |
188 |
*/ |
189 |
static int |
190 |
get_tty (char *ttydev) |
191 |
{ |
192 |
return open (ttydev, O_RDWR | O_NOCTTY, 0); |
193 |
} |
194 |
|
195 |
/*----------------------------------------------------------------------*/ |
196 |
/* |
197 |
* Make our tty a controlling tty so that /dev/tty points to us |
198 |
*/ |
199 |
static int |
200 |
control_tty (int fd_tty) |
201 |
{ |
202 |
int fd; |
203 |
|
204 |
setsid (); |
205 |
|
206 |
#ifdef TIOCSCTTY |
207 |
ioctl (fd_tty, TIOCSCTTY, NULL); |
208 |
#else |
209 |
fd = open (ttyname (fd_tty), O_RDWR); |
210 |
if (fd >= 0) |
211 |
close (fd); |
212 |
#endif |
213 |
|
214 |
fd = open ("/dev/tty", O_WRONLY); |
215 |
if (fd < 0) |
216 |
return -1; /* fatal */ |
217 |
|
218 |
close (fd); |
219 |
|
220 |
return 0; |
221 |
} |
222 |
|
223 |
void |
224 |
ptytty::close_tty () |
225 |
{ |
226 |
if (tty < 0) |
227 |
return; |
228 |
|
229 |
close (tty); |
230 |
tty = -1; |
231 |
} |
232 |
|
233 |
bool |
234 |
ptytty::make_controlling_tty () |
235 |
{ |
236 |
return control_tty (tty) >= 0; |
237 |
} |
238 |
|
239 |
void |
240 |
ptytty::set_utf8_mode (bool on) |
241 |
{ |
242 |
#ifdef IUTF8 |
243 |
if (pty < 0) |
244 |
return; |
245 |
|
246 |
struct termios tio; |
247 |
|
248 |
if (tcgetattr (pty, &tio) != -1) |
249 |
{ |
250 |
tcflag_t new_cflag = tio.c_iflag; |
251 |
|
252 |
if (on) |
253 |
new_cflag |= IUTF8; |
254 |
else |
255 |
new_cflag &= ~IUTF8; |
256 |
|
257 |
if (new_cflag != tio.c_iflag) |
258 |
{ |
259 |
tio.c_iflag = new_cflag; |
260 |
tcsetattr (pty, TCSANOW, &tio); |
261 |
} |
262 |
} |
263 |
#endif |
264 |
} |
265 |
|
266 |
static struct ttyconf { |
267 |
gid_t gid; |
268 |
mode_t mode; |
269 |
|
270 |
ttyconf () |
271 |
{ |
272 |
#ifdef TTY_GID_SUPPORT |
273 |
struct group *gr = getgrnam ("tty"); |
274 |
|
275 |
if (gr) |
276 |
{ |
277 |
/* change group ownership of tty to "tty" */ |
278 |
mode = S_IRUSR | S_IWUSR | S_IWGRP; |
279 |
gid = gr->gr_gid; |
280 |
} |
281 |
else |
282 |
#endif /* TTY_GID_SUPPORT */ |
283 |
{ |
284 |
mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
285 |
gid = 0; |
286 |
} |
287 |
} |
288 |
} ttyconf; |
289 |
|
290 |
ptytty_unix::ptytty_unix () |
291 |
{ |
292 |
name = 0; |
293 |
#if UTMP_SUPPORT |
294 |
cmd_pid = 0; |
295 |
#endif |
296 |
} |
297 |
|
298 |
ptytty_unix::~ptytty_unix () |
299 |
{ |
300 |
#if UTMP_SUPPORT |
301 |
logout (); |
302 |
#endif |
303 |
put (); |
304 |
} |
305 |
|
306 |
void |
307 |
ptytty_unix::put () |
308 |
{ |
309 |
if (name) |
310 |
{ |
311 |
chmod (name, RESTORE_TTY_MODE); |
312 |
chown (name, 0, ttyconf.gid); |
313 |
} |
314 |
|
315 |
close_tty (); |
316 |
|
317 |
if (pty >= 0) |
318 |
close (pty); |
319 |
|
320 |
free (name); |
321 |
|
322 |
pty = tty = -1; |
323 |
name = 0; |
324 |
} |
325 |
|
326 |
bool |
327 |
ptytty_unix::get () |
328 |
{ |
329 |
/* get master (pty) */ |
330 |
if ((pty = get_pty (&tty, &name)) < 0) |
331 |
return false; |
332 |
|
333 |
/* get slave (tty) */ |
334 |
if (tty < 0) |
335 |
{ |
336 |
#ifndef NO_SETOWNER_TTYDEV |
337 |
chown (name, getuid (), ttyconf.gid); /* fail silently */ |
338 |
chmod (name, ttyconf.mode); |
339 |
# ifdef HAVE_REVOKE |
340 |
revoke (name); |
341 |
# endif |
342 |
#endif |
343 |
|
344 |
if ((tty = get_tty (name)) < 0) |
345 |
{ |
346 |
put (); |
347 |
return false; |
348 |
} |
349 |
} |
350 |
|
351 |
#if defined(I_PUSH) |
352 |
/* |
353 |
* Push STREAMS modules: |
354 |
* ptem: pseudo-terminal hardware emulation module. |
355 |
* ldterm: standard terminal line discipline. |
356 |
* ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. |
357 |
* |
358 |
* After we push the STREAMS modules, the first open () on the slave side |
359 |
* should make the "ptem" (or "ldterm" depending upon either which OS |
360 |
* version or which set of manual pages you have) module give us a |
361 |
* controlling terminal. We must already have close ()d the master side |
362 |
* fd in this child process before we push STREAMS modules on because the |
363 |
* documentation is really unclear about whether it is any close () on |
364 |
* the master side or the last close () - i.e. a proper STREAMS dismantling |
365 |
* close () - on the master side which causes a hang up to be sent |
366 |
* through - Geoff Wing |
367 |
*/ |
368 |
#if defined(HAVE_ISASTREAM) && defined(HAVE_STROPTS_H) |
369 |
if (isastream (tty) == 1) |
370 |
# endif |
371 |
{ |
372 |
ioctl (tty, I_PUSH, "ptem"); |
373 |
ioctl (tty, I_PUSH, "ldterm"); |
374 |
ioctl (tty, I_PUSH, "ttcompat"); |
375 |
} |
376 |
#endif |
377 |
|
378 |
#if UTMP_SUPPORT |
379 |
# if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) |
380 |
int fd_stdin = dup (STDIN_FILENO); |
381 |
dup2 (tty, STDIN_FILENO); |
382 |
|
383 |
utmp_pos = ttyslot (); |
384 |
|
385 |
dup2 (fd_stdin, STDIN_FILENO); |
386 |
close (fd_stdin); |
387 |
# endif |
388 |
#endif |
389 |
|
390 |
return true; |
391 |
} |
392 |
|