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