~drizzle-trunk/drizzle/development

2148.7.11 by Brian Aker
Remove some of the dependencies from session.h so that we make the include
1
/* 
2
   Copyright (C) 2011 Brian Aker
3
   Copyright (C) 2006 MySQL AB
900 by Brian Aker
Creating signal handler plugin.
4
5
   This program is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; version 2 of the License.
8
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU General Public License for more details.
13
14
   You should have received a copy of the GNU General Public License
15
   along with this program; if not, write to the Free Software
16
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
17
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
18
#include <config.h>
2148.7.11 by Brian Aker
Remove some of the dependencies from session.h so that we make the include
19
900 by Brian Aker
Creating signal handler plugin.
20
#include <drizzled/gettext.h>
21
#include <drizzled/error.h>
1022.2.2 by Monty Taylor
Small cleanup inspired by merge.
22
#include <drizzled/unireg.h>
1237.9.3 by Padraig O'Sullivan
Removed one the includes I put in server_includes.h for the last commit to get rid of the inclusion
23
#include <drizzled/plugin/storage_engine.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
24
#include <drizzled/pthread_globals.h>
25
#include <drizzled/internal/my_pthread.h>
26
#include <drizzled/internal/my_sys.h>
27
#include <drizzled/plugin/daemon.h>
28
#include <drizzled/signal_handler.h>
900 by Brian Aker
Creating signal handler plugin.
29
2148.7.11 by Brian Aker
Remove some of the dependencies from session.h so that we make the include
30
#include <drizzled/session.h>
31
#include <drizzled/session/cache.h>
1932.3.4 by Brian Aker
Move counter lock so that ownership is held by session_list.
32
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
33
#include <drizzled/debug.h>
2104.3.3 by Brian Aker
Cleanup test flags for debug options on server startup
34
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
35
#include <drizzled/drizzled.h>
1733.3.1 by LinuxJedi
Make exit happen in main thread rather than signal handler thread thus avoiding a segfault due to a double kill of the signal handler thread
36
2148.7.11 by Brian Aker
Remove some of the dependencies from session.h so that we make the include
37
#include <drizzled/refresh_version.h>
38
1755.2.11 by Brian Aker
Update for boost.
39
#include <boost/thread/thread.hpp>
1813.2.2 by Monty Taylor
Replaced pid-file with fs::path.
40
#include <boost/filesystem.hpp>
1755.2.11 by Brian Aker
Update for boost.
41
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
42
#include <sys/stat.h>
43
#include <fcntl.h>
44
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
45
900 by Brian Aker
Creating signal handler plugin.
46
static bool kill_in_progress= false;
1755.2.11 by Brian Aker
Update for boost.
47
void signal_hand(void);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
48
49
namespace drizzled
50
{
900 by Brian Aker
Creating signal handler plugin.
51
extern int cleanup_done;
1241.9.33 by Monty Taylor
Moved most of the global vars to set_var where they belong.
52
extern bool volatile abort_loop;
53
extern bool volatile shutdown_in_progress;
1813.2.2 by Monty Taylor
Replaced pid-file with fs::path.
54
extern boost::filesystem::path pid_file;
900 by Brian Aker
Creating signal handler plugin.
55
/* Prototypes -> all of these should be factored out into a propper shutdown */
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
56
extern void close_connections(void);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
57
}
58
59
using namespace drizzled;
60
900 by Brian Aker
Creating signal handler plugin.
61
62
1932.3.4 by Brian Aker
Move counter lock so that ownership is held by session_list.
63
900 by Brian Aker
Creating signal handler plugin.
64
/**
65
  Force server down. Kill all connections and threads and exit.
66
67
  @param  sig_ptr       Signal number that caused kill_server to be called.
68
69
  @note
70
    A signal number of 0 mean that the function was not called
71
    from a signal handler and there is thus no signal to block
72
    or stop, we just want to kill the server.
73
*/
74
1816.2.6 by Monty Taylor
Fixed three more ICC warnings.
75
static void kill_server(int sig)
900 by Brian Aker
Creating signal handler plugin.
76
{
77
  // if there is a signal during the kill in progress, ignore the other
78
  if (kill_in_progress)				// Safety
1022.2.18 by Monty Taylor
Fixed the fix on solaris.
79
    return;
900 by Brian Aker
Creating signal handler plugin.
80
  kill_in_progress=true;
81
  abort_loop=1;					// This should be set
82
  if (sig != 0) // 0 is not a valid signal number
1300.5.23 by Monty Taylor
Merged in revs removing depend on the plugin tree.
83
    ignore_signal(sig);                    /* purify inspected */
900 by Brian Aker
Creating signal handler plugin.
84
  if (sig == SIGTERM || sig == 0)
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
85
    errmsg_printf(error::INFO, _(ER(ER_NORMAL_SHUTDOWN)),internal::my_progname);
900 by Brian Aker
Creating signal handler plugin.
86
  else
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
87
    errmsg_printf(error::ERROR, _(ER(ER_GOT_SIGNAL)),internal::my_progname,sig);
88
900 by Brian Aker
Creating signal handler plugin.
89
  close_connections();
1733.3.3 by LinuxJedi
Fix valgrind warnings (plus make exit clean up properly)
90
  clean_up(1);
900 by Brian Aker
Creating signal handler plugin.
91
}
92
93
/**
94
  Create file to store pid number.
95
*/
96
static void create_pid_file()
97
{
914.2.1 by Brian Aker
Cleanup pid handler.
98
  int file;
99
  char buff[1024];
100
1813.2.2 by Monty Taylor
Replaced pid-file with fs::path.
101
  if ((file = open(pid_file.file_string().c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH)) > 0)
900 by Brian Aker
Creating signal handler plugin.
102
  {
914.2.1 by Brian Aker
Cleanup pid handler.
103
    int length;
104
105
    length= snprintf(buff, 1024, "%ld\n", (long) getpid()); 
106
107
    if ((write(file, buff, length)) == length)
900 by Brian Aker
Creating signal handler plugin.
108
    {
914.2.1 by Brian Aker
Cleanup pid handler.
109
      if (close(file) != -1)
110
        return;
900 by Brian Aker
Creating signal handler plugin.
111
    }
914.2.1 by Brian Aker
Cleanup pid handler.
112
    (void)close(file); /* We can ignore the error, since we are going to error anyway at this point */
900 by Brian Aker
Creating signal handler plugin.
113
  }
1798.2.2 by Monty Taylor
Fix trailing 0 problem.
114
  memset(buff, 0, sizeof(buff));
1813.2.2 by Monty Taylor
Replaced pid-file with fs::path.
115
  snprintf(buff, sizeof(buff)-1, "Can't start server: can't create PID file (%s)", pid_file.file_string().c_str());
914.2.1 by Brian Aker
Cleanup pid handler.
116
  sql_perror(buff);
900 by Brian Aker
Creating signal handler plugin.
117
  exit(1);
118
}
119
120
121
/** This threads handles all signals and alarms. */
1755.2.11 by Brian Aker
Update for boost.
122
void signal_hand()
900 by Brian Aker
Creating signal handler plugin.
123
{
124
  sigset_t set;
125
  int sig;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
126
  internal::my_thread_init();				// Init new thread
1782.3.1 by Brian Aker
Pushes up thread ownership to the modules that create them.
127
  boost::this_thread::at_thread_exit(&internal::my_thread_end);
900 by Brian Aker
Creating signal handler plugin.
128
  signal_thread_in_use= true;
129
2104.3.3 by Brian Aker
Cleanup test flags for debug options on server startup
130
  if ((drizzled::getDebug().test(drizzled::debug::ALLOW_SIGINT)))
900 by Brian Aker
Creating signal handler plugin.
131
  {
132
    (void) sigemptyset(&set);			// Setup up SIGINT for debug
133
    (void) sigaddset(&set,SIGINT);		// For debugging
2104.3.3 by Brian Aker
Cleanup test flags for debug options on server startup
134
    (void) pthread_sigmask(SIG_UNBLOCK, &set, NULL);
900 by Brian Aker
Creating signal handler plugin.
135
  }
136
  (void) sigemptyset(&set);			// Setup up SIGINT for debug
137
#ifndef IGNORE_SIGHUP_SIGQUIT
1779.2.1 by Brian Aker
Fix issue with signal thread not shutting down (OSX).
138
  if (sigaddset(&set,SIGQUIT))
139
  {
140
    std::cerr << "failed setting sigaddset() with SIGQUIT\n";
141
  }
142
  if (sigaddset(&set,SIGHUP))
143
  {
144
    std::cerr << "failed setting sigaddset() with SIGHUP\n";
145
  }
900 by Brian Aker
Creating signal handler plugin.
146
#endif
1779.2.1 by Brian Aker
Fix issue with signal thread not shutting down (OSX).
147
  if (sigaddset(&set,SIGTERM))
148
  {
149
    std::cerr << "failed setting sigaddset() with SIGTERM\n";
150
  }
151
  if (sigaddset(&set,SIGTSTP))
152
  {
153
    std::cerr << "failed setting sigaddset() with SIGTSTP\n";
154
  }
900 by Brian Aker
Creating signal handler plugin.
155
156
  /* Save pid to this process (or thread on Linux) */
157
  create_pid_file();
158
159
  /*
905 by Brian Aker
Code cleanup in signal_handler.cc
160
    signal to init that we are ready
161
    This works by waiting for init to free mutex,
900 by Brian Aker
Creating signal handler plugin.
162
    after which we signal it that we are ready.
163
    At this pointer there is no other threads running, so there
164
    should not be any other pthread_cond_signal() calls.
165
166
    We call lock/unlock to out wait any thread/session which is
167
    dieing. Since only comes from this code, this should be safe.
168
    (Asked MontyW over the phone about this.) -Brian
169
170
  */
1932.3.4 by Brian Aker
Move counter lock so that ownership is held by session_list.
171
  session::Cache::singleton().mutex().lock();
172
  session::Cache::singleton().mutex().unlock();
1689.2.5 by Brian Aker
Convert COND_thread_count to boost.
173
  COND_thread_count.notify_all();
900 by Brian Aker
Creating signal handler plugin.
174
1779.2.1 by Brian Aker
Fix issue with signal thread not shutting down (OSX).
175
  if (pthread_sigmask(SIG_BLOCK, &set, NULL))
176
  {
177
    std::cerr << "Failed to set pthread_sigmask() in signal handler\n";
178
  }
179
900 by Brian Aker
Creating signal handler plugin.
180
  for (;;)
181
  {
182
    int error;					// Used when debugging
1755.2.13 by Brian Aker
Add join in order to allow for signal thread to be shutdown during shutdown
183
900 by Brian Aker
Creating signal handler plugin.
184
    if (shutdown_in_progress && !abort_loop)
185
    {
186
      sig= SIGTERM;
187
      error=0;
188
    }
189
    else
1755.2.13 by Brian Aker
Add join in order to allow for signal thread to be shutdown during shutdown
190
    {
1779.2.1 by Brian Aker
Fix issue with signal thread not shutting down (OSX).
191
      while ((error= sigwait(&set, &sig)) == EINTR) ;
1755.2.13 by Brian Aker
Add join in order to allow for signal thread to be shutdown during shutdown
192
    }
193
900 by Brian Aker
Creating signal handler plugin.
194
    if (cleanup_done)
195
    {
196
      signal_thread_in_use= false;
197
1755.2.11 by Brian Aker
Update for boost.
198
      return;
900 by Brian Aker
Creating signal handler plugin.
199
    }
200
    switch (sig) {
201
    case SIGTERM:
202
    case SIGQUIT:
203
    case SIGKILL:
1779.2.1 by Brian Aker
Fix issue with signal thread not shutting down (OSX).
204
    case SIGTSTP:
900 by Brian Aker
Creating signal handler plugin.
205
      /* switch to the old log message processing */
206
      if (!abort_loop)
207
      {
208
        abort_loop=1;				// mark abort for threads
1816.2.6 by Monty Taylor
Fixed three more ICC warnings.
209
        kill_server(sig);		// MIT THREAD has a alarm thread
900 by Brian Aker
Creating signal handler plugin.
210
      }
211
      break;
212
    case SIGHUP:
213
      if (!abort_loop)
1109.1.4 by Brian Aker
More Table refactor
214
      {
215
        refresh_version++;
1152.1.3 by Brian Aker
refactored drizzled::plugin::StorageEngine::flushLogs()
216
        drizzled::plugin::StorageEngine::flushLogs(NULL);
1109.1.4 by Brian Aker
More Table refactor
217
      }
900 by Brian Aker
Creating signal handler plugin.
218
      break;
219
    default:
971.6.11 by Eric Day
Removed purecov messages.
220
      break;
900 by Brian Aker
Creating signal handler plugin.
221
    }
222
  }
223
}
224
1324.2.3 by Monty Taylor
Remove plugin deinit.
225
class SignalHandler :
226
  public drizzled::plugin::Daemon
