From e4f6f5b57ffa8fd93b41829b7130ad7f46af26ba Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Sat, 2 Mar 2013 03:08:10 +0000 Subject: Use sigaction instead of a signal handler to wait for the SIGHUP --- jaildaemon.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/jaildaemon.c b/jaildaemon.c index 3ae8612..7dad058 100644 --- a/jaildaemon.c +++ b/jaildaemon.c @@ -36,7 +36,6 @@ typedef struct { } daemon_task; /* Forward declarations */ -static void signal_handler( int signal ); static void term_handler( int signal ); static void kill_all_probes( void ); static int check_for_jail( int jid ); @@ -49,14 +48,8 @@ static void exerr( char * message ); static void warn( char * message ); static void usage( char * command ); -/* This is the handler installed in the jailed process. It will exit with the - proper exit code to make the host system daemon recognize the process has - deliberately killed itself and was not just shutdown with the jail */ -static void signal_handler( int signal ) { - if( signal == SIGHUP ) - _exit( MAGIC_EXIT_CODE ); -} - +/* This handler ensures that we clean up our probes if asked to terminate + gracefully */ static void term_handler( int signal ) { if( signal == SIGTERM ) exit(0); @@ -174,28 +167,27 @@ static int check_for_jail( int jid ) { } static pid_t fork_and_jail( int jid, char * proctitle ) { + int sig; pid_t pid = fork(); if( !pid ) { - struct sigaction sa; + sigset_t sigset; /* Set proctitle so that jail's pgrep -f can identify the process */ if( proctitle && *proctitle ) setproctitle( "%s", proctitle ); - /* Setup signal handler for SIGHUP */ - sa.sa_handler = signal_handler; - sigemptyset(&sa.sa_mask); - sa.sa_flags = SA_RESTART; - if( sigaction(SIGHUP, &sa, NULL) == -1 ) - exerr( "when install signal handler" ); - /* Throw ourself into the jail */ if( jail_attach( jid ) ) exerr( "when attaching to jail" ); - /* Spin and wait for SIGHUP */ - while( 1 ) - sleep(32); + /* wait for SIGHUP */ + sigemptyset(&sigset); + sigaddset(&sigset, SIGHUP); + sigprocmask(SIG_BLOCK, &sigset, NULL); + while( !sigwait( &sigset, &sig ) ) + if( sig == SIGHUP ) + exit( MAGIC_EXIT_CODE ); + exit(0); } return pid; } @@ -281,7 +273,6 @@ static void fork_and_execve( int kq, daemon_task * t_in ) { static void kill_all_probes( void ) { size_t i; -syslog( LOG_ERR, "KILLING PROBES" ); if( g_probes ) for( i = 0; i < g_probes_size; ++i ) if( g_probes[i] ) -- cgit v1.2.3