sql_show.cc 42.8 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2

unknown's avatar
unknown committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

unknown's avatar
unknown committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

unknown's avatar
unknown committed
13 14 15 16 17 18 19 20
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* Function with list databases, tables or fields */

#include "mysql_priv.h"
21
#include "sql_select.h"                         // For select_describe
unknown's avatar
unknown committed
22
#include "sql_acl.h"
23
#include "repl_failsafe.h"
unknown's avatar
unknown committed
24
#include <my_dir.h>
unknown's avatar
unknown committed
25

unknown's avatar
unknown committed
26 27 28 29
#ifdef HAVE_BERKELEY_DB
#include "ha_berkeley.h"			// For berkeley_show_logs
#endif

unknown's avatar
unknown committed
30
/* extern "C" pthread_mutex_t THR_LOCK_keycache; */
unknown's avatar
unknown committed
31 32 33 34 35 36

static const char *grant_names[]={
  "select","insert","update","delete","create","drop","reload","shutdown",
  "process","file","grant","references","index","alter"};

static TYPELIB grant_types = { sizeof(grant_names)/sizeof(char **),
37 38
                               "grant_types",
                               grant_names};
unknown's avatar
unknown committed
39 40

static int mysql_find_files(THD *thd,List<char> *files, const char *db,
41
                            const char *path, const char *wild, bool dir);
unknown's avatar
unknown committed
42 43

static int
44 45 46 47
store_create_info(THD *thd, TABLE *table, String *packet);

static void
append_identifier(THD *thd, String *packet, const char *name);
unknown's avatar
unknown committed
48

unknown's avatar
unknown committed
49 50
extern struct st_VioSSLAcceptorFd * ssl_acceptor_fd;

unknown's avatar
unknown committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/****************************************************************************
** Send list of databases
** A database is a directory in the mysql_data_home directory
****************************************************************************/


int
mysqld_show_dbs(THD *thd,const char *wild)
{
  Item_string *field=new Item_string("",0);
  List<Item> field_list;
  char *end;
  List<char> files;
  char *file_name;
  DBUG_ENTER("mysqld_show_dbs");

unknown's avatar
unknown committed
67
  field->name=(char*) thd->alloc(20+ (wild ? (uint) strlen(wild)+4: 0));
unknown's avatar
unknown committed
68 69 70 71 72 73 74 75 76 77
  field->max_length=NAME_LEN;
  end=strmov(field->name,"Database");
  if (wild && wild[0])
    strxmov(end," (",wild,")",NullS);
  field_list.push_back(field);

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);
  if (mysql_find_files(thd,&files,NullS,mysql_data_home,wild,1))
    DBUG_RETURN(1);
unknown's avatar
unknown committed
78
  List_iterator_fast<char> it(files);
unknown's avatar
unknown committed
79 80
  while ((file_name=it++))
  {
81 82 83 84 85 86
    if (!opt_safe_show_db || thd->master_access ||
	acl_get(thd->host, thd->ip, (char*) &thd->remote.sin_addr,
		thd->priv_user, file_name) ||
	(grant_option && !check_grant_db(thd, file_name)))
      {
      thd->packet.length(0);
87
      net_store_data(&thd->packet, thd->convert_set, file_name);
88 89 90 91
      if (my_net_write(&thd->net, (char*) thd->packet.ptr(),
		       thd->packet.length()))
	DBUG_RETURN(-1);
    }
unknown's avatar
unknown committed
92 93 94 95 96
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}

97 98 99 100
/***************************************************************************
** List all open tables in a database
***************************************************************************/

101
int mysqld_show_open_tables(THD *thd,const char *wild)
102 103
{
  List<Item> field_list;
104 105
  OPEN_TABLE_LIST *open_list;
  CONVERT *convert=thd->convert_set;
106 107
  DBUG_ENTER("mysqld_show_open_tables");

108 109 110 111
  field_list.push_back(new Item_empty_string("Database",NAME_LEN));
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
  field_list.push_back(new Item_int("In_use",0, 4));
  field_list.push_back(new Item_int("Name_locked",0, 4));
112 113 114

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);
115

unknown's avatar
unknown committed
116
  if (!(open_list=list_open_tables(thd,wild)) && thd->fatal_error)
117 118
    DBUG_RETURN(-1);

119
  for ( ; open_list ; open_list=open_list->next)
120 121
  {
    thd->packet.length(0);
122 123 124 125
    net_store_data(&thd->packet,convert, open_list->db);
    net_store_data(&thd->packet,convert, open_list->table);
    net_store_data(&thd->packet,open_list->in_use);
    net_store_data(&thd->packet,open_list->locked);
126 127 128 129 130 131 132 133
    if (my_net_write(&thd->net,(char*) thd->packet.ptr(),thd->packet.length()))
      DBUG_RETURN(-1);
  }

  send_eof(&thd->net);
  DBUG_RETURN(0);
}

unknown's avatar
unknown committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147
/***************************************************************************
** List all tables in a database (fast version)
** A table is a .frm file in the current databasedir
***************************************************************************/

int mysqld_show_tables(THD *thd,const char *db,const char *wild)
{
  Item_string *field=new Item_string("",0);
  List<Item> field_list;
  char path[FN_LEN],*end;
  List<char> files;
  char *file_name;
  DBUG_ENTER("mysqld_show_tables");

unknown's avatar
unknown committed
148
  field->name=(char*) thd->alloc(20+(uint) strlen(db)+(wild ? (uint) strlen(wild)+4:0));
unknown's avatar
unknown committed
149 150 151 152 153 154 155 156 157 158 159
  end=strxmov(field->name,"Tables_in_",db,NullS);
  if (wild && wild[0])
    strxmov(end," (",wild,")",NullS);
  field->max_length=NAME_LEN;
  (void) sprintf(path,"%s/%s",mysql_data_home,db);
  (void) unpack_dirname(path,path);
  field_list.push_back(field);
  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);
  if (mysql_find_files(thd,&files,db,path,wild,0))
    DBUG_RETURN(-1);
unknown's avatar
unknown committed
160
  List_iterator_fast<char> it(files);
unknown's avatar
unknown committed
161 162 163
  while ((file_name=it++))
  {
    thd->packet.length(0);
164
    net_store_data(&thd->packet, thd->convert_set, file_name);
unknown's avatar
unknown committed
165 166 167 168 169 170 171 172 173 174
    if (my_net_write(&thd->net,(char*) thd->packet.ptr(),thd->packet.length()))
      DBUG_RETURN(-1);
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}


static int
mysql_find_files(THD *thd,List<char> *files, const char *db,const char *path,
175
                 const char *wild, bool dir)
unknown's avatar
unknown committed
176 177 178 179 180 181 182 183 184
{
  uint i;
  char *ext;
  MY_DIR *dirp;
  FILEINFO *file;
  uint col_access=thd->col_access;
  TABLE_LIST table_list;
  DBUG_ENTER("mysql_find_files");

185 186
  if (wild && !wild[0])
    wild=0;
unknown's avatar
unknown committed
187 188 189 190 191
  bzero((char*) &table_list,sizeof(table_list));

  if (!(dirp = my_dir(path,MYF(MY_WME | (dir ? MY_WANT_STAT : 0)))))
    DBUG_RETURN(-1);

192
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
unknown's avatar
unknown committed
193 194 195
  {
    file=dirp->dir_entry+i;
    if (dir)
196
    {                                           /* Return databases */
unknown's avatar
unknown committed
197 198 199
#ifdef USE_SYMDIR
      char *ext;
      if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
200
        *ext=0;                                 /* Remove extension */
unknown's avatar
unknown committed
201 202 203
      else
#endif
      {
204
        if (file->name[0] == '.' || !MY_S_ISDIR(file->mystat.st_mode) ||
205
            (wild && wild_compare(file->name,wild)))
206
          continue;
unknown's avatar
unknown committed
207 208 209 210
      }
    }
    else
    {
211
        // Return only .frm files which aren't temp files.
unknown's avatar
unknown committed
212
      if (my_strcasecmp(ext=fn_ext(file->name),reg_ext) ||
213
          is_prefix(file->name,tmp_file_prefix))
214
        continue;
unknown's avatar
unknown committed
215
      *ext=0;
216 217 218 219 220 221 222 223 224 225
      if (wild)
      {
	if (lower_case_table_names)
	{
	  if (wild_case_compare(file->name,wild))
	    continue;
	}
	else if (wild_compare(file->name,wild))
	  continue;
      }
unknown's avatar
unknown committed
226 227 228 229 230 231 232
    }
    /* Don't show tables where we don't have any privileges */
    if (db && !(col_access & TABLE_ACLS))
    {
      table_list.db= (char*) db;
      table_list.real_name=file->name;
      table_list.grant.privilege=col_access;
unknown's avatar
unknown committed
233
      if (check_grant(thd,TABLE_ACLS,&table_list,1,1))
234
        continue;
unknown's avatar
unknown committed
235
    }
236
    if (files->push_back(thd->strdup(file->name)))
unknown's avatar
unknown committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    {
      my_dirend(dirp);
      DBUG_RETURN(-1);
    }
  }
  DBUG_PRINT("info",("found: %d files", files->elements));
  my_dirend(dirp);
  DBUG_RETURN(0);
}

/***************************************************************************
** Extended version of mysqld_show_tables
***************************************************************************/

int mysqld_extend_show_tables(THD *thd,const char *db,const char *wild)
{
  Item *item;
  List<char> files;
  List<Item> field_list;
  char path[FN_LEN];
  char *file_name;
  TABLE *table;
  String *packet= &thd->packet;
260
  CONVERT *convert=thd->convert_set;
unknown's avatar
unknown committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  DBUG_ENTER("mysqld_extend_show_tables");

  (void) sprintf(path,"%s/%s",mysql_data_home,db);
  (void) unpack_dirname(path,path);

  field_list.push_back(item=new Item_empty_string("Name",NAME_LEN));
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Type",10));
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Row_format",10));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Rows",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Avg_row_length",(int32) 0,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Data_length",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Max_data_length",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Index_length",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Data_free",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Auto_increment",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_datetime("Create_time"));
  item->maybe_null=1;
  field_list.push_back(item=new Item_datetime("Update_time"));
  item->maybe_null=1;
  field_list.push_back(item=new Item_datetime("Check_time"));
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Create_options",255));
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Comment",80));
  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);

  if (mysql_find_files(thd,&files,db,path,wild,0))
    DBUG_RETURN(-1);
unknown's avatar
unknown committed
300
  List_iterator_fast<char> it(files);
unknown's avatar
unknown committed
301 302 303 304 305
  while ((file_name=it++))
  {
    TABLE_LIST table_list;
    bzero((char*) &table_list,sizeof(table_list));
    packet->length(0);
306
    net_store_data(packet,convert, file_name);
unknown's avatar
unknown committed
307 308 309 310 311
    table_list.db=(char*) db;
    table_list.real_name=table_list.name=file_name;
    if (!(table = open_ltable(thd, &table_list, TL_READ)))
    {
      for (uint i=0 ; i < field_list.elements ; i++)
312
        net_store_null(packet);
313
      net_store_data(packet,convert, thd->net.last_error);
unknown's avatar
unknown committed
314 315 316 317 318 319 320
      thd->net.last_error[0]=0;
    }
    else
    {
      struct tm tm_tmp;
      handler *file=table->file;
      file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_NO_LOCK);
321 322
      net_store_data(packet, convert, file->table_type());
      net_store_data(packet, convert,
323 324 325 326
                     (table->db_options_in_use & HA_OPTION_PACK_RECORD) ?
                     "Dynamic" :
                     (table->db_options_in_use & HA_OPTION_COMPRESS_RECORD)
                     ? "Compressed" : "Fixed");
unknown's avatar
unknown committed
327 328 329 330
      net_store_data(packet, (longlong) file->records);
      net_store_data(packet, (uint32) file->mean_rec_length);
      net_store_data(packet, (longlong) file->data_file_length);
      if (file->max_data_file_length)
331
        net_store_data(packet, (longlong) file->max_data_file_length);
unknown's avatar
unknown committed
332
      else
333
        net_store_null(packet);
unknown's avatar
unknown committed
334 335 336 337
      net_store_data(packet, (longlong) file->index_file_length);
      net_store_data(packet, (longlong) file->delete_length);
      if (table->found_next_number_field)
      {
338 339 340 341 342
        table->next_number_field=table->found_next_number_field;
        table->next_number_field->reset();
        file->update_auto_increment();
        net_store_data(packet, table->next_number_field->val_int());
        table->next_number_field=0;
unknown's avatar
unknown committed
343 344
      }
      else
345
        net_store_null(packet);
unknown's avatar
unknown committed
346
      if (!file->create_time)
347
        net_store_null(packet);
unknown's avatar
unknown committed
348 349
      else
      {
350 351
        localtime_r(&file->create_time,&tm_tmp);
        net_store_data(packet, &tm_tmp);
unknown's avatar
unknown committed
352 353
      }
      if (!file->update_time)
354
        net_store_null(packet);
unknown's avatar
unknown committed
355 356
      else
      {
357 358
        localtime_r(&file->update_time,&tm_tmp);
        net_store_data(packet, &tm_tmp);
unknown's avatar
unknown committed
359 360
      }
      if (!file->check_time)
361
        net_store_null(packet);
unknown's avatar
unknown committed
362 363
      else
      {
364 365
        localtime_r(&file->check_time,&tm_tmp);
        net_store_data(packet, &tm_tmp);
unknown's avatar
unknown committed
366 367
      }
      {
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        char option_buff[350],*ptr;
        ptr=option_buff;
        if (table->min_rows)
        {
          ptr=strmov(ptr," min_rows=");
          ptr=longlong10_to_str(table->min_rows,ptr,10);
        }
        if (table->max_rows)
        {
          ptr=strmov(ptr," max_rows=");
          ptr=longlong10_to_str(table->max_rows,ptr,10);
        }
        if (table->avg_row_length)
        {
          ptr=strmov(ptr," avg_row_length=");
          ptr=longlong10_to_str(table->avg_row_length,ptr,10);
        }
        if (table->db_create_options & HA_OPTION_PACK_KEYS)
          ptr=strmov(ptr," pack_keys=1");
        if (table->db_create_options & HA_OPTION_NO_PACK_KEYS)
          ptr=strmov(ptr," pack_keys=0");
        if (table->db_create_options & HA_OPTION_CHECKSUM)
          ptr=strmov(ptr," checksum=1");
        if (table->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
          ptr=strmov(ptr," delay_key_write=1");
        if (table->row_type != ROW_TYPE_DEFAULT)
394
          ptr=strxmov(ptr, " row_format=", ha_row_type[(uint) table->row_type],
395 396 397 398 399 400 401 402
                      NullS);
        if (file->raid_type)
        {
          char buff[100];
          sprintf(buff," raid_type=%s raid_chunks=%d raid_chunksize=%ld",
                  my_raid_type(file->raid_type), file->raid_chunks, file->raid_chunksize/RAID_BLOCK_SIZE);
          ptr=strmov(ptr,buff);
        }
403
        net_store_data(packet, convert, option_buff+1,
404
                       (ptr == option_buff ? 0 : (uint) (ptr-option_buff)-1));
unknown's avatar
unknown committed
405
      }
406 407 408 409 410 411
      {
	char *comment=table->file->update_table_comment(table->comment);
	net_store_data(packet, comment);
	if (comment != table->comment)
	  my_free(comment,MYF(0));
      }
unknown's avatar
unknown committed
412 413 414
      close_thread_tables(thd,0);
    }
    if (my_net_write(&thd->net,(char*) packet->ptr(),
415
                     packet->length()))
unknown's avatar
unknown committed
416 417 418 419 420 421 422 423 424 425 426 427 428
      DBUG_RETURN(-1);
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}



