~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_connect.cc

  • Committer: Stewart Smith
  • Date: 2009-01-12 05:43:13 UTC
  • mto: (784.1.4 for-brian)
  • mto: This revision was merged to the branch mainline in revision 785.
  • Revision ID: stewart@flamingspork.com-20090112054313-edk6kpf4l6kpz4j7
fix archive_basic for drizzle

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