~drizzle-trunk/drizzle/development

1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
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>
1786.3.1 by Monty Taylor
Initial working local catalog.
26
#include <sys/stat.h>
27
#include <sys/types.h>
28
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
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
1300.5.7 by Monty Taylor
Fixed Sun Studio issues.
41
#if defined(HAVE_LOCALE_H)
42
# include <locale.h>
43
#endif
44
1791.3.5 by mordred
Now lets get the proper values into these bad boys.
45
#include <boost/filesystem.hpp>
1300.5.7 by Monty Taylor
Fixed Sun Studio issues.
46
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
47
#include "drizzled/plugin.h"
48
#include "drizzled/gettext.h"
49
#include "drizzled/configmake.h"
50
#include "drizzled/session.h"
1966.2.5 by Brian Aker
Style change around session list.
51
#include "drizzled/session/cache.h"
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
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"
1300.5.22 by Monty Taylor
Moved and reworked a wrapper around sigset - which we shouldn't be using
61
#include "drizzled/signal_handler.h"
1471.3.2 by Monty Taylor
Merged in old drizzled-as-lib patch.
62
#include "drizzled/replication_services.h"
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
63
#include "drizzled/transaction_services.h"
2039.6.3 by Brian Aker
Update for session to have a catalog object.
64
#include "drizzled/catalog/local.h"
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
65
1861.4.8 by Brian Aker
Adds in a portion of the ref constraints table.
66
#include "drizzled/util/backtrace.h"
67
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
68
using namespace drizzled;
69
using namespace std;
1791.3.5 by mordred
Now lets get the proper values into these bad boys.
70
namespace fs=boost::filesystem;
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
71
72
static pthread_t select_thread;
73
static uint32_t thr_kill_signal;
74
1791.3.4 by mordred
Well, so handing pointers returned by std::string::c_str() is a bad idea anyway - but you certainly shouldn't do it in static initializers before you do all of your changes to the string. Sigh.
75
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
76
/**
77
  All global error messages are sent here where the first one is stored
78
  for the client.
79
*/
2087.3.1 by Brian Aker
Entire convert over to time_t.
80
static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
81
{
82
  Session *session;
83
  /*
84
    Put here following assertion when situation with EE_* error codes
85
    will be fixed
86
  */
87
  if ((session= current_session))
88
  {
89
    if (MyFlags & ME_FATALERROR)
90
      session->is_fatal_error= 1;
91
92
    /*
93
      TODO: There are two exceptions mechanism (Session and sp_rcontext),
94
      this could be improved by having a common stack of handlers.
95
    */
96
    if (session->handle_error(error, str,
97
                          DRIZZLE_ERROR::WARN_LEVEL_ERROR))
98
      return;;
99
100
    /*
101
      session->lex->current_select == 0 if lex structure is not inited
102
      (not query command (COM_QUERY))
103
    */
104
    if (! (session->lex->current_select &&
105
        session->lex->current_select->no_error && !session->is_fatal_error))
106
    {
107
      if (! session->main_da.is_error())            // Return only first message
108
      {
2087.3.1 by Brian Aker
Entire convert over to time_t.
109
        if (error == EE_OK)
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
110
          error= ER_UNKNOWN_ERROR;
2087.3.1 by Brian Aker
Entire convert over to time_t.
111
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
112
        if (str == NULL)
113
          str= ER(error);
2087.3.1 by Brian Aker
Entire convert over to time_t.
114
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
115
        session->main_da.set_error_status(error, str);
116
      }
117
    }
118
119
    if (!session->no_warnings_for_error && !session->is_fatal_error)
120
    {
121
      /*
122
        Suppress infinite recursion if there a memory allocation error
123
        inside push_warning.
124
      */
125
      session->no_warnings_for_error= true;
126
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
127
      session->no_warnings_for_error= false;
128
      }
129
    }
130
    if (!session || MyFlags & ME_NOREFRESH)
131
        errmsg_printf(ERRMSG_LVL_ERROR, "%s: %s",internal::my_progname,str);
132
}
133
134
static void init_signals(void)
135
{
136
  sigset_t set;
137
  struct sigaction sa;
138
139
  if (!(test_flags.test(TEST_NO_STACKTRACE) || 
140
        test_flags.test(TEST_CORE_ON_SIGNAL)))
141
  {
142
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
143
    sigemptyset(&sa.sa_mask);
144
    sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);
145
146
    sa.sa_handler= drizzled_handle_segfault;
147
    sigaction(SIGSEGV, &sa, NULL);
148
    sigaction(SIGABRT, &sa, NULL);
149
#ifdef SIGBUS
150
    sigaction(SIGBUS, &sa, NULL);
151
#endif
152
    sigaction(SIGILL, &sa, NULL);
153
    sigaction(SIGFPE, &sa, NULL);
154
  }
