~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_connect.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2007 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
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
 */
15
19
 
16
20
 
17
21
/*
19
23
*/
20
24
#include <drizzled/server_includes.h>
21
25
#include <drizzled/authentication.h>
22
 
#include <drizzled/drizzled_error_messages.h>
 
26
#include <drizzled/error.h>
 
27
#include <netdb.h>
 
28
 
 
29
extern scheduler_functions thread_scheduler;
23
30
 
24
31
#define MIN_HANDSHAKE_SIZE      6
25
32
 
54
61
/**
55
62
  Check if user exist and password supplied is correct.
56
63
 
57
 
  @param  thd         thread handle, thd->security_ctx->{host,user,ip} are used
 
64
  @param  session         thread handle, session->security_ctx->{host,user,ip} are used
58
65
  @param  command     originator of the check: now check_user is called
59
66
                      during connect and change user procedures; used for
60
67
                      logging.
67
74
 
68
75
  @note Host, user and passwd may point to communication buffer.
69
76
  Current implementation does not depend on that, but future changes
70
 
  should be done with this in mind; 'thd' is INOUT, all other params
 
77
  should be done with this in mind; 'session' is INOUT, all other params
71
78
  are 'IN'.
72
79
 
73
80
  @retval  0  OK
76
83
*/
77
84
 
78
85
int
79
 
