Updating for 1.0.2 release
[awesomized/libmemcached] / util / signal.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * DD Util
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <config.h>
24
25 #include <cassert>
26 #include <cerrno>
27 #include <csignal>
28 #include <cstdlib>
29 #include <cstring>
30 #include <iostream>
31
32 #include <util/signal.hpp>
33
34 namespace datadifferential {
35 namespace util {
36
37 #define MAGIC_MEMORY 123569
38
39 bool SignalThread::is_shutdown()
40 {
41 bool ret;
42 pthread_mutex_lock(&shutdown_mutex);
43 ret= bool(__shutdown != SHUTDOWN_RUNNING);
44 pthread_mutex_unlock(&shutdown_mutex);
45
46 return ret;
47 }
48
49 void SignalThread::set_shutdown(shutdown_t arg)
50 {
51 pthread_mutex_lock(&shutdown_mutex);
52 __shutdown= arg;
53 pthread_mutex_unlock(&shutdown_mutex);
54
55 if (arg == SHUTDOWN_GRACEFUL)
56 {
57 if (pthread_kill(thread, SIGUSR2) == 0)
58 {
59 void *retval;
60 pthread_join(thread, &retval);
61 }
62 }
63 }
64
65 shutdown_t SignalThread::get_shutdown()
66 {
67 shutdown_t local;
68 pthread_mutex_lock(&shutdown_mutex);
69 local= __shutdown;
70 pthread_mutex_unlock(&shutdown_mutex);
71
72 return local;
73 }
74
75 void SignalThread::post()
76 {
77 sem_post(&lock);
78 }
79
80 void SignalThread::test()
81 {
82 assert(magic_memory == MAGIC_MEMORY);
83 assert(sigismember(&set, SIGABRT));
84 assert(sigismember(&set, SIGINT));
85 assert(sigismember(&set, SIGQUIT));
86 assert(sigismember(&set, SIGTERM));
87 assert(sigismember(&set, SIGUSR2));
88 }
89
90 SignalThread::~SignalThread()
91 {
92 if (not is_shutdown())
93 {
94 set_shutdown(SHUTDOWN_GRACEFUL);
95 }
96
97 #if 0
98 if (pthread_equal(thread, pthread_self()) != 0 and (pthread_kill(thread, 0) == ESRCH) == true)
99 {
100 void *retval;
101 pthread_join(thread, &retval);
102 }
103 #endif
104 sem_destroy(&lock);
105 }
106
107 extern "C" {
108
109 static void *sig_thread(void *arg)
110 {
111 SignalThread *context= (SignalThread*)arg;
112
113 context->test();
114 context->post();
115
116 while (context->get_shutdown() == SHUTDOWN_RUNNING)
117 {
118 int sig;
119
120 if (context->wait(sig) == -1)
121 {
122 std::cerr << "sigwait() returned errno:" << strerror(errno) << std::endl;
123 continue;
124 }
125
126 switch (sig)
127 {
128 case SIGUSR2:
129 break;
130
131 case SIGABRT:
132 case SIGINT:
133 case SIGQUIT:
134 case SIGTERM:
135 if (context->is_shutdown() == false)
136 {
137 context->set_shutdown(SHUTDOWN_FORCED);
138 }
139
140 if (context->exit_on_signal())
141 {
142 exit(EXIT_SUCCESS);
143 }
144
145 break;
146
147 default:
148 std::cerr << "Signal handling thread got unexpected signal " << strsignal(sig) << std::endl;
149 break;
150 }
151 }
152
153 return NULL;
154 }
155
156 }
157
158 SignalThread::SignalThread(bool exit_on_signal_arg) :
159 _exit_on_signal(exit_on_signal_arg),
160 magic_memory(MAGIC_MEMORY),
161 thread(pthread_self())
162 {
163 pthread_mutex_init(&shutdown_mutex, NULL);
164 sigemptyset(&set);
165
166 sigaddset(&set, SIGABRT);
167 sigaddset(&set, SIGINT);
168 sigaddset(&set, SIGQUIT);
169 sigaddset(&set, SIGTERM);
170 sigaddset(&set, SIGUSR2);
171
172 sem_init(&lock, 0, 0);
173 }
174
175
176 bool SignalThread::setup()
177 {
178 set_shutdown(SHUTDOWN_RUNNING);
179
180 int error;
181 if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
182 {
183 std::cerr << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")" << std::endl;
184 return false;
185 }
186
187 if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0)
188 {
189 std::cerr << "pthread_create() died during pthread_create(" << strerror(error) << ")" << std::endl;
190 return false;
191 }
192
193 sem_wait(&lock);
194
195 return true;
196 }
197
198 } /* namespace util */
199 } /* namespace datadifferential */