900 by Brian Aker
Creating signal handler plugin.
227
{
1324.2.3 by Monty Taylor
Remove plugin deinit.
228
  SignalHandler(const SignalHandler &);
229
  SignalHandler& operator=(const SignalHandler &);
1755.2.11 by Brian Aker
Update for boost.
230
  boost::thread thread;
231
1324.2.3 by Monty Taylor
Remove plugin deinit.
232
public:
1755.2.11 by Brian Aker
Update for boost.
233
  SignalHandler() :
234
    drizzled::plugin::Daemon("Signal Handler")
900 by Brian Aker
Creating signal handler plugin.
235
  {
1689.2.5 by Brian Aker
Convert COND_thread_count to boost.
236
    // @todo fix spurious wakeup issue
1932.3.4 by Brian Aker
Move counter lock so that ownership is held by session_list.
237
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
1755.2.11 by Brian Aker
Update for boost.
238
    thread= boost::thread(signal_hand);
239
    signal_thread= thread.native_handle();
240
    COND_thread_count.wait(scopedLock);
900 by Brian Aker
Creating signal handler plugin.
241
  }
1324.2.3 by Monty Taylor
Remove plugin deinit.
242
243
  /**
244
    This is mainly needed when running with purify, but it's still nice to
245
    know that all child threads have died when drizzled exits.
900 by Brian Aker
Creating signal handler plugin.
246
  */
1324.2.3 by Monty Taylor
Remove plugin deinit.
247
  ~SignalHandler()
900 by Brian Aker
Creating signal handler plugin.
248
  {
1324.2.3 by Monty Taylor
Remove plugin deinit.
249
    /*
1672.3.7 by Brian Aker
Remve usleep for nanosleep.
250
      Wait up to 100000 micro-seconds for signal thread to die. We use this mainly to
1324.2.3 by Monty Taylor
Remove plugin deinit.
251
      avoid getting warnings that internal::my_thread_end has not been called
252
    */
1779.2.1 by Brian Aker
Fix issue with signal thread not shutting down (OSX).
253
    bool completed= false;
254
    /*
255
     * We send SIGTERM and then do a timed join. If that fails we will on
256
     * the last pthread_kill() call SIGTSTP. OSX (and FreeBSD) seem to
257
     * prefer this. -Brian
258
   */
259
    uint32_t count= 2; // How many times to try join and see if the caller died.
260
    while (not completed and count--)
261
    {
262
      int error;
263
      int signal= count == 1 ? SIGTSTP : SIGTERM;
264
      
265
      if ((error= pthread_kill(thread.native_handle(), signal)))
266
      {
267
        char buffer[1024]; // No reason for number;
268
        strerror_r(error, buffer, sizeof(buffer));
269
        std::cerr << "pthread_kill() error on shutdown of signal thread (" << buffer << ")\n";
270
        break;
271
      }
272
      else
273
      {
274
        boost::posix_time::milliseconds duration(100);
275
        completed= thread.timed_join(duration);
276
      }
277
    }
900 by Brian Aker
Creating signal handler plugin.
278
  }
1324.2.3 by Monty Taylor
Remove plugin deinit.
279
};
900 by Brian Aker
Creating signal handler plugin.
280
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
281
static int init(drizzled::module::Context& context)
1324.2.3 by Monty Taylor
Remove plugin deinit.
282
{
1755.2.10 by Brian Aker
Clean up the calls around the signal handler.
283
  context.add(new SignalHandler);
284
900 by Brian Aker
Creating signal handler plugin.
285
  return 0;
286
}
287
1324.2.3 by Monty Taylor
Remove plugin deinit.
288
1228.1.5 by Monty Taylor
Merged in some naming things.
289
DRIZZLE_DECLARE_PLUGIN
900 by Brian Aker
Creating signal handler plugin.
290
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
291
  DRIZZLE_VERSION_ID,
900 by Brian Aker
Creating signal handler plugin.
292
  "signal_handler",
293
  "0.1",
294
  "Brian Aker",
295
  "Default Signal Handler",
296
  PLUGIN_LICENSE_GPL,
297
  init, /* Plugin Init */
2095.3.1 by Monty Taylor
Re-purpose the old plugin sysvar slot in the struct to be a depends list.
298
  NULL,   /* depends */
900 by Brian Aker
Creating signal handler plugin.
299
  NULL    /* config options */
300
}
1228.1.5 by Monty Taylor
Merged in some naming things.
301
DRIZZLE_DECLARE_PLUGIN_END;