/***************************************************************************
** List all columns in a table
***************************************************************************/

int
unknown's avatar
unknown committed
429 430
mysqld_show_fields(THD *thd, TABLE_LIST *table_list,const char *wild,
		   bool verbose)
unknown's avatar
unknown committed
431 432 433 434
{
  TABLE *table;
  handler *file;
  char tmp[MAX_FIELD_WIDTH];
435
  CONVERT *convert=thd->convert_set;
unknown's avatar
unknown committed
436 437
  DBUG_ENTER("mysqld_show_fields");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
438
                      table_list->real_name));
unknown's avatar
unknown committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
    send_error(&thd->net);
    DBUG_RETURN(1);
  }
  file=table->file;
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
  (void) get_table_grant(thd, table_list);

  List<Item> field_list;
  field_list.push_back(new Item_empty_string("Field",NAME_LEN));
  field_list.push_back(new Item_empty_string("Type",40));
  field_list.push_back(new Item_empty_string("Null",1));
  field_list.push_back(new Item_empty_string("Key",3));
  field_list.push_back(new Item_empty_string("Default",NAME_LEN));
  field_list.push_back(new Item_empty_string("Extra",20));
unknown's avatar
unknown committed
456 457
  if (verbose)
    field_list.push_back(new Item_empty_string("Privileges",80));
unknown's avatar
unknown committed
458

459
        // Send first number of fields and records
unknown's avatar
unknown committed
460 461 462 463 464 465 466 467 468
  {
    char *pos;
    pos=net_store_length(tmp, (uint) field_list.elements);
    pos=net_store_length(pos,(ulonglong) file->records);
    (void) my_net_write(&thd->net,tmp,(uint) (pos-tmp));
  }

  if (send_fields(thd,field_list,0))
    DBUG_RETURN(1);
469
  restore_record(table,2);      // Get empty record
unknown's avatar
unknown committed
470 471

  Field **ptr,*field;
unknown's avatar
unknown committed
472
  String *packet= &thd->packet;
unknown's avatar
unknown committed
473 474 475 476 477 478
  for (ptr=table->field; (field= *ptr) ; ptr++)
  {
    if (!wild || !wild[0] || !wild_case_compare(field->field_name,wild))
    {
#ifdef NOT_USED
      if (thd->col_access & TABLE_ACLS ||
479 480
          ! check_grant_column(thd,table,field->field_name,
                               (uint) strlen(field->field_name),1))
unknown's avatar
unknown committed
481 482
#endif
      {
483 484
        byte *pos;
        uint flags=field->flags;
unknown's avatar
unknown committed
485
        String type(tmp,sizeof(tmp));
486 487 488 489
        uint col_access;
        bool null_default_value=0;

        packet->length(0);
490
        net_store_data(packet,convert,field->field_name);
491
        field->sql_type(type);
492
        net_store_data(packet,convert,type.ptr(),type.length());
493 494 495 496

        pos=(byte*) ((flags & NOT_NULL_FLAG) &&
                     field->type() != FIELD_TYPE_TIMESTAMP ?
                     "" : "YES");
497
        net_store_data(packet,convert,(const char*) pos);
498 499 500
        pos=(byte*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
                     (field->flags & UNIQUE_KEY_FLAG) ? "UNI" :
                     (field->flags & MULTIPLE_KEY_FLAG) ? "MUL":"");
501
        net_store_data(packet,convert,(char*) pos);
502 503 504 505 506 507 508 509

        if (field->type() == FIELD_TYPE_TIMESTAMP ||
            field->unireg_check == Field::NEXT_NUMBER)
          null_default_value=1;
        if (!null_default_value && !field->is_null())
        {                                               // Not null by default
          type.set(tmp,sizeof(tmp));
          field->val_str(&type,&type);
510
          net_store_data(packet,convert,type.ptr(),type.length());
511 512 513 514
        }
        else if (field->maybe_null() || null_default_value)
          net_store_null(packet);                       // Null as default
        else
515
          net_store_data(packet,convert,tmp,0);
516 517 518 519

        char *end=tmp;
        if (field->unireg_check == Field::NEXT_NUMBER)
          end=strmov(tmp,"auto_increment");
520
        net_store_data(packet,convert,tmp,(uint) (end-tmp));
521

unknown's avatar
unknown committed
522 523 524 525 526 527 528 529 530 531 532 533 534
	if (verbose)
	{
	  /* Add grant options */
	  col_access= get_column_grant(thd,table_list,field) & COL_ACLS;
	  end=tmp;
	  for (uint bitnr=0; col_access ; col_access>>=1,bitnr++)
	  {
	    if (col_access & 1)
	    {
	      *end++=',';
	      end=strmov(end,grant_types.type_names[bitnr]);
	    }
	  }
535
	  net_store_data(packet,convert, tmp+1,end == tmp ? 0 : (uint) (end-tmp-1));
unknown's avatar
unknown committed
536
	}
537 538
        if (my_net_write(&thd->net,(char*) packet->ptr(),packet->length()))
          DBUG_RETURN(1);
unknown's avatar
unknown committed
539 540 541 542 543 544 545 546 547 548 549
      }
    }
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}

