~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/main.cc

  • Committer: Brian Aker
  • Date: 2010-05-15 01:19:45 UTC
  • Revision ID: brian@gaz-20100515011945-uxhf94vi0tzm0vq6
Rename of KEY to KeyInfo

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
 
 
27
#if TIME_WITH_SYS_TIME
 
28
# include <sys/time.h>
 
29
# include <time.h>
 
30
#else
 
31
# if HAVE_SYS_TIME_H
 
32
#  include <sys/time.h>
 
33
# else
 
34
#  include <time.h>
 
35
# endif
 
36
#endif
 
37
 
 
38
#if defined(HAVE_LOCALE_H)
 
39
# include <locale.h>
 
40
#endif
 
41
 
 
42
 
 
43
#include "drizzled/plugin.h"
 
44
#include "drizzled/gettext.h"
 
45
#include "drizzled/configmake.h"
 
46
#include "drizzled/session.h"
 
47
#include "drizzled/internal/my_sys.h"
 
48
#include "drizzled/unireg.h"
 
49
#include "drizzled/stacktrace.h"
 
50
#include "drizzled/drizzled.h"
 
51
#include "drizzled/errmsg_print.h"
 
52
#include "drizzled/data_home.h"
 
53
#include "drizzled/plugin/listen.h"
 
54
#include "drizzled/plugin/client.h"
 
55
#include "drizzled/pthread_globals.h"
 
56
#include "drizzled/tztime.h"
 
57
#include "drizzled/signal_handler.h"
 
58
#include "drizzled/replication_services.h"
 
59
 
 
60
using namespace drizzled;
 
61
using namespace std;
 
62
 
 
63
static pthread_t select_thread;
 
64
static uint32_t thr_kill_signal;
 
65
 
 
66
/**
 
67
  All global error messages are sent here where the first one is stored
 
68
  for the client.
 
69
*/
 
70
static void my_message_sql(uint32_t error, const char *str, myf MyFlags)
 
71
{
 
72
  Session *session;
 
73
  /*
 
74
    Put here following assertion when situation with EE_* error codes
 
75
    will be fixed
 
76
  */
 
77
  if ((session= current_session))
 
78
  {
 
79
    if (MyFlags & ME_FATALERROR)
 
80
      session->is_fatal_error= 1;
 
81
 
 
82
    /*
 
83
      TODO: There are two exceptions mechanism (Session and sp_rcontext),
 
84
      this could be improved by having a common stack of handlers.
 
85
    */
 
86
    if (session->handle_error(error, str,
 
87
                          DRIZZLE_ERROR::WARN_LEVEL_ERROR))
 
88
      return;;
 
89
 
 
90
    /*
 
91
      session->lex->current_select == 0 if lex structure is not inited
 
92
      (not query command (COM_QUERY))
 
93
    */
 
94
    if (! (session->lex->current_select &&
 
95
        session->lex->current_select->no_error && !session->is_fatal_error))
 
96
    {
 
97
      if (! session->main_da.is_error())            // Return only first message
 
98
      {
 
99
        if (error == 0)
 
100
          error= ER_UNKNOWN_ERROR;
 
101
        if (str == NULL)
 
102
          str= ER(error);
 
103
        session->main_da.set_error_status(error, str);
 
104
      }
 
105
    }
 
106
 
 
107
    if (!session->no_warnings_for_error && !session->is_fatal_error)
 
108
    {
 
109
      /*
 
110
        Suppress infinite recursion if there a memory allocation error
 
111
        inside push_warning.
 
112
      */
 
113
      session->no_warnings_for_error= true;
 
114
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
 
115
      session->no_warnings_for_error= false;
 
116
      }
 
117
    }
 
118
    if (!session || MyFlags & ME_NOREFRESH)
 
119
        errmsg_printf(ERRMSG_LVL_ERROR, "%s: %s",internal::my_progname,str);
 
120
}
 
121
 
 
122
static void init_signals(void)
 
