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> |
Chris Lattner | fafac12 | 2003-08-01 19:16:29 +0000 | [diff] [blame^] | 14 | #include "Config/config.h" // Get the signal handler return type |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 16 | static std::vector<std::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[] = { |
Chris Lattner | 7c97cee | 2002-09-13 14:57:24 +0000 | [diff] [blame] | 27 | SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ |
| 28 | #ifdef SIGEMT |
| 29 | , SIGEMT |
| 30 | #endif |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 31 | }; |
| 32 | static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]); |
| 33 | |
| 34 | |
| 35 | // SignalHandler - The signal handler that runs... |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 36 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 37 | while (!FilesToRemove.empty()) { |
| 38 | std::remove(FilesToRemove.back().c_str()); |
| 39 | FilesToRemove.pop_back(); |
| 40 | } |
| 41 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 42 | if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 43 | exit(1); // If this is an interrupt signal, exit the program |
| 44 | |
| 45 | // Otherwise if it is a fault (like SEGV) reissue the signal to die... |
Chris Lattner | 39602b2 | 2003-05-27 16:25:04 +0000 | [diff] [blame] | 46 | signal(Sig, SIG_DFL); |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); } |
| 50 | |
| 51 | // RemoveFileOnSignal - The public API |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 52 | void RemoveFileOnSignal(const std::string &Filename) { |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 53 | FilesToRemove.push_back(Filename); |
| 54 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 55 | std::for_each(IntSigs, IntSigsEnd, RegisterHandler); |
| 56 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 57 | } |