ATLAS Offline Software
Loading...
Searching...
No Matches
SealSignal.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
16
17#ifndef CXXUTILS_SEAL_SIGNAL_H // wlav SEAL_BASE_SIGNAL_H
18#define CXXUTILS_SEAL_SIGNAL_H // wlav SEAL_BASE_SIGNAL_H
19
21#include "CxxUtils/SealCommon.h"
22//# include "SealBase/sysapi/IOTypes.h" wlav
23
24// These should be hidden, but we can't do that for now: the clients
25// must be able to operate on sigset_t and pid_t. Note that we do not
26// want to have <csignal> -- we need all the extra POSIX stuff.
27# include <signal.h>
28# include <sys/types.h>
29# include <climits>
30#include <atomic>
31
32
33// Hacks for fields we might not have in siginfo_t. Just print zero.
34#ifdef si_utime // sss
35# define HAVE_SIGINFO_SI_UTIME 1 // sss
36#endif // sss
37#ifdef si_int // sss
38# define HAVE_SIGINFO_SI_INT 1 // sss
39#endif // sss
40# if !HAVE_SIGINFO_SI_UTIME // darwin
41# define si_utime si_signo ? 0 : 0
42# define si_stime si_signo ? 0 : 0
43# endif
44# if !HAVE_SIGINFO_SI_INT
45# if HAVE_SIGINFO_SI_VALUE // darwin
46# define si_int si_value.sigval_int
47# define si_ptr si_value.sigval_ptr
48# else // (none known)
49# define si_int si_signo ? 0 : 0
50# define si_ptr si_signo ? (void *) 0 : (void *) 0
51# endif
52# endif
53
54
55# if !HAVE_POSIX_SIGNALS
56// Forward declare POSIX signal handling stuff for platforms that
57// don't have them. This allows them to be mentioned in the Signal
58// interface and minimally used in the clients. Special kludge for
59// Windows.
60# ifdef _WIN32
61typedef struct _EXCEPTION_RECORD siginfo_t;
62#define SIGHUP 1 /* Hangup (POSIX). */
63#define SIGQUIT 3 /* Quit (POSIX). */
64#define SIGTRAP 5 /* Trace trap (POSIX). */
65#define SIGKILL 9 /* Kill, unblockable (POSIX). */
66#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */
67#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */
68#define SIGPIPE 13 /* Broken pipe (POSIX). */
69#define SIGALRM 14 /* Alarm clock (POSIX). */
70#define SIGCHLD 17 /* Child status has changed (POSIX). */
71#define SIGCONT 18 /* Continue (POSIX). */
72#define SIGSTOP 19 /* Stop, unblockable (POSIX). */
73#define SIGTSTP 20 /* Keyboard stop (POSIX). */
74#define SIGTTIN 21 /* Background read from tty (POSIX). */
75#define SIGTTOU 22 /* Background write to tty (POSIX). */
76# else
77struct siginfo_t {};
78# endif
79
80typedef int sigset_t;
81
82# define sigemptyset(x) (0)
83# define sigfillset(x) (0)
84# define sigaddset(x,y) (0)
85# define sigdelset(x,y) (0)
86# define sigismember(x,y) (0)
87# endif
88
89//namespace seal { wlav
90namespace Athena { // wlav
91
101{
102public:
111 static const int USR1_DUMP_CORE = 1;
112
115 static const int FATAL_ON_QUIT = 2;
116
119 static const int FATAL_ON_INT = 4;
120
122 static const int FATAL_DUMP_CORE = 8;
123
126 static const int FATAL_DUMP_SIG = 16;
127
130 static const int FATAL_DUMP_STACK = 32;
131
134 static const int FATAL_DUMP_LIBS = 64;
135
138 static const int FATAL_DUMP_CONTEXT = 128;
139
142 static const int FATAL_AUTO_EXIT = 256;
143
145 static const int FATAL_DEFAULT = (USR1_DUMP_CORE
153
165 typedef bool (*QuitHook) (int sig, siginfo_t *info, void *x);
166
176 typedef bool (*FatalHook) (int sig, siginfo_t *info, void *x);
177
182 typedef void (*FatalReturn) (int sig, siginfo_t *info, void *x);
183
196 typedef void (*HandlerType) (int sig, siginfo_t *info, void *extra);
197
198
199 // Generic signal operations
200 // - Signal names
201 static const char * name (int sig);
202
203 // - Signal handlers and masks
204 static HandlerType handler (int sig, sigset_t *mask = 0);
205 static HandlerType handle (int sig, HandlerType handler,
206 const sigset_t *blockMask = 0);
207 static void revert (int sig);
208 static void ignore (int sig);
209
210 static void block (int sig, bool sense);
211 static void block (const sigset_t *mask, bool sense);
212 static void mask (const sigset_t *mask, sigset_t *old = 0);
213
214 // - Sending and receiving signals
215 static int raise (int sig);
216 static int kill (pid_t process, int sig);
217 static int queue (int sig, int value = 0);
218 static int queue (int sig, void *value);
219 static int queue (pid_t process, int sig, int value = 0);
220 static int queue (pid_t process, int sig, void *value);
221
222 static bool pending (int sig);
223 static void pending (sigset_t *mask);
224 static void suspend (const sigset_t *mask);
225 static bool wait (int sig,
226 siginfo_t *info = 0,
227 long msecs = -1);
228 static int wait (const sigset_t *mask,
229 siginfo_t *info = 0,
230 long msecs = -1);
231
232 // Assisted handling of program termination signals
233 static void handleQuit ATLAS_NOT_THREAD_SAFE (QuitHook hook = 0);
234 static QuitHook handleQuitHook ATLAS_NOT_THREAD_SAFE (void);
235
236 static void quit ATLAS_NOT_THREAD_SAFE (int sig, siginfo_t *info, void *x);
237
238 // Assisted handling of fatal signals
239 static void handleFatal ATLAS_NOT_THREAD_SAFE (const char *applicationName = 0,
240 IOFD fd = IOFD_INVALID,
241 FatalHook hook = 0,
242 FatalReturn mainreturn = 0,
243 unsigned options = FATAL_DEFAULT);
244 static IOFD handleFatalFd ATLAS_NOT_THREAD_SAFE (void);
245 static FatalHook handleFatalHook ATLAS_NOT_THREAD_SAFE (void);
246 static FatalReturn handleFatalReturn ATLAS_NOT_THREAD_SAFE (void);
247 static unsigned handleFatalOptions ATLAS_NOT_THREAD_SAFE (void);
248
249 static void fatal ATLAS_NOT_THREAD_SAFE (int sig, siginfo_t *info, void *x);
250 static bool fatalDump ATLAS_NOT_THREAD_SAFE (int sig, siginfo_t *info, void *x,
251 IOFD fd,
252 unsigned options);
253 static bool fatalDump ATLAS_NOT_THREAD_SAFE (int sig, siginfo_t *info, void *x);
254 static int fatalLevel ATLAS_NOT_THREAD_SAFE (void);
255 static bool crashed ATLAS_NOT_THREAD_SAFE (void);
256
257 static void dumpInfo (IOFD fd, char *buf,
258 unsigned int buf_size,
259 int sig,
260 const siginfo_t *info);
261 static void dumpMemory (IOFD fd, char *buf,
262 unsigned int buf_size,
263 const void *data, size_t n);
264 static unsigned long dumpContext (IOFD fd, char *buf,
265 unsigned int buf_size,
266 const void *context);
267
268 static const char * describe (int sig, int code);
269
270private:
271 static void trampoline (int sig);
272
273 static bool s_crashed;
274 static int s_inFatal;
275 static std::atomic<unsigned long> s_lastSP;
276 static const char *s_applicationName;
280 static unsigned s_fatalOptions;
282#if !HAVE_POSIX_SIGNALS
284#endif
285};
286
287
288//} // namespace seal wlav
289} // namespace Athena wlav
290
291
292extern "C" {
295 void CxxUtils_installFatalHandler ATLAS_NOT_THREAD_SAFE ();
296}
297
298
299#endif // CXXUTILS_SEAL_SIGNAL_H wlav SEAL_BASE_SIGNAL_H
int32_t pid_t
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
Collecting a few shared bits and pieces from SEAL headers.
#define IOFD_INVALID
Invalid channel descriptor constant.
Definition SealCommon.h:20
int IOFD
Type the system uses for channel descriptors.
Definition SealCommon.h:27
void CxxUtils_installFatalHandler ATLAS_NOT_THREAD_SAFE()
Install fatal handler with default options.
int sigset_t
Definition SealSignal.h:80
#define x
Define macros for attributes used to control the static checker.
Utilities for handling signals and fatal errors.
Definition SealSignal.h:101
static QuitHook handleQuitHook ATLAS_NOT_THREAD_SAFE(void)
static HandlerType handler(int sig, sigset_t *mask=0)
Return the current handler for signal number sig and its blocked signals in mask (if non-null).
static FatalHook handleFatalHook ATLAS_NOT_THREAD_SAFE(void)
static unsigned s_fatalOptions
The current fatal signal handling options.
Definition SealSignal.h:280
static int raise(int sig)
Raise the signal number sig.
static FatalReturn handleFatalReturn ATLAS_NOT_THREAD_SAFE(void)
static const int FATAL_DUMP_CONTEXT
Option to make fataldump(int, siginfo_t *, void *) (invoked by fatal(int, siginfo_t *,...
Definition SealSignal.h:138
bool(* QuitHook)(int sig, siginfo_t *info, void *x)
Application clean-up hook invoked before quit(int , siginfo_t *, void *) exits from program terminati...
Definition SealSignal.h:165
static const char * name(int sig)
Return the name of the signal number sig.
static const int FATAL_DEFAULT
Default options to handleFatal(const char *, IOFD fd, FatalHook, FatalReturn, unsigned.
Definition SealSignal.h:145
static unsigned long dumpContext(IOFD fd, char *buf, unsigned int buf_size, const void *context)
Utility function to dump the process context, as obtained for instance through signal handler paramet...
static void suspend(const sigset_t *mask)
Temporarily replace the signal mask of the process with mask and then suspend until a signal is recei...
static const int FATAL_AUTO_EXIT
Option to make fatal(int, siginfo_t *, void *) exit via quit(int , siginfo_t *, void *).
Definition SealSignal.h:142
static int kill(pid_t process, int sig)
Send the signal sig to process identified by process.
static void handleQuit ATLAS_NOT_THREAD_SAFE(QuitHook hook=0)
static IOFD s_fatalFd
The output file descriptor for fataldump().
Definition SealSignal.h:277
static int queue(int sig, int value=0)
Queue signal sig for this process with additional data value.
static bool fatalDump ATLAS_NOT_THREAD_SAFE(int sig, siginfo_t *info, void *x)
static void quit ATLAS_NOT_THREAD_SAFE(int sig, siginfo_t *info, void *x)
static void fatal ATLAS_NOT_THREAD_SAFE(int sig, siginfo_t *info, void *x)
static const char * describe(int sig, int code)
Return the description for signal info code code for signal number sig.
static const int FATAL_ON_QUIT
Option to make SIGHUP, SIGTERM and SIGQUIT fatal instead of just quit(int , siginfo_t *,...
Definition SealSignal.h:115
static bool crashed ATLAS_NOT_THREAD_SAFE(void)
static FatalReturn s_fatalReturn
The application main return hook for fatal signals.
Definition SealSignal.h:279
bool(* FatalHook)(int sig, siginfo_t *info, void *x)
Application hook to run in fatal().
Definition SealSignal.h:176
static void mask(const sigset_t *mask, sigset_t *old=0)
Set the list of currently blocked signals to mask and return the old setting in old (if non-null).
static void dumpMemory(IOFD fd, char *buf, unsigned int buf_size, const void *data, size_t n)
Utility function to dump memory section from data for n bytes.
static IOFD handleFatalFd ATLAS_NOT_THREAD_SAFE(void)
static const char * s_applicationName
The current application name.
Definition SealSignal.h:276
static bool fatalDump ATLAS_NOT_THREAD_SAFE(int sig, siginfo_t *info, void *x, IOFD fd, unsigned options)
static int s_inFatal
Indicator that we are currently executing inside fatal().
Definition SealSignal.h:274
static bool wait(int sig, siginfo_t *info=0, long msecs=-1)
Suspend the thread waiting for signal sig at most msecs milliseconds.
static void block(int sig, bool sense)
Block or unblock the signal number sig.
static FatalHook s_fatalHook
The application handler hook for fatal signals.
Definition SealSignal.h:278
static const int FATAL_DUMP_CORE
Option to make fatal(int, siginfo_t *, void *) dump a core file before crashing.
Definition SealSignal.h:122
static std::atomic< unsigned long > s_lastSP
Used to switch to a raw stack dump if we crash during a backtrace.
Definition SealSignal.h:275
static void dumpInfo(IOFD fd, char *buf, unsigned int buf_size, int sig, const siginfo_t *info)
Utility function to dump the signal info descriptor for signal sig, as obtained for instance through ...
static unsigned handleFatalOptions ATLAS_NOT_THREAD_SAFE(void)
static void ignore(int sig)
Ignore the signal number sig.
static void revert(int sig)
Revert the signal number sig back to its default behaviour.
static bool s_crashed
Indicator that the application has been crashed: that a fatal signal has been delivered.
Definition SealSignal.h:273
static bool pending(int sig)
Check if sig is pending for this process.
static const int FATAL_DUMP_LIBS
Option to make fataldump(int sig, siginfo_t *, void *) (invoked by fatal(int, siginfo_t *,...
Definition SealSignal.h:134
static QuitHook s_quitHook
The application handler hook for quitting-related signals.
Definition SealSignal.h:281
static const int USR1_DUMP_CORE
Option that instructs fatal(int, siginfo_t *, void *) to call coredump() on SIGUSR1.
Definition SealSignal.h:111
static void trampoline(int sig)
Internal signal handler trampoline to convert handler arguments to look more like POSIX signals.
static int fatalLevel ATLAS_NOT_THREAD_SAFE(void)
static const int FATAL_DUMP_SIG
Option to make fataldump(int, siginfo_t *, void *) (invoked by fatal(int, siginfo_t *,...
Definition SealSignal.h:126
void(* FatalReturn)(int sig, siginfo_t *info, void *x)
Application hook to jump back to the main program from a fatal signal, for example using siglongjmp.
Definition SealSignal.h:182
static HandlerType handle(int sig, HandlerType handler, const sigset_t *blockMask=0)
Install a new signal handler handler for signal number sig and returns the old handler.
static const int FATAL_DUMP_STACK
Option to make fataldump(int, siginfo_t *, void *) (invoked by fatal(int, siginfo_t *,...
Definition SealSignal.h:130
static HandlerType s_trampolines[NSIG]
Actual signal handlers when POSIX signals are not available.
Definition SealSignal.h:283
static void handleFatal ATLAS_NOT_THREAD_SAFE(const char *applicationName=0, IOFD fd=IOFD_INVALID, FatalHook hook=0, FatalReturn mainreturn=0, unsigned options=FATAL_DEFAULT)
void(* HandlerType)(int sig, siginfo_t *info, void *extra)
Signal handler type.
Definition SealSignal.h:196
static const int FATAL_ON_INT
Option to make SIGINT fatal.
Definition SealSignal.h:119
const std::string process
Some weak symbol referencing magic... These are declared in AthenaKernel/getMessageSvc....
static char buf[SIGNAL_MESSAGE_BUFSIZE]
Dump application state information on a fatal signal.