155
156
  if (test_flags.test(TEST_CORE_ON_SIGNAL))
157
  {
158
    /* Change limits so that we will get a core file */
159
    struct rlimit rl;
160
    rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
161
    if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
162
        errmsg_printf(ERRMSG_LVL_WARN,
163
                      _("setrlimit could not change the size of core files "
164
                        "to 'infinity';  We may not be able to generate a "
165
                        "core file on signals"));
166
  }
167
  (void) sigemptyset(&set);
1300.5.23 by Monty Taylor
Merged in revs removing depend on the plugin tree.
168
  ignore_signal(SIGPIPE);
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
169
  sigaddset(&set,SIGPIPE);
170
#ifndef IGNORE_SIGHUP_SIGQUIT
171
  sigaddset(&set,SIGQUIT);
172
  sigaddset(&set,SIGHUP);
173
#endif
174
  sigaddset(&set,SIGTERM);
175
176
  /* Fix signals if blocked by parents (can happen on Mac OS X) */
177
  sigemptyset(&sa.sa_mask);
178
  sa.sa_flags = 0;
179
  sa.sa_handler = drizzled_print_signal_warning;
1300.5.23 by Monty Taylor
Merged in revs removing depend on the plugin tree.
180
  sigaction(SIGTERM, &sa, NULL);
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
181
  sa.sa_flags = 0;
182
  sa.sa_handler = drizzled_print_signal_warning;
1300.5.23 by Monty Taylor
Merged in revs removing depend on the plugin tree.
183
  sigaction(SIGHUP, &sa, NULL);
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
184
#ifdef SIGTSTP
185
  sigaddset(&set,SIGTSTP);
186
#endif
187
  if (test_flags.test(TEST_SIGINT))
188
  {
1300.5.23 by Monty Taylor
Merged in revs removing depend on the plugin tree.
189
    sa.sa_flags= 0;
190
    sa.sa_handler= drizzled_end_thread_signal;
191
    sigaction(thr_kill_signal, &sa, NULL);
192
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
193
    // May be SIGINT
194
    sigdelset(&set, thr_kill_signal);
195
  }
196
  else
1608.1.2 by Brian Aker
Merge enum test
197
  {
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
198
    sigaddset(&set,SIGINT);
1608.1.2 by Brian Aker
Merge enum test
199
  }
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
200
  sigprocmask(SIG_SETMASK,&set,NULL);
201
  pthread_sigmask(SIG_SETMASK,&set,NULL);
1300.5.23 by Monty Taylor
Merged in revs removing depend on the plugin tree.
202
  return;
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
203
}
204
2017.2.1 by Brian Aker
Cleanup error messages around bad table definitions.
205
static void GoogleProtoErrorThrower(google::protobuf::LogLevel level,
206
                                    const char* ,
207
                                    int, const string& ) throw(const char *)
