blob: 73decfd89be6019181d5ac72a9c2f7e05e96e568 [file] [log] [blame]
Chris Lattnerbac27a42002-04-18 19:53:53 +00001//===- Signals.cpp - Signal Handling support ------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerbac27a42002-04-18 19:53:53 +00009//
10// This file defines some helpful functions for dealing with the possibility of
Misha Brukman950971d2003-09-16 15:31:46 +000011// Unix signals occuring while your program is running.
Chris Lattnerbac27a42002-04-18 19:53:53 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "Support/Signals.h"
16#include <vector>
17#include <algorithm>
18#include <cstdlib>
19#include <cstdio>
20#include <signal.h>
Chris Lattnerfafac122003-08-01 19:16:29 +000021#include "Config/config.h" // Get the signal handler return type
Chris Lattnerbac27a42002-04-18 19:53:53 +000022
Brian Gaeked0fde302003-11-11 22:41:34 +000023namespace llvm {
24
Chris Lattner01e770a2003-05-22 21:59:35 +000025static std::vector<std::string> FilesToRemove;
Chris Lattnerbac27a42002-04-18 19:53:53 +000026
27// IntSigs - Signals that may interrupt the program at any time.
28static const int IntSigs[] = {
29 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
30};
31static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
32
33// KillSigs - Signals that are synchronous with the program that will cause it
34// to die.
35static const int KillSigs[] = {
Chris Lattner7c97cee2002-09-13 14:57:24 +000036 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
37#ifdef SIGEMT
38 , SIGEMT
39#endif
Chris Lattnerbac27a42002-04-18 19:53:53 +000040};
41static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
42
43
44// SignalHandler - The signal handler that runs...
John Criswell7a73b802003-06-30 21:59:07 +000045static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattnerbac27a42002-04-18 19:53:53 +000046 while (!FilesToRemove.empty()) {
47 std::remove(FilesToRemove.back().c_str());
48 FilesToRemove.pop_back();
49 }
50
Anand Shuklacfb22d32002-06-25 20:55:50 +000051 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
Chris Lattnerbac27a42002-04-18 19:53:53 +000052 exit(1); // If this is an interrupt signal, exit the program
53
54 // Otherwise if it is a fault (like SEGV) reissue the signal to die...
Chris Lattner39602b22003-05-27 16:25:04 +000055 signal(Sig, SIG_DFL);
Chris Lattnerbac27a42002-04-18 19:53:53 +000056}
57
58static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
59
60// RemoveFileOnSignal - The public API
Chris Lattner01e770a2003-05-22 21:59:35 +000061void RemoveFileOnSignal(const std::string &Filename) {
Chris Lattnerbac27a42002-04-18 19:53:53 +000062 FilesToRemove.push_back(Filename);
63
Anand Shuklacfb22d32002-06-25 20:55:50 +000064 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
65 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
Chris Lattnerbac27a42002-04-18 19:53:53 +000066}
Brian Gaeked0fde302003-11-11 22:41:34 +000067
68} // End llvm namespace