~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_connect.cc

  • Committer: Brian Aker
  • Date: 2008-09-04 19:31:00 UTC
  • Revision ID: brian@tangent.org-20080904193100-l849hgghfy4urj43
Changing default character set from this point on.

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))) ||
182
 
      !my_strcasecmp(&my_charset_utf8_general_ci,
 
186
      !(thd->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
 
187
      !my_strcasecmp(&my_charset_latin1,
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
 
 
239
#ifdef SIGNAL_WITH_VIO_CLOSE
 
240
  thd->set_active_vio(net->vio);
 
241
#endif
 
242
 
234
243
  // TCP/IP connection
235
244
  {
236
245
    char ip[NI_MAXHOST];
237
246
 
238
 
    if (net_peer_addr(net, ip, &session->peer_port, NI_MAXHOST))
 
247
    if (vio_peer_addr(net->vio, ip, &thd->peer_port, NI_MAXHOST))
239
248
    {
240
 
      my_error(ER_BAD_HOST_ERROR, MYF(0), session->main_security_ctx.ip);
 
249
      my_error(ER_BAD_HOST_ERROR, MYF(0), thd->main_security_ctx.ip);
241
250
      return 1;
242
251
    }
243
 
    if (!(session->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
 
252
    if (!(thd->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
244
253
      return 1; /* The error is set by my_strdup(). */
245
254
  }
246
 
  net_keepalive(net, true);
 
255
  vio_keepalive(net->vio, true);
247
256
  
248
257
  uint32_t server_capabilites;
249
258
  {
257
266
    server_capabilites|= CLIENT_COMPRESS;
258
267
#endif /* HAVE_COMPRESS */
259
268
 
260
 
    end= my_stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
261
 
    int4store((unsigned char*) end, session->thread_id);
 
269
    end= stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
 
270
    int4store((uchar*) end, thd->thread_id);
262
271
    end+= 4;
263
272
    /*
264
273
      So as check_connection is the only entry point to authorization
265
274
      procedure, scramble is set here. This gives us new scramble for
266
275
      each handshake.
267
276
    */
268
 
    create_random_string(session->scramble, SCRAMBLE_LENGTH, &session->rand);
 
277
    create_random_string(thd->scramble, SCRAMBLE_LENGTH, &thd->rand);
269
278
    /*
270
279
      Old clients does not understand long scrambles, but can ignore packet
271
280
      tail: that's why first part of the scramble is placed here, and second
272
281
      part at the end of packet.
273
282
    */
274
 
    end= strmake(end, session->scramble, SCRAMBLE_LENGTH_323) + 1;
 
283
    end= strmake(end, thd->scramble, SCRAMBLE_LENGTH_323) + 1;
275
284
   
276
285
    int2store(end, server_capabilites);
277
286
    /* write server characteristics: up to 16 bytes allowed */
278
287
    end[2]=(char) default_charset_info->number;
279
 
    int2store(end+3, session->server_status);
 
288
    int2store(end+3, thd->server_status);
280
289
    memset(end+5, 0, 13);
281
290
    end+= 18;
282
291
    /* write scramble tail */
283
 
    end= strmake(end, session->scramble + SCRAMBLE_LENGTH_323, 
 
292
    end= strmake(end, thd->scramble + SCRAMBLE_LENGTH_323, 
284
293
                 SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323) + 1;
285
294
 
286
295
    /* 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)) ||
 
296
    if (net_write_command(net, (uchar) protocol_version, (uchar*) "", 0,
 
297
                          (uchar*) buff, (size_t) (end-buff)) ||
289
298
        (pkt_len= my_net_read(net)) == packet_error ||
290
299
        pkt_len < MIN_HANDSHAKE_SIZE)
291
300
    {
292
301
      my_error(ER_HANDSHAKE_ERROR, MYF(0),
293
 
               session->main_security_ctx.ip);
 
302
               thd->main_security_ctx.ip);
294
303
      return 1;
295
304
    }
296
305
  }
297
 
  if (session->packet.alloc(session->variables.net_buffer_length))
 
306
  if (thd->packet.alloc(thd->variables.net_buffer_length))
298
307
    return 1; /* The error is set by alloc(). */
299
308
 
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();
 
309
  thd->client_capabilities= uint2korr(net->read_pos);
 
310
 
 
311
 
 
312
  thd->client_capabilities|= ((uint32_t) uint2korr(net->read_pos+2)) << 16;
 
313
  thd->max_client_packet_length= uint4korr(net->read_pos+4);
 
314
  thd_init_client_charset(thd, (uint) net->read_pos[8]);
 
315
  thd->update_charset();
307
316
  end= (char*) net->read_pos+32;
308
317
 
309
318
  /*
310
319
    Disable those bits which are not supported by the server.
311
320
    This is a precautionary measure, if the client lies. See Bug#27944.
312
321
  */
313
 
  session->client_capabilities&= server_capabilites;
 
322
  thd->client_capabilities&= server_capabilites;
314
323
 
315
324
  if (end >= (char*) net->read_pos+ pkt_len +2)
316
325
  {
317
326
 
318
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
 
327
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
319
328
    return 1;
320
329
  }
321
330
 
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) &&
 
331
  if (thd->client_capabilities & CLIENT_INTERACTIVE)
 
332
    thd->variables.net_wait_timeout= thd->variables.net_interactive_timeout;
 
333
  if ((thd->client_capabilities & CLIENT_TRANSACTIONS) &&
325
334
      opt_using_transactions)
326
 
    net->return_status= &session->server_status;
 
335
    net->return_status= &thd->server_status;
327
336
 
328
337
  char *user= end;
329
338
  char *passwd= strchr(user, '\0')+1;
330
 
  uint32_t user_len= passwd - user - 1;
 
339
  uint user_len= passwd - user - 1;
331
340
  char *db= passwd;
332
341
  char db_buff[NAME_LEN + 1];           // buffer to store db in utf8
333
342
  char user_buff[USERNAME_LENGTH + 1];  // buffer to store user in utf8
334
 
  uint32_t dummy_errors;
 
343
  uint dummy_errors;
335
344
 
336
345
  /*
337
346
    Old clients send null-terminated string as password; new clients send
343
352
    Cast *passwd to an unsigned char, so that it doesn't extend the sign for
344
353
    *passwd > 127 and become 2**32-127+ after casting to uint.
345
354
  */
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 ?
 
355
  uint passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
 
356
    (uchar)(*passwd++) : strlen(passwd);
 
357
  db= thd->client_capabilities & CLIENT_CONNECT_WITH_DB ?
349
358
    db + passwd_len + 1 : 0;
350
359
  /* strlen() can't be easily deleted without changing protocol */
351
 
  uint32_t db_len= db ? strlen(db) : 0;
 
360
  uint db_len= db ? strlen(db) : 0;
352
361
 
353
362
  if (passwd + passwd_len + db_len > (char *)net->read_pos + pkt_len)
354
363
  {
355
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
 
364
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
356
365
    return 1;
357
366
  }
358
367
 
362
371
    db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
363
372
                             system_charset_info,
364
373
                             db, db_len,
365
 
                             session->charset(), &dummy_errors)]= 0;
 
374
                             thd->charset(), &dummy_errors)]= 0;
366
375
    db= db_buff;
367
376
  }
368
377
 
369
378
  user_buff[user_len= copy_and_convert(user_buff, sizeof(user_buff)-1,
370
379
                                       system_charset_info, user, user_len,
371
 
                                       session->charset(), &dummy_errors)]= '\0';
 
380
                                       thd->charset(), &dummy_errors)]= '\0';
372
381
  user= user_buff;
373
382
 
374
383
  /* If username starts and ends in "'", chop them off */
379
388
    user_len-= 2;
380
389
  }