int
mysqld_show_create(THD *thd, TABLE_LIST *table_list)
{
  TABLE *table;
550
  CONVERT *convert=thd->convert_set;
unknown's avatar
unknown committed
551 552
  DBUG_ENTER("mysqld_show_create");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
553
                      table_list->real_name));
unknown's avatar
unknown committed
554

unknown's avatar
unknown committed
555
  /* Only one table for now */
unknown's avatar
unknown committed
556 557 558 559 560 561 562 563 564 565 566 567
  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
    send_error(&thd->net);
    DBUG_RETURN(1);
  }

  List<Item> field_list;
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
  field_list.push_back(new Item_empty_string("Create Table",1024));

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);
568

unknown's avatar
unknown committed
569
  String *packet = &thd->packet;
unknown's avatar
unknown committed
570 571
  {
    packet->length(0);
572
    net_store_data(packet,convert, table->table_name);
unknown's avatar
unknown committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    // a hack - we need to reserve some space for the length before
    // we know what it is - let's assume that the length of create table
    // statement will fit into 3 bytes ( 16 MB max :-) )
    ulong store_len_offset = packet->length();
    packet->length(store_len_offset + 4);
    if (store_create_info(thd, table, packet))
      DBUG_RETURN(-1);
    ulong create_len = packet->length() - store_len_offset - 4;
    if (create_len > 0x00ffffff) // better readable in HEX ...
      DBUG_RETURN(1);  // just in case somebody manages to create a table
    // with *that* much stuff in the definition

    // now we have to store the length in three bytes, even if it would fit
    // into fewer, so we cannot use net_store_data() anymore,
    // and do it ourselves
    char* p = (char*)packet->ptr() + store_len_offset;
    *p++ = (char) 253; // The client the length is stored using 3-bytes
    int3store(p, create_len);

    // now we are in business :-)
    if (my_net_write(&thd->net, (char*)packet->ptr(), packet->length()))
      DBUG_RETURN(1);
  }
unknown's avatar
unknown committed
596 597 598 599 600
  send_eof(&thd->net);
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
601 602 603 604 605 606 607 608 609 610 611 612 613
int
mysqld_show_logs(THD *thd)
{
  DBUG_ENTER("mysqld_show_logs");

  List<Item> field_list;
  field_list.push_back(new Item_empty_string("File",FN_REFLEN));
  field_list.push_back(new Item_empty_string("Type",10));
  field_list.push_back(new Item_empty_string("Status",10));

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);

614
#ifdef HAVE_BERKELEY_DB
615
  if (!berkeley_skip && berkeley_show_logs(thd))
unknown's avatar
unknown committed
616
    DBUG_RETURN(-1);
617
#endif
unknown's avatar
unknown committed
618 619 620 621 622 623

  send_eof(&thd->net);
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
624 625 626 627 628
int
mysqld_show_keys(THD *thd, TABLE_LIST *table_list)
{
  TABLE *table;
  char buff[256];
629
  CONVERT *convert=thd->convert_set;
unknown's avatar
unknown committed
630 631
  DBUG_ENTER("mysqld_show_keys");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
632
                      table_list->real_name));
unknown's avatar
unknown committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
    send_error(&thd->net);
    DBUG_RETURN(1);
  }

  List<Item> field_list;
  Item *item;
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
  field_list.push_back(new Item_int("Non_unique",0,1));
  field_list.push_back(new Item_empty_string("Key_name",NAME_LEN));
  field_list.push_back(new Item_int("Seq_in_index",0,2));
  field_list.push_back(new Item_empty_string("Column_name",NAME_LEN));
  field_list.push_back(item=new Item_empty_string("Collation",1));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Cardinality",0,11));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Sub_part",0,3));
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Packed",10));
  item->maybe_null=1;
  field_list.push_back(new Item_empty_string("Comment",255));
  item->maybe_null=1;

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);

unknown's avatar
unknown committed
661
  String *packet= &thd->packet;
unknown's avatar
unknown committed
662 663 664 665 666 667 668 669 670
  KEY *key_info=table->key_info;
  table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK | HA_STATUS_TIME);
  for (uint i=0 ; i < table->keys ; i++,key_info++)
  {
    KEY_PART_INFO *key_part= key_info->key_part;
    char *end;
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
      packet->length(0);
671 672 673
      net_store_data(packet,convert,table->table_name);
      net_store_data(packet,convert,((key_info->flags & HA_NOSAME) ? "0" :"1"), 1);
      net_store_data(packet,convert,key_info->name);
unknown's avatar
unknown committed
674
      end=int10_to_str((long) (j+1),(char*) buff,10);
675 676 677
      net_store_data(packet,convert,buff,(uint) (end-buff));
      net_store_data(packet,convert,
		     key_part->field ? key_part->field->field_name :
678
                     "?unknown field?");
unknown's avatar
unknown committed
679
      if (table->file->option_flag() & HA_READ_ORDER)
680 681 682
        net_store_data(packet,convert,
		       ((key_part->key_part_flag & HA_REVERSE_SORT) ?
			"D" : "A"), 1);
unknown's avatar
unknown committed
683
      else
684
        net_store_null(packet); /* purecov: inspected */
unknown's avatar
unknown committed
685 686 687
      KEY *key=table->key_info+i;
      if (key->rec_per_key[j])
      {
688 689
        ulong records=(table->file->records / key->rec_per_key[j]);
        end=int10_to_str((long) records, buff, 10);
690
        net_store_data(packet,convert,buff,(uint) (end-buff));
unknown's avatar
unknown committed
691 692
      }
      else
693
        net_store_null(packet);
unknown's avatar
unknown committed
694
      if (!key_part->field ||
695 696
          key_part->length !=
          table->field[key_part->fieldnr-1]->key_length())
unknown's avatar
unknown committed
697
      {
698
        end=int10_to_str((long) key_part->length, buff,10); /* purecov: inspected */
699
        net_store_data(packet,convert,buff,(uint) (end-buff)); /* purecov: inspected */
unknown's avatar
unknown committed
700 701
      }
      else
702 703
        net_store_null(packet);
      net_store_null(packet);                   // No pack_information yet
704 705
      net_store_data(packet,convert,
		     key_info->flags & HA_FULLTEXT ? "FULLTEXT":"");
unknown's avatar
unknown committed
706
      if (my_net_write(&thd->net,(char*) packet->ptr(),packet->length()))
707
        DBUG_RETURN(1); /* purecov: inspected */
unknown's avatar
unknown committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    }
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}


/****************************************************************************
** Return only fields for API mysql_list_fields
** Use "show table wildcard" in mysql instead of this
****************************************************************************/

