Update libtest.
[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 <config.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, SIGABRT));
95 assert(sigismember(&set, SIGQUIT));
96 assert(sigismember(&set, SIGINT));
97 assert(sigismember(&set, SIGVTALRM));
98 }
99 assert(sigismember(&set, SIGUSR2));
100 }
101
102 bool SignalThread::unblock()
103 {
104 int error;
105 if ((error= pthread_sigmask(SIG_UNBLOCK, &set, NULL)) != 0)
106 {
107 Error << "While trying to reset signal mask to original set, pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
108 return false;
109 }
110
111 return true;
112 }
113
114 SignalThread::~SignalThread()
115 {
116 if (is_shutdown() == false)
117 {
118 set_shutdown(SHUTDOWN_GRACEFUL);
119 }
120
121 #if 0
122 if (pthread_equal(thread, pthread_self()) != 0 and (pthread_kill(thread, 0) == ESRCH) == true)
123 {
124 void *retval;
125 pthread_join(thread, &retval);
126 }
127 #endif
128 sem_destroy(&lock);
129
130 unblock();
131 }
132
133 extern "C" {
134
135 static void *sig_thread(void *arg)
136 {
137 SignalThread *context= (SignalThread*)arg;
138
139 context->test();
140 context->post();
141
142 while (context->get_shutdown() == SHUTDOWN_RUNNING)
143 {
144 int sig;
145
146 if (context->wait(sig) == -1)
147 {
148 Error << "sigwait() returned errno:" << strerror(errno);
149 continue;
150 }
151
152 switch (sig)
153 {
154 case SIGVTALRM:
155 Error << "SIGVTALRM was called";
156 context->unblock();
157 raise(SIGVTALRM);
158
159 case SIGABRT:
160 case SIGUSR2:
161 case SIGINT:
162 case SIGQUIT:
163 if (context->is_shutdown() == false)
164 {
165 context->set_shutdown(SHUTDOWN_FORCED);
166 }
167 break;
168 case SIGPIPE:
169 {
170 Error << "Ignoring SIGPIPE";
171 }
172 break;
173
174 case 0:
175 Error << "Inside of gdb";
176 break;
177
178 default:
179 Error << "Signal handling thread got unexpected signal " << strsignal(sig);
180 break;
181 }
182 }
183
184 return NULL;
185 }
186
187 }
188
189 SignalThread::SignalThread() :
190 magic_memory(MAGIC_MEMORY),
191 thread(pthread_self())
192 {
193 pthread_mutex_init(&shutdown_mutex, NULL);
194 sigemptyset(&set);
195 if (bool(getenv("LIBTEST_IN_GDB")) == false)
196 {
197 sigaddset(&set, SIGABRT);
198 sigaddset(&set, SIGQUIT);
199 sigaddset(&set, SIGINT);
200 sigaddset(&set, SIGVTALRM);
201 }
202 sigaddset(&set, SIGPIPE);
203
204 sigaddset(&set, SIGUSR2);
205
206 sem_init(&lock, 0, 0);
207
208 sigemptyset(&original_set);
209 pthread_sigmask(SIG_BLOCK, NULL, &original_set);
210 }
211
212
213 bool SignalThread::setup()
214 {
215 set_shutdown(SHUTDOWN_RUNNING);
216
217 if (sigismember(&original_set, SIGQUIT))
218 {
219 Error << strsignal(SIGQUIT) << " has been previously set.";
220 }
221
222 if (sigismember(&original_set, SIGINT))
223 {
224 Error << strsignal(SIGINT) << " has been previously set.";
225 }
226
227 if (sigismember(&original_set, SIGVTALRM))
228 {
229 Error << strsignal(SIGVTALRM) << " has been previously set.";
230 }
231
232 if (sigismember(&original_set, SIGUSR2))
233 {
234 Error << strsignal(SIGUSR2) << " has been previously set.";
235 }
236
237 int error;
238 if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
239 {
240 Error << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
241 return false;
242 }
243
244 if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0)
245 {
246 Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
247 return false;
248 }
249
250 sem_wait(&lock);
251
252 return true;
253 }