1 | // This file is part of libptytty. Do not make local modifications. |
|
|
2 | // http://software.schmorp.de/pkg/libptytty |
|
|
3 | |
|
|
4 | /*----------------------------------------------------------------------* |
1 | /*----------------------------------------------------------------------* |
5 | * File: ptytty.C |
2 | * File: ptytty.C |
6 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
7 | * |
4 | * |
8 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
9 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
6 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
10 | * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com> |
7 | * Copyright (c) 2004-2006 Marc Lehmann <schmorp@schmorp.de> |
11 | * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it> |
8 | * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it> |
12 | * |
9 | * |
13 | * This program is free software; you can redistribute it and/or modify |
10 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License as published by |
11 | * it under the terms of the GNU General Public License as published by |
15 | * the Free Software Foundation; either version 2 of the License, or |
12 | * the Free Software Foundation; either version 2 of the License, or |
… | |
… | |
23 | * You should have received a copy of the GNU General Public License |
20 | * You should have received a copy of the GNU General Public License |
24 | * along with this program; if not, write to the Free Software |
21 | * along with this program; if not, write to the Free Software |
25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
26 | *---------------------------------------------------------------------*/ |
23 | *---------------------------------------------------------------------*/ |
27 | |
24 | |
28 | #include "../config.h" |
25 | #include "config.h" |
29 | |
26 | |
30 | #include "ptytty.h" |
27 | #include "ptytty.h" |
31 | |
28 | |
32 | #include <cstdlib> |
29 | #include <stdlib.h> |
|
|
30 | #include <stdio.h> |
33 | #include <cstring> |
31 | #include <string.h> |
34 | #include <csignal> |
|
|
35 | |
32 | |
36 | #include <sys/types.h> |
33 | #include <sys/types.h> |
|
|
34 | #include <sys/stat.h> |
37 | #include <unistd.h> |
35 | #include <unistd.h> |
38 | #include <fcntl.h> |
36 | #include <fcntl.h> |
39 | |
37 | |
40 | #ifdef HAVE_SYS_IOCTL_H |
38 | #ifdef HAVE_SYS_IOCTL_H |
41 | # include <sys/ioctl.h> |
39 | # include <sys/ioctl.h> |
42 | #endif |
40 | #endif |
43 | #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H) |
41 | #ifdef HAVE_STROPTS_H |
44 | # include <sys/stropts.h> /* for I_PUSH */ |
|
|
45 | #endif |
|
|
46 | #ifdef HAVE_ISASTREAM |
|
|
47 | # include <stropts.h> |
42 | # include <stropts.h> |
48 | #endif |
43 | #endif |
49 | #if defined(HAVE_PTY_H) |
44 | #if defined(HAVE_PTY_H) |
50 | # include <pty.h> |
45 | # include <pty.h> |
51 | #elif defined(HAVE_LIBUTIL_H) |
46 | #elif defined(HAVE_LIBUTIL_H) |
… | |
… | |
55 | #endif |
50 | #endif |
56 | #ifdef TTY_GID_SUPPORT |
51 | #ifdef TTY_GID_SUPPORT |
57 | #include <grp.h> |
52 | #include <grp.h> |
58 | #endif |
53 | #endif |
59 | |
54 | |
60 | #include <cstdio> |
55 | #ifndef O_NOCTTY |
|
|
56 | # define O_NOCTTY 0 |
|
|
57 | #endif |
61 | |
58 | |
62 | ///////////////////////////////////////////////////////////////////////////// |
59 | ///////////////////////////////////////////////////////////////////////////// |
63 | |
60 | |
64 | /* ------------------------------------------------------------------------- * |
61 | /* ------------------------------------------------------------------------- * |
65 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
62 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
… | |
… | |
75 | get_pty (int *fd_tty, char **ttydev) |
72 | get_pty (int *fd_tty, char **ttydev) |
76 | { |
73 | { |
77 | int pfd; |
74 | int pfd; |
78 | |
75 | |
79 | # if defined(HAVE_GETPT) |
76 | # if defined(HAVE_GETPT) |
80 | pfd = getpt(); |
77 | pfd = getpt (); |
81 | # elif defined(HAVE_POSIX_OPENPT) |
78 | # elif defined(HAVE_POSIX_OPENPT) |
82 | pfd = posix_openpt (O_RDWR); |
79 | pfd = posix_openpt (O_RDWR | O_NOCTTY); |
83 | # else |
80 | # else |
|
|
81 | # ifdef _AIX |
84 | pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0); |
82 | pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0); |
|
|
83 | # else |
|
|
84 | pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
|
|
85 | # endif |
85 | # endif |
86 | # endif |
86 | |
87 | |
87 | if (pfd >= 0) |
88 | if (pfd >= 0) |
88 | { |
89 | { |
89 | if (grantpt (pfd) == 0 /* change slave permissions */ |
90 | if (grantpt (pfd) == 0 /* change slave permissions */ |
90 | && unlockpt (pfd) == 0) |
91 | && unlockpt (pfd) == 0) |
|
|
92 | { |
91 | { /* slave now unlocked */ |
93 | /* slave now unlocked */ |
92 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
94 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
93 | return pfd; |
95 | return pfd; |
94 | } |
96 | } |
95 | |
97 | |
96 | close (pfd); |
98 | close (pfd); |
… | |
… | |
122 | |
124 | |
123 | static int |
125 | static int |
124 | get_pty (int *fd_tty, char **ttydev) |
126 | get_pty (int *fd_tty, char **ttydev) |
125 | { |
127 | { |
126 | int pfd; |
128 | int pfd; |
|
|
129 | char *slave; |
127 | |
130 | |
128 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
131 | slave = _getpty (&pfd, O_RDWR | O_NOCTTY, 0622, 0); |
129 | |
132 | |
130 | if (*ttydev != NULL) |
133 | if (slave != NULL) |
131 | return pfd; |
|
|
132 | |
|
|
133 | return -1; |
|
|
134 | } |
|
|
135 | |
|
|
136 | #elif defined(HAVE_DEV_PTC) |
|
|
137 | |
|
|
138 | static int |
|
|
139 | get_pty (int *fd_tty, char **ttydev) |
|
|
140 | { |
|
|
141 | int pfd; |
|
|
142 | |
|
|
143 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
|
|
144 | { |
134 | { |
145 | *ttydev = strdup (ttyname (pfd)); |
|
|
146 | return pfd; |
|
|
147 | } |
|
|
148 | |
|
|
149 | return -1; |
|
|
150 | } |
|
|
151 | |
|
|
152 | #elif defined(HAVE_DEV_CLONE) |
|
|
153 | |
|
|
154 | static int |
|
|
155 | get_pty (int *fd_tty, char **ttydev) |
|
|
156 | { |
|
|
157 | int pfd; |
|
|
158 | |
|
|
159 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
|
|
160 | { |
|
|
161 | *ttydev = strdup (ptsname (pfd)); |
135 | *ttydev = strdup (slave); |
162 | return pfd; |
136 | return pfd; |
163 | } |
137 | } |
164 | |
138 | |
165 | return -1; |
139 | return -1; |
166 | } |
140 | } |
… | |
… | |
178 | const char *majors = "pqrstuvwxyzabcde"; |
152 | const char *majors = "pqrstuvwxyzabcde"; |
179 | const char *minors = "0123456789abcdef"; |
153 | const char *minors = "0123456789abcdef"; |
180 | |
154 | |
181 | for (i = 0; i < 256; i++) |
155 | for (i = 0; i < 256; i++) |
182 | { |
156 | { |
183 | snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); |
157 | snprintf (pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); |
184 | snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); |
158 | snprintf (tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); |
185 | |
159 | |
186 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
160 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
187 | { |
161 | { |
188 | snprintf(pty_name, 32, "/dev/ptyp%d", i); |
162 | snprintf (pty_name, 32, "/dev/ptyp%d", i); |
189 | snprintf(tty_name, 32, "/dev/ttyp%d", i); |
163 | snprintf (tty_name, 32, "/dev/ttyp%d", i); |
190 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
164 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
191 | continue; |
165 | continue; |
192 | } |
166 | } |
193 | |
167 | |
194 | if (access (tty_name, R_OK | W_OK) == 0) |
168 | if (access (tty_name, R_OK | W_OK) == 0) |
… | |
… | |
224 | { |
198 | { |
225 | int fd; |
199 | int fd; |
226 | |
200 | |
227 | setsid (); |
201 | setsid (); |
228 | |
202 | |
229 | #if defined(HAVE_DEV_PTMX) && defined(I_PUSH) |
|
|
230 | /* |
|
|
231 | * Push STREAMS modules: |
|
|
232 | * ptem: pseudo-terminal hardware emulation module. |
|
|
233 | * ldterm: standard terminal line discipline. |
|
|
234 | * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. |
|
|
235 | * |
|
|
236 | * After we push the STREAMS modules, the first open () on the slave side |
|
|
237 | * (i.e. the next section between the dashes giving us "tty opened OK") |
|
|
238 | * should make the "ptem" (or "ldterm" depending upon either which OS |
|
|
239 | * version or which set of manual pages you have) module give us a |
|
|
240 | * controlling terminal. We must already have close ()d the master side |
|
|
241 | * fd in this child process before we push STREAMS modules on because the |
|
|
242 | * documentation is really unclear about whether it is any close () on |
|
|
243 | * the master side or the last close () - i.e. a proper STREAMS dismantling |
|
|
244 | * close () - on the master side which causes a hang up to be sent |
|
|
245 | * through - Geoff Wing |
|
|
246 | */ |
|
|
247 | # ifdef HAVE_ISASTREAM |
|
|
248 | if (isastream (fd_tty) == 1) |
|
|
249 | # endif |
|
|
250 | { |
|
|
251 | ioctl (fd_tty, I_PUSH, "ptem"); |
|
|
252 | ioctl (fd_tty, I_PUSH, "ldterm"); |
|
|
253 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
|
|
254 | } |
|
|
255 | #endif |
|
|
256 | |
|
|
257 | #ifdef TIOCSCTTY |
203 | #ifdef TIOCSCTTY |
258 | ioctl (fd_tty, TIOCSCTTY, NULL); |
204 | ioctl (fd_tty, TIOCSCTTY, NULL); |
259 | #else |
205 | #else |
260 | fd = open (ttyname (fd_tty), O_RDWR); |
206 | fd = open (ttyname (fd_tty), O_RDWR); |
261 | if (fd >= 0) |
207 | if (fd >= 0) |
… | |
… | |
322 | { |
268 | { |
323 | #ifdef TTY_GID_SUPPORT |
269 | #ifdef TTY_GID_SUPPORT |
324 | struct group *gr = getgrnam ("tty"); |
270 | struct group *gr = getgrnam ("tty"); |
325 | |
271 | |
326 | if (gr) |
272 | if (gr) |
|
|
273 | { |
327 | { /* change group ownership of tty to "tty" */ |
274 | /* change group ownership of tty to "tty" */ |
328 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
275 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
329 | gid = gr->gr_gid; |
276 | gid = gr->gr_gid; |
330 | } |
277 | } |
331 | else |
278 | else |
332 | #endif /* TTY_GID_SUPPORT */ |
279 | #endif /* TTY_GID_SUPPORT */ |
333 | { |
280 | { |
334 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
281 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
335 | gid = 0; |
282 | gid = 0; |
336 | } |
283 | } |
337 | } |
284 | } |
… | |
… | |
377 | ptytty_unix::get () |
324 | ptytty_unix::get () |
378 | { |
325 | { |
379 | /* get master (pty) */ |
326 | /* get master (pty) */ |
380 | if ((pty = get_pty (&tty, &name)) < 0) |
327 | if ((pty = get_pty (&tty, &name)) < 0) |
381 | return false; |
328 | return false; |
382 | |
|
|
383 | fcntl (pty, F_SETFL, O_NONBLOCK); |
|
|
384 | |
329 | |
385 | /* get slave (tty) */ |
330 | /* get slave (tty) */ |
386 | if (tty < 0) |
331 | if (tty < 0) |
387 | { |
332 | { |
388 | #ifndef NO_SETOWNER_TTYDEV |
333 | #ifndef NO_SETOWNER_TTYDEV |
… | |
… | |
398 | put (); |
343 | put (); |
399 | return false; |
344 | return false; |
400 | } |
345 | } |
401 | } |
346 | } |
402 | |
347 | |
|
|
348 | #if defined(I_PUSH) |
|
|
349 | /* |
|
|
350 | * Push STREAMS modules: |
|
|
351 | * ptem: pseudo-terminal hardware emulation module. |
|
|
352 | * ldterm: standard terminal line discipline. |
|
|
353 | * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. |
|
|
354 | * |
|
|
355 | * On Solaris, a process can acquire a controlling terminal in the |
|
|
356 | * following ways: |
|
|
357 | * - open() of /dev/ptmx or of a slave device without O_NOCTTY |
|
|
358 | * - I_PUSH ioctl() of the "ptem" or "ldterm" module on a slave device |
|
|
359 | * The second case is problematic, because it cannot be disabled. |
|
|
360 | * Fortunately, Solaris (10 and 11 at least) provides an undocumented |
|
|
361 | * __IPUSH_NOCTTY ioctl which does not have this side-effect, so we |
|
|
362 | * use it if defined. See |
|
|
363 | * https://github.com/illumos/illumos-gate/blob/master/usr/src/uts/common/os/streamio.c#L3755 |
|
|
364 | * https://github.com/illumos/illumos-gate/blob/master/usr/src/uts/common/io/ptem.c#L203 |
|
|
365 | * https://github.com/illumos/illumos-gate/blob/master/usr/src/uts/common/io/ldterm.c#L794 |
|
|
366 | * Note that an open() of a slave device autoloads the modules, |
|
|
367 | * with __I_PUSH_NOCTTY, if xpg[46] mode is enabled (which requires |
|
|
368 | * linking /usr/lib/values-xpg[46].o). |
|
|
369 | * https://github.com/illumos/illumos-gate/blob/master/usr/src/lib/libc/port/sys/open.c#L173 |
|
|
370 | */ |
|
|
371 | |
|
|
372 | #ifdef __I_PUSH_NOCTTY |
|
|
373 | # define PT_I_PUSH __I_PUSH_NOCTTY |
|
|
374 | #else |
|
|
375 | # define PT_I_PUSH I_PUSH |
|
|
376 | #endif |
|
|
377 | |
|
|
378 | #if defined(HAVE_ISASTREAM) && defined(HAVE_STROPTS_H) |
|
|
379 | if (isastream (tty) == 1) |
|
|
380 | # endif |
|
|
381 | { |
|
|
382 | ioctl (tty, PT_I_PUSH, "ptem"); |
|
|
383 | ioctl (tty, PT_I_PUSH, "ldterm"); |
|
|
384 | ioctl (tty, PT_I_PUSH, "ttcompat"); |
|
|
385 | } |
|
|
386 | #endif |
|
|
387 | |
|
|
388 | #if UTMP_SUPPORT |
|
|
389 | # if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) |
|
|
390 | int fd_stdin = dup (STDIN_FILENO); |
|
|
391 | dup2 (tty, STDIN_FILENO); |
|
|
392 | |
|
|
393 | utmp_pos = ttyslot (); |
|
|
394 | |
|
|
395 | dup2 (fd_stdin, STDIN_FILENO); |
|
|
396 | close (fd_stdin); |
|
|
397 | # endif |
|
|
398 | #endif |
|
|
399 | |
403 | return true; |
400 | return true; |
404 | } |
401 | } |
405 | |
402 | |