Small cleanups.
[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 <pthread.h>
40 #include <semaphore.h>
41 #include <csignal>
42
43 #include <libtest/signal.h>
44
45 using namespace libtest;
46
47 struct context_st {
48 sigset_t set;
49 sem_t lock;
50
51 context_st()
52 {
53 sigemptyset(&set);
54 sigaddset(&set, SIGQUIT);
55 sigaddset(&set, SIGINT);
56
57 sem_init(&lock, 0, 0);
58 }
59
60 void test()
61 {
62 assert(sigismember(&set, SIGQUIT));
63 assert(sigismember(&set, SIGINT));
64 }
65
66 int wait(int& sig)
67 {
68 return sigwait(&set, &sig);
69 }
70
71 ~context_st()
72 {
73 sem_destroy(&lock);
74 }
75 };
76
77 static volatile shutdown_t __shutdown;
78 static pthread_mutex_t shutdown_mutex;
79 static pthread_t thread;
80
81 bool is_shutdown()
82 {
83 bool ret;
84 pthread_mutex_lock(&shutdown_mutex);
85 ret= bool(__shutdown != SHUTDOWN_RUNNING);
86 pthread_mutex_unlock(&shutdown_mutex);
87
88 return ret;
89 }
90
91 void set_shutdown(shutdown_t arg)
92 {
93 pthread_mutex_lock(&shutdown_mutex);
94 __shutdown= arg;
95 pthread_mutex_unlock(&shutdown_mutex);
96
97 if (arg == SHUTDOWN_GRACEFUL)
98 {
99 pthread_kill(thread, SIGQUIT);
100
101 void *retval;
102 pthread_join(thread, &retval);
103 }
104 }
105
106 shutdown_t get_shutdown()
107 {
108 shutdown_t local;
109 pthread_mutex_lock(&shutdown_mutex);
110 local= __shutdown;
111 pthread_mutex_unlock(&shutdown_mutex);
112
113 return local;
114 }
115
116 extern "C" {
117
118 static void *sig_thread(void *arg)
119 {
120 context_st *context= (context_st*)arg;
121 assert(context);
122
123 context->test();
124 sem_post(&context->lock);
125
126 while (get_shutdown() == SHUTDOWN_RUNNING)
127 {
128 int sig;
129
130 if (context->wait(sig) == -1)
131 {
132 Error << "sigwait() returned errno:" << strerror(errno);
133 continue;
134 }
135
136 switch (sig)
137 {
138 case SIGINT:
139 case SIGQUIT:
140 if (is_shutdown() == false)
141 {
142 Error << "Signal handling thread got signal " << strsignal(sig);
143 set_shutdown(SHUTDOWN_FORCED);
144 }
145 break;
146
147 default:
148 Error << "Signal handling thread got unexpected signal " << strsignal(sig);
149 break;
150 }
151 }
152
153 delete context;
154
155 return NULL;
156 }
157
158 }
159
160 void setup_signals()
161 {
162 pthread_mutex_init(&shutdown_mutex, NULL);
163 set_shutdown(SHUTDOWN_RUNNING);
164
165 context_st *context= new context_st;
166
167 assert(context);
168
169 sigset_t old_set;
170 sigemptyset(&old_set);
171 pthread_sigmask(SIG_BLOCK, NULL, &old_set);
172
173 if (sigismember(&old_set, SIGQUIT))
174 {
175 Error << strsignal(SIGQUIT) << " has been previously set.";
176 }
177 if (sigismember(&old_set, SIGINT))
178 {
179 Error << strsignal(SIGINT) << " has been previously set.";
180 }
181
182 int error;
183 if ((error= pthread_sigmask(SIG_BLOCK, &context->set, NULL)) != 0)
184 {
185 Error << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
186 exit(EXIT_FAILURE);
187 }
188
189 if ((error= pthread_create(&thread, NULL, &sig_thread, (void *) &context->set)) != 0)
190 {
191 Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
192 exit(EXIT_FAILURE);
193 }
194
195 sem_wait(&context->lock);
196 }