From 0ffd30a6e480481d6bc2f5a0303ab26bdd9f4470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Tue, 19 Apr 2005 13:36:20 +0000 Subject: [PATCH] rewrite getpty to make use openpty when its found, save the slave fd so that cleanopen can use it if its available git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@14848 ec53bebd-3082-4978-b11e-865c3cabbd6b --- appl/telnet/telnetd/sys_term.c | 233 +++++++++++++++++---------------- 1 file changed, 119 insertions(+), 114 deletions(-) diff --git a/appl/telnet/telnetd/sys_term.c b/appl/telnet/telnetd/sys_term.c index bcf27bc62..2a44f043b 100644 --- a/appl/telnet/telnetd/sys_term.c +++ b/appl/telnet/telnetd/sys_term.c @@ -358,6 +358,8 @@ getnpty() * Returns the file descriptor of the opened pty. */ +static int ptyslavefd = -1; + static char Xline[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; char *line = Xline; @@ -378,150 +380,150 @@ static char *ptsname(int fd) int getpty(int *ptynum) { -#ifdef __osf__ /* XXX */ - int master; - int slave; - if(openpty(&master, &slave, line, 0, 0) == 0){ - close(slave); - return master; +#if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */ + { + int master; + int slave; + if(openpty(&master, &slave, line, 0, 0) == 0){ + ptyslavefd = slave; + return master; + } } - return -1; -#else +#endif /* HAVE_OPENPTY .... */ #ifdef HAVE__GETPTY - int master, slave; - char *p; - p = _getpty(&master, O_RDWR, 0600, 1); - if(p == NULL) - return -1; - strlcpy(line, p, sizeof(Xline)); - return master; -#else - - int p; - char *cp, *p1, *p2; - int i; -#if SunOS == 40 - int dummy; -#endif -#if __linux - int master; - int slave; - if(openpty(&master, &slave, line, 0, 0) == 0){ - close(slave); + { + int master; + char *p; + p = _getpty(&master, O_RDWR, 0600, 1); + if(p == NULL) + return -1; + strlcpy(line, p, sizeof(Xline)); return master; } -#else +#endif + #ifdef STREAMSPTY - char *clone[] = { "/dev/ptc", "/dev/ptmx", "/dev/ptm", - "/dev/ptym/clone", 0 }; - - char **q; - for(q=clone; *q; q++){ - p=open(*q, O_RDWR); - if(p >= 0){ + { + char *clone[] = { "/dev/ptc", "/dev/ptmx", "/dev/ptm", + "/dev/ptym/clone", 0 }; + + char **q; + for(q=clone; *q; q++){ + p=open(*q, O_RDWR); + if(p >= 0){ #ifdef HAVE_GRANTPT - grantpt(p); + grantpt(p); #endif #ifdef HAVE_UNLOCKPT - unlockpt(p); + unlockpt(p); #endif - strlcpy(line, ptsname(p), sizeof(Xline)); - really_stream = 1; - return p; + strlcpy(line, ptsname(p), sizeof(Xline)); + really_stream = 1; + return p; + } } } #endif /* STREAMSPTY */ #ifndef _CRAY - -#ifndef __hpux - snprintf(line, sizeof(Xline), "/dev/ptyXX"); - p1 = &line[8]; - p2 = &line[9]; -#else - snprintf(line, sizeof(Xline), "/dev/ptym/ptyXX"); - p1 = &line[13]; - p2 = &line[14]; -#endif - + { + int p; + char *cp, *p1, *p2; + int i; - for (cp = "pqrstuvwxyzPQRST"; *cp; cp++) { - struct stat stb; - - *p1 = *cp; - *p2 = '0'; - /* - * This stat() check is just to keep us from - * looping through all 256 combinations if there - * aren't that many ptys available. - */ - if (stat(line, &stb) < 0) - break; - for (i = 0; i < 16; i++) { - *p2 = "0123456789abcdef"[i]; - p = open(line, O_RDWR); - if (p > 0) { #ifndef __hpux - line[5] = 't'; + snprintf(line, sizeof(Xline), "/dev/ptyXX"); + p1 = &line[8]; + p2 = &line[9]; #else - for (p1 = &line[8]; *p1; p1++) - *p1 = *(p1+1); - line[9] = 't'; + snprintf(line, sizeof(Xline), "/dev/ptym/ptyXX"); + p1 = &line[13]; + p2 = &line[14]; #endif - chown(line, 0, 0); - chmod(line, 0600); + + + for (cp = "pqrstuvwxyzPQRST"; *cp; cp++) { + struct stat stb; + + *p1 = *cp; + *p2 = '0'; + /* + * This stat() check is just to keep us from + * looping through all 256 combinations if there + * aren't that many ptys available. + */ + if (stat(line, &stb) < 0) + break; + for (i = 0; i < 16; i++) { + *p2 = "0123456789abcdef"[i]; + p = open(line, O_RDWR); + if (p > 0) { #if SunOS == 40 - if (ioctl(p, TIOCGPGRP, &dummy) == 0 - || errno != EIO) { - chmod(line, 0666); - close(p); - line[5] = 'p'; - } else + int dummy; +#endif + +#ifndef __hpux + line[5] = 't'; +#else + for (p1 = &line[8]; *p1; p1++) + *p1 = *(p1+1); + line[9] = 't'; +#endif + chown(line, 0, 0); + chmod(line, 0600); +#if SunOS == 40 + if (ioctl(p, TIOCGPGRP, &dummy) == 0 + || errno != EIO) { + chmod(line, 0666); + close(p); + line[5] = 'p'; + } else #endif /* SunOS == 40 */ - return(p); + return(p); + } } } } #else /* CRAY */ - extern lowpty, highpty; - struct stat sb; - - for (*ptynum = lowpty; *ptynum <= highpty; (*ptynum)++) { - snprintf(myline, sizeof(myline), "/dev/pty/%03d", *ptynum); - p = open(myline, 2); - if (p < 0) - continue; - snprintf(line, sizeof(Xline), "/dev/ttyp%03d", *ptynum); - /* - * Here are some shenanigans to make sure that there - * are no listeners lurking on the line. - */ - if(stat(line, &sb) < 0) { - close(p); - continue; - } - if(sb.st_uid || sb.st_gid || sb.st_mode != 0600) { - chown(line, 0, 0); - chmod(line, 0600); - close(p); + { + extern lowpty, highpty; + struct stat sb; + int p; + + for (*ptynum = lowpty; *ptynum <= highpty; (*ptynum)++) { + snprintf(myline, sizeof(myline), "/dev/pty/%03d", *ptynum); p = open(myline, 2); if (p < 0) continue; - } - /* - * Now it should be safe...check for accessability. - */ - if (access(line, 6) == 0) - return(p); - else { - /* no tty side to pty so skip it */ - close(p); + snprintf(line, sizeof(Xline), "/dev/ttyp%03d", *ptynum); + /* + * Here are some shenanigans to make sure that there + * are no listeners lurking on the line. + */ + if(stat(line, &sb) < 0) { + close(p); + continue; + } + if(sb.st_uid || sb.st_gid || sb.st_mode != 0600) { + chown(line, 0, 0); + chmod(line, 0600); + close(p); + p = open(myline, 2); + if (p < 0) + continue; + } + /* + * Now it should be safe...check for accessability. + */ + if (access(line, 6) == 0) + return(p); + else { + /* no tty side to pty so skip it */ + close(p); + } } } #endif /* CRAY */ -#endif /* STREAMSPTY */ -#endif /* OPENPTY */ return(-1); -#endif } @@ -966,6 +968,9 @@ int cleanopen(char *line) { int t; + if (ptyslavefd != -1) + return ptyslavefd; + #ifdef STREAMSPTY if (!really_stream) #endif