void
mysqld_list_fields(THD *thd, TABLE_LIST *table_list, const char *wild)
{
  TABLE *table;
  DBUG_ENTER("mysqld_list_fields");
  DBUG_PRINT("enter",("table: %s",table_list->real_name));

  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
    send_error(&thd->net);
    DBUG_VOID_RETURN;
  }
  List<Item> field_list;

  Field **ptr,*field;
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
    if (!wild || !wild[0] || !wild_case_compare(field->field_name,wild))
      field_list.push_back(new Item_field(field));
  }
740
  restore_record(table,2);              // Get empty record
unknown's avatar
unknown committed
741 742 743 744 745 746 747 748 749
  if (send_fields(thd,field_list,2))
    DBUG_VOID_RETURN;
  VOID(net_flush(&thd->net));
  DBUG_VOID_RETURN;
}

int
mysqld_dump_create_info(THD *thd, TABLE *table, int fd)
{
750
  CONVERT *convert=thd->convert_set;
unknown's avatar
unknown committed
751 752
  DBUG_ENTER("mysqld_dump_create_info");
  DBUG_PRINT("enter",("table: %s",table->real_name));
753

unknown's avatar
unknown committed
754 755
  String* packet = &thd->packet;
  packet->length(0);
756
  if (store_create_info(thd,table,packet))
unknown's avatar
unknown committed
757
    DBUG_RETURN(-1);
758

759 760 761
  if (convert)
    convert->convert((char*) packet->ptr(), packet->length());
  if (fd < 0)
762
  {
763
    if (my_net_write(&thd->net, (char*)packet->ptr(), packet->length()))
764 765 766
      DBUG_RETURN(-1);
    VOID(net_flush(&thd->net));
  }
unknown's avatar
unknown committed
767
  else
768
  {
769 770
    if (my_write(fd, (const byte*) packet->ptr(), packet->length(),
		 MYF(MY_WME)))
771 772
      DBUG_RETURN(-1);
  }
unknown's avatar
unknown committed
773 774
  DBUG_RETURN(0);
}
775

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
static void
append_identifier(THD *thd, String *packet, const char *name)
{
  if (thd->options & OPTION_QUOTE_SHOW_CREATE)
  {
    packet->append("`", 1);
    packet->append(name);
    packet->append("`", 1);
  }
  else
  {
    packet->append(name);
  }
}

unknown's avatar
unknown committed
791
static int
792
store_create_info(THD *thd, TABLE *table, String *packet)
unknown's avatar
unknown committed
793 794 795 796 797
{
  DBUG_ENTER("store_create_info");
  DBUG_PRINT("enter",("table: %s",table->real_name));

  restore_record(table,2); // Get empty record
798

unknown's avatar
unknown committed
799 800 801
  List<Item> field_list;
  char tmp[MAX_FIELD_WIDTH];
  String type(tmp, sizeof(tmp));
802 803 804 805
  if (table->tmp_table)
    packet->append("CREATE TEMPORARY TABLE ", 23);
  else
    packet->append("CREATE TABLE ", 13);
806
  append_identifier(thd,packet,table->real_name);
807
  packet->append(" (\n", 3);
808

unknown's avatar
unknown committed
809 810 811
  Field **ptr,*field;
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
812
    if (ptr != table->field)
813
      packet->append(",\n", 2);
814

unknown's avatar
unknown committed
815
    uint flags = field->flags;
816
    packet->append("  ", 2);
817
    append_identifier(thd,packet,field->field_name);
unknown's avatar
unknown committed
818 819
    packet->append(' ');
    // check for surprises from the previous call to Field::sql_type()
820
    if (type.ptr() != tmp)
unknown's avatar
unknown committed
821
      type.set(tmp, sizeof(tmp));
822

unknown's avatar
unknown committed
823 824
    field->sql_type(type);
    packet->append(type.ptr(),type.length());
825

826
    bool has_default = (field->type() != FIELD_TYPE_BLOB &&
827 828 829 830
			field->type() != FIELD_TYPE_TIMESTAMP &&
			field->unireg_check != Field::NEXT_NUMBER);
    if (flags & NOT_NULL_FLAG)
      packet->append(" NOT NULL", 9);
831

832
    if (has_default)
833 834
    {
      packet->append(" default ", 9);
835
      if (!field->is_null())
836 837 838 839
      {                                             // Not null by default
        type.set(tmp,sizeof(tmp));
        field->val_str(&type,&type);
        packet->append('\'');
840
	if (type.length())
unknown's avatar
unknown committed
841
          append_unescaped(packet, type.c_ptr());
842
        packet->append('\'');
unknown's avatar
unknown committed
843
      }
844
      else if (field->maybe_null())
845 846 847 848
        packet->append("NULL", 4);                    // Null as default
      else
        packet->append(tmp,0);
    }
849

unknown's avatar
unknown committed
850
    if (field->unireg_check == Field::NEXT_NUMBER)
851
          packet->append(" auto_increment", 15 );
unknown's avatar
unknown committed
852 853 854 855 856
  }

  KEY *key_info=table->key_info;
  table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK | HA_STATUS_TIME);
  uint primary_key = table->primary_key;
857

unknown's avatar
unknown committed
858 859
  for (uint i=0 ; i < table->keys ; i++,key_info++)
  {
860 861
    KEY_PART_INFO *key_part= key_info->key_part;
    bool found_primary=0;
862
    packet->append(",\n  ", 4);
863

864 865 866
    if (i == primary_key && !strcmp(key_info->name,"PRIMARY"))
    {
      found_primary=1;
867
      packet->append("PRIMARY ", 8);
868
    }
869
    else if (key_info->flags & HA_NOSAME)
870
      packet->append("UNIQUE ", 7);
871
    else if (key_info->flags & HA_FULLTEXT)
872 873
      packet->append("FULLTEXT ", 9);
    packet->append("KEY ", 4);
unknown's avatar
unknown committed
874

875
    if (!found_primary)
876
     append_identifier(thd,packet,key_info->name);
877

878
    packet->append(" (", 2);
879

unknown's avatar
unknown committed
880 881
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
882
      if (j)
883
        packet->append(',');
884

885
      if (key_part->field)
886
        append_identifier(thd,packet,key_part->field->field_name);
unknown's avatar
unknown committed
887
      if (!key_part->field ||
888 889 890
          (key_part->length !=
           table->field[key_part->fieldnr-1]->key_length() &&
           !(key_info->flags & HA_FULLTEXT)))
unknown's avatar
unknown committed
891
      {
892 893 894 895 896
        char buff[64];
        buff[0] = '(';
        char* end=int10_to_str((long) key_part->length, buff + 1,10);
        *end++ = ')';
        packet->append(buff,(uint) (end-buff));
unknown's avatar
unknown committed
897 898 899 900
      }
    }
    packet->append(')');
  }
901
  packet->append("\n)", 2);
902

unknown's avatar
unknown committed
903
  handler *file = table->file;
904
  packet->append(" TYPE=", 6);
unknown's avatar
unknown committed
905 906 907
  packet->append(file->table_type());
  char buff[128];
  char* p;
908

909
  if (table->min_rows)