1608.1.2 by Brian Aker
Merge enum test
208
{
209
  switch(level)
210
  {
211
  case google::protobuf::LOGLEVEL_INFO:
212
    break;
213
  case google::protobuf::LOGLEVEL_WARNING:
214
  case google::protobuf::LOGLEVEL_ERROR:
215
  case google::protobuf::LOGLEVEL_FATAL:
216
  default:
217
    throw("error in google protocol buffer parsing");
218
  }
219
}
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
220
221
int main(int argc, char **argv)
222
{
223
#if defined(ENABLE_NLS)
224
# if defined(HAVE_LOCALE_H)
225
  setlocale(LC_ALL, "");
226
# endif
2068.4.1 by Andrew Hutchings
Fix intl domain
227
  bindtextdomain("drizzle7", LOCALEDIR);
228
  textdomain("drizzle7");
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
229
#endif
230
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
231
  module::Registry &modules= module::Registry::singleton();
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
232
233
  MY_INIT(argv[0]);		// init my_sys library & pthreads
234
  /* nothing should come before this line ^^^ */
235
236
  /* Set signal used to kill Drizzle */
237
  thr_kill_signal= SIGINT;
238
1608.1.2 by Brian Aker
Merge enum test
239
  google::protobuf::SetLogHandler(&GoogleProtoErrorThrower);
240
1743.4.1 by LinuxJedi
Make sure unireg_abort shows the reason for the fail.
241
  /* Function generates error messages before abort */
1794.3.5 by Monty Taylor
Fixed temporoary dir sequencing.
242
  error_handler_hook= my_message_sql;
1757.2.1 by Monty Taylor
Moved where we set errmsg_handler_hook.
243
  /* init_common_variables must get basic settings such as data_home_dir
244
     and plugin_load_list. */
1794.3.1 by Monty Taylor
Rearranged option processing.
245
  if (init_common_variables(argc, argv, modules))
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
246
    unireg_abort(1);				// Will do exit
247
1794.3.5 by Monty Taylor
Fixed temporoary dir sequencing.
248
  /*
249
    init signals & alarm
250
    After this we can't quit by a simple unireg_abort
251
  */
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
252
  init_signals();
253
254
255
  select_thread=pthread_self();
256
  select_thread_in_use=1;
257
1786.3.1 by Monty Taylor
Initial working local catalog.
258
  if (not opt_help)
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
259
  {
1813.2.9 by Monty Taylor
Made data_home be fs::path natively.
260
    if (chdir(getDataHome().file_string().c_str()))
1786.3.1 by Monty Taylor
Initial working local catalog.
261
    {
262
      errmsg_printf(ERRMSG_LVL_ERROR,
263
                    _("Data directory %s does not exist\n"),
1813.2.9 by Monty Taylor
Made data_home be fs::path natively.
264
                    getDataHome().file_string().c_str());
1786.3.1 by Monty Taylor
Initial working local catalog.
265
      unireg_abort(1);
266
    }
267
    if (mkdir("local", 0700))
268
    {
269
      /* We don't actually care */
270
    }
271
    if (chdir("local"))
272
    {
273
      errmsg_printf(ERRMSG_LVL_ERROR,
274
                    _("Local catalog %s/local does not exist\n"),
1813.2.9 by Monty Taylor
Made data_home be fs::path natively.
275
                    getDataHome().file_string().c_str());
1786.3.1 by Monty Taylor
Initial working local catalog.
276
      unireg_abort(1);
277
    }
1813.2.9 by Monty Taylor
Made data_home be fs::path natively.
278
279
    full_data_home= fs::system_complete(getDataHome());
1786.3.2 by Monty Taylor
Cleaned up the initial pass at this. It's not perfect, but it does work.
280
    getDataHomeCatalog()= "./";
281
    getDataHome()= "../";
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
282
  }
1786.3.1 by Monty Taylor
Initial working local catalog.
283
284
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
285
286
  if (server_id == 0)
287
  {
288
    server_id= 1;
289
  }
290
1757.2.6 by Monty Taylor
We've gotten further. Now things sometimes work, but some things aren't
291
  if (init_server_components(modules))
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
292
    unireg_abort(1);
293
1471.3.2 by Monty Taylor
Merged in old drizzled-as-lib patch.
294
  /**
295
   * This check must be done after init_server_components for now
296
   * because we don't yet have plugin dependency tracking...
297
   *
298
   * ReplicationServices::evaluateRegisteredPlugins() will print error messages to stderr
299
   * via errmsg_printf().
300
   *
301
   * @todo
302
   *
303
   * not checking return since unireg_abort() hangs
304
   */
305
  ReplicationServices &replication_services= ReplicationServices::singleton();
306
    (void) replication_services.evaluateRegisteredPlugins();
307
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
308
  if (plugin::Listen::setup())
309
    unireg_abort(1);
310
1757.2.1 by Monty Taylor
Moved where we set errmsg_handler_hook.
311
1300.5.17 by Monty Taylor
Merged trunk.
312
  assert(plugin::num_trx_monitored_objects > 0);
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
313
  if (drizzle_rm_tmp_tables() ||
314
      my_tz_init((Session *)0, default_tz_name))
315
  {
316
    abort_loop= true;
317
    select_thread_in_use=0;
318
    (void) pthread_kill(signal_thread, SIGTERM);
319
1813.2.2 by Monty Taylor
Replaced pid-file with fs::path.
320
    (void) unlink(pid_file.file_string().c_str());	// Not needed anymore
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
321
322
    exit(1);
323
  }
324
325
  errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_STARTUP)), internal::my_progname,
