~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_connect.cc

  • Committer: Brian Aker
  • Date: 2009-02-20 22:48:37 UTC
  • Revision ID: brian@tangent.org-20090220224837-fw5wrf46n4ru3e6a
First pass of stripping uint

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