381
390
 
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))))
 
391
  if (thd->main_security_ctx.user)
 
392
    x_free(thd->main_security_ctx.user);
 
393
  if (!(thd->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
386
394
    return 1; /* The error is set by my_strdup(). */
387
 
  return check_user(session, passwd, passwd_len, db, true);
 
395
  return check_user(thd, COM_CONNECT, passwd, passwd_len, db, true);
388
396
}
389
397
 
390
398
 
393
401
 
394
402
  SYNOPSIS
395
403
    bool setup_connection_thread_globals()
396
 
    session    Thread/connection handler
 
404
    thd    Thread/connection handler
397
405
 
398
406
  RETURN
399
407
    0   ok
401
409
        In this case we will close the connection and increment status
402
410
*/
403
411
 
404
 
bool setup_connection_thread_globals(Session *session)
 
412
bool setup_connection_thread_globals(THD *thd)
405
413
{
406
 
  if (session->store_globals())
 
414
  if (thd->store_globals())
407
415
  {
408
 
    close_connection(session, ER_OUT_OF_RESOURCES, 1);
 
416
    close_connection(thd, ER_OUT_OF_RESOURCES, 1);
409
417
    statistic_increment(aborted_connects,&LOCK_status);
410
 
    thread_scheduler.end_thread(session, 0);
 
418
    thread_scheduler.end_thread(thd, 0);
411
419
    return 1;                                   // Error
412
420
  }
413
421
  return 0;
419
427
 
420
428
  SYNOPSIS
421
429
   login_connection()
422
 
   session        Thread handler
 
430
   thd        Thread handler
423
431
 
424
432
  NOTES
425
433
    Connection is not closed in case of errors
430
438
*/
431
439
 
432
440
 
433
 
bool login_connection(Session *session)
 
441
bool login_connection(THD *thd)
434
442
{
435
 
  NET *net= &session->net;
 
443
  NET *net= &thd->net;
436
444
  int error;
437
445
 
438
446
  /* Use "connect_timeout" value during connection phase */
439
447
  my_net_set_read_timeout(net, connect_timeout);
440
448
  my_net_set_write_timeout(net, connect_timeout);
441
449
  
442
 
  lex_start(session);
 
450
  lex_start(thd);
443
451
 
444
 
  error= check_connection(session);
445
 
  net_end_statement(session);
 
452
  error= check_connection(thd);
 
453
  net_end_statement(thd);
446
454
 
447
455
  if (error)
448
456
  {                                             // Wrong permissions
450
458
    return(1);
451
459
  }
452
460
  /* 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);
 
461
  my_net_set_read_timeout(net, thd->variables.net_read_timeout);
 
462
  my_net_set_write_timeout(net, thd->variables.net_write_timeout);
455
463
  return(0);
456
464
}
457
465
 
463
471
    This mainly updates status variables
464
472
*/
465
473
 
466
 
void end_connection(Session *session)
 
474
void end_connection(THD *thd)
467
475
{
468
 
  NET *net= &session->net;
469
 
  plugin_sessionvar_cleanup(session);
 
476
  NET *net= &thd->net;
 
477
  plugin_thdvar_cleanup(thd);
470
478
 
471
 
  if (session->killed || (net->error && net->vio != 0))
 
479
  if (thd->killed || (net->error && net->vio != 0))
472
480
  {
473
481
    statistic_increment(aborted_threads,&LOCK_status);
474
482
  }
475
483
 
476
484
  if (net->error && net->vio != 0)
477
485
  {
478
 
    if (!session->killed && session->variables.log_warnings > 1)
 
486
    if (!thd->killed && thd->variables.log_warnings > 1)
479
487
    {
480
 
      Security_context *sctx= session->security_ctx;
 
488
      Security_context *sctx= thd->security_ctx;
481
489
 
482
490
      sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
483
 
                        session->thread_id,(session->db ? session->db : "unconnected"),
 
491
                        thd->thread_id,(thd->db ? thd->db : "unconnected"),
484
492
                        sctx->user ? sctx->user : "unauthenticated",
485
493
                        sctx->ip,
486
 
                        (session->main_da.is_error() ? session->main_da.message() :
 
494
                        (thd->main_da.is_error() ? thd->main_da.message() :
487
495
                         ER(ER_UNKNOWN_ERROR)));
488
496
    }
489
497
  }
491
499
 
492
500
 
493
501
/*
494
 
  Initialize Session to handle queries
 
502
  Initialize THD to handle queries
495
503
*/
496
504
 
497
 
void prepare_new_connection_state(Session* session)
 
505
void prepare_new_connection_state(THD* thd)
498
506
{
499
 
  Security_context *sctx= session->security_ctx;
 
507
  Security_context *sctx= thd->security_ctx;
500
508
 
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
  if (thd->variables.max_join_size == HA_POS_ERROR)
 
510
    thd->options |= OPTION_BIG_SELECTS;
 
511
  if (thd->client_capabilities & CLIENT_COMPRESS)
 
512
    thd->net.compress=1;                                // Use compression
505
513
 
506
514
  /*
507
 
    Much of this is duplicated in create_embedded_session() for the
 
515
    Much of this is duplicated in create_embedded_thd() for the
508
516
    embedded server library.
509
517
    TODO: refactor this to avoid code duplication there
510
518
  */
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();
 
519
  thd->version= refresh_version;
 
520
  thd->set_proc_info(0);
 
521
  thd->command= COM_SLEEP;
 
522
  thd->set_time();
 
523
  thd->init_for_queries();
516
524
 
517
525
  /* In the past this would only run of the user did not have SUPER_ACL */
518
526
  if (sys_init_connect.value_length)
519
527
  {
520
 
    execute_init_command(session, &sys_init_connect, &LOCK_sys_init_connect);
521
 
    if (session->is_error())
 
528
    execute_init_command(thd, &sys_init_connect, &LOCK_sys_init_connect);
 
529
    if (thd->is_error())
522
530
    {
523
 
      session->killed= Session::KILL_CONNECTION;
 
531
      thd->killed= THD::KILL_CONNECTION;
524
532
      sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
525
 
                        session->thread_id,(session->db ? session->db : "unconnected"),
 
533
                        thd->thread_id,(thd->db ? thd->db : "unconnected"),
526
534
                        sctx->user ? sctx->user : "unauthenticated",
527
535
                        sctx->ip, "init_connect command failed");
528
 
      sql_print_warning("%s", session->main_da.message());
 
536
      sql_print_warning("%s", thd->main_da.message());
529
537
    }
530
 
    session->set_proc_info(0);
531
 
    session->set_time();
532
 
    session->init_for_queries();
 
538
    thd->set_proc_info(0);
 
539
    thd->set_time();
 
540
    thd->init_for_queries();
533
541
  }
534
542
}
535
543
 
539
547
 
540
548
  SYNOPSIS
541
549
    handle_one_connection()
542
 
    arg         Connection object (Session)
 
550
    arg         Connection object (THD)
543
551
 
544
552
  IMPLEMENTATION
545
553
    This function (normally) does the following:
546
554
    - Initialize thread
547
 
    - Initialize Session to be used with this thread
 
555
    - Initialize THD to be used with this thread
548
556
    - Authenticate user
549
557
    - Execute all queries sent on the connection
550
558
    - Take connection down
553
561
 
554
562
pthread_handler_t handle_one_connection(void *arg)
555
563
{
556
 
  Session *session= (Session*) arg;
557
 
  uint32_t launch_time= (uint32_t) ((session->thr_create_utime= my_micro_time()) -
558
 
                              session->connect_utime);
 
564
  THD *thd= (THD*) arg;
 
565
  uint32_t launch_time= (uint32_t) ((thd->thr_create_utime= my_micro_time()) -
 
566
                              thd->connect_utime);
559
567
 
560
568
  if (thread_scheduler.init_new_connection_thread())
561
569
  {
562
 
    close_connection(session, ER_OUT_OF_RESOURCES, 1);
 
570
    close_connection(thd, ER_OUT_OF_RESOURCES, 1);
563
571
    statistic_increment(aborted_connects,&LOCK_status);
564
 
    thread_scheduler.end_thread(session,0);
 
572
    thread_scheduler.end_thread(thd,0);
565
573
    return 0;
566
574
  }
567
575
  if (launch_time >= slow_launch_time*1000000L)
571
579
    handle_one_connection() is normally the only way a thread would
572
580
    start and would always be on the very high end of the stack ,
573
581
    therefore, the thread stack always starts at the address of the
574
 
    first local variable of handle_one_connection, which is session. We
 
582
    first local variable of handle_one_connection, which is thd. We
575
583
    need to know the start of the stack so that we could check for
576
584
    stack overruns.
577
585
  */
578
 
  session->thread_stack= (char*) &session;
579
 
  if (setup_connection_thread_globals(session))
 
586
  thd->thread_stack= (char*) &thd;
 
587
  if (setup_connection_thread_globals(thd))
580
588
    return 0;
581
589
 
582
590
  for (;;)
583
591
  {
584
 
    NET *net= &session->net;
 
592
    NET *net= &thd->net;
585
593
 
586
 
    if (login_connection(session))
 
594
    if (login_connection(thd))
587
595
      goto end_thread;
588
596
 
589
 
    prepare_new_connection_state(session);
 
597
    prepare_new_connection_state(thd);
590
598
 
591
599
    while (!net->error && net->vio != 0 &&
592
 
           !(session->killed == Session::KILL_CONNECTION))
 
600
           !(thd->killed == THD::KILL_CONNECTION))
593
601
    {
594
 
      if (do_command(session))
 
602
      if (do_command(thd))
595
603
        break;
596
604
    }
597
 
    end_connection(session);
 
605
    end_connection(thd);
598
606
   
599
607
end_thread:
600
 
    close_connection(session, 0, 1);
601
 
    if (thread_scheduler.end_thread(session,1))
 
608
    close_connection(thd, 0, 1);
 
609
    if (thread_scheduler.end_thread(thd,1))
602
610
      return 0;                                 // Probably no-threads
603
611
 
604
612
    /*
606
614
      thread-handler=no-threads or this thread has been schedule to
607
615
      handle the next connection.
608
616
    */
609
 
    session= current_session;
610
 
    session->thread_stack= (char*) &session;
 
617
    thd= current_thd;
 
618
    thd->thread_stack= (char*) &thd;
611
619
  }
612
620
}