~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/signal_handler/signal_handler.cc

Merge of Keycache gutting code. (this is a test).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
   Copyright (C) 2011 Brian Aker
3
 
   Copyright (C) 2006 MySQL AB
 
1
/* Copyright (C) 2006 MySQL AB
4
2
 
5
3
   This program is free software; you can redistribute it and/or modify
6
4
   it under the terms of the GNU General Public License as published by
15
13
   along with this program; if not, write to the Free Software
16
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
17
15
 
18
 
#include <config.h>
19
 
 
 
16
#include "config.h"
20
17
#include <drizzled/gettext.h>
21
18
#include <drizzled/error.h>
22
19
#include <drizzled/unireg.h>
23
20
#include <drizzled/plugin/storage_engine.h>
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>
29
 
 
30
 
#include <drizzled/session.h>
31
 
#include <drizzled/session/cache.h>
32
 
 
33
 
#include <drizzled/debug.h>
34
 
 
35
 
#include <drizzled/drizzled.h>
36
 
 
37
 
#include <drizzled/refresh_version.h>
38
 
 
39
 
#include <boost/thread/thread.hpp>
40
 
#include <boost/filesystem.hpp>
 
21
#include <drizzled/cursor.h> /* for refresh_version */
 
22
#include "drizzled/pthread_globals.h"
 
23
#include "drizzled/internal/my_pthread.h"
 
24
#include "drizzled/internal/my_sys.h"
 
25
#include "drizzled/plugin/daemon.h"
 
26
#include "drizzled/signal_handler.h"
41
27
 
42
28
#include <sys/stat.h>
43
29
#include <fcntl.h>
44
30
 
45
31
 
46
32
static bool kill_in_progress= false;
47
 
void signal_hand(void);
 
33
static bool volatile signal_thread_in_use= false;
 
34
extern "C" pthread_handler_t signal_hand(void *);
48
35
 
49
36
namespace drizzled
50
37
{
51
38
extern int cleanup_done;
52
39
extern bool volatile abort_loop;
53
40
extern bool volatile shutdown_in_progress;
54
 
extern boost::filesystem::path pid_file;
 
41
extern char pidfile_name[FN_REFLEN];
55
42
/* Prototypes -> all of these should be factored out into a propper shutdown */
56
43
extern void close_connections(void);
 
44
extern std::bitset<12> test_flags;
57
45
}
58
46
 
59
47
using namespace drizzled;
60
48
 
61
49
 
62
50
 
63
 
 
64
51
/**
65
52
  Force server down. Kill all connections and threads and exit.
66
53
 
72
59
    or stop, we just want to kill the server.
73
60
*/
74
61
 
75
 
static void kill_server(int sig)
 
62
static void kill_server(void *sig_ptr)
76
63
{
 
64
  int sig=(int) (long) sig_ptr;                 // This is passed a int
77
65
  // if there is a signal during the kill in progress, ignore the other
78
66
  if (kill_in_progress)                         // Safety
79
67
    return;
82
70
  if (sig != 0) // 0 is not a valid signal number
83
71
    ignore_signal(sig);                    /* purify inspected */
84
72
  if (sig == SIGTERM || sig == 0)
85
 
    errmsg_printf(error::INFO, _(ER(ER_NORMAL_SHUTDOWN)),internal::my_progname);
 
73
    errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_NORMAL_SHUTDOWN)),internal::my_progname);
86
74
  else
87
 
    errmsg_printf(error::ERROR, _(ER(ER_GOT_SIGNAL)),internal::my_progname,sig);
 
75
    errmsg_printf(ERRMSG_LVL_ERROR, _(ER(ER_GOT_SIGNAL)),internal::my_progname,sig);
88
76
 
89
77
  close_connections();
90
 
  clean_up(1);
 
78
  if (sig != SIGTERM && sig != 0)
 
79
    unireg_abort(1);
 
80
  else
 
81
    unireg_end();
91
82
}
92
83
 
93
84
/**
98
89
  int file;
99
90
  char buff[1024];
100
91
 
101
 
  if ((file = open(pid_file.file_string().c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH)) > 0)
 
92
  assert(pidfile_name[0]);
 
93
  if ((file = open(pidfile_name, O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH)) > 0)
102
94
  {
103
95
    int length;
104
96
 
111
103
    }
112
104
    (void)close(file); /* We can ignore the error, since we are going to error anyway at this point */
