1 //===- Signals.cpp - Signal Handling support ------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines some helpful functions for dealing with the possibility of
11 // Unix signals occuring while your program is running.
13 //===----------------------------------------------------------------------===//
15 #include "Support/Signals.h"
21 #include "Config/config.h" // Get the signal handler return type
23 static std::vector<std::string> FilesToRemove;
25 // IntSigs - Signals that may interrupt the program at any time.
26 static const int IntSigs[] = {
27 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
29 static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
31 // KillSigs - Signals that are synchronous with the program that will cause it
33 static const int KillSigs[] = {
34 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
39 static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
42 // SignalHandler - The signal handler that runs...
43 static RETSIGTYPE SignalHandler(int Sig) {
44 while (!FilesToRemove.empty()) {
45 std::remove(FilesToRemove.back().c_str());
46 FilesToRemove.pop_back();
49 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
50 exit(1); // If this is an interrupt signal, exit the program
52 // Otherwise if it is a fault (like SEGV) reissue the signal to die...
56 static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
58 // RemoveFileOnSignal - The public API
59 void RemoveFileOnSignal(const std::string &Filename) {
60 FilesToRemove.push_back(Filename);
62 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
63 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);