~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/main.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

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/session/cache.h"
52
 
#include "drizzled/internal/my_sys.h"
53
 
#include "drizzled/unireg.h"
54
 
#include "drizzled/drizzled.h"
55
 
#include "drizzled/errmsg_print.h"
56
 
#include "drizzled/data_home.h"
57
 
#include "drizzled/plugin/listen.h"
58
 
#include "drizzled/plugin/client.h"
59
 
#include "drizzled/pthread_globals.h"
60
 
#include "drizzled/tztime.h"
61
 
#include "drizzled/signal_handler.h"
62
 
#include "drizzled/replication_services.h"
63
 
#include "drizzled/transaction_services.h"
64
 
 
65
 
#include "drizzled/util/backtrace.h"
66
 
 
67
 
using namespace drizzled;
68
 
using namespace std;
69
 
namespace fs=boost::filesystem;
70
 
 
71
 
static pthread_t select_thread;
72
 
static uint32_t thr_kill_signal;
73
 
 
74
 
 
75
 
/**
76
 
  All global error messages are sent here where the first one is stored
77
 
  for the client.
78
 
*/
79
 
static void my_message_sql(uint32_t error, const char *str, myf MyFlags)
80
 
{
81
 
  Session *session;
82
 
  /*
83
 
    Put here following assertion when situation with EE_* error codes
84
 
    will be fixed
85
 
  */
86
 
  if ((session= current_session))
87
 
  {
88
 
    if (MyFlags & ME_FATALERROR)
89
 
      session->is_fatal_error= 1;
90
 
 
91
 
    /*
92
 
      TODO: There are two exceptions mechanism (Session and sp_rcontext),
93
 
      this could be improved by having a common stack of handlers.
94
 
    */
95
 
    if (session->handle_error(error, str,
96
 
                          DRIZZLE_ERROR::WARN_LEVEL_ERROR))
97
 
      return;;
98
 
 
99
 
    /*
100
 
      session->lex->current_select == 0 if lex structure is not inited
101
 
      (not query command (COM_QUERY))
102
 
    */
103
 
    if (! (session->lex->current_select &&
104
 
        session->lex->current_select->no_error && !session->is_fatal_error))
105
 
    {
106
 
      if (! session->main_da.is_error())            // Return only first message
107
 
      {
108
 
        if (error == 0)
109
 
          error= ER_UNKNOWN_ERROR;
110
 
        if (str == NULL)
111
 
          str= ER(error);
112
 
        session->main_da.set_error_status(error, str);
113
 
      }
114
 
    }
115
 
 
116
 
    if (!session->no_warnings_for_error && !session->is_fatal_error)
117
 
    {
118
 
      /*
119
 
        Suppress infinite recursion if there a memory allocation error
120
 
        inside push_warning.
121
 
      */
122
 
      session->no_warnings_for_error= true;
123
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
124
 
      session->no_warnings_for_error= false;
125
 
      }
126
 
    }
127
 
    if (!session || MyFlags & ME_NOREFRESH)
128
 
        errmsg_printf(ERRMSG_LVL_ERROR, "%s: %s",internal::my_progname,str);
129
 
}
130
 
 
131
 
static void init_signals(void)
132
 