113
105
  }
114
 
  memset(buff, 0, sizeof(buff));
115
 
  snprintf(buff, sizeof(buff)-1, "Can't start server: can't create PID file (%s)", pid_file.file_string().c_str());
 
106
  snprintf(buff, 1024, "Can't start server: can't create PID file (%s)", pidfile_name);
116
107
  sql_perror(buff);
117
108
  exit(1);
118
109
}
119
110
 
120
111
 
121
112
/** This threads handles all signals and alarms. */
122
 
void signal_hand()
 
113
pthread_handler_t signal_hand(void *)
123
114
{
124
115
  sigset_t set;
125
116
  int sig;
126
117
  internal::my_thread_init();                           // Init new thread
127
 
  boost::this_thread::at_thread_exit(&internal::my_thread_end);
128
118
  signal_thread_in_use= true;
129
119
 
130
 
  if ((drizzled::getDebug().test(drizzled::debug::ALLOW_SIGINT)))
 
120
  if (internal::thd_lib_detected != THD_LIB_LT && 
 
121
      (test_flags.test(TEST_SIGINT)))
131
122
  {
132
123
    (void) sigemptyset(&set);                   // Setup up SIGINT for debug
133
124
    (void) sigaddset(&set,SIGINT);              // For debugging
134
 
    (void) pthread_sigmask(SIG_UNBLOCK, &set, NULL);
 
125
    (void) pthread_sigmask(SIG_UNBLOCK,&set,NULL);
135
126
  }
136
127
  (void) sigemptyset(&set);                     // Setup up SIGINT for debug
137
128
#ifndef IGNORE_SIGHUP_SIGQUIT
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
 
  }
 
129
  (void) sigaddset(&set,SIGQUIT);
 
130
  (void) sigaddset(&set,SIGHUP);
146
131
#endif
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
 
  }
 
132
  (void) sigaddset(&set,SIGTERM);
 
133
  (void) sigaddset(&set,SIGTSTP);
155
134
 
156
135
  /* Save pid to this process (or thread on Linux) */
157
136
  create_pid_file();
168
147
    (Asked MontyW over the phone about this.) -Brian
169
148
 
170
149
  */
171
 
  session::Cache::singleton().mutex().lock();
172
 
  session::Cache::singleton().mutex().unlock();
 
150
  LOCK_thread_count.lock();
 
151
  LOCK_thread_count.unlock();
173
152
  COND_thread_count.notify_all();
174
153
 
175
 
  if (pthread_sigmask(SIG_BLOCK, &set, NULL))
176
 
  {
177
 
    std::cerr << "Failed to set pthread_sigmask() in signal handler\n";
178
 
  }
179
 
 
 
154
  (void) pthread_sigmask(SIG_BLOCK,&set,NULL);
180
155
  for (;;)
