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.44 by ayin, Tue Jan 17 17:59:31 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# ifdef PTYS_ARE_GETPT 77# if defined(HAVE_GETPT)
76 pfd = getpt(); 78 pfd = getpt();
77# else 79# elif defined(HAVE_POSIX_OPENPT)
78# ifdef PTYS_ARE_POSIX
79 pfd = posix_openpt (O_RDWR); 80 pfd = posix_openpt (O_RDWR);
80# else 81# else
81 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); 82 pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0);
82# endif
83# endif 83# endif
84 if (pfd >= 0) 84 if (pfd >= 0)
85 { 85 {
86 if (grantpt (pfd) == 0 /* change slave permissions */ 86 if (grantpt (pfd) == 0 /* change slave permissions */
87 && unlockpt (pfd) == 0) 87 && unlockpt (pfd) == 0)
88 { /* slave now unlocked */ 88 { /* slave now unlocked */
89 *ttydev = strdup (ptsname (pfd)); /* get slave's name */ 89 *ttydev = strdup (ptsname (pfd)); /* get slave's name */
90 return pfd; 90 return pfd;
91 } 91 }
92
92 close (pfd); 93 close (pfd);
93 } 94 }
94#endif 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 112
113 return -1; 113 return -1;
114} 114}
115 115#elif defined(HAVE__GETPTY)
116static inline int 116static int
117get_pty__getpty (int *fd_tty, char **ttydev) 117get_pty (int *fd_tty, char **ttydev)
118{ 118{
119#ifdef PTYS_ARE__GETPTY
120 int pfd; 119 int pfd;
121 120
122 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 121 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
123 if (*ttydev != NULL) 122 if (*ttydev != NULL)
124 return pfd; 123 return pfd;
125#endif 124
126 return -1; 125 return -1;
127} 126}
128 127#elif defined(HAVE_DEV_PTC)
129static inline int 128static int
130get_pty_ptc (int *fd_tty, char **ttydev) 129get_pty (int *fd_tty, char **ttydev)
131{ 130{
132#ifdef PTYS_ARE_PTC
133 int pfd; 131 int pfd;
134 132
135 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) 133 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
136 { 134 {
137 *ttydev = strdup (ttyname (pfd)); 135 *ttydev = strdup (ttyname (pfd));
138 return pfd; 136 return pfd;
139 } 137 }
140#endif 138
141 return -1; 139 return -1;
142} 140}
143 141#elif defined(HAVE_DEV_CLONE)
144static inline int 142static int
145get_pty_clone (int *fd_tty, char **ttydev) 143get_pty (int *fd_tty, char **ttydev)
146{ 144{
147#ifdef PTYS_ARE_CLONE
148 int pfd; 145 int pfd;
149 146
150 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)
151 { 148 {
152 *ttydev = strdup (ptsname (pfd)); 149 *ttydev = strdup (ptsname (pfd));
153 return pfd; 150 return pfd;
154 } 151 }
155#endif 152
156 return -1; 153 return -1;
157} 154}
158 155#else
159static inline int 156/* Based on the code in openssh/openbsd-compat/bsd-openpty.c */
160get_pty_numeric (int *fd_tty, char **ttydev)
161{
162#ifdef PTYS_ARE_NUMERIC
163 int pfd;
164 int idx;
165 char *c1, *c2;
166 char pty_name[] = "/dev/ptyp???";
167 char tty_name[] = "/dev/ttyp???";
168
169 c1 = &(pty_name[sizeof (pty_name) - 4]);
170 c2 = &(tty_name[sizeof (tty_name) - 4]);
171 for (idx = 0; idx < 256; idx++)
172 {
173 sprintf (c1, "%d", idx);
174 sprintf (c2, "%d", idx);
175 if (access (tty_name, F_OK) < 0)
176 {
177 idx = 256;
178 break;
179 }
180
181 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
182 {
183 if (access (tty_name, R_OK | W_OK) == 0)
184 {
185 *ttydev = strdup (tty_name);
186 return pfd;
187 }
188
189 close (pfd);
190 }
191 }
192#endif
193 return -1;
194}
195
196static inline int
197get_pty_searched (int *fd_tty, char **ttydev)
198{
199#ifdef PTYS_ARE_SEARCHED
200# ifndef PTYCHAR1
201# define PTYCHAR1 "pqrstuvwxyz"
202# endif
203# ifndef PTYCHAR2
204# define PTYCHAR2 "0123456789abcdef"
205# endif
206 int pfd;
207 const char *c1, *c2;
208 char pty_name[] = "/dev/pty??";
209 char tty_name[] = "/dev/tty??";
210
211 for (c1 = PTYCHAR1; *c1; c1++)
212 {
213 pty_name[ (sizeof (pty_name) - 3)] =
214 tty_name[ (sizeof (pty_name) - 3)] = *c1;
215 for (c2 = PTYCHAR2; *c2; c2++)
216 {
217 pty_name[ (sizeof (pty_name) - 2)] =
218 tty_name[ (sizeof (pty_name) - 2)] = *c2;
219 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
220 {
221 if (access (tty_name, R_OK | W_OK) == 0)
222 {
223 *ttydev = strdup (tty_name);
224 return pfd;
225 }
226
227 close (pfd);
228 }
229 }
230 }
231#endif
232 return -1;
233}
234
235static int 157static int
236get_pty (int *fd_tty, char **ttydev) 158get_pty (int *fd_tty, char **ttydev)
237{ 159{
238 int pfd; 160 int pfd;
239 161 int i;
240 if ((pfd = get_pty_streams (fd_tty, ttydev)) != -1 162 char pty_name[32];
241 || (pfd = get_pty_openpty (fd_tty, ttydev)) != -1 163 char tty_name[32];
242 || (pfd = get_pty__getpty (fd_tty, ttydev)) != -1 164 const char *majors = "pqrstuvwxyzabcde";
243 || (pfd = get_pty_ptc (fd_tty, ttydev)) != -1 165 const char *minors = "0123456789abcdef";
244 || (pfd = get_pty_clone (fd_tty, ttydev)) != -1 166 for (i = 0; i < 256; i++)
245 || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1 167 {
246 || (pfd = get_pty_searched (fd_tty, ttydev)) != -1) 168 snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]);
169 snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]);
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;
176 }
177 if (access (tty_name, R_OK | W_OK) == 0)
178 {
179 *ttydev = strdup (tty_name);
247 return pfd; 180 return pfd;
248 return -1; 181 }
182
183 close (pfd);
184 }
249} 185}
186#endif
250 187
251/*----------------------------------------------------------------------*/ 188/*----------------------------------------------------------------------*/
252/* 189/*
253 * Returns tty file descriptor, or -1 on failure 190 * Returns tty file descriptor, or -1 on failure
254 */ 191 */
263 * Make our tty a controlling tty so that /dev/tty points to us 200 * Make our tty a controlling tty so that /dev/tty points to us
264 */ 201 */
265static int 202static int
266control_tty (int fd_tty) 203control_tty (int fd_tty)
267{ 204{
268 int fd;
269
270 /* ---------------------------------------- */
271 setsid (); 205 setsid ();
272 206
273 /* ---------------------------------------- */
274# if defined(PTYS_ARE_PTMX) && defined(I_PUSH) 207#if defined(HAVE_DEV_PTMX) && defined(I_PUSH)
275 /* 208 /*
276 * Push STREAMS modules: 209 * Push STREAMS modules:
277 * ptem: pseudo-terminal hardware emulation module. 210 * ptem: pseudo-terminal hardware emulation module.
278 * ldterm: standard terminal line discipline. 211 * ldterm: standard terminal line discipline.
279 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. 212 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
287 * documentation is really unclear about whether it is any close () on 220 * documentation is really unclear about whether it is any close () on
288 * 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
289 * 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
290 * through - Geoff Wing 223 * through - Geoff Wing
291 */ 224 */
292# ifdef HAVE_ISASTREAM 225# ifdef HAVE_ISASTREAM
293 if (isastream (fd_tty) == 1) 226 if (isastream (fd_tty) == 1)
294# endif 227# endif
295 { 228 {
296 ioctl (fd_tty, I_PUSH, "ptem"); 229 ioctl (fd_tty, I_PUSH, "ptem");
297 ioctl (fd_tty, I_PUSH, "ldterm"); 230 ioctl (fd_tty, I_PUSH, "ldterm");
298 ioctl (fd_tty, I_PUSH, "ttcompat"); 231 ioctl (fd_tty, I_PUSH, "ttcompat");
299 } 232 }
300# endif 233#endif
301 /* ---------------------------------------- */ 234
302 fd = ioctl (fd_tty, TIOCSCTTY, NULL); 235 ioctl (fd_tty, TIOCSCTTY, NULL);
303 /* ---------------------------------------- */ 236
304 fd = open ("/dev/tty", O_WRONLY); 237 int fd = open ("/dev/tty", O_WRONLY);
305 if (fd < 0) 238 if (fd < 0)
306 return -1; /* fatal */ 239 return -1; /* fatal */
240
307 close (fd); 241 close (fd);
308 /* ---------------------------------------- */
309 242
310 return 0; 243 return 0;
311} 244}
312 245
313void 246void
351 } 284 }
352 } 285 }
353#endif 286#endif
354} 287}
355 288
356/////////////////////////////////////////////////////////////////////////////
357
358#ifndef NO_SETOWNER_TTYDEV
359static struct ttyconf { 289static struct ttyconf {
360 gid_t gid; 290 gid_t gid;
361 mode_t mode; 291 mode_t mode;
362 292
363 ttyconf () 293 ttyconf ()
372 } 302 }
373 else 303 else
374#endif /* TTY_GID_SUPPORT */ 304#endif /* TTY_GID_SUPPORT */
375 { 305 {
376 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; 306 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
377 gid = getgid (); 307 gid = 0;
378 } 308 }
379 } 309 }
380} ttyconf; 310} ttyconf;
381 311
382/////////////////////////////////////////////////////////////////////////////
383
384void
385rxvt_ptytty_unix::privileges (rxvt_privaction action)
386{
387 if (!name || !*name)
388 return;
389
390 if (action == SAVE)
391 {
392# ifndef RESET_TTY_TO_COMMON_DEFAULTS
393 /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */
394 if (lstat (name, &savestat) < 0) /* you lose out */
395 ;
396 else
397# endif
398 {
399 saved = true;
400 chown (name, getuid (), ttyconf.gid); /* fail silently */
401 chmod (name, ttyconf.mode);
402# ifdef HAVE_REVOKE
403 revoke (name);
404# endif
405 }
406 }
407 else
408 { /* action == RESTORE */
409# ifndef RESET_TTY_TO_COMMON_DEFAULTS
410 if (saved)
411 {
412 chmod (name, savestat.st_mode);
413 chown (name, savestat.st_uid, savestat.st_gid);
414 }
415# else
416 chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
417 chown (name, 0, 0);
418# endif
419
420 }
421}
422#endif
423
424rxvt_ptytty_unix::rxvt_ptytty_unix () 312rxvt_ptytty_unix::rxvt_ptytty_unix ()
425{ 313{
426 pty = tty = -1;
427 name = 0; 314 name = 0;
428#ifndef NO_SETOWNER_TTYDEV
429 saved = false;
430#endif
431#if UTMP_SUPPORT 315#if UTMP_SUPPORT
432 cmd_pid = 0; 316 cmd_pid = 0;
433#endif 317#endif
434} 318}
435 319
442} 326}
443 327
444void 328void
445rxvt_ptytty_unix::put () 329rxvt_ptytty_unix::put ()
446{ 330{
447#ifndef NO_SETOWNER_TTYDEV 331 chmod (name, RESTORE_TTY_MODE);
448 privileges (RESTORE); 332 chown (name, 0, ttyconf.gid);
449#endif
450 333
451 if (pty >= 0) close (pty);
452 close_tty (); 334 close_tty ();
335
336 if (pty >= 0)
337 close (pty);
338
453 free (name); 339 free (name);
454 340
455 pty = tty = -1; 341 pty = tty = -1;
456 name = 0; 342 name = 0;
457} 343}
467 353
468 /* get slave (tty) */ 354 /* get slave (tty) */
469 if (tty < 0) 355 if (tty < 0)
470 { 356 {
471#ifndef NO_SETOWNER_TTYDEV 357#ifndef NO_SETOWNER_TTYDEV
472 privileges (SAVE); 358 chown (name, getuid (), ttyconf.gid); /* fail silently */
359 chmod (name, ttyconf.mode);
360# ifdef HAVE_REVOKE
361 revoke (name);
362# endif
473#endif 363#endif
474 364
475 if ((tty = get_tty (name)) < 0) 365 if ((tty = get_tty (name)) < 0)
476 { 366 {
477 put (); 367 put ();
630 close (sv[1]); 520 close (sv[1]);
631 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 521 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
632 } 522 }
633 else 523 else
634 { 524 {
635 setgid (getegid ());
636 setuid (geteuid ());
637
638 // server, pty-helper 525 // server, pty-helper
639 sock_fd = sv[1]; 526 sock_fd = sv[1];
640 527
641 for (int fd = 0; fd < 1023; fd++) 528 for (int fd = 0; fd < 1023; fd++)
642 if (fd != sock_fd && fd != 1) 529 if (fd != sock_fd)
643 close (fd); 530 close (fd);
644 531
645 serve (); 532 serve ();
646 _exit (EXIT_SUCCESS); 533 _exit (EXIT_SUCCESS);
647 } 534 }
648} 535}
536
649#endif 537#endif
650 538
651// a "factory" *g* 539// a "factory" *g*
652rxvt_ptytty * 540rxvt_ptytty *
653rxvt_new_ptytty () 541rxvt_new_ptytty ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines