1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
24
#include <sys/resource.h>
27
#if TIME_WITH_SYS_TIME
28
# include <sys/time.h>
32
# include <sys/time.h>
38
#if defined(HAVE_LOCALE_H)
43
#include "drizzled/plugin.h"
44
#include "drizzled/gettext.h"
45
#include "drizzled/configmake.h"
46
#include "drizzled/session.h"
47
#include "drizzled/internal/my_sys.h"
48
#include "drizzled/unireg.h"
49
#include "drizzled/drizzled.h"
50
#include "drizzled/errmsg_print.h"
51
#include "drizzled/data_home.h"
52
#include "drizzled/plugin/listen.h"
53
#include "drizzled/plugin/client.h"
54
#include "drizzled/pthread_globals.h"
55
#include "drizzled/tztime.h"
56
#include "drizzled/signal_handler.h"
57
#include "drizzled/replication_services.h"
59
using namespace drizzled;
62
static pthread_t select_thread;
63
static uint32_t thr_kill_signal;
66
All global error messages are sent here where the first one is stored
69
static void my_message_sql(uint32_t error, const char *str, myf MyFlags)
73
Put here following assertion when situation with EE_* error codes
76
if ((session= current_session))
78
if (MyFlags & ME_FATALERROR)
79
session->is_fatal_error= 1;
82
TODO: There are two exceptions mechanism (Session and sp_rcontext),
83
this could be improved by having a common stack of handlers.
85
if (session->handle_error(error, str,
86
DRIZZLE_ERROR::WARN_LEVEL_ERROR))
90
session->lex->current_select == 0 if lex structure is not inited
91
(not query command (COM_QUERY))
93
if (! (session->lex->current_select &&
94
session->lex->current_select->no_error && !session->is_fatal_error))
96
if (! session->main_da.is_error()) // Return only first message
99
error= ER_UNKNOWN_ERROR;
102
session->main_da.set_error_status(error, str);
106
if (!session->no_warnings_for_error && !session->is_fatal_error)
109
Suppress infinite recursion if there a memory allocation error
112
session->no_warnings_for_error= true;
113
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
114
session->no_warnings_for_error= false;
117
if (!session || MyFlags & ME_NOREFRESH)
118
errmsg_printf(ERRMSG_LVL_ERROR, "%s: %s",internal::my_progname,str);
121
static void init_signals(void)
126
if (!(test_flags.test(TEST_NO_STACKTRACE) ||
127
test_flags.test(TEST_CORE_ON_SIGNAL)))
129
sa.sa_flags = SA_RESETHAND | SA_NODEFER;
130
sigemptyset(&sa.sa_mask);
131
sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
133
sa.sa_handler= drizzled_handle_segfault;
134
sigaction(SIGSEGV, &sa, NULL);
135
sigaction(SIGABRT, &sa, NULL);
137
sigaction(SIGBUS, &sa, NULL);
139
sigaction(SIGILL, &sa, NULL);
140
sigaction(SIGFPE, &sa, NULL);
143
if (test_flags.test(TEST_CORE_ON_SIGNAL))
145
/* Change limits so that we will get a core file */
147
rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
148
if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
149
errmsg_printf(ERRMSG_LVL_WARN,
150
_("setrlimit could not change the size of core files "
151
"to 'infinity'; We may not be able to generate a "
152
"core file on signals"));
154
(void) sigemptyset(&set);
155
ignore_signal(SIGPIPE);
156
sigaddset(&set,SIGPIPE);
157
#ifndef IGNORE_SIGHUP_SIGQUIT
158
sigaddset(&set,SIGQUIT);
159
sigaddset(&set,SIGHUP);
161
sigaddset(&set,SIGTERM);
163
/* Fix signals if blocked by parents (can happen on Mac OS X) */
164
sigemptyset(&sa.sa_mask);
166
sa.sa_handler = drizzled_print_signal_warning;
167
sigaction(SIGTERM, &sa, NULL);
169
sa.sa_handler = drizzled_print_signal_warning;
170
sigaction(SIGHUP, &sa, NULL);
172
sigaddset(&set,SIGTSTP);
174
if (test_flags.test(TEST_SIGINT))
177
sa.sa_handler= drizzled_end_thread_signal;
178
sigaction(thr_kill_signal, &sa, NULL);
181
sigdelset(&set, thr_kill_signal);
185
sigaddset(&set,SIGINT);
187
sigprocmask(SIG_SETMASK,&set,NULL);
188
pthread_sigmask(SIG_SETMASK,&set,NULL);
192
static void GoogleProtoErrorThrower(google::protobuf::LogLevel level, const char* filename,
193
int line, const string& message) throw(const char *)
200
case google::protobuf::LOGLEVEL_INFO:
202
case google::protobuf::LOGLEVEL_WARNING:
203
case google::protobuf::LOGLEVEL_ERROR:
204
case google::protobuf::LOGLEVEL_FATAL:
206
throw("error in google protocol buffer parsing");
210
int main(int argc, char **argv)
212
#if defined(ENABLE_NLS)
213
# if defined(HAVE_LOCALE_H)
214
setlocale(LC_ALL, "");
216
bindtextdomain("drizzle", LOCALEDIR);
217
textdomain("drizzle");
220
module::Registry &modules= module::Registry::singleton();
221
plugin::Client *client;
224
MY_INIT(argv[0]); // init my_sys library & pthreads
225
/* nothing should come before this line ^^^ */
227
/* Set signal used to kill Drizzle */
228
thr_kill_signal= SIGINT;
230
google::protobuf::SetLogHandler(&GoogleProtoErrorThrower);
232
/* Function generates error messages before abort */
233
/* init_common_variables must get basic settings such as data_home_dir
234
and plugin_load_list. */
235
if (init_common_variables(argc, argv))
236
unireg_abort(1); // Will do exit
241
select_thread=pthread_self();
242
select_thread_in_use=1;
244
if (chdir(data_home_real) && !opt_help)
246
errmsg_printf(ERRMSG_LVL_ERROR, _("Data directory %s does not exist\n"), data_home_real);
249
data_home= data_home_buff;
250
data_home[0]=FN_CURLIB; // all paths are relative from here
259
if (init_server_components(modules))
263
* This check must be done after init_server_components for now
264
* because we don't yet have plugin dependency tracking...
266
* ReplicationServices::evaluateRegisteredPlugins() will print error messages to stderr
267
* via errmsg_printf().
271
* not checking return since unireg_abort() hangs
273
ReplicationServices &replication_services= ReplicationServices::singleton();
274
(void) replication_services.evaluateRegisteredPlugins();
276
if (plugin::Listen::setup())
281
After this we can't quit by a simple unireg_abort
283
error_handler_hook= my_message_sql;
285
assert(plugin::num_trx_monitored_objects > 0);
286
if (drizzle_rm_tmp_tables() ||
287
my_tz_init((Session *)0, default_tz_name))
290
select_thread_in_use=0;
291
(void) pthread_kill(signal_thread, SIGTERM);
293
(void) unlink(pidfile_name); // Not needed anymore
298
errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_STARTUP)), internal::my_progname,
299
PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
302
/* Listen for new connections and start new session for each connection
303
accepted. The listen.getClient() method will return NULL when the server
304
should be shutdown. */
305
while ((client= plugin::Listen::getClient()) != NULL)
307
if (!(session= new Session(client)))
313
/* If we error on creation we drop the connection and delete the session. */
314
if (session->schedule())
315
Session::unlink(session);
318
LOCK_thread_count.lock();
319
select_thread_in_use=0; // For close_connections
320
LOCK_thread_count.unlock();
321
COND_thread_count.notify_all();
323
/* Wait until cleanup is done */
324
LOCK_thread_count.lock();
325
while (!ready_to_exit)
326
pthread_cond_wait(COND_server_end.native_handle(), LOCK_thread_count.native_handle());
327
LOCK_thread_count.unlock();
330
module::Registry::shutdown();