--- rxvt-unicode/src/ptytty.C 2006/01/17 15:41:33 1.41 +++ rxvt-unicode/src/ptytty.C 2006/01/17 18:06:14 1.45 @@ -66,16 +66,44 @@ * 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 int -get_pty (int *fd_tty, char **ttydev) +static inline int +get_pty_streams (int *fd_tty, char **ttydev) { +#ifdef NO_SETOWNER_TTYDEV int pfd; -#ifdef PTYS_ARE_OPENPTY - char tty_name[sizeof "/dev/pts/????\0"]; +# if defined(PTYS_ARE_GETPT) + pfd = getpt(); +# elif defined(PTYS_ARE_POSIX) + pfd = posix_openpt (O_RDWR); +# else + pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); +# endif + if (pfd >= 0) + { + if (grantpt (pfd) == 0 /* change slave permissions */ + && unlockpt (pfd) == 0) + { /* slave now unlocked */ + *ttydev = strdup (ptsname (pfd)); /* get slave's name */ + return pfd; + } + + close (pfd); + } +#endif - int res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); + return -1; +} +static inline int +get_pty_openpty (int *fd_tty, char **ttydev) +{ +#ifdef PTYS_ARE_OPENPTY + int pfd; + int res; + char tty_name[sizeof "/dev/pts/????\0"]; + + res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); if (res != -1) { *ttydev = strdup (tty_name); @@ -83,41 +111,29 @@ } #endif + return -1; +} + +static inline int +get_pty__getpty (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 -#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) -# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) - - { -# ifdef PTYS_ARE_GETPT - pfd = getpt(); -# else -# ifdef 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 */ - && unlockpt (pfd) == 0) - { /* slave now unlocked */ - *ttydev = strdup (ptsname (pfd)); /* get slave's name */ - return pfd; - } - close (pfd); - } - } -# endif -#endif + return -1; +} +static inline int +get_pty_ptc (int *fd_tty, char **ttydev) +{ #ifdef PTYS_ARE_PTC + int pfd; + if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) { *ttydev = strdup (ttyname (pfd)); @@ -125,7 +141,15 @@ } #endif + return -1; +} + +static inline int +get_pty_clone (int *fd_tty, char **ttydev) +{ #ifdef PTYS_ARE_CLONE + int pfd; + if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) { *ttydev = strdup (ptsname (pfd)); @@ -133,78 +157,108 @@ } #endif + return -1; +} + +static inline int +get_pty_numeric (int *fd_tty, char **ttydev) +{ #ifdef PTYS_ARE_NUMERIC - { - 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 -#ifdef PTYS_ARE_SEARCHED - { - const char *c1, *c2; - char pty_name[] = "/dev/pty??"; - char tty_name[] = "/dev/tty??"; + 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; - 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); - } - } - } - } + 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; } +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; +} + /*----------------------------------------------------------------------*/ /* * Returns tty file descriptor, or -1 on failure @@ -222,12 +276,8 @@ static int control_tty (int fd_tty) { - int fd; - - /* ---------------------------------------- */ setsid (); - /* ---------------------------------------- */ # if defined(PTYS_ARE_PTMX) && defined(I_PUSH) /* * Push STREAMS modules: @@ -255,14 +305,14 @@ ioctl (fd_tty, I_PUSH, "ttcompat"); } # endif - /* ---------------------------------------- */ - fd = ioctl (fd_tty, TIOCSCTTY, NULL); - /* ---------------------------------------- */ - fd = open ("/dev/tty", O_WRONLY); + + ioctl (fd_tty, TIOCSCTTY, NULL); + + int fd = open ("/dev/tty", O_WRONLY); if (fd < 0) return -1; /* fatal */ + close (fd); - /* ---------------------------------------- */ return 0; } @@ -373,7 +423,6 @@ chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); chown (name, 0, 0); # endif - } } #endif @@ -392,7 +441,9 @@ rxvt_ptytty_unix::~rxvt_ptytty_unix () { +#if UTMP_SUPPORT logout (); +#endif put (); } @@ -539,20 +590,22 @@ } else if (cmd.type == command::login) { +#if UTMP_SUPPORT if (find (ptys.begin (), ptys.end (), cmd.id)) { cmd.hostname[sizeof (cmd.hostname) - 1] = 0; cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); } +#endif } else if (cmd.type == command::destroy) { rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); - if (*pty) + if (pty) { - ptys.erase (pty); delete *pty; + ptys.erase (pty); } } else @@ -592,7 +645,7 @@ sock_fd = sv[1]; for (int fd = 0; fd < 1023; fd++) - if (fd != sock_fd) + if (fd != sock_fd && fd != 1) close (fd); serve ();