ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/ptytty.C
(Generate patch)

Comparing rxvt-unicode/src/ptytty.C (file contents):
Revision 1.5 by pcg, Sat Jan 31 03:27:45 2004 UTC vs.
Revision 1.10 by pcg, Fri Apr 2 20:41:01 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines