1 | // |
---|
2 | // SignalHandler.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/SignalHandler.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: SignalHandler |
---|
9 | // |
---|
10 | // Definition of the SignalHandler class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_SignalHandler_INCLUDED |
---|
40 | #define Foundation_SignalHandler_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | |
---|
45 | |
---|
46 | #if defined(POCO_OS_FAMILY_UNIX) |
---|
47 | |
---|
48 | |
---|
49 | #include <vector> |
---|
50 | #include <setjmp.h> |
---|
51 | |
---|
52 | |
---|
53 | namespace Poco { |
---|
54 | |
---|
55 | |
---|
56 | class Foundation_API SignalHandler |
---|
57 | /// This helper class simplifies the handling of POSIX signals. |
---|
58 | /// |
---|
59 | /// The class provides a signal handler (installed with |
---|
60 | /// installHandlers()) that translates certain POSIX |
---|
61 | /// signals (SIGILL, SIGBUS, SIGSEGV, SIGSYS) into |
---|
62 | /// C++ exceptions. |
---|
63 | /// |
---|
64 | /// Internally, a stack of sigjmp_buf structs is maintained for |
---|
65 | /// each thread. The constructor pushes a new sigjmp_buf onto |
---|
66 | /// the current thread's stack. The destructor pops the sigjmp_buf |
---|
67 | /// from the stack. |
---|
68 | /// |
---|
69 | /// The poco_throw_on_signal macro creates an instance of SignalHandler |
---|
70 | /// on the stack, which results in a new sigjmp_buf being created. |
---|
71 | /// The sigjmp_buf is then set-up with sigsetjmp(). |
---|
72 | /// |
---|
73 | /// The handleSignal() method, which is invoked when a signal arrives, |
---|
74 | /// checks if a sigjmp_buf is available for the current thread. |
---|
75 | /// If so, siglongjmp() is used to jump out of the signal handler. |
---|
76 | /// |
---|
77 | /// Typical usage is as follows: |
---|
78 | /// |
---|
79 | /// try |
---|
80 | /// { |
---|
81 | /// poco_throw_on_signal; |
---|
82 | /// ... |
---|
83 | /// } |
---|
84 | /// catch (Poco::SignalException&) |
---|
85 | /// { |
---|
86 | /// ... |
---|
87 | /// } |
---|
88 | /// |
---|
89 | /// The best way to deal with a SignalException is to log as much context |
---|
90 | /// information as possible, to aid in debugging, and then to exit. |
---|
91 | /// |
---|
92 | /// The SignalHandler can be disabled globally by compiling POCO and client |
---|
93 | /// code with the POCO_NO_SIGNAL_HANDLER macro defined. |
---|
94 | { |
---|
95 | public: |
---|
96 | SignalHandler(); |
---|
97 | /// Creates the SignalHandler. |
---|
98 | |
---|
99 | ~SignalHandler(); |
---|
100 | /// Destroys the SignalHandler. |
---|
101 | |
---|
102 | sigjmp_buf& jumpBuffer(); |
---|
103 | /// Returns the top-most sigjmp_buf for the current thread. |
---|
104 | |
---|
105 | static void throwSignalException(int sig); |
---|
106 | /// Throws a SignalException with a textual description |
---|
107 | /// of the given signal as argument. |
---|
108 | |
---|
109 | static void install(); |
---|
110 | /// Installs signal handlers for SIGILL, SIGBUS, SIGSEGV |
---|
111 | /// and SIGSYS. |
---|
112 | |
---|
113 | protected: |
---|
114 | static void handleSignal(int sig); |
---|
115 | /// The actual signal handler. |
---|
116 | |
---|
117 | struct JumpBuffer |
---|
118 | /// sigjmp_buf cannot be used to instantiate a std::vector, |
---|
119 | /// so we provide a wrapper struct. |
---|
120 | { |
---|
121 | sigjmp_buf buf; |
---|
122 | }; |
---|
123 | typedef std::vector<JumpBuffer> JumpBufferVec; |
---|
124 | |
---|
125 | static JumpBufferVec& jumpBufferVec(); |
---|
126 | /// Returns the JumpBufferVec for the current thread. |
---|
127 | |
---|
128 | private: |
---|
129 | static JumpBufferVec _jumpBufferVec; |
---|
130 | |
---|
131 | friend class ThreadImpl; |
---|
132 | }; |
---|
133 | |
---|
134 | |
---|
135 | #ifndef POCO_NO_SIGNAL_HANDLER |
---|
136 | #define poco_throw_on_signal \ |
---|
137 | Poco::SignalHandler _poco_signalHandler; \ |
---|
138 | int _poco_signal = sigsetjmp(_poco_signalHandler.jumpBuffer(), 1); \ |
---|
139 | if (_poco_signal) _poco_signalHandler.throwSignalException(_poco_signal); |
---|
140 | #else |
---|
141 | #define poco_throw_on_signal |
---|
142 | #endif |
---|
143 | |
---|
144 | |
---|
145 | } // namespace Poco |
---|
146 | |
---|
147 | |
---|
148 | #endif // POCO_OS_FAMILY_UNIX |
---|
149 | |
---|
150 | |
---|
151 | #endif // Foundation_SignalHandler_INCLUDED |
---|