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