Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[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 Error << "SIGALRM";
157 if (gdb_is_caller())
158 {
159 abort();
160 }
161 exit(EXIT_SKIP);
162
163 case SIGVTALRM:
164 Error << "SIGVTALRM was called";
165 context->unblock();
166 raise(SIGVTALRM);
167
168 case SIGABRT:
169 case SIGUSR2:
170 case SIGINT:
171 case SIGQUIT:
172 if (context->is_shutdown() == false)
173 {
174 context->set_shutdown(SHUTDOWN_FORCED);
175 }
176 break;
177 case SIGPIPE:
178 {
179 Error << "Ignoring SIGPIPE";
180 }
181 break;
182
183 case 0:
184 Error << "Inside of gdb";
185 break;
186
187 default:
188 Error << "Signal handling thread got unexpected signal " << strsignal(sig);
189 break;
190 }
191 }
192
193 return NULL;
194 }
195
196 }
197
198 SignalThread::SignalThread() :
199 magic_memory(MAGIC_MEMORY),
200 thread(pthread_self())
201 {
202 pthread_mutex_init(&shutdown_mutex, NULL);
203 sigemptyset(&set);
204 if (bool(getenv("LIBTEST_IN_GDB")) == false)
205 {
206 sigaddset(&set, SIGALRM);
207 sigaddset(&set, SIGABRT);
208 sigaddset(&set, SIGQUIT);
209 sigaddset(&set, SIGINT);
210 sigaddset(&set, SIGVTALRM);
211 }
212 else
213 {
214 Out << "Inside of GDB, disabling signal handlers";
215 }
216
217 sigaddset(&set, SIGPIPE);
218
219 sigaddset(&set, SIGUSR2);
220
221 sem_init(&lock, 0, 0);
222
223 sigemptyset(&original_set);
224 pthread_sigmask(SIG_BLOCK, NULL, &original_set);
225 }
226
227
228 bool SignalThread::setup()
229 {
230 set_shutdown(SHUTDOWN_RUNNING);
231
232 if (sigismember(&original_set, SIGQUIT))
233 {
234 Error << strsignal(SIGQUIT) << " has been previously set.";
235 }
236
237 if (sigismember(&original_set, SIGINT))
238 {
239 Error << strsignal(SIGINT) << " has been previously set.";
240 }
241
242 if (sigismember(&original_set, SIGVTALRM))
243 {
244 Error << strsignal(SIGVTALRM) << " has been previously set.";
245 }
246
247 if (sigismember(&original_set, SIGUSR2))
248 {
249 Error << strsignal(SIGUSR2) << " has been previously set.";
250 }
251
252 int error;
253 if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
254 {
255 Error << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
256 return false;
257 }
258
259 if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0)
260 {
261 Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
262 return false;
263 }
264
265 sem_wait(&lock);
266
267 return true;
268 }