Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 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> |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame^] | 14 | using std::string; |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 15 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame^] | 16 | static std::vector<string> FilesToRemove; |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 17 | |
| 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 |
| 21 | }; |
| 22 | static 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. |
| 26 | static const int KillSigs[] = { |
| 27 | SIGILL, SIGTRAP, SIGABRT, SIGEMT, SIGFPE, |
| 28 | SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ |
| 29 | }; |
| 30 | static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]); |
| 31 | |
| 32 | |
| 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(); |
| 38 | } |
| 39 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame^] | 40 | if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 41 | exit(1); // If this is an interrupt signal, exit the program |
| 42 | |
| 43 | // Otherwise if it is a fault (like SEGV) reissue the signal to die... |
| 44 | } |
| 45 | |
| 46 | static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); } |
| 47 | |
| 48 | // RemoveFileOnSignal - The public API |
| 49 | void RemoveFileOnSignal(const string &Filename) { |
| 50 | FilesToRemove.push_back(Filename); |
| 51 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame^] | 52 | std::for_each(IntSigs, IntSigsEnd, RegisterHandler); |
| 53 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 54 | } |