63
63
#include <drizzled/session/cache.h>
64
64
#include <drizzled/signal_handler.h>
65
65
#include <drizzled/transaction_services.h>
66
#include <drizzled/tztime.h>
67
66
#include <drizzled/unireg.h>
68
67
#include <drizzled/util/backtrace.h>
69
68
#include <drizzled/current_session.h>
70
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>
72
75
using namespace drizzled;
73
76
using namespace std;
85
88
static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
90
Session* session= current_session;
89
92
Put here following assertion when situation with EE_* error codes
92
if ((session= current_session))
94
97
if (MyFlags & ME_FATALERROR)
95
99
session->is_fatal_error= 1;
98
103
@TODO There are two exceptions mechanism (Session and sp_rcontext),
99
104
this could be improved by having a common stack of handlers.
101
106
if (session->handle_error(error, str, DRIZZLE_ERROR::WARN_LEVEL_ERROR))
105
session->getLex()->current_select == 0 if lex structure is not inited
111
session->lex().current_select == 0 if lex structure is not inited
106
112
(not query command (COM_QUERY))
108
if (! (session->getLex()->current_select &&
109
session->getLex()->current_select->no_error && !session->is_fatal_error))
114
if (! (session->lex().current_select &&
115
session->lex().current_select->no_error && !session->is_fatal_error))
111
if (! session->main_da.is_error()) // Return only first message
117
if (! session->main_da().is_error()) // Return only first message
113
119
if (error == EE_OK)
114
120
error= ER_UNKNOWN_ERROR;
238
244
module::Registry &modules= module::Registry::singleton();
240
MY_INIT(argv[0]); // init my_sys library & pthreads
246
drizzled::internal::my_progname= argv[0];
247
drizzled::internal::my_init();
241
249
/* nothing should come before this line ^^^ */
243
251
/* Set signal used to kill Drizzle */
248
256
/* Function generates error messages before abort */
249
257
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
259
/* init_common_variables must get basic settings such as data_home_dir and plugin_load_list. */
260
if (not init_variables_before_daemonizing(argc, argv))
262
unireg_abort << "init_variables_before_daemonizing() failed"; // Will do exit
265
if (opt_daemon and was_help_requested() == false)
258
267
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
264
fprintf(stderr, "failed to daemon() in order to daemonize\n");
273
unireg_abort << "--daemon failed";
269
if (init_remaining_variables(modules))
270
unireg_abort(1); // Will do exit
277
if (not init_variables_after_daemonizing(modules))
279
unireg_abort << "init_variables_after_daemonizing() failed"; // Will do exit
273
284
init signals & alarm
279
select_thread=pthread_self();
290
select_thread= pthread_self();
280
291
select_thread_in_use=1;
293
if (was_help_requested() == false)
284
295
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());
297
unireg_abort << "Data directory " << getDataHome().file_string() << " does not exist";
300
ifstream old_uuid_file ("server.uuid");
301
if (old_uuid_file.is_open())
303
getline(old_uuid_file, server_uuid);
304
old_uuid_file.close();
309
char uuid_string[37];
310
uuid_generate_random(uu);
311
uuid_unparse(uu, uuid_string);
312
ofstream new_uuid_file ("server.uuid");
313
new_uuid_file << uuid_string;
314
new_uuid_file.close();
315
server_uuid= string(uuid_string);
318
if (mkdir("local", 0700) == -1)
328
unireg_abort << "Could not create local catalog, permission denied in directory:" << getcwd(cwd, sizeof(cwd));
334
unireg_abort << "Could not create local catalog, in directory:" << getcwd(cwd, sizeof(cwd)) << " system error was:" << strerror(errno);
339
if (chdir("local") == -1)
341
unireg_abort << "Local catalog does not exist, was unable to chdir() to " << getDataHome().file_string();
344
setFullDataHome(boost::filesystem::system_complete(getDataHome()));
345
errmsg_printf(error::INFO, "Data Home directory is : %s", getFullDataHome().native_file_string().c_str());
310
348
if (server_id == 0)
341
378
* not checking return since unireg_abort() hangs
343
ReplicationServices &replication_services= ReplicationServices::singleton();
344
(void) replication_services.evaluateRegisteredPlugins();
380
(void) ReplicationServices::evaluateRegisteredPlugins();
346
382
if (plugin::Listen::setup())
384
unireg_abort << "Failed plugin::Listen::setup()";
349
387
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();
388
drizzle_rm_tmp_tables();
389
errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname, PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
367
391
/* 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()));
393
Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
394
setCurrentSession(session.get());
395
TransactionServices::sendStartupEvent(*session);
396
plugin_startup_window(modules, *session.get());
384
401
daemon_is_ready();
405
errmsg_printf(error::INFO, "Drizzle startup complete, listening for connections will now begin.");
387
408
Listen for new connections and start new session for each connection
388
409
accepted. The listen.getClient() method will return NULL when the server
389
410
should be shutdown.
391
plugin::Client *client;
392
while ((client= plugin::Listen::getClient()) != NULL)
412
while (plugin::Client* client= plugin::Listen::getClient())
394
Session::shared_ptr session;
395
session= Session::make_shared(client, client->catalog());
414
Session::shared_ptr session= Session::make_shared(client, client->catalog());
403
416
/* If we error on creation we drop the connection and delete the session. */
404
417
if (Session::schedule(session))
408
421
/* 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());
423
Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
424
setCurrentSession(session.get());
425
TransactionServices::sendShutdownEvent(*session.get());
421
boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
429
boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
422
430
select_thread_in_use= false; // For close_connections
424
432
COND_thread_count.notify_all();
426
434
/* Wait until cleanup is done */
427
session::Cache::singleton().shutdownSecond();
435
session::Cache::shutdownSecond();
430
438
module::Registry::shutdown();
431
439
internal::my_end();
441
errmsg_printf(error::INFO, "Drizzle is now shutting down");