~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/signal_handler/signal_handler.cc

  • Committer: Stewart Smith
  • Date: 2010-02-15 01:56:32 UTC
  • mto: (1273.13.96 build)
  • mto: This revision was merged to the branch mainline in revision 1308.
  • Revision ID: stewart@flamingspork.com-20100215015632-pm7lnxfq5j5uh8kj
move DATABASE() to function plugin. modify parser so that it looks for a function named 'database' when DATABASE() is called. Special case still needed in parser due to hilarity of not-really-reserved words.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include "drizzled/pthread_globals.h"
23
23
#include "drizzled/internal/my_pthread.h"
24
24
#include "drizzled/internal/my_sys.h"
25
 
#include "drizzled/plugin/daemon.h"
26
 
#include "drizzled/signal_handler.h"
27
 
 
28
 
#include "drizzled/session/cache.h"
29
 
 
30
 
#include "drizzled/drizzled.h"
31
 
 
32
 
#include <boost/thread/thread.hpp>
33
 
#include <boost/filesystem.hpp>
34
25
 
35
26
#include <sys/stat.h>
36
27
#include <fcntl.h>
37
28
 
38
29
 
39
30
static bool kill_in_progress= false;
40
 
void signal_hand(void);
 
31
static bool volatile signal_thread_in_use= false;
 
32
extern "C" pthread_handler_t signal_hand(void *);
41
33
 
42
34
namespace drizzled
43
35
{
44
36
extern int cleanup_done;
45
37
extern bool volatile abort_loop;
46
38
extern bool volatile shutdown_in_progress;
47
 
extern boost::filesystem::path pid_file;
 
39
extern char pidfile_name[FN_REFLEN];
48
40
/* Prototypes -> all of these should be factored out into a propper shutdown */
49
41
extern void close_connections(void);
50
42
extern std::bitset<12> test_flags;
54
46
 
55
47
 
56
48
 
57
 
 
58
49
/**
59
50
  Force server down. Kill all connections and threads and exit.
60
51
 
66
57
    or stop, we just want to kill the server.
67
58
*/
68
59
 
69
 
static void kill_server(int sig)
 
60
static void kill_server(void *sig_ptr)
70
61
{
 
62
  int sig=(int) (long) sig_ptr;                 // This is passed a int
71
63
  // if there is a signal during the kill in progress, ignore the other
72
64
  if (kill_in_progress)                         // Safety
73
65
    return;
74
66
  kill_in_progress=true;
75
67
  abort_loop=1;                                 // This should be set
76
68
  if (sig != 0) // 0 is not a valid signal number
77
 
    ignore_signal(sig);                    /* purify inspected */
 
69
    my_sigset(sig, SIG_IGN);                    /* purify inspected */
78
70
  if (sig == SIGTERM || sig == 0)
79
71
    errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_NORMAL_SHUTDOWN)),internal::my_progname);
80
72
  else
81
73
    errmsg_printf(ERRMSG_LVL_ERROR, _(ER(ER_GOT_SIGNAL)),internal::my_progname,sig);
 
74
 
82
75
  close_connections();
83
 
  clean_up(1);
 
76
  if (sig != SIGTERM && sig != 0)
 
77
    unireg_abort(1);
 
78
  else
 
79
    unireg_end();
84
80
}
85
81
 
86
82
/**
91
87
  int file;
92
88
  char buff[1024];
93
89
 
94
 
  if ((file = open(pid_file.file_string().c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH)) > 0)
 
90
  assert(pidfile_name[0]);
 
91
  if ((file = open(pidfile_name, O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH)) > 0)
95
92
  {
96
93
    int length;
97
94
 
104
101
    }
105
102
    (void)close(file); /* We can ignore the error, since we are going to error anyway at this point */
106
103
  }
107
 
  memset(buff, 0, sizeof(buff));
108
 
  snprintf(buff, sizeof(buff)-1, "Can't start server: can't create PID file (%s)", pid_file.file_string().c_str());
 
104
  snprintf(buff, 1024, "Can't start server: can't create PID file (%s)", pidfile_name);
109
105
  sql_perror(buff);
110
106
  exit(1);
111
107
}
112
108
 
113
109
 
114
110
/** This threads handles all signals and alarms. */
115
 
void signal_hand()
 
