kpty: use ptsname_r() if available, assume ptsname() is available

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2021-06-16 18:48:45 +03:00
parent ac0a81cd61
commit e529267d21
4 changed files with 22 additions and 33 deletions

View file

@ -150,7 +150,7 @@ if (UNIX)
check_function_exists(posix_openpt HAVE_POSIX_OPENPT) check_function_exists(posix_openpt HAVE_POSIX_OPENPT)
endif (openpty_in_libc OR openpty_in_libutil) 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(tcgetattr HAVE_TCGETATTR)
check_function_exists(tcsetattr HAVE_TCSETATTR) check_function_exists(tcsetattr HAVE_TCSETATTR)
endif (UNIX) endif (UNIX)

View file

@ -5,7 +5,7 @@
#cmakedefine HAVE_POSIX_OPENPT 1 #cmakedefine HAVE_POSIX_OPENPT 1
#cmakedefine HAVE_GRANTPT 1 #cmakedefine HAVE_GRANTPT 1
#cmakedefine HAVE_OPENPTY 1 #cmakedefine HAVE_OPENPTY 1
#cmakedefine HAVE_PTSNAME 1 #cmakedefine HAVE_PTSNAME_R 1
#cmakedefine HAVE_REVOKE 1 #cmakedefine HAVE_REVOKE 1
#cmakedefine HAVE_UNLOCKPT 1 #cmakedefine HAVE_UNLOCKPT 1
#cmakedefine HAVE_TCGETATTR #cmakedefine HAVE_TCGETATTR

View file

@ -69,10 +69,9 @@ int main (int argc, char *argv[])
gid_t gid; gid_t gid;
uid_t uid; uid_t uid;
mode_t mod; mode_t mod;
char* tty; char* tty = 0;
int fd; int fd;
#if !defined(HAVE_PTSNAME) && defined(TIOCGPTN) #if defined(HAVE_PTSNAME_R)
int ptyno;
char ttyb[32]; char ttyb[32];
#endif #endif
@ -96,14 +95,14 @@ int main (int argc, char *argv[])
fd = atoi(argv[2]); fd = atoi(argv[2]);
/* get slave pty name from master pty file handle *********/ /* 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); tty = ptsname(fd);
if (!tty) if (!tty)
#elif defined(TIOCGPTN)
if (!ioctl(fd, TIOCGPTN, &ptyno)) {
sprintf(ttyb, "/dev/pts/%d", ptyno);
tty = ttyb;
} else
#endif #endif
{ {
/* Check that fd is a valid master pseudo terminal. */ /* Check that fd is a valid master pseudo terminal. */

View file

@ -190,8 +190,6 @@ bool KPty::open()
#else #else
#if defined(HAVE_PTSNAME) || defined(TIOCGPTN)
#ifdef HAVE_POSIX_OPENPT #ifdef HAVE_POSIX_OPENPT
d->masterFd = ::posix_openpt(O_RDWR|O_NOCTTY); d->masterFd = ::posix_openpt(O_RDWR|O_NOCTTY);
#elif defined(PTM_DEVICE) #elif defined(PTM_DEVICE)
@ -201,16 +199,15 @@ bool KPty::open()
#endif #endif
if (d->masterFd >= 0) 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); char *ptsn = ptsname(d->masterFd);
if (ptsn) { if (ptsn) {
d->ttyName = 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 #endif
#ifdef HAVE_GRANTPT #ifdef HAVE_GRANTPT
if (!grantpt(d->masterFd)) if (!grantpt(d->masterFd))
@ -222,7 +219,6 @@ bool KPty::open()
::close(d->masterFd); ::close(d->masterFd);
d->masterFd = -1; d->masterFd = -1;
} }
#endif // HAVE_PTSNAME || TIOCGPTN
// Linux device names, FIXME: Trouble on other systems? // Linux device names, FIXME: Trouble on other systems?
for (const char* s3 = "pqrstuvwxyzabcde"; *s3; s3++) for (const char* s3 = "pqrstuvwxyzabcde"; *s3; s3++)
@ -332,10 +328,6 @@ bool KPty::open()
bool KPty::open(int fd) 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); Q_D(KPty);
if (d->masterFd >= 0) { if (d->masterFd >= 0) {
@ -345,16 +337,15 @@ bool KPty::open(int fd)
d->ownMaster = false; 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); char *ptsn = ptsname(fd);
if (ptsn) { if (ptsn) {
d->ttyName = 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 # endif
} else { } else {
kWarning(175) << "Failed to determine pty slave device for fd" << fd; kWarning(175) << "Failed to determine pty slave device for fd" << fd;
@ -368,7 +359,6 @@ bool KPty::open(int fd)
} }
return true; return true;
#endif
} }
void KPty::closeSlave() void KPty::closeSlave()