~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_connect.cc

  • Committer: Monty Taylor
  • Date: 2008-09-15 17:24:04 UTC
  • Revision ID: monty@inaugust.com-20080915172404-ygh6hiyu0q7qpa9x
Removed strndup calls.

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