diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index ca664ea1..2235b55d 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -150,7 +150,7 @@ if (UNIX) check_function_exists(posix_openpt HAVE_POSIX_OPENPT) endif (openpty_in_libc OR openpty_in_libutil) - check_function_exists(ptsname HAVE_PTSNAME) + check_function_exists(ptsname_r HAVE_PTSNAME_R) check_function_exists(tcgetattr HAVE_TCGETATTR) check_function_exists(tcsetattr HAVE_TCSETATTR) endif (UNIX) diff --git a/config-pty.h.cmake b/config-pty.h.cmake index 3daf8155..3ee54cf3 100644 --- a/config-pty.h.cmake +++ b/config-pty.h.cmake @@ -5,7 +5,7 @@ #cmakedefine HAVE_POSIX_OPENPT 1 #cmakedefine HAVE_GRANTPT 1 #cmakedefine HAVE_OPENPTY 1 -#cmakedefine HAVE_PTSNAME 1 +#cmakedefine HAVE_PTSNAME_R 1 #cmakedefine HAVE_REVOKE 1 #cmakedefine HAVE_UNLOCKPT 1 #cmakedefine HAVE_TCGETATTR diff --git a/kpty/kgrantpty.c b/kpty/kgrantpty.c index d015c4fc..f11a4348 100644 --- a/kpty/kgrantpty.c +++ b/kpty/kgrantpty.c @@ -69,10 +69,9 @@ int main (int argc, char *argv[]) gid_t gid; uid_t uid; mode_t mod; - char* tty; + char* tty = 0; int fd; -#if !defined(HAVE_PTSNAME) && defined(TIOCGPTN) - int ptyno; +#if defined(HAVE_PTSNAME_R) char ttyb[32]; #endif @@ -96,14 +95,14 @@ int main (int argc, char *argv[]) fd = atoi(argv[2]); /* get slave pty name from master pty file handle *********/ -#ifdef HAVE_PTSNAME +#ifdef HAVE_PTSNAME_R + ::memset(ttyb, '\0', sizeof(ttyb) * sizeof(char)); + if (ptsname_r(fd, ttyb, sizeof(ttyb)) == 0) { + tty = ttyb; + if (!tty) +#else tty = ptsname(fd); if (!tty) -#elif defined(TIOCGPTN) - if (!ioctl(fd, TIOCGPTN, &ptyno)) { - sprintf(ttyb, "/dev/pts/%d", ptyno); - tty = ttyb; - } else #endif { /* Check that fd is a valid master pseudo terminal. */ diff --git a/kpty/kpty.cpp b/kpty/kpty.cpp index 2a02668d..ae74e864 100644 --- a/kpty/kpty.cpp +++ b/kpty/kpty.cpp @@ -190,8 +190,6 @@ bool KPty::open() #else -#if defined(HAVE_PTSNAME) || defined(TIOCGPTN) - #ifdef HAVE_POSIX_OPENPT d->masterFd = ::posix_openpt(O_RDWR|O_NOCTTY); #elif defined(PTM_DEVICE) @@ -201,16 +199,15 @@ bool KPty::open() #endif if (d->masterFd >= 0) { -#ifdef HAVE_PTSNAME +#ifdef HAVE_PTSNAME_R + char ptsn[32]; + ::memset(ptsn, '\0', sizeof(ptsn) * sizeof(char)); + if (ptsname_r(fd, ptsn, sizeof(ptsn)) == 0) { + d->ttyName = ptsn; +#else char *ptsn = ptsname(d->masterFd); if (ptsn) { d->ttyName = ptsn; -#else - int ptyno; - if (!ioctl(d->masterFd, TIOCGPTN, &ptyno)) { - char buf[32]; - sprintf(buf, "/dev/pts/%d", ptyno); - d->ttyName = buf; #endif #ifdef HAVE_GRANTPT if (!grantpt(d->masterFd)) @@ -222,7 +219,6 @@ bool KPty::open() ::close(d->masterFd); d->masterFd = -1; } -#endif // HAVE_PTSNAME || TIOCGPTN // Linux device names, FIXME: Trouble on other systems? for (const char* s3 = "pqrstuvwxyzabcde"; *s3; s3++) @@ -332,10 +328,6 @@ bool KPty::open() bool KPty::open(int fd) { -#if !defined(HAVE_PTSNAME) && !defined(TIOCGPTN) - kWarning(175) << "Unsupported attempt to open pty with fd" << fd; - return false; -#else Q_D(KPty); if (d->masterFd >= 0) { @@ -345,16 +337,15 @@ bool KPty::open(int fd) d->ownMaster = false; -# ifdef HAVE_PTSNAME +# ifdef HAVE_PTSNAME_R + char ptsn[32]; + ::memset(ptsn, '\0', sizeof(ptsn) * sizeof(char)); + if (ptsname_r(fd, ptsn, sizeof(ptsn)) == 0) { + d->ttyName = ptsn; +# else char *ptsn = ptsname(fd); if (ptsn) { d->ttyName = ptsn; -# else - int ptyno; - if (!ioctl(fd, TIOCGPTN, &ptyno)) { - char buf[32]; - sprintf(buf, "/dev/pts/%d", ptyno); - d->ttyName = buf; # endif } else { kWarning(175) << "Failed to determine pty slave device for fd" << fd; @@ -368,7 +359,6 @@ bool KPty::open(int fd) } return true; -#endif } void KPty::closeSlave()