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