--- rxvt-unicode/src/ptytty.C 2006/01/17 17:59:31 1.44 +++ rxvt-unicode/src/ptytty.C 2006/01/19 18:53:05 1.51 @@ -69,18 +69,17 @@ static inline int get_pty_streams (int *fd_tty, char **ttydev) { -#ifdef NO_SETOWNER_TTYDEV +#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) +# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) int pfd; -# ifdef PTYS_ARE_GETPT +# if defined(PTYS_ARE_GETPT) pfd = getpt(); -# else -# ifdef PTYS_ARE_POSIX +# elif defined(PTYS_ARE_POSIX) pfd = posix_openpt (O_RDWR); # else pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); # endif -# endif if (pfd >= 0) { if (grantpt (pfd) == 0 /* change slave permissions */ @@ -89,9 +88,12 @@ *ttydev = strdup (ptsname (pfd)); /* get slave's name */ return pfd; } + close (pfd); } +# endif #endif + return -1; } @@ -101,7 +103,7 @@ #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) @@ -110,6 +112,7 @@ return pfd; } #endif + return -1; } @@ -123,6 +126,7 @@ if (*ttydev != NULL) return pfd; #endif + return -1; } @@ -138,6 +142,7 @@ return pfd; } #endif + return -1; } @@ -153,6 +158,7 @@ return pfd; } #endif + return -1; } @@ -160,37 +166,40 @@ get_pty_numeric (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; - } - - 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); - } - } + 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; + } + + 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); + } + } #endif - return -1; + + return -1; } static inline int @@ -203,33 +212,36 @@ # 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); - } - } - } + 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); + } + } + } #endif - return -1; + + return -1; } static int @@ -245,6 +257,7 @@ || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1 || (pfd = get_pty_searched (fd_tty, ttydev)) != -1) return pfd; + return -1; } @@ -265,13 +278,9 @@ static int control_tty (int fd_tty) { - int fd; - - /* ---------------------------------------- */ setsid (); - /* ---------------------------------------- */ -# if defined(PTYS_ARE_PTMX) && defined(I_PUSH) +#if defined(PTYS_ARE_PTMX) && defined(I_PUSH) /* * Push STREAMS modules: * ptem: pseudo-terminal hardware emulation module. @@ -289,23 +298,23 @@ * 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 - /* ---------------------------------------- */ - fd = ioctl (fd_tty, TIOCSCTTY, NULL); - /* ---------------------------------------- */ - fd = open ("/dev/tty", O_WRONLY); +#endif + + ioctl (fd_tty, TIOCSCTTY, NULL); + + int fd = open ("/dev/tty", O_WRONLY); if (fd < 0) return -1; /* fatal */ + close (fd); - /* ---------------------------------------- */ return 0; } @@ -353,9 +362,6 @@ #endif } -///////////////////////////////////////////////////////////////////////////// - -#ifndef NO_SETOWNER_TTYDEV static struct ttyconf { gid_t gid; mode_t mode; @@ -374,60 +380,15 @@ #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 @@ -444,12 +405,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; @@ -469,7 +432,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) @@ -632,20 +599,18 @@ } else { - setgid (getegid ()); - setuid (geteuid ()); - // server, pty-helper sock_fd = sv[1]; for (int fd = 0; fd < 1023; fd++) - if (fd != sock_fd && fd != 1) + if (fd != sock_fd) close (fd); serve (); _exit (EXIT_SUCCESS); } } + #endif // a "factory" *g*