ATLAS Offline Software
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 
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
61 typedef 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
77 struct siginfo_t {};
78 # endif
79 
80 typedef 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
90 namespace Athena { // wlav
91 
100 class Signal
101 {
102 public:
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
146  | FATAL_ON_INT
152  | FATAL_AUTO_EXIT);
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);
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 
270 private:
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;
277  static IOFD s_fatalFd;
280  static unsigned s_fatalOptions;
282 #if !HAVE_POSIX_SIGNALS
283  static HandlerType s_trampolines [NSIG];
284 #endif
285 };
286 
287 
288 //} // namespace seal wlav
289 } // namespace Athena wlav
290 
291 
292 extern "C" {
295  void CxxUtils_installFatalHandler ATLAS_NOT_THREAD_SAFE ();
296 }
297 
298 
299 #endif // CXXUTILS_SEAL_SIGNAL_H wlav SEAL_BASE_SIGNAL_H
grepfile.info
info
Definition: grepfile.py:38
Athena::Signal::ATLAS_NOT_THREAD_SAFE
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)
pid_t
int32_t pid_t
Definition: FPGATrackSimTypes.h:19
Athena::Signal::s_applicationName
static const char * s_applicationName
The current application name.
Definition: SealSignal.h:276
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Athena::Signal::s_fatalFd
static IOFD s_fatalFd
The output file descriptor for #fataldump().
Definition: SealSignal.h:277
Athena::Signal::FATAL_DUMP_STACK
static const int FATAL_DUMP_STACK
Option to make #fataldump(int, siginfo_t *, void *) (invoked by #fatal(int, siginfo_t *,...
Definition: SealSignal.h:130
Athena::Signal::suspend
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...
Definition: SealSignal.cxx:500
siginfo_t
Definition: SealSignal.h:77
Athena::Signal::s_crashed
static bool s_crashed
Indicator that the application has been crashed: that a fatal signal has been delivered.
Definition: SealSignal.h:273
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static IOFD handleFatalFd ATLAS_NOT_THREAD_SAFE(void)
Athena::Signal::s_fatalOptions
static unsigned s_fatalOptions
The current fatal signal handling options.
Definition: SealSignal.h:280
Athena::Signal::FATAL_DUMP_CORE
static const int FATAL_DUMP_CORE
Option to make #fatal(int, siginfo_t *, void *) dump a core file before crashing.
Definition: SealSignal.h:122
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static bool crashed ATLAS_NOT_THREAD_SAFE(void)
SealCommon.h
Collecting a few shared bits and pieces from SEAL headers.
Athena::Signal::FATAL_DEFAULT
static const int FATAL_DEFAULT
Default options to #handleFatal(const char *, IOFD fd, FatalHook, FatalReturn, unsigned.
Definition: SealSignal.h:145
athena.value
value
Definition: athena.py:124
Athena::Signal::dumpMemory
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.
Definition: SealSignal.cxx:1185
Athena::Signal::dumpContext
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...
Definition: SealSignal.cxx:1206
Athena::Signal::handler
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).
Definition: SealSignal.cxx:236
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static FatalHook handleFatalHook ATLAS_NOT_THREAD_SAFE(void)
Athena::Signal
Utilities for handling signals and fatal errors.
Definition: SealSignal.h:101
x
#define x
SUSY_SimplifiedModel_PostInclude.process
string process
Definition: SUSY_SimplifiedModel_PostInclude.py:42
Athena::Signal::FATAL_ON_INT
static const int FATAL_ON_INT
Option to make SIGINT fatal.
Definition: SealSignal.h:119
Athena::Signal::USR1_DUMP_CORE
static const int USR1_DUMP_CORE
Option that instructs #fatal(int, siginfo_t *, void *) to call #coredump() on SIGUSR1.
Definition: SealSignal.h:111
sigset_t
int sigset_t
Definition: SealSignal.h:80
Athena::Signal::FATAL_DUMP_SIG
static const int FATAL_DUMP_SIG
Option to make #fataldump(int, siginfo_t *, void *) (invoked by #fatal(int, siginfo_t *,...
Definition: SealSignal.h:126
histSizes.code
code
Definition: histSizes.py:129
Athena::Signal::mask
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).
Definition: SealSignal.cxx:365
ATLAS_NOT_THREAD_SAFE
void CxxUtils_installFatalHandler ATLAS_NOT_THREAD_SAFE()
Install fatal handler with default options.
Definition: IOVDbTestAlg.cxx:124
Athena
Some weak symbol referencing magic...
Definition: AthLegacySequence.h:21
Athena::Signal::wait
static bool wait(int sig, siginfo_t *info=0, long msecs=-1)
Suspend the thread waiting for signal sig at most msecs milliseconds.
Definition: SealSignal.cxx:515
beamspotman.n
n
Definition: beamspotman.py:731
Athena::Signal::ignore
static void ignore(int sig)
Ignore the signal number sig.
Definition: SealSignal.cxx:331
IOFD
int IOFD
Type the system uses for channel descriptors.
Definition: SealCommon.h:27
Athena::Signal::s_quitHook
static QuitHook s_quitHook
The application handler hook for quitting-related signals.
Definition: SealSignal.h:281
Athena::Signal::FatalReturn
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
python.BuildSignatureFlags.sig
sig
Definition: BuildSignatureFlags.py:218
Athena::Signal::HandlerType
void(* HandlerType)(int sig, siginfo_t *info, void *extra)
Signal handler type.
Definition: SealSignal.h:196
Athena::Signal::FATAL_DUMP_LIBS
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
Athena::Signal::queue
static int queue(int sig, int value=0)
Queue signal sig for this process with additional data value.
Definition: SealSignal.cxx:446
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
Athena::Signal::FATAL_DUMP_CONTEXT
static const int FATAL_DUMP_CONTEXT
Option to make #fataldump(int, siginfo_t *, void *) (invoked by #fatal(int, siginfo_t *,...
Definition: SealSignal.h:138
Athena::Signal::name
static const char * name(int sig)
Return the name of the signal number sig.
Definition: SealSignal.cxx:216
python.handimod.extra
int extra
Definition: handimod.py:522
Athena::Signal::FATAL_AUTO_EXIT
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
ReadFromCoolCompare.fd
fd
Definition: ReadFromCoolCompare.py:196
Athena::Signal::s_lastSP
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
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static bool fatalDump ATLAS_NOT_THREAD_SAFE(int sig, siginfo_t *info, void *x, IOFD fd, unsigned options)
Athena::Signal::FATAL_ON_QUIT
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
IOFD_INVALID
#define IOFD_INVALID
Invalid channel descriptor constant.
Definition: SealCommon.h:20
Athena::Signal::trampoline
static void trampoline(int sig)
Internal signal handler trampoline to convert handler arguments to look more like POSIX signals.
Definition: SealSignal.cxx:203
Athena::Signal::s_fatalHook
static FatalHook s_fatalHook
The application handler hook for fatal signals.
Definition: SealSignal.h:278
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static void handleQuit ATLAS_NOT_THREAD_SAFE(QuitHook hook=0)
Athena::Signal::block
static void block(int sig, bool sense)
Block or unblock the signal number sig.
Definition: SealSignal.cxx:338
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static bool fatalDump ATLAS_NOT_THREAD_SAFE(int sig, siginfo_t *info, void *x)
Athena::Signal::describe
static const char * describe(int sig, int code)
Return the description for signal info code code for signal number sig.
Definition: SealSignal.cxx:968
Athena::Signal::FatalHook
bool(* FatalHook)(int sig, siginfo_t *info, void *x)
Application hook to run in fatal().
Definition: SealSignal.h:176
Athena::Signal::s_fatalReturn
static FatalReturn s_fatalReturn
The application main return hook for fatal signals.
Definition: SealSignal.h:279
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static unsigned handleFatalOptions ATLAS_NOT_THREAD_SAFE(void)
CSV_InDetExporter.old
old
Definition: CSV_InDetExporter.py:145
Athena::Signal::handle
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.
Definition: SealSignal.cxx:277
Athena::Signal::dumpInfo
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 ...
Definition: SealSignal.cxx:1084
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static void quit ATLAS_NOT_THREAD_SAFE(int sig, siginfo_t *info, void *x)
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static FatalReturn handleFatalReturn ATLAS_NOT_THREAD_SAFE(void)
Athena::Signal::QuitHook
bool(* QuitHook)(int sig, siginfo_t *info, void *x)
Application clean-up hook invoked before #quit(int , siginfo_t *, void *) exits from program terminat...
Definition: SealSignal.h:165
Athena::Signal::revert
static void revert(int sig)
Revert the signal number sig back to its default behaviour.
Definition: SealSignal.cxx:326
checker_macros.h
Define macros for attributes used to control the static checker.
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
Athena::Signal::s_inFatal
static int s_inFatal
Indicator that we are currently executing inside #fatal().
Definition: SealSignal.h:274
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static QuitHook handleQuitHook ATLAS_NOT_THREAD_SAFE(void)
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static void fatal ATLAS_NOT_THREAD_SAFE(int sig, siginfo_t *info, void *x)
Athena::Signal::kill
static int kill(pid_t process, int sig)
Send the signal sig to process identified by process.
Definition: SealSignal.cxx:389
Athena::Signal::pending
static bool pending(int sig)
Check if sig is pending for this process.
Definition: SealSignal.cxx:471
Athena::Signal::s_trampolines
static HandlerType s_trampolines[NSIG]
Actual signal handlers when POSIX signals are not available.
Definition: SealSignal.h:283
Athena::Signal::ATLAS_NOT_THREAD_SAFE
static int fatalLevel ATLAS_NOT_THREAD_SAFE(void)