~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/main.cc

  • Committer: Brian Aker
  • Date: 2011-02-12 08:10:17 UTC
  • mto: This revision was merged to the branch mainline in revision 2161.
  • Revision ID: brian@tangent.org-20110212081017-7793i41ybt7gp5ty
More removal of session from includes.

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>
74
69
 
75
70
using namespace drizzled;
76
71
using namespace std;
 
72
namespace fs=boost::filesystem;
77
73
 
78
74
static pthread_t select_thread;
79
75
static uint32_t thr_kill_signal;
80
76
 
81
 
extern bool opt_daemon;
82
 
 
83
77
 
84
78
/**
85
79
  All global error messages are sent here where the first one is stored
87
81
*/
88
82
static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
89
83
{
90
 
  Session* session= current_session;
 
84
  Session *session;
91
85
  /*
92
86
    Put here following assertion when situation with EE_* error codes
93
87
    will be fixed
94
88
  */
95
 
  if (session)
 
89
  if ((session= current_session))
96
90
  {
97
91
    if (MyFlags & ME_FATALERROR)
98
92
      session->is_fatal_error= 1;
100
94
    /*
101
95
      @TODO There are two exceptions mechanism (Session and sp_rcontext),
102
96
      this could be improved by having a common stack of handlers.
103
 
 
 
97
    */
104
98
    if (session->handle_error(error, str, DRIZZLE_ERROR::WARN_LEVEL_ERROR))
105
99
      return;
106
 
    */
107
100
 
108
101
    /*
109
 
      session->lex().current_select == 0 if lex structure is not inited
 
102
      session->lex->current_select == 0 if lex structure is not inited
110
103
      (not query command (COM_QUERY))
111
104
    */
112
 
    if (! (session->lex().current_select &&
113
 
           session->lex().current_select->no_error && !session->is_fatal_error))
 
105
    if (! (session->lex->current_select &&
 
106
           session->lex->current_select->no_error && !session->is_fatal_error))
114
107
    {
115
 
      if (! session->main_da().is_error())            // Return only first message
 
108
      if (! session->main_da.is_error())            // Return only first message
116
109
      {
117
110
        if (error == EE_OK)
118
111
          error= ER_UNKNOWN_ERROR;
120
113
        if (str == NULL)
121
114
          str= ER(error);
122
115
 
123
 
        session->main_da().set_error_status(error, str);
 
116
        session->main_da.set_error_status(error, str);
124
117
      }
125
118
    }
126
119
 
147
140
  sigset_t set;
148
141
  struct sigaction sa;
149
142
 
150
 
  if (not (getDebug().test(debug::NO_STACKTRACE) ||
 
143
  if (not (getDebug().test(debug::NO_STACKTRACE) || 
151
144
        getDebug().test(debug::CORE_ON_SIGNAL)))
152
145
  {
153
146
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
251
244
 
252
245
  /* Function generates error messages before abort */
253
246
  error_handler_hook= my_message_sql;
254
 
 
255
247
  /* init_common_variables must get basic settings such as data_home_dir
256
248
     and plugin_load_list. */
257
 
  if (init_basic_variables(argc, argv))
258
 
    unireg_abort(1);                            // Will do exit
259
 
 
260
 
  if (opt_daemon)
261
 
  {
262
 
    if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
263
 
    {
264
 
      perror("Failed to ignore SIGHUP");
265
 
    }
266
 
    if (daemonize())
267
 
    {
268
 
      fprintf(stderr, "failed to daemon() in order to daemonize\n");
269
 
      exit(EXIT_FAILURE);
270
 
    }
271
 
  }
272
 
 
273
 
  if (init_remaining_variables(modules))
 
249
  if (init_common_variables(argc, argv, modules))
274
250
    unireg_abort(1);                            // Will do exit
275
251
 
276
252
  /*
292
268
                    getDataHome().file_string().c_str());
293
269
      unireg_abort(1);
294
270
    }
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
271
    if (mkdir("local", 0700))
315
272
    {
316
273
      /* We don't actually care */
323
280
      unireg_abort(1);
324
281
    }
325
282
 
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());
 
283
    fs::path &full_data_home= getFullDataHome();
 
284
    full_data_home= fs::system_complete(getDataHome());
 
285
    std::cerr << "home " << full_data_home << std::endl;
329
286
  }
330
287
 
 
288
 
 
289
 
331
290
  if (server_id == 0)
332
291
  {
333
292
    server_id= 1;
368
327
    unireg_abort(1);
369
328
 
370
329
  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);
 
330
  if (drizzle_rm_tmp_tables() || my_tz_init((Session *)0, default_tz_name))
 
331
  {
 
332
    abort_loop= true;
 
333
    select_thread_in_use=0;
 
334
    (void) pthread_kill(signal_thread, SIGTERM);
 
335
 
 
336
    (void) unlink(pid_file.file_string().c_str());      // Not needed anymore
 
337
 
 
338
    unireg_abort(1);
 
339
  }
 
340
 
 
341
  errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname,
 
342
                PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
373
343
 
374
344
 
375
345
  TransactionServices &transaction_services= TransactionServices::singleton();
376
346
 
377
347
  /* Send server startup event */
378
348
  {
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());
 
349
    Session::shared_ptr session;
 
350
 
 
351
    if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
 
352
    {
 
353
      currentSession().release();
 
354
      currentSession().reset(session.get());
 
355
 
 
356
 
 
357
      transaction_services.sendStartupEvent(*session);
 
358
 
 
359
      plugin_startup_window(modules, *(session.get()));
 
360
    }
383
361
  }
384
362
 
385
 
  if (opt_daemon)
386
 
    daemon_is_ready();
387
363
 
388
 
  /*
 
364
  /* 
389
365
    Listen for new connections and start new session for each connection
390
366
     accepted. The listen.getClient() method will return NULL when the server
391
367
     should be shutdown.
392
368
   */
393
 
  while (plugin::Client* client= plugin::Listen::getClient())
 
369
  plugin::Client *client;
 
370
  while ((client= plugin::Listen::getClient()) != NULL)
394
371
  {
395
 
    Session::shared_ptr session= Session::make_shared(client, client->catalog());
 
372
    Session::shared_ptr session;
 
373
    session= Session::make_shared(client, client->catalog());
 
374
 
 
375
    if (not session)
 
376
    {
 
377
      delete client;
 
378
      continue;
 
379
    }
396
380
 
397
381
    /* If we error on creation we drop the connection and delete the session. */
398
382
    if (Session::schedule(session))
401
385
 
402
386
  /* Send server shutdown event */
403
387
  {
404
 
    Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
405
 
    currentSession().reset(session.get());
406
 
    transaction_services.sendShutdownEvent(*session.get());
 
388
    Session::shared_ptr session;
 
389
 
 
390
    if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
 
391
    {
 
392
      currentSession().release();
 
393
      currentSession().reset(session.get());
 
394
      transaction_services.sendShutdownEvent(*session.get());
 
395
    }
407
396
  }
408
397
 
409
398
  {
410
 
    boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
 
399
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
411
400
    select_thread_in_use= false;                        // For close_connections
412
401
  }
413
402
  COND_thread_count.notify_all();
414
403
 
415
404
  /* Wait until cleanup is done */
416
 
  session::Cache::shutdownSecond();
 
405
  session::Cache::singleton().shutdownSecond();
417
406
 
418
407
  clean_up(1);
419
408
  module::Registry::shutdown();