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.46 by root, Thu Jan 19 09:57:20 2006 UTC vs.
Revision 1.53 by ayin, Sat Jan 21 18:15:26 2006 UTC

33#include <fcntl.h> 33#include <fcntl.h>
34 34
35#ifdef HAVE_SYS_IOCTL_H 35#ifdef HAVE_SYS_IOCTL_H
36# include <sys/ioctl.h> 36# include <sys/ioctl.h>
37#endif 37#endif
38#if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) 38#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
39# include <sys/stropts.h> /* for I_PUSH */ 39# include <sys/stropts.h> /* for I_PUSH */
40#endif 40#endif
41#ifdef HAVE_ISASTREAM 41#ifdef HAVE_ISASTREAM
42# include <stropts.h> 42# include <stropts.h>
43#endif 43#endif
46#elif defined(HAVE_LIBUTIL_H) 46#elif defined(HAVE_LIBUTIL_H)
47# include <libutil.h> 47# include <libutil.h>
48#elif defined(HAVE_UTIL_H) 48#elif defined(HAVE_UTIL_H)
49# include <util.h> 49# include <util.h>
50#endif 50#endif
51#ifdef TTY_GID_SUPPORT
52#include <grp.h>
53#endif
51 54
52#include <cstdio> 55#include <cstdio>
53#include <grp.h>
54 56
55#include "rxvtutil.h" 57#include "rxvtutil.h"
56#include "fdpass.h" 58#include "fdpass.h"
57#include "ptytty.h" 59#include "ptytty.h"
58 60
64/* 66/*
65 * Returns pty file descriptor, or -1 on failure 67 * Returns pty file descriptor, or -1 on failure
66 * If successful, ttydev is set to the name of the slave device. 68 * If successful, ttydev is set to the name of the slave device.
67 * fd_tty _may_ also be set to an open fd to the slave device 69 * fd_tty _may_ also be set to an open fd to the slave device
68 */ 70 */
69static inline int 71#if defined(UNIX98_PTY)
72static int
70get_pty_streams (int *fd_tty, char **ttydev) 73get_pty (int *fd_tty, char **ttydev)
71{ 74{
72#ifdef NO_SETOWNER_TTYDEV
73 int pfd; 75 int pfd;
74 76
75# if defined(PTYS_ARE_GETPT) 77# if defined(HAVE_GETPT)
76 pfd = getpt(); 78 pfd = getpt();
77# elif defined(PTYS_ARE_POSIX) 79# elif defined(HAVE_POSIX_OPENPT)
78 pfd = posix_openpt (O_RDWR); 80 pfd = posix_openpt (O_RDWR);
79# else 81# else
80 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); 82 pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0);
81# endif 83# endif
82 if (pfd >= 0) 84 if (pfd >= 0)
83 { 85 {
84 if (grantpt (pfd) == 0 /* change slave permissions */ 86 if (grantpt (pfd) == 0 /* change slave permissions */
85 && unlockpt (pfd) == 0) 87 && unlockpt (pfd) == 0)
88 return pfd; 90 return pfd;
89 } 91 }
90 92
91 close (pfd); 93 close (pfd);
92 } 94 }
93#endif
94 95
95 return -1; 96 return -1;
96} 97}
97 98#elif defined(HAVE_OPENPTY)
98static inline int 99static int
99get_pty_openpty (int *fd_tty, char **ttydev) 100get_pty (int *fd_tty, char **ttydev)
100{ 101{
101#ifdef PTYS_ARE_OPENPTY
102 int pfd; 102 int pfd;
103 int res; 103 int res;
104 char tty_name[sizeof "/dev/pts/????\0"]; 104 char tty_name[32];
105 105
106 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); 106 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL);
107 if (res != -1) 107 if (res != -1)
108 { 108 {
109 *ttydev = strdup (tty_name); 109 *ttydev = strdup (tty_name);
110 return pfd; 110 return pfd;
111 } 111 }
112#endif
113 112
114 return -1; 113 return -1;
115} 114}
116 115#elif defined(HAVE__GETPTY)
117static inline int 116static int
118get_pty__getpty (int *fd_tty, char **ttydev) 117get_pty (int *fd_tty, char **ttydev)
119{ 118{
120#ifdef PTYS_ARE__GETPTY
121 int pfd; 119 int pfd;
122 120
123 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 121 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
124 if (*ttydev != NULL) 122 if (*ttydev != NULL)
125 return pfd; 123 return pfd;
126#endif
127 124
128 return -1; 125 return -1;
129} 126}
130 127#elif defined(HAVE_DEV_PTC)
131static inline int 128static int
132get_pty_ptc (int *fd_tty, char **ttydev) 129get_pty (int *fd_tty, char **ttydev)
133{ 130{
134#ifdef PTYS_ARE_PTC
135 int pfd; 131 int pfd;
136 132
137 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) 133 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
138 { 134 {
139 *ttydev = strdup (ttyname (pfd)); 135 *ttydev = strdup (ttyname (pfd));
140 return pfd; 136 return pfd;
141 } 137 }
142#endif
143 138
144 return -1; 139 return -1;
145} 140}
146 141#elif defined(HAVE_DEV_CLONE)
147static inline int 142static int
148get_pty_clone (int *fd_tty, char **ttydev) 143get_pty (int *fd_tty, char **ttydev)
149{ 144{
150#ifdef PTYS_ARE_CLONE
151 int pfd; 145 int pfd;
152 146
153 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) 147 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0)
154 { 148 {
155 *ttydev = strdup (ptsname (pfd)); 149 *ttydev = strdup (ptsname (pfd));
156 return pfd; 150 return pfd;
157 } 151 }
158#endif
159 152
160 return -1; 153 return -1;
161} 154}
162 155#else
163static inline int 156/* Based on the code in openssh/openbsd-compat/bsd-openpty.c */
157static int
164get_pty_numeric (int *fd_tty, char **ttydev) 158get_pty (int *fd_tty, char **ttydev)
165{ 159{
166#ifdef PTYS_ARE_NUMERIC
167 int pfd; 160 int pfd;
168 int idx; 161 int i;
169 char *c1, *c2; 162 char pty_name[32];
170 char pty_name[] = "/dev/ptyp???"; 163 char tty_name[32];
171 char tty_name[] = "/dev/ttyp???"; 164 const char *majors = "pqrstuvwxyzabcde";
172 165 const char *minors = "0123456789abcdef";
173 c1 = &(pty_name[sizeof (pty_name) - 4]);
174 c2 = &(tty_name[sizeof (tty_name) - 4]);
175
176 for (idx = 0; idx < 256; idx++) 166 for (i = 0; i < 256; i++)
177 { 167 {
178 sprintf (c1, "%d", idx); 168 snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]);
179 sprintf (c2, "%d", idx); 169 snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]);
180
181 if (access (tty_name, F_OK) < 0)
182 {
183 idx = 256;
184 break;
185 }
186
187 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) 170 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
171 {
172 snprintf(pty_name, 32, "/dev/ptyp%d", i);
173 snprintf(tty_name, 32, "/dev/ttyp%d", i);
174 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
175 continue;
188 { 176 }
189 if (access (tty_name, R_OK | W_OK) == 0) 177 if (access (tty_name, R_OK | W_OK) == 0)
190 { 178 {
191 *ttydev = strdup (tty_name); 179 *ttydev = strdup (tty_name);
192 return pfd; 180 return pfd;
193 } 181 }
194 182
195 close (pfd); 183 close (pfd);
196 }
197 } 184 }
198#endif
199
200 return -1;
201} 185}
202
203static inline int
204get_pty_searched (int *fd_tty, char **ttydev)
205{
206#ifdef PTYS_ARE_SEARCHED
207# ifndef PTYCHAR1
208# define PTYCHAR1 "pqrstuvwxyz"
209# endif 186#endif
210# ifndef PTYCHAR2
211# define PTYCHAR2 "0123456789abcdef"
212# endif
213 int pfd;
214 const char *c1, *c2;
215 char pty_name[] = "/dev/pty??";
216 char tty_name[] = "/dev/tty??";
217
218 for (c1 = PTYCHAR1; *c1; c1++)
219 {
220 pty_name[ (sizeof (pty_name) - 3)] =
221 tty_name[ (sizeof (pty_name) - 3)] = *c1;
222
223 for (c2 = PTYCHAR2; *c2; c2++)
224 {
225 pty_name[ (sizeof (pty_name) - 2)] =
226 tty_name[ (sizeof (pty_name) - 2)] = *c2;
227
228 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
229 {
230 if (access (tty_name, R_OK | W_OK) == 0)
231 {
232 *ttydev = strdup (tty_name);
233 return pfd;
234 }
235
236 close (pfd);
237 }
238 }
239 }
240#endif
241
242 return -1;
243}
244
245static int
246get_pty (int *fd_tty, char **ttydev)
247{
248 int pfd;
249
250 if ((pfd = get_pty_streams (fd_tty, ttydev)) != -1
251 || (pfd = get_pty_openpty (fd_tty, ttydev)) != -1
252 || (pfd = get_pty__getpty (fd_tty, ttydev)) != -1
253 || (pfd = get_pty_ptc (fd_tty, ttydev)) != -1
254 || (pfd = get_pty_clone (fd_tty, ttydev)) != -1
255 || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1
256 || (pfd = get_pty_searched (fd_tty, ttydev)) != -1)
257 return pfd;
258
259 return -1;
260}
261 187
262/*----------------------------------------------------------------------*/ 188/*----------------------------------------------------------------------*/
263/* 189/*
264 * Returns tty file descriptor, or -1 on failure 190 * Returns tty file descriptor, or -1 on failure
265 */ 191 */
276static int 202static int
277control_tty (int fd_tty) 203control_tty (int fd_tty)
278{ 204{
279 setsid (); 205 setsid ();
280 206
281# if defined(PTYS_ARE_PTMX) && defined(I_PUSH) 207#if defined(HAVE_DEV_PTMX) && defined(I_PUSH)
282 /* 208 /*
283 * Push STREAMS modules: 209 * Push STREAMS modules:
284 * ptem: pseudo-terminal hardware emulation module. 210 * ptem: pseudo-terminal hardware emulation module.
285 * ldterm: standard terminal line discipline. 211 * ldterm: standard terminal line discipline.
286 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. 212 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
294 * documentation is really unclear about whether it is any close () on 220 * documentation is really unclear about whether it is any close () on
295 * the master side or the last close () - i.e. a proper STREAMS dismantling 221 * the master side or the last close () - i.e. a proper STREAMS dismantling
296 * close () - on the master side which causes a hang up to be sent 222 * close () - on the master side which causes a hang up to be sent
297 * through - Geoff Wing 223 * through - Geoff Wing
298 */ 224 */
299# ifdef HAVE_ISASTREAM 225# ifdef HAVE_ISASTREAM
300 if (isastream (fd_tty) == 1) 226 if (isastream (fd_tty) == 1)
301# endif 227# endif
302 { 228 {
303 ioctl (fd_tty, I_PUSH, "ptem"); 229 ioctl (fd_tty, I_PUSH, "ptem");
304 ioctl (fd_tty, I_PUSH, "ldterm"); 230 ioctl (fd_tty, I_PUSH, "ldterm");
305 ioctl (fd_tty, I_PUSH, "ttcompat"); 231 ioctl (fd_tty, I_PUSH, "ttcompat");
306 } 232 }
307# endif 233#endif
308 234
309 ioctl (fd_tty, TIOCSCTTY, NULL); 235 ioctl (fd_tty, TIOCSCTTY, NULL);
310 236
311 int fd = open ("/dev/tty", O_WRONLY); 237 int fd = open ("/dev/tty", O_WRONLY);
312 if (fd < 0) 238 if (fd < 0)
358 } 284 }
359 } 285 }
360#endif 286#endif
361} 287}
362 288
363/////////////////////////////////////////////////////////////////////////////
364
365#ifndef NO_SETOWNER_TTYDEV
366static struct ttyconf { 289static struct ttyconf {
367 gid_t gid; 290 gid_t gid;
368 mode_t mode; 291 mode_t mode;
369 292
370 ttyconf () 293 ttyconf ()
379 } 302 }
380 else 303 else
381#endif /* TTY_GID_SUPPORT */ 304#endif /* TTY_GID_SUPPORT */
382 { 305 {
383 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; 306 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
384 gid = getgid (); 307 gid = 0;
385 } 308 }
386 } 309 }
387} ttyconf; 310} ttyconf;
388 311
389/////////////////////////////////////////////////////////////////////////////
390
391void
392rxvt_ptytty_unix::privileges (rxvt_privaction action)
393{
394 if (!name || !*name)
395 return;
396
397 if (action == SAVE)
398 {
399# ifndef RESET_TTY_TO_COMMON_DEFAULTS
400 /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */
401 if (lstat (name, &savestat) < 0) /* you lose out */
402 ;
403 else
404# endif
405 {
406 saved = true;
407 chown (name, getuid (), ttyconf.gid); /* fail silently */
408 chmod (name, ttyconf.mode);
409# ifdef HAVE_REVOKE
410 revoke (name);
411# endif
412 }
413 }
414 else
415 { /* action == RESTORE */
416# ifndef RESET_TTY_TO_COMMON_DEFAULTS
417 if (saved)
418 {
419 chmod (name, savestat.st_mode);
420 chown (name, savestat.st_uid, savestat.st_gid);
421 }
422# else
423 chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
424 chown (name, 0, 0);
425# endif
426 }
427}
428#endif
429
430rxvt_ptytty_unix::rxvt_ptytty_unix () 312rxvt_ptytty_unix::rxvt_ptytty_unix ()
431{ 313{
432 pty = tty = -1;
433 name = 0; 314 name = 0;
434#ifndef NO_SETOWNER_TTYDEV
435 saved = false;
436#endif
437#if UTMP_SUPPORT 315#if UTMP_SUPPORT
438 cmd_pid = 0; 316 cmd_pid = 0;
439#endif 317#endif
440} 318}
441 319
448} 326}
449 327
450void 328void
451rxvt_ptytty_unix::put () 329rxvt_ptytty_unix::put ()
452{ 330{
453#ifndef NO_SETOWNER_TTYDEV 331 chmod (name, RESTORE_TTY_MODE);
454 privileges (RESTORE); 332 chown (name, 0, ttyconf.gid);
455#endif
456 333
457 if (pty >= 0) close (pty);
458 close_tty (); 334 close_tty ();
335
336 if (pty >= 0)
337 close (pty);
338
459 free (name); 339 free (name);
460 340
461 pty = tty = -1; 341 pty = tty = -1;
462 name = 0; 342 name = 0;
463} 343}
473 353
474 /* get slave (tty) */ 354 /* get slave (tty) */
475 if (tty < 0) 355 if (tty < 0)
476 { 356 {
477#ifndef NO_SETOWNER_TTYDEV 357#ifndef NO_SETOWNER_TTYDEV
478 privileges (SAVE); 358 chown (name, getuid (), ttyconf.gid); /* fail silently */
359 chmod (name, ttyconf.mode);
360# ifdef HAVE_REVOKE
361 revoke (name);
362# endif
479#endif 363#endif
480 364
481 if ((tty = get_tty (name)) < 0) 365 if ((tty = get_tty (name)) < 0)
482 { 366 {
483 put (); 367 put ();
647 531
648 serve (); 532 serve ();
649 _exit (EXIT_SUCCESS); 533 _exit (EXIT_SUCCESS);
650 } 534 }
651} 535}
536
652#endif 537#endif
653 538
654// a "factory" *g* 539// a "factory" *g*
655rxvt_ptytty * 540rxvt_ptytty *
656rxvt_new_ptytty () 541rxvt_new_ptytty ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines