From 02833e4c179e4933d10f0d3159ba3ecd1f468b12 Mon Sep 17 00:00:00 2001 From: ehouse Date: Wed, 11 Oct 2006 02:06:20 +0000 Subject: [PATCH] Make self-restarting: fork child and wait (to deal with asserts). --- relay/xwrelay.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/relay/xwrelay.cpp b/relay/xwrelay.cpp index df11f1b1a..5bd01d96e 100644 --- a/relay/xwrelay.cpp +++ b/relay/xwrelay.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #if defined(__FreeBSD__) # if (OSVERSION > 500000) @@ -451,6 +452,20 @@ SIGINT_handler( int sig ) shutdown(); } +static void +printWhy( int status ) +{ + if ( WIFEXITED(status) ) { + logf( XW_LOGINFO, "why: exited" ); + } else if ( WIFSIGNALED(status) ) { + logf( XW_LOGINFO, "why: signaled" ); + } else if ( WCOREDUMP(status) ) { + logf( XW_LOGINFO, "why: core" ); + } else if ( WIFSTOPPED(status) ) { + logf( XW_LOGINFO, "why: traced" ); + } +} /* printWhy */ + int main( int argc, char** argv ) { int port = 0; @@ -525,6 +540,22 @@ int main( int argc, char** argv ) PermID::SetServerName( serverName ); PermID::SetIDFileName( idFileName ); + /* loop forever, relaunching children as they die. */ + for ( ; ; ) { + pid_t pid = fork(); + if ( pid == 0 ) { /* child */ + break; + } else if ( pid > 0 ) { + int status; + logf( XW_LOGINFO, "parent waiting on child pid=%d", pid ); + waitpid( pid, &status, 0 ); + printWhy( status ); + sleep( 45 ); /* give time to close sockets? */ + } else { + logf( XW_LOGERROR, "fork() => %s", strerror(errno) ); + } + } + g_listener = make_socket( INADDR_ANY, port ); if ( g_listener == -1 ) { exit( 1 );