--- rxvt-unicode/src/ptytty.C 2006/01/19 09:57:20 1.46 +++ rxvt-unicode/src/ptytty.C 2006/01/21 18:15:26 1.53 @@ -35,7 +35,7 @@ #ifdef HAVE_SYS_IOCTL_H # include #endif -#if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) +#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H) # include /* for I_PUSH */ #endif #ifdef HAVE_ISASTREAM @@ -48,9 +48,11 @@ #elif defined(HAVE_UTIL_H) # include #endif +#ifdef TTY_GID_SUPPORT +#include +#endif #include -#include #include "rxvtutil.h" #include "fdpass.h" @@ -66,18 +68,18 @@ * If successful, ttydev is set to the name of the slave device. * fd_tty _may_ also be set to an open fd to the slave device */ -static inline int -get_pty_streams (int *fd_tty, char **ttydev) +#if defined(UNIX98_PTY) +static int +get_pty (int *fd_tty, char **ttydev) { -#ifdef NO_SETOWNER_TTYDEV int pfd; -# if defined(PTYS_ARE_GETPT) +# if defined(HAVE_GETPT) pfd = getpt(); -# elif defined(PTYS_ARE_POSIX) +# elif defined(HAVE_POSIX_OPENPT) pfd = posix_openpt (O_RDWR); # else - pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); + pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0); # endif if (pfd >= 0) { @@ -90,18 +92,16 @@ close (pfd); } -#endif return -1; } - -static inline int -get_pty_openpty (int *fd_tty, char **ttydev) +#elif defined(HAVE_OPENPTY) +static int +get_pty (int *fd_tty, char **ttydev) { -#ifdef PTYS_ARE_OPENPTY int pfd; int res; - char tty_name[sizeof "/dev/pts/????\0"]; + char tty_name[32]; res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); if (res != -1) @@ -109,29 +109,25 @@ *ttydev = strdup (tty_name); return pfd; } -#endif return -1; } - -static inline int -get_pty__getpty (int *fd_tty, char **ttydev) +#elif defined(HAVE__GETPTY) +static int +get_pty (int *fd_tty, char **ttydev) { -#ifdef PTYS_ARE__GETPTY int pfd; *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); if (*ttydev != NULL) return pfd; -#endif return -1; } - -static inline int -get_pty_ptc (int *fd_tty, char **ttydev) +#elif defined(HAVE_DEV_PTC) +static int +get_pty (int *fd_tty, char **ttydev) { -#ifdef PTYS_ARE_PTC int pfd; if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) @@ -139,15 +135,13 @@ *ttydev = strdup (ttyname (pfd)); return pfd; } -#endif return -1; } - -static inline int -get_pty_clone (int *fd_tty, char **ttydev) +#elif defined(HAVE_DEV_CLONE) +static int +get_pty (int *fd_tty, char **ttydev) { -#ifdef PTYS_ARE_CLONE int pfd; if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) @@ -155,109 +149,41 @@ *ttydev = strdup (ptsname (pfd)); return pfd; } -#endif return -1; } - -static inline int -get_pty_numeric (int *fd_tty, char **ttydev) +#else +/* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ +static int +get_pty (int *fd_tty, char **ttydev) { -#ifdef PTYS_ARE_NUMERIC int pfd; - int idx; - char *c1, *c2; - char pty_name[] = "/dev/ptyp???"; - char tty_name[] = "/dev/ttyp???"; - - c1 = &(pty_name[sizeof (pty_name) - 4]); - c2 = &(tty_name[sizeof (tty_name) - 4]); - - for (idx = 0; idx < 256; idx++) - { - sprintf (c1, "%d", idx); - sprintf (c2, "%d", idx); - - if (access (tty_name, F_OK) < 0) - { - idx = 256; - break; + int i; + char pty_name[32]; + char tty_name[32]; + const char *majors = "pqrstuvwxyzabcde"; + const char *minors = "0123456789abcdef"; + for (i = 0; i < 256; i++) + { + snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); + snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); + if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) + { + snprintf(pty_name, 32, "/dev/ptyp%d", i); + snprintf(tty_name, 32, "/dev/ttyp%d", i); + if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) + continue; } - - if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) + if (access (tty_name, R_OK | W_OK) == 0) { - if (access (tty_name, R_OK | W_OK) == 0) - { - *ttydev = strdup (tty_name); - return pfd; - } - - close (pfd); + *ttydev = strdup (tty_name); + return pfd; } - } -#endif - return -1; -} - -static inline int -get_pty_searched (int *fd_tty, char **ttydev) -{ -#ifdef PTYS_ARE_SEARCHED -# ifndef PTYCHAR1 -# define PTYCHAR1 "pqrstuvwxyz" -# endif -# ifndef PTYCHAR2 -# define PTYCHAR2 "0123456789abcdef" -# endif - int pfd; - const char *c1, *c2; - char pty_name[] = "/dev/pty??"; - char tty_name[] = "/dev/tty??"; - - for (c1 = PTYCHAR1; *c1; c1++) - { - pty_name[ (sizeof (pty_name) - 3)] = - tty_name[ (sizeof (pty_name) - 3)] = *c1; - - for (c2 = PTYCHAR2; *c2; c2++) - { - pty_name[ (sizeof (pty_name) - 2)] = - tty_name[ (sizeof (pty_name) - 2)] = *c2; - - if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) - { - if (access (tty_name, R_OK | W_OK) == 0) - { - *ttydev = strdup (tty_name); - return pfd; - } - - close (pfd); - } - } + close (pfd); } -#endif - - return -1; -} - -static int -get_pty (int *fd_tty, char **ttydev) -{ - int pfd; - - if ((pfd = get_pty_streams (fd_tty, ttydev)) != -1 - || (pfd = get_pty_openpty (fd_tty, ttydev)) != -1 - || (pfd = get_pty__getpty (fd_tty, ttydev)) != -1 - || (pfd = get_pty_ptc (fd_tty, ttydev)) != -1 - || (pfd = get_pty_clone (fd_tty, ttydev)) != -1 - || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1 - || (pfd = get_pty_searched (fd_tty, ttydev)) != -1) - return pfd; - - return -1; } +#endif /*----------------------------------------------------------------------*/ /* @@ -278,7 +204,7 @@ { setsid (); -# if defined(PTYS_ARE_PTMX) && defined(I_PUSH) +#if defined(HAVE_DEV_PTMX) && defined(I_PUSH) /* * Push STREAMS modules: * ptem: pseudo-terminal hardware emulation module. @@ -296,15 +222,15 @@ * close () - on the master side which causes a hang up to be sent * through - Geoff Wing */ -# ifdef HAVE_ISASTREAM +# ifdef HAVE_ISASTREAM if (isastream (fd_tty) == 1) -# endif +# endif { ioctl (fd_tty, I_PUSH, "ptem"); ioctl (fd_tty, I_PUSH, "ldterm"); ioctl (fd_tty, I_PUSH, "ttcompat"); } -# endif +#endif ioctl (fd_tty, TIOCSCTTY, NULL); @@ -360,9 +286,6 @@ #endif } -///////////////////////////////////////////////////////////////////////////// - -#ifndef NO_SETOWNER_TTYDEV static struct ttyconf { gid_t gid; mode_t mode; @@ -381,59 +304,14 @@ #endif /* TTY_GID_SUPPORT */ { mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; - gid = getgid (); + gid = 0; } } } ttyconf; -///////////////////////////////////////////////////////////////////////////// - -void -rxvt_ptytty_unix::privileges (rxvt_privaction action) -{ - if (!name || !*name) - return; - - if (action == SAVE) - { -# ifndef RESET_TTY_TO_COMMON_DEFAULTS - /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */ - if (lstat (name, &savestat) < 0) /* you lose out */ - ; - else -# endif - { - saved = true; - chown (name, getuid (), ttyconf.gid); /* fail silently */ - chmod (name, ttyconf.mode); -# ifdef HAVE_REVOKE - revoke (name); -# endif - } - } - else - { /* action == RESTORE */ -# ifndef RESET_TTY_TO_COMMON_DEFAULTS - if (saved) - { - chmod (name, savestat.st_mode); - chown (name, savestat.st_uid, savestat.st_gid); - } -# else - chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); - chown (name, 0, 0); -# endif - } -} -#endif - rxvt_ptytty_unix::rxvt_ptytty_unix () { - pty = tty = -1; name = 0; -#ifndef NO_SETOWNER_TTYDEV - saved = false; -#endif #if UTMP_SUPPORT cmd_pid = 0; #endif @@ -450,12 +328,14 @@ void rxvt_ptytty_unix::put () { -#ifndef NO_SETOWNER_TTYDEV - privileges (RESTORE); -#endif + chmod (name, RESTORE_TTY_MODE); + chown (name, 0, ttyconf.gid); - if (pty >= 0) close (pty); close_tty (); + + if (pty >= 0) + close (pty); + free (name); pty = tty = -1; @@ -475,7 +355,11 @@ if (tty < 0) { #ifndef NO_SETOWNER_TTYDEV - privileges (SAVE); + chown (name, getuid (), ttyconf.gid); /* fail silently */ + chmod (name, ttyconf.mode); +# ifdef HAVE_REVOKE + revoke (name); +# endif #endif if ((tty = get_tty (name)) < 0) @@ -649,6 +533,7 @@ _exit (EXIT_SUCCESS); } } + #endif // a "factory" *g*