111
pthread_handler_t signal_hand(void *)
116
112
{
117
113
  sigset_t set;
118
114
  int sig;
119
115
  internal::my_thread_init();                           // Init new thread
120
 
  boost::this_thread::at_thread_exit(&internal::my_thread_end);
121
116
  signal_thread_in_use= true;
122
117
 
123
 
  if ((test_flags.test(TEST_SIGINT)))
 
118
  if (internal::thd_lib_detected != THD_LIB_LT && 
 
119
      (test_flags.test(TEST_SIGINT)))
124
120
  {
125
121
    (void) sigemptyset(&set);                   // Setup up SIGINT for debug
126
122
    (void) sigaddset(&set,SIGINT);              // For debugging
128
124
  }
129
125
  (void) sigemptyset(&set);                     // Setup up SIGINT for debug
130
126
#ifndef IGNORE_SIGHUP_SIGQUIT
131
 
  if (sigaddset(&set,SIGQUIT))
132
 
  {
133
 
    std::cerr << "failed setting sigaddset() with SIGQUIT\n";
134
 
  }
135
 
  if (sigaddset(&set,SIGHUP))
136
 
  {
137
 
    std::cerr << "failed setting sigaddset() with SIGHUP\n";
138
 
  }
 
127
  (void) sigaddset(&set,SIGQUIT);
 
128
  (void) sigaddset(&set,SIGHUP);
139
129
#endif
140
 
  if (sigaddset(&set,SIGTERM))
141
 
  {
142
 
    std::cerr << "failed setting sigaddset() with SIGTERM\n";
143
 
  }
144
 
  if (sigaddset(&set,SIGTSTP))
145
 
  {
146
 
    std::cerr << "failed setting sigaddset() with SIGTSTP\n";
147
 
  }
 
130
  (void) sigaddset(&set,SIGTERM);
 
131
  (void) sigaddset(&set,SIGTSTP);
148
132
 
149
133
  /* Save pid to this process (or thread on Linux) */
150
134
  create_pid_file();
151
135
 
 
136
#ifdef HAVE_STACK_TRACE_ON_SEGV
 
137
  if (opt_do_pstack)
 
138
  {
 
139
    sprintf(pstack_file_name,"drizzled-%lu-%%d-%%d.backtrace", (uint32_t)getpid());
 
140
    pstack_install_segv_action(pstack_file_name);
 
141
  }
 
142
#endif /* HAVE_STACK_TRACE_ON_SEGV */
 
143
 
152
144
  /*
153
145
    signal to init that we are ready
154
146
    This works by waiting for init to free mutex,
161
153
    (Asked MontyW over the phone about this.) -Brian
162
154
 
163
155
  */
164
 
  session::Cache::singleton().mutex().lock();
165
 
  session::Cache::singleton().mutex().unlock();
166
 
  COND_thread_count.notify_all();
167
 
 
168
 
  if (pthread_sigmask(SIG_BLOCK, &set, NULL))
169
 
  {
170
 
    std::cerr << "Failed to set pthread_sigmask() in signal handler\n";
171
 
  }
172
 
 
 
156
  if (pthread_mutex_lock(&LOCK_thread_count) == 0)
 
157
    (void) pthread_mutex_unlock(&LOCK_thread_count);
 
158
  (void) pthread_cond_broadcast(&COND_thread_count);
 
159
 
 
160
  (void) pthread_sigmask(SIG_BLOCK,&set,NULL);
173
161
  for (;;)
174
162
  {
175
163
    int error;                                  // Used when debugging
176
 
 
177
164
    if (shutdown_in_progress && !abort_loop)
178
165
    {
179
166
      sig= SIGTERM;
180
167
      error=0;
181
168
    }
182
169
    else
183
 
    {
184
 
      while ((error= sigwait(&set, &sig)) == EINTR) ;
185
 
    }
186
 
 
 
170
      while ((error= sigwait(&set,&sig)) == EINTR) ;
187
171
    if (cleanup_done)
188
172
    {
 
173
      internal::my_thread_end();
189
174
      signal_thread_in_use= false;
190
175
 
191
 
      return;
 
176
      return NULL;
192
177
    }
193
178
    switch (sig) {
194
179
    case SIGTERM:
195
180
    case SIGQUIT:
196
181
    case SIGKILL:
197
 
    case SIGTSTP:
198
182
      /* switch to the old log message processing */
199
183
      if (!abort_loop)
200
184
      {
201
185
        abort_loop=1;                           // mark abort for threads
202
 
        kill_server(sig);               // MIT THREAD has a alarm thread
 
186
        kill_server((void*) sig);       // MIT THREAD has a alarm thread
203
187
      }
204
188
      break;
205
189
    case SIGHUP:
215
199
  }
216
200
}
217
201
 
218
 
class SignalHandler :
219
 
  public drizzled::plugin::Daemon
220
 
{
221
 
  SignalHandler(const SignalHandler &);
222
 
  SignalHandler& operator=(const SignalHandler &);
223
 
  boost::thread thread;
224
 
 
225
 
public:
226
 
  SignalHandler() :
227
 
    drizzled::plugin::Daemon("Signal Handler")
228
 
  {
229
 
    // @todo fix spurious wakeup issue
230
 
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
231
 
    thread= boost::thread(signal_hand);
232
 
    signal_thread= thread.native_handle();
233
 
    COND_thread_count.wait(scopedLock);
234
 
  }
235
 
 
236
 
  /**
237
 
    This is mainly needed when running with purify, but it's still nice to
238
 
    know that all child threads have died when drizzled exits.
239
 
  */
240
 
  ~SignalHandler()
241
 
  {
242
 
    /*
243
 
      Wait up to 100000 micro-seconds for signal thread to die. We use this mainly to
244
 
      avoid getting warnings that internal::my_thread_end has not been called
245
 
    */
246
 
    bool completed= false;
247
 
    /*
248
 
     * We send SIGTERM and then do a timed join. If that fails we will on
249
 
     * the last pthread_kill() call SIGTSTP. OSX (and FreeBSD) seem to
250
 
     * prefer this. -Brian
251
 
   */
252
 
    uint32_t count= 2; // How many times to try join and see if the caller died.
253
 
    while (not completed and count--)
254
 
    {
255
 
      int error;
256
 
      int signal= count == 1 ? SIGTSTP : SIGTERM;
257
 
      
258
 
      if ((error= pthread_kill(thread.native_handle(), signal)))
259
 
      {
260
 
        char buffer[1024]; // No reason for number;
261
 
        strerror_r(error, buffer, sizeof(buffer));
262
 
        std::cerr << "pthread_kill() error on shutdown of signal thread (" << buffer << ")\n";
263
 
        break;
264
 
      }
265
 
      else
266
 
      {
267
 
        boost::posix_time::milliseconds duration(100);
268
 
        completed= thread.timed_join(duration);
269
 
      }
270
 
    }
271
 
  }
272
 
};
273
 
 
274
 
static int init(drizzled::module::Context& context)
275
 
{
276
 
  context.add(new SignalHandler);
277
 
 
278
 
  return 0;
279
 
}
280
 
 
 
202
 
 
203
static int init(drizzled::plugin::Registry&)
 
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
 
 
229
  (void) pthread_mutex_lock(&LOCK_thread_count);
 
230
  if ((error=pthread_create(&signal_thread, &thr_attr, signal_hand, 0)))
 
231
  {
 
232
      errmsg_printf(ERRMSG_LVL_ERROR, _("Can't create interrupt-thread (error %d, errno: %d)"),
 
233
                    error,errno);
 
234
    exit(1);
 
235
  }
 
236
  (void) pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
 
237
  pthread_mutex_unlock(&LOCK_thread_count);
 
238
 
 
239
  (void) pthread_attr_destroy(&thr_attr);
 
240
 
 
241
  return 0;
 
242
}
 
243
 
 
244
/**
 
245
  This is mainly needed when running with purify, but it's still nice to
 
246
  know that all child threads have died when drizzled exits.
 
247
*/
 
248
static int deinit(drizzled::plugin::Registry&)
 
249
{
 
250
  uint32_t i;
 
251
  /*
 
252
    Wait up to 10 seconds for signal thread to die. We use this mainly to
 
253
    avoid getting warnings that internal::my_thread_end has not been called
 
254
  */
 
255
  for (i= 0 ; i < 100 && signal_thread_in_use; i++)
 
256
  {
 
257
    if (pthread_kill(signal_thread, SIGTERM) != ESRCH)
 
258
      break;
 
259
    usleep(100);                                // Give it time to die
 
260
  }
 
261
 
 
262
  return 0;
 
263
}
281
264
 
282
265
static drizzle_sys_var* system_variables[]= {
283
266
  NULL
292
275
  "Default Signal Handler",
293
276
  PLUGIN_LICENSE_GPL,
294
277
  init, /* Plugin Init */
 
278
  deinit, /* Plugin Deinit */
 
279
  NULL,   /* status variables */
295
280
  system_variables,   /* system variables */
296
281
  NULL    /* config options */
297
282
}