910 911 912 913 914
  {
    packet->append(" MIN_ROWS=");
    p = longlong10_to_str(table->min_rows, buff, 10);
    packet->append(buff, (uint) (p - buff));
  }
unknown's avatar
unknown committed
915

916
  if (table->max_rows)
917 918 919 920 921
  {
    packet->append(" MAX_ROWS=");
    p = longlong10_to_str(table->max_rows, buff, 10);
    packet->append(buff, (uint) (p - buff));
  }
922 923 924 925 926 927
  if (table->avg_row_length)
  {
    packet->append(" AVG_ROW_LENGTH=");
    p=longlong10_to_str(table->avg_row_length, buff,10);
    packet->append(buff, (uint) (p - buff));
  }
928

unknown's avatar
unknown committed
929
  if (table->db_create_options & HA_OPTION_PACK_KEYS)
930
    packet->append(" PACK_KEYS=1", 12);
unknown's avatar
unknown committed
931
  if (table->db_create_options & HA_OPTION_NO_PACK_KEYS)
932
    packet->append(" PACK_KEYS=0", 12);
unknown's avatar
unknown committed
933
  if (table->db_create_options & HA_OPTION_CHECKSUM)
934
    packet->append(" CHECKSUM=1", 11);
unknown's avatar
unknown committed
935
  if (table->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
936
    packet->append(" DELAY_KEY_WRITE=1",18);
937 938 939 940 941
  if (table->row_type != ROW_TYPE_DEFAULT)
  {
    packet->append(" ROW_FORMAT=",12);
    packet->append(ha_row_type[(uint) table->row_type]);
  }
942
  table->file->append_create_info(packet);
unknown's avatar
merge  
unknown committed
943
  if (table->comment && table->comment[0])
unknown's avatar
unknown committed
944 945 946 947 948
  {
    packet->append(" COMMENT='", 10);
    append_unescaped(packet, table->comment);
    packet->append('\'');
  }
949 950 951 952 953 954 955
  if (file->raid_type)
  {
    char buff[100];
    sprintf(buff," RAID_TYPE=%s RAID_CHUNKS=%d RAID_CHUNKSIZE=%ld",
            my_raid_type(file->raid_type), file->raid_chunks, file->raid_chunksize/RAID_BLOCK_SIZE);
    packet->append(buff);
  }
unknown's avatar
unknown committed
956 957 958 959 960 961 962 963 964 965 966
  DBUG_RETURN(0);
}


/****************************************************************************
** Return info about all processes
** returns for each thread: thread id, user, host, db, command, info
****************************************************************************/

class thread_info :public ilink {
public:
unknown's avatar
unknown committed
967
  static void *operator new(size_t size) {return (void*) sql_alloc((uint) size); }
unknown's avatar
unknown committed
968
  static void operator delete(void *ptr __attribute__((unused)),
969
                              size_t size __attribute__((unused))) {} /*lint -e715 */
unknown's avatar
unknown committed
970

unknown's avatar
unknown committed
971 972
  ulong thread_id;
  time_t start_time;
973
  uint   command;
unknown's avatar
unknown committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
  const char *user,*host,*db,*proc_info,*state_info;
  char *query;
};

#ifdef __GNUC__
template class I_List<thread_info>;
#endif


void mysqld_list_processes(THD *thd,const char *user, bool verbose)
{
  Item *field;
  List<Item> field_list;
  I_List<thread_info> thread_infos;
  ulong max_query_length= verbose ? max_allowed_packet : PROCESS_LIST_WIDTH;
989
  CONVERT *convert=thd->convert_set;
unknown's avatar
unknown committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  DBUG_ENTER("mysqld_list_processes");

  field_list.push_back(new Item_int("Id",0,7));
  field_list.push_back(new Item_empty_string("User",16));
  field_list.push_back(new Item_empty_string("Host",64));
  field_list.push_back(field=new Item_empty_string("db",NAME_LEN));
  field->maybe_null=1;
  field_list.push_back(new Item_empty_string("Command",16));
  field_list.push_back(new Item_empty_string("Time",7));
  field_list.push_back(field=new Item_empty_string("State",30));
  field->maybe_null=1;
  field_list.push_back(field=new Item_empty_string("Info",max_query_length));
  field->maybe_null=1;
  if (send_fields(thd,field_list,1))
    DBUG_VOID_RETURN;

  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
  if (!thd->killed)
  {
    I_List_iterator<THD> it(threads);
    THD *tmp;
    while ((tmp=it++))
    {
      if ((tmp->net.vio || tmp->system_thread) &&
1014
          (!user || (tmp->user && !strcmp(tmp->user,user))))
unknown's avatar
unknown committed
1015
      {
1016 1017 1018
        thread_info *thd_info=new thread_info;

        thd_info->thread_id=tmp->thread_id;
1019 1020 1021 1022 1023 1024 1025
        thd_info->user=thd->strdup(tmp->user ? tmp->user :
				   (tmp->system_thread ?
				    "system user" : "unauthenticated user"));
        thd_info->host=thd->strdup(tmp->host ? tmp->host :
				   (tmp->ip ? tmp->ip :
				    (tmp->system_thread ? "none" :
				     "connecting host")));
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
        if ((thd_info->db=tmp->db))             // Safe test
          thd_info->db=thd->strdup(thd_info->db);
        thd_info->command=(int) tmp->command;
        if (tmp->mysys_var)
          pthread_mutex_lock(&tmp->mysys_var->mutex);
        thd_info->proc_info= (char*) (tmp->killed ? "Killed" : 0);
        thd_info->state_info= (char*) (tmp->locked ? "Locked" :
                                       tmp->net.reading_or_writing ?
                                       (tmp->net.reading_or_writing == 2 ?
                                        "Writing to net" :
                                        thd_info->command == COM_SLEEP ? "" :
                                        "Reading from net") :
                                       tmp->proc_info ? tmp->proc_info :
                                       tmp->mysys_var &&
                                       tmp->mysys_var->current_cond ?
                                       "Waiting on cond" : NullS);
        if (tmp->mysys_var)
          pthread_mutex_unlock(&tmp->mysys_var->mutex);
unknown's avatar
unknown committed
1044 1045

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1046 1047
        if (pthread_kill(tmp->real_id,0))
          tmp->proc_info="*** DEAD ***";        // This shouldn't happen
unknown's avatar
unknown committed
1048
#endif
unknown's avatar
unknown committed
1049 1050 1051
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1052
        thd_info->start_time= tmp->start_time;
unknown's avatar
unknown committed
1053
#endif
1054 1055 1056
        thd_info->query=0;
        if (tmp->query)
        {
unknown's avatar
unknown committed
1057 1058
	  /* query_length is always set before tmp->query */
          uint length= min(max_query_length, tmp->query_length);
1059 1060 1061 1062
          thd_info->query=(char*) thd->memdup(tmp->query,length+1);
          thd_info->query[length]=0;
        }
        thread_infos.append(thd_info);
unknown's avatar
unknown committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

  thread_info *thd_info;
  String *packet= &thd->packet;
  while ((thd_info=thread_infos.get()))
  {
    char buff[20],*end;
    packet->length(0);
    end=int10_to_str((long) thd_info->thread_id, buff,10);
1075 1076 1077
    net_store_data(packet,convert,buff,(uint) (end-buff));
    net_store_data(packet,convert,thd_info->user);
    net_store_data(packet,convert,thd_info->host);
unknown's avatar
unknown committed
1078
    if (thd_info->db)
1079
      net_store_data(packet,convert,thd_info->db);
unknown's avatar
unknown committed
1080 1081 1082
    else
      net_store_null(packet);
    if (thd_info->proc_info)
1083
      net_store_data(packet,convert,thd_info->proc_info);
unknown's avatar
unknown committed
1084
    else
1085
      net_store_data(packet,convert,command_name[thd_info->command]);
unknown's avatar
unknown committed
1086
    if (thd_info->start_time)
1087 1088
      net_store_data(packet,
		     (uint32) (time((time_t*) 0) - thd_info->start_time));
unknown's avatar
unknown committed
1089 1090 1091
    else
      net_store_null(packet);
    if (thd_info->state_info)
1092
      net_store_data(packet,convert,thd_info->state_info);
unknown's avatar
unknown committed
1093 1094 1095
    else
      net_store_null(packet);
    if (thd_info->query)
1096
      net_store_data(packet,convert,thd_info->query);
unknown's avatar
unknown committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    else
      net_store_null(packet);
    if (my_net_write(&thd->net,(char*) packet->ptr(),packet->length()))
      break; /* purecov: inspected */
  }
  send_eof(&thd->net);
  DBUG_VOID_RETURN;
}


/*****************************************************************************
** Status functions
*****************************************************************************/


int mysqld_show(THD *thd, const char *wild, show_var_st *variables)
{
  uint i;
  char buff[8192];
  String packet2(buff,sizeof(buff));
  List<Item> field_list;
1118
  CONVERT *convert=thd->convert_set;
unknown's avatar
unknown committed
1119 1120 1121 1122 1123 1124
  DBUG_ENTER("mysqld_show");
  field_list.push_back(new Item_empty_string("Variable_name",30));
  field_list.push_back(new Item_empty_string("Value",256));
  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1); /* purecov: inspected */

unknown's avatar
unknown committed
1125
  /* pthread_mutex_lock(&THR_LOCK_keycache); */
unknown's avatar
unknown committed
1126 1127 1128 1129 1130 1131
  pthread_mutex_lock(&LOCK_status);
  for (i=0; variables[i].name; i++)
  {
    if (!(wild && wild[0] && wild_compare(variables[i].name,wild)))
    {
      packet2.length(0);
1132
      net_store_data(&packet2,convert,variables[i].name);
unknown's avatar
unknown committed
1133 1134 1135
      switch (variables[i].type){
      case SHOW_LONG:
      case SHOW_LONG_CONST:
1136 1137
        net_store_data(&packet2,(uint32) *(ulong*) variables[i].value);
        break;
unknown's avatar
unknown committed
1138
      case SHOW_BOOL:
1139 1140 1141
        net_store_data(&packet2,(ulong) *(bool*) variables[i].value ?
                       "ON" : "OFF");
        break;
unknown's avatar
unknown committed
1142
      case SHOW_MY_BOOL:
1143 1144 1145
        net_store_data(&packet2,(ulong) *(my_bool*) variables[i].value ?
                       "ON" : "OFF");
        break;
unknown's avatar
unknown committed
1146 1147
      case SHOW_INT_CONST:
      case SHOW_INT:
1148 1149
        net_store_data(&packet2,(uint32) *(int*) variables[i].value);
        break;
1150 1151 1152 1153 1154 1155 1156 1157
      case SHOW_HAVE:
      {
	SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) variables[i].value;
        net_store_data(&packet2, (tmp == SHOW_OPTION_NO ? "NO" :
				  tmp == SHOW_OPTION_YES ? "YES" :
				  "DISABLED"));
        break;
      }
unknown's avatar
unknown committed
1158
      case SHOW_CHAR:
1159
        net_store_data(&packet2,convert, variables[i].value);
1160
        break;
unknown's avatar
unknown committed
1161
      case SHOW_STARTTIME:
1162 1163
        net_store_data(&packet2,(uint32) (thd->query_start() - start_time));
        break;
unknown's avatar
unknown committed
1164
      case SHOW_QUESTION:
1165 1166
        net_store_data(&packet2,(uint32) thd->query_id);
        break;
1167 1168 1169
      case SHOW_RPL_STATUS:
	net_store_data(&packet2, rpl_status_type[(int)rpl_status]);
	break;
unknown's avatar
unknown committed
1170
      case SHOW_OPENTABLES:
1171 1172
        net_store_data(&packet2,(uint32) cached_tables());
        break;
unknown's avatar
unknown committed
1173
      case SHOW_CHAR_PTR:
1174 1175
        {
          char *value= *(char**) variables[i].value;
1176
          net_store_data(&packet2,convert, value ? value : "");
1177 1178
          break;
        }
unknown's avatar
unknown committed
1179
#ifdef HAVE_OPENSSL
unknown's avatar
unknown committed
1180
	/* First group - functions relying on CTX */
unknown's avatar
unknown committed
1181 1182
      case SHOW_SSL_CTX_SESS_ACCEPT:
	net_store_data(&packet2,(uint32) 
1183 1184
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_accept(ssl_acceptor_fd->ssl_context_)));
unknown's avatar
unknown committed
1185 1186 1187
        break;
      case SHOW_SSL_CTX_SESS_ACCEPT_GOOD:
	net_store_data(&packet2,(uint32) 
1188 1189
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_accept_good(ssl_acceptor_fd->ssl_context_)));
unknown's avatar
unknown committed
1190
        break;
1191 1192
      case SHOW_SSL_CTX_SESS_CONNECT_GOOD:
	net_store_data(&packet2,(uint32) 
1193 1194
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_connect_good(ssl_acceptor_fd->ssl_context_)));
1195
        break;
unknown's avatar
unknown committed
1196 1197
      case SHOW_SSL_CTX_SESS_ACCEPT_RENEGOTIATE:
	net_store_data(&packet2,(uint32) 
1198 1199
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_accept_renegotiate(ssl_acceptor_fd->ssl_context_)));
unknown's avatar
unknown committed
1200
        break;
1201 1202
      case SHOW_SSL_CTX_SESS_CONNECT_RENEGOTIATE:
	net_store_data(&packet2,(uint32) 
1203 1204
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_connect_renegotiate(ssl_acceptor_fd->ssl_context_)));
1205
        break;
unknown's avatar
unknown committed
1206 1207
      case SHOW_SSL_CTX_SESS_CB_HITS:
	net_store_data(&packet2,(uint32) 
1208 1209
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_cb_hits(ssl_acceptor_fd->ssl_context_)));
unknown's avatar
unknown committed
1210
        break;
