2004-08-30 03:22:48 +08:00
|
|
|
//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-08-30 03:22:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines some helpful functions for dealing with the possibility of
|
|
|
|
// Unix signals occuring while your program is running.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2007-09-07 12:06:50 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-08-30 03:22:48 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2004-12-27 14:16:52 +08:00
|
|
|
#if HAVE_EXECINFO_H
|
2004-08-30 03:22:48 +08:00
|
|
|
# include <execinfo.h> // For backtrace().
|
|
|
|
#endif
|
2004-12-27 14:16:52 +08:00
|
|
|
#if HAVE_SIGNAL_H
|
2004-08-30 03:22:48 +08:00
|
|
|
#include <signal.h>
|
2004-12-27 14:16:52 +08:00
|
|
|
#endif
|
2007-04-08 02:52:17 +08:00
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2006-08-02 01:59:14 +08:00
|
|
|
using namespace llvm;
|
2004-08-30 03:22:48 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-04-11 05:11:47 +08:00
|
|
|
static bool StackTraceRequested = false;
|
2006-06-16 08:00:57 +08:00
|
|
|
|
2005-08-02 10:14:22 +08:00
|
|
|
/// InterruptFunction - The function to call if ctrl-c is pressed.
|
2008-04-11 05:11:47 +08:00
|
|
|
static void (*InterruptFunction)() = 0;
|
2005-08-02 10:14:22 +08:00
|
|
|
|
2008-04-11 05:11:47 +08:00
|
|
|
static std::vector<sys::Path> *FilesToRemove = 0 ;
|
|
|
|
static std::vector<sys::Path> *DirectoriesToRemove = 0;
|
2004-08-30 03:22:48 +08:00
|
|
|
|
|
|
|
// IntSigs - Signals that may interrupt the program at any time.
|
2008-04-11 05:11:47 +08:00
|
|
|
static const int IntSigs[] = {
|
2004-08-30 03:22:48 +08:00
|
|
|
SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
|
|
|
|
};
|
2008-04-11 05:11:47 +08:00
|
|
|
static const int *IntSigsEnd = IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]);
|
2004-08-30 03:22:48 +08:00
|
|
|
|
|
|
|
// KillSigs - Signals that are synchronous with the program that will cause it
|
|
|
|
// to die.
|
2008-04-11 05:11:47 +08:00
|
|
|
static const int KillSigs[] = {
|
2004-08-30 03:22:48 +08:00
|
|
|
SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
|
|
|
|
#ifdef SIGEMT
|
|
|
|
, SIGEMT
|
|
|
|
#endif
|
|
|
|
};
|
2008-04-11 05:11:47 +08:00
|
|
|
static const int *KillSigsEnd = KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]);
|
2004-08-30 03:22:48 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_BACKTRACE
|
2008-04-11 05:11:47 +08:00
|
|
|
static void* StackTrace[256];
|
2004-08-30 03:22:48 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// PrintStackTrace - In the case of a program crash or fault, print out a stack
|
|
|
|
// trace so that the user has an indication of why and where we died.
|
|
|
|
//
|
|
|
|
// On glibc systems we have the 'backtrace' function, which works nicely, but
|
2008-02-16 02:05:54 +08:00
|
|
|
// doesn't demangle symbols.
|
2008-04-11 05:11:47 +08:00
|
|
|
static void PrintStackTrace() {
|
2004-08-30 03:22:48 +08:00
|
|
|
#ifdef HAVE_BACKTRACE
|
|
|
|
// Use backtrace() to output a backtrace on Linux systems with glibc.
|
2007-09-07 12:06:50 +08:00
|
|
|
int depth = backtrace(StackTrace, array_lengthof(StackTrace));
|
2008-02-16 02:05:54 +08:00
|
|
|
backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
|
2004-08-30 03:22:48 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignalHandler - The signal handler that runs...
|
2008-04-11 05:11:47 +08:00
|
|
|
static RETSIGTYPE SignalHandler(int Sig) {
|
2004-08-30 03:22:48 +08:00
|
|
|
if (FilesToRemove != 0)
|
|
|
|
while (!FilesToRemove->empty()) {
|
2006-08-07 13:34:08 +08:00
|
|
|
FilesToRemove->back().eraseFromDisk(true);
|
2004-08-30 03:22:48 +08:00
|
|
|
FilesToRemove->pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DirectoriesToRemove != 0)
|
|
|
|
while (!DirectoriesToRemove->empty()) {
|
2005-07-08 11:08:58 +08:00
|
|
|
DirectoriesToRemove->back().eraseFromDisk(true);
|
2004-08-30 03:22:48 +08:00
|
|
|
DirectoriesToRemove->pop_back();
|
|
|
|
}
|
|
|
|
|
2005-08-02 10:14:22 +08:00
|
|
|
if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
|
|
|
|
if (InterruptFunction) {
|
|
|
|
void (*IF)() = InterruptFunction;
|
|
|
|
InterruptFunction = 0;
|
|
|
|
IF(); // run the interrupt function.
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
exit(1); // If this is an interrupt signal, exit the program
|
|
|
|
}
|
|
|
|
}
|
2004-08-30 03:22:48 +08:00
|
|
|
|
|
|
|
// Otherwise if it is a fault (like SEGV) output the stacktrace to
|
|
|
|
// STDERR (if we can) and reissue the signal to die...
|
2006-06-16 08:00:57 +08:00
|
|
|
if (StackTraceRequested)
|
|
|
|
PrintStackTrace();
|
2004-08-30 03:22:48 +08:00
|
|
|
signal(Sig, SIG_DFL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just call signal
|
2008-04-11 05:11:47 +08:00
|
|
|
static void RegisterHandler(int Signal) {
|
2004-08-30 03:22:48 +08:00
|
|
|
signal(Signal, SignalHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-02 10:14:22 +08:00
|
|
|
void sys::SetInterruptFunction(void (*IF)()) {
|
|
|
|
InterruptFunction = IF;
|
|
|
|
RegisterHandler(SIGINT);
|
|
|
|
}
|
|
|
|
|
2004-08-30 03:22:48 +08:00
|
|
|
// RemoveFileOnSignal - The public API
|
2006-08-26 05:37:17 +08:00
|
|
|
bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
|
2004-08-30 03:22:48 +08:00
|
|
|
if (FilesToRemove == 0)
|
2006-08-07 13:34:08 +08:00
|
|
|
FilesToRemove = new std::vector<sys::Path>;
|
2004-08-30 03:22:48 +08:00
|
|
|
|
2006-08-07 13:34:08 +08:00
|
|
|
FilesToRemove->push_back(Filename);
|
2004-08-30 03:22:48 +08:00
|
|
|
|
|
|
|
std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
|
|
|
|
std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
|
2006-08-26 05:37:17 +08:00
|
|
|
return false;
|
2004-08-30 03:22:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveDirectoryOnSignal - The public API
|
2006-08-26 05:37:17 +08:00
|
|
|
bool sys::RemoveDirectoryOnSignal(const sys::Path& path, std::string* ErrMsg) {
|
2006-08-02 01:59:14 +08:00
|
|
|
// Not a directory?
|
2007-04-08 02:52:17 +08:00
|
|
|
struct stat buf;
|
|
|
|
if (0 != stat(path.c_str(), &buf)) {
|
|
|
|
MakeErrMsg(ErrMsg, path.toString() + ": can't get status of file");
|
2007-03-30 03:05:44 +08:00
|
|
|
return true;
|
2007-04-08 02:52:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISDIR(buf.st_mode)) {
|
2006-08-26 05:37:17 +08:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = path.toString() + " is not a directory";
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-30 03:22:48 +08:00
|
|
|
|
|
|
|
if (DirectoriesToRemove == 0)
|
|
|
|
DirectoriesToRemove = new std::vector<sys::Path>;
|
|
|
|
|
|
|
|
DirectoriesToRemove->push_back(path);
|
|
|
|
|
|
|
|
std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
|
|
|
|
std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
|
2006-08-26 05:37:17 +08:00
|
|
|
return false;
|
2004-08-30 03:22:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
|
|
|
|
/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
|
|
|
|
void sys::PrintStackTraceOnErrorSignal() {
|
2006-06-16 08:00:57 +08:00
|
|
|
StackTraceRequested = true;
|
2004-08-30 03:22:48 +08:00
|
|
|
std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
|
|
|
|
}
|