{
133
 
  sigset_t set;
134
 
  struct sigaction sa;
135
 
 
136
 
  if (!(test_flags.test(TEST_NO_STACKTRACE) || 
137
 
        test_flags.test(TEST_CORE_ON_SIGNAL)))
138
 
  {
139
 
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
140
 
    sigemptyset(&sa.sa_mask);
141
 
    sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
142
 
 
143
 
    sa.sa_handler= drizzled_handle_segfault;
144
 
    sigaction(SIGSEGV, &sa, NULL);
145
 
    sigaction(SIGABRT, &sa, NULL);
146
 
#ifdef SIGBUS
147
 
    sigaction(SIGBUS, &sa, NULL);
148
 
#endif
149
 
    sigaction(SIGILL, &sa, NULL);
150
 
    sigaction(SIGFPE, &sa, NULL);
151
 
  }
152
 
 
153
 
  if (test_flags.test(TEST_CORE_ON_SIGNAL))
154
 
  {
155
 
    /* Change limits so that we will get a core file */
156
 
    struct rlimit rl;
157
 
    rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
158
 
    if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
159
 
        errmsg_printf(ERRMSG_LVL_WARN,
160
 
                      _("setrlimit could not change the size of core files "
161
 
                        "to 'infinity';  We may not be able to generate a "
162
 
                        "core file on signals"));
163
 
  }
164
 
  (void) sigemptyset(&set);
165
 
  ignore_signal(SIGPIPE);
166
 
  sigaddset(&set,SIGPIPE);
167
 
#ifndef IGNORE_SIGHUP_SIGQUIT
168
 
  sigaddset(&set,SIGQUIT);
169
 
  sigaddset(&set,SIGHUP);
170
 
#endif
171
 
  sigaddset(&set,SIGTERM);
172
 
 
173
 
  /* Fix signals if blocked by parents (can happen on Mac OS X) */
174
 
  sigemptyset(&sa.sa_mask);
175
 
  sa.sa_flags = 0;
176
 
  sa.sa_handler = drizzled_print_signal_warning;
177
 
  sigaction(SIGTERM, &sa, NULL);
178
 
  sa.sa_flags = 0;
179
 
  sa.sa_handler = drizzled_print_signal_warning;
180
 
  sigaction(SIGHUP, &sa, NULL);
181
 
#ifdef SIGTSTP
182
 
  sigaddset(&set,SIGTSTP);
183
 
#endif
184
 
  if (test_flags.test(TEST_SIGINT))
185
 
  {
186
 
    sa.sa_flags= 0;
187
 
    sa.sa_handler= drizzled_end_thread_signal;
188
 
    sigaction(thr_kill_signal, &sa, NULL);
189
 
 
190
 
    // May be SIGINT
191
 
    sigdelset(&set, thr_kill_signal);
192
 
  }
193
 
  else
194
 
  {
195
 
    sigaddset(&set,SIGINT);
196
 
  }
197
 
  sigprocmask(SIG_SETMASK,&set,NULL);
198
 
  pthread_sigmask(SIG_SETMASK,&set,NULL);
199
 
  return;
200
 
}
201
 
 
202
 
static void GoogleProtoErrorThrower(google::protobuf::LogLevel level, const char* filename,
203
 
                       int line, const string& message) throw(const char *)
204
 
{
205
 
  (void)filename;
206
 
  (void)line;
207
 
  (void)message;
208
 
  std::cerr << "\n";
209
 
  drizzled::util::custom_backtrace();
210
 
  std::cerr << "\n";
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
 
    std::cerr << "GoogleProtoErrorThrower(" << filename << ", " << line << ", " << message << ")";
220
 
    throw("error in google protocol buffer parsing");
221
 
  }
222
 
}
223
 
 
224
 
int main(int argc, char **argv)
225
 
{
226
 
#if defined(ENABLE_NLS)
227
 
# if defined(HAVE_LOCALE_H)
228
 
  setlocale(LC_ALL, "");
229
 
# endif
230
 
  bindtextdomain("drizzle", LOCALEDIR);
231
 
  textdomain("drizzle");
232
 
#endif
233
 
 
234
 
  module::Registry &modules= module::Registry::singleton();
235
 
  plugin::Client *client;
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
 
  {
337
 
    Session *session;
338
 
 
339
 
    if ((session= new Session(plugin::Listen::getNullClient())))
340
 
    {
341
 
      currentSession().release();
342
 
      currentSession().reset(session);
343
 
      transaction_services.sendStartupEvent(session);
344
 
      delete session;
345
 
    }
346
 
  }
347
 
 
348
 
 
349
 
  /* Listen for new connections and start new session for each connection
350
 
     accepted. The listen.getClient() method will return NULL when the server
351
 
     should be shutdown. */
352
 
  while ((client= plugin::Listen::getClient()) != NULL)
353
 
  {
354
 
    Session::shared_ptr session(new Session(client));
355
 
 
356
 
    if (not session)
357
 
    {
358
 
      delete client;
359
 
      continue;
360
 
    }
361
 
 
362
 
    /* If we error on creation we drop the connection and delete the session. */
363
 
    if (Session::schedule(session))
364
 
      Session::unlink(session);
365
 
  }
366
 
 
367
 
  /* Send server shutdown event */
368
 
  {
369
 
    Session *session;
370
 
 
371
 
    if ((session= new Session(plugin::Listen::getNullClient())))
372
 
    {
373
 
      currentSession().release();
374
 
      currentSession().reset(session);
375
 
      transaction_services.sendShutdownEvent(session);
376
 
      delete session;
377
 
    }
378
 
  }
379
 
 
380
 
  {
381
 
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
382
 
    select_thread_in_use= false;                        // For close_connections
383
 
  }
384
 
  COND_thread_count.notify_all();
385
 
 
386
 
  /* Wait until cleanup is done */
387
 
  {
388
 
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
389
 
    while (not ready_to_exit)
390
 
      COND_server_end.wait(scopedLock);
391
 
  }
392
 
 
393
 
  clean_up(1);
394
 
  module::Registry::shutdown();
395
 
  internal::my_end();
396
 
 
397
 
  return 0;
398
 
}
399