missing inttypes.h
[m6w6/libmemcached] / src / 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 "mem_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 void SignalThread::sighup(signal_callback_fn* arg)
105 {
106 _sighup= arg;
107 }
108
109 void SignalThread::sighup()
110 {
111 if (_sighup)
112 {
113 _sighup();
114 }
115 }
116
117 SignalThread::~SignalThread()
118 {
119 if (not is_shutdown())
120 {
121 set_shutdown(SHUTDOWN_GRACEFUL);
122 }
123
124 #if 0
125 if (pthread_equal(thread, pthread_self()) and (pthread_kill(thread, 0) == ESRCH) == true)
126 {
127 void *retval;
128 pthread_join(thread, &retval);
129 }
130 #endif
131 sem_destroy(&lock);
132 }
133
134 extern "C" {
135
136 static void *sig_thread(void *arg)
137 {
138 SignalThread *context= (SignalThread*)arg;
139
140 context->test();
141 context->post();
142
143 while (context->get_shutdown() == SHUTDOWN_RUNNING)
144 {
145 int sig;
146
147 if (context->wait(sig) == -1)
148 {
149 std::cerr << "sigwait() returned errno:" << strerror(errno) << std::endl;
150 continue;
151 }
152
153 switch (sig)
154 {
155 case SIGUSR2:
156 break;
157
158 case SIGHUP:
159 context->sighup();
160 break;
161
162 case SIGABRT:
163 case SIGINT:
164 case SIGQUIT:
165 case SIGTERM:
166 if (context->is_shutdown() == false)
167 {
168 context->set_shutdown(SHUTDOWN_FORCED);
169 }
170
171 if (context->exit_on_signal())
172 {
173 exit(EXIT_SUCCESS);
174 }
175
176 break;
177
178 default:
179 std::cerr << "Signal handling thread got unexpected signal " << strsignal(sig) << std::endl;
180 break;
181 }
182 }
183
184 return NULL;
185 }
186
187 }
188
189 SignalThread::SignalThread(bool exit_on_signal_arg) :
190 _exit_on_signal(exit_on_signal_arg),
191 magic_memory(MAGIC_MEMORY),
192 __shutdown(SHUTDOWN_RUNNING),
193 thread(pthread_self()),
194 _sighup(NULL)
195 {
196 pthread_mutex_init(&shutdown_mutex, NULL);
197 sigemptyset(&set);
198
199 sigaddset(&set, SIGABRT);
200 sigaddset(&set, SIGINT);
201 sigaddset(&set, SIGQUIT);
202 sigaddset(&set, SIGTERM);
203 sigaddset(&set, SIGUSR2);
204
205 sem_init(&lock, 0, 0);
206 }
207
208
209 bool SignalThread::setup()
210 {
211 set_shutdown(SHUTDOWN_RUNNING);
212
213 int error;
214 if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)))
215 {
216 std::cerr << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")" << std::endl;
217 return false;
218 }
219
220 if ((error= pthread_create(&thread, NULL, &sig_thread, this)))
221 {
222 std::cerr << "pthread_create() died during pthread_create(" << strerror(error) << ")" << std::endl;
223 return false;
224 }
225
226 sem_wait(&lock);
227
228 return true;
229 }
230
231 } /* namespace util */
232 } /* namespace datadifferential */