• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdecore
 

kdecore

kpty.cpp
00001 /*
00002 
00003    This file is part of the KDE libraries
00004    Copyright (C) 1997-2002 The Konsole Developers
00005    Copyright (C) 2002 Waldo Bastian <bastian@kde.org>
00006    Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021    Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include <config.h>
00025 
00026 #include "kpty.h"
00027 #include "kprocess.h"
00028 
00029 #ifdef __sgi
00030 #define __svr4__
00031 #endif
00032 
00033 #ifdef __osf__
00034 #define _OSF_SOURCE
00035 #include <float.h>
00036 #endif
00037 
00038 #ifdef _AIX
00039 #define _ALL_SOURCE
00040 #endif
00041 
00042 // __USE_XOPEN isn't defined by default in ICC
00043 // (needed for ptsname(), grantpt() and unlockpt())
00044 #ifdef __INTEL_COMPILER
00045 #  ifndef __USE_XOPEN
00046 #    define __USE_XOPEN
00047 #  endif
00048 #endif
00049 
00050 #include <sys/types.h>
00051 #include <sys/ioctl.h>
00052 #include <sys/time.h>
00053 #include <sys/resource.h>
00054 #include <sys/stat.h>
00055 #include <sys/param.h>
00056 
00057 #ifdef HAVE_SYS_STROPTS_H
00058 # include <sys/stropts.h>   // Defines I_PUSH
00059 # define _NEW_TTY_CTRL
00060 #endif
00061 
00062 #include <errno.h>
00063 #include <fcntl.h>
00064 #include <time.h>
00065 #include <stdlib.h>
00066 #include <stdio.h>
00067 #include <string.h>
00068 #include <unistd.h>
00069 #include <grp.h>
00070 
00071 #if defined(HAVE_LIBUTIL_H)
00072 # include <libutil.h>
00073 # if (!defined(__FreeBSD__) || __FreeBSD_version < 900007)
00074 #   define USE_LOGIN
00075 # endif
00076 #endif
00077 #if defined(HAVE_UTIL_H)
00078 # include <util.h>
00079 # define USE_LOGIN
00080 #endif
00081 
00082 #ifdef USE_LOGIN
00083 # include <utmp.h>
00084 #endif
00085 
00086 #ifdef HAVE_TERMIOS_H
00087 /* for HP-UX (some versions) the extern C is needed, and for other
00088    platforms it doesn't hurt */
00089 extern "C" {
00090 # include <termios.h>
00091 }
00092 #endif
00093 
00094 #if !defined(__osf__)
00095 # ifdef HAVE_TERMIO_H
00096 /* needed at least on AIX */
00097 #  include <termio.h>
00098 # endif
00099 #endif
00100 
00101 #if defined(HAVE_TCGETATTR)
00102 # define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
00103 #elif defined(TIOCGETA)
00104 # define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
00105 #elif defined(TCGETS)
00106 # define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
00107 #else
00108 # error
00109 #endif
00110 
00111 #if defined(HAVE_TCSETATTR) && defined(TCSANOW)
00112 # define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
00113 #elif defined(TIOCSETA)
00114 # define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
00115 #elif defined(TCSETS)
00116 # define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
00117 #else
00118 # error
00119 #endif
00120 
00121 #if defined (_HPUX_SOURCE)
00122 # define _TERMIOS_INCLUDED
00123 # include <bsdtty.h>
00124 #endif
00125 
00126 #if defined(HAVE_PTY_H)
00127 # include <pty.h>
00128 #endif
00129 
00130 #include <kdebug.h>
00131 #include <kstandarddirs.h>  // locate
00132 
00133 #ifndef CINTR
00134 #define CINTR   0x03
00135 #endif
00136 #ifndef CQUIT
00137 #define CQUIT   0x1c
00138 #endif
00139 #ifndef CERASE
00140 #define CERASE  0x7f
00141 #endif
00142 
00143 #define TTY_GROUP "tty"
00144 
00146 // private functions //
00148 
00149 #ifdef HAVE_UTEMPTER
00150 class KProcess_Utmp : public KProcess
00151 {
00152 public:
00153    int commSetupDoneC()
00154    {
00155      dup2(cmdFd, 0);
00156      dup2(cmdFd, 1);
00157      dup2(cmdFd, 3);
00158      return 1;
00159    }
00160    int cmdFd;
00161 };
00162 #endif
00163 
00164 #define BASE_CHOWN "kgrantpty"
00165 
00166 
00167 
00169 // private data //
00171 
00172 struct KPtyPrivate {
00173    KPtyPrivate() :
00174      xonXoff(false),
00175      utf8(false),
00176      masterFd(-1), slaveFd(-1)
00177    {
00178      memset(&winSize, 0, sizeof(winSize));
00179      winSize.ws_row = 24;
00180      winSize.ws_col = 80;
00181    }
00182 
00183    bool xonXoff : 1;
00184    bool utf8    : 1;
00185    int masterFd;
00186    int slaveFd;
00187    struct winsize winSize;
00188 
00189    TQCString ttyName;
00190 };
00191 
00193 // public member functions //
00195 
00196 KPty::KPty()
00197 {
00198   d = new KPtyPrivate;
00199 }
00200 
00201 KPty::~KPty()
00202 {
00203   close();
00204   delete d;
00205 }
00206 
00207 bool KPty::setPty(int pty_master)
00208 {
00209    // a pty is already open
00210    if(d->masterFd >= 0) {
00211       kdWarning(175) << "KPty::setPty(): " << "d->masterFd >= 0" << endl;
00212       return false;
00213    }
00214    d->masterFd = pty_master;
00215    return _attachPty(pty_master);
00216 }
00217 
00218 bool KPty::_attachPty(int pty_master)
00219 {
00220   if (d->slaveFd  < 0 ) {
00221 
00222     kdDebug(175) << "KPty::_attachPty(): " << pty_master << endl;
00223 #if defined(HAVE_PTSNAME)
00224     char *ptsn = ptsname(d->masterFd);
00225     if (ptsn) {
00226         d->ttyName = ptsn;
00227     } else {
00228        ::close(d->masterFd);
00229        d->masterFd = -1;
00230        return false;
00231     }
00232 #endif
00233 
00234 #if defined(HAVE_GRANTPT)
00235     if (grantpt(d->masterFd)) {
00236       return false;
00237     }
00238 #else
00239     struct stat st;
00240     if (stat(d->ttyName.data(), &st))
00241       return false; // this just cannot happen ... *cough*  Yeah right, I just
00242                     // had it happen when pty #349 was allocated.  I guess
00243                     // there was some sort of leak?  I only had a few open.
00244     if (((st.st_uid != getuid()) ||
00245          (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
00246         !chownpty(true))
00247     {
00248       kdWarning(175)
00249         << "KPty::_attachPty(): " << "chownpty failed for device " << d->ttyName << endl
00250         << "KPty::_attachPty(): " << "This means the communication can be eavesdropped." << endl;
00251     }
00252 #endif
00253 
00254 #ifdef BSD
00255     revoke(d->ttyName.data());
00256 #endif
00257 
00258 #ifdef HAVE_UNLOCKPT
00259     unlockpt(d->masterFd);
00260 #endif
00261 
00262     d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
00263     if (d->slaveFd < 0)
00264     {
00265       kdWarning(175) << "KPty::_attachPty(): " << "Can't open slave pseudo teletype" << endl;
00266       ::close(d->masterFd);
00267       d->masterFd = -1;
00268       return false;
00269     }
00270 #ifdef HAVE_OPENPTY
00271     // set screen size
00272     ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
00273 #endif
00274   }
00275 
00276 #if (defined(__svr4__) || defined(__sgi__))
00277   // Solaris
00278   ioctl(d->slaveFd, I_PUSH, "ptem");
00279   ioctl(d->slaveFd, I_PUSH, "ldterm");
00280 #endif
00281 
00282     // set xon/xoff & control keystrokes
00283   // without the '::' some version of HP-UX thinks, this declares
00284   // the struct in this class, in this method, and fails to find
00285   // the correct tc[gs]etattr
00286   struct ::termios ttmode;
00287 
00288   _tcgetattr(d->slaveFd, &ttmode);
00289 
00290   if (!d->xonXoff)
00291     ttmode.c_iflag &= ~(IXOFF | IXON);
00292   else
00293     ttmode.c_iflag |= (IXOFF | IXON);
00294 
00295 #ifdef IUTF8
00296   if (!d->utf8)
00297     ttmode.c_iflag &= ~IUTF8;
00298   else
00299     ttmode.c_iflag |= IUTF8;
00300 #endif
00301 
00302   ttmode.c_cc[VINTR] = CINTR;
00303   ttmode.c_cc[VQUIT] = CQUIT;
00304   ttmode.c_cc[VERASE] = CERASE;
00305 
00306   _tcsetattr(d->slaveFd, &ttmode);
00307 
00308 #ifndef HAVE_OPENPTY
00309   // set screen size
00310   ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
00311 #endif
00312 
00313   fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
00314   fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
00315 
00316   return true;
00317 }
00318 
00319 bool KPty::open()
00320 {
00321   if (d->masterFd >= 0)
00322     return true;
00323 
00324 #if defined(HAVE_OPENPTY)
00325   char cpty[16];
00326 
00327   if (openpty(&d->masterFd, &d->slaveFd, cpty, NULL, &d->winSize) == 0) {
00328     d->ttyName = cpty;
00329   } else {
00330     kdWarning(175) << "Can't open slave pseudo teletype" << endl;
00331     return false;
00332   }
00333 #else
00334 
00335   TQCString ptyName;
00336 
00337   // Find a master pty that we can open ////////////////////////////////
00338 
00339   // Because not all the pty animals are created equal, they want to
00340   // be opened by several different methods.
00341 
00342   // We try, as we know them, one by one.
00343 
00344 #if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
00345 #if defined(HAVE_GETPT)
00346   d->masterFd = ::getpt();
00347 #elif defined(HAVE_POSIX_OPENPT)
00348   d->masterFd = ::posix_openpt(O_RDWR);
00349 #elif defined(_AIX)
00350   d->masterFd = ::open("/dev/ptc",O_RDWR);
00351 #else
00352   d->masterFd = ::open("/dev/ptmx",O_RDWR);
00353 #endif
00354   if (d->masterFd >= 0)
00355   {
00356     char *ptsn = ptsname(d->masterFd);
00357     if (ptsn) {
00358         grantpt(d->masterFd);
00359         d->ttyName = ptsn;
00360         goto gotpty;
00361     } else {
00362        ::close(d->masterFd);
00363        d->masterFd = -1;
00364     }
00365   }
00366 #endif
00367 
00368   // Linux device names, FIXME: Trouble on other systems?
00369   for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
00370   {
00371     for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
00372     {
00373       ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
00374       d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
00375 
00376       d->masterFd = ::open(ptyName.data(), O_RDWR);
00377       if (d->masterFd >= 0)
00378       {
00379 #ifdef __sun
00380         /* Need to check the process group of the pty.
00381          * If it exists, then the slave pty is in use,
00382          * and we need to get another one.
00383          */
00384         int pgrp_rtn;
00385         if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
00386           ::close(d->masterFd);
00387           d->masterFd = -1;
00388           continue;
00389         }
00390 #endif /* sun */
00391         if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits
00392         {
00393           if (!geteuid())
00394           {
00395             struct group* p = getgrnam(TTY_GROUP);
00396             if (!p)
00397               p = getgrnam("wheel");
00398             gid_t gid = p ? p->gr_gid : getgid ();
00399 
00400             chown(d->ttyName.data(), getuid(), gid);
00401             chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
00402           }
00403           goto gotpty;
00404         }
00405         ::close(d->masterFd);
00406         d->masterFd = -1;
00407       }
00408     }
00409   }
00410 
00411   kdWarning(175) << "KPty::open(): " << "Can't open a pseudo teletype" << endl;
00412   return false;
00413 #endif
00414 
00415  gotpty:
00416   return  _attachPty(d->masterFd);
00417 
00418   return true;
00419 }
00420 
00421 void KPty::close()
00422 {
00423    if (d->masterFd < 0)
00424       return;
00425    // don't bother resetting unix98 pty, it will go away after closing master anyway.
00426    if (memcmp(d->ttyName.data(), "/dev/pts/", 9)) {
00427       if (!geteuid()) {
00428          struct stat st;
00429          if (!stat(d->ttyName.data(), &st)) {
00430             chown(d->ttyName.data(), 0, st.st_gid == getgid() ? 0 : -1);
00431             chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
00432          }
00433       } else {
00434          fcntl(d->masterFd, F_SETFD, 0);
00435          chownpty(false);
00436       }
00437    }
00438    ::close(d->slaveFd);
00439    ::close(d->masterFd);
00440    d->masterFd = d->slaveFd = -1;
00441 }
00442 
00443 void KPty::setCTty()
00444 {
00445     // Setup job control //////////////////////////////////
00446 
00447     // Become session leader, process group leader,
00448     // and get rid of the old controlling terminal.
00449     setsid();
00450 
00451     // make our slave pty the new controlling terminal.
00452 #ifdef TIOCSCTTY
00453     ioctl(d->slaveFd, TIOCSCTTY, 0);
00454 #else
00455     // SVR4 hack: the first tty opened after setsid() becomes controlling tty
00456     ::close(::open(d->ttyName, O_WRONLY, 0));
00457 #endif
00458 
00459     // make our new process group the foreground group on the pty
00460     int pgrp = getpid();
00461 #if defined(_POSIX_VERSION) || defined(__svr4__)
00462     tcsetpgrp (d->slaveFd, pgrp);
00463 #elif defined(TIOCSPGRP)
00464     ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
00465 #endif
00466 }
00467 
00468 void KPty::login(const char *user, const char *remotehost)
00469 {
00470 #ifdef HAVE_UTEMPTER
00471     KProcess_Utmp utmp;
00472     utmp.cmdFd = d->masterFd;
00473     utmp << UTEMPTER_HELPER << "add";
00474     if (remotehost)
00475       utmp << remotehost;
00476     utmp.start(KProcess::Block);
00477     Q_UNUSED(user);
00478     Q_UNUSED(remotehost);
00479 #elif defined(USE_LOGIN)
00480     const char *str_ptr;
00481     struct utmp l_struct;
00482     memset(&l_struct, 0, sizeof(struct utmp));
00483     // note: strncpy without terminators _is_ correct here. man 4 utmp
00484 
00485     if (user)
00486       strncpy(l_struct.ut_name, user, UT_NAMESIZE);
00487 
00488     if (remotehost)
00489       strncpy(l_struct.ut_host, remotehost, UT_HOSTSIZE);
00490 
00491 # ifndef __GLIBC__
00492     str_ptr = d->ttyName.data();
00493     if (!memcmp(str_ptr, "/dev/", 5))
00494         str_ptr += 5;
00495     strncpy(l_struct.ut_line, str_ptr, UT_LINESIZE);
00496 # endif
00497 
00498     // Handle 64-bit time_t properly, where it may be larger
00499     // than the integral type of ut_time.
00500     {
00501         time_t ut_time_temp;
00502         time(&ut_time_temp);
00503         l_struct.ut_time=ut_time_temp;
00504     }
00505 
00506     ::login(&l_struct);
00507 #else
00508     Q_UNUSED(user);
00509     Q_UNUSED(remotehost);
00510 #endif
00511 }
00512 
00513 void KPty::logout()
00514 {
00515 #ifdef HAVE_UTEMPTER
00516     KProcess_Utmp utmp;
00517     utmp.cmdFd = d->masterFd;
00518     utmp << UTEMPTER_HELPER << "del";
00519     utmp.start(KProcess::Block);
00520 #elif defined(USE_LOGIN)
00521     const char *str_ptr = d->ttyName.data();
00522     if (!memcmp(str_ptr, "/dev/", 5))
00523         str_ptr += 5;
00524 # ifdef __GLIBC__
00525     else {
00526         const char *sl_ptr = strrchr(str_ptr, '/');
00527         if (sl_ptr)
00528             str_ptr = sl_ptr + 1;
00529     }
00530 # endif
00531     ::logout(str_ptr);
00532 #endif
00533 }
00534 
00535 void KPty::setWinSize(int lines, int columns)
00536 {
00537   d->winSize.ws_row = (unsigned short)lines;
00538   d->winSize.ws_col = (unsigned short)columns;
00539   if (d->masterFd >= 0)
00540     ioctl( d->masterFd, TIOCSWINSZ, (char *)&d->winSize );
00541 }
00542 
00543 void KPty::setXonXoff(bool useXonXoff)
00544 {
00545   d->xonXoff = useXonXoff;
00546   if (d->masterFd >= 0) {
00547     // without the '::' some version of HP-UX thinks, this declares
00548     // the struct in this class, in this method, and fails to find
00549     // the correct tc[gs]etattr
00550     struct ::termios ttmode;
00551 
00552     _tcgetattr(d->masterFd, &ttmode);
00553 
00554     if (!useXonXoff)
00555       ttmode.c_iflag &= ~(IXOFF | IXON);
00556     else
00557       ttmode.c_iflag |= (IXOFF | IXON);
00558 
00559     _tcsetattr(d->masterFd, &ttmode);
00560   }
00561 }
00562 
00563 void KPty::setUtf8Mode(bool useUtf8)
00564 {
00565   d->utf8 = useUtf8;
00566 #ifdef IUTF8
00567   if (d->masterFd >= 0) {
00568     // without the '::' some version of HP-UX thinks, this declares
00569     // the struct in this class, in this method, and fails to find
00570     // the correct tc[gs]etattr
00571     struct ::termios ttmode;
00572 
00573     _tcgetattr(d->masterFd, &ttmode);
00574 
00575     if (!useUtf8)
00576       ttmode.c_iflag &= ~IUTF8;
00577     else
00578       ttmode.c_iflag |= IUTF8;
00579 
00580     _tcsetattr(d->masterFd, &ttmode);
00581   }
00582 #endif
00583 }
00584 
00585 const char *KPty::ttyName() const
00586 {
00587     return d->ttyName.data();
00588 }
00589 
00590 int KPty::masterFd() const
00591 {
00592     return d->masterFd;
00593 }
00594 
00595 int KPty::slaveFd() const
00596 {
00597     return d->slaveFd;
00598 }
00599 
00600 // private
00601 bool KPty::chownpty(bool grant)
00602 {
00603 #if !defined(__OpenBSD__) && !defined(__FreeBSD__)
00604   KProcess proc;
00605   proc << locate("exe", BASE_CHOWN) << (grant?"--grant":"--revoke") << TQString::number(d->masterFd);
00606   return proc.start(KProcess::Block) && proc.normalExit() && !proc.exitStatus();
00607 #endif
00608 }
00609 

kdecore

Skip menu "kdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.7.6.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |