kdecore Library API Documentation

kprocctrl.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1997 Christian Czezakte (e9025461@student.tuwien.ac.at)
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kprocess.h"
00021 #include "kprocctrl.h"
00022 
00023 #include <config.h>
00024 
00025 #include <sys/time.h>
00026 #include <sys/types.h>
00027 #include <sys/wait.h>
00028 #include <unistd.h>
00029 #include <errno.h>
00030 #include <fcntl.h>
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 
00034 #include <qsocketnotifier.h>
00035 
00036 KProcessController *KProcessController::theKProcessController;
00037 int KProcessController::refCount;
00038 
00039 void KProcessController::ref()
00040 {
00041   if( !refCount ) {
00042     theKProcessController = new KProcessController;
00043     setupHandlers();
00044   }
00045   refCount++;
00046 }
00047 
00048 void KProcessController::deref()
00049 {
00050   refCount--;
00051   if( !refCount ) {
00052     resetHandlers();
00053     delete theKProcessController;
00054     theKProcessController = 0;
00055   }
00056 }
00057 
00058 KProcessController::KProcessController()
00059   : needcheck( false )
00060 {
00061   if( pipe( fd ) )
00062   {
00063     perror( "pipe" );
00064     abort();
00065   }
00066 
00067   fcntl( fd[0], F_SETFL, O_NONBLOCK ); // in case slotDoHousekeeping is called without polling first
00068   fcntl( fd[1], F_SETFL, O_NONBLOCK ); // in case it fills up
00069   fcntl( fd[0], F_SETFD, FD_CLOEXEC );
00070   fcntl( fd[1], F_SETFD, FD_CLOEXEC );
00071 
00072   notifier = new QSocketNotifier( fd[0], QSocketNotifier::Read );
00073   notifier->setEnabled( true );
00074   QObject::connect( notifier, SIGNAL(activated(int)),
00075                     SLOT(slotDoHousekeeping()));
00076 }
00077 
00078 KProcessController::~KProcessController()
00079 {
00080   delete notifier;
00081 
00082   close( fd[0] );
00083   close( fd[1] );
00084 }
00085 
00086 
00087 extern "C" {
00088 static void theReaper( int num )
00089 {
00090   KProcessController::theSigCHLDHandler( num );
00091 }
00092 }
00093 
00094 struct sigaction KProcessController::oldChildHandlerData;
00095 bool KProcessController::handlerSet = false;
00096 
00097 void KProcessController::setupHandlers()
00098 {
00099   if( handlerSet )
00100       return;
00101   handlerSet = true;
00102 
00103   struct sigaction act;
00104   sigemptyset( &act.sa_mask );
00105 
00106   act.sa_handler = SIG_IGN;
00107   act.sa_flags = 0;
00108   sigaction( SIGPIPE, &act, 0L );
00109 
00110   act.sa_handler = theReaper;
00111   act.sa_flags = SA_NOCLDSTOP;
00112   // CC: take care of SunOS which automatically restarts interrupted system
00113   // calls (and thus does not have SA_RESTART)
00114 #ifdef SA_RESTART
00115   act.sa_flags |= SA_RESTART;
00116 #endif
00117   sigaction( SIGCHLD, &act, &oldChildHandlerData );
00118 
00119   sigaddset( &act.sa_mask, SIGCHLD );
00120   // Make sure we don't block this signal. gdb tends to do that :-(
00121   sigprocmask( SIG_UNBLOCK, &act.sa_mask, 0 );
00122 }
00123 
00124 void KProcessController::resetHandlers()
00125 {
00126   if( !handlerSet )
00127       return;
00128   handlerSet = false;
00129 
00130   sigaction( SIGCHLD, &oldChildHandlerData, 0 );
00131   // there should be no problem with SIGPIPE staying SIG_IGN
00132 }
00133 
00134 // the pipe is needed to sync the child reaping with our event processing,
00135 // as otherwise there are race conditions, locking requirements, and things
00136 // generally get harder
00137 void KProcessController::theSigCHLDHandler( int arg )
00138 {
00139   int saved_errno = errno;
00140 
00141   char dummy = 0;
00142   ::write( theKProcessController->fd[1], &dummy, 1 );
00143 
00144   if( oldChildHandlerData.sa_handler != SIG_IGN &&
00145       oldChildHandlerData.sa_handler != SIG_DFL )
00146      oldChildHandlerData.sa_handler( arg ); // call the old handler
00147 
00148   errno = saved_errno;
00149 }
00150 
00151 int KProcessController::notifierFd() const
00152 {
00153   return fd[0];
00154 }
00155 
00156 void KProcessController::unscheduleCheck()
00157 {
00158   char dummy[16]; // somewhat bigger - just in case several have queued up
00159   if( ::read( fd[0], dummy, sizeof(dummy) ) > 0 )
00160     needcheck = true;
00161 }
00162 
00163 void
00164 KProcessController::rescheduleCheck()
00165 {
00166   if( needcheck )
00167   {
00168     needcheck = false;
00169     char dummy = 0;
00170     ::write( fd[1], &dummy, 1 );
00171   }
00172 }
00173 
00174 void KProcessController::slotDoHousekeeping()
00175 {
00176   char dummy[16]; // somewhat bigger - just in case several have queued up
00177   ::read( fd[0], dummy, sizeof(dummy) );
00178 
00179   int status;
00180  again:
00181   QValueListIterator<KProcess*> it( kProcessList.begin() );
00182   QValueListIterator<KProcess*> eit( kProcessList.end() );
00183   while( it != eit )
00184   {
00185     KProcess *prc = *it;
00186     if( prc->runs && waitpid( prc->pid_, &status, WNOHANG ) > 0 )
00187     {
00188       prc->processHasExited( status );
00189       // the callback can nuke the whole process list and even 'this'
00190       if (!theKProcessController)
00191         return;
00192       goto again;
00193     }
00194     ++it;
00195   }
00196   QValueListIterator<int> uit( unixProcessList.begin() );
00197   QValueListIterator<int> ueit( unixProcessList.end() );
00198   while( uit != ueit )
00199   {
00200     if( waitpid( *uit, 0, WNOHANG ) > 0 )
00201     {
00202       uit = unixProcessList.remove( uit );
00203       deref(); // counterpart to addProcess, can invalidate 'this'
00204     } else
00205       ++uit;
00206   }
00207 }
00208 
00209 bool KProcessController::waitForProcessExit( int timeout )
00210 {
00211   for(;;)
00212   {
00213     struct timeval tv, *tvp;
00214     if (timeout < 0)
00215       tvp = 0;
00216     else
00217     {
00218       tv.tv_sec = timeout;
00219       tv.tv_usec = 0;
00220       tvp = &tv;
00221     }
00222 
00223     fd_set fds;
00224     FD_ZERO( &fds );
00225     FD_SET( fd[0], &fds );
00226 
00227     switch( select( fd[0]+1, &fds, 0, 0, tvp ) )
00228     {
00229     case -1:
00230       if( errno == EINTR )
00231         continue;
00232       // fall through; should never happen
00233     case 0:
00234       return false;
00235     default:
00236       slotDoHousekeeping();
00237       return true;
00238     }
00239   }
00240 }
00241 
00242 void KProcessController::addKProcess( KProcess* p )
00243 {
00244   kProcessList.append( p );
00245 }
00246 
00247 void KProcessController::removeKProcess( KProcess* p )
00248 {
00249   kProcessList.remove( p );
00250 }
00251 
00252 void KProcessController::addProcess( int pid )
00253 {
00254   unixProcessList.append( pid );
00255   ref(); // make sure we stay around when the KProcess goes away
00256 }
00257 
00258 #include "kprocctrl.moc"
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 30 10:09:40 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003