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 }
int kill(pid_t pid, int sig)
Definition: signal.c:15
void remove_signal_catcher()
void install_signal_catcher()
void register_child(pid_t pid)
void signal_catcher(int sig)
void unregister_child()
pid_t child_pid
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
void exit(int status)
Definition: stdlib.c:102