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
#include <sys/types.h>
30
#if TIME_WITH_SYS_TIME
31
# include <sys/time.h>
35
# include <sys/time.h>
41
#if defined(HAVE_LOCALE_H)
45
#include <boost/filesystem.hpp>
47
#include "drizzled/plugin.h"
48
#include "drizzled/gettext.h"
49
#include "drizzled/configmake.h"
50
#include "drizzled/session.h"
51
#include "drizzled/internal/my_sys.h"
52
#include "drizzled/unireg.h"
53
#include "drizzled/drizzled.h"
54
#include "drizzled/errmsg_print.h"
55
#include "drizzled/data_home.h"
56
#include "drizzled/plugin/listen.h"
57
#include "drizzled/plugin/client.h"
58
#include "drizzled/pthread_globals.h"
59
#include "drizzled/tztime.h"
60
#include "drizzled/signal_handler.h"
61
#include "drizzled/replication_services.h"
63
using namespace drizzled;
65
namespace fs=boost::filesystem;
67
static pthread_t select_thread;
68
static uint32_t thr_kill_signal;
72
All global error messages are sent here where the first one is stored
75
static void my_message_sql(uint32_t error, const char *str, myf MyFlags)
79
Put here following assertion when situation with EE_* error codes
82
if ((session= current_session))
84
if (MyFlags & ME_FATALERROR)
85
session->is_fatal_error= 1;
88
TODO: There are two exceptions mechanism (Session and sp_rcontext),
89
this could be improved by having a common stack of handlers.
91
if (session->handle_error(error, str,
92
DRIZZLE_ERROR::WARN_LEVEL_ERROR))
96
session->lex->current_select == 0 if lex structure is not inited
97
(not query command (COM_QUERY))
99
if (! (session->lex->current_select &&
100
session->lex->current_select->no_error && !session->is_fatal_error))
102
if (! session->main_da.is_error()) // Return only first message
105
error= ER_UNKNOWN_ERROR;
108
session->main_da.set_error_status(error, str);
112
if (!session->no_warnings_for_error && !session->is_fatal_error)
115
Suppress infinite recursion if there a memory allocation error
118
session->no_warnings_for_error= true;
119
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
120
session->no_warnings_for_error= false;
123
if (!session || MyFlags & ME_NOREFRESH)
124
errmsg_printf(ERRMSG_LVL_ERROR, "%s: %s",internal::my_progname,str);
127
static void init_signals(void)
132
if (!(test_flags.test(TEST_NO_STACKTRACE) ||
133
test_flags.test(TEST_CORE_ON_SIGNAL)))
135
sa.sa_flags = SA_RESETHAND | SA_NODEFER;
136
sigemptyset(&sa.sa_mask);
137
sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
139
sa.sa_handler= drizzled_handle_segfault;
140
sigaction(SIGSEGV, &sa, NULL);
141
sigaction(SIGABRT, &sa, NULL);
143
sigaction(SIGBUS, &sa, NULL);
145
sigaction(SIGILL, &sa, NULL);
146
sigaction(SIGFPE, &sa, NULL);
149
if (test_flags.test(TEST_CORE_ON_SIGNAL))
151
/* Change limits so that we will get a core file */
153
rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
154
if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
155
errmsg_printf(ERRMSG_LVL_WARN,
156
_("setrlimit could not change the size of core files "
157
"to 'infinity'; We may not be able to generate a "
158
"core file on signals"));
160
(void) sigemptyset(&set);
161
ignore_signal(SIGPIPE);
162
sigaddset(&set,SIGPIPE);
163
#ifndef IGNORE_SIGHUP_SIGQUIT
164
sigaddset(&set,SIGQUIT);
165
sigaddset(&set,SIGHUP);
167
sigaddset(&set,SIGTERM);
169
/* Fix signals if blocked by parents (can happen on Mac OS X) */
170
sigemptyset(&sa.sa_mask);
172
sa.sa_handler = drizzled_print_signal_warning;
173
sigaction(SIGTERM, &sa, NULL);
175
sa.sa_handler = drizzled_print_signal_warning;
176
sigaction(SIGHUP, &sa, NULL);
178
sigaddset(&set,SIGTSTP);
180
if (test_flags.test(TEST_SIGINT))
183
sa.sa_handler= drizzled_end_thread_signal;
184
sigaction(thr_kill_signal, &sa, NULL);
187
sigdelset(&set, thr_kill_signal);
191
sigaddset(&set,SIGINT);
193
sigprocmask(SIG_SETMASK,&set,NULL);
194
pthread_sigmask(SIG_SETMASK,&set,NULL);
198
static void GoogleProtoErrorThrower(google::protobuf::LogLevel level, const char* filename,
199
int line, const string& message) throw(const char *)
206
case google::protobuf::LOGLEVEL_INFO:
208
case google::protobuf::LOGLEVEL_WARNING:
209
case google::protobuf::LOGLEVEL_ERROR:
210
case google::protobuf::LOGLEVEL_FATAL:
212
throw("error in google protocol buffer parsing");
216
int main(int argc, char **argv)
218
#if defined(ENABLE_NLS)
219
# if defined(HAVE_LOCALE_H)
220
setlocale(LC_ALL, "");
222
bindtextdomain("drizzle", LOCALEDIR);
223
textdomain("drizzle");
226
module::Registry &modules= module::Registry::singleton();
227
plugin::Client *client;
230
MY_INIT(argv[0]); // init my_sys library & pthreads
231
/* nothing should come before this line ^^^ */
233
/* Set signal used to kill Drizzle */
234
thr_kill_signal= SIGINT;
236
google::protobuf::SetLogHandler(&GoogleProtoErrorThrower);
238
/* Function generates error messages before abort */
239
/* init_common_variables must get basic settings such as data_home_dir
240
and plugin_load_list. */
241
if (init_common_variables(argc, argv))
242
unireg_abort(1); // Will do exit
247
select_thread=pthread_self();
248
select_thread_in_use=1;
252
if (chdir(getDataHome().c_str()))
254
errmsg_printf(ERRMSG_LVL_ERROR,
255
_("Data directory %s does not exist\n"),
256
getDataHome().c_str());
259
if (mkdir("local", 0700))
261
/* We don't actually care */
265
errmsg_printf(ERRMSG_LVL_ERROR,
266
_("Local catalog %s/local does not exist\n"),
267
getDataHome().c_str());
270
/* TODO: This is a hack until we can properly support std::string in sys_var*/
271
char **data_home_ptr= getDatadirPtr();
272
*data_home_ptr= new char[getDataHome().size()+1] ();
273
fs::path full_data_home_path(fs::system_complete(fs::path(getDataHome())));
274
std::string full_data_home(full_data_home_path.file_string());
275
memcpy(*data_home_ptr, full_data_home.c_str(), full_data_home.size());
276
getDataHomeCatalog()= "./";
277
getDataHome()= "../";
287
if (init_server_components(modules))
291
* This check must be done after init_server_components for now
292
* because we don't yet have plugin dependency tracking...
294
* ReplicationServices::evaluateRegisteredPlugins() will print error messages to stderr
295
* via errmsg_printf().
299
* not checking return since unireg_abort() hangs
301
ReplicationServices &replication_services= ReplicationServices::singleton();
302
(void) replication_services.evaluateRegisteredPlugins();
304
if (plugin::Listen::setup())
309
After this we can't quit by a simple unireg_abort
311
error_handler_hook= my_message_sql;
313
assert(plugin::num_trx_monitored_objects > 0);
314
if (drizzle_rm_tmp_tables() ||
315
my_tz_init((Session *)0, default_tz_name))
318
select_thread_in_use=0;
319
(void) pthread_kill(signal_thread, SIGTERM);
321
(void) unlink(pidfile_name); // Not needed anymore
326
errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_STARTUP)), internal::my_progname,
327
PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
330
/* Listen for new connections and start new session for each connection
331
accepted. The listen.getClient() method will return NULL when the server
332
should be shutdown. */
333
while ((client= plugin::Listen::getClient()) != NULL)
335
if (!(session= new Session(client)))
341
/* If we error on creation we drop the connection and delete the session. */
342
if (session->schedule())
343
Session::unlink(session);
346
LOCK_thread_count.lock();
347
select_thread_in_use=0; // For close_connections
348
LOCK_thread_count.unlock();
349
COND_thread_count.notify_all();
351
/* Wait until cleanup is done */
352
LOCK_thread_count.lock();
353
while (!ready_to_exit)
354
pthread_cond_wait(COND_server_end.native_handle(), LOCK_thread_count.native_handle());
355
LOCK_thread_count.unlock();
358
module::Registry::shutdown();