Fix bad if path
[m6w6/libmemcached] / libtest / signal.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "libtest/yatlcon.h"
38 #include <libtest/common.h>
39
40 #include <csignal>
41
42 #include <libtest/signal.h>
43
44 using namespace libtest;
45
46 #define MAGIC_MEMORY 123569
47
48 bool SignalThread::is_shutdown()
49 {
50 bool ret;
51 pthread_mutex_lock(&shutdown_mutex);
52 ret= bool(__shutdown != SHUTDOWN_RUNNING);
53 pthread_mutex_unlock(&shutdown_mutex);
54
55 return ret;
56 }
57
58 void SignalThread::set_shutdown(shutdown_t arg)
59 {
60 pthread_mutex_lock(&shutdown_mutex);
61 __shutdown= arg;
62 pthread_mutex_unlock(&shutdown_mutex);
63
64 if (arg == SHUTDOWN_GRACEFUL)
65 {
66 if (pthread_kill(thread, SIGUSR2) == 0)
67 {
68 void *retval;
69 pthread_join(thread, &retval);
70 }
71 }
72 }
73
74 shutdown_t SignalThread::get_shutdown()
75 {
76 shutdown_t local;
77 pthread_mutex_lock(&shutdown_mutex);
78 local= __shutdown;
79 pthread_mutex_unlock(&shutdown_mutex);
80
81 return local;
82 }
83
84 void SignalThread::post()
85 {
86 sem_post(&lock);
87 }
88
89 void SignalThread::test()
90 {
91 assert(magic_memory == MAGIC_MEMORY);
92 if (bool(getenv("LIBTEST_IN_GDB")) == false)
93 {
94 assert(sigismember(&set, SIGALRM));
95 assert(sigismember(&set, SIGABRT));
96 assert(sigismember(&set, SIGQUIT));
97 assert(sigismember(&set, SIGINT));
98 assert(sigismember(&set, SIGVTALRM));
99 }
100 assert(sigismember(&set, SIGUSR2));
101 }
102
103 bool SignalThread::unblock()
104 {
105 int error;
106 if ((error= pthread_sigmask(SIG_UNBLOCK, &set, NULL)) != 0)
107 {
108 Error << "While trying to reset signal mask to original set, pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
109 return false;
110 }
111
112 return true;
113 }
114
115 SignalThread::~SignalThread()
116 {
117 if (is_shutdown() == false)
118 {
119 set_shutdown(SHUTDOWN_GRACEFUL);
120 }
121
122 #if 0
123 if (pthread_equal(thread, pthread_self()) != 0 and (pthread_kill(thread, 0) == ESRCH) == true)
124 {
125 void *retval;
126 pthread_join(thread, &retval);
127 }
128 #endif
129 sem_destroy(&lock);
130
131 unblock();
132 }
133
134 extern "C" {
135
136 static void *sig_thread(void *arg)
137 {
138 SignalThread *context= (SignalThread*)arg;
139
140 context->test();
141 context->post();
142
143 while (context->get_shutdown() == SHUTDOWN_RUNNING)
144 {
145 int sig;
146
147 if (context->wait(sig) == -1)
148 {
149 Error << "sigwait() returned errno:" << strerror(errno);
150 continue;
151 }
152
153 switch (sig)
154 {
155 case SIGALRM:
156 case SIGVTALRM:
157 Error << strsignal(sig);
158 if (gdb_is_caller())
159 {
160 abort();
161 }
162 exit(EXIT_FAILURE);
163
164 case SIGABRT:
165 case SIGUSR2:
166 case SIGINT:
167 case SIGQUIT:
168 if (context->is_shutdown() == false)
169 {
170 context->set_shutdown(SHUTDOWN_FORCED);
171 }
172 break;
173 case SIGPIPE:
174 {
175 Error << "Ignoring SIGPIPE";
176 }
177 break;
178
179 case 0:
180 Error << "Inside of gdb";
181 break;
182
183 default:
184 Error << "Signal handling thread got unexpected signal " << strsignal(sig);
185 break;
186 }
187 }
188
189 return NULL;
190 }
191
192 }
193
194 SignalThread::SignalThread() :
195 magic_memory(MAGIC_MEMORY),
196 thread(pthread_self())
197 {
198 pthread_mutex_init(&shutdown_mutex, NULL);
199 sigemptyset(&set);
200 if (bool(getenv("LIBTEST_IN_GDB")) == false)
201 {
202 sigaddset(&set, SIGALRM);
203 sigaddset(&set, SIGABRT);
204 sigaddset(&set, SIGQUIT);
205 sigaddset(&set, SIGINT);
206 sigaddset(&set, SIGVTALRM);
207 }
208 sigaddset(&set, SIGPIPE);
209
210 sigaddset(&set, SIGUSR2);
211
212 sem_init(&lock, 0, 0);
213
214 sigemptyset(&original_set);
215 pthread_sigmask(SIG_BLOCK, NULL, &original_set);
216 }
217
218
219 bool SignalThread::setup()
220 {
221 set_shutdown(SHUTDOWN_RUNNING);
222
223 if (sigismember(&original_set, SIGQUIT))
224 {
225 Error << strsignal(SIGQUIT) << " has been previously set.";
226 }
227
228 if (sigismember(&original_set, SIGINT))
229 {
230 Error << strsignal(SIGINT) << " has been previously set.";
231 }
232
233 if (sigismember(&original_set, SIGVTALRM))
234 {
235 Error << strsignal(SIGVTALRM) << " has been previously set.";
236 }
237
238 if (sigismember(&original_set, SIGUSR2))
239 {
240 Error << strsignal(SIGUSR2) << " has been previously set.";
241 }
242
243 int error;
244 if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
245 {
246 Error << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
247 return false;
248 }
249
250 if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0)
251 {
252 Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
253 return false;
254 }
255
256 sem_wait(&lock);
257
258 return true;
259 }