~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/main.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
 
20
#include "config.h"
21
21
 
22
22
#include <pthread.h>
23
23
#include <signal.h>
63
63
#include <drizzled/session/cache.h>
64
64
#include <drizzled/signal_handler.h>
65
65
#include <drizzled/transaction_services.h>
 
66
#include <drizzled/tztime.h>
66
67
#include <drizzled/unireg.h>
67
68
#include <drizzled/util/backtrace.h>
68
 
#include <drizzled/current_session.h>
69
 
#include <drizzled/daemon.h>
70
 
#include <drizzled/diagnostics_area.h>
71
 
#include <drizzled/sql_base.h>
72
 
#include <drizzled/sql_lex.h>
73
 
#include <drizzled/system_variables.h>
 
69
 
 
70
extern "C" int daemonize(int nochdir, int noclose, int wait_sigusr1);
 
71
extern "C" int daemon_is_ready(void);
74
72
 
75
73
using namespace drizzled;
76
74
using namespace std;
 
75
namespace fs=boost::filesystem;
77
76
 
78
77
static pthread_t select_thread;
79
78
static uint32_t thr_kill_signal;
87
86
*/
88
87
static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
89
88
{
90
 
  Session* session= current_session;
 
89
  Session *session;
91
90
  /*
92
91
    Put here following assertion when situation with EE_* error codes
93
92
    will be fixed
94
93
  */
95
 
  if (session)
 
94
  if ((session= current_session))
96
95
  {
97
96
    if (MyFlags & ME_FATALERROR)
98
97
      session->is_fatal_error= 1;
100
99
    /*
101
100
      @TODO There are two exceptions mechanism (Session and sp_rcontext),
102
101
      this could be improved by having a common stack of handlers.
103
 
 
 
102
    */
104
103
    if (session->handle_error(error, str, DRIZZLE_ERROR::WARN_LEVEL_ERROR))
105
104
      return;
106
 
    */
107
105
 
108
106
    /*
109
 
      session->lex().current_select == 0 if lex structure is not inited
 
107
      session->lex->current_select == 0 if lex structure is not inited
110
108
      (not query command (COM_QUERY))
111
109
    */
112
 
    if (! (session->lex().current_select &&
113
 
           session->lex().current_select->no_error && !session->is_fatal_error))
 
110
    if (! (session->lex->current_select &&
 
111
           session->lex->current_select->no_error && !session->is_fatal_error))
114
112
    {
115
 
      if (! session->main_da().is_error())            // Return only first message
 
113
      if (! session->main_da.is_error())            // Return only first message
116
114
      {
117
115
        if (error == EE_OK)
118
116
          error= ER_UNKNOWN_ERROR;
120
118
        if (str == NULL)
121
119
          str= ER(error);
122
120
 
123
 
        session->main_da().set_error_status(error, str);
 
121
        session->main_da.set_error_status(error, str);
124
122
      }
125
123
    }
126
124
 
147
145
  sigset_t set;
148
146
  struct sigaction sa;
149
147
 
150
 
  if (not (getDebug().test(debug::NO_STACKTRACE) ||
 
148
  if (not (getDebug().test(debug::NO_STACKTRACE) || 
151
149
        getDebug().test(debug::CORE_ON_SIGNAL)))
152
150
  {
153
151
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
263
261
    {
264
262
      perror("Failed to ignore SIGHUP");
265
263
    }
266
 
    if (daemonize())
 
264
    if (daemonize(1, 1, 1) == -1)
267
265
    {
268
266
      fprintf(stderr, "failed to daemon() in order to daemonize\n");
269
267
      exit(EXIT_FAILURE);
292
290
                    getDataHome().file_string().c_str());
293
291
      unireg_abort(1);
294
292
    }
295
 
 
296
 
    ifstream old_uuid_file ("server.uuid");
297
 
    if (old_uuid_file.is_open())
298
 
    {
299
 
      getline (old_uuid_file, server_uuid);
300
 
      old_uuid_file.close();
301
 
    } 
302
 
    else 
303
 
    {
304
 
      uuid_t uu;
305
 
      char uuid_string[37];
306
 
      uuid_generate_random(uu);
307
 
      uuid_unparse(uu, uuid_string);
308
 
      ofstream new_uuid_file ("server.uuid");
309
 
      new_uuid_file << uuid_string;
310
 
      new_uuid_file.close();
311
 
      server_uuid= string(uuid_string);
312
 
    }
313
 
 
314
293
    if (mkdir("local", 0700))
315
294
    {
316
295
      /* We don't actually care */
323
302
      unireg_abort(1);
324
303
    }
325
304
 
326
 
    boost::filesystem::path &full_data_home= getFullDataHome();
327
 
    full_data_home= boost::filesystem::system_complete(getDataHome());
328
 
    errmsg_printf(error::INFO, "Data Home directory is : %s", full_data_home.native_file_string().c_str());
 
305
    fs::path &full_data_home= getFullDataHome();
 
306
    full_data_home= fs::system_complete(getDataHome());
 
307
    std::cerr << "home " << full_data_home << std::endl;
329
308
  }
330
309
 
 
310
 
 
311
 
331
312
  if (server_id == 0)
332
313
  {
333
314
    server_id= 1;
368
349
    unireg_abort(1);
369
350
 
370
351
  assert(plugin::num_trx_monitored_objects > 0);
371
 
  drizzle_rm_tmp_tables();
372
 
  errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname, PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
 
352
  if (drizzle_rm_tmp_tables() || my_tz_init((Session *)0, default_tz_name))
 
353
  {
 
354
    abort_loop= true;
 
355
    select_thread_in_use=0;
 
356
    (void) pthread_kill(signal_thread, SIGTERM);
 
357
 
 
358
    (void) unlink(pid_file.file_string().c_str());      // Not needed anymore
 
359
 
 
360
    unireg_abort(1);
 
361
  }
 
362
 
 
363
  errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname,
 
364
                PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
373
365
 
374
366
 
375
367
  TransactionServices &transaction_services= TransactionServices::singleton();
376
368
 
377
369
  /* Send server startup event */
378
370
  {
379
 
    Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
380
 
    currentSession().reset(session.get());
381
 
    transaction_services.sendStartupEvent(*session);
382
 
    plugin_startup_window(modules, *session.get());
 
371
    Session::shared_ptr session;
 
372
 
 
373
    if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
 
374
    {
 
375
      currentSession().release();
 
376
      currentSession().reset(session.get());
 
377
 
 
378
 
 
379
      transaction_services.sendStartupEvent(*session);
 
380
 
 
381
      plugin_startup_window(modules, *(session.get()));
 
382
    }
383
383
  }
384
384
 
385
385
  if (opt_daemon)
386
386
    daemon_is_ready();
387
387
 
388
 
  /*
 
388
  /* 
389
389
    Listen for new connections and start new session for each connection
390
390
     accepted. The listen.getClient() method will return NULL when the server
391
391
     should be shutdown.
392
392
   */
393
 
  while (plugin::Client* client= plugin::Listen::getClient())
 
393
  plugin::Client *client;
 
394
  while ((client= plugin::Listen::getClient()) != NULL)
394
395
  {
395
 
    Session::shared_ptr session= Session::make_shared(client, client->catalog());
 
396
    Session::shared_ptr session;
 
397
    session= Session::make_shared(client, client->catalog());
 
398
 
 
399
    if (not session)
 
400
    {
 
401
      delete client;
 
402
      continue;
 
403
    }
396
404
 
397
405
    /* If we error on creation we drop the connection and delete the session. */
398
406
    if (Session::schedule(session))
401
409
 
402
410
  /* Send server shutdown event */
403
411
  {
404
 
    Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
405
 
    currentSession().reset(session.get());
406
 
    transaction_services.sendShutdownEvent(*session.get());
 
412
    Session::shared_ptr session;
 
413
 
 
414
    if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
 
415
    {
 
416
      currentSession().release();
 
417
      currentSession().reset(session.get());
 
418
      transaction_services.sendShutdownEvent(*session.get());
 
419
    }
407
420
  }
408
421
 
409
422
  {
410
 
    boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
 
423
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
411
424
    select_thread_in_use= false;                        // For close_connections
412
425
  }
413
426
  COND_thread_count.notify_all();
414
427
 
415
428
  /* Wait until cleanup is done */
416
 
  session::Cache::shutdownSecond();
 
429
  session::Cache::singleton().shutdownSecond();
417
430
 
418
431
  clean_up(1);
419
432
  module::Registry::shutdown();