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.2 by pcg, Mon Nov 24 17:31:27 2003 UTC vs.
Revision 1.7 by pcg, Mon Mar 1 01:06:46 2004 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: ptytty.c 2 * File: ptytty.c
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * $Id: ptytty.C,v 1.2 2003/11/24 17:31:27 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
47# include <sys/ioctl.h> 52# include <sys/ioctl.h>
48#endif 53#endif
49#if defined(PTYS_ARE_PTMX) && !defined(__CYGWIN32__) 54#if defined(PTYS_ARE_PTMX) && !defined(__CYGWIN32__)
50# include <sys/stropts.h> /* for I_PUSH */ 55# include <sys/stropts.h> /* for I_PUSH */
51#endif 56#endif
52
53 57
54#ifdef DEBUG_TTY 58#ifdef DEBUG_TTY
55# define D_TTY(x) fprintf x ; fputc('\n', stderr) ; fflush(stderr) 59# define D_TTY(x) fprintf x ; fputc('\n', stderr) ; fflush(stderr)
56#else 60#else
57# define D_TTY(x) 61# define D_TTY(x)
65 * 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.
66 * 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
67 */ 71 */
68/* EXTPROTO */ 72/* EXTPROTO */
69int 73int
70rxvt_get_pty(int *fd_tty, const char **ttydev) 74rxvt_get_pty (int *fd_tty, const char **ttydev)
71{ 75{
72 int pfd; 76 int pfd;
73 77
74#ifdef PTYS_ARE_OPENPTY 78#ifdef PTYS_ARE_OPENPTY
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)
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_NDELAY | 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
96
90 char *ptydev; 97 char *ptydev;
91 98
92 while ((ptydev = getpty()) != NULL) 99 while ((ptydev = getpty ()) != NULL)
93 if ((pfd = open(ptydev, O_RDWR | O_NOCTTY, 0)) >= 0) { 100 if ((pfd = open (ptydev, O_RDWR | O_NOCTTY, 0)) >= 0)
101 {
94 *ttydev = ptydev; 102 *ttydev = ptydev;
95 return pfd; 103 return pfd;
96 } 104 }
97#endif 105#endif
98 106
99#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) 107#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT)
100# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_PTMX) 108# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_PTMX)
109
101 { 110 {
102# ifdef PTYS_ARE_GETPT 111# ifdef PTYS_ARE_GETPT
103 pfd = getpt(); 112 pfd = getpt ();
104# else 113# else
114
105 pfd = open("/dev/ptmx", O_RDWR | O_NOCTTY, 0); 115 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0);
106# endif 116# endif
117
107 if (pfd >= 0) { 118 if (pfd >= 0)
119 {
108 if (grantpt(pfd) == 0 /* change slave permissions */ 120 if (grantpt (pfd) == 0 /* change slave permissions */
109 && unlockpt(pfd) == 0) { /* slave now unlocked */ 121 && unlockpt (pfd) == 0)
122 { /* slave now unlocked */
110 *ttydev = ptsname(pfd); /* get slave's name */ 123 *ttydev = ptsname (pfd); /* get slave's name */
111 return pfd; 124 return pfd;
112 } 125 }
113 close(pfd); 126 close (pfd);
114 }
115 } 127 }
128 }
116# endif 129# endif
117#endif 130#endif
118 131
119#ifdef PTYS_ARE_PTC 132#ifdef PTYS_ARE_PTC
120 if ((pfd = open("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) { 133 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
134 {
121 *ttydev = ttyname(pfd); 135 *ttydev = ttyname (pfd);
122 return pfd; 136 return pfd;
123 } 137 }
124#endif 138#endif
125 139
126#ifdef PTYS_ARE_CLONE 140#ifdef PTYS_ARE_CLONE
127 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)
142 {
128 *ttydev = ptsname(pfd); 143 *ttydev = ptsname (pfd);
129 return pfd; 144 return pfd;
130 } 145 }
131#endif 146#endif
132 147
133#ifdef PTYS_ARE_NUMERIC 148#ifdef PTYS_ARE_NUMERIC
134 { 149 {
135 int idx; 150 int idx;
136 char *c1, *c2; 151 char *c1, *c2;
137 char pty_name[] = "/dev/ptyp???"; 152 char pty_name[] = "/dev/ptyp???";
138 char tty_name[] = "/dev/ttyp???"; 153 char tty_name[] = "/dev/ttyp???";
139 154
140 c1 = &(pty_name[sizeof(pty_name) - 4]); 155 c1 = & (pty_name[sizeof (pty_name) - 4]);
141 c2 = &(tty_name[sizeof(tty_name) - 4]); 156 c2 = & (tty_name[sizeof (tty_name) - 4]);
142 for (idx = 0; idx < 256; idx++) { 157 for (idx = 0; idx < 256; idx++)
158 {
143 sprintf(c1, "%d", idx); 159 sprintf (c1, "%d", idx);
144 sprintf(c2, "%d", idx); 160 sprintf (c2, "%d", idx);
145 if (access(tty_name, F_OK) < 0) { 161 if (access (tty_name, F_OK) < 0)
146 idx = 256; 162 {
147 break; 163 idx = 256;
148 } 164 break;
165 }
149 if ((pfd = open(pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) { 166 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
167 {
150 if (access(tty_name, R_OK | W_OK) == 0) { 168 if (access (tty_name, R_OK | W_OK) == 0)
169 {
151 *ttydev = strdup(tty_name); 170 *ttydev = strdup (tty_name);
152 return pfd; 171 return pfd;
153 } 172 }
154 close(pfd); 173 close (pfd);
155 } 174 }
156 }
157 } 175 }
176 }
158#endif 177#endif
159#ifdef PTYS_ARE_SEARCHED 178#ifdef PTYS_ARE_SEARCHED
160 { 179 {
161 const char *c1, *c2; 180 const char *c1, *c2;
162 char pty_name[] = "/dev/pty??"; 181 char pty_name[] = "/dev/pty??";
163 char tty_name[] = "/dev/tty??"; 182 char tty_name[] = "/dev/tty??";
164 183
165# ifndef PTYCHAR1 184# ifndef PTYCHAR1
166# define PTYCHAR1 "pqrstuvwxyz" 185# define PTYCHAR1 "pqrstuvwxyz"
167# endif 186# endif
168# ifndef PTYCHAR2 187# ifndef PTYCHAR2
169# define PTYCHAR2 "0123456789abcdef" 188# define PTYCHAR2 "0123456789abcdef"
170# endif 189# endif
190
171 for (c1 = PTYCHAR1; *c1; c1++) { 191 for (c1 = PTYCHAR1; *c1; c1++)
192 {
172 pty_name[(sizeof(pty_name) - 3)] = 193 pty_name[ (sizeof (pty_name) - 3)] =
173 tty_name[(sizeof(pty_name) - 3)] = *c1; 194 tty_name[ (sizeof (pty_name) - 3)] = *c1;
174 for (c2 = PTYCHAR2; *c2; c2++) { 195 for (c2 = PTYCHAR2; *c2; c2++)
196 {
175 pty_name[(sizeof(pty_name) - 2)] = 197 pty_name[ (sizeof (pty_name) - 2)] =
176 tty_name[(sizeof(pty_name) - 2)] = *c2; 198 tty_name[ (sizeof (pty_name) - 2)] = *c2;
177 if ((pfd = open(pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) { 199 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
200 {
178 if (access(tty_name, R_OK | W_OK) == 0) { 201 if (access (tty_name, R_OK | W_OK) == 0)
179 *ttydev = strdup(tty_name); 202 {
180 return pfd; 203 *ttydev = strdup (tty_name);
181 } 204 return pfd;
182 close(pfd); 205 }
183 } 206 close (pfd);
184 } 207 }
185 } 208 }
186 } 209 }
210 }
187#endif 211#endif
188 return -1; 212 return -1;
189} 213}
190 214
191/*----------------------------------------------------------------------*/ 215/*----------------------------------------------------------------------*/
192/* 216/*
193 * Returns tty file descriptor, or -1 on failure 217 * Returns tty file descriptor, or -1 on failure
194 */ 218 */
195/* EXTPROTO */ 219/* EXTPROTO */
196int 220int
197rxvt_get_tty(const char *ttydev) 221rxvt_get_tty (const char *ttydev)
198{ 222{
199 return open(ttydev, O_RDWR | O_NOCTTY, 0); 223 return open (ttydev, O_RDWR | O_NOCTTY, 0);
200} 224}
201 225
202/*----------------------------------------------------------------------*/ 226/*----------------------------------------------------------------------*/
203/* 227/*
204 * Make our tty a controlling tty so that /dev/tty points to us 228 * Make our tty a controlling tty so that /dev/tty points to us
205 */ 229 */
206/* EXTPROTO */ 230/* EXTPROTO */
207int 231int
208rxvt_control_tty(int fd_tty, const char *ttydev) 232rxvt_control_tty (int fd_tty, const char *ttydev)
209{ 233{
210#ifndef __QNX__ 234#ifndef __QNX__
211 int fd; 235 int fd;
212 236
213 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));
214/* ---------------------------------------- */ 238 /* ---------------------------------------- */
215# ifdef HAVE_SETSID 239# ifdef HAVE_SETSID
216 setsid(); 240 setsid ();
217# endif 241# endif
218# if defined(HAVE_SETPGID) 242# if defined(HAVE_SETPGID)
219 setpgid(0, 0); 243 setpgid (0, 0);
220# elif defined(HAVE_SETPGRP) 244# elif defined(HAVE_SETPGRP)
221 setpgrp(0, 0); 245 setpgrp (0, 0);
222# endif 246# endif
223/* ---------------------------------------- */ 247 /* ---------------------------------------- */
224# ifdef TIOCNOTTY 248# ifdef TIOCNOTTY
249
225 fd = open("/dev/tty", O_RDWR | O_NOCTTY); 250 fd = open ("/dev/tty", O_RDWR | O_NOCTTY);
226 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"));
227 if (fd >= 0) { 252 if (fd >= 0)
253 {
228 ioctl(fd, TIOCNOTTY, NULL); /* void tty associations */ 254 ioctl (fd, TIOCNOTTY, NULL); /* void tty associations */
229 close(fd); 255 close (fd);
230 } 256 }
231# endif 257# endif
232/* ---------------------------------------- */ 258 /* ---------------------------------------- */
233 fd = open("/dev/tty", O_RDWR | O_NOCTTY); 259 fd = open ("/dev/tty", O_RDWR | O_NOCTTY);
234 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)"));
235 if (fd >= 0) 261 if (fd >= 0)
236 close(fd); /* ouch: still have controlling tty */ 262 close (fd); /* ouch: still have controlling tty */
237/* ---------------------------------------- */ 263 /* ---------------------------------------- */
238#if defined(PTYS_ARE_PTMX) && defined(I_PUSH) 264#if defined(PTYS_ARE_PTMX) && defined(I_PUSH)
239/* 265 /*
240 * Push STREAMS modules: 266 * Push STREAMS modules:
241 * ptem: pseudo-terminal hardware emulation module. 267 * ptem: pseudo-terminal hardware emulation module.
242 * ldterm: standard terminal line discipline. 268 * ldterm: standard terminal line discipline.
243 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. 269 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
244 * 270 *
245 * 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
246 * (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")
247 * should make the "ptem" (or "ldterm" depending upon either which OS 273 * should make the "ptem" (or "ldterm" depending upon either which OS
248 * 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
249 * controlling terminal. We must already have close()d the master side 275 * controlling terminal. We must already have close ()d the master side
250 * 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
251 * documentation is really unclear about whether it is any close() on 277 * documentation is really unclear about whether it is any close () on
252 * 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
253 * 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
254 * through - Geoff Wing 280 * through - Geoff Wing
255 */ 281 */
256# ifdef HAVE_ISASTREAM 282# ifdef HAVE_ISASTREAM
257 if (isastream(fd_tty) == 1) 283 if (isastream (fd_tty) == 1)
258# endif 284# endif
259 { 285 {
260 D_TTY((stderr, "rxvt_control_tty(): Pushing STREAMS modules")); 286 D_TTY ((stderr, "rxvt_control_tty (): Pushing STREAMS modules"));
261 ioctl(fd_tty, I_PUSH, "ptem"); 287 ioctl (fd_tty, I_PUSH, "ptem");
262 ioctl(fd_tty, I_PUSH, "ldterm"); 288 ioctl (fd_tty, I_PUSH, "ldterm");
263 ioctl(fd_tty, I_PUSH, "ttcompat"); 289 ioctl (fd_tty, I_PUSH, "ttcompat");
264 } 290 }
265#endif 291#endif
266/* ---------------------------------------- */ 292 /* ---------------------------------------- */
267# if defined(TIOCSCTTY) 293# if defined(TIOCSCTTY)
268 fd = ioctl(fd_tty, TIOCSCTTY, NULL); 294 fd = ioctl (fd_tty, TIOCSCTTY, NULL);
269 D_TTY((stderr, "rxvt_control_tty(): ioctl(..,TIOCSCTTY): %d", fd)); 295 D_TTY ((stderr, "rxvt_control_tty (): ioctl (..,TIOCSCTTY): %d", fd));
270# elif defined(TIOCSETCTTY) 296# elif defined(TIOCSETCTTY)
271 fd = ioctl(fd_tty, TIOCSETCTTY, NULL); 297 fd = ioctl (fd_tty, TIOCSETCTTY, NULL);
272 D_TTY((stderr, "rxvt_control_tty(): ioctl(..,TIOCSETCTTY): %d", fd)); 298 D_TTY ((stderr, "rxvt_control_tty (): ioctl (..,TIOCSETCTTY): %d", fd));
273# else 299# else
274 fd = open(ttydev, O_RDWR); 300 fd = open (ttydev, O_RDWR);
275 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"));
276 if (fd >= 0) 302 if (fd >= 0)
277 close(fd); 303 close (fd);
278# endif 304# endif
279/* ---------------------------------------- */ 305 /* ---------------------------------------- */
280 fd = open("/dev/tty", O_WRONLY); 306 fd = open ("/dev/tty", O_WRONLY);
281 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)"));
282 if (fd < 0) 308 if (fd < 0)
283 return -1; /* fatal */ 309 return -1; /* fatal */
284 close(fd); 310 close (fd);
285/* ---------------------------------------- */ 311 /* ---------------------------------------- */
286 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 ()));
287/* ---------------------------------------- */ 313 /* ---------------------------------------- */
288#endif /* ! __QNX__ */ 314#endif /* ! __QNX__ */
315
289 return 0; 316 return 0;
290} 317}
291/*----------------------- end-of-file (C source) -----------------------*/ 318/*----------------------- end-of-file (C source) -----------------------*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines