Logo Search packages:      
Sourcecode: jack-tools version File versions  Download package

signal.c

#ifndef _SIGNAL_C
#define _SIGNAL_C

#include <signal.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>

volatile unsigned int signal_received = 0 ;

inline int
end_of_process_signaled ( void )
{
  return ( signal_received & 1<<SIGHUP || 
         signal_received & 1<<SIGINT || 
         signal_received & 1<<SIGQUIT || 
         signal_received & 1<<SIGPIPE || 
         signal_received & 1<<SIGTERM ) ;
}

inline int
usr_signal_received ( void )
{
  return ( signal_received & 1<<SIGUSR1 || 
         signal_received & 1<<SIGUSR2 ) ;
}

void 
actual_signal_handler ( int s )
{
  signal_received |= 1 << s ;
}

void *
signal_management_thread ( void *PTR )
{
  pthread_setcanceltype ( PTHREAD_CANCEL_ASYNCHRONOUS , 0 ) ;
  sigset_t blocked ;  
  sigprocmask ( SIG_SETMASK , 0 , &blocked ) ;
  int s ;
  sigwait ( &blocked , &s ) ;
  if ( s != SIGSEGV ) {
    sigprocmask ( SIG_UNBLOCK , &blocked , 0 ) ;
  }
  actual_signal_handler ( s ) ;
  return NULL ;
}

int
install_signal_handlers ( void )
{
  sigset_t signals ;
  sigemptyset ( &signals ) ;
  sigaddset ( &signals , SIGHUP ) ;
  sigaddset ( &signals , SIGINT ) ;
  sigaddset ( &signals , SIGQUIT ) ;
  sigaddset ( &signals , SIGPIPE ) ;
  sigaddset ( &signals , SIGTERM ) ;
  sigaddset ( &signals , SIGUSR1 ) ;
  sigaddset ( &signals , SIGUSR2 ) ;
  struct sigaction action ;
  action.sa_handler = actual_signal_handler ;
  action.sa_mask = signals ;
  action.sa_flags = SA_RESTART | SA_RESETHAND ;
  int i ;
  for ( i = 1 ; i < 32 ; i++ ) {
    if ( sigismember ( &signals , i ) ) {
      if ( sigaction ( i , &action , 0 ) ) {
      fprintf ( stderr , "sigaction() failed: %d\n" , i ) ;
      return -1 ;
      }
    }
  } 
  if ( pthread_sigmask ( SIG_SETMASK , &signals , 0 ) ) {
    fprintf ( stderr , "pthread_sigmask() failed: %s\n" , strerror ( errno ) ) ;
    return -1;
  }
  pthread_t signal_thread_id ;
  if ( pthread_create ( &signal_thread_id , 0 , signal_management_thread , 0 ) ) {
    fprintf ( stderr , "pthread_create() failed\n" ) ;
    return -1;
  }
  pthread_detach ( signal_thread_id ) ;
  return 0 ;
}

#endif

Generated by  Doxygen 1.6.0   Back to index