1211 1212
      case SHOW_SSL_CTX_SESS_HITS:
	net_store_data(&packet2,(uint32) 
1213 1214
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_hits(ssl_acceptor_fd->ssl_context_)));
1215 1216 1217
        break;
      case SHOW_SSL_CTX_SESS_CACHE_FULL:
	net_store_data(&packet2,(uint32) 
1218 1219
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_cache_full(ssl_acceptor_fd->ssl_context_)));
1220 1221 1222
        break;
      case SHOW_SSL_CTX_SESS_MISSES:
	net_store_data(&packet2,(uint32) 
1223 1224
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_misses(ssl_acceptor_fd->ssl_context_)));
1225 1226 1227
        break;
      case SHOW_SSL_CTX_SESS_TIMEOUTS:
	net_store_data(&packet2,(uint32) 
1228 1229
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_timeouts(ssl_acceptor_fd->ssl_context_)));
1230
        break;
unknown's avatar
unknown committed
1231 1232
      case SHOW_SSL_CTX_SESS_NUMBER:
	net_store_data(&packet2,(uint32) 
1233 1234
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_number(ssl_acceptor_fd->ssl_context_)));
unknown's avatar
unknown committed
1235
        break;
1236 1237
      case SHOW_SSL_CTX_SESS_CONNECT:
	net_store_data(&packet2,(uint32) 
1238 1239
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_connect(ssl_acceptor_fd->ssl_context_)));
1240
        break;
unknown's avatar
unknown committed
1241 1242
      case SHOW_SSL_CTX_SESS_GET_CACHE_SIZE:
	net_store_data(&packet2,(uint32) 
1243 1244
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_sess_get_cache_size(ssl_acceptor_fd->ssl_context_)));
unknown's avatar
unknown committed
1245 1246 1247
        break;
      case SHOW_SSL_CTX_GET_VERIFY_MODE:
	net_store_data(&packet2,(uint32) 
1248 1249
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_get_verify_mode(ssl_acceptor_fd->ssl_context_)));
unknown's avatar
unknown committed
1250 1251 1252
        break;
      case SHOW_SSL_CTX_GET_VERIFY_DEPTH:
	net_store_data(&packet2,(uint32) 
1253 1254
		       (!ssl_acceptor_fd ? 0 :
			SSL_CTX_get_verify_depth(ssl_acceptor_fd->ssl_context_)));
unknown's avatar
unknown committed
1255 1256
        break;
      case SHOW_SSL_CTX_GET_SESSION_CACHE_MODE:
1257 1258 1259 1260 1261
	if (!ssl_acceptor_fd)
	{
	  net_store_data(&packet2,"NONE" );
	  break;
	}
unknown's avatar
unknown committed
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	switch(SSL_CTX_get_session_cache_mode(ssl_acceptor_fd->ssl_context_))
	{
          case SSL_SESS_CACHE_OFF:
            net_store_data(&packet2,"OFF" );
	    break;
          case SSL_SESS_CACHE_CLIENT:
            net_store_data(&packet2,"CLIENT" );
	    break;
          case SSL_SESS_CACHE_SERVER:
            net_store_data(&packet2,"SERVER" );
	    break;
          case SSL_SESS_CACHE_BOTH:
            net_store_data(&packet2,"BOTH" );
	    break;
          case SSL_SESS_CACHE_NO_AUTO_CLEAR:
            net_store_data(&packet2,"NO_AUTO_CLEAR" );
	    break;
          case SSL_SESS_CACHE_NO_INTERNAL_LOOKUP:
            net_store_data(&packet2,"NO_INTERNAL_LOOKUP" );
	    break;
	  default:
            net_store_data(&packet2,"Unknown");
	    break;
	}
        break;
unknown's avatar
unknown committed
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
	/* First group - functions relying on SSL */
      case SHOW_SSL_GET_VERSION:
	net_store_data(&packet2, thd->net.vio->ssl_ ? 
			SSL_get_version(thd->net.vio->ssl_) : "");
        break;
      case SHOW_SSL_SESSION_REUSED:
	net_store_data(&packet2,(uint32) (thd->net.vio->ssl_ ? 
			SSL_session_reused(thd->net.vio->ssl_) : 0));
        break;
      case SHOW_SSL_GET_DEFAULT_TIMEOUT:
	net_store_data(&packet2,(uint32) (thd->net.vio->ssl_ ?
			SSL_get_default_timeout(thd->net.vio->ssl_):0));
        break;
      case SHOW_SSL_GET_VERIFY_MODE:
	net_store_data(&packet2,(uint32) (thd->net.vio->ssl_ ?
			SSL_get_verify_mode(thd->net.vio->ssl_):0));
        break;
      case SHOW_SSL_GET_VERIFY_DEPTH:
	net_store_data(&packet2,(uint32) (thd->net.vio->ssl_ ?
			SSL_get_verify_depth(thd->net.vio->ssl_):0));
        break;
      case SHOW_SSL_GET_CIPHER:
1309 1310 1311
	net_store_data(&packet2, thd->net.vio->ssl_ ?
		       SSL_get_cipher(thd->net.vio->ssl_) : "");
	break;
1312
      case SHOW_SSL_GET_CIPHER_LIST:
1313
	if (thd->net.vio->ssl_)
1314
	{
1315 1316 1317
	  char buf[1024], *pos;
	  pos=buf;
	  for (int i=0 ; i++ ;)
1318 1319 1320 1321
	  {
	    const char *p=SSL_get_cipher_list(thd->net.vio->ssl_,i);
	    if (p == NULL) 
	      break;
1322 1323
	    pos=strmov(pos, p);
	    *pos++= ':';
1324
	  }
1325 1326 1327
	  if (pos != buf)
	    pos--;				// Remove last ':'
	  *pos=0;
1328
 	  net_store_data(&packet2, buf);
1329 1330
        }
	else
1331
 	  net_store_data(&packet2, "");
unknown's avatar
unknown committed
1332
        break;
unknown's avatar
unknown committed
1333 1334

#endif /* HAVE_OPENSSL */
unknown's avatar
unknown committed
1335 1336
      }
      if (my_net_write(&thd->net, (char*) packet2.ptr(),packet2.length()))
1337
        goto err;                               /* purecov: inspected */
unknown's avatar
unknown committed
1338 1339 1340
    }
  }
  pthread_mutex_unlock(&LOCK_status);
unknown's avatar
unknown committed
1341
  /* pthread_mutex_unlock(&THR_LOCK_keycache); */
unknown's avatar
unknown committed
1342 1343 1344 1345 1346
  send_eof(&thd->net);
  DBUG_RETURN(0);

 err:
  pthread_mutex_unlock(&LOCK_status);
unknown's avatar
unknown committed
1347
  /* pthread_mutex_unlock(&THR_LOCK_keycache); */
unknown's avatar
unknown committed
1348 1349 1350 1351
  DBUG_RETURN(1);
}

#ifdef __GNUC__
unknown's avatar
unknown committed
1352
template class List_iterator_fast<char>;
unknown's avatar
unknown committed
1353 1354
template class List<char>;
#endif