~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/main.cc

  • Committer: Brian Aker
  • Date: 2010-11-08 18:24:58 UTC
  • mto: (1921.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 1916.
  • Revision ID: brian@tangent.org-20101108182458-twv4hyix43ojno80
Merge in changes such that lock is now broken out into its own directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
 
 
29
 
 
30
#if TIME_WITH_SYS_TIME
 
31
# include <sys/time.h>
 
32
# include <time.h>
 
33
#else
 
34
# if HAVE_SYS_TIME_H
 
35
#  include <sys/time.h>
 
36
# else
 
37
#  include <time.h>
 
38
# endif
 
39
#endif
 
40
 
 
41
#if defined(HAVE_LOCALE_H)
 
42
# include <locale.h>
 
43
#endif
 
44
 
 
45
#include <boost/filesystem.hpp>
 
46
 
 
47
#include "drizzled/plugin.h"
 
48
#include "drizzled/gettext.h"
 
49
#include "drizzled/configmake.h"
 
50
#include "drizzled/session.h"
 
51
#include "drizzled/internal/my_sys.h"
 
52
#include "drizzled/unireg.h"
 
53
#include "drizzled/drizzled.h"
 
54
#include "drizzled/errmsg_print.h"
 
55
#include "drizzled/data_home.h"
 
56
#include "drizzled/plugin/listen.h"
 
57
#include "drizzled/plugin/client.h"
 
58
#include "drizzled/pthread_globals.h"
 
59
#include "drizzled/tztime.h"
 
60
#include "drizzled/signal_handler.h"
 
61
#include "drizzled/replication_services.h"
 
62
#include "drizzled/transaction_services.h"
 
63
 
 
64
#include "drizzled/util/backtrace.h"
 
65
 
 
66
using namespace drizzled;
 
67
using namespace std;
 
68
namespace fs=boost::filesystem;
 
69
 
 
70
static pthread_t select_thread;
 
71
static uint32_t thr_kill_signal;
 
72
 
 
73
 
 
74
/**
 
75
  All global error messages are sent here where the first one is stored
 
76
  for the client.
 
77
*/
 
78
static void my_message_sql(uint32_t error, const char *str, myf MyFlags)
 
79
{
 
80
  Session *session;
 
81
  /*
 
82
    Put here following assertion when situation with EE_* error codes
 
83
    will be fixed
 
84
  */
 
85
  if ((session= current_session))
 
86
  {
 
87
    if (MyFlags & ME_FATALERROR)
 
88
      session->is_fatal_error= 1;
 
89
 
 
90
    /*
 
91
      TODO: There are two exceptions mechanism (Session and sp_rcontext),
 
92
      this could be improved by having a common stack of handlers.
 
93
    */
 
94
    if (session->handle_error(error, str,
 
95
                          DRIZZLE_ERROR::WARN_LEVEL_ERROR))
 
96
      return;;
 
97
 
 
98
    /*
 
99
      session->lex->current_select == 0 if lex structure is not inited
 
100
      (not query command (COM_QUERY))
 
101
    */
 
102
    if (! (session->lex->current_select &&
 
103
        session->lex->current_select->no_error && !session->is_fatal_error))
 
104
    {
 
105
      if (! session->main_da.is_error())            // Return only first message
 
106
      {
 
107
        if (error == 0)
 
108
          error= ER_UNKNOWN_ERROR;
 
109
        if (str == NULL)
 
110
          str= ER(error);
 
111
        session->main_da.set_error_status(error, str);
 
112
      }
 
113
    }
 
114
 
 
115
    if (!session->no_warnings_for_error && !session->is_fatal_error)
 
116
    {
 
117
      /*
 
118
        Suppress infinite recursion if there a memory allocation error
 
119
        inside push_warning.
 
120
      */
 
121
      session->no_warnings_for_error= true;
 
122
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
 
123
      session->no_warnings_for_error= false;
 
124
      }
 
125
    }
 
126
    if (!session || MyFlags & ME_NOREFRESH)
 
127
        errmsg_printf(ERRMSG_LVL_ERROR, "%s: %s",internal::my_progname,str);
 
128
}
 
129
 
 
130
static void init_signals(void)
 
131
{
 
132
  sigset_t set;
 
133
  struct sigaction sa;
 
134
 
 
135
  if (!(test_flags.test(TEST_NO_STACKTRACE) || 
 
136
        test_flags.test(TEST_CORE_ON_SIGNAL)))
 
137
  {
 
138
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
 
139
    sigemptyset(&sa.sa_mask);
 
140
    sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
 
141
 
 
142
    sa.sa_handler= drizzled_handle_segfault;
 
143
    sigaction(SIGSEGV, &sa, NULL);
 
144
    sigaction(SIGABRT, &sa, NULL);
 
145
#ifdef SIGBUS
 
146
    sigaction(SIGBUS, &sa, NULL);
 
147
#endif
 
148
    sigaction(SIGILL, &sa, NULL);
 
149
    sigaction(SIGFPE, &sa, NULL);
 
150
  }
 
151
 
 
152
  if (test_flags.test(TEST_CORE_ON_SIGNAL))
 
153
  {
 
154
    /* Change limits so that we will get a core file */
 
155
    struct rlimit rl;
 
156
    rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
 
157
    if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
 
158
        errmsg_printf(ERRMSG_LVL_WARN,
 
159
                      _("setrlimit could not change the size of core files "
 
160
                        "to 'infinity';  We may not be able to generate a "
 
161
                        "core file on signals"));
 
162
  }
 
163
  (void) sigemptyset(&set);
 
164
  ignore_signal(SIGPIPE);
 
165
  sigaddset(&set,SIGPIPE);
 
166
#ifndef IGNORE_SIGHUP_SIGQUIT
 
167
  sigaddset(&set,SIGQUIT);
 
168
  sigaddset(&set,SIGHUP);
 
169
#endif
 
170
  sigaddset(&set,SIGTERM);
 
171
 
 
172
  /* Fix signals if blocked by parents (can happen on Mac OS X) */
 
173
  sigemptyset(&sa.sa_mask);
 
174
  sa.sa_flags = 0;
 
175
  sa.sa_handler = drizzled_print_signal_warning;
 
176
  sigaction(SIGTERM, &sa, NULL);
 
177
  sa.sa_flags = 0;
 
178
  sa.sa_handler = drizzled_print_signal_warning;
 
179
  sigaction(SIGHUP, &sa, NULL);
 
180
#ifdef SIGTSTP
 
181
  sigaddset(&set,SIGTSTP);
 
182
#endif
 
183
  if (test_flags.test(TEST_SIGINT))
 
184
  {
 
185
    sa.sa_flags= 0;
 
186
    sa.sa_handler= drizzled_end_thread_signal;
 
187
    sigaction(thr_kill_signal, &sa, NULL);
 
188
 
 
189
    // May be SIGINT
 
190
    sigdelset(&set, thr_kill_signal);
 
191
  }
 
192
  else
 
193
  {
 
194
    sigaddset(&set,SIGINT);
 
195
  }
 
196
  sigprocmask(SIG_SETMASK,&set,NULL);
 
197
  pthread_sigmask(SIG_SETMASK,&set,NULL);
 
198
  return;
 
199
}
 
200
 
 
201
static void GoogleProtoErrorThrower(google::protobuf::LogLevel level, const char* filename,
 
202
                       int line, const string& message) throw(const char *)
 
203
{
 
204
  (void)filename;
 
205
  (void)line;
 
206
  (void)message;
 
207
  std::cerr << "\n";
 
208
  drizzled::util::custom_backtrace();
 
209
  std::cerr << "\n";
 
210
  switch(level)
 
211
  {
 
212
  case google::protobuf::LOGLEVEL_INFO:
 
213
    break;
 
214
  case google::protobuf::LOGLEVEL_WARNING:
 
215
  case google::protobuf::LOGLEVEL_ERROR:
 
216
  case google::protobuf::LOGLEVEL_FATAL:
 
217
  default:
 
218
    std::cerr << "GoogleProtoErrorThrower(" << filename << ", " << line << ", " << message << ")";
 
219
    throw("error in google protocol buffer parsing");
 
220
  }
 
221
}
 
222
 
 
223
int main(int argc, char **argv)
 
224
{
 
225
#if defined(ENABLE_NLS)
 
226
# if defined(HAVE_LOCALE_H)
 
227
  setlocale(LC_ALL, "");
 
228
# endif
 
229
  bindtextdomain("drizzle", LOCALEDIR);
 
230
  textdomain("drizzle");
 
231
#endif
 
232
 
 
233
  module::Registry &modules= module::Registry::singleton();
 
234
  plugin::Client *client;
 
235
  Session *session;
 
236
 
 
237
  MY_INIT(argv[0]);             // init my_sys library & pthreads
 
238
  /* nothing should come before this line ^^^ */
 
239
 
 
240
  /* Set signal used to kill Drizzle */
 
241
  thr_kill_signal= SIGINT;
 
242
 
 
243
  google::protobuf::SetLogHandler(&GoogleProtoErrorThrower);
 
244
 
 
245
  /* Function generates error messages before abort */
 
246
  error_handler_hook= my_message_sql;
 
247
  /* init_common_variables must get basic settings such as data_home_dir
 
248
     and plugin_load_list. */
 
249
  if (init_common_variables(argc, argv, modules))
 
250
    unireg_abort(1);                            // Will do exit
 
251
 
 
252
  /*
 
253
    init signals & alarm
 
254
    After this we can't quit by a simple unireg_abort
 
255
  */
 
256
  init_signals();
 
257
 
 
258
 
 
259
  select_thread=pthread_self();
 
260
  select_thread_in_use=1;
 
261
 
 
262
  if (not opt_help)
 
263
  {
 
264
    if (chdir(getDataHome().file_string().c_str()))
 
265
    {
 
266
      errmsg_printf(ERRMSG_LVL_ERROR,
 
267
                    _("Data directory %s does not exist\n"),
 
268
                    getDataHome().file_string().c_str());
 
269
      unireg_abort(1);
 
270
    }
 
271
    if (mkdir("local", 0700))
 
272
    {
 
273
      /* We don't actually care */
 
274
    }
 
275
    if (chdir("local"))
 
276
    {
 
277
      errmsg_printf(ERRMSG_LVL_ERROR,
 
278
                    _("Local catalog %s/local does not exist\n"),
 
279
                    getDataHome().file_string().c_str());
 
280
      unireg_abort(1);
 
281
    }
 
282
 
 
283
    full_data_home= fs::system_complete(getDataHome());
 
284
    getDataHomeCatalog()= "./";
 
285
    getDataHome()= "../";
 
286
  }
 
287
 
 
288
 
 
289
 
 
290
  if (server_id == 0)
 
291
  {
 
292
    server_id= 1;
 
293
  }
 
294
 
 
295
  if (init_server_components(modules))
 
296
    unireg_abort(1);
 
297
 
 
298
  /**
 
299
   * This check must be done after init_server_components for now
 
300
   * because we don't yet have plugin dependency tracking...
 
301
   *
 
302
   * ReplicationServices::evaluateRegisteredPlugins() will print error messages to stderr
 
303
   * via errmsg_printf().
 
304
   *
 
305
   * @todo
 
306
   *
 
307
   * not checking return since unireg_abort() hangs
 
308
   */
 
309
  ReplicationServices &replication_services= ReplicationServices::singleton();
 
310
    (void) replication_services.evaluateRegisteredPlugins();
 
311
 
 
312
  if (plugin::Listen::setup())
 
313
    unireg_abort(1);
 
314
 
 
315
 
 
316
  assert(plugin::num_trx_monitored_objects > 0);
 
317
  if (drizzle_rm_tmp_tables() ||
 
318
      my_tz_init((Session *)0, default_tz_name))
 
319
  {
 
320
    abort_loop= true;
 
321
    select_thread_in_use=0;
 
322
    (void) pthread_kill(signal_thread, SIGTERM);
 
323
 
 
324
    (void) unlink(pid_file.file_string().c_str());      // Not needed anymore
 
325
 
 
326
    exit(1);
 
327
  }
 
328
 
 
329
  errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_STARTUP)), internal::my_progname,
 
