Fabcoin Core  0.16.2
P2P Digital Currency
ossig.h
Go to the documentation of this file.
1 // ossig.h - written and placed in the public domain by Jeffrey Walton
2 //
6 
7 #ifndef CRYPTOPP_OS_SIGNAL_H
8 #define CRYPTOPP_OS_SIGNAL_H
9 
10 #include "config.h"
11 
12 #if defined(UNIX_SIGNALS_AVAILABLE)
13 # include <signal.h>
14 #endif
15 
17 
18 // ************** Unix and Linux compatibles ***************
19 
20 #if defined(UNIX_SIGNALS_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING)
21 
25 extern "C" {
26  typedef void (*SignalHandlerFn) (int);
27 };
28 
35 extern "C" {
36  inline void NullSignalHandler(int unused) {CRYPTOPP_UNUSED(unused);}
37 };
38 
53 template <int S, bool O=false>
54 struct SignalHandler
55 {
70  SignalHandler(SignalHandlerFn pfn = NULL, int flags = 0) : m_installed(false)
71  {
72  // http://pubs.opengroup.org/onlinepubs/007908799/xsh/sigaction.html
73  struct sigaction new_handler;
74 
75  do
76  {
77  int ret = 0;
78 
79  ret = sigaction (S, 0, &m_old);
80  if (ret != 0) break; // Failed
81 
82  // Don't step on another's handler if Overwrite=false
83  if (m_old.sa_handler != 0 && !O) break;
84 
85 #if defined __CYGWIN__
86  // http://github.com/weidai11/cryptopp/issues/315
87  memset(&new_handler, 0x00, sizeof(new_handler));
88 #else
89  ret = sigemptyset (&new_handler.sa_mask);
90  if (ret != 0) break; // Failed
91 #endif
92 
93  new_handler.sa_handler = (pfn ? pfn : &NullSignalHandler);
94  new_handler.sa_flags = (pfn ? flags : 0);
95 
96  // Install it
97  ret = sigaction (S, &new_handler, 0);
98  if (ret != 0) break; // Failed
99 
100  m_installed = true;
101 
102  } while(0);
103  }
104 
105  ~SignalHandler()
106  {
107  if (m_installed)
108  sigaction (S, &m_old, 0);
109  }
110 
111 private:
112  struct sigaction m_old;
113  bool m_installed;
114 
115 private:
116  // Not copyable
117  SignalHandler(const SignalHandler &);
118  void operator=(const SignalHandler &);
119 };
120 #endif
121 
123 
124 #endif // CRYPTOPP_OS_SIGNAL_H
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Library configuration file.
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
#define NAMESPACE_END
Definition: config.h:201
#define S(a)
Definition: mars.cpp:50