process.cpp

00001 /* vi: ts=8 sts=4 sw=4
00002  *
00003  * $Id: process.cpp 438855 2005-07-26 10:22:29Z mlaurent $
00004  *
00005  * This file is part of the KDE project, module kdesu.
00006  * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
00007  * 
00008  * This file contains code from TEShell.C of the KDE konsole. 
00009  * Copyright (c) 1997,1998 by Lars Doelle <lars.doelle@on-line.de> 
00010  *
00011  * This is free software; you can use this library under the GNU Library 
00012  * General Public License, version 2. See the file "COPYING.LIB" for the 
00013  * exact licensing terms.
00014  *
00015  * process.cpp: Functionality to build a front end to password asking
00016  *  terminal programs.
00017  */
00018 
00019 #include <config.h>
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #include <fcntl.h>
00025 #include <signal.h>
00026 #include <errno.h>
00027 #include <string.h>
00028 #include <termios.h>
00029 #include <signal.h>
00030 
00031 #include <sys/types.h>
00032 #include <sys/wait.h>
00033 #include <sys/stat.h>
00034 #include <sys/time.h>
00035 #include <sys/resource.h>
00036 #include <sys/ioctl.h>
00037 
00038 #if defined(__SVR4) && defined(sun)
00039 #include <stropts.h>
00040 #include <sys/stream.h>
00041 #endif
00042 
00043 #ifdef HAVE_SYS_SELECT_H
00044 #include <sys/select.h>                // Needed on some systems.
00045 #endif
00046 
00047 #include <qglobal.h>
00048 #include <qcstring.h>
00049 #include <qfile.h>
00050 
00051 #include <kdebug.h>
00052 #include <kstandarddirs.h>
00053 
00054 #include "process.h"
00055 #include "kdesu_pty.h"
00056 #include "kcookie.h"
00057 
00058 int PtyProcess::waitMS(int fd,int ms)
00059 {
00060     struct timeval tv;
00061     tv.tv_sec = 0; 
00062     tv.tv_usec = 1000*ms;
00063 
00064     fd_set fds;
00065     FD_ZERO(&fds);
00066     FD_SET(fd,&fds);
00067     return select(fd+1, &fds, 0L, 0L, &tv);
00068 }
00069 
00070 /*
00071 ** Basic check for the existence of @p pid.
00072 ** Returns true iff @p pid is an extant process.
00073 */
00074 bool PtyProcess::checkPid(pid_t pid)
00075 {
00076     return kill(pid,0) == 0;
00077 }
00078 
00079 /*
00080 ** Check process exit status for process @p pid.
00081 ** On error (no child, no exit), return Error (-1).
00082 ** If child @p pid has exited, return its exit status,
00083 ** (which may be zero).
00084 ** If child @p has not exited, return NotExited (-2).
00085 */
00086 
00087 int PtyProcess::checkPidExited(pid_t pid)
00088 {
00089     int state, ret;
00090     ret = waitpid(pid, &state, WNOHANG);
00091 
00092     if (ret < 0) 
00093     {
00094         kdError(900) << k_lineinfo << "waitpid(): " << perror << "\n";
00095         return Error;
00096     }
00097     if (ret == pid) 
00098     {
00099         if (WIFEXITED(state))
00100             return WEXITSTATUS(state);
00101         return Killed;
00102     }
00103 
00104     return NotExited;
00105 }
00106 
00107 
00108 class PtyProcess::PtyProcessPrivate
00109 {
00110 public:
00111     QCStringList env;
00112 };
00113 
00114 
00115 PtyProcess::PtyProcess()
00116 {
00117     m_bTerminal = false;
00118     m_bErase = false;
00119     m_pPTY = 0L;
00120     d = new PtyProcessPrivate;
00121 }
00122 
00123 
00124 int PtyProcess::init()
00125 {
00126     delete m_pPTY;
00127     m_pPTY = new PTY();
00128     m_Fd = m_pPTY->getpt();
00129     if (m_Fd < 0)
00130         return -1;
00131     if ((m_pPTY->grantpt() < 0) || (m_pPTY->unlockpt() < 0)) 
00132     {
00133         kdError(900) << k_lineinfo << "Master setup failed.\n";
00134         m_Fd = -1;
00135         return -1;
00136     }
00137     m_TTY = m_pPTY->ptsname();
00138     m_Inbuf.resize(0);
00139     return 0;
00140 }
00141 
00142 
00143 PtyProcess::~PtyProcess()
00144 {
00145     delete m_pPTY;
00146     delete d;
00147 }
00148 
00150 void PtyProcess::setEnvironment( const QCStringList &env )
00151 {
00152     d->env = env;
00153 }
00154 
00155 const QCStringList& PtyProcess::environment() const
00156 {
00157     return d->env;
00158 }
00159 
00160 /*
00161  * Read one line of input. The terminal is in canonical mode, so you always
00162  * read a line at at time, but it's possible to receive multiple lines in
00163  * one time.
00164  */
00165 
00166 QCString PtyProcess::readLine(bool block)
00167 {
00168     int pos;
00169     QCString ret;
00170 
00171     if (!m_Inbuf.isEmpty()) 
00172     {
00173         pos = m_Inbuf.find('\n');
00174         if (pos == -1) 
00175         {
00176             ret = m_Inbuf;
00177             m_Inbuf.resize(0);
00178         } else
00179         {
00180             ret = m_Inbuf.left(pos);
00181             m_Inbuf = m_Inbuf.mid(pos+1);
00182         }
00183         return ret;
00184     }
00185 
00186     int flags = fcntl(m_Fd, F_GETFL);
00187     if (flags < 0) 
00188     {
00189         kdError(900) << k_lineinfo << "fcntl(F_GETFL): " << perror << "\n";
00190         return ret;
00191     }
00192     int oflags = flags;
00193     if (block)
00194         flags &= ~O_NONBLOCK;
00195     else
00196         flags |= O_NONBLOCK;
00197 
00198     if ((flags != oflags) && (fcntl(m_Fd, F_SETFL, flags) < 0))
00199     {
00200        // We get an error here when the child process has closed 
00201        // the file descriptor already.
00202        return ret;
00203     }
00204 
00205     int nbytes;
00206     char buf[256];
00207     while (1) 
00208     {
00209         nbytes = read(m_Fd, buf, 255);
00210         if (nbytes == -1) 
00211         {
00212             if (errno == EINTR)
00213                 continue;
00214             else break;
00215         }
00216         if (nbytes == 0)
00217             break;        // eof
00218 
00219         buf[nbytes] = '\000';
00220         m_Inbuf += buf;
00221 
00222         pos = m_Inbuf.find('\n');
00223         if (pos == -1) 
00224         {
00225             ret = m_Inbuf;
00226             m_Inbuf.resize(0);
00227         } else 
00228         {
00229             ret = m_Inbuf.left(pos);
00230             m_Inbuf = m_Inbuf.mid(pos+1);
00231         }
00232         break;
00233     }
00234 
00235     return ret;
00236 }
00237 
00238 
00239 void PtyProcess::writeLine(const QCString &line, bool addnl)
00240 {
00241     if (!line.isEmpty())
00242         write(m_Fd, line, line.length());
00243     if (addnl)
00244         write(m_Fd, "\n", 1);
00245 }
00246 
00247 
00248 void PtyProcess::unreadLine(const QCString &line, bool addnl)
00249 {
00250     QCString tmp = line;
00251     if (addnl)
00252         tmp += '\n';
00253     if (!tmp.isEmpty())
00254         m_Inbuf.prepend(tmp);
00255 }
00256 
00257 /*
00258  * Fork and execute the command. This returns in the parent.
00259  */
00260 
00261 int PtyProcess::exec(const QCString &command, const QCStringList &args)
00262 {
00263     kdDebug(900) << k_lineinfo << "Running `" << command << "'\n";
00264 
00265     if (init() < 0)
00266         return -1;
00267 
00268     // Open the pty slave before forking. See SetupTTY()
00269     int slave = open(m_TTY, O_RDWR);
00270     if (slave < 0) 
00271     {
00272         kdError(900) << k_lineinfo << "Could not open slave pty.\n";
00273         return -1;
00274     } 
00275 
00276     if ((m_Pid = fork()) == -1) 
00277     {
00278         kdError(900) << k_lineinfo << "fork(): " << perror << "\n";
00279         return -1;
00280     } 
00281 
00282     // Parent
00283     if (m_Pid) 
00284     {
00285         close(slave);
00286         return 0;
00287     }
00288 
00289     // Child
00290     if (SetupTTY(slave) < 0)
00291         _exit(1);
00292 
00293     for(QCStringList::ConstIterator it = d->env.begin();
00294         it != d->env.end(); it++)
00295     {
00296         putenv((*it).data());
00297     }
00298     unsetenv("KDE_FULL_SESSION");
00299 
00300     // From now on, terminal output goes through the tty.
00301 
00302     QCString path;
00303     if (command.contains('/'))
00304         path = command;
00305     else 
00306     {
00307         QString file = KStandardDirs::findExe(command);
00308         if (file.isEmpty()) 
00309         {
00310             kdError(900) << k_lineinfo << command << " not found\n"; 
00311             _exit(1);
00312         } 
00313         path = QFile::encodeName(file);
00314     }
00315 
00316     const char **argp = (const char **)malloc((args.count()+2)*sizeof(char *));
00317     int i = 0;
00318     argp[i++] = path;
00319     for (QCStringList::ConstIterator it=args.begin(); it!=args.end(); ++it)
00320         argp[i++] = *it;
00321 
00322     argp[i] = 0L;
00323         
00324     execv(path, (char * const *)argp);
00325     kdError(900) << k_lineinfo << "execv(\"" << path << "\"): " << perror << "\n";
00326     _exit(1);
00327     return -1; // Shut up compiler. Never reached.
00328 }
00329 
00330 
00331 /*
00332  * Wait until the terminal is set into no echo mode. At least one su 
00333  * (RH6 w/ Linux-PAM patches) sets noecho mode AFTER writing the Password: 
00334  * prompt, using TCSAFLUSH. This flushes the terminal I/O queues, possibly 
00335  * taking the password  with it. So we wait until no echo mode is set 
00336  * before writing the password.
00337  * Note that this is done on the slave fd. While Linux allows tcgetattr() on
00338  * the master side, Solaris doesn't.
00339  */
00340 
00341 int PtyProcess::WaitSlave()
00342 {
00343     int slave = open(m_TTY, O_RDWR);
00344     if (slave < 0) 
00345     {
00346         kdError(900) << k_lineinfo << "Could not open slave tty.\n";
00347         return -1;
00348     }
00349 
00350     kdDebug(900) << k_lineinfo << "Child pid " << m_Pid << endl;
00351     
00352     struct termios tio;
00353     while (1) 
00354     {
00355     if (!checkPid(m_Pid))
00356     {
00357         close(slave);
00358         return -1;
00359     }
00360         if (tcgetattr(slave, &tio) < 0) 
00361         {
00362             kdError(900) << k_lineinfo << "tcgetattr(): " << perror << "\n";
00363             close(slave);
00364             return -1;
00365         }
00366         if (tio.c_lflag & ECHO) 
00367         {
00368             kdDebug(900) << k_lineinfo << "Echo mode still on.\n";
00369         waitMS(slave,100);
00370             continue;
00371         }
00372         break;
00373     }
00374     close(slave);
00375     return 0;
00376 }
00377 
00378 
00379 int PtyProcess::enableLocalEcho(bool enable)
00380 {
00381     int slave = open(m_TTY, O_RDWR);
00382     if (slave < 0) 
00383     {
00384         kdError(900) << k_lineinfo << "Could not open slave tty.\n";
00385         return -1;
00386     }
00387     struct termios tio;
00388     if (tcgetattr(slave, &tio) < 0) 
00389     {
00390         kdError(900) << k_lineinfo << "tcgetattr(): " << perror << "\n";
00391         close(slave); return -1;
00392     }
00393     if (enable)
00394         tio.c_lflag |= ECHO;
00395     else
00396         tio.c_lflag &= ~ECHO;
00397     if (tcsetattr(slave, TCSANOW, &tio) < 0) 
00398     {
00399         kdError(900) << k_lineinfo << "tcsetattr(): " << perror << "\n";
00400         close(slave); return -1;
00401     }
00402     close(slave);
00403     return 0;
00404 }
00405 
00406 
00407 /*
00408  * Copy output to stdout until the child process exists, or a line of output
00409  * matches `m_Exit'.
00410  * We have to use waitpid() to test for exit. Merely waiting for EOF on the
00411  * pty does not work, because the target process may have children still
00412  * attached to the terminal.
00413  */
00414 
00415 int PtyProcess::waitForChild()
00416 {
00417     int retval = 1;
00418 
00419     fd_set fds;
00420     FD_ZERO(&fds);
00421 
00422     while (1) 
00423     {
00424         FD_SET(m_Fd, &fds);
00425         int ret = select(m_Fd+1, &fds, 0L, 0L, 0L);
00426         if (ret == -1) 
00427         {
00428             if (errno != EINTR) 
00429             {
00430                 kdError(900) << k_lineinfo << "select(): " << perror << "\n";
00431                 return -1;
00432             }
00433             ret = 0;
00434         }
00435 
00436         if (ret) 
00437         {
00438             QCString line = readLine(false);
00439             while (!line.isNull()) 
00440             {
00441                 if (!m_Exit.isEmpty() && !qstrnicmp(line, m_Exit, m_Exit.length()))
00442                     kill(m_Pid, SIGTERM);
00443                 if (m_bTerminal) 
00444                 {
00445                     fputs(line, stdout);
00446                     fputc('\n', stdout);
00447                 }
00448                 line = readLine(false);
00449             }
00450         }
00451 
00452     ret = checkPidExited(m_Pid);
00453     if (ret == Error)
00454     {
00455         if (errno == ECHILD) retval = 0;
00456         else retval = 1;
00457         break;
00458     }
00459     else if (ret == Killed)
00460     {
00461         retval = 0;
00462         break;
00463     }
00464     else if (ret == NotExited)
00465     {
00466         // keep checking
00467     }
00468     else
00469     {
00470         retval = ret;
00471         break;
00472     }
00473     }
00474     return retval;
00475 }
00476    
00477 /*
00478  * SetupTTY: Creates a new session. The filedescriptor "fd" should be
00479  * connected to the tty. It is closed after the tty is reopened to make it
00480  * our controlling terminal. This way the tty is always opened at least once
00481  * so we'll never get EIO when reading from it.
00482  */
00483 
00484 int PtyProcess::SetupTTY(int fd)
00485 {    
00486     // Reset signal handlers
00487     for (int sig = 1; sig < NSIG; sig++)
00488         signal(sig, SIG_DFL);
00489     signal(SIGHUP, SIG_IGN);
00490 
00491     // Close all file handles
00492     struct rlimit rlp;
00493     getrlimit(RLIMIT_NOFILE, &rlp);
00494     for (int i = 0; i < (int)rlp.rlim_cur; i++)
00495         if (i != fd) close(i); 
00496 
00497     // Create a new session.
00498     setsid();
00499 
00500     // Open slave. This will make it our controlling terminal
00501     int slave = open(m_TTY, O_RDWR);
00502     if (slave < 0) 
00503     {
00504         kdError(900) << k_lineinfo << "Could not open slave side: " << perror << "\n";
00505         return -1;
00506     }
00507     close(fd);
00508 
00509 #if defined(__SVR4) && defined(sun)
00510 
00511     // Solaris STREAMS environment.
00512     // Push these modules to make the stream look like a terminal.
00513     ioctl(slave, I_PUSH, "ptem");
00514     ioctl(slave, I_PUSH, "ldterm");
00515 
00516 #endif
00517 
00518 #ifdef TIOCSCTTY
00519     ioctl(slave, TIOCSCTTY, NULL);
00520 #endif
00521 
00522     // Connect stdin, stdout and stderr
00523     dup2(slave, 0); dup2(slave, 1); dup2(slave, 2);
00524     if (slave > 2) 
00525         close(slave);
00526 
00527     // Disable OPOST processing. Otherwise, '\n' are (on Linux at least)
00528     // translated to '\r\n'.
00529     struct termios tio;
00530     if (tcgetattr(0, &tio) < 0) 
00531     {
00532         kdError(900) << k_lineinfo << "tcgetattr(): " << perror << "\n";
00533         return -1;
00534     }
00535     tio.c_oflag &= ~OPOST;
00536     if (tcsetattr(0, TCSANOW, &tio) < 0) 
00537     {
00538         kdError(900) << k_lineinfo << "tcsetattr(): " << perror << "\n";
00539         return -1;
00540     }
00541 
00542     return 0;
00543 }
00544 
00545 void PtyProcess::virtual_hook( int, void* )
00546 { /*BASE::virtual_hook( id, data );*/ }
KDE Home | KDE Accessibility Home | Description of Access Keys