Signals.cpp revision 7c97cee26a25f398c31be7f8853d363410f6a31e
1//===- Signals.cpp - Signal Handling support ------------------------------===// 2// 3// This file defines some helpful functions for dealing with the possibility of 4// unix signals occuring while your program is running. 5// 6//===----------------------------------------------------------------------===// 7 8#include "Support/Signals.h" 9#include <vector> 10#include <algorithm> 11#include <cstdlib> 12#include <cstdio> 13#include <signal.h> 14using std::string; 15 16static std::vector<string> FilesToRemove; 17 18// IntSigs - Signals that may interrupt the program at any time. 19static const int IntSigs[] = { 20 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 21}; 22static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]); 23 24// KillSigs - Signals that are synchronous with the program that will cause it 25// to die. 26static const int KillSigs[] = { 27 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ 28#ifdef SIGEMT 29 , SIGEMT 30#endif 31}; 32static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]); 33 34 35// SignalHandler - The signal handler that runs... 36static void SignalHandler(int Sig) { 37 while (!FilesToRemove.empty()) { 38 std::remove(FilesToRemove.back().c_str()); 39 FilesToRemove.pop_back(); 40 } 41 42 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) 43 exit(1); // If this is an interrupt signal, exit the program 44 45 // Otherwise if it is a fault (like SEGV) reissue the signal to die... 46} 47 48static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); } 49 50// RemoveFileOnSignal - The public API 51void RemoveFileOnSignal(const string &Filename) { 52 FilesToRemove.push_back(Filename); 53 54 std::for_each(IntSigs, IntSigsEnd, RegisterHandler); 55 std::for_each(KillSigs, KillSigsEnd, RegisterHandler); 56} 57