-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcatch.c
160 lines (142 loc) · 3.62 KB
/
catch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "catch.h"
#include "message.h"
#include "ruler.h"
#include "statistics.h"
#include <assert.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*------------------------------------------------------------------------*/
static atomic_int caught_signal;
static volatile struct ruler *one_global_ruler;
static atomic_bool catching_signals;
static atomic_bool catching_alarm;
/*------------------------------------------------------------------------*/
#define SIGNALS \
SIGNAL (SIGABRT) \
SIGNAL (SIGBUS) \
SIGNAL (SIGILL) \
SIGNAL (SIGINT) \
SIGNAL (SIGSEGV) \
SIGNAL (SIGTERM)
// clang-format off
// Saved previous signal handlers.
#define SIGNAL(SIG) \
static void (*saved_ ## SIG ## _handler)(int);
SIGNALS
#undef SIGNAL
static void (*saved_SIGALRM_handler)(int);
// clang-format on
static void reset_alarm_handler (void) {
if (atomic_exchange (&catching_alarm, false))
signal (SIGALRM, saved_SIGALRM_handler);
}
void reset_signal_handlers (void) {
one_global_ruler = 0;
if (atomic_exchange (&catching_signals, false)) {
// clang-format off
#define SIGNAL(SIG) \
signal (SIG, saved_ ## SIG ## _handler);
SIGNALS
#undef SIGNAL
// clang-format on
}
reset_alarm_handler ();
}
static void caught_message (int sig) {
if (verbosity < 0)
return;
const char *name = "SIGNUNKNOWN";
#define SIGNAL(SIG) \
if (sig == SIG) \
name = #SIG;
SIGNALS
#undef SIGNAL
if (sig == SIGALRM)
name = "SIGALRM";
char buffer[80];
sprintf (buffer, "c\nc caught signal %d (%s)\nc\n", sig, name);
size_t bytes = strlen (buffer);
if (write (1, buffer, bytes) != bytes)
exit (2);
}
static void raising_message (int sig) {
if (verbosity < 0)
return;
const char *name = "SIGNUNKNOWN";
#define SIGNAL(SIG) \
if (sig == SIG) \
name = #SIG;
SIGNALS
#undef SIGNAL
if (sig == SIGALRM)
name = "SIGALRM";
char buffer[80];
sprintf (buffer,
"c\nc raising signal %d (%s) after reporting statistics\n", sig,
name);
size_t bytes = strlen (buffer);
if (write (1, buffer, bytes) != bytes)
exit (2);
}
static void exit_message (void) {
const char *message = "c calling 'exit (1)' as raising signal returned\n";
size_t bytes = strlen (message);
if (write (1, message, bytes) != bytes)
exit (2);
}
static void catch_signal (int sig) {
if (atomic_exchange (&caught_signal, sig))
return;
caught_message (sig);
#ifndef QUIET
struct ruler *ruler = (struct ruler *) one_global_ruler;
#endif
reset_signal_handlers ();
#ifndef QUIET
if (ruler)
print_ruler_statistics (ruler);
#endif
raising_message (sig);
raise (sig);
exit_message ();
exit (1);
}
static void catch_alarm (int sig) {
assert (sig == SIGALRM);
if (!catching_alarm)
catch_signal (sig);
if (atomic_exchange (&caught_signal, sig))
return;
if (verbosity > 0)
caught_message (sig);
reset_alarm_handler ();
assert (one_global_ruler);
set_terminate ((struct ruler *) one_global_ruler, 0);
caught_signal = 0;
}
static void set_alarm_handler (unsigned seconds) {
assert (seconds);
assert (!catching_alarm);
saved_SIGALRM_handler = signal (SIGALRM, catch_alarm);
alarm (seconds);
catching_alarm = true;
}
void set_signal_handlers (struct ruler *ruler) {
unsigned seconds = ruler->options.seconds;
one_global_ruler = ruler;
assert (!catching_signals);
// clang-format off
#define SIGNAL(SIG) \
saved_ ## SIG ##_handler = signal (SIG, catch_signal);
SIGNALS
#undef SIGNAL
// clang-format on
catching_signals = true;
if (seconds)
set_alarm_handler (seconds);
}