181
156
  {
182
157
    int error;                                  // Used when debugging
183
 
 
184
158
    if (shutdown_in_progress && !abort_loop)
185
159
    {
186
160
      sig= SIGTERM;
187
161
      error=0;
188
162
    }
189
163
    else
190
 
    {
191
 
      while ((error= sigwait(&set, &sig)) == EINTR) ;
192
 
    }
193
 
 
 
164
      while ((error= sigwait(&set,&sig)) == EINTR) ;
194
165
    if (cleanup_done)
195
166
    {
 
167
      internal::my_thread_end();
196
168
      signal_thread_in_use= false;
197
169
 
198
 
      return;
 
170
      return NULL;
199
171
    }
200
172
    switch (sig) {
201
173
    case SIGTERM:
202
174
    case SIGQUIT:
203
175
    case SIGKILL:
204
 
    case SIGTSTP:
205
176
      /* switch to the old log message processing */
206
177
      if (!abort_loop)
207
178
      {
208
179
        abort_loop=1;                           // mark abort for threads
209
 
        kill_server(sig);               // MIT THREAD has a alarm thread
 
180
        kill_server((void*) sig);       // MIT THREAD has a alarm thread
210
181
      }
211
182
      break;
212
183
    case SIGHUP:
227
198
{
228
199
  SignalHandler(const SignalHandler &);
229
200
  SignalHandler& operator=(const SignalHandler &);
230
 
  boost::thread thread;
231
 
 
232
201
public:
233
 
  SignalHandler() :
234
 
    drizzled::plugin::Daemon("Signal Handler")
 
202
  SignalHandler()
 
203
    : drizzled::plugin::Daemon("Signal Handler")
235
204
  {
 
205
    int error;
 
206
    pthread_attr_t thr_attr;
 
207
    size_t my_thread_stack_size= 65536;
 
208
 
 
209
    (void) pthread_attr_init(&thr_attr);
 
210
    pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM);
 
211
    (void) pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
 
212
    {
 
213
      struct sched_param tmp_sched_param;
 
214
 
 
215
      memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
 
216
      tmp_sched_param.sched_priority= INTERRUPT_PRIOR;
 
217
      (void)pthread_attr_setschedparam(&thr_attr, &tmp_sched_param);
 
218
    }
 
219
#if defined(__ia64__) || defined(__ia64)
 
220
    /*
 
221
      Peculiar things with ia64 platforms - it seems we only have half the
 
222
      stack size in reality, so we have to double it here
 
223
    */
 
224
    pthread_attr_setstacksize(&thr_attr, my_thread_stack_size*2);
 
225
# else
 
226
    pthread_attr_setstacksize(&thr_attr, my_thread_stack_size);
 
227
#endif
 
228
 
236
229
    // @todo fix spurious wakeup issue
237
 
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
238
 
    thread= boost::thread(signal_hand);
239
 
    signal_thread= thread.native_handle();
240
 
    COND_thread_count.wait(scopedLock);
 
230
    (void) LOCK_thread_count.lock();
 
231
    if ((error=pthread_create(&signal_thread, &thr_attr, signal_hand, 0)))
 
232
    {
 
233
      errmsg_printf(ERRMSG_LVL_ERROR,
 
234
                    _("Can't create interrupt-thread (error %d, errno: %d)"),
 
235
                    error,errno);
 
236
      exit(1);
 
237
    }
 
238
    pthread_cond_wait(COND_thread_count.native_handle(), LOCK_thread_count.native_handle());
 
239
    LOCK_thread_count.unlock();
 
240
 
 
241
    (void) pthread_attr_destroy(&thr_attr);
241
242
  }
242
243
 
243
244
  /**
250
251
      Wait up to 100000 micro-seconds for signal thread to die. We use this mainly to
251
252
      avoid getting warnings that internal::my_thread_end has not been called
252
253
    */
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--)
 
254
    for (uint32_t i= 0 ; i < 100 && signal_thread_in_use; i++)
261
255
    {
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";
 
256
      if (pthread_kill(signal_thread, SIGTERM) != ESRCH)
270
257
        break;
271
 
      }
272
 
      else
273
 
      {
274
 
        boost::posix_time::milliseconds duration(100);
275
 
        completed= thread.timed_join(duration);
276
 
      }
 
258
 
 
259
      struct timespec tm;
 
260
      tm.tv_sec= 0;
 
261
      tm.tv_nsec= 100000;
 
262
 
 
263
      nanosleep(&tm, NULL);                             // Give it time to die
277
264
    }
 
265
 
278
266
  }
279
267
};
280
268
 
281
269
static int init(drizzled::module::Context& context)
282
270
{
283
 
  context.add(new SignalHandler);
284
 
 
 
271
  SignalHandler *handler= new SignalHandler;
 
272
  context.add(handler);
285
273
  return 0;
286
274
}
287
275
 
288
276
 
 
277
static drizzle_sys_var* system_variables[]= {
 
278
  NULL
 
279
};
 
280
 
289
281
DRIZZLE_DECLARE_PLUGIN
290
282
{
291
283
  DRIZZLE_VERSION_ID,
295
287
  "Default Signal Handler",
296
288
  PLUGIN_LICENSE_GPL,
297
289
  init, /* Plugin Init */
298
 
  NULL,   /* depends */
 
290
  system_variables,   /* system variables */
299
291
  NULL    /* config options */
300
292
}
301
293
DRIZZLE_DECLARE_PLUGIN_END;