1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/*
Copyright (C) 2011 Brian Aker
Copyright (C) 2006 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <config.h>
#include <drizzled/gettext.h>
#include <drizzled/error.h>
#include <drizzled/plugin/storage_engine.h>
#include <drizzled/pthread_globals.h>
#include <drizzled/internal/my_pthread.h>
#include <drizzled/internal/my_sys.h>
#include <drizzled/plugin/daemon.h>
#include <drizzled/signal_handler.h>
#include <drizzled/session.h>
#include <drizzled/session/cache.h>
#include <drizzled/debug.h>
#include <drizzled/drizzled.h>
#include <drizzled/open_tables_state.h>
#include <boost/thread/thread.hpp>
#include <boost/filesystem.hpp>
#include <sys/stat.h>
#include <fcntl.h>
static bool kill_in_progress= false;
void signal_hand(void);
namespace drizzled {
extern int cleanup_done;
extern bool volatile abort_loop;
extern bool volatile shutdown_in_progress;
extern boost::filesystem::path pid_file;
/* Prototypes -> all of these should be factored out into a propper shutdown */
extern void close_connections(void);
}
using namespace drizzled;
/**
Force server down. Kill all connections and threads and exit.
@param sig_ptr Signal number that caused kill_server to be called.
@note
A signal number of 0 mean that the function was not called
from a signal handler and there is thus no signal to block
or stop, we just want to kill the server.
*/
static void kill_server(int sig)
{
// if there is a signal during the kill in progress, ignore the other
if (kill_in_progress) // Safety
return;
kill_in_progress=true;
abort_loop=1; // This should be set
if (sig != 0) // 0 is not a valid signal number
ignore_signal(sig); /* purify inspected */
if (sig == SIGTERM || sig == 0)
errmsg_printf(error::INFO, _(ER(ER_NORMAL_SHUTDOWN)),internal::my_progname);
else
errmsg_printf(error::ERROR, _(ER(ER_GOT_SIGNAL)),internal::my_progname,sig);
close_connections();
clean_up(1);
}
/**
Create file to store pid number.
*/
static void create_pid_file()
{
int file;
char buff[1024];
if ((file = open(pid_file.file_string().c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH)) > 0)
{
int length;
length= snprintf(buff, 1024, "%ld\n", (long) getpid());
if ((write(file, buff, length)) == length)
{
if (close(file) != -1)
return;
}
(void)close(file); /* We can ignore the error, since we are going to error anyway at this point */
}
memset(buff, 0, sizeof(buff));
snprintf(buff, sizeof(buff)-1, "Can't start server: can't create PID file (%s)", pid_file.file_string().c_str());
sql_perror(buff);
exit(1);
}
/** This threads handles all signals and alarms. */
void signal_hand()
{
sigset_t set;
int sig;
internal::my_thread_init(); // Init new thread
signal_thread_in_use= true;
if ((drizzled::getDebug().test(drizzled::debug::ALLOW_SIGINT)))
{
(void) sigemptyset(&set); // Setup up SIGINT for debug
(void) sigaddset(&set,SIGINT); // For debugging
(void) pthread_sigmask(SIG_UNBLOCK, &set, NULL);
}
(void) sigemptyset(&set); // Setup up SIGINT for debug
#ifndef IGNORE_SIGHUP_SIGQUIT
if (sigaddset(&set,SIGQUIT))
{
std::cerr << "failed setting sigaddset() with SIGQUIT\n";
}
if (sigaddset(&set,SIGHUP))
{
std::cerr << "failed setting sigaddset() with SIGHUP\n";
}
#endif
if (sigaddset(&set,SIGTERM))
{
std::cerr << "failed setting sigaddset() with SIGTERM\n";
}
if (sigaddset(&set,SIGTSTP))
{
std::cerr << "failed setting sigaddset() with SIGTSTP\n";
}
/* Save pid to this process (or thread on Linux) */
create_pid_file();
/*
signal to init that we are ready
This works by waiting for init to free mutex,
after which we signal it that we are ready.
At this pointer there is no other threads running, so there
should not be any other pthread_cond_signal() calls.
We call lock/unlock to out wait any thread/session which is
dieing. Since only comes from this code, this should be safe.
(Asked MontyW over the phone about this.) -Brian
*/
session::Cache::mutex().lock();
session::Cache::mutex().unlock();
COND_thread_count.notify_all();
if (pthread_sigmask(SIG_BLOCK, &set, NULL))
{
std::cerr << "Failed to set pthread_sigmask() in signal handler\n";
}
for (;;)
{
if (shutdown_in_progress && not abort_loop)
{
sig= SIGTERM;
}
else
{
while (sigwait(&set, &sig) == EINTR)
{
}
}
if (cleanup_done)
{
signal_thread_in_use= false;
return;
}
switch (sig)
{
case SIGTERM:
case SIGQUIT:
case SIGKILL:
case SIGTSTP:
/* switch to the old log message processing */
if (!abort_loop)
{
abort_loop= 1; // mark abort for threads
kill_server(sig); // MIT THREAD has a alarm thread
}
break;
case SIGHUP:
if (not abort_loop)
{
g_refresh_version++;
drizzled::plugin::StorageEngine::flushLogs(NULL);
}
break;
}
}
}
class SignalHandler :
public drizzled::plugin::Daemon
{
boost::thread thread;
public:
SignalHandler() :
drizzled::plugin::Daemon("Signal Handler")
{
// @todo fix spurious wakeup issue
boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
thread= boost::thread(signal_hand);
signal_thread= thread.native_handle();
COND_thread_count.wait(scopedLock);
}
/**
This is mainly needed when running with purify, but it's still nice to
know that all child threads have died when drizzled exits.
*/
~SignalHandler()
{
/*
Wait up to 100000 micro-seconds for signal thread to die. We use this mainly to
avoid getting warnings that internal::my_thread_end has not been called
*/
bool completed= false;
/*
* We send SIGTERM and then do a timed join. If that fails we will on
* the last pthread_kill() call SIGTSTP. OSX (and FreeBSD) seem to
* prefer this. -Brian
*/
uint32_t count= 2; // How many times to try join and see if the caller died.
while (not completed and count--)
{
int signal= count == 1 ? SIGTSTP : SIGTERM;
if (int error= pthread_kill(thread.native_handle(), signal))
{
char buffer[1024]; // No reason for number;
strerror_r(error, buffer, sizeof(buffer));
std::cerr << "pthread_kill() error on shutdown of signal thread (" << buffer << ")\n";
break;
}
else
{
boost::posix_time::milliseconds duration(100);
completed= thread.timed_join(duration);
}
}
}
};
static int init(drizzled::module::Context& context)
{
context.add(new SignalHandler);
return 0;
}
DRIZZLE_DECLARE_PLUGIN
{
DRIZZLE_VERSION_ID,
"signal_handler",
"0.1",
"Brian Aker",
"Default Signal Handler",
PLUGIN_LICENSE_GPL,
init, /* Plugin Init */
NULL, /* depends */
NULL /* config options */
}
DRIZZLE_DECLARE_PLUGIN_END;
|