330
                PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
 
331
 
 
332
 
 
333
  TransactionServices &transaction_services= TransactionServices::singleton();
 
334
 
 
335
  /* Send server startup event */
 
336
  if ((session= new Session(plugin::Listen::getNullClient())))
 
337
  {
 
338
    currentSession().release();
 
339
    currentSession().reset(session);
 
340
    transaction_services.sendStartupEvent(session);
 
341
    session->lockForDelete();
 
342
    delete session;
 
343
  }
 
344
 
 
345
 
 
346
  /* Listen for new connections and start new session for each connection
 
347
     accepted. The listen.getClient() method will return NULL when the server
 
348
     should be shutdown. */
 
349
  while ((client= plugin::Listen::getClient()) != NULL)
 
350
  {
 
351
    if (!(session= new Session(client)))
 
352
    {
 
353
      delete client;
 
354
      continue;
 
355
    }
 
356
 
 
357
    /* If we error on creation we drop the connection and delete the session. */
 
358
    if (session->schedule())
 
359
      Session::unlink(session);
 
360
  }
 
361
 
 
362
  /* Send server shutdown event */
 
363
  if ((session= new Session(plugin::Listen::getNullClient())))
 
364
  {
 
365
    currentSession().release();
 
366
    currentSession().reset(session);
 
367
    transaction_services.sendShutdownEvent(session);
 
368
    session->lockForDelete();
 
369
    delete session;
 
370
  }
 
371
 
 
372
  LOCK_thread_count.lock();
 
373
  select_thread_in_use=0;                       // For close_connections
 
374
  LOCK_thread_count.unlock();
 
375
  COND_thread_count.notify_all();
 
376
 
 
377
  /* Wait until cleanup is done */
 
378
  {
 
379
    boost::mutex::scoped_lock scopedLock(LOCK_thread_count);
 
380
    while (!ready_to_exit)
 
381
      COND_server_end.wait(scopedLock);
 
382
  }
 
383
 
 
384
  clean_up(1);
 
385
  module::Registry::shutdown();
 
386
  internal::my_end();
 
387
 
 
388
  return 0;
 
389
}
 
390