~drizzle-trunk/drizzle/development

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