1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: ptytty.c |
2 | * File: ptytty.c |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * $Id: ptytty.C,v 1.4 2003/12/02 21:49:46 pcg Exp $ |
|
|
5 | * |
4 | * |
6 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
7 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
6 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
8 | * |
7 | * |
9 | * This program is free software; you can redistribute it and/or modify |
8 | * This program is free software; you can redistribute it and/or modify |
… | |
… | |
25 | |
24 | |
26 | /* |
25 | /* |
27 | * Try to be self-contained except for the above autoconfig'd defines |
26 | * Try to be self-contained except for the above autoconfig'd defines |
28 | */ |
27 | */ |
29 | |
28 | |
|
|
29 | #if 0 |
|
|
30 | #ifdef HAVE_GETPT |
|
|
31 | # define _GNU_SOURCE |
|
|
32 | #endif |
|
|
33 | #endif |
|
|
34 | |
30 | #include <stdio.h> |
35 | #include <stdio.h> |
31 | #ifdef HAVE_STDLIB_H |
36 | #ifdef HAVE_STDLIB_H |
32 | # include <stdlib.h> |
37 | # include <stdlib.h> |
33 | #endif |
38 | #endif |
34 | #ifdef HAVE_SYS_TYPES_H |
39 | #ifdef HAVE_SYS_TYPES_H |
… | |
… | |
64 | * If successful, ttydev is set to the name of the slave device. |
69 | * If successful, ttydev is set to the name of the slave device. |
65 | * fd_tty _may_ also be set to an open fd to the slave device |
70 | * fd_tty _may_ also be set to an open fd to the slave device |
66 | */ |
71 | */ |
67 | /* EXTPROTO */ |
72 | /* EXTPROTO */ |
68 | int |
73 | int |
69 | rxvt_get_pty(int *fd_tty, const char **ttydev) |
74 | rxvt_get_pty (int *fd_tty, const char **ttydev) |
70 | { |
75 | { |
71 | int pfd; |
76 | int pfd; |
72 | |
77 | |
73 | #ifdef PTYS_ARE_OPENPTY |
78 | #ifdef PTYS_ARE_OPENPTY |
74 | |
79 | |
75 | char tty_name[sizeof "/dev/pts/????\0"]; |
80 | char tty_name[sizeof "/dev/pts/????\0"]; |
76 | |
81 | |
77 | if (openpty(&pfd, fd_tty, tty_name, NULL, NULL) != -1) |
82 | if (openpty (&pfd, fd_tty, tty_name, NULL, NULL) != -1) |
78 | { |
83 | { |
79 | *ttydev = strdup(tty_name); |
84 | *ttydev = strdup (tty_name); |
80 | return pfd; |
85 | return pfd; |
81 | } |
86 | } |
82 | #endif |
87 | #endif |
83 | |
88 | |
84 | #ifdef PTYS_ARE__GETPTY |
89 | #ifdef PTYS_ARE__GETPTY |
85 | *ttydev = _getpty(&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
90 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
86 | if (*ttydev != NULL) |
91 | if (*ttydev != NULL) |
87 | return pfd; |
92 | return pfd; |
88 | #endif |
93 | #endif |
89 | |
94 | |
90 | #ifdef PTYS_ARE_GETPTY |
95 | #ifdef PTYS_ARE_GETPTY |
91 | |
96 | |
92 | char *ptydev; |
97 | char *ptydev; |
93 | |
98 | |
94 | while ((ptydev = getpty()) != NULL) |
99 | while ((ptydev = getpty ()) != NULL) |
95 | if ((pfd = open(ptydev, O_RDWR | O_NOCTTY, 0)) >= 0) |
100 | if ((pfd = open (ptydev, O_RDWR | O_NOCTTY, 0)) >= 0) |
96 | { |
101 | { |
97 | *ttydev = ptydev; |
102 | *ttydev = ptydev; |
98 | return pfd; |
103 | return pfd; |
99 | } |
104 | } |
100 | #endif |
105 | #endif |
… | |
… | |
102 | #if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) |
107 | #if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) |
103 | # if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_PTMX) |
108 | # if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_PTMX) |
104 | |
109 | |
105 | { |
110 | { |
106 | # ifdef PTYS_ARE_GETPT |
111 | # ifdef PTYS_ARE_GETPT |
107 | pfd = getpt(); |
112 | pfd = getpt (); |
108 | # else |
113 | # else |
109 | |
114 | |
110 | pfd = open("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
115 | pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
111 | # endif |
116 | # endif |
112 | |
117 | |
113 | if (pfd >= 0) |
118 | if (pfd >= 0) |
114 | { |
119 | { |
115 | if (grantpt(pfd) == 0 /* change slave permissions */ |
120 | if (grantpt (pfd) == 0 /* change slave permissions */ |
116 | && unlockpt(pfd) == 0) |
121 | && unlockpt (pfd) == 0) |
117 | { /* slave now unlocked */ |
122 | { /* slave now unlocked */ |
118 | *ttydev = ptsname(pfd); /* get slave's name */ |
123 | *ttydev = ptsname (pfd); /* get slave's name */ |
119 | return pfd; |
124 | return pfd; |
120 | } |
125 | } |
121 | close(pfd); |
126 | close (pfd); |
122 | } |
127 | } |
123 | } |
128 | } |
124 | # endif |
129 | # endif |
125 | #endif |
130 | #endif |
126 | |
131 | |
127 | #ifdef PTYS_ARE_PTC |
132 | #ifdef PTYS_ARE_PTC |
128 | if ((pfd = open("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
133 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
129 | { |
134 | { |
130 | *ttydev = ttyname(pfd); |
135 | *ttydev = ttyname (pfd); |
131 | return pfd; |
136 | return pfd; |
132 | } |
137 | } |
133 | #endif |
138 | #endif |
134 | |
139 | |
135 | #ifdef PTYS_ARE_CLONE |
140 | #ifdef PTYS_ARE_CLONE |
136 | if ((pfd = open("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
141 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
137 | { |
142 | { |
138 | *ttydev = ptsname(pfd); |
143 | *ttydev = ptsname (pfd); |
139 | return pfd; |
144 | return pfd; |
140 | } |
145 | } |
141 | #endif |
146 | #endif |
142 | |
147 | |
143 | #ifdef PTYS_ARE_NUMERIC |
148 | #ifdef PTYS_ARE_NUMERIC |
… | |
… | |
145 | int idx; |
150 | int idx; |
146 | char *c1, *c2; |
151 | char *c1, *c2; |
147 | char pty_name[] = "/dev/ptyp???"; |
152 | char pty_name[] = "/dev/ptyp???"; |
148 | char tty_name[] = "/dev/ttyp???"; |
153 | char tty_name[] = "/dev/ttyp???"; |
149 | |
154 | |
150 | c1 = &(pty_name[sizeof(pty_name) - 4]); |
155 | c1 = & (pty_name[sizeof (pty_name) - 4]); |
151 | c2 = &(tty_name[sizeof(tty_name) - 4]); |
156 | c2 = & (tty_name[sizeof (tty_name) - 4]); |
152 | for (idx = 0; idx < 256; idx++) |
157 | for (idx = 0; idx < 256; idx++) |
153 | { |
158 | { |
154 | sprintf(c1, "%d", idx); |
159 | sprintf (c1, "%d", idx); |
155 | sprintf(c2, "%d", idx); |
160 | sprintf (c2, "%d", idx); |
156 | if (access(tty_name, F_OK) < 0) |
161 | if (access (tty_name, F_OK) < 0) |
157 | { |
162 | { |
158 | idx = 256; |
163 | idx = 256; |
159 | break; |
164 | break; |
160 | } |
165 | } |
161 | if ((pfd = open(pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
166 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
162 | { |
167 | { |
163 | if (access(tty_name, R_OK | W_OK) == 0) |
168 | if (access (tty_name, R_OK | W_OK) == 0) |
164 | { |
169 | { |
165 | *ttydev = strdup(tty_name); |
170 | *ttydev = strdup (tty_name); |
166 | return pfd; |
171 | return pfd; |
167 | } |
172 | } |
168 | close(pfd); |
173 | close (pfd); |
169 | } |
174 | } |
170 | } |
175 | } |
171 | } |
176 | } |
172 | #endif |
177 | #endif |
173 | #ifdef PTYS_ARE_SEARCHED |
178 | #ifdef PTYS_ARE_SEARCHED |
… | |
… | |
183 | # define PTYCHAR2 "0123456789abcdef" |
188 | # define PTYCHAR2 "0123456789abcdef" |
184 | # endif |
189 | # endif |
185 | |
190 | |
186 | for (c1 = PTYCHAR1; *c1; c1++) |
191 | for (c1 = PTYCHAR1; *c1; c1++) |
187 | { |
192 | { |
188 | pty_name[(sizeof(pty_name) - 3)] = |
193 | pty_name[ (sizeof (pty_name) - 3)] = |
189 | tty_name[(sizeof(pty_name) - 3)] = *c1; |
194 | tty_name[ (sizeof (pty_name) - 3)] = *c1; |
190 | for (c2 = PTYCHAR2; *c2; c2++) |
195 | for (c2 = PTYCHAR2; *c2; c2++) |
191 | { |
196 | { |
192 | pty_name[(sizeof(pty_name) - 2)] = |
197 | pty_name[ (sizeof (pty_name) - 2)] = |
193 | tty_name[(sizeof(pty_name) - 2)] = *c2; |
198 | tty_name[ (sizeof (pty_name) - 2)] = *c2; |
194 | if ((pfd = open(pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
199 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
195 | { |
200 | { |
196 | if (access(tty_name, R_OK | W_OK) == 0) |
201 | if (access (tty_name, R_OK | W_OK) == 0) |
197 | { |
202 | { |
198 | *ttydev = strdup(tty_name); |
203 | *ttydev = strdup (tty_name); |
199 | return pfd; |
204 | return pfd; |
200 | } |
205 | } |
201 | close(pfd); |
206 | close (pfd); |
202 | } |
207 | } |
203 | } |
208 | } |
204 | } |
209 | } |
205 | } |
210 | } |
206 | #endif |
211 | #endif |
… | |
… | |
227 | rxvt_control_tty (int fd_tty, const char *ttydev) |
232 | rxvt_control_tty (int fd_tty, const char *ttydev) |
228 | { |
233 | { |
229 | #ifndef __QNX__ |
234 | #ifndef __QNX__ |
230 | int fd; |
235 | int fd; |
231 | |
236 | |
232 | D_TTY ((stderr, "rxvt_control_tty(): pid: %d, tty fd: %d, dev: %s", getpid(), fd_tty, ttydev)); |
237 | D_TTY ((stderr, "rxvt_control_tty (): pid: %d, tty fd: %d, dev: %s", getpid (), fd_tty, ttydev)); |
233 | /* ---------------------------------------- */ |
238 | /* ---------------------------------------- */ |
234 | # ifdef HAVE_SETSID |
239 | # ifdef HAVE_SETSID |
235 | setsid (); |
240 | setsid (); |
236 | # endif |
241 | # endif |
237 | # if defined(HAVE_SETPGID) |
242 | # if defined(HAVE_SETPGID) |
… | |
… | |
241 | # endif |
246 | # endif |
242 | /* ---------------------------------------- */ |
247 | /* ---------------------------------------- */ |
243 | # ifdef TIOCNOTTY |
248 | # ifdef TIOCNOTTY |
244 | |
249 | |
245 | fd = open ("/dev/tty", O_RDWR | O_NOCTTY); |
250 | fd = open ("/dev/tty", O_RDWR | O_NOCTTY); |
246 | D_TTY ((stderr, "rxvt_control_tty(): Voiding tty associations: previous=%s", fd < 0 ? "no" : "yes")); |
251 | D_TTY ((stderr, "rxvt_control_tty (): Voiding tty associations: previous=%s", fd < 0 ? "no" : "yes")); |
247 | if (fd >= 0) |
252 | if (fd >= 0) |
248 | { |
253 | { |
249 | ioctl (fd, TIOCNOTTY, NULL); /* void tty associations */ |
254 | ioctl (fd, TIOCNOTTY, NULL); /* void tty associations */ |
250 | close (fd); |
255 | close (fd); |
251 | } |
256 | } |
252 | # endif |
257 | # endif |
253 | /* ---------------------------------------- */ |
258 | /* ---------------------------------------- */ |
254 | fd = open ("/dev/tty", O_RDWR | O_NOCTTY); |
259 | fd = open ("/dev/tty", O_RDWR | O_NOCTTY); |
255 | D_TTY ((stderr, "rxvt_control_tty(): /dev/tty has controlling tty? %s", fd < 0 ? "no (good)" : "yes (bad)")); |
260 | D_TTY ((stderr, "rxvt_control_tty (): /dev/tty has controlling tty? %s", fd < 0 ? "no (good)" : "yes (bad)")); |
256 | if (fd >= 0) |
261 | if (fd >= 0) |
257 | close (fd); /* ouch: still have controlling tty */ |
262 | close (fd); /* ouch: still have controlling tty */ |
258 | /* ---------------------------------------- */ |
263 | /* ---------------------------------------- */ |
259 | #if defined(PTYS_ARE_PTMX) && defined(I_PUSH) |
264 | #if defined(PTYS_ARE_PTMX) && defined(I_PUSH) |
260 | /* |
265 | /* |
261 | * Push STREAMS modules: |
266 | * Push STREAMS modules: |
262 | * ptem: pseudo-terminal hardware emulation module. |
267 | * ptem: pseudo-terminal hardware emulation module. |
263 | * ldterm: standard terminal line discipline. |
268 | * ldterm: standard terminal line discipline. |
264 | * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. |
269 | * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. |
265 | * |
270 | * |
266 | * After we push the STREAMS modules, the first open() on the slave side |
271 | * After we push the STREAMS modules, the first open () on the slave side |
267 | * (i.e. the next section between the dashes giving us "tty opened OK") |
272 | * (i.e. the next section between the dashes giving us "tty opened OK") |
268 | * should make the "ptem" (or "ldterm" depending upon either which OS |
273 | * should make the "ptem" (or "ldterm" depending upon either which OS |
269 | * version or which set of manual pages you have) module give us a |
274 | * version or which set of manual pages you have) module give us a |
270 | * controlling terminal. We must already have close()d the master side |
275 | * controlling terminal. We must already have close ()d the master side |
271 | * fd in this child process before we push STREAMS modules on because the |
276 | * fd in this child process before we push STREAMS modules on because the |
272 | * documentation is really unclear about whether it is any close() on |
277 | * documentation is really unclear about whether it is any close () on |
273 | * the master side or the last close() - i.e. a proper STREAMS dismantling |
278 | * the master side or the last close () - i.e. a proper STREAMS dismantling |
274 | * close() - on the master side which causes a hang up to be sent |
279 | * close () - on the master side which causes a hang up to be sent |
275 | * through - Geoff Wing |
280 | * through - Geoff Wing |
276 | */ |
281 | */ |
277 | # ifdef HAVE_ISASTREAM |
282 | # ifdef HAVE_ISASTREAM |
278 | if (isastream (fd_tty) == 1) |
283 | if (isastream (fd_tty) == 1) |
279 | # endif |
284 | # endif |
280 | { |
285 | { |
281 | D_TTY ((stderr, "rxvt_control_tty(): Pushing STREAMS modules")); |
286 | D_TTY ((stderr, "rxvt_control_tty (): Pushing STREAMS modules")); |
282 | ioctl (fd_tty, I_PUSH, "ptem"); |
287 | ioctl (fd_tty, I_PUSH, "ptem"); |
283 | ioctl (fd_tty, I_PUSH, "ldterm"); |
288 | ioctl (fd_tty, I_PUSH, "ldterm"); |
284 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
289 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
285 | } |
290 | } |
286 | #endif |
291 | #endif |
287 | /* ---------------------------------------- */ |
292 | /* ---------------------------------------- */ |
288 | # if defined(TIOCSCTTY) |
293 | # if defined(TIOCSCTTY) |
289 | fd = ioctl (fd_tty, TIOCSCTTY, NULL); |
294 | fd = ioctl (fd_tty, TIOCSCTTY, NULL); |
290 | D_TTY ((stderr, "rxvt_control_tty(): ioctl(..,TIOCSCTTY): %d", fd)); |
295 | D_TTY ((stderr, "rxvt_control_tty (): ioctl (..,TIOCSCTTY): %d", fd)); |
291 | # elif defined(TIOCSETCTTY) |
296 | # elif defined(TIOCSETCTTY) |
292 | fd = ioctl (fd_tty, TIOCSETCTTY, NULL); |
297 | fd = ioctl (fd_tty, TIOCSETCTTY, NULL); |
293 | D_TTY ((stderr, "rxvt_control_tty(): ioctl(..,TIOCSETCTTY): %d", fd)); |
298 | D_TTY ((stderr, "rxvt_control_tty (): ioctl (..,TIOCSETCTTY): %d", fd)); |
294 | # else |
299 | # else |
295 | fd = open (ttydev, O_RDWR); |
300 | fd = open (ttydev, O_RDWR); |
296 | D_TTY ((stderr, "rxvt_control_tty(): tty open%s", fd < 0 ? " failure" : "ed OK")); |
301 | D_TTY ((stderr, "rxvt_control_tty (): tty open%s", fd < 0 ? " failure" : "ed OK")); |
297 | if (fd >= 0) |
302 | if (fd >= 0) |
298 | close(fd); |
303 | close (fd); |
299 | # endif |
304 | # endif |
300 | /* ---------------------------------------- */ |
305 | /* ---------------------------------------- */ |
301 | fd = open ("/dev/tty", O_WRONLY); |
306 | fd = open ("/dev/tty", O_WRONLY); |
302 | D_TTY ((stderr, "rxvt_control_tty(): do we have controlling tty now: %s", fd < 0 ? "no (fatal)" : "yes (good)")); |
307 | D_TTY ((stderr, "rxvt_control_tty (): do we have controlling tty now: %s", fd < 0 ? "no (fatal)" : "yes (good)")); |
303 | if (fd < 0) |
308 | if (fd < 0) |
304 | return -1; /* fatal */ |
309 | return -1; /* fatal */ |
305 | close (fd); |
310 | close (fd); |
306 | /* ---------------------------------------- */ |
311 | /* ---------------------------------------- */ |
307 | D_TTY ((stderr, "rxvt_control_tty(): tcgetpgrp(): %d getpgrp(): %d", tcgetpgrp(fd_tty), getpgrp())); |
312 | D_TTY ((stderr, "rxvt_control_tty (): tcgetpgrp (): %d getpgrp (): %d", tcgetpgrp (fd_tty), getpgrp ())); |
308 | /* ---------------------------------------- */ |
313 | /* ---------------------------------------- */ |
309 | #endif /* ! __QNX__ */ |
314 | #endif /* ! __QNX__ */ |
310 | |
315 | |
311 | return 0; |
316 | return 0; |
312 | } |
317 | } |