From da38bfc4ed4d6714a5d9f1b42e002fe8221ae268 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 20 May 2010 06:32:38 +0000 Subject: Made process easier to use, when you ask it if the process has ended, it actually checks. novel, eh? --- src/process.cpp | 19 +++++++++++++++---- src/process.h | 6 ++++++ 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/process.cpp b/src/process.cpp index da38e64..7cb5983 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -66,6 +66,11 @@ Bu::Process::~Process() close(); } +void Bu::Process::wait() +{ + close(); +} + void Bu::Process::gexec( Flags eFlags, const char *sName, char *const argv[] ) { int iaStdIn[2]; @@ -258,13 +263,17 @@ void Bu::Process::setBlocking( bool bBlocking ) { if( bBlocking ) { - fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )&(~O_NONBLOCK) ); - fcntl( iStdErr, F_SETFL, fcntl( iStdErr, F_GETFL, 0 )&(~O_NONBLOCK) ); + if( !bStdOutEos ) + fcntl( iStdOut, F_SETFL, fcntl(iStdOut,F_GETFL,0 )&(~O_NONBLOCK) ); + if( !bStdErrEos ) + fcntl( iStdErr, F_SETFL, fcntl(iStdErr,F_GETFL,0 )&(~O_NONBLOCK) ); } else { - fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )|O_NONBLOCK ); - fcntl( iStdErr, F_SETFL, fcntl( iStdErr, F_GETFL, 0 )|O_NONBLOCK ); + if( !bStdOutEos ) + fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )|O_NONBLOCK ); + if( !bStdErrEos ) + fcntl( iStdErr, F_SETFL, fcntl( iStdErr, F_GETFL, 0 )|O_NONBLOCK ); } this->bBlocking = bBlocking; } @@ -293,6 +302,8 @@ void Bu::Process::select( bool &bStdOut, bool &bStdErr ) bool Bu::Process::isRunning() { + if( waitpid( iPid, NULL, WNOHANG ) == iPid ) + close(); return iPid != 0; } diff --git a/src/process.h b/src/process.h index 665f1e0..3fcf1f2 100644 --- a/src/process.h +++ b/src/process.h @@ -37,6 +37,12 @@ namespace Bu Process( Flags eFlags, const char *sName, const char *argv, ...); virtual ~Process(); + /** + * Waits until the process exits. This blocks the caller until the + * child process terminates. + */ + void wait(); + virtual void close(); virtual size_t read( void *pBuf, size_t nBytes ); virtual size_t readErr( void *pBuf, size_t nBytes ); -- cgit v1.2.3