check_user(THD *thd, enum enum_server_command command,
80
 
           const char *passwd,
81
 
           uint passwd_len, const char *db,
 
86
check_user(Session *session, const char *passwd,
 
87
           uint32_t passwd_len, const char *db,
82
88
           bool check_count)
83
89
{
84
90
  LEX_STRING db_str= { (char *) db, db ? strlen(db) : 0 };
85
91
  bool is_authenticated;
86
92
 
87
93
  /*
88
 
    Clear thd->db as it points to something, that will be freed when
 
94
    Clear session->db as it points to something, that will be freed when
89
95
    connection is closed. We don't want to accidentally free a wrong
90
96
    pointer if connect failed. Also in case of 'CHANGE USER' failure,
91
97
    current database will be switched to 'no database selected'.
92
98
  */
93
 
  thd->reset_db(NULL, 0);
 
99
  session->reset_db(NULL, 0);
94
100
 
95
101
  if (passwd_len != 0 && passwd_len != SCRAMBLE_LENGTH)
96
102
  {
97
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
 
103
    my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
98
104
    return(1);
99
105
  }
100
106
 
101
 
  is_authenticated= authenticate_user(thd, passwd);
 
107
  is_authenticated= authenticate_user(session, passwd);
102
108
 
103
109
  if (is_authenticated != true)
104
110
  {
105
111
    my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
106
 
             thd->main_security_ctx.user,
107
 
             thd->main_security_ctx.ip,
 
112
             session->main_security_ctx.user,
 
113
             session->main_security_ctx.ip,
108
114
             passwd_len ? ER(ER_YES) : ER(ER_NO));
109
115
 
110
116
    return 1;
112
118
 
113
119
 
114
120
  USER_RESOURCES ur;
115
 
  thd->security_ctx->skip_grants();
 
121
  session->security_ctx->skip_grants();
116
122
  memset(&ur, 0, sizeof(USER_RESOURCES));
117
123
 
118
124
  if (check_count)
119
125
  {
120
126
    pthread_mutex_lock(&LOCK_connection_count);
121
127
    bool count_ok= connection_count <= max_connections;
122
 
    VOID(pthread_mutex_unlock(&LOCK_connection_count));
 
128
    pthread_mutex_unlock(&LOCK_connection_count);
123
129
 
124
130
    if (!count_ok)
125
131
    {                                         // too many connections
128
134
    }
129
135
  }
130
136
 
131
 
  /*
132
 
    Log the command before authentication checks, so that the user can
133
 
    check the log for the tried login tried and also to detect
134
 
    break-in attempts.
135
 
  */
136
 
  general_log_print(thd, command,
137
 
                    ((char*) "%s@%s on %s"),
138
 
                    thd->main_security_ctx.user,
139
 
                    thd->main_security_ctx.ip,
140
 
                    db ? db : (char*) "");
141
 
 
142
137
  /* Change database if necessary */
143
138
  if (db && db[0])
144
139
  {
145
 
    if (mysql_change_db(thd, &db_str, false))
 
140
    if (mysql_change_db(session, &db_str, false))
146
141
    {
147
142
      /* mysql_change_db() has pushed the error message. */
148
143
      return(1);
149
144
    }
150
145
  }
151
 
  my_ok(thd);
152
 
  thd->password= test(passwd_len);          // remember for error messages 
 
146
  my_ok(session);
 
147
  session->password= test(passwd_len);          // remember for error messages 
153
148
  /* Ready to handle queries */
154
149
  return(0);
155
150
}
160
155
  started with corresponding variable that is greater then 0.
161
156
*/
162
157
 
163
 
extern "C" uchar *get_key_conn(user_conn *buff, size_t *length,
 
158
extern "C" unsigned char *get_key_conn(user_conn *buff, size_t *length,
164
159
                               bool not_used __attribute__((unused)))
165
160
{
166
161
  *length= buff->len;
167
 
  return (uchar*) buff->user;
 
162
  return (unsigned char*) buff->user;
168
163
}
169
164
 
170
165
 
171
166
extern "C" void free_user(struct user_conn *uc)
172
167
{
173
 
  my_free((char*) uc,MYF(0));
 
168
  free((char*) uc);
174
169
}
175
170
 
176
 
void thd_init_client_charset(THD *thd, uint cs_number)
 
171
void session_init_client_charset(Session *session, uint32_t cs_number)
177
172
{
178
173
  /*
179
174
   Use server character set and collation if
183
178
   - client character set doesn't exists in server
184
179
  */
185
180
  if (!opt_character_set_client_handshake ||
186
 
      !(thd->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
 
181
      !(session->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
187
182
      !my_strcasecmp(&my_charset_utf8_general_ci,
188
183
                     global_system_variables.character_set_client->name,
189
 
                     thd->variables.character_set_client->name))
 
184
                     session->variables.character_set_client->name))
190
185
  {
191
 
    thd->variables.character_set_client=
 
186
    session->variables.character_set_client=
192
187
      global_system_variables.character_set_client;
193
 
    thd->variables.collation_connection=
 
188
    session->variables.collation_connection=
194
189
      global_system_variables.collation_connection;
195
 
    thd->variables.character_set_results=
 
190
    session->variables.character_set_results=
196
191
      global_system_variables.character_set_results;
197
192
  }
198
193
  else
199
194
  {
200
 
    thd->variables.character_set_results=
201
 
      thd->variables.collation_connection= 
202
 
      thd->variables.character_set_client;
 
195
    session->variables.character_set_results=
 
196
      session->variables.collation_connection= 
 
197
      session->variables.character_set_client;
203
198
  }
204
199
}
205
200
 
218
213
}
219
214
 
220
215
/*
221
 
  Perform handshake, authorize client and update thd ACL variables.
 
216
  Perform handshake, authorize client and update session ACL variables.
222
217
 
223
218
  SYNOPSIS
224
219
    check_connection()
225
 
    thd  thread handle
 
220
    session  thread handle
226
221
 
227
222
  RETURN
228
 
     0  success, OK is sent to user, thd is updated.
 
223
     0  success, OK is sent to user, session is updated.
229
224
    -1  error, which is sent to user
230
225
   > 0  error code (not sent to user)
231
226
*/
232
227
 
233
 
static int check_connection(THD *thd)
 
228
static int check_connection(Session *session)
234
229
{
235
 
  NET *net= &thd->net;
 
230
  NET *net= &session->net;
236
231
  uint32_t pkt_len= 0;
237
232
  char *end;
238
233
 
240
235
  {
241
236
    char ip[NI_MAXHOST];
242
237
 
243
 
    if (net_peer_addr(net, ip, &thd->peer_port, NI_MAXHOST))
 
238
    if (net_peer_addr(net, ip, &session->peer_port, NI_MAXHOST))
244
239
    {
245
 
      my_error(ER_BAD_HOST_ERROR, MYF(0), thd->main_security_ctx.ip);
 
240
      my_error(ER_BAD_HOST_ERROR, MYF(0), session->main_security_ctx.ip);
246
241
      return 1;
247
242
    }
248
 
    if (!(thd->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
 
243
    if (!(session->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
249
244
      return 1; /* The error is set by my_strdup(). */
250
245
  }
251
246
  net_keepalive(net, true);
262
257
    server_capabilites|= CLIENT_COMPRESS;
263
258
#endif /* HAVE_COMPRESS */
264
259
 
265
 
    end= stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
266
 
    int4store((uchar*) end, thd->thread_id);
 
260
    end= my_stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
 
261
    int4store((unsigned char*) end, session->thread_id);
267
262
    end+= 4;
268
263
    /*
269
264
      So as check_connection is the only entry point to authorization
270
265
      procedure, scramble is set here. This gives us new scramble for
271
266
      each handshake.
272
267
    */
273
 
    create_random_string(thd->scramble, SCRAMBLE_LENGTH, &thd->rand);
 
268
    create_random_string(session->scramble, SCRAMBLE_LENGTH, &session->rand);
274
269
    /*
275
270
      Old clients does not understand long scrambles, but can ignore packet
276
271
      tail: that's why first part of the scramble is placed here, and second
277
272
      part at the end of packet.
278
273
    */
279
 
    end= strmake(end, thd->scramble, SCRAMBLE_LENGTH_323) + 1;
 
274
    end= strmake(end, session->scramble, SCRAMBLE_LENGTH_323) + 1;
280
275
   
281
276
    int2store(end, server_capabilites);
282
277
    /* write server characteristics: up to 16 bytes allowed */
283
278
    end[2]=(char) default_charset_info->number;
284
 
    int2store(end+3, thd->server_status);
 
279
    int2store(end+3, session->server_status);
285
280
    memset(end+5, 0, 13);
286
281
    end+= 18;
287
282
    /* write scramble tail */
288
 
    end= strmake(end, thd->scramble + SCRAMBLE_LENGTH_323, 
 
283
    end= strmake(end, session->scramble + SCRAMBLE_LENGTH_323, 
289
284
                 SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323) + 1;
290
285
 
291
286
    /* At this point we write connection message and read reply */
292
 
    if (net_write_command(net, (uchar) protocol_version, (uchar*) "", 0,
293
 
                          (uchar*) buff, (size_t) (end-buff)) ||
 
287
    if (net_write_command(net, (unsigned char) protocol_version, (unsigned char*) "", 0,
 
288
                          (unsigned char*) buff, (size_t) (end-buff)) ||
294
289
        (pkt_len= my_net_read(net)) == packet_error ||
295
290
        pkt_len < MIN_HANDSHAKE_SIZE)
296
291
    {
297
292
      my_error(ER_HANDSHAKE_ERROR, MYF(0),
298
 
               thd->main_security_ctx.ip);
 
293
               session->main_security_ctx.ip);
299
294
      return 1;
300
295
    }
301
296
  }
302
 
  if (thd->packet.alloc(thd->variables.net_buffer_length))
 
297
  if (session->packet.alloc(session->variables.net_buffer_length))
303
298
    return 1; /* The error is set by alloc(). */
304
299
 
305
 
  thd->client_capabilities= uint2korr(net->read_pos);
306
 
 
307
 
 
308
 
  thd->client_capabilities|= ((uint32_t) uint2korr(net->read_pos+2)) << 16;
309
 
  thd->max_client_packet_length= uint4korr(net->read_pos+4);
310
 
  thd_init_client_charset(thd, (uint) net->read_pos[8]);
311
 
  thd->update_charset();
 
300
  session->client_capabilities= uint2korr(net->read_pos);
 
301
 
 
302
 
 
303
  session->client_capabilities|= ((uint32_t) uint2korr(net->read_pos+2)) << 16;
 
304
  session->max_client_packet_length= uint4korr(net->read_pos+4);
 
305
  session_init_client_charset(session, (uint) net->read_pos[8]);
 
306
  session->update_charset();
312
307
  end= (char*) net->read_pos+32;
313
308
 
314
309
  /*
315
310
    Disable those bits which are not supported by the server.
316
311
    This is a precautionary measure, if the client lies. See Bug#27944.
317
312
  */
318
 
  thd->client_capabilities&= server_capabilites;
 
313
  session->client_capabilities&= server_capabilites;
319
314
 
320
315
  if (end >= (char*) net->read_pos+ pkt_len +2)
321
316
  {
322
317
 
323
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
 
318
    my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
324
319
    return 1;
325
320
  }
326
321
 
327
 
  if (thd->client_capabilities & CLIENT_INTERACTIVE)
328
 
    thd->variables.net_wait_timeout= thd->variables.net_interactive_timeout;
329
 
  if ((thd->client_capabilities & CLIENT_TRANSACTIONS) &&
 
322
  if (session->client_capabilities & CLIENT_INTERACTIVE)
 
323
    session->variables.net_wait_timeout= session->variables.net_interactive_timeout;
 
324
  if ((session->client_capabilities & CLIENT_TRANSACTIONS) &&
330
325
      opt_using_transactions)
331
 
    net->return_status= &thd->server_status;
 
326
    net->return_status= &session->server_status;
332
327
 
333
328
  char *user= end;
334
329
  char *passwd= strchr(user, '\0')+1;
335
 
  uint user_len= passwd - user - 1;
 
330
  uint32_t user_len= passwd - user - 1;
336
331
  char *db= passwd;
337
332
  char db_buff[NAME_LEN + 1];           // buffer to store db in utf8
338
333
  char user_buff[USERNAME_LENGTH + 1];  // buffer to store user in utf8
339
 
  uint dummy_errors;
 
334
  uint32_t dummy_errors;
340
335
 
341
336
  /*
342
337
    Old clients send null-terminated string as password; new clients send
348
343
    Cast *passwd to an unsigned char, so that it doesn't extend the sign for
349
344
    *passwd > 127 and become 2**32-127+ after casting to uint.
350
345
  */
351
 
  uint passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
352
 
    (uchar)(*passwd++) : strlen(passwd);
353
 
  db= thd->client_capabilities & CLIENT_CONNECT_WITH_DB ?
 
346
  uint32_t passwd_len= session->client_capabilities & CLIENT_SECURE_CONNECTION ?
 
347
    (unsigned char)(*passwd++) : strlen(passwd);
 
348
  db= session->client_capabilities & CLIENT_CONNECT_WITH_DB ?
354
349
    db + passwd_len + 1 : 0;
355
350
  /* strlen() can't be easily deleted without changing protocol */
356
 
  uint db_len= db ? strlen(db) : 0;
 
351
  uint32_t db_len= db ? strlen(db) : 0;
357
352
 
358
353
  if (passwd + passwd_len + db_len > (char *)net->read_pos + pkt_len)
359
354
  {
360
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
 
355
    my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
361
356
    return 1;
362
357
  }
363
358
 
367
362
    db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
368
363
                             system_charset_info,
369
364
                             db, db_len,
370
 
                             thd->charset(), &dummy_errors)]= 0;
 
365
                             session->charset(), &dummy_errors)]= 0;
371
366
    db= db_buff;
372
367
  }
373
368
 
374
369
  user_buff[user_len= copy_and_convert(user_buff, sizeof(user_buff)-1,
375
370
                                       system_charset_info, user, user_len,
376
 
                                       thd->charset(), &dummy_errors)]= '\0';
 
371
                                       session->charset(), &dummy_errors)]= '\0';
