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