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