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