summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOBATA Akio <obache@wizdas.com>2020-08-29 18:40:18 +0900
committerSlávek Banko <slavek.banko@axis.cz>2020-08-31 11:52:51 +0200
commitad371ca0b5f280e735898e3a329e49577dd5bfe8 (patch)
tree8b83fa66c2ce67849b7aeb24b2c776db4748226f
parentbde317d2cd53eac0e4274257f4e5e8f718e32fd6 (diff)
downloadtdelibs-ad371ca0b5f280e735898e3a329e49577dd5bfe8.tar.gz
tdelibs-ad371ca0b5f280e735898e3a329e49577dd5bfe8.zip
kpty: prevent to open pseudo ttys twice for openpty(3) platforms
Signed-off-by: OBATA Akio <obache@wizdas.com> (cherry picked from commit 8f7371cddfa402a5561f46007985832b32383728)
-rw-r--r--tdecore/kpty.cpp63
1 files changed, 38 insertions, 25 deletions
diff --git a/tdecore/kpty.cpp b/tdecore/kpty.cpp
index f832cd97e..a3ff34aec 100644
--- a/tdecore/kpty.cpp
+++ b/tdecore/kpty.cpp
@@ -217,49 +217,60 @@ bool KPty::setPty(int pty_master)
bool KPty::_attachPty(int pty_master)
{
- TQCString ptyName;
+ if (d->slaveFd < 0 ) {
kdDebug(175) << "KPty::_attachPty(): " << pty_master << endl;
-#if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
+#if defined(HAVE_PTSNAME)
char *ptsn = ptsname(d->masterFd);
if (ptsn) {
- grantpt(d->masterFd);
d->ttyName = ptsn;
} else {
::close(d->masterFd);
d->masterFd = -1;
+ return false;
}
#endif
- struct stat st;
- if (stat(d->ttyName.data(), &st))
- return false; // this just cannot happen ... *cough* Yeah right, I just
- // had it happen when pty #349 was allocated. I guess
- // there was some sort of leak? I only had a few open.
- if (((st.st_uid != getuid()) ||
- (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
- !chownpty(true))
- {
- kdWarning(175)
- << "KPty::_attachPty(): " << "chownpty failed for device " << ptyName << "::" << d->ttyName << endl
- << "KPty::_attachPty(): " << "This means the communication can be eavesdropped." << endl;
- }
+#if defined(HAVE_GRANTPT)
+ if (grantpt(d->masterFd)) {
+ return false;
+ }
+#else
+ struct stat st;
+ if (stat(d->ttyName.data(), &st))
+ return false; // this just cannot happen ... *cough* Yeah right, I just
+ // had it happen when pty #349 was allocated. I guess
+ // there was some sort of leak? I only had a few open.
+ if (((st.st_uid != getuid()) ||
+ (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
+ !chownpty(true))
+ {
+ kdWarning(175)
+ << "KPty::_attachPty(): " << "chownpty failed for device " << d->ttyName << endl
+ << "KPty::_attachPty(): " << "This means the communication can be eavesdropped." << endl;
+ }
+#endif
#ifdef BSD
- revoke(d->ttyName.data());
+ revoke(d->ttyName.data());
#endif
#ifdef HAVE_UNLOCKPT
- unlockpt(d->masterFd);
+ unlockpt(d->masterFd);
#endif
- d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
- if (d->slaveFd < 0)
- {
- kdWarning(175) << "KPty::_attachPty(): " << "Can't open slave pseudo teletype" << endl;
- ::close(d->masterFd);
- d->masterFd = -1;
- return false;
+ d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
+ if (d->slaveFd < 0)
+ {
+ kdWarning(175) << "KPty::_attachPty(): " << "Can't open slave pseudo teletype" << endl;
+ ::close(d->masterFd);
+ d->masterFd = -1;
+ return false;
+ }
+#ifdef HAVE_OPENPTY
+ // set screen size
+ ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
+#endif
}
#if (defined(__svr4__) || defined(__sgi__))
@@ -294,8 +305,10 @@ bool KPty::_attachPty(int pty_master)
_tcsetattr(d->slaveFd, &ttmode);
+#ifndef HAVE_OPENPTY
// set screen size
ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
+#endif
fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);