|
9 | 9 | \*******************************************************************/
|
10 | 10 |
|
11 | 11 | #include "signal_catcher.h"
|
| 12 | +#include "invariant.h" |
12 | 13 |
|
13 | 14 | #if defined(_WIN32)
|
14 |
| -#include <process.h> |
15 | 15 | #else
|
16 | 16 | #include <cstdlib>
|
17 |
| -#include <csignal> |
18 | 17 | #endif
|
19 | 18 |
|
20 |
| -#include <vector> |
21 |
| - |
22 | 19 | // Here we have an instance of an ugly global object.
|
23 | 20 | // It keeps track of any child processes that we'll kill
|
24 |
| -// when we are told to terminate. |
| 21 | +// when we are told to terminate. "No child" is indicated by '0'. |
25 | 22 |
|
26 | 23 | #ifdef _WIN32
|
27 | 24 | #else
|
28 |
| -std::vector<pid_t> pids_of_children; |
| 25 | +pid_t child_pid = 0; |
| 26 | + |
| 27 | +void register_child(pid_t pid) |
| 28 | +{ |
| 29 | + PRECONDITION(child_pid == 0); |
| 30 | + child_pid = pid; |
| 31 | +} |
| 32 | + |
| 33 | +void unregister_child() |
| 34 | +{ |
| 35 | + PRECONDITION(child_pid != 0); |
| 36 | + child_pid = 0; |
| 37 | +} |
29 | 38 | #endif
|
30 | 39 |
|
31 | 40 | void install_signal_catcher()
|
32 | 41 | {
|
33 |
| - #if defined(_WIN32) |
34 |
| - #else |
| 42 | +#if defined(_WIN32) |
| 43 | +#else |
35 | 44 | // declare act to deal with action on signal set
|
36 | 45 | // NOLINTNEXTLINE(readability/identifiers)
|
37 | 46 | static struct sigaction act;
|
38 | 47 |
|
39 |
| - act.sa_handler=signal_catcher; |
40 |
| - act.sa_flags=0; |
| 48 | + act.sa_handler = signal_catcher; |
| 49 | + act.sa_flags = 0; |
41 | 50 | sigfillset(&(act.sa_mask));
|
42 | 51 |
|
43 | 52 | // install signal handler
|
44 | 53 | sigaction(SIGTERM, &act, nullptr);
|
45 |
| - #endif |
| 54 | +#endif |
46 | 55 | }
|
47 | 56 |
|
48 | 57 | void remove_signal_catcher()
|
49 | 58 | {
|
50 |
| - #if defined(_WIN32) |
51 |
| - #else |
| 59 | +#if defined(_WIN32) |
| 60 | +#else |
52 | 61 | // declare act to deal with action on signal set
|
53 | 62 | // NOLINTNEXTLINE(readability/identifiers)
|
54 | 63 | static struct sigaction act;
|
55 | 64 |
|
56 |
| - act.sa_handler=SIG_DFL; |
57 |
| - act.sa_flags=0; |
| 65 | + act.sa_handler = SIG_DFL; |
| 66 | + act.sa_flags = 0; |
58 | 67 | sigfillset(&(act.sa_mask));
|
59 | 68 |
|
60 | 69 | sigaction(SIGTERM, &act, nullptr);
|
61 |
| - #endif |
| 70 | +#endif |
62 | 71 | }
|
63 | 72 |
|
64 | 73 | void signal_catcher(int sig)
|
65 | 74 | {
|
66 |
| - #if defined(_WIN32) |
67 |
| - #else |
| 75 | +#if defined(_WIN32) |
| 76 | +#else |
68 | 77 |
|
69 |
| - #if 1 |
| 78 | +#if 0 |
70 | 79 | // kill any children by killing group
|
71 | 80 | killpg(0, sig);
|
72 |
| - #else |
73 |
| - // pass on to any children |
74 |
| - for(const auto &pid : pids_of_children) |
75 |
| - kill(pid, sig); |
76 |
| - #endif |
| 81 | +#else |
| 82 | + // pass on to our child, if any |
| 83 | + if(child_pid != 0) |
| 84 | + kill(child_pid, sig); |
| 85 | +#endif |
77 | 86 |
|
78 | 87 | exit(sig); // should contemplate something from sysexits.h
|
79 |
| - #endif |
| 88 | +#endif |
80 | 89 | }
|
0 commit comments