drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
drizzled/signal_handler.cc
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#include <drizzled/util/backtrace.h>
34#include <drizzled/statistics_variables.h>
35#include <drizzled/system_variables.h>
36
37#define DRIZZLE_UNIX_SOCKET_PATH "/tmp/mysql.socket"
38
39using namespace drizzled;
40namespace fs= boost::filesystem;
41
42static uint32_t killed_threads;
43static bool segfaulted= false;
44
45/*
46 * We declare these extern "C" because they are passed to system callback functions.
47 * We prefix them because extern "C"-ing something effectively removes the namespace
48 * from the linker symbols, meaning they would be exporting symbols like
49 * "print_signal_warning".
50 */
51extern "C"
52{
53
54void drizzled_print_signal_warning(int sig)
55{
56 if (global_system_variables.log_warnings)
57 errmsg_printf(error::WARN, _("Got signal %d from thread %" PRIu32),
58 sig, global_thread_id);
59 sigset_t set;
60 sigemptyset(&set);
61
62 struct sigaction sa;
63 sa.sa_handler= drizzled_print_signal_warning;
64 sa.sa_mask= set;
65 sa.sa_flags= 0;
66 sigaction(sig, &sa, NULL); /* int. thread system calls */
67 if (sig == SIGALRM)
68 alarm(2); /* reschedule alarm */
69}
70
72void drizzled_end_thread_signal(int )
73{
74 Session *session= current_session;
75 if (session)
76 {
77 Session::shared_ptr session_ptr(session::Cache::find(session->getSessionId()));
78 if (not session_ptr) // We need to make we have a lock on session before we do anything with it.
79 return;
80
81 killed_threads++;
82
83 // We need to get the ID before we kill off the session
84 session_ptr->scheduler->killSessionNow(session_ptr);
85 DRIZZLE_CONNECTION_DONE(session_ptr->getSessionId());
86 }
87}
88
89static void write_core(int sig)
90{
91 signal(sig, SIG_DFL);
92#ifdef HAVE_gcov
93 /*
94 For GCOV build, crashing will prevent the writing of code coverage
95 information from this process, causing gcov output to be incomplete.
96 So we force the writing of coverage information here before terminating.
97 */
98 extern void __gcov_flush(void);
99 __gcov_flush();
100#endif
101 pthread_kill(pthread_self(), sig);
102}
103
104void drizzled_handle_segfault(int sig)
105{
106 time_t curr_time;
107 struct tm tm;
108
109 /*
110 Strictly speaking, one needs a mutex here
111 but since we have got SIGSEGV already, things are a mess
112 so not having the mutex is not as bad as possibly using a buggy
113 mutex - so we keep things simple
114 */
115 if (segfaulted)
116 {
117 fprintf(stderr, _("Fatal signal %d while backtracing\n"), sig);
118 exit(1);
119 }
120
121 segfaulted= true;
122
123 curr_time= time(NULL);
124 if(curr_time == (time_t)-1)
125 {
126 fprintf(stderr, _("Fatal: time() call failed\n"));
127 exit(1);
128 }
129
130 localtime_r(&curr_time, &tm);
131
132 fprintf(stderr,_("%02d%02d%02d %2d:%02d:%02d - drizzled got signal %d;\n"
133 "This could be because you hit a bug. It is also possible that "
134 "this binary\n or one of the libraries it was linked against is "
135 "corrupt, improperly built,\n or misconfigured. This error can "
136 "also be caused by malfunctioning hardware.\n"),
137 tm.tm_year % 100, tm.tm_mon+1, tm.tm_mday,
138 tm.tm_hour, tm.tm_min, tm.tm_sec,
139 sig);
140 fprintf(stderr, _("We will try our best to scrape up some info that "
141 "will hopefully help diagnose\n"
142 "the problem, but since we have already crashed, "
143 "something is definitely wrong\nand this may fail.\n\n"));
144 fprintf(stderr, "read_buffer_size=%ld\n", (long) global_system_variables.read_buff_size);
145 fprintf(stderr, "max_used_connections=%" PRIu64 "\n", current_global_counters.max_used_connections);
146 fprintf(stderr, "connection_count=%u\n", uint32_t(connection_count));
147 fprintf(stderr, _("It is possible that drizzled could use up to \n"
148 "(read_buffer_size + sort_buffer_size)*thread_count\n"
149 "bytes of memory\n"
150 "Hope that's ok; if not, decrease some variables in the "
151 "equation.\n\n"));
152
153 call_backtrace();
154
155 write_core(sig);
156
157 exit(1);
158}
159
160} /* extern "C" */
TODO: Rename this file - func.h is stupid.