Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SignalHandler.cpp
Go to the documentation of this file.
1#include "SignalHandler.h"
2
3#include <csignal>
4#include <iostream>
5
6namespace storm {
7namespace utility {
8namespace resources {
9
10// Maximal waiting time after abort signal before terminating
12
13SignalInformation::SignalInformation() : terminate(false), lastSignal(0) {}
14
15SignalInformation::~SignalInformation() {
16 // Intentionally left empty.
17}
18
19SignalInformation& SignalInformation::infos() {
20 static SignalInformation signalInfo;
21 return signalInfo;
22}
23
32void signalHandler(int signal) {
33 if (!isTerminate()) {
34 // First time we get an abort signal
35 // We give the program a number of seconds to print results obtained so far before termination
36 std::cerr << "ERROR: The program received signal " << signal << " and will be aborted in " << maxWaitTime << "s.\n";
37 SignalInformation::infos().setTerminate(true);
38 // Remember original signal such that the program returns the correct original signal
39 SignalInformation::infos().setErrorCode(signal);
40 // Trigger a new signal after waitTime
42 } else {
43 // Second time we get a signal
44 // We now definitely have to terminate as fast as possible
45 if (SignalInformation::infos().getErrorCode() == SIGXCPU) {
46 std::cerr << "TIMEOUT.\n";
47 } else if (SignalInformation::infos().getErrorCode() == ENOMEM) {
48 std::cerr << "OUT OF MEMORY.\n";
49 } else if (SignalInformation::infos().getErrorCode() == SIGABRT || SignalInformation::infos().getErrorCode() == SIGINT) {
50 std::cerr << "ABORT.\n";
51 } else {
52 std::cerr << "Received signal " << signal << '\n';
53 }
54 quickest_exit(SignalInformation::infos().getErrorCode());
55 }
56}
57
58void installSignalHandler(int maximalWaitTime) {
59 // Set the waiting time
60 maxWaitTime = maximalWaitTime;
61
62 // We use the newer sigaction instead of signal
63 struct sigaction sa;
64 sa.sa_handler = signalHandler;
65 sigemptyset(&sa.sa_mask);
66 sa.sa_flags = SA_RESTART;
67
68 // CPU Limit
69 if (sigaction(SIGXCPU, &sa, nullptr) == -1) {
70 std::cerr << "FATAL: Installing a signal handler failed.\n";
71 }
72 // Memory out
73 if (sigaction(ENOMEM, &sa, nullptr) == -1) {
74 std::cerr << "FATAL: Installing a signal handler failed.\n";
75 }
76 if (sigaction(SIGSEGV, &sa, nullptr) == -1) {
77 std::cerr << "FATAL: Installing a signal handler failed.\n";
78 }
79 if (sigaction(SIGABRT, &sa, nullptr) == -1) {
80 std::cerr << "FATAL: Installing a signal handler failed.\n";
81 }
82 if (sigaction(SIGINT, &sa, nullptr) == -1) {
83 std::cerr << "FATAL: Installing a signal handler failed.\n";
84 }
85 if (sigaction(SIGTERM, &sa, nullptr) == -1) {
86 std::cerr << "FATAL: Installing a signal handler failed.\n";
87 }
88 if (sigaction(SIGALRM, &sa, nullptr) == -1) {
89 std::cerr << "FATAL: Installing a signal handler failed.\n";
90 }
91}
92
93} // namespace resources
94} // namespace utility
95} // namespace storm
void quickest_exit(int errorCode)
Exit without cleanup.
void setTimeoutAlarm(uint_fast64_t timeout)
Set timeout by raising an alarm after timeout seconds.
void signalHandler(int signal)
Signal handler for aborts, etc.
bool isTerminate()
Check whether the program should terminate (due to some abort signal).
void installSignalHandler(int maximalWaitTime)
Register some signal handlers to detect and correctly handle abortion (due to timeout for example).
LabParser.cpp.
Definition cli.cpp:18