--- rxvt-unicode/src/ptytty.C 2006/01/17 17:59:31 1.44 +++ rxvt-unicode/src/ptytty.C 2006/01/19 10:44:44 1.48 @@ -72,15 +72,13 @@ #ifdef NO_SETOWNER_TTYDEV 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 +87,11 @@ *ttydev = strdup (ptsname (pfd)); /* get slave's name */ return pfd; } + close (pfd); } #endif + return -1; } @@ -110,6 +110,7 @@ return pfd; } #endif + return -1; } @@ -123,6 +124,7 @@ if (*ttydev != NULL) return pfd; #endif + return -1; } @@ -138,6 +140,7 @@ return pfd; } #endif + return -1; } @@ -153,6 +156,7 @@ return pfd; } #endif + return -1; } @@ -160,37 +164,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 +210,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 +255,7 @@ || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1 || (pfd = get_pty_searched (fd_tty, ttydev)) != -1) return pfd; + return -1; } @@ -265,13 +276,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 +296,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; } @@ -374,7 +381,7 @@ #endif /* TTY_GID_SUPPORT */ { mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; - gid = getgid (); + gid = 0; } } } ttyconf; @@ -389,34 +396,16 @@ 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); + chown (name, getuid (), ttyconf.gid); /* fail silently */ + chmod (name, ttyconf.mode); # ifdef HAVE_REVOKE - revoke (name); + 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 - + chmod (name, RESTORE_TTY_MODE); + chown (name, 0, ttyconf.gid); } } #endif @@ -425,9 +414,6 @@ { pty = tty = -1; name = 0; -#ifndef NO_SETOWNER_TTYDEV - saved = false; -#endif #if UTMP_SUPPORT cmd_pid = 0; #endif @@ -632,14 +618,11 @@ } 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 ();