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