Merge in libtest updates.
[m6w6/libmemcached] / libtest / signal.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * uTest, libtest
4 *
5 * Copyright (C) 2011 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/common.h>
38
39 #include <csignal>
40
41 #include <libtest/signal.h>
42
43 using namespace libtest;
44
45 #define MAGIC_MEMORY 123569
46
47 bool SignalThread::is_shutdown()
48 {
49 bool ret;
50 pthread_mutex_lock(&shutdown_mutex);
51 ret= bool(__shutdown != SHUTDOWN_RUNNING);
52 pthread_mutex_unlock(&shutdown_mutex);
53
54 return ret;
55 }
56
57 void SignalThread::set_shutdown(shutdown_t arg)
58 {
59 pthread_mutex_lock(&shutdown_mutex);
60 __shutdown= arg;
61 pthread_mutex_unlock(&shutdown_mutex);
62
63 if (arg == SHUTDOWN_GRACEFUL)
64 {
65 if (pthread_kill(thread, SIGUSR2) == 0)
66 {
67 void *retval;
68 pthread_join(thread, &retval);
69 }
70 }
71 }
72
73 shutdown_t SignalThread::get_shutdown()
74 {
75 shutdown_t local;
76 pthread_mutex_lock(&shutdown_mutex);
77 local= __shutdown;
78 pthread_mutex_unlock(&shutdown_mutex);
79
80 return local;
81 }
82
83 void SignalThread::post()
84 {
85 sem_post(&lock);
86 }
87
88 void SignalThread::test()
89 {
90 assert(magic_memory == MAGIC_MEMORY);
91 if (not getenv("LIBTEST_IN_GDB"))
92 {
93 assert(sigismember(&set, SIGABRT));
94 assert(sigismember(&set, SIGQUIT));
95 assert(sigismember(&set, SIGINT));
96 }
97 assert(sigismember(&set, SIGUSR2));
98 }
99
100 SignalThread::~SignalThread()
101 {
102 if (pthread_equal(thread, pthread_self()) != 0 and (pthread_kill(thread, 0) == ESRCH) == true)
103 {
104 void *retval;
105 pthread_join(thread, &retval);
106 }
107 sem_destroy(&lock);
108 }
109
110 extern "C" {
111
112 static void *sig_thread(void *arg)
113 {
114 SignalThread *context= (SignalThread*)arg;
115
116 context->test();
117 context->post();
118
119 while (context->get_shutdown() == SHUTDOWN_RUNNING)
120 {
121 int sig;
122
123 if (context->wait(sig) == -1)
124 {
125 Error << "sigwait() returned errno:" << strerror(errno);
126 continue;
127 }
128
129 switch (sig)
130 {
131 case SIGABRT:
132 case SIGUSR2:
133 case SIGINT:
134 case SIGQUIT:
135 if (context->is_shutdown() == false)
136 {
137 context->set_shutdown(SHUTDOWN_FORCED);
138 }
139 break;
140
141 default:
142 Error << "Signal handling thread got unexpected signal " << strsignal(sig);
143 break;
144 }
145 }
146
147 return NULL;
148 }
149
150 }
151
152 SignalThread::SignalThread() :
153 magic_memory(MAGIC_MEMORY),
154 thread(pthread_self())
155 {
156 pthread_mutex_init(&shutdown_mutex, NULL);
157 sigemptyset(&set);
158 if (not getenv("LIBTEST_IN_GDB"))
159 {
160 sigaddset(&set, SIGABRT);
161 sigaddset(&set, SIGQUIT);
162 sigaddset(&set, SIGINT);
163 }
164
165 sigaddset(&set, SIGUSR2);
166
167 sem_init(&lock, 0, 0);
168 }
169
170
171 bool SignalThread::setup()
172 {
173 set_shutdown(SHUTDOWN_RUNNING);
174
175 sigset_t old_set;
176 sigemptyset(&old_set);
177 pthread_sigmask(SIG_BLOCK, NULL, &old_set);
178
179 if (sigismember(&old_set, SIGQUIT))
180 {
181 Error << strsignal(SIGQUIT) << " has been previously set.";
182 }
183 if (sigismember(&old_set, SIGINT))
184 {
185 Error << strsignal(SIGINT) << " has been previously set.";
186 }
187 if (sigismember(&old_set, SIGUSR2))
188 {
189 Error << strsignal(SIGUSR2) << " has been previously set.";
190 }
191
192 int error;
193 if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
194 {
195 Error << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
196 return false;
197 }
198
199 if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0)
200 {
201 Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
202 return false;
203 }
204
205 sem_wait(&lock);
206
207 return true;
208 }