cprover
signal_catcher.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 Date:
8 
9 \*******************************************************************/
10 
11 #include "signal_catcher.h"
12 
13 #if defined(_WIN32)
14 #include <process.h>
15 #else
16 #include <cstdlib>
17 #include <csignal>
18 #endif
19 
20 #include <vector>
21 
22 // Here we have an instance of an ugly global object.
23 // It keeps track of any child processes that we'll kill
24 // when we are told to terminate.
25 
26 #ifdef _WIN32
27 #else
28 std::vector<pid_t> pids_of_children;
29 #endif
30 
32 {
33  #if defined(_WIN32)
34  #else
35  // declare act to deal with action on signal set
36  // NOLINTNEXTLINE(readability/identifiers)
37  static struct sigaction act;
38 
39  act.sa_handler=signal_catcher;
40  act.sa_flags=0;
41  sigfillset(&(act.sa_mask));
42 
43  // install signal handler
44  sigaction(SIGTERM, &act, nullptr);
45  #endif
46 }
47 
49 {
50  #if defined(_WIN32)
51  #else
52  // declare act to deal with action on signal set
53  // NOLINTNEXTLINE(readability/identifiers)
54  static struct sigaction act;
55 
56  act.sa_handler=SIG_DFL;
57  act.sa_flags=0;
58  sigfillset(&(act.sa_mask));
59 
60  sigaction(SIGTERM, &act, nullptr);
61  #endif
62 }
63 
64 void signal_catcher(int sig)
65 {
66  #if defined(_WIN32)
67  #else
68 
69  #if 1
70  // kill any children by killing group
71  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
77 
78  exit(sig); // should contemplate something from sysexits.h
79  #endif
80 }
std::vector< pid_t > pids_of_children
void signal_catcher(int sig)
void remove_signal_catcher()
void install_signal_catcher()