blob: 29daedf0fb9a05a3ead305ba4566a725ecc3113a [file] [log] [blame]
Chris Lattnerbac27a42002-04-18 19:53:53 +00001//===- 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 Shuklacfb22d32002-06-25 20:55:50 +000014using std::string;
Chris Lattnerbac27a42002-04-18 19:53:53 +000015
Anand Shuklacfb22d32002-06-25 20:55:50 +000016static std::vector<string> FilesToRemove;
Chris Lattnerbac27a42002-04-18 19:53:53 +000017
18// IntSigs - Signals that may interrupt the program at any time.
19static const int IntSigs[] = {
20 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
21};
22static 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.
26static const int KillSigs[] = {
27 SIGILL, SIGTRAP, SIGABRT, SIGEMT, SIGFPE,
28 SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
29};
30static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
31
32
33// SignalHandler - The signal handler that runs...
34static void SignalHandler(int Sig) {
35 while (!FilesToRemove.empty()) {
36 std::remove(FilesToRemove.back().c_str());
37 FilesToRemove.pop_back();
38 }
39
Anand Shuklacfb22d32002-06-25 20:55:50 +000040 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
Chris Lattnerbac27a42002-04-18 19:53:53 +000041 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
46static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
47
48// RemoveFileOnSignal - The public API
49void RemoveFileOnSignal(const string &Filename) {
50 FilesToRemove.push_back(Filename);
51
Anand Shuklacfb22d32002-06-25 20:55:50 +000052 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
53 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
Chris Lattnerbac27a42002-04-18 19:53:53 +000054}