~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/signal_handler.cc

  • Committer: Mark Atwood
  • Date: 2008-10-16 11:33:16 UTC
  • mto: (520.1.13 drizzle)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: mark@fallenpegasus.com-20081016113316-ff6jdt31ck90sjdh
an implemention of the errmsg plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include <config.h>
21
 
 
22
 
#include <signal.h>
23
 
 
24
 
#include <drizzled/signal_handler.h>
25
 
#include <drizzled/drizzled.h>
26
 
#include <drizzled/session.h>
27
 
#include <drizzled/session/cache.h>
28
 
#include <drizzled/internal/my_sys.h>
29
 
#include <drizzled/probes.h>
30
 
#include <drizzled/plugin.h>
31
 
#include <drizzled/plugin/scheduler.h>
32
 
#include <drizzled/current_session.h>
33
 
 
34
 
#include <drizzled/util/backtrace.h>
35
 
 
36
 
using namespace drizzled;
37
 
 
38
 
static uint32_t killed_threads;
39
 
static bool segfaulted= false;
40
 
 
41
 
/*
42
 
 * We declare these extern "C" because they are passed to system callback functions
43
 
 * and Sun Studio does not like it when those don't have C linkage. We prefix them
44
 
 * because extern "C"-ing something effectively removes the namespace from the
45
 
 * linker symbols, meaning they would be exporting symbols like "print_signal_warning
46
 
 */
47
 
extern "C"
48
 
{
49
 
 
50
 
void drizzled_print_signal_warning(int sig)
51
 
{
52
 
  if (global_system_variables.log_warnings)
53
 
    errmsg_printf(error::WARN, _("Got signal %d from thread %"PRIu32),
54
 
                  sig, global_thread_id);
55
 
#ifndef HAVE_BSD_SIGNALS
56
 
  sigset_t set;
57
 
  sigemptyset(&set);
58
 
 
59
 
  struct sigaction sa;
60
 
  sa.sa_handler= drizzled_print_signal_warning;
61
 
  sa.sa_mask= set;
62
 
  sa.sa_flags= 0;
63
 
  sigaction(sig, &sa, NULL);  /* int. thread system calls */
64
 
#endif
65
 
  if (sig == SIGALRM)
66
 
    alarm(2);                                   /* reschedule alarm */
67
 
}
68
 
 
69
 
/** Called when a thread is aborted. */
70
 
void drizzled_end_thread_signal(int )
71
 
{
72
 
  Session *session= current_session;
73
 
  if (session)
74
 
  {
75
 
    Session::shared_ptr session_ptr(session::Cache::singleton().find(session->getSessionId()));
76
 
    if (not session_ptr) // We need to make we have a lock on session before we do anything with it.
77
 
      return;
78
 
 
79
 
    killed_threads++;
80
 
 
81
 
    // We need to get the ID before we kill off the session
82
 
    session_ptr->scheduler->killSessionNow(session_ptr);
83
 
    DRIZZLE_CONNECTION_DONE(session_ptr->getSessionId());
84
 
  }
85
 
}
86
 
 
87
 
static void write_core(int sig)
88
 
{
89
 
  signal(sig, SIG_DFL);
90
 
#ifdef HAVE_gcov
91
 
  /*
92
 
    For GCOV build, crashing will prevent the writing of code coverage
93
 
    information from this process, causing gcov output to be incomplete.
94
 
    So we force the writing of coverage information here before terminating.
95
 
  */
96
 
  extern void __gcov_flush(void);
97
 
  __gcov_flush();
98
 
#endif
99
 
  pthread_kill(pthread_self(), sig);
100
 
#if defined(P_MYID) && !defined(SCO)
101
 
  /* On Solaris, the above kill is not enough */
102
 
  sigsend(P_PID,P_MYID,sig);
103
 
#endif
104
 
}
105
 
 
106
 
void drizzled_handle_segfault(int sig)
107
 
{
108
 
  time_t curr_time;
109
 
  struct tm tm;
110
 
 
111
 
  /*
112
 
    Strictly speaking, one needs a mutex here
113
 
    but since we have got SIGSEGV already, things are a mess
114
 
    so not having the mutex is not as bad as possibly using a buggy
115
 
    mutex - so we keep things simple
116
 
  */
117
 
  if (segfaulted)
118
 
  {
119
 
    fprintf(stderr, _("Fatal signal %d while backtracing\n"), sig);
120
 
    exit(1);
121
 
  }
122
 
 
123
 
  segfaulted= true;
124
 
 
125
 
  curr_time= time(NULL);
126
 
  if(curr_time == (time_t)-1)
127
 
  {
128
 
    fprintf(stderr, _("Fatal: time() call failed\n"));
129
 
    exit(1);
130
 
  }
131
 
 
132
 
  localtime_r(&curr_time, &tm);
133
 
  
134
 
  fprintf(stderr,_("%02d%02d%02d %2d:%02d:%02d - drizzled got signal %d;\n"
135
 
          "This could be because you hit a bug. It is also possible that "
136
 
          "this binary\n or one of the libraries it was linked against is "
137
 
          "corrupt, improperly built,\n or misconfigured. This error can "
138
 
          "also be caused by malfunctioning hardware.\n"),
139
 
          tm.tm_year % 100, tm.tm_mon+1, tm.tm_mday,
140
 
          tm.tm_hour, tm.tm_min, tm.tm_sec,
141
 
          sig);
142
 
  fprintf(stderr, _("We will try our best to scrape up some info that "
143
 
                    "will hopefully help diagnose\n"
144
 
                    "the problem, but since we have already crashed, "
145
 
                    "something is definitely wrong\nand this may fail.\n\n"));
146
 
  fprintf(stderr, "read_buffer_size=%ld\n", (long) global_system_variables.read_buff_size);
147
 
  fprintf(stderr, "max_used_connections=%"PRIu64"\n", current_global_counters.max_used_connections);
148
 
  fprintf(stderr, "connection_count=%u\n", uint32_t(connection_count));
149
 
  fprintf(stderr, _("It is possible that drizzled could use up to \n"
150
 
                    "(read_buffer_size + sort_buffer_size)*thread_count\n"
151
 
                    "bytes of memory\n"
152
 
                    "Hope that's ok; if not, decrease some variables in the "
153
 
                    "equation.\n\n"));
154
 
 
155
 
  drizzled::util::custom_backtrace();
156
 
 
157
 
  write_core(sig);
158
 
 
159
 
  exit(1);
160
 
}
161
 
 
162
 
} /* extern "C" */