Security Scol plugin
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
16NAMESPACE_BEGIN(CryptoPP)
17
18// ************** Unix and Linux compatibles ***************
19
20#if defined(UNIX_SIGNALS_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING)
21
25extern "C" {
26 typedef void (*SignalHandlerFn) (int);
27}
28
35extern "C" {
36 inline void NullSignalHandler(int unused) {CRYPTOPP_UNUSED(unused);}
37}
38
57template <int S, bool O=false>
58struct SignalHandler
59{
78 SignalHandler(SignalHandlerFn pfn = NULLPTR, int flags = 0) : m_installed(false)
79 {
80 // http://pubs.opengroup.org/onlinepubs/007908799/xsh/sigaction.html
81 struct sigaction new_handler;
82
83 do
84 {
85 int ret = 0;
86
87 ret = sigaction (S, 0, &m_old);
88 if (ret != 0) break; // Failed
89
90 // Don't step on another's handler if Overwrite=false
91 if (m_old.sa_handler != 0 && !O) break;
92
93 // Cygwin/Newlib requires -D_XOPEN_SOURCE=700
94 ret = sigemptyset (&new_handler.sa_mask);
95 if (ret != 0) break; // Failed
96
97 new_handler.sa_handler = (pfn ? pfn : &NullSignalHandler);
98 new_handler.sa_flags = (pfn ? flags : 0);
99
100 // Install it
101 ret = sigaction (S, &new_handler, 0);
102 if (ret != 0) break; // Failed
103
104 m_installed = true;
105
106 } while(0);
107 }
108
109 ~SignalHandler()
110 {
111 if (m_installed)
112 sigaction (S, &m_old, 0);
113 }
114
115private:
116 struct sigaction m_old;
117 bool m_installed;
118
119private:
120 // Not copyable
121 SignalHandler(const SignalHandler &);
122 void operator=(const SignalHandler &);
123};
124#endif
125
126NAMESPACE_END
127
128#endif // CRYPTOPP_OS_SIGNAL_H
Library configuration file.