123
{
 
124
  sigset_t set;
 
125
  struct sigaction sa;
 
126
 
 
127
  if (!(test_flags.test(TEST_NO_STACKTRACE) || 
 
128
        test_flags.test(TEST_CORE_ON_SIGNAL)))
 
129
  {
 
130
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
 
131
    sigemptyset(&sa.sa_mask);
 
132
    sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
 
133
 
 
134
    init_stacktrace();
 
135
    sa.sa_handler= drizzled_handle_segfault;
 
136
    sigaction(SIGSEGV, &sa, NULL);
 
137
    sigaction(SIGABRT, &sa, NULL);
 
138
#ifdef SIGBUS
 
139
    sigaction(SIGBUS, &sa, NULL);
 
140
#endif
 
141
    sigaction(SIGILL, &sa, NULL);
 
142
    sigaction(SIGFPE, &sa, NULL);
 
143
  }
 
144
 
 
145
  if (test_flags.test(TEST_CORE_ON_SIGNAL))
 
146
  {
 
147
    /* Change limits so that we will get a core file */
 
148
    struct rlimit rl;
 
149
    rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
 
150
    if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
 
151
        errmsg_printf(ERRMSG_LVL_WARN,
 
152
                      _("setrlimit could not change the size of core files "
 
153
                        "to 'infinity';  We may not be able to generate a "
 
154
                        "core file on signals"));
 
155
  }
 
156
  (void) sigemptyset(&set);
 
157
  ignore_signal(SIGPIPE);
 
158
  sigaddset(&set,SIGPIPE);
 
159
#ifndef IGNORE_SIGHUP_SIGQUIT
 
160
  sigaddset(&set,SIGQUIT);
 
161
  sigaddset(&set,SIGHUP);
 
162
#endif
 
163
  sigaddset(&set,SIGTERM);
 
164
 
 
165
  /* Fix signals if blocked by parents (can happen on Mac OS X) */
 
166
  sigemptyset(&sa.sa_mask);
 
167
  sa.sa_flags = 0;
 
168
  sa.sa_handler = drizzled_print_signal_warning;
 
169
  sigaction(SIGTERM, &sa, NULL);
 
170
  sa.sa_flags = 0;
 
171
  sa.sa_handler = drizzled_print_signal_warning;
 
172
  sigaction(SIGHUP, &sa, NULL);
 
173
#ifdef SIGTSTP
 
174
  sigaddset(&set,SIGTSTP);
 
175
#endif
 
176
  if (test_flags.test(TEST_SIGINT))
 
177
  {
 
178
    sa.sa_flags= 0;
 
179
    sa.sa_handler= drizzled_end_thread_signal;
 
180
    sigaction(thr_kill_signal, &sa, NULL);
 
181
 
 
182
    // May be SIGINT
 
183
    sigdelset(&set, thr_kill_signal);
 
184
  }
 
185
  else
 
186
    sigaddset(&set,SIGINT);
 
187
  sigprocmask(SIG_SETMASK,&set,NULL);
 
188
  pthread_sigmask(SIG_SETMASK,&set,NULL);
 
189
  return;
 
190
}
 
191
 
 
192
 
 
193
int main(int argc, char **argv)
 
