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, Inc.
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/abort_exception.h>
48
#include <drizzled/catalog/local.h>
49
#include <drizzled/configmake.h>
50
#include <drizzled/data_home.h>
51
#include <drizzled/debug.h>
52
#include <drizzled/drizzled.h>
53
#include <drizzled/errmsg_print.h>
54
#include <drizzled/gettext.h>
55
#include <drizzled/internal/my_sys.h>
56
#include <drizzled/plugin.h>
57
#include <drizzled/plugin/client.h>
58
#include <drizzled/plugin/listen.h>
59
#include <drizzled/plugin/monitored_in_transaction.h>
60
#include <drizzled/pthread_globals.h>
61
#include <drizzled/replication_services.h>
62
#include <drizzled/session.h>
63
#include <drizzled/session/cache.h>
64
#include <drizzled/signal_handler.h>
65
#include <drizzled/transaction_services.h>
66
#include <drizzled/tztime.h>
67
#include <drizzled/unireg.h>
68
#include <drizzled/util/backtrace.h>
69
#include <drizzled/current_session.h>
70
#include <drizzled/daemon.h>
72
using namespace drizzled;
75
static pthread_t select_thread;
76
static uint32_t thr_kill_signal;
78
extern bool opt_daemon;
82
All global error messages are sent here where the first one is stored
85
static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
89
Put here following assertion when situation with EE_* error codes
92
if ((session= current_session))
94
if (MyFlags & ME_FATALERROR)
95
session->is_fatal_error= 1;
98
@TODO There are two exceptions mechanism (Session and sp_rcontext),
99
this could be improved by having a common stack of handlers.
101
if (session->handle_error(error, str, DRIZZLE_ERROR::WARN_LEVEL_ERROR))
105
session->getLex()->current_select == 0 if lex structure is not inited
106
(not query command (COM_QUERY))
108
if (! (session->getLex()->current_select &&
109
session->getLex()->current_select->no_error && !session->is_fatal_error))
111
if (! session->main_da.is_error()) // Return only first message
114
error= ER_UNKNOWN_ERROR;
119
session->main_da.set_error_status(error, str);
123
if (!session->no_warnings_for_error && !session->is_fatal_error)
126
Suppress infinite recursion if there a memory allocation error
129
session->no_warnings_for_error= true;
130
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
131
session->no_warnings_for_error= false;
135
if (not session || MyFlags & ME_NOREFRESH)
137
errmsg_printf(error::ERROR, "%s: %s",internal::my_progname,str);
141
static void init_signals(void)
146
if (not (getDebug().test(debug::NO_STACKTRACE) ||
147
getDebug().test(debug::CORE_ON_SIGNAL)))
149
sa.sa_flags = SA_RESETHAND | SA_NODEFER;
150
sigemptyset(&sa.sa_mask);
151
sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
153
sa.sa_handler= drizzled_handle_segfault;
154
sigaction(SIGSEGV, &sa, NULL);
155
sigaction(SIGABRT, &sa, NULL);
157
sigaction(SIGBUS, &sa, NULL);
159
sigaction(SIGILL, &sa, NULL);
160
sigaction(SIGFPE, &sa, NULL);
163
if (getDebug().test(debug::CORE_ON_SIGNAL))
165
/* Change limits so that we will get a core file */
167
rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
168
if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
169
errmsg_printf(error::WARN,
170
_("setrlimit could not change the size of core files "
171
"to 'infinity'; We may not be able to generate a "
172
"core file on signals"));
174
(void) sigemptyset(&set);
175
ignore_signal(SIGPIPE);
176
sigaddset(&set,SIGPIPE);
177
#ifndef IGNORE_SIGHUP_SIGQUIT
178
sigaddset(&set,SIGQUIT);
179
sigaddset(&set,SIGHUP);
181
sigaddset(&set,SIGTERM);
183
/* Fix signals if blocked by parents (can happen on Mac OS X) */
184
sigemptyset(&sa.sa_mask);
186
sa.sa_handler = drizzled_print_signal_warning;
187
sigaction(SIGTERM, &sa, NULL);
189
sa.sa_handler = drizzled_print_signal_warning;
190
sigaction(SIGHUP, &sa, NULL);
192
sigaddset(&set,SIGTSTP);
194
if (getDebug().test(debug::ALLOW_SIGINT))
197
sa.sa_handler= drizzled_end_thread_signal;
198
sigaction(thr_kill_signal, &sa, NULL);
201
sigdelset(&set, thr_kill_signal);
205
sigaddset(&set,SIGINT);
207
sigprocmask(SIG_SETMASK,&set,NULL);
208
pthread_sigmask(SIG_SETMASK,&set,NULL);
212
static void GoogleProtoErrorThrower(google::protobuf::LogLevel level,
214
int, const string& ) throw(const char *)
218
case google::protobuf::LOGLEVEL_INFO:
220
case google::protobuf::LOGLEVEL_WARNING:
221
case google::protobuf::LOGLEVEL_ERROR:
222
case google::protobuf::LOGLEVEL_FATAL:
224
throw("error in google protocol buffer parsing");
228
int main(int argc, char **argv)
230
#if defined(ENABLE_NLS)
231
# if defined(HAVE_LOCALE_H)
232
setlocale(LC_ALL, "");
234
bindtextdomain("drizzle7", LOCALEDIR);
235
textdomain("drizzle7");
238
module::Registry &modules= module::Registry::singleton();
240
MY_INIT(argv[0]); // init my_sys library & pthreads
241
/* nothing should come before this line ^^^ */
243
/* Set signal used to kill Drizzle */
244
thr_kill_signal= SIGINT;
246
google::protobuf::SetLogHandler(&GoogleProtoErrorThrower);
248
/* Function generates error messages before abort */
249
error_handler_hook= my_message_sql;
251
/* init_common_variables must get basic settings such as data_home_dir
252
and plugin_load_list. */
253
if (init_basic_variables(argc, argv))
254
unireg_abort(1); // Will do exit
258
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
260
perror("Failed to ignore SIGHUP");
264
fprintf(stderr, "failed to daemon() in order to daemonize\n");
269
if (init_remaining_variables(modules))
270
unireg_abort(1); // Will do exit
274
After this we can't quit by a simple unireg_abort
279
select_thread=pthread_self();
280
select_thread_in_use=1;
284
if (chdir(getDataHome().file_string().c_str()))
286
errmsg_printf(error::ERROR,
287
_("Data directory %s does not exist\n"),
288
getDataHome().file_string().c_str());
291
if (mkdir("local", 0700))
293
/* We don't actually care */
297
errmsg_printf(error::ERROR,
298
_("Local catalog %s/local does not exist\n"),
299
getDataHome().file_string().c_str());
303
boost::filesystem::path &full_data_home= getFullDataHome();
304
full_data_home= boost::filesystem::system_complete(getDataHome());
305
errmsg_printf(error::INFO, "Data Home directory is : %s", full_data_home.native_file_string().c_str());
317
if (init_server_components(modules))
320
catch (abort_exception& ex)
323
cout << _("Drizzle has receieved an abort event.") << endl;
324
cout << _("In Function: ") << *::boost::get_error_info<boost::throw_function>(ex) << endl;
325
cout << _("In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
326
cout << _("On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
333
* This check must be done after init_server_components for now
334
* because we don't yet have plugin dependency tracking...
336
* ReplicationServices::evaluateRegisteredPlugins() will print error messages to stderr
337
* via errmsg_printf().
341
* not checking return since unireg_abort() hangs
343
ReplicationServices &replication_services= ReplicationServices::singleton();
344
(void) replication_services.evaluateRegisteredPlugins();
346
if (plugin::Listen::setup())
349
assert(plugin::num_trx_monitored_objects > 0);
350
if (drizzle_rm_tmp_tables())
353
select_thread_in_use=0;
354
(void) pthread_kill(signal_thread, SIGTERM);
356
(void) unlink(pid_file.file_string().c_str()); // Not needed anymore
361
errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname,
362
PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
365
TransactionServices &transaction_services= TransactionServices::singleton();
367
/* Send server startup event */
369
Session::shared_ptr session;
371
if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
373
currentSession().release();
374
currentSession().reset(session.get());
377
transaction_services.sendStartupEvent(*session);
379
plugin_startup_window(modules, *(session.get()));
387
Listen for new connections and start new session for each connection
388
accepted. The listen.getClient() method will return NULL when the server
391
plugin::Client *client;
392
while ((client= plugin::Listen::getClient()) != NULL)
394
Session::shared_ptr session;
395
session= Session::make_shared(client, client->catalog());
403
/* If we error on creation we drop the connection and delete the session. */
404
if (Session::schedule(session))
405
Session::unlink(session);
408
/* Send server shutdown event */
410
Session::shared_ptr session;
412
if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
414
currentSession().release();
415
currentSession().reset(session.get());
416
transaction_services.sendShutdownEvent(*session.get());
421
boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
422
select_thread_in_use= false; // For close_connections
424
COND_thread_count.notify_all();
426
/* Wait until cleanup is done */
427
session::Cache::singleton().shutdownSecond();
430
module::Registry::shutdown();