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>
78 static pthread_t select_thread;
79 static uint32_t thr_kill_signal;
81 extern bool opt_daemon;
89 static void my_message_sql(drizzled::error_t error,
const char *str, myf MyFlags)
91 Session* session= current_session;
98 if (MyFlags & ME_FATALERROR)
115 if (! (session->lex().current_select &&
116 session->lex().current_select->no_error && !session->
is_fatal_error))
118 if (! session->main_da().is_error())
121 error= ER_UNKNOWN_ERROR;
137 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
142 if (not session || MyFlags & ME_NOREFRESH)
144 errmsg_printf(error::ERROR,
"%s: %s",internal::my_progname,str);
148 static void init_signals(
void)
153 if (not (getDebug().test(debug::NO_STACKTRACE) ||
154 getDebug().test(debug::CORE_ON_SIGNAL)))
156 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
157 sigemptyset(&sa.sa_mask);
158 sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
160 sa.sa_handler= drizzled_handle_segfault;
161 sigaction(SIGSEGV, &sa, NULL);
162 sigaction(SIGABRT, &sa, NULL);
164 sigaction(SIGBUS, &sa, NULL);
166 sigaction(SIGILL, &sa, NULL);
167 sigaction(SIGFPE, &sa, NULL);
170 if (getDebug().test(debug::CORE_ON_SIGNAL))
174 rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
175 if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
176 errmsg_printf(error::WARN,
177 _(
"setrlimit could not change the size of core files "
178 "to 'infinity'; We may not be able to generate a "
179 "core file on signals"));
181 (void) sigemptyset(&set);
182 ignore_signal(SIGPIPE);
183 sigaddset(&set,SIGPIPE);
184 #ifndef IGNORE_SIGHUP_SIGQUIT
185 sigaddset(&set,SIGQUIT);
186 sigaddset(&set,SIGHUP);
188 sigaddset(&set,SIGTERM);
191 sigemptyset(&sa.sa_mask);
193 sa.sa_handler = drizzled_print_signal_warning;
194 sigaction(SIGTERM, &sa, NULL);
196 sa.sa_handler = drizzled_print_signal_warning;
197 sigaction(SIGHUP, &sa, NULL);
199 sigaddset(&set,SIGTSTP);
201 if (getDebug().test(debug::ALLOW_SIGINT))
204 sa.sa_handler= drizzled_end_thread_signal;
205 sigaction(thr_kill_signal, &sa, NULL);
208 sigdelset(&set, thr_kill_signal);
212 sigaddset(&set,SIGINT);
214 sigprocmask(SIG_SETMASK,&set,NULL);
215 pthread_sigmask(SIG_SETMASK,&set,NULL);
217 (void) sigemptyset(&set);
218 sigaddset(&set,SIGTSTP);
219 sigaddset(&set,SIGINT);
220 sigprocmask(SIG_UNBLOCK,&set,NULL);
221 pthread_sigmask(SIG_UNBLOCK,&set,NULL);
223 sa.sa_handler = drizzled_sigint_handler;
224 sigaction(SIGINT,&sa,NULL);
231 static void GoogleProtoErrorThrower(google::protobuf::LogLevel level,
233 int,
const string& ) throw(const
char *)
237 case google::protobuf::LOGLEVEL_INFO:
239 case google::protobuf::LOGLEVEL_WARNING:
240 case google::protobuf::LOGLEVEL_ERROR:
241 case google::protobuf::LOGLEVEL_FATAL:
243 throw(
"error in google protocol buffer parsing");
247 int main(
int argc,
char **argv)
249 #if defined(ENABLE_NLS)
250 # if defined(HAVE_LOCALE_H)
251 setlocale(LC_ALL,
"");
253 bindtextdomain(
"drizzle", LOCALEDIR);
254 textdomain(
"drizzle");
259 drizzled::internal::my_progname= argv[0];
260 drizzled::internal::my_init();
265 thr_kill_signal= SIGINT;
267 google::protobuf::SetLogHandler(&GoogleProtoErrorThrower);
270 error_handler_hook= my_message_sql;
273 if (init_variables_before_daemonizing(argc, argv) ==
false)
275 unireg_abort <<
"init_variables_before_daemonizing() failed";
278 if (opt_daemon and was_help_requested() ==
false)
280 if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
282 perror(
"Failed to ignore SIGHUP");
287 unireg_abort <<
"--daemon failed";
291 if (init_variables_after_daemonizing(modules) ==
false)
293 unireg_abort <<
"init_variables_after_daemonizing() failed";
303 select_thread= pthread_self();
304 select_thread_in_use=1;
306 if (was_help_requested() ==
false)
308 if (chdir(getDataHome().file_string().c_str()))
310 unireg_abort <<
"Data directory " << getDataHome().file_string() <<
" does not exist";
313 ifstream old_uuid_file (
"server.uuid");
314 if (old_uuid_file.is_open())
316 getline(old_uuid_file, server_uuid);
317 old_uuid_file.close();
322 char uuid_string[37];
323 uuid_generate_random(uu);
324 uuid_unparse(uu, uuid_string);
325 ofstream new_uuid_file (
"server.uuid");
326 new_uuid_file << uuid_string;
327 new_uuid_file.close();
328 server_uuid= string(uuid_string);
331 if (mkdir(
"local", 0700) == -1)
341 unireg_abort <<
"Could not create local catalog, permission denied in directory:" << getcwd(cwd,
sizeof(cwd));
347 unireg_abort <<
"Could not create local catalog, in directory:" << getcwd(cwd,
sizeof(cwd)) <<
" system error was:" << strerror(errno);
352 setFullDataHome(boost::filesystem::system_complete(getDataHome()));
353 errmsg_printf(error::INFO,
"Data Home directory is : %s", getFullDataHome().native_file_string().c_str());
363 init_server_components(modules);
368 cout << _(
"Drizzle has receieved an abort event.") << endl;
369 cout << _(
"In Function: ") << *::boost::get_error_info<boost::throw_function>(ex) << endl;
370 cout << _(
"In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
371 cout << _(
"On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
373 unireg_abort <<
"init_server_components() failed";
392 unireg_abort <<
"Failed plugin::Listen::setup()";
395 assert(plugin::num_trx_monitored_objects > 0);
396 drizzle_rm_tmp_tables();
397 errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname, PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
402 setCurrentSession(session.get());
404 plugin_startup_window(modules, *session.get());
413 errmsg_printf(error::INFO,
"Drizzle startup complete, listening for connections will now begin.");
422 Session::shared_ptr session= Session::make_shared(
client,
client->catalog());
425 if (Session::schedule(session))
427 Session::unlink(session);
434 setCurrentSession(session.get());
439 boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
440 select_thread_in_use=
false;
442 COND_thread_count.notify_all();
445 session::Cache::shutdownSecond();
448 module::Registry::shutdown();
451 errmsg_printf(error::INFO,
"Drizzle is now shutting down");
static bool sendStartupEvent(Session &)
TODO: Rename this file - func.h is stupid.
static bool sendShutdownEvent(Session &)
static plugin::Client * getClient()
static plugin::Client * getNullClient()
bool no_warnings_for_error
void set_error_status(drizzled::error_t sql_errno_arg, const char *message_arg)
static bool evaluateRegisteredPlugins()