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.7 by pcg, Mon Mar 1 01:06:46 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 <stdio.h>
30#ifdef HAVE_STDLIB_H 36#ifdef HAVE_STDLIB_H
31# include <stdlib.h> 37# include <stdlib.h>
32#endif 38#endif
33#ifdef HAVE_SYS_TYPES_H 39#ifdef HAVE_SYS_TYPES_H
63 * 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.
64 * 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
65 */ 71 */
66/* EXTPROTO */ 72/* EXTPROTO */
67int 73int
68rxvt_get_pty(int *fd_tty, const char **ttydev) 74rxvt_get_pty (int *fd_tty, const char **ttydev)
69{ 75{
70 int pfd; 76 int pfd;
71 77
72#ifdef PTYS_ARE_OPENPTY 78#ifdef PTYS_ARE_OPENPTY
73 79
74 char tty_name[sizeof "/dev/pts/????\0"]; 80 char tty_name[sizeof "/dev/pts/????\0"];
75 81
76 if (openpty(&pfd, fd_tty, tty_name, NULL, NULL) != -1) 82 if (openpty (&pfd, fd_tty, tty_name, NULL, NULL) != -1)
77 { 83 {
78 *ttydev = strdup(tty_name); 84 *ttydev = strdup (tty_name);
79 return pfd; 85 return pfd;
80 } 86 }
81#endif 87#endif
82 88
83#ifdef PTYS_ARE__GETPTY 89#ifdef PTYS_ARE__GETPTY
84 *ttydev = _getpty(&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 90 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
85 if (*ttydev != NULL) 91 if (*ttydev != NULL)
86 return pfd; 92 return pfd;
87#endif 93#endif
88 94
89#ifdef PTYS_ARE_GETPTY 95#ifdef PTYS_ARE_GETPTY
90 96
91 char *ptydev; 97 char *ptydev;
92 98
93 while ((ptydev = getpty()) != NULL) 99 while ((ptydev = getpty ()) != NULL)
94 if ((pfd = open(ptydev, O_RDWR | O_NOCTTY, 0)) >= 0) 100 if ((pfd = open (ptydev, O_RDWR | O_NOCTTY, 0)) >= 0)
95 { 101 {
96 *ttydev = ptydev; 102 *ttydev = ptydev;
97 return pfd; 103 return pfd;
98 } 104 }
99#endif 105#endif
101#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) 107#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT)
102# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_PTMX) 108# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_PTMX)
103 109
104 { 110 {
105# ifdef PTYS_ARE_GETPT 111# ifdef PTYS_ARE_GETPT
106 pfd = getpt(); 112 pfd = getpt ();
107# else 113# else
108 114
109 pfd = open("/dev/ptmx", O_RDWR | O_NOCTTY, 0); 115 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0);
110# endif 116# endif
111 117
112 if (pfd >= 0) 118 if (pfd >= 0)
113 { 119 {
114 if (grantpt(pfd) == 0 /* change slave permissions */ 120 if (grantpt (pfd) == 0 /* change slave permissions */
115 && unlockpt(pfd) == 0) 121 && unlockpt (pfd) == 0)
116 { /* slave now unlocked */ 122 { /* slave now unlocked */
117 *ttydev = ptsname(pfd); /* get slave's name */ 123 *ttydev = ptsname (pfd); /* get slave's name */
118 return pfd; 124 return pfd;
119 } 125 }
120 close(pfd); 126 close (pfd);
121 } 127 }
122 } 128 }
123# endif 129# endif
124#endif 130#endif
125 131
126#ifdef PTYS_ARE_PTC 132#ifdef PTYS_ARE_PTC
127 if ((pfd = open("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) 133 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
128 { 134 {
129 *ttydev = ttyname(pfd); 135 *ttydev = ttyname (pfd);
130 return pfd; 136 return pfd;
131 } 137 }
132#endif 138#endif
133 139
134#ifdef PTYS_ARE_CLONE 140#ifdef PTYS_ARE_CLONE
135 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)
136 { 142 {
137 *ttydev = ptsname(pfd); 143 *ttydev = ptsname (pfd);
138 return pfd; 144 return pfd;
139 } 145 }
140#endif 146#endif
141 147
142#ifdef PTYS_ARE_NUMERIC 148#ifdef PTYS_ARE_NUMERIC
144 int idx; 150 int idx;
145 char *c1, *c2; 151 char *c1, *c2;
146 char pty_name[] = "/dev/ptyp???"; 152 char pty_name[] = "/dev/ptyp???";
147 char tty_name[] = "/dev/ttyp???"; 153 char tty_name[] = "/dev/ttyp???";
148 154
149 c1 = &(pty_name[sizeof(pty_name) - 4]); 155 c1 = & (pty_name[sizeof (pty_name) - 4]);
150 c2 = &(tty_name[sizeof(tty_name) - 4]); 156 c2 = & (tty_name[sizeof (tty_name) - 4]);
151 for (idx = 0; idx < 256; idx++) 157 for (idx = 0; idx < 256; idx++)
152 { 158 {
153 sprintf(c1, "%d", idx); 159 sprintf (c1, "%d", idx);
154 sprintf(c2, "%d", idx); 160 sprintf (c2, "%d", idx);
155 if (access(tty_name, F_OK) < 0) 161 if (access (tty_name, F_OK) < 0)
156 { 162 {
157 idx = 256; 163 idx = 256;
158 break; 164 break;
159 } 165 }
160 if ((pfd = open(pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) 166 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
161 { 167 {
162 if (access(tty_name, R_OK | W_OK) == 0) 168 if (access (tty_name, R_OK | W_OK) == 0)
163 { 169 {
164 *ttydev = strdup(tty_name); 170 *ttydev = strdup (tty_name);
165 return pfd; 171 return pfd;
166 } 172 }
167 close(pfd); 173 close (pfd);
168 } 174 }
169 } 175 }
170 } 176 }
171#endif 177#endif
172#ifdef PTYS_ARE_SEARCHED 178#ifdef PTYS_ARE_SEARCHED
182# define PTYCHAR2 "0123456789abcdef" 188# define PTYCHAR2 "0123456789abcdef"
183# endif 189# endif
184 190
185 for (c1 = PTYCHAR1; *c1; c1++) 191 for (c1 = PTYCHAR1; *c1; c1++)
186 { 192 {
187 pty_name[(sizeof(pty_name) - 3)] = 193 pty_name[ (sizeof (pty_name) - 3)] =
188 tty_name[(sizeof(pty_name) - 3)] = *c1; 194 tty_name[ (sizeof (pty_name) - 3)] = *c1;
189 for (c2 = PTYCHAR2; *c2; c2++) 195 for (c2 = PTYCHAR2; *c2; c2++)
190 { 196 {
191 pty_name[(sizeof(pty_name) - 2)] = 197 pty_name[ (sizeof (pty_name) - 2)] =
192 tty_name[(sizeof(pty_name) - 2)] = *c2; 198 tty_name[ (sizeof (pty_name) - 2)] = *c2;
193 if ((pfd = open(pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) 199 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
194 { 200 {
195 if (access(tty_name, R_OK | W_OK) == 0) 201 if (access (tty_name, R_OK | W_OK) == 0)
196 { 202 {
197 *ttydev = strdup(tty_name); 203 *ttydev = strdup (tty_name);
198 return pfd; 204 return pfd;
199 } 205 }
200 close(pfd); 206 close (pfd);
201 } 207 }
202 } 208 }
203 } 209 }
204 } 210 }
205#endif 211#endif
226rxvt_control_tty (int fd_tty, const char *ttydev) 232rxvt_control_tty (int fd_tty, const char *ttydev)
227{ 233{
228#ifndef __QNX__ 234#ifndef __QNX__
229 int fd; 235 int fd;
230 236
231 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));
232 /* ---------------------------------------- */ 238 /* ---------------------------------------- */
233# ifdef HAVE_SETSID 239# ifdef HAVE_SETSID
234 setsid (); 240 setsid ();
235# endif 241# endif
236# if defined(HAVE_SETPGID) 242# if defined(HAVE_SETPGID)
240# endif 246# endif
241 /* ---------------------------------------- */ 247 /* ---------------------------------------- */
242# ifdef TIOCNOTTY 248# ifdef TIOCNOTTY
243 249
244 fd = open ("/dev/tty", O_RDWR | O_NOCTTY); 250 fd = open ("/dev/tty", O_RDWR | O_NOCTTY);
245 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"));
246 if (fd >= 0) 252 if (fd >= 0)
247 { 253 {
248 ioctl (fd, TIOCNOTTY, NULL); /* void tty associations */ 254 ioctl (fd, TIOCNOTTY, NULL); /* void tty associations */
249 close (fd); 255 close (fd);
250 } 256 }
251# endif 257# endif
252 /* ---------------------------------------- */ 258 /* ---------------------------------------- */
253 fd = open ("/dev/tty", O_RDWR | O_NOCTTY); 259 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)")); 260 D_TTY ((stderr, "rxvt_control_tty (): /dev/tty has controlling tty? %s", fd < 0 ? "no (good)" : "yes (bad)"));
255 if (fd >= 0) 261 if (fd >= 0)
256 close (fd); /* ouch: still have controlling tty */ 262 close (fd); /* ouch: still have controlling tty */
257 /* ---------------------------------------- */ 263 /* ---------------------------------------- */
258#if defined(PTYS_ARE_PTMX) && defined(I_PUSH) 264#if defined(PTYS_ARE_PTMX) && defined(I_PUSH)
259 /* 265 /*
260 * Push STREAMS modules: 266 * Push STREAMS modules:
261 * ptem: pseudo-terminal hardware emulation module. 267 * ptem: pseudo-terminal hardware emulation module.
262 * ldterm: standard terminal line discipline. 268 * ldterm: standard terminal line discipline.
263 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. 269 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
264 * 270 *
265 * 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
266 * (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")
267 * should make the "ptem" (or "ldterm" depending upon either which OS 273 * 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 274 * 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 275 * 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 276 * 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 277 * 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 278 * 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 279 * close () - on the master side which causes a hang up to be sent
274 * through - Geoff Wing 280 * through - Geoff Wing
275 */ 281 */
276# ifdef HAVE_ISASTREAM 282# ifdef HAVE_ISASTREAM
277 if (isastream (fd_tty) == 1) 283 if (isastream (fd_tty) == 1)
278# endif 284# endif
279 { 285 {
280 D_TTY ((stderr, "rxvt_control_tty(): Pushing STREAMS modules")); 286 D_TTY ((stderr, "rxvt_control_tty (): Pushing STREAMS modules"));
281 ioctl (fd_tty, I_PUSH, "ptem"); 287 ioctl (fd_tty, I_PUSH, "ptem");
282 ioctl (fd_tty, I_PUSH, "ldterm"); 288 ioctl (fd_tty, I_PUSH, "ldterm");
283 ioctl (fd_tty, I_PUSH, "ttcompat"); 289 ioctl (fd_tty, I_PUSH, "ttcompat");
284 } 290 }
285#endif 291#endif
286 /* ---------------------------------------- */ 292 /* ---------------------------------------- */
287# if defined(TIOCSCTTY) 293# if defined(TIOCSCTTY)
288 fd = ioctl (fd_tty, TIOCSCTTY, NULL); 294 fd = ioctl (fd_tty, TIOCSCTTY, NULL);
289 D_TTY ((stderr, "rxvt_control_tty(): ioctl(..,TIOCSCTTY): %d", fd)); 295 D_TTY ((stderr, "rxvt_control_tty (): ioctl (..,TIOCSCTTY): %d", fd));
290# elif defined(TIOCSETCTTY) 296# elif defined(TIOCSETCTTY)
291 fd = ioctl (fd_tty, TIOCSETCTTY, NULL); 297 fd = ioctl (fd_tty, TIOCSETCTTY, NULL);
292 D_TTY ((stderr, "rxvt_control_tty(): ioctl(..,TIOCSETCTTY): %d", fd)); 298 D_TTY ((stderr, "rxvt_control_tty (): ioctl (..,TIOCSETCTTY): %d", fd));
293# else 299# else
294 fd = open (ttydev, O_RDWR); 300 fd = open (ttydev, O_RDWR);
295 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"));
296 if (fd >= 0) 302 if (fd >= 0)
297 close(fd); 303 close (fd);
298# endif 304# endif
299 /* ---------------------------------------- */ 305 /* ---------------------------------------- */
300 fd = open ("/dev/tty", O_WRONLY); 306 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)")); 307 D_TTY ((stderr, "rxvt_control_tty (): do we have controlling tty now: %s", fd < 0 ? "no (fatal)" : "yes (good)"));
302 if (fd < 0) 308 if (fd < 0)
303 return -1; /* fatal */ 309 return -1; /* fatal */
304 close (fd); 310 close (fd);
305 /* ---------------------------------------- */ 311 /* ---------------------------------------- */
306 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 ()));
307 /* ---------------------------------------- */ 313 /* ---------------------------------------- */
308#endif /* ! __QNX__ */ 314#endif /* ! __QNX__ */
309 315
310 return 0; 316 return 0;
311} 317}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines