• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KDECore

kpty.cpp

Go to the documentation of this file.
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 #ifdef HAVE_LIBUTIL_H
00072 # include <libutil.h>
00073 # define USE_LOGIN
00074 #elif defined(HAVE_UTIL_H)
00075 # include <util.h>
00076 # define USE_LOGIN
00077 #endif
00078 
00079 #ifdef USE_LOGIN
00080 # include <utmp.h>
00081 #endif
00082 
00083 #ifdef HAVE_TERMIOS_H
00084 /* for HP-UX (some versions) the extern C is needed, and for other
00085    platforms it doesn't hurt */
00086 extern "C" {
00087 # include <termios.h>
00088 }
00089 #endif
00090 
00091 #if !defined(__osf__)
00092 # ifdef HAVE_TERMIO_H
00093 /* needed at least on AIX */
00094 #  include <termio.h>
00095 # endif
00096 #endif
00097 
00098 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
00099 # define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
00100 #else
00101 # if defined(_HPUX_SOURCE) || defined(__Lynx__) || defined (__CYGWIN__)
00102 #  define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
00103 # else
00104 #  define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
00105 # endif
00106 #endif
00107 
00108 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
00109 # define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
00110 #else
00111 # if defined(_HPUX_SOURCE) || defined(__CYGWIN__)
00112 #  define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode) 
00113 # else
00114 #  define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
00115 # endif
00116 #endif
00117 
00118 #if defined (_HPUX_SOURCE)
00119 # define _TERMIOS_INCLUDED
00120 # include <bsdtty.h>
00121 #endif
00122 
00123 #if defined(HAVE_PTY_H)
00124 # include <pty.h>
00125 #endif
00126 
00127 #include <kdebug.h>
00128 #include <kstandarddirs.h>  // locate
00129 
00130 #ifndef CINTR
00131 #define CINTR   0x03
00132 #endif
00133 #ifndef CQUIT
00134 #define CQUIT   0x1c
00135 #endif
00136 #ifndef CERASE
00137 #define CERASE  0x7f
00138 #endif
00139 
00140 #define TTY_GROUP "tty"
00141 
00143 // private functions //
00145 
00146 #ifdef HAVE_UTEMPTER
00147 class KProcess_Utmp : public KProcess
00148 {
00149 public:
00150    int commSetupDoneC()
00151    {
00152      dup2(cmdFd, 0);
00153      dup2(cmdFd, 1);
00154      dup2(cmdFd, 3);
00155      return 1;
00156    }
00157    int cmdFd;
00158 };
00159 #endif
00160 
00161 #define BASE_CHOWN "kgrantpty"
00162 
00163 
00164 
00166 // private data //
00168 
00169 struct KPtyPrivate {
00170    KPtyPrivate() :
00171      xonXoff(false),
00172      utf8(false),
00173      masterFd(-1), slaveFd(-1)
00174    {
00175      memset(&winSize, 0, sizeof(winSize));
00176      winSize.ws_row = 24;
00177      winSize.ws_col = 80;
00178    }
00179 
00180    bool xonXoff : 1;
00181    bool utf8    : 1;
00182    int masterFd;
00183    int slaveFd;
00184    struct winsize winSize;
00185 
00186    QCString ttyName;
00187 };
00188 
00190 // public member functions //
00192 
00193 KPty::KPty()
00194 {
00195   d = new KPtyPrivate;
00196 }
00197 
00198 KPty::~KPty()
00199 {
00200   close();
00201   delete d;
00202 }
00203 
00204 bool KPty::open()
00205 {
00206   if (d->masterFd >= 0)
00207     return true;
00208 
00209   QCString ptyName;
00210 
00211   // Find a master pty that we can open ////////////////////////////////
00212 
00213   // Because not all the pty animals are created equal, they want to
00214   // be opened by several different methods.
00215 
00216   // We try, as we know them, one by one.
00217 
00218 #if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
00219 #ifdef _AIX
00220   d->masterFd = ::open("/dev/ptc",O_RDWR);
00221 #else
00222   d->masterFd = ::open("/dev/ptmx",O_RDWR);
00223 #endif
00224   if (d->masterFd >= 0)
00225   {
00226     char *ptsn = ptsname(d->masterFd);
00227     if (ptsn) {
00228         grantpt(d->masterFd);
00229         d->ttyName = ptsn;
00230         goto gotpty;
00231     } else {
00232        ::close(d->masterFd);
00233        d->masterFd = -1;
00234     }
00235   }
00236 #endif
00237 
00238   // Linux device names, FIXME: Trouble on other systems?
00239   for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
00240   {
00241     for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
00242     {
00243       ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
00244       d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
00245 
00246       d->masterFd = ::open(ptyName.data(), O_RDWR);
00247       if (d->masterFd >= 0)
00248       {
00249 #ifdef __sun
00250         /* Need to check the process group of the pty.
00251          * If it exists, then the slave pty is in use,
00252          * and we need to get another one.
00253          */
00254         int pgrp_rtn;
00255         if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
00256           ::close(d->masterFd);
00257           d->masterFd = -1;
00258           continue;
00259         }
00260 #endif /* sun */
00261         if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits
00262         {
00263           if (!geteuid())
00264           {
00265             struct group* p = getgrnam(TTY_GROUP);
00266             if (!p)
00267               p = getgrnam("wheel");
00268             gid_t gid = p ? p->gr_gid : getgid ();
00269 
00270             chown(d->ttyName.data(), getuid(), gid);
00271             chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
00272           }
00273           goto gotpty;
00274         }
00275         ::close(d->masterFd);
00276         d->masterFd = -1;
00277       }
00278     }
00279   }
00280 
00281   kdWarning(175) << "Can't open a pseudo teletype" << endl;
00282   return false;
00283 
00284  gotpty:
00285   struct stat st;
00286   if (stat(d->ttyName.data(), &st))
00287     return false; // this just cannot happen ... *cough*  Yeah right, I just
00288                   // had it happen when pty #349 was allocated.  I guess
00289                   // there was some sort of leak?  I only had a few open.
00290   if (((st.st_uid != getuid()) ||
00291        (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
00292       !chownpty(true))
00293   {
00294     kdWarning(175)
00295       << "chownpty failed for device " << ptyName << "::" << d->ttyName
00296       << "\nThis means the communication can be eavesdropped." << endl;
00297   }
00298 
00299 #ifdef BSD
00300   revoke(d->ttyName.data());
00301 #endif
00302 
00303 #ifdef HAVE_UNLOCKPT
00304   unlockpt(d->masterFd);
00305 #endif
00306 
00307   d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
00308   if (d->slaveFd < 0)
00309   {
00310     kdWarning(175) << "Can't open slave pseudo teletype" << endl;
00311     ::close(d->masterFd);
00312     d->masterFd = -1;
00313     return false;
00314   }
00315 
00316 #if (defined(__svr4__) || defined(__sgi__))
00317   // Solaris
00318   ioctl(d->slaveFd, I_PUSH, "ptem");
00319   ioctl(d->slaveFd, I_PUSH, "ldterm");
00320 #endif
00321 
00322     // set xon/xoff & control keystrokes
00323   // without the '::' some version of HP-UX thinks, this declares
00324   // the struct in this class, in this method, and fails to find
00325   // the correct tc[gs]etattr
00326   struct ::termios ttmode;
00327 
00328   _tcgetattr(d->slaveFd, &ttmode);
00329 
00330   if (!d->xonXoff)
00331     ttmode.c_iflag &= ~(IXOFF | IXON);
00332   else
00333     ttmode.c_iflag |= (IXOFF | IXON);
00334 
00335 #ifdef IUTF8
00336   if (!d->utf8)
00337     ttmode.c_iflag &= ~IUTF8;
00338   else
00339     ttmode.c_iflag |= IUTF8;
00340 #endif
00341 
00342   ttmode.c_cc[VINTR] = CINTR;
00343   ttmode.c_cc[VQUIT] = CQUIT;
00344   ttmode.c_cc[VERASE] = CERASE;
00345 
00346   _tcsetattr(d->slaveFd, &ttmode);
00347 
00348   // set screen size
00349   ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
00350 
00351   fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
00352   fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
00353 
00354   return true;
00355 }
00356 
00357 void KPty::close()
00358 {
00359    if (d->masterFd < 0)
00360       return;
00361    // don't bother resetting unix98 pty, it will go away after closing master anyway.
00362    if (memcmp(d->ttyName.data(), "/dev/pts/", 9)) {
00363       if (!geteuid()) {
00364          struct stat st;
00365          if (!stat(d->ttyName.data(), &st)) {
00366             chown(d->ttyName.data(), 0, st.st_gid == getgid() ? 0 : -1);
00367             chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
00368          }
00369       } else {
00370          fcntl(d->masterFd, F_SETFD, 0);
00371          chownpty(false);
00372       }
00373    }
00374    ::close(d->slaveFd);
00375    ::close(d->masterFd);
00376    d->masterFd = d->slaveFd = -1;
00377 }
00378 
00379 void KPty::setCTty()
00380 {
00381     // Setup job control //////////////////////////////////
00382 
00383     // Become session leader, process group leader,
00384     // and get rid of the old controlling terminal.
00385     setsid();
00386 
00387     // make our slave pty the new controlling terminal.
00388 #ifdef TIOCSCTTY
00389     ioctl(d->slaveFd, TIOCSCTTY, 0);
00390 #else
00391     // SVR4 hack: the first tty opened after setsid() becomes controlling tty
00392     ::close(::open(d->ttyName, O_WRONLY, 0));
00393 #endif
00394 
00395     // make our new process group the foreground group on the pty
00396     int pgrp = getpid();
00397 #if defined(_POSIX_VERSION) || defined(__svr4__)
00398     tcsetpgrp (d->slaveFd, pgrp);
00399 #elif defined(TIOCSPGRP)
00400     ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
00401 #endif
00402 }
00403 
00404 void KPty::login(const char *user, const char *remotehost)
00405 {
00406 #ifdef HAVE_UTEMPTER
00407     KProcess_Utmp utmp;
00408     utmp.cmdFd = d->masterFd;
00409     utmp << "/usr/sbin/utempter" << "-a" << d->ttyName << "";
00410     utmp.start(KProcess::Block);
00411     Q_UNUSED(user);
00412     Q_UNUSED(remotehost);
00413 #elif defined(USE_LOGIN)
00414     const char *str_ptr;
00415     struct utmp l_struct;
00416     memset(&l_struct, 0, sizeof(struct utmp));
00417     // note: strncpy without terminators _is_ correct here. man 4 utmp
00418 
00419     if (user)
00420       strncpy(l_struct.ut_name, user, UT_NAMESIZE);
00421 
00422     if (remotehost)
00423       strncpy(l_struct.ut_host, remotehost, UT_HOSTSIZE);
00424 
00425 # ifndef __GLIBC__
00426     str_ptr = d->ttyName.data();
00427     if (!memcmp(str_ptr, "/dev/", 5))
00428         str_ptr += 5;
00429     strncpy(l_struct.ut_line, str_ptr, UT_LINESIZE);
00430 # endif
00431 
00432     // Handle 64-bit time_t properly, where it may be larger
00433     // than the integral type of ut_time.
00434     {
00435         time_t ut_time_temp;
00436         time(&ut_time_temp);
00437         l_struct.ut_time=ut_time_temp;
00438     }
00439 
00440     ::login(&l_struct);
00441 #else
00442     Q_UNUSED(user);
00443     Q_UNUSED(remotehost);
00444 #endif
00445 }
00446 
00447 void KPty::logout()
00448 {
00449 #ifdef HAVE_UTEMPTER
00450     KProcess_Utmp utmp;
00451     utmp.cmdFd = d->masterFd;
00452     utmp << "/usr/sbin/utempter" << "-d" << d->ttyName;
00453     utmp.start(KProcess::Block);
00454 #elif defined(USE_LOGIN)
00455     const char *str_ptr = d->ttyName.data();
00456     if (!memcmp(str_ptr, "/dev/", 5))
00457         str_ptr += 5;
00458 # ifdef __GLIBC__
00459     else {
00460         const char *sl_ptr = strrchr(str_ptr, '/');
00461         if (sl_ptr)
00462             str_ptr = sl_ptr + 1;
00463     }
00464 # endif
00465     ::logout(str_ptr);
00466 #endif
00467 }
00468 
00469 void KPty::setWinSize(int lines, int columns)
00470 {
00471   d->winSize.ws_row = (unsigned short)lines;
00472   d->winSize.ws_col = (unsigned short)columns;
00473   if (d->masterFd >= 0)
00474     ioctl( d->masterFd, TIOCSWINSZ, (char *)&d->winSize );
00475 }
00476 
00477 void KPty::setXonXoff(bool useXonXoff)
00478 {
00479   d->xonXoff = useXonXoff;
00480   if (d->masterFd >= 0) {
00481     // without the '::' some version of HP-UX thinks, this declares
00482     // the struct in this class, in this method, and fails to find
00483     // the correct tc[gs]etattr
00484     struct ::termios ttmode;
00485 
00486     _tcgetattr(d->masterFd, &ttmode);
00487 
00488     if (!useXonXoff)
00489       ttmode.c_iflag &= ~(IXOFF | IXON);
00490     else
00491       ttmode.c_iflag |= (IXOFF | IXON);
00492 
00493     _tcsetattr(d->masterFd, &ttmode);
00494   }
00495 }
00496 
00497 void KPty::setUtf8Mode(bool useUtf8)
00498 {
00499   d->utf8 = useUtf8;
00500 #ifdef IUTF8
00501   if (d->masterFd >= 0) {
00502     // without the '::' some version of HP-UX thinks, this declares
00503     // the struct in this class, in this method, and fails to find
00504     // the correct tc[gs]etattr
00505     struct ::termios ttmode;
00506 
00507     _tcgetattr(d->masterFd, &ttmode);
00508 
00509     if (!useUtf8)
00510       ttmode.c_iflag &= ~IUTF8;
00511     else
00512       ttmode.c_iflag |= IUTF8;
00513 
00514     _tcsetattr(d->masterFd, &ttmode);
00515   }
00516 #endif
00517 }
00518 
00519 const char *KPty::ttyName() const
00520 {
00521     return d->ttyName.data();
00522 }
00523 
00524 int KPty::masterFd() const
00525 {
00526     return d->masterFd;
00527 }
00528 
00529 int KPty::slaveFd() const
00530 {
00531     return d->slaveFd;
00532 }
00533 
00534 // private
00535 bool KPty::chownpty(bool grant)
00536 {
00537   KProcess proc;
00538   proc << locate("exe", BASE_CHOWN) << (grant?"--grant":"--revoke") << QString::number(d->masterFd);
00539   return proc.start(KProcess::Block) && proc.normalExit() && !proc.exitStatus();
00540 }
00541 

KDECore

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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal