~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/System/Unix/Signals.inc

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
 
2
// 
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// License. See LICENSE.TXT for details.
 
7
// 
 
8
//===----------------------------------------------------------------------===//
 
9
//
 
10
// This file defines some helpful functions for dealing with the possibility of
 
11
// Unix signals occuring while your program is running.
 
12
//
 
13
//===----------------------------------------------------------------------===//
 
14
 
 
15
#include "Unix.h"
 
16
#include "llvm/ADT/STLExtras.h"
 
17
#include "llvm/System/Mutex.h"
 
18
#include <vector>
 
19
#include <algorithm>
 
20
#if HAVE_EXECINFO_H
 
21
# include <execinfo.h>         // For backtrace().
 
22
#endif
 
23
#if HAVE_SIGNAL_H
 
24
#include <signal.h>
 
25
#endif
 
26
#if HAVE_SYS_STAT_H
 
27
#include <sys/stat.h>
 
28
#endif
 
29
#if HAVE_DLFCN_H && __GNUG__
 
30
#include <dlfcn.h>
 
31
#include <cxxabi.h> 
 
32
#endif
 
33
using namespace llvm;
 
34
 
 
35
static RETSIGTYPE SignalHandler(int Sig);  // defined below.
 
36
 
 
37
static SmartMutex<true> SignalsMutex;
 
38
 
 
39
/// InterruptFunction - The function to call if ctrl-c is pressed.
 
40
static void (*InterruptFunction)() = 0;
 
41
 
 
42
static std::vector<sys::Path> *FilesToRemove = 0;
 
43
static std::vector<std::pair<void(*)(void*), void*> > *CallBacksToRun = 0;
 
44
 
 
45
// IntSigs - Signals that may interrupt the program at any time.
 
46
static const int IntSigs[] = {
 
47
  SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
 
48
};
 
49
static const int *const IntSigsEnd =
 
50
  IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]);
 
51
 
 
52
// KillSigs - Signals that are synchronous with the program that will cause it
 
53
// to die.
 
54
static const int KillSigs[] = {
 
55
  SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV
 
56
#ifdef SIGSYS
 
57
  , SIGSYS
 
58
#endif
 
59
#ifdef SIGXCPU
 
60
  , SIGXCPU
 
61
#endif
 
62
#ifdef SIGXFSZ
 
63
  , SIGXFSZ
 
64
#endif
 
65
#ifdef SIGEMT
 
66
  , SIGEMT
 
67
#endif
 
68
};
 
69
static const int *const KillSigsEnd =
 
70
  KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]);
 
71
 
 
72
static unsigned NumRegisteredSignals = 0;
 
73
static struct {
 
74
  struct sigaction SA;
 
75
  int SigNo;
 
76
} RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];
 
77
 
 
78
 
 
79
static void RegisterHandler(int Signal) {
 
80
  assert(NumRegisteredSignals <
 
81
         sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&
 
82
         "Out of space for signal handlers!");
 
83
 
 
84
  struct sigaction NewHandler;
 
85
  
 
86
  NewHandler.sa_handler = SignalHandler;
 
87
  NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
 
88
  sigemptyset(&NewHandler.sa_mask); 
 
89
  
 
90
  // Install the new handler, save the old one in RegisteredSignalInfo.
 
91
  sigaction(Signal, &NewHandler,
 
92
            &RegisteredSignalInfo[NumRegisteredSignals].SA);
 
93
  RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
 
94
  ++NumRegisteredSignals;
 
95
}
 
96
 
 
97
static void RegisterHandlers() {
 
98
  // If the handlers are already registered, we're done.
 
99
  if (NumRegisteredSignals != 0) return;
 
100
 
 
101
  std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
 
102
  std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
 
103
}
 
104
 
 
105
static void UnregisterHandlers() {
 
106
  // Restore all of the signal handlers to how they were before we showed up.
 
107
  for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
 
108
    sigaction(RegisteredSignalInfo[i].SigNo,
 
109
              &RegisteredSignalInfo[i].SA, 0);
 
110
  NumRegisteredSignals = 0;
 
111
}
 
112
 
 
113
 
 
114
 
 
115
// SignalHandler - The signal handler that runs.
 
116
static RETSIGTYPE SignalHandler(int Sig) {
 
117
  // Restore the signal behavior to default, so that the program actually
 
118
  // crashes when we return and the signal reissues.  This also ensures that if
 
119
  // we crash in our signal handler that the program will terminate immediately
 
120
  // instead of recursing in the signal handler.
 
121
  UnregisterHandlers();
 
122
 
 
123
  // Unmask all potentially blocked kill signals.
 
124
  sigset_t SigMask;
 
125
  sigfillset(&SigMask);
 
126
  sigprocmask(SIG_UNBLOCK, &SigMask, 0);
 
127
 
 
128
  SignalsMutex.acquire();
 
129
  if (FilesToRemove != 0)
 
130
    while (!FilesToRemove->empty()) {
 
131
      FilesToRemove->back().eraseFromDisk(true);
 
132
      FilesToRemove->pop_back();
 
133
    }
 
134
 
 
135
  if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
 
136
    if (InterruptFunction) {
 
137
      void (*IF)() = InterruptFunction;
 
138
      SignalsMutex.release();
 
139
      InterruptFunction = 0;
 
140
      IF();        // run the interrupt function.
 
141
      return;
 
142
    }
 
143
    
 
144
    SignalsMutex.release();
 
145
    raise(Sig);   // Execute the default handler.
 
146
    return;
 
147
  }
 