194
{
 
195
#if defined(ENABLE_NLS)
 
196
# if defined(HAVE_LOCALE_H)
 
197
  setlocale(LC_ALL, "");
 
198
# endif
 
199
  bindtextdomain("drizzle", LOCALEDIR);
 
200
  textdomain("drizzle");
 
201
#endif
 
202
 
 
203
  plugin::Registry &plugins= plugin::Registry::singleton();
 
204
  plugin::Client *client;
 
205
  Session *session;
 
206
 
 
207
  MY_INIT(argv[0]);             // init my_sys library & pthreads
 
208
  /* nothing should come before this line ^^^ */
 
209
 
 
210
  /* Set signal used to kill Drizzle */
 
211
#if defined(SIGUSR2)
 
212
  thr_kill_signal= internal::thd_lib_detected == THD_LIB_LT ? SIGINT : SIGUSR2;
 
213
#else
 
214
  thr_kill_signal= SIGINT;
 
215
#endif
 
216
 
 
217
  if (init_common_variables(DRIZZLE_CONFIG_NAME,
 
218
                            argc, argv, load_default_groups))
 
219
    unireg_abort(1);                            // Will do exit
 
220
 
 
221
  init_signals();
 
222
 
 
223
 
 
224
  select_thread=pthread_self();
 
225
  select_thread_in_use=1;
 
226
 
 
227
  if (chdir(data_home_real) && !opt_help)
 
228
  {
 
229
    errmsg_printf(ERRMSG_LVL_ERROR, _("Data directory %s does not exist\n"), data_home_real);
 
230
    unireg_abort(1);
 
231
  }
 
232
  data_home= data_home_buff;
 
233
  data_home[0]=FN_CURLIB;               // all paths are relative from here
 
234
  data_home[1]=0;
 
235
  data_home_len= 2;
 
236
 
 
237
  if ((user_info= check_user(drizzled_user)))
 
238
  {
 
239
    set_user(drizzled_user, user_info);
 
240
  }
 
241
 
 
242
  if (server_id == 0)
 
243
  {
 
244
    server_id= 1;
 
245
  }
 
246
 
 
247
  if (init_server_components(plugins))
 
248
    unireg_abort(1);
 
249
 
 
250
  /**
 
251
   * This check must be done after init_server_components for now
 
252
   * because we don't yet have plugin dependency tracking...
 
253
   *
 
254
   * ReplicationServices::evaluateRegisteredPlugins() will print error messages to stderr
 
255
   * via errmsg_printf().
 
256
   *
 
257
   * @todo
 
258
   *
 
259
   * not checking return since unireg_abort() hangs
 
260
   */
 
261
  ReplicationServices &replication_services= ReplicationServices::singleton();
 
262
    (void) replication_services.evaluateRegisteredPlugins();
 
263
 
 
264
  if (plugin::Listen::setup())
 
265
    unireg_abort(1);
 
266
 
 
267
  /*
 
268
    init signals & alarm
 
269
    After this we can't quit by a simple unireg_abort
 
270
  */
 
271
  error_handler_hook= my_message_sql;
 
272
 
 
273
  assert(plugin::num_trx_monitored_objects > 0);
 
274
  if (drizzle_rm_tmp_tables() ||
 
275
      my_tz_init((Session *)0, default_tz_name))
 
276
  {
 
277
    abort_loop= true;
 
278
    select_thread_in_use=0;
 
279
    (void) pthread_kill(signal_thread, SIGTERM);
 
280
 
 
281
    (void) unlink(pidfile_name);        // Not needed anymore
 
282
 
 
283
    exit(1);
 
284
  }
 
285
 
 
286
  init_status_vars();
 
287
 
 
288
  errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_STARTUP)), internal::my_progname,
 
289
                PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
 
290
 
 
291
 
 
292
  /* Listen for new connections and start new session for each connection
 
293
     accepted. The listen.getClient() method will return NULL when the server
 
294
     should be shutdown. */
 
295
  while ((client= plugin::Listen::getClient()) != NULL)
 
296
  {
 
297
    if (!(session= new Session(client)))
 
298
    {
 
299
      delete client;
 
300
      continue;
 
301
    }
 
302
 
 
303
    /* If we error on creation we drop the connection and delete the session. */
 
304
    if (session->schedule())
 
305
      Session::unlink(session);
 
306
  }
 
307
 
 
308
  /* (void) pthread_attr_destroy(&connection_attrib); */
 
309
 
 
310
 
 
311
  (void) pthread_mutex_lock(&LOCK_thread_count);
 
312
  select_thread_in_use=0;                       // For close_connections
 
313
  (void) pthread_mutex_unlock(&LOCK_thread_count);
 
314
  (void) pthread_cond_broadcast(&COND_thread_count);
 
315
 
 
316
  /* Wait until cleanup is done */
 
317
  (void) pthread_mutex_lock(&LOCK_thread_count);
 
318
  while (!ready_to_exit)
 
319
    pthread_cond_wait(&COND_server_end,&LOCK_thread_count);
 
320
  (void) pthread_mutex_unlock(&LOCK_thread_count);
 
321
 
 
322
  clean_up(1);
 
323
  plugin::Registry::shutdown();
 
324
  clean_up_mutexes();
 
325
  internal::my_end();
 
326
  return 0;
 
327
}
 
328