📄 signal.c
字号:
estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation. *//* (C) COPYRIGHT International Business Machines Corp. 2001 */#include "pthread.h"#pragma info(none)#include "pkcsslotd.h"#pragma info(restore)#pragma info(nopar, noppt)#ifdef PKCS64extern BOOL IsValidProcessEntry ( pid_t_64 pid, time_t_64 RegTime );#elseextern BOOL IsValidProcessEntry ( pid_t pid, time_t RegTime );#endifstatic int SigsToIntercept[] = { SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGTSTP, SIGTTIN, SIGTTOU, SIGUSR1, SIGUSR2, SIGPROF#if defined(AIX) , SIGDANGER, SIGVTALRM, SIGMIGRATE, SIGPRE, SIGGRANT, SIGRETRACT, SIGSOUND, SIGSAK#endif};/* SIGCONT - Don't want to exit on SIGCONT; it'll in fact mask other signals - kill -HUP actually sends SIGCONT before SIGHUP *//* SIGCHLD - Don't want to exit. Should never receive, but we do, apparently when something tries to cancel the GC Thread */static int SigsToIgnore[] = { SIGCHLD, };/************************************************** * slotdGenericSignalHandler * * Main signal handler for the daemon. Doesn't * allow the daemon to be killed unless we're * not in use ***************************************************/void slotdGenericSignalHandler( int Signal ) { int procindex; BOOL OkToExit = TRUE; /******************************************************** * DbgLog calls (possibly) printf, syslog_r, etc. * The behavior of these functions is "undefined" * when called from a signal handler according to * the sigaction man page. * * Thus, they're only called in development * versions of the code. ********************************************************/ #ifdef DEV DbgLog(DL2, "slotdGenericSignalHandler got %s (%d; %#x)", SignalConst(Signal), Signal, Signal); #endif /* DEV */#if !defined(NOGARBAGE) StopGCThread(shmp); CheckForGarbage(shmp);#endif for ( procindex = 0; (procindex < NUMBER_PROCESSES_ALLOWED); procindex++ ) {#ifdef PKCS64 Slot_Mgr_Proc_t_64 *pProc = &(shmp->proc_table[procindex]);#else Slot_Mgr_Proc_t *pProc = &(shmp->proc_table[procindex]);#endif if ( shmp == NULL ) { break; } if ( ( pProc->inuse ) #if !(NOGARBAGE) && ( IsValidProcessEntry( pProc->proc_id, pProc->reg_time))#endif ) { /* Someone's still using us... Log it */ OkToExit = FALSE; #ifdef DEV WarnLog("Process %d is still registered", pProc->proc_id); #endif } } if ( !OkToExit ) { DbgLog(DL1,"Continuing execution");#if !defined(NOGARBAGE) StartGCThread(shmp);#endif return; } InfoLog("Exiting on %s (%d; %#x)", SignalConst(Signal), Signal, Signal); DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); exit(0);}#pragma info(restore)#if TEST_MUTEXES || TEST_COND_VARS void TestSig ( int Signal ) { int err; LogLog ( "TestSig got %s (%d; %#x)", SignalConst(Signal), Signal, Signal); #if TEST_MUTEXES /* Release the global shared memory mutex */ pthread_mutex_unlock(&(shmp->slt_mutex)); LogLog("TestSig unlocked the mutex"); #endif /* TEST_MUTEXES */ #if TEST_COND_VARS LogLog("Locking the CV Mutex"); if ( (err = pthread_mutex_lock( &(shmp->shmem_cv_mutex) )) != 0 ) { DbgLog(DL0,"TestSig: pthread_mutex_lock returned %s (%d; %#x)\n", SysConst(err), err, err ); return; } /* Manipulate the variable */ LogLog("Locked the CV Mutex"); /* Signal one app/thread */ if ( (err = pthread_cond_signal( &(shmp->shmem_cv) ) ) != 0 ) { DbgLog(DL0,"TestSig: pthread_cond_signal returned %s (%d; %#x)\n", SysConst(err), err, err ); return; } LogLog("Releasing the CV Mutex"); if ( (err = pthread_mutex_unlock ( &(shmp->shmem_cv_mutex) ) ) != 0 ) { DbgLog(DL0,"TestSig: pthread_mutex_unlock returned %s (%d; %#x)\n", SysConst(err), err, err ); return; } LogLog("CV Mutex released"); #endif /* TEST_COND_VARS */ return;}#endif /* TEST_MUTEXES || TEST_COND_VARS*//*************************************************** * SetupSignalHandlers - * * Installs slotdGenericSignalHandler for the listed signals * ***************************************************/int SetupSignalHandlers ( void ) { int i; struct sigaction new_action; new_action.sa_handler = slotdGenericSignalHandler; sigemptyset(&(new_action.sa_mask)); sigaddset(&(new_action.sa_mask), SIGCHLD); /* sigaddset(&(new_action.sa_mask), SA_NOCLDWAIT); */ /* sigaddset(&(new_action.sa_mask), SA_NOCLDSTOP); */ #pragma info(nocnd) new_action.sa_flags = (RESTART_SYS_CALLS ? SA_RESTART : 0); #pragma info(restore) for ( i = 0; i < (sizeof(SigsToIntercept) / sizeof(SigsToIntercept[0])); i++ ) { if ( sigaction ( SigsToIntercept[i], &new_action, NULL ) != 0 ) { //DbgLog("SetupSignalHandlers - sigaction failed for %s (%d; %#x)", SignalConst(SigsToIntercept[i]), SigsToIntercept[i], SigsToIntercept[i]); return FALSE; } } new_action.sa_handler = SIG_IGN; sigemptyset(&(new_action.sa_mask)); for ( i = 0; i < (sizeof ( SigsToIgnore ) / sizeof (SigsToIgnore[0]) ); i++ ) { if ( sigaction ( SigsToIgnore[i], &new_action, NULL ) != 0 ) { //DbgLog ( "Failed to ignore signal."); return FALSE; } } #if TEST_MUTEXES || TEST_COND_VARS new_action.sa_handler = TestSig; if ( sigaction ( SIGUSR1, &new_action, NULL ) != 0 ) { DbgLog(DL0,"Failed to set TestSig for SIGUSR1"); return FALSE; } #endif /* TEST_MUTEXES || TEST_COND_VARS */ return TRUE;}/*********************************************************************** * GCBlockSignals - * * Garbage collector calls this to prevent signals from getting * sent to the GC thread. * ***********************************************************************/BOOL GCBlockSignals (void) { int i; int ret; sigset_t SigSet; sigemptyset(&SigSet); for ( i = 0; i < (sizeof(SigsToIntercept) / sizeof(SigsToIntercept[0]) ); i++ ) { sigaddset(&SigSet, SigsToIntercept[i]); } ret = pthread_sigmask(SIG_SETMASK, &SigSet, NULL); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -