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/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>
75 using namespace drizzled;
78 static pthread_t select_thread;
79 static uint32_t thr_kill_signal;
81 extern bool opt_daemon;
88 static void my_message_sql(drizzled::error_t error,
const char *str, myf MyFlags)
90 Session* session= current_session;
97 if (MyFlags & ME_FATALERROR)
114 if (! (session->lex().current_select &&
115 session->lex().current_select->no_error && !session->
is_fatal_error))
117 if (! session->main_da().is_error())
120 error= ER_UNKNOWN_ERROR;
136 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
141 if (not session || MyFlags & ME_NOREFRESH)
143 errmsg_printf(error::ERROR,
"%s: %s",internal::my_progname,str);
147 static void init_signals(
void)
152 if (not (getDebug().test(debug::NO_STACKTRACE) ||
153 getDebug().test(debug::CORE_ON_SIGNAL)))
155 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
156 sigemptyset(&sa.sa_mask);
157 sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
159 sa.sa_handler= drizzled_handle_segfault;
160 sigaction(SIGSEGV, &sa, NULL);
161 sigaction(SIGABRT, &sa, NULL);
163 sigaction(SIGBUS, &sa, NULL);
165 sigaction(SIGILL, &sa, NULL);
166 sigaction(SIGFPE, &sa, NULL);
169 if (getDebug().test(debug::CORE_ON_SIGNAL))
173 rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
174 if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
175 errmsg_printf(error::WARN,
176 _(
"setrlimit could not change the size of core files "
177 "to 'infinity'; We may not be able to generate a "
178 "core file on signals"));
180 (void) sigemptyset(&set);
181 ignore_signal(SIGPIPE);
182 sigaddset(&set,SIGPIPE);
183 #ifndef IGNORE_SIGHUP_SIGQUIT
184 sigaddset(&set,SIGQUIT);
185 sigaddset(&set,SIGHUP);
187 sigaddset(&set,SIGTERM);
190 sigemptyset(&sa.sa_mask);
192 sa.sa_handler = drizzled_print_signal_warning;
193 sigaction(SIGTERM, &sa, NULL);
195 sa.sa_handler = drizzled_print_signal_warning;
196 sigaction(SIGHUP, &sa, NULL);
198 sigaddset(&set,SIGTSTP);
200 if (getDebug().test(debug::ALLOW_SIGINT))
203 sa.sa_handler= drizzled_end_thread_signal;
204 sigaction(thr_kill_signal, &sa, NULL);
207 sigdelset(&set, thr_kill_signal);
211 sigaddset(&set,SIGINT);
213 sigprocmask(SIG_SETMASK,&set,NULL);
214 pthread_sigmask(SIG_SETMASK,&set,NULL);
218 static void GoogleProtoErrorThrower(google::protobuf::LogLevel level,
220 int,
const string& ) throw(const
char *)
224 case google::protobuf::LOGLEVEL_INFO:
226 case google::protobuf::LOGLEVEL_WARNING:
227 case google::protobuf::LOGLEVEL_ERROR:
228 case google::protobuf::LOGLEVEL_FATAL:
230 throw(
"error in google protocol buffer parsing");
234 int main(
int argc,
char **argv)
236 #if defined(ENABLE_NLS)
237 # if defined(HAVE_LOCALE_H)
238 setlocale(LC_ALL,
"");
240 bindtextdomain(
"drizzle", LOCALEDIR);
241 textdomain(
"drizzle");
246 drizzled::internal::my_progname= argv[0];
247 drizzled::internal::my_init();
252 thr_kill_signal= SIGINT;
254 google::protobuf::SetLogHandler(&GoogleProtoErrorThrower);
257 error_handler_hook= my_message_sql;
260 if (init_variables_before_daemonizing(argc, argv) ==
false)
262 unireg_abort <<
"init_variables_before_daemonizing() failed";
265 if (opt_daemon and was_help_requested() ==
false)
267 if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
269 perror(
"Failed to ignore SIGHUP");
274 unireg_abort <<
"--daemon failed";
278 if (init_variables_after_daemonizing(modules) ==
false)
280 unireg_abort <<
"init_variables_after_daemonizing() failed";
290 select_thread= pthread_self();
291 select_thread_in_use=1;
293 if (was_help_requested() ==
false)
295 if (chdir(getDataHome().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());
355 init_server_components(modules);
360 cout << _(
"Drizzle has receieved an abort event.") << endl;
361 cout << _(
"In Function: ") << *::boost::get_error_info<boost::throw_function>(ex) << endl;
362 cout << _(
"In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
363 cout << _(
"On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
365 unireg_abort <<
"init_server_components() failed";
384 unireg_abort <<
"Failed plugin::Listen::setup()";
387 assert(plugin::num_trx_monitored_objects > 0);
388 drizzle_rm_tmp_tables();
389 errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname, PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
394 setCurrentSession(session.get());
396 plugin_startup_window(modules, *session.get());
405 errmsg_printf(error::INFO,
"Drizzle startup complete, listening for connections will now begin.");
414 Session::shared_ptr session= Session::make_shared(client, client->catalog());
417 if (Session::schedule(session))
419 Session::unlink(session);
426 setCurrentSession(session.get());
431 boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
432 select_thread_in_use=
false;
434 COND_thread_count.notify_all();
437 session::Cache::shutdownSecond();
440 module::Registry::shutdown();
443 errmsg_printf(error::INFO,
"Drizzle is now shutting down");