45
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/unireg.h>
67
#include <drizzled/util/backtrace.h>
68
#include <drizzled/current_session.h>
69
#include <drizzled/daemon.h>
70
#include <drizzled/diagnostics_area.h>
71
#include <drizzled/sql_base.h>
72
#include <drizzled/sql_lex.h>
73
#include <drizzled/system_variables.h>
47
#include "drizzled/plugin.h"
48
#include "drizzled/gettext.h"
49
#include "drizzled/configmake.h"
50
#include "drizzled/session.h"
51
#include "drizzled/session/cache.h"
52
#include "drizzled/internal/my_sys.h"
53
#include "drizzled/unireg.h"
54
#include "drizzled/drizzled.h"
55
#include "drizzled/errmsg_print.h"
56
#include "drizzled/data_home.h"
57
#include "drizzled/plugin/listen.h"
58
#include "drizzled/plugin/client.h"
59
#include "drizzled/pthread_globals.h"
60
#include "drizzled/tztime.h"
61
#include "drizzled/signal_handler.h"
62
#include "drizzled/replication_services.h"
63
#include "drizzled/transaction_services.h"
65
#include "drizzled/util/backtrace.h"
75
67
using namespace drizzled;
76
68
using namespace std;
69
namespace fs=boost::filesystem;
78
71
static pthread_t select_thread;
79
72
static uint32_t thr_kill_signal;
81
extern bool opt_daemon;
85
76
All global error messages are sent here where the first one is stored
88
static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
79
static void my_message_sql(uint32_t error, const char *str, myf MyFlags)
90
Session* session= current_session;
92
83
Put here following assertion when situation with EE_* error codes
86
if ((session= current_session))
97
88
if (MyFlags & ME_FATALERROR)
98
89
session->is_fatal_error= 1;
101
@TODO There are two exceptions mechanism (Session and sp_rcontext),
92
TODO: There are two exceptions mechanism (Session and sp_rcontext),
102
93
this could be improved by having a common stack of handlers.
104
if (session->handle_error(error, str, DRIZZLE_ERROR::WARN_LEVEL_ERROR))
95
if (session->handle_error(error, str,
96
DRIZZLE_ERROR::WARN_LEVEL_ERROR))
109
session->lex().current_select == 0 if lex structure is not inited
100
session->lex->current_select == 0 if lex structure is not inited
110
101
(not query command (COM_QUERY))
112
if (! (session->lex().current_select &&
113
session->lex().current_select->no_error && !session->is_fatal_error))
103
if (! (session->lex->current_select &&
104
session->lex->current_select->no_error && !session->is_fatal_error))
115
if (! session->main_da().is_error()) // Return only first message
106
if (! session->main_da.is_error()) // Return only first message
118
109
error= ER_UNKNOWN_ERROR;
123
session->main_da().set_error_status(error, str);
112
session->main_da.set_error_status(error, str);
164
150
sigaction(SIGFPE, &sa, NULL);
167
if (getDebug().test(debug::CORE_ON_SIGNAL))
153
if (test_flags.test(TEST_CORE_ON_SIGNAL))
169
155
/* Change limits so that we will get a core file */
170
156
struct rlimit rl;
171
157
rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
172
158
if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
173
errmsg_printf(error::WARN,
159
errmsg_printf(ERRMSG_LVL_WARN,
174
160
_("setrlimit could not change the size of core files "
175
161
"to 'infinity'; We may not be able to generate a "
176
162
"core file on signals"));
254
250
/* Function generates error messages before abort */
255
251
error_handler_hook= my_message_sql;
257
/* init_common_variables must get basic settings such as data_home_dir and plugin_load_list. */
258
if (not init_variables_before_daemonizing(argc, argv))
260
unireg_abort << "init_variables_before_daemonizing() failed"; // Will do exit
263
if (opt_daemon and was_help_requested() == false)
265
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
267
perror("Failed to ignore SIGHUP");
271
unireg_abort << "--daemon failed";
275
if (not init_variables_after_daemonizing(modules))
277
unireg_abort << "init_variables_after_daemonizing() failed"; // Will do exit
252
/* init_common_variables must get basic settings such as data_home_dir
253
and plugin_load_list. */
254
if (init_common_variables(argc, argv, modules))
255
unireg_abort(1); // Will do exit
282
258
init signals & alarm
288
select_thread= pthread_self();
264
select_thread=pthread_self();
289
265
select_thread_in_use=1;
291
if (was_help_requested() == false)
293
269
if (chdir(getDataHome().file_string().c_str()))
295
unireg_abort << "Data directory " << getDataHome().file_string() << " does not exist";
298
ifstream old_uuid_file ("server.uuid");
299
if (old_uuid_file.is_open())
301
getline(old_uuid_file, server_uuid);
302
old_uuid_file.close();
307
char uuid_string[37];
308
uuid_generate_random(uu);
309
uuid_unparse(uu, uuid_string);
310
ofstream new_uuid_file ("server.uuid");
311
new_uuid_file << uuid_string;
312
new_uuid_file.close();
313
server_uuid= string(uuid_string);
316
if (mkdir("local", 0700) == -1)
326
unireg_abort << "Could not create local catalog, permission denied in directory:" << getcwd(cwd, sizeof(cwd));
332
unireg_abort << "Could not create local catalog, in directory:" << getcwd(cwd, sizeof(cwd)) << " system error was:" << strerror(errno);
337
if (chdir("local") == -1)
339
unireg_abort << "Local catalog does not exist, was unable to chdir() to " << getDataHome().file_string();
342
setFullDataHome(boost::filesystem::system_complete(getDataHome()));
343
errmsg_printf(error::INFO, "Data Home directory is : %s", getFullDataHome().native_file_string().c_str());
271
errmsg_printf(ERRMSG_LVL_ERROR,
272
_("Data directory %s does not exist\n"),
273
getDataHome().file_string().c_str());
276
if (mkdir("local", 0700))
278
/* We don't actually care */
282
errmsg_printf(ERRMSG_LVL_ERROR,
283
_("Local catalog %s/local does not exist\n"),
284
getDataHome().file_string().c_str());
288
full_data_home= fs::system_complete(getDataHome());
289
getDataHomeCatalog()= "./";
290
getDataHome()= "../";
346
295
if (server_id == 0)
353
init_server_components(modules);
355
catch (abort_exception& ex)
358
cout << _("Drizzle has receieved an abort event.") << endl;
359
cout << _("In Function: ") << *::boost::get_error_info<boost::throw_function>(ex) << endl;
360
cout << _("In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
361
cout << _("On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
363
unireg_abort << "init_server_components() failed";
300
if (init_server_components(modules))
368
304
* This check must be done after init_server_components for now
376
312
* not checking return since unireg_abort() hangs
378
(void) ReplicationServices::evaluateRegisteredPlugins();
314
ReplicationServices &replication_services= ReplicationServices::singleton();
315
(void) replication_services.evaluateRegisteredPlugins();
380
317
if (plugin::Listen::setup())
382
unireg_abort << "Failed plugin::Listen::setup()";
385
321
assert(plugin::num_trx_monitored_objects > 0);
386
drizzle_rm_tmp_tables();
387
errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname, PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
322
if (drizzle_rm_tmp_tables() ||
323
my_tz_init((Session *)0, default_tz_name))
326
select_thread_in_use=0;
327
(void) pthread_kill(signal_thread, SIGTERM);
329
(void) unlink(pid_file.file_string().c_str()); // Not needed anymore
334
errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_STARTUP)), internal::my_progname,
335
PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
338
TransactionServices &transaction_services= TransactionServices::singleton();
389
340
/* Send server startup event */
391
Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
392
setCurrentSession(session.get());
393
TransactionServices::sendStartupEvent(*session);
394
plugin_startup_window(modules, *session.get());
403
Listen for new connections and start new session for each connection
344
if ((session= new Session(plugin::Listen::getNullClient())))
346
currentSession().release();
347
currentSession().reset(session);
348
transaction_services.sendStartupEvent(session);
354
/* Listen for new connections and start new session for each connection
404
355
accepted. The listen.getClient() method will return NULL when the server
407
while (plugin::Client* client= plugin::Listen::getClient())
356
should be shutdown. */
357
while ((client= plugin::Listen::getClient()) != NULL)
409
Session::shared_ptr session= Session::make_shared(client, client->catalog());
359
Session::shared_ptr session(new Session(client));
411
367
/* If we error on creation we drop the connection and delete the session. */
412
368
if (Session::schedule(session))
416
372
/* Send server shutdown event */
418
Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
419
setCurrentSession(session.get());
420
TransactionServices::sendShutdownEvent(*session.get());
376
if ((session= new Session(plugin::Listen::getNullClient())))
378
currentSession().release();
379
currentSession().reset(session);
380
transaction_services.sendShutdownEvent(session);
424
boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
386
boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
425
387
select_thread_in_use= false; // For close_connections
427
389
COND_thread_count.notify_all();
429
391
/* Wait until cleanup is done */
430
session::Cache::shutdownSecond();
393
boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
394
while (not ready_to_exit)
395
COND_server_end.wait(scopedLock);
433
399
module::Registry::shutdown();