Add a timeout while waiting for the parent to finish clean up or die.

This commit is contained in:
Steve Checkoway
2006-08-06 03:52:46 +00:00
parent 65f55ff83e
commit a4061b645d
@@ -6,6 +6,7 @@
#include <cerrno> #include <cerrno>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/select.h>
#include "Backtrace.h" #include "Backtrace.h"
#include "BacktraceNames.h" #include "BacktraceNames.h"
@@ -16,6 +17,7 @@
#include "RageLog.h" /* for RageLog::GetAdditionalLog, etc. only */ #include "RageLog.h" /* for RageLog::GetAdditionalLog, etc. only */
#include "ProductInfo.h" #include "ProductInfo.h"
#include "ArchHooks.h" #include "ArchHooks.h"
#include "RageTimer.h"
#if defined(MACOSX) #if defined(MACOSX)
#include "archutils/Darwin/Crash.h" #include "archutils/Darwin/Crash.h"
@@ -147,21 +149,40 @@ static void child_process()
const RString CrashedThread(temp); const RString CrashedThread(temp);
delete[] temp; delete[] temp;
/* Wait for the child to either finish cleaning up or die. XXX: /* Wait for the child to either finish cleaning up or die. */
* This should time out, in case something deadlocks. */ fd_set rs;
struct timeval timeout = { 5, 0 }; // 5 seconds
char x; FD_ZERO( &rs );
int ret = read( 3, &x, sizeof(x) ); FD_SET( 3, &rs );
if( ret > 0 ) int ret = select( 4, &rs, NULL, NULL, &timeout );
if( ret == 0 )
{ {
fprintf( stderr, "Unexpected child read() result: %i\n", ret ); fputs( "Timeout exceeded.\n", stderr );
/* keep going */
} }
else if( (ret == -1 && errno != EPIPE) || ret != 0 ) else if( (ret == -1 && errno != EPIPE) || ret != 1 )
{ {
/* We expect an EOF or EPIPE. What happened? */ fprintf( stderr, "Unexpected return from select() result: %d (%s)\n", ret, strerror(errno) );
fprintf( stderr, "Unexpected child read() result: %i (%s)\n", ret, strerror(errno) ); // Keep going.
/* keep going */ }
else
{
char x;
// No need to check FD_ISSET( 3, &rs ) because it was the only descriptor in the set.
ret = read( 3, &x, sizeof(x) );
if( ret > 0 )
{
fprintf( stderr, "Unexpected child read() result: %i\n", ret );
/* keep going */
}
else if( (ret == -1 && errno != EPIPE) || ret != 0 )
{
/* We expect an EOF or EPIPE. What happened? */
fprintf( stderr, "Unexpected child read() result: %i (%s)\n", ret, strerror(errno) );
/* keep going */
}
} }
RString sCrashInfoPath = "/tmp"; RString sCrashInfoPath = "/tmp";
@@ -215,12 +236,13 @@ static void child_process()
} }
} }
case CrashData::FORCE_CRASH: case CrashData::FORCE_CRASH:
crash.reason[ sizeof(crash.reason)-1] = 0; crash.reason[sizeof(crash.reason)-1] = 0;
reason = crash.reason; reason = crash.reason;
break; break;
} }
fprintf( CrashDump, "Architecture: %s\n", HOOKS->GetArchName().c_str() ); // XXX: HOOKS isn't set when this is called.
//fprintf( CrashDump, "Architecture: %s\n", HOOKS->GetArchName().c_str() );
fprintf( CrashDump, "Crash reason: %s\n", reason.c_str() ); fprintf( CrashDump, "Crash reason: %s\n", reason.c_str() );
fprintf( CrashDump, "Crashed thread: %s\n\n", CrashedThread.c_str() ); fprintf( CrashDump, "Crashed thread: %s\n\n", CrashedThread.c_str() );
@@ -252,7 +274,7 @@ static void child_process()
CrashHandler::InformUserOfCrash( sCrashInfoPath ); CrashHandler::InformUserOfCrash( sCrashInfoPath );
#else #else
/* stdout may have been inadvertently closed by the crash in the parent; /* stdout may have been inadvertently closed by the crash in the parent;
* write to /dev/tty instead. */ * write to /dev/tty instead. */
FILE *tty = fopen( "/dev/tty", "w" ); FILE *tty = fopen( "/dev/tty", "w" );
if( tty == NULL ) if( tty == NULL )
tty = stderr; tty = stderr;