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
24 static std::vector<std::string> FilesToRemove;
26 // IntSigs - Signals that may interrupt the program at any time.
27 static const int IntSigs[] = {
28 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
30 static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
32 // KillSigs - Signals that are synchronous with the program that will cause it
34 static const int KillSigs[] = {
35 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
40 static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
43 // SignalHandler - The signal handler that runs...
44 static RETSIGTYPE SignalHandler(int Sig) {
45 while (!FilesToRemove.empty()) {
46 std::remove(FilesToRemove.back().c_str());
47 FilesToRemove.pop_back();
50 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
51 exit(1); // If this is an interrupt signal, exit the program
53 // Otherwise if it is a fault (like SEGV) reissue the signal to die...
57 static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
59 // RemoveFileOnSignal - The public API
60 void llvm::RemoveFileOnSignal(const std::string &Filename) {
61 FilesToRemove.push_back(Filename);
63 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
64 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);