148
 
 
149
  SignalsMutex.release();
 
150
 
 
151
  // Otherwise if it is a fault (like SEGV) run any handler.
 
152
  if (CallBacksToRun)
 
153
    for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i)
 
154
      (*CallBacksToRun)[i].first((*CallBacksToRun)[i].second);
 
155
}
 
156
 
 
157
 
 
158
 
 
159
void llvm::sys::SetInterruptFunction(void (*IF)()) {
 
160
  SignalsMutex.acquire();
 
161
  InterruptFunction = IF;
 
162
  SignalsMutex.release();
 
163
  RegisterHandlers();
 
164
}
 
165
 
 
166
// RemoveFileOnSignal - The public API
 
167
bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename,
 
168
                                   std::string* ErrMsg) {
 
169
  SignalsMutex.acquire();
 
170
  if (FilesToRemove == 0)
 
171
    FilesToRemove = new std::vector<sys::Path>();
 
172
 
 
173
  FilesToRemove->push_back(Filename);
 
174
 
 
175
  SignalsMutex.release();
 
176
 
 
177
  RegisterHandlers();
 
178
  return false;
 
179
}
 
180
 
 
181
/// AddSignalHandler - Add a function to be called when a signal is delivered
 
182
/// to the process.  The handler can have a cookie passed to it to identify
 
183
/// what instance of the handler it is.
 
184
void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
 
185
  if (CallBacksToRun == 0)
 
186
    CallBacksToRun = new std::vector<std::pair<void(*)(void*), void*> >();
 
187
  CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
 
188
  RegisterHandlers();
 
189
}
 
190
 
 
191
 
 
192
// PrintStackTrace - In the case of a program crash or fault, print out a stack
 
193
// trace so that the user has an indication of why and where we died.
 
194
//
 
195
// On glibc systems we have the 'backtrace' function, which works nicely, but
 
196
// doesn't demangle symbols.  
 
197
static void PrintStackTrace(void *) {
 
198
#ifdef HAVE_BACKTRACE
 
199
  static void* StackTrace[256];
 
200
  // Use backtrace() to output a backtrace on Linux systems with glibc.
 
201
  int depth = backtrace(StackTrace,
 
202
                        static_cast<int>(array_lengthof(StackTrace)));
 
203
#if HAVE_DLFCN_H && __GNUG__ && 0
 
204
/* CLAMAV LOCAL: turn off dladdr() because we don't link -ldl */
 
205
  int width = 0;
 
206
  for (int i = 0; i < depth; ++i) {
 
207
    Dl_info dlinfo;
 
208
    dladdr(StackTrace[i], &dlinfo);
 
209
    const char* name = strrchr(dlinfo.dli_fname, '/');
 
210
 
 
211
    int nwidth;
 
212
    if (name == NULL) nwidth = strlen(dlinfo.dli_fname);
 
213
    else              nwidth = strlen(name) - 1;
 
214
 
 
215
    if (nwidth > width) width = nwidth;
 
216
  }
 
217
 
 
218
  for (int i = 0; i < depth; ++i) {
 
219
    Dl_info dlinfo;
 
220
    dladdr(StackTrace[i], &dlinfo);
 
221
 
 
222
    fprintf(stderr, "%-2d", i);
 
223
 
 
224
    const char* name = strrchr(dlinfo.dli_fname, '/');
 
225
    if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname);
 
226
    else              fprintf(stderr, " %-*s", width, name+1);
 
227
 
 
228
    fprintf(stderr, " %#0*lx",
 
229
            (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);
 
230
 
 
231
    if (dlinfo.dli_sname != NULL) {
 
232
      int res;
 
233
      fputc(' ', stderr);
 
234
      char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res);
 
235
      if (d == NULL) fputs(dlinfo.dli_sname, stderr);
 
236
      else           fputs(d, stderr);
 
237
      free(d);
 
238
 
 
239
      fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr);
 
240
    }
 
241
    fputc('\n', stderr);
 
242
  }
 
243
#else
 
244
  backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
 
245
#endif
 
246
#endif
 
247
}
 
248
 
 
249
/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
 
250
/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
 
251
void llvm::sys::PrintStackTraceOnErrorSignal() {
 
252
  AddSignalHandler(PrintStackTrace, 0);
 
253
}
 
254