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 refCount++; 00044 } 00045 00046 void KProcessController::deref() 00047 { 00048 refCount--; 00049 if( !refCount ) { 00050 delete theKProcessController; 00051 theKProcessController = 0; 00052 } 00053 } 00054 00055 KProcessController::KProcessController() 00056 : needcheck( false ) 00057 { 00058 if( pipe( fd ) ) 00059 { 00060 perror( "pipe" ); 00061 abort(); 00062 } 00063 00064 fcntl( fd[0], F_SETFL, O_NONBLOCK ); // in case slotDoHousekeeping is called without polling first 00065 fcntl( fd[1], F_SETFL, O_NONBLOCK ); // in case it fills up 00066 fcntl( fd[0], F_SETFD, FD_CLOEXEC ); 00067 fcntl( fd[1], F_SETFD, FD_CLOEXEC ); 00068 00069 notifier = new QSocketNotifier( fd[0], QSocketNotifier::Read ); 00070 notifier->setEnabled( true ); 00071 QObject::connect( notifier, SIGNAL(activated(int)), 00072 SLOT(slotDoHousekeeping())); 00073 00074 setupHandlers(); 00075 } 00076 00077 KProcessController::~KProcessController() 00078 { 00079 resetHandlers(); 00080 00081 delete notifier; 00082 00083 close( fd[0] ); 00084 close( fd[1] ); 00085 } 00086 00087 00088 extern "C" { 00089 static void theReaper( int num ) 00090 { 00091 KProcessController::theSigCHLDHandler( num ); 00092 } 00093 } 00094 00095 struct sigaction KProcessController::oldChildHandlerData; 00096 bool KProcessController::handlerSet = false; 00097 00098 void KProcessController::setupHandlers() 00099 { 00100 if( handlerSet ) 00101 return; 00102 handlerSet = true; 00103 00104 struct sigaction act; 00105 sigemptyset( &act.sa_mask ); 00106 00107 act.sa_handler = SIG_IGN; 00108 act.sa_flags = 0; 00109 sigaction( SIGPIPE, &act, 0L ); 00110 00111 act.sa_handler = theReaper; 00112 act.sa_flags = SA_NOCLDSTOP; 00113 // CC: take care of SunOS which automatically restarts interrupted system 00114 // calls (and thus does not have SA_RESTART) 00115 #ifdef SA_RESTART 00116 act.sa_flags |= SA_RESTART; 00117 #endif 00118 sigaction( SIGCHLD, &act, &oldChildHandlerData ); 00119 00120 sigaddset( &act.sa_mask, SIGCHLD ); 00121 // Make sure we don't block this signal. gdb tends to do that :-( 00122 sigprocmask( SIG_UNBLOCK, &act.sa_mask, 0 ); 00123 } 00124 00125 void KProcessController::resetHandlers() 00126 { 00127 if( !handlerSet ) 00128 return; 00129 handlerSet = false; 00130 00131 sigaction( SIGCHLD, &oldChildHandlerData, 0 ); 00132 // there should be no problem with SIGPIPE staying SIG_IGN 00133 } 00134 00135 // the pipe is needed to sync the child reaping with our event processing, 00136 // as otherwise there are race conditions, locking requirements, and things 00137 // generally get harder 00138 void KProcessController::theSigCHLDHandler( int arg ) 00139 { 00140 int saved_errno = errno; 00141 00142 char dummy = 0; 00143 ::write( theKProcessController->fd[1], &dummy, 1 ); 00144 00145 if( oldChildHandlerData.sa_handler != SIG_IGN && 00146 oldChildHandlerData.sa_handler != SIG_DFL ) 00147 oldChildHandlerData.sa_handler( arg ); // call the old handler 00148 00149 errno = saved_errno; 00150 } 00151 00152 int KProcessController::notifierFd() const 00153 { 00154 return fd[0]; 00155 } 00156 00157 void KProcessController::unscheduleCheck() 00158 { 00159 char dummy[16]; // somewhat bigger - just in case several have queued up 00160 if( ::read( fd[0], dummy, sizeof(dummy) ) > 0 ) 00161 needcheck = true; 00162 } 00163 00164 void 00165 KProcessController::rescheduleCheck() 00166 { 00167 if( needcheck ) 00168 { 00169 needcheck = false; 00170 char dummy = 0; 00171 ::write( fd[1], &dummy, 1 ); 00172 } 00173 } 00174 00175 void KProcessController::slotDoHousekeeping() 00176 { 00177 char dummy[16]; // somewhat bigger - just in case several have queued up 00178 ::read( fd[0], dummy, sizeof(dummy) ); 00179 00180 int status; 00181 again: 00182 QValueListIterator<KProcess*> it( kProcessList.begin() ); 00183 QValueListIterator<KProcess*> eit( kProcessList.end() ); 00184 while( it != eit ) 00185 { 00186 KProcess *prc = *it; 00187 if( prc->runs && waitpid( prc->pid_, &status, WNOHANG ) > 0 ) 00188 { 00189 prc->processHasExited( status ); 00190 // the callback can nuke the whole process list and even 'this' 00191 if (!theKProcessController) 00192 return; 00193 goto again; 00194 } 00195 ++it; 00196 } 00197 QValueListIterator<int> uit( unixProcessList.begin() ); 00198 QValueListIterator<int> ueit( unixProcessList.end() ); 00199 while( uit != ueit ) 00200 { 00201 if( waitpid( *uit, 0, WNOHANG ) > 0 ) 00202 { 00203 uit = unixProcessList.remove( uit ); 00204 deref(); // counterpart to addProcess, can invalidate 'this' 00205 } else 00206 ++uit; 00207 } 00208 } 00209 00210 bool KProcessController::waitForProcessExit( int timeout ) 00211 { 00212 for(;;) 00213 { 00214 struct timeval tv, *tvp; 00215 if (timeout < 0) 00216 tvp = 0; 00217 else 00218 { 00219 tv.tv_sec = timeout; 00220 tv.tv_usec = 0; 00221 tvp = &tv; 00222 } 00223 00224 fd_set fds; 00225 FD_ZERO( &fds ); 00226 FD_SET( fd[0], &fds ); 00227 00228 switch( select( fd[0]+1, &fds, 0, 0, tvp ) ) 00229 { 00230 case -1: 00231 if( errno == EINTR ) 00232 continue; 00233 // fall through; should never happen 00234 case 0: 00235 return false; 00236 default: 00237 slotDoHousekeeping(); 00238 return true; 00239 } 00240 } 00241 } 00242 00243 void KProcessController::addKProcess( KProcess* p ) 00244 { 00245 kProcessList.append( p ); 00246 } 00247 00248 void KProcessController::removeKProcess( KProcess* p ) 00249 { 00250 kProcessList.remove( p ); 00251 } 00252 00253 void KProcessController::addProcess( int pid ) 00254 { 00255 unixProcessList.append( pid ); 00256 ref(); // make sure we stay around when the KProcess goes away 00257 } 00258 00259 #include "kprocctrl.moc"
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:54:56 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003