drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
main.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
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.
9 *
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.
14 *
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
18 */
19
20#include <config.h>
21
22#include <pthread.h>
23#include <signal.h>
24#include <sys/resource.h>
25#include <unistd.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/time.h>
29#include <time.h>
30
31# include <locale.h>
32
33#include <boost/filesystem.hpp>
34
35#include <drizzled/vc_version.h>
36#include <drizzled/abort_exception.h>
37#include <drizzled/catalog/local.h>
38#include <drizzled/configmake.h>
39#include <drizzled/data_home.h>
40#include <drizzled/debug.h>
41#include <drizzled/drizzled.h>
42#include <drizzled/errmsg_print.h>
43#include <drizzled/gettext.h>
44#include <drizzled/internal/my_sys.h>
45#include <drizzled/plugin.h>
46#include <drizzled/plugin/client.h>
47#include <drizzled/plugin/listen.h>
48#include <drizzled/plugin/monitored_in_transaction.h>
49#include <drizzled/pthread_globals.h>
50#include <drizzled/replication_services.h>
51#include <drizzled/session.h>
52#include <drizzled/session/cache.h>
53#include <drizzled/signal_handler.h>
54#include <drizzled/transaction_services.h>
55#include <drizzled/drizzled_abort.h>
56#include <drizzled/util/backtrace.h>
57#include <drizzled/current_session.h>
58#include <drizzled/diagnostics_area.h>
59#include <drizzled/sql_base.h>
60#include <drizzled/sql_lex.h>
61#include <drizzled/system_variables.h>
62
63using namespace drizzled;
64using namespace std;
65
66static pthread_t select_thread;
67static uint32_t thr_kill_signal;
68
73static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
74{
75 Session* session= current_session;
76 /*
77 Put here following assertion when situation with EE_* error codes
78 will be fixed
79 */
80 if (session)
81 {
82 if (MyFlags & ME_FATALERROR)
83 {
84 session->is_fatal_error= 1;
85 }
86
87 /*
88 @TODO There are two exceptions mechanism (Session and sp_rcontext),
89 this could be improved by having a common stack of handlers.
90
91 if (session->handle_error(error, str, DRIZZLE_ERROR::WARN_LEVEL_ERROR))
92 return;
93 */
94
95 /*
96 session->lex().current_select == 0 if lex structure is not inited
97 (not query command (COM_QUERY))
98 */
99 if (! (session->lex().current_select &&
100 session->lex().current_select->no_error && !session->is_fatal_error))
101 {
102 if (! session->main_da().is_error()) // Return only first message
103 {
104 if (error == EE_OK)
105 error= ER_UNKNOWN_ERROR;
106
107 if (str == NULL)
108 str= ER(error);
109
110 session->main_da().set_error_status(error, str);
111 }
112 }
113
114 if (!session->no_warnings_for_error && !session->is_fatal_error)
115 {
116 /*
117 Suppress infinite recursion if there a memory allocation error
118 inside push_warning.
119 */
120 session->no_warnings_for_error= true;
121 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
122 session->no_warnings_for_error= false;
123 }
124 }
125
126 if (not session || MyFlags & ME_NOREFRESH)
127 {
128 errmsg_printf(error::ERROR, "%s: %s",internal::my_progname,str);
129 }
130}
131
132static void init_signals(void)
133{
134 sigset_t set;
135 struct sigaction sa;
136
137 if (not (getDebug().test(debug::NO_STACKTRACE) ||
138 getDebug().test(debug::CORE_ON_SIGNAL)))
139 {
140 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
141 sigemptyset(&sa.sa_mask);
142 sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
143
144 sa.sa_handler= drizzled_handle_segfault;
145 sigaction(SIGSEGV, &sa, NULL);
146 sigaction(SIGABRT, &sa, NULL);
147#ifdef SIGBUS
148 sigaction(SIGBUS, &sa, NULL);
149#endif
150 sigaction(SIGILL, &sa, NULL);
151 sigaction(SIGFPE, &sa, NULL);
152 }
153
154 if (getDebug().test(debug::CORE_ON_SIGNAL))
155 {
156 /* Change limits so that we will get a core file */
157 struct rlimit rl;
158 rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
159 if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
160 errmsg_printf(error::WARN,
161 _("setrlimit could not change the size of core files "
162 "to 'infinity'; We may not be able to generate a "
163 "core file on signals"));
164 }
165 (void) sigemptyset(&set);
166 ignore_signal(SIGPIPE);
167 sigaddset(&set,SIGPIPE);
168#ifndef IGNORE_SIGHUP_SIGQUIT
169 sigaddset(&set,SIGQUIT);
170 sigaddset(&set,SIGHUP);
171#endif
172 sigaddset(&set,SIGTERM);
173 sigaddset(&set,SIGINT);
174
175 /* Fix signals if blocked by parents (can happen on Mac OS X) */
176 sigemptyset(&sa.sa_mask);
177 sa.sa_flags = 0;
178 sa.sa_handler = drizzled_print_signal_warning;
179 sigaction(SIGTERM, &sa, NULL);
180 sa.sa_flags = 0;
181 sa.sa_handler = drizzled_print_signal_warning;
182 sigaction(SIGHUP, &sa, NULL);
183#ifdef SIGTSTP
184 sigaddset(&set,SIGTSTP);
185#endif
186 if (getDebug().test(debug::ALLOW_SIGINT) && thr_kill_signal != SIGINT)
187 {
188 sa.sa_flags= 0;
189 sa.sa_handler= drizzled_end_thread_signal;
190 sigaction(thr_kill_signal, &sa, NULL);
191
192 sigdelset(&set, thr_kill_signal);
193 }
194 sigprocmask(SIG_SETMASK,&set,NULL);
195 pthread_sigmask(SIG_SETMASK,&set,NULL);
196
197 (void) sigemptyset(&set);
198 sigaddset(&set,SIGTSTP);
199 sigprocmask(SIG_UNBLOCK,&set,NULL);
200 pthread_sigmask(SIG_UNBLOCK,&set,NULL);
201
202 return;
203}
204
205
206
207static void GoogleProtoErrorThrower(google::protobuf::LogLevel level,
208 const char* ,
209 int, const string& )
210{
211 switch(level)
212 {
213 case google::protobuf::LOGLEVEL_INFO:
214 break;
215 case google::protobuf::LOGLEVEL_WARNING:
216 case google::protobuf::LOGLEVEL_ERROR:
217 case google::protobuf::LOGLEVEL_FATAL:
218 default:
219 throw("error in google protocol buffer parsing");
220 }
221}
222
223int main(int argc, char **argv)
224{
225#if defined(ENABLE_NLS)
226 setlocale(LC_ALL, "");
227 bindtextdomain("drizzle", LOCALEDIR);
228 textdomain("drizzle");
229#endif
230
231 module::Registry &modules= module::Registry::singleton();
232
233 drizzled::internal::my_progname= argv[0];
234 drizzled::internal::my_init();
235
236 /* nothing should come before this line ^^^ */
237
238 /* Set signal used to kill Drizzle */
239 thr_kill_signal= SIGINT;
240
241 google::protobuf::SetLogHandler(&GoogleProtoErrorThrower);
242
243 /* Function generates error messages before abort */
244 error_handler_hook= my_message_sql;
245
246 /* init_common_variables must get basic settings such as data_home_dir and plugin_load_list. */
247 if (init_variables_before_daemonizing(argc, argv) == false)
248 {
249 drizzled_abort << "init_variables_before_daemonizing() failed"; // Will do exit
250 }
251
252 if (init_variables_after_daemonizing(modules) == false)
253 {
254 drizzled_abort << "init_variables_after_daemonizing() failed"; // Will do exit
255 }
256
257 /*
258 init signals & alarm
259 After this we can't quit by a simple drizzled_abort
260 */
261
262 init_signals();
263
264 select_thread= pthread_self();
265 select_thread_in_use=1;
266
267 if (was_help_requested() == false)
268 {
269 if (chdir(getDataHome().string().c_str()))
270 {
271 drizzled_abort << "Data directory " << getDataHome().string() << " does not exist";
272 }
273
274 ifstream old_uuid_file ("server.uuid");
275 if (old_uuid_file.is_open())
276 {
277 getline(old_uuid_file, server_uuid);
278 old_uuid_file.close();
279 }
280 else
281 {
282 uuid_t uu;
283 char uuid_string[37];
284 uuid_generate_random(uu);
285 uuid_unparse(uu, uuid_string);
286 ofstream new_uuid_file ("server.uuid");
287 new_uuid_file << uuid_string;
288 new_uuid_file.close();
289 server_uuid= string(uuid_string);
290 }
291
292 if (mkdir("local", 0700) == -1)
293 {
294 switch (errno)
295 {
296 case EEXIST:
297 break;
298
299 case EACCES:
300 {
301 char cwd[1024];
302 drizzled_abort << "Could not create local catalog, permission denied in directory:" << getcwd(cwd, sizeof(cwd));
303 }
304 /* fallthrough */
305
306 default:
307 {
308 char cwd[1024];
309 drizzled_abort << "Could not create local catalog, in directory:" << getcwd(cwd, sizeof(cwd)) << " system error was:" << strerror(errno);
310 }
311 }
312 }
313
314 setFullDataHome(boost::filesystem::system_complete(getDataHome()));
315 errmsg_printf(error::INFO, "Data Home directory is : %s", getFullDataHome().native().c_str());
316 }
317
318 if (server_id == 0)
319 {
320 server_id= 1;
321 }
322
323 try
324 {
325 init_server_components(modules);
326 }
327 catch (abort_exception& ex)
328 {
329#if defined(DEBUG)
330 cout << _("Drizzle has receieved an abort event.") << endl;
331 cout << _("In Function: ") << *::boost::get_error_info<boost::throw_function>(ex) << endl;
332 cout << _("In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
333 cout << _("On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
334#endif
335 drizzled_abort << "init_server_components() failed";
336 }
337
338
351
353 {
354 drizzled_abort << "Failed plugin::Listen::setup()";
355 }
356
357 assert(plugin::num_trx_monitored_objects > 0);
358 drizzle_rm_tmp_tables();
359 errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname, DRIZZLE_RELEASE_VERSION, COMPILATION_COMMENT);
360
361 /* Send server startup event */
362 {
363 Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
364 setCurrentSession(session.get());
366 plugin_startup_window(modules, *session.get());
367 }
368
369 errmsg_printf(error::INFO, "Drizzle startup complete, listening for connections will now begin.");
370
371 /*
372 Listen for new connections and start new session for each connection
373 accepted. The Listen::serveConnections() method will return NULL when the server
374 should be shutdown.
375 */
376 while (plugin::Client* client= plugin::Listen::serveConnections())
377 {
378 Session::shared_ptr session= Session::make_shared(client, client->catalog());
379
380 /* If we error on creation we drop the connection and delete the session. */
381 if (Session::schedule(session))
382 {
383 Session::unlink(session);
384 }
385 }
386
387 /* Send server shutdown event */
388 {
389 Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
390 setCurrentSession(session.get());
392 }
393
394 {
395 boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
396 select_thread_in_use= false; // For close_connections
397 }
398 COND_thread_count.notify_all();
399
400 /* Wait until cleanup is done */
401 session::Cache::shutdownSecond();
402
403 clean_up(1);
404 internal::my_end();
405
406 errmsg_printf(error::INFO, "Drizzle is now shutting down");
407
408 // module::Registry is a static singleton guaranteed to be destructed here.
409 return 0;
410}
411
static bool sendShutdownEvent(Session &)
static bool sendStartupEvent(Session &)
static bool setup(void)
Definition listen.cc:75
static plugin::Client * serveConnections()
Definition listen.cc:136
static plugin::Client * getNullClient()
Definition listen.cc:184
TODO: Rename this file - func.h is stupid.
STL namespace.