377
372
  user= user_buff;
378
373
 
379
374
  /* If username starts and ends in "'", chop them off */
384
379
    user_len-= 2;
385
380
  }
386
381
 
387
 
  if (thd->main_security_ctx.user)
388
 
    x_free(thd->main_security_ctx.user);
389
 
  if (!(thd->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
 
382
  if (session->main_security_ctx.user)
 
383
    if (session->main_security_ctx.user)
 
384
      free(session->main_security_ctx.user);
 
385
  if (!(session->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
390
386
    return 1; /* The error is set by my_strdup(). */
391
 
  return check_user(thd, COM_CONNECT, passwd, passwd_len, db, true);
 
387
  return check_user(session, passwd, passwd_len, db, true);
392
388
}
393
389
 
394
390
 
397
393
 
398
394
  SYNOPSIS
399
395
    bool setup_connection_thread_globals()
400
 
    thd    Thread/connection handler
 
396
    session    Thread/connection handler
401
397
 
402
398
  RETURN
403
399
    0   ok
405
401
        In this case we will close the connection and increment status
406
402
*/
407
403
 
408
 
bool setup_connection_thread_globals(THD *thd)
 
404
bool setup_connection_thread_globals(Session *session)
409
405
{
410
 
  if (thd->store_globals())
 
406
  if (session->store_globals())
411
407
  {
412
 
    close_connection(thd, ER_OUT_OF_RESOURCES, 1);
 
408
    close_connection(session, ER_OUT_OF_RESOURCES, 1);
413
409
    statistic_increment(aborted_connects,&LOCK_status);
414
 
    thread_scheduler.end_thread(thd, 0);
 
410
    thread_scheduler.end_thread(session, 0);
415
411
    return 1;                                   // Error
416
412
  }
417
413
  return 0;
423
419
 
424
420
  SYNOPSIS
425
421
   login_connection()
426
 
   thd        Thread handler
 
422
   session        Thread handler
427
423
 
428
424
  NOTES
429
425
    Connection is not closed in case of errors
434
430
*/
435
431
 
436
432
 
437
 
bool login_connection(THD *thd)
 
433
bool login_connection(Session *session)
438
434
{
439
 
  NET *net= &thd->net;
 
435
  NET *net= &session->net;
440
436
  int error;
441
437
 
442
438
  /* Use "connect_timeout" value during connection phase */
443
439
  my_net_set_read_timeout(net, connect_timeout);
444
440
  my_net_set_write_timeout(net, connect_timeout);
445
441
  
446
 
  lex_start(thd);
 
442
  lex_start(session);
447
443
 
448
 
  error= check_connection(thd);
449
 
  net_end_statement(thd);
 
444
  error= check_connection(session);
 
445
  net_end_statement(session);
450
446
 
451
447
  if (error)
452
448
  {                                             // Wrong permissions
454
450
    return(1);
455
451
  }
456
452
  /* Connect completed, set read/write timeouts back to default */
457
 
  my_net_set_read_timeout(net, thd->variables.net_read_timeout);
458
 
  my_net_set_write_timeout(net, thd->variables.net_write_timeout);
 
453
  my_net_set_read_timeout(net, session->variables.net_read_timeout);
 
454
  my_net_set_write_timeout(net, session->variables.net_write_timeout);
459
455
  return(0);
460
456
}
461
457
 
467
463
    This mainly updates status variables
468
464
*/
469
465
 
470
 
void end_connection(THD *thd)
 
466
void end_connection(Session *session)
471
467
{
472
 
  NET *net= &thd->net;
473
 
  plugin_thdvar_cleanup(thd);
 
468
  NET *net= &session->net;
 
469
  plugin_sessionvar_cleanup(session);
474
470
 
475
 
  if (thd->killed || (net->error && net->vio != 0))
 
471
  if (session->killed || (net->error && net->vio != 0))
476
472
  {
477
473
    statistic_increment(aborted_threads,&LOCK_status);
478
474
  }
479
475
 
480
476
  if (net->error && net->vio != 0)
481
477
  {
482
 
    if (!thd->killed && thd->variables.log_warnings > 1)
 
478
    if (!session->killed && session->variables.log_warnings > 1)
483
479
    {
484
 
      Security_context *sctx= thd->security_ctx;
 
480
      Security_context *sctx= session->security_ctx;
485
481
 
486
482
      sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
487
 
                        thd->thread_id,(thd->db ? thd->db : "unconnected"),
 
483
                        session->thread_id,(session->db ? session->db : "unconnected"),
488
484
                        sctx->user ? sctx->user : "unauthenticated",
489
485
                        sctx->ip,
490
 
                        (thd->main_da.is_error() ? thd->main_da.message() :
 
486
                        (session->main_da.is_error() ? session->main_da.message() :
491
487
                         ER(ER_UNKNOWN_ERROR)));
492
488
    }
493
489
  }
495
491
 
496
492
 
497
493
/*
498
 
  Initialize THD to handle queries
 
494
  Initialize Session to handle queries
499
495
*/
500
496
 
501
 
void prepare_new_connection_state(THD* thd)
 
497
void prepare_new_connection_state(Session* session)
502
498
{
503
 
  Security_context *sctx= thd->security_ctx;
 
499
  Security_context *sctx= session->security_ctx;
504
500
 
505
 
  if (thd->variables.max_join_size == HA_POS_ERROR)
506
 
    thd->options |= OPTION_BIG_SELECTS;
507
 
  if (thd->client_capabilities & CLIENT_COMPRESS)
508
 
    thd->net.compress=1;                                // Use compression
 
501
  if (session->variables.max_join_size == HA_POS_ERROR)
 
502
    session->options |= OPTION_BIG_SELECTS;
 
503
  if (session->client_capabilities & CLIENT_COMPRESS)
 
504
    session->net.compress=1;                            // Use compression
509
505
 
510
506
  /*
511
 
    Much of this is duplicated in create_embedded_thd() for the
 
507
    Much of this is duplicated in create_embedded_session() for the
512
508
    embedded server library.
513
509
    TODO: refactor this to avoid code duplication there
514
510
  */
515
 
  thd->version= refresh_version;
516
 
  thd->set_proc_info(0);
517
 
  thd->command= COM_SLEEP;
518
 
  thd->set_time();
519
 
  thd->init_for_queries();
 
511
  session->version= refresh_version;
 
512
  session->set_proc_info(0);
 
513
  session->command= COM_SLEEP;
 
514
  session->set_time();
 
515
  session->init_for_queries();
520
516
 
521
517
  /* In the past this would only run of the user did not have SUPER_ACL */
522
518
  if (sys_init_connect.value_length)
523
519
  {
524
 
    execute_init_command(thd, &sys_init_connect, &LOCK_sys_init_connect);
525
 
    if (thd->is_error())
 
520
    execute_init_command(session, &sys_init_connect, &LOCK_sys_init_connect);
 
521
    if (session->is_error())
526
522
    {
527
 
      thd->killed= THD::KILL_CONNECTION;
 
523
      session->killed= Session::KILL_CONNECTION;
528
524
      sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
529
 
                        thd->thread_id,(thd->db ? thd->db : "unconnected"),
 
525
                        session->thread_id,(session->db ? session->db : "unconnected"),
530
526
                        sctx->user ? sctx->user : "unauthenticated",
531
527
                        sctx->ip, "init_connect command failed");
532
 
      sql_print_warning("%s", thd->main_da.message());
 
528
      sql_print_warning("%s", session->main_da.message());
533
529
    }
534
 
    thd->set_proc_info(0);
535
 
    thd->set_time();
536
 
    thd->init_for_queries();
 
530
    session->set_proc_info(0);
 
531
    session->set_time();
 
532
    session->init_for_queries();
537
533
  }
538
534
}
539
535
 
543
539
 
544
540
  SYNOPSIS
545
541
    handle_one_connection()
546
 
    arg         Connection object (THD)
 
542
    arg         Connection object (Session)
547
543
 
548
544
  IMPLEMENTATION
549
545
    This function (normally) does the following:
550
546
    - Initialize thread
551
 
    - Initialize THD to be used with this thread
 
547
    - Initialize Session to be used with this thread
552
548
    - Authenticate user
553
549
    - Execute all queries sent on the connection
554
550
    - Take connection down
557
553
 
558
554
pthread_handler_t handle_one_connection(void *arg)
559
555
{
560
 
  THD *thd= (THD*) arg;
561
 
  uint32_t launch_time= (uint32_t) ((thd->thr_create_utime= my_micro_time()) -
562
 
                              thd->connect_utime);
 
556
  Session *session= (Session*) arg;
 
557
  uint32_t launch_time= (uint32_t) ((session->thr_create_utime= my_micro_time()) -
 
558
                              session->connect_utime);
563
559
 
564
560
  if (thread_scheduler.init_new_connection_thread())
565
561
  {
566
 
    close_connection(thd, ER_OUT_OF_RESOURCES, 1);
 
562
    close_connection(session, ER_OUT_OF_RESOURCES, 1);
567
563
    statistic_increment(aborted_connects,&LOCK_status);
568
 
    thread_scheduler.end_thread(thd,0);
 
564
    thread_scheduler.end_thread(session,0);
569
565
    return 0;
570
566
  }
571
567
  if (launch_time >= slow_launch_time*1000000L)
575
571
    handle_one_connection() is normally the only way a thread would
576
572
    start and would always be on the very high end of the stack ,
577
573
    therefore, the thread stack always starts at the address of the
578
 
    first local variable of handle_one_connection, which is thd. We
 
574
    first local variable of handle_one_connection, which is session. We
579
575
    need to know the start of the stack so that we could check for
580
576
    stack overruns.
581
577
  */
582
 
  thd->thread_stack= (char*) &thd;
583
 
  if (setup_connection_thread_globals(thd))
 
578
  session->thread_stack= (char*) &session;
 
579
  if (setup_connection_thread_globals(session))
584
580
    return 0;
585
581
 
586
582
  for (;;)
587
583
  {
588
 
    NET *net= &thd->net;
 
584
    NET *net= &session->net;
589
585
 
590
 
    if (login_connection(thd))
 
586
    if (login_connection(session))
591
587
      goto end_thread;
592
588
 
593
 
    prepare_new_connection_state(thd);
 
589
    prepare_new_connection_state(session);
594
590
 
595
591
    while (!net->error && net->vio != 0 &&
596
 
           !(thd->killed == THD::KILL_CONNECTION))
 
592
           !(session->killed == Session::KILL_CONNECTION))
597
593
    {
598
 
      if (do_command(thd))
 
594
      if (do_command(session))
599
595
        break;
600
596
    }
601
 
    end_connection(thd);
 
597
    end_connection(session);
602
598
   
603
599
end_thread:
604
 
    close_connection(thd, 0, 1);
605
 
    if (thread_scheduler.end_thread(thd,1))
 
600
    close_connection(session, 0, 1);
 
601
    if (thread_scheduler.end_thread(session,1))
606
602
      return 0;                                 // Probably no-threads
607
603
 
608
604
    /*
610
606
      thread-handler=no-threads or this thread has been schedule to
611
607
      handle the next connection.
612
608
    */
613
 
    thd= current_thd;
614
 
    thd->thread_stack= (char*) &thd;
 
609
    session= current_session;
 
610
    session->thread_stack= (char*) &session;
615
611
  }
616
612
}