326
                PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
327
328
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
329
  TransactionServices &transaction_services= TransactionServices::singleton();
330
331
  /* Send server startup event */
332
  {
2039.6.3 by Brian Aker
Update for session to have a catalog object.
333
    Session::shared_ptr session;
1932.3.3 by Brian Aker
Pull in code to abstract out the session list a bit.
334
2039.6.3 by Brian Aker
Update for session to have a catalog object.
335
    if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
1932.3.3 by Brian Aker
Pull in code to abstract out the session list a bit.
336
    {
337
      currentSession().release();
2039.6.3 by Brian Aker
Update for session to have a catalog object.
338
      currentSession().reset(session.get());
339
      transaction_services.sendStartupEvent(session.get());
1932.3.3 by Brian Aker
Pull in code to abstract out the session list a bit.
340
    }
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
341
  }
342
2040.7.2 by Monty Taylor
Revert unneeded change to the while loop.
343
2039.6.3 by Brian Aker
Update for session to have a catalog object.
344
  /* 
345
    Listen for new connections and start new session for each connection
2040.7.2 by Monty Taylor
Revert unneeded change to the while loop.
346
     accepted. The listen.getClient() method will return NULL when the server
2039.6.3 by Brian Aker
Update for session to have a catalog object.
347
     should be shutdown.
348
   */
349
  plugin::Client *client;
2040.7.2 by Monty Taylor
Revert unneeded change to the while loop.
350
  while ((client= plugin::Listen::getClient()) != NULL)
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
351
  {
2039.6.3 by Brian Aker
Update for session to have a catalog object.
352
    Session::shared_ptr session;
353
    session= Session::make_shared(client, catalog::local());
1932.3.3 by Brian Aker
Pull in code to abstract out the session list a bit.
354
2040.7.2 by Monty Taylor
Revert unneeded change to the while loop.
355
    if (not session)
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
356
    {
2040.7.2 by Monty Taylor
Revert unneeded change to the while loop.
357
      delete client;
358
      continue;
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
359
    }
360
2040.7.2 by Monty Taylor
Revert unneeded change to the while loop.
361
    /* If we error on creation we drop the connection and delete the session. */
362
    if (Session::schedule(session))
363
      Session::unlink(session);
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
364
  }
365
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
366
  /* Send server shutdown event */
367
  {
2039.6.3 by Brian Aker
Update for session to have a catalog object.
368
    Session::shared_ptr session;
1932.3.3 by Brian Aker
Pull in code to abstract out the session list a bit.
369
2039.6.3 by Brian Aker
Update for session to have a catalog object.
370
    if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
1932.3.3 by Brian Aker
Pull in code to abstract out the session list a bit.
371
    {
372
      currentSession().release();
2039.6.3 by Brian Aker
Update for session to have a catalog object.
373
      currentSession().reset(session.get());
374
      transaction_services.sendShutdownEvent(session.get());
1932.3.3 by Brian Aker
Pull in code to abstract out the session list a bit.
375
    }
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
376
  }
377
1932.3.4 by Brian Aker
Move counter lock so that ownership is held by session_list.
378
  {
379
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
380
    select_thread_in_use= false;			// For close_connections
381
  }
1689.2.5 by Brian Aker
Convert COND_thread_count to boost.
382
  COND_thread_count.notify_all();
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
383
384
  /* Wait until cleanup is done */
1798.3.1 by Brian Aker
Remove native_handle usage.
385
  {
1932.3.4 by Brian Aker
Move counter lock so that ownership is held by session_list.
386
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
387
    while (not ready_to_exit)
1798.3.1 by Brian Aker
Remove native_handle usage.
388
      COND_server_end.wait(scopedLock);
389
  }
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
390
391
  clean_up(1);
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
392
  module::Registry::shutdown();
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
393
  internal::my_end();
1798.3.1 by Brian Aker
Remove native_handle usage.
394
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
395
  return 0;
396
}
397