sql_servers.cc 32.7 KB
Newer Older
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1 2 3 4
/* Copyright (C) 2000-2003 MySQL AB

   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
5
   the Free Software Foundation; version 2 of the License.
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

   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.

   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 */


/*
  The servers are saved in the system table "servers"
*/

#include "mysql_priv.h"
#include "hash_filo.h"
#include <m_ctype.h>
#include <stdarg.h>
#include "sp_head.h"
#include "sp.h"

28 29 30 31 32 33
/*
  We only use 1 mutex to guard the data structures - THR_LOCK_servers.
  Read locked when only reading data and write-locked for all other access.
*/

static HASH servers_cache;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
34 35 36
static MEM_ROOT mem;
static rw_lock_t THR_LOCK_servers;

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
static bool get_server_from_table_to_cache(TABLE *table);

/* insert functions */
static int insert_server(THD *thd, FOREIGN_SERVER *server_options);
static int insert_server_record(TABLE *table, FOREIGN_SERVER *server);
static int insert_server_record_into_cache(FOREIGN_SERVER *server);
static void prepare_server_struct_for_insert(LEX_SERVER_OPTIONS *server_options,
                                             FOREIGN_SERVER *server);
/* drop functions */ 
static int delete_server_record(TABLE *table,
                                char *server_name,
                                int server_name_length);
static int delete_server_record_in_cache(LEX_SERVER_OPTIONS *server_options);

/* update functions */
static void prepare_server_struct_for_update(LEX_SERVER_OPTIONS *server_options,
                                             FOREIGN_SERVER *existing,
                                             FOREIGN_SERVER *altered);
static int update_server(THD *thd, FOREIGN_SERVER *existing, 
					     FOREIGN_SERVER *altered);
static int update_server_record(TABLE *table, FOREIGN_SERVER *server);
static int update_server_record_in_cache(FOREIGN_SERVER *existing,
                                         FOREIGN_SERVER *altered);
/* utility functions */
static void merge_server_struct(FOREIGN_SERVER *from, FOREIGN_SERVER *to);



patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
65 66 67 68 69 70 71 72 73 74 75 76
static byte *servers_cache_get_key(FOREIGN_SERVER *server, uint *length,
			       my_bool not_used __attribute__((unused)))
{
  DBUG_ENTER("servers_cache_get_key");
  DBUG_PRINT("info", ("server_name_length %d server_name %s",
                      server->server_name_length,
                      server->server_name));

  *length= (uint) server->server_name_length;
  DBUG_RETURN((byte*) server->server_name);
}

77

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/*
  Initialize structures responsible for servers used in federated
  server scheme information for them from the server
  table in the 'mysql' database.

  SYNOPSIS
    servers_init()
      dont_read_server_table  TRUE if we want to skip loading data from
                            server table and disable privilege checking.

  NOTES
    This function is mostly responsible for preparatory steps, main work
    on initialization and grants loading is done in servers_reload().

  RETURN VALUES
    0	ok
    1	Could not initialize servers
*/

97
bool servers_init(bool dont_read_servers_table)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
98 99
{
  THD  *thd;
100
  bool return_val= FALSE;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
101 102 103
  DBUG_ENTER("servers_init");

  /* init the mutex */
patg@radha.patg.net's avatar
patg@radha.patg.net committed
104
  if (my_rwlock_init(&THR_LOCK_servers, NULL))
105
    DBUG_RETURN(TRUE);
patg@radha.patg.net's avatar
patg@radha.patg.net committed
106

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
107 108 109 110
  /* initialise our servers cache */
  if (hash_init(&servers_cache, system_charset_info, 32, 0, 0,
                (hash_get_key) servers_cache_get_key, 0, 0))
  {
111
    return_val= TRUE; /* we failed, out of memory? */
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
112 113 114 115 116 117 118 119 120 121 122 123 124
    goto end;
  }

  /* Initialize the mem root for data */
  init_alloc_root(&mem, ACL_ALLOC_BLOCK_SIZE, 0);

  if (dont_read_servers_table)
    goto end;

  /*
    To be able to run this from boot, we allocate a temporary THD
  */
  if (!(thd=new THD))
125
    DBUG_RETURN(TRUE);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  thd->thread_stack= (char*) &thd;
  thd->store_globals();
  /*
    It is safe to call servers_reload() since servers_* arrays and hashes which
    will be freed there are global static objects and thus are initialized
    by zeros at startup.
  */
  return_val= servers_reload(thd);
  delete thd;
  /* Remember that we don't have a THD */
  my_pthread_setspecific_ptr(THR_THD,  0);

end:
  DBUG_RETURN(return_val);
}

/*
  Initialize server structures

  SYNOPSIS
    servers_load()
      thd     Current thread
      tables  List containing open "mysql.servers"

  RETURN VALUES
    FALSE  Success
    TRUE   Error
*/

155
static bool servers_load(THD *thd, TABLE_LIST *tables)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
156 157 158
{
  TABLE *table;
  READ_RECORD read_record_info;
159
  bool return_val= TRUE;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  DBUG_ENTER("servers_load");

  /* first, send all cached rows to sleep with the fishes, oblivion!
     I expect this crappy comment replaced */
  free_root(&mem, MYF(MY_MARK_BLOCKS_FREE));
  my_hash_reset(&servers_cache);

  init_read_record(&read_record_info,thd,table=tables[0].table,NULL,1,0);
  while (!(read_record_info.read_record(&read_record_info)))
  {
    /* return_val is already TRUE, so no need to set */
    if ((get_server_from_table_to_cache(table)))
      goto end;
  }

175
  return_val= FALSE;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

end:
  end_read_record(&read_record_info);
  DBUG_RETURN(return_val);
}


/*
  Forget current servers cache and read new servers 
  from the conneciton table.

  SYNOPSIS
    servers_reload()
      thd  Current thread

  NOTE
    All tables of calling thread which were open and locked by LOCK TABLES
    statement will be unlocked and closed.
    This function is also used for initialization of structures responsible
    for user/db-level privilege checking.

  RETURN VALUE
    FALSE  Success
    TRUE   Failure
*/

202
bool servers_reload(THD *thd)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
203 204
{
  TABLE_LIST tables[1];
205
  bool return_val= TRUE;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
206 207 208 209 210 211 212 213 214
  DBUG_ENTER("servers_reload");

  if (thd->locked_tables)
  {					// Can't have locked tables here
    thd->lock=thd->locked_tables;
    thd->locked_tables=0;
    close_thread_tables(thd);
  }

215 216 217
  DBUG_PRINT("info", ("locking servers_cache"));
  rw_wrlock(&THR_LOCK_servers);

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  bzero((char*) tables, sizeof(tables));
  tables[0].alias= tables[0].table_name= (char*) "servers";
  tables[0].db= (char*) "mysql";
  tables[0].lock_type= TL_READ;

  if (simple_open_n_lock_tables(thd, tables))
  {
    sql_print_error("Fatal error: Can't open and lock privilege tables: %s",
		    thd->net.last_error);
    goto end;
  }

  if ((return_val= servers_load(thd, tables)))
  {					// Error. Revert to old list
    /* blast, for now, we have no servers, discuss later way to preserve */

    DBUG_PRINT("error",("Reverting to old privileges"));
    servers_free();
  }

end:
  close_thread_tables(thd);
240 241
  DBUG_PRINT("info", ("unlocking servers_cache"));
  rw_unlock(&THR_LOCK_servers);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
242 243 244
  DBUG_RETURN(return_val);
}

245

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
/*
  Initialize structures responsible for servers used in federated
  server scheme information for them from the server
  table in the 'mysql' database.

  SYNOPSIS
    get_server_from_table_to_cache()
      TABLE *table         open table pointer


  NOTES
    This function takes a TABLE pointer (pointing to an opened
    table). With this open table, a FOREIGN_SERVER struct pointer
    is allocated into root memory, then each member of the FOREIGN_SERVER
    struct is populated. A char pointer takes the return value of get_field
    for each column we're interested in obtaining, and if that pointer
    isn't 0x0, the FOREIGN_SERVER member is set to that value, otherwise,
    is set to the value of an empty string, since get_field would set it to
    0x0 if the column's value is empty, even if the default value for that
    column is NOT NULL.

  RETURN VALUES
    0	ok
    1	could not insert server struct into global servers cache
*/

272 273
static bool 
get_server_from_table_to_cache(TABLE *table)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313
{
  /* alloc a server struct */
  char *ptr;
  char *blank= (char*)"";
  FOREIGN_SERVER *server= (FOREIGN_SERVER *)alloc_root(&mem,
                                                       sizeof(FOREIGN_SERVER));
  DBUG_ENTER("get_server_from_table_to_cache");
  table->use_all_columns();

  /* get each field into the server struct ptr */
  server->server_name= get_field(&mem, table->field[0]);
  server->server_name_length= strlen(server->server_name);
  ptr= get_field(&mem, table->field[1]);
  server->host= ptr ? ptr : blank;
  ptr= get_field(&mem, table->field[2]);
  server->db= ptr ? ptr : blank;
  ptr= get_field(&mem, table->field[3]);
  server->username= ptr ? ptr : blank;
  ptr= get_field(&mem, table->field[4]);
  server->password= ptr ? ptr : blank;
  ptr= get_field(&mem, table->field[5]);
  server->sport= ptr ? ptr : blank;

  server->port= server->sport ? atoi(server->sport) : 0;

  ptr= get_field(&mem, table->field[6]);
  server->socket= ptr && strlen(ptr) ? ptr : NULL;
  ptr= get_field(&mem, table->field[7]);
  server->scheme= ptr ? ptr : blank;
  ptr= get_field(&mem, table->field[8]);
  server->owner= ptr ? ptr : blank;
  DBUG_PRINT("info", ("server->server_name %s", server->server_name));
  DBUG_PRINT("info", ("server->host %s", server->host));
  DBUG_PRINT("info", ("server->db %s", server->db));
  DBUG_PRINT("info", ("server->username %s", server->username));
  DBUG_PRINT("info", ("server->password %s", server->password));
  DBUG_PRINT("info", ("server->socket %s", server->socket));
  if (my_hash_insert(&servers_cache, (byte*) server))
  {
    DBUG_PRINT("info", ("had a problem inserting server %s at %lx",
patg@radha.patg.net's avatar
patg@radha.patg.net committed
314
                        server->server_name, (long unsigned int) server));
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    // error handling needed here
    DBUG_RETURN(TRUE);
  }
  DBUG_RETURN(FALSE);
}


/*
  SYNOPSIS
    insert_server()
      THD   *thd     - thread pointer
      FOREIGN_SERVER *server - pointer to prepared FOREIGN_SERVER struct

  NOTES
    This function takes a server object that is has all members properly
    prepared, ready to be inserted both into the mysql.servers table and
    the servers cache.
332 333
	
    THR_LOCK_servers must be write locked.
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
334 335 336 337 338 339

  RETURN VALUES
    0  - no error
    other - error code
*/

340 341
static int 
insert_server(THD *thd, FOREIGN_SERVER *server)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
342
{
343
  int error= -1;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
344 345 346 347 348 349 350 351 352 353 354
  TABLE_LIST tables;
  TABLE *table;

  DBUG_ENTER("insert_server");

  bzero((char*) &tables, sizeof(tables));
  tables.db= (char*) "mysql";
  tables.alias= tables.table_name= (char*) "servers";

  /* need to open before acquiring THR_LOCK_plugin or it will deadlock */
  if (! (table= open_ltable(thd, &tables, TL_WRITE)))
355
    goto end;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
356 357 358 359 360 361 362 363 364 365 366 367 368

  /* insert the server into the table */
  if ((error= insert_server_record(table, server)))
    goto end;

  /* insert the server into the cache */
  if ((error= insert_server_record_into_cache(server)))
    goto end;

end:
  DBUG_RETURN(error);
}

369

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
370 371 372 373 374 375 376 377 378
/*
  SYNOPSIS
    int insert_server_record_into_cache()
      FOREIGN_SERVER *server

  NOTES
    This function takes a FOREIGN_SERVER pointer to an allocated (root mem)
    and inserts it into the global servers cache

379 380
    THR_LOCK_servers must be write locked.

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
381 382 383 384 385 386
  RETURN VALUE
    0   - no error
    >0  - error code

*/

387 388
static int 
insert_server_record_into_cache(FOREIGN_SERVER *server)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
389 390 391 392 393 394 395 396
{
  int error=0;
  DBUG_ENTER("insert_server_record_into_cache");
  /*
    We succeded in insertion of the server to the table, now insert
    the server to the cache
  */
  DBUG_PRINT("info", ("inserting server %s at %lx, length %d",
patg@radha.patg.net's avatar
patg@radha.patg.net committed
397
                        server->server_name, (long unsigned int) server,
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
398 399 400 401
                        server->server_name_length));
  if (my_hash_insert(&servers_cache, (byte*) server))
  {
    DBUG_PRINT("info", ("had a problem inserting server %s at %lx",
patg@radha.patg.net's avatar
patg@radha.patg.net committed
402
                        server->server_name, (long unsigned int) server));
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
403 404 405 406 407 408
    // error handling needed here
    error= 1;
  }
  DBUG_RETURN(error);
}

409

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/*
  SYNOPSIS
    store_server_fields()
      TABLE *table
      FOREIGN_SERVER *server

  NOTES
    This function takes an opened table object, and a pointer to an 
    allocated FOREIGN_SERVER struct, and then stores each member of
    the FOREIGN_SERVER to the appropriate fields in the table, in 
    advance of insertion into the mysql.servers table

  RETURN VALUE
    VOID

*/

427 428
static void 
store_server_fields(TABLE *table, FOREIGN_SERVER *server)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
{

  table->use_all_columns();
  /*
    "server" has already been prepped by prepare_server_struct_for_<>
    so, all we need to do is check if the value is set (> -1 for port)

    If this happens to be an update, only the server members that 
    have changed will be set. If an insert, then all will be set,
    even if with empty strings
  */
  if (server->host)
    table->field[1]->store(server->host,
                           (uint) strlen(server->host), system_charset_info);
  if (server->db)
    table->field[2]->store(server->db,
                           (uint) strlen(server->db), system_charset_info);
  if (server->username)
    table->field[3]->store(server->username,
                           (uint) strlen(server->username), system_charset_info);
  if (server->password)
    table->field[4]->store(server->password,
                           (uint) strlen(server->password), system_charset_info);
  if (server->port > -1)
    table->field[5]->store(server->port);

  if (server->socket)
    table->field[6]->store(server->socket,
                           (uint) strlen(server->socket), system_charset_info);
  if (server->scheme)
    table->field[7]->store(server->scheme,
                           (uint) strlen(server->scheme), system_charset_info);
  if (server->owner)
    table->field[8]->store(server->owner,
                           (uint) strlen(server->owner), system_charset_info);
}

/*
  SYNOPSIS
    insert_server_record()
      TABLE *table
      FOREIGN_SERVER *server

  NOTES
    This function takes the arguments of an open table object and a pointer
    to an allocated FOREIGN_SERVER struct. It stores the server_name into
    the first field of the table (the primary key, server_name column). With
    this, index_read_idx is called, if the record is found, an error is set
    to ER_FOREIGN_SERVER_EXISTS (the server with that server name exists in the
    table), if not, then store_server_fields stores all fields of the
    FOREIGN_SERVER to the table, then ha_write_row is inserted. If an error
    is encountered in either index_read_idx or ha_write_row, then that error
    is returned

  RETURN VALUE
    0 - no errors
    >0 - error code

  */

489
static
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
490 491 492 493 494 495
int insert_server_record(TABLE *table, FOREIGN_SERVER *server)
{
  int error;
  DBUG_ENTER("insert_server_record");
  table->use_all_columns();

496 497
  empty_record(table);

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  /* set the field that's the PK to the value we're looking for */
  table->field[0]->store(server->server_name,
                         server->server_name_length,
                         system_charset_info);

  /* read index until record is that specified in server_name */
  if ((error= table->file->index_read_idx(table->record[0], 0,
                                   (byte *)table->field[0]->ptr,
                                   table->key_info[0].key_length,
                                   HA_READ_KEY_EXACT)))
  {
    /* if not found, err */
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
    {
      table->file->print_error(error, MYF(0));
      error= 1;
    }
    /* store each field to be inserted */
    store_server_fields(table, server);

    DBUG_PRINT("info",("record for server '%s' not found!",
                       server->server_name));
    /* write/insert the new server */
    if ((error=table->file->ha_write_row(table->record[0])))
    {
      table->file->print_error(error, MYF(0));
    }
    else
      error= 0;
  }
  else
    error= ER_FOREIGN_SERVER_EXISTS;
  DBUG_RETURN(error);
}

/*
  SYNOPSIS
    drop_server()
      THD *thd
      LEX_SERVER_OPTIONS *server_options

  NOTES
    This function takes as its arguments a THD object pointer and a pointer
    to a LEX_SERVER_OPTIONS struct from the parser. The member 'server_name'
    of this LEX_SERVER_OPTIONS struct contains the value of the server to be
    deleted. The mysql.servers table is opened via open_ltable, a table object
    returned, the servers cache mutex locked, then delete_server_record is
    called with this table object and LEX_SERVER_OPTIONS server_name and
    server_name_length passed, containing the name of the server to be
    dropped/deleted, then delete_server_record_in_cache is called to delete
    the server from the servers cache.

  RETURN VALUE
    0 - no error
    > 0 - error code
*/

int drop_server(THD *thd, LEX_SERVER_OPTIONS *server_options)
{
557
  int error;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
558 559 560 561 562 563 564 565 566 567 568 569 570
  TABLE_LIST tables;
  TABLE *table;

  DBUG_ENTER("drop_server");
  DBUG_PRINT("info", ("server name server->server_name %s",
                      server_options->server_name));

  bzero((char*) &tables, sizeof(tables));
  tables.db= (char*) "mysql";
  tables.alias= tables.table_name= (char*) "servers";

  rw_wrlock(&THR_LOCK_servers);

571 572 573
  /* hit the memory hit first */
  if ((error= delete_server_record_in_cache(server_options)))
    goto end;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
574

575 576 577
  if (! (table= open_ltable(thd, &tables, TL_WRITE)))
  {
    error= my_errno;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
578
    goto end;
579
  }
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
580

581 582 583
  error= delete_server_record(table,
                              server_options->server_name,
                              server_options->server_name_length);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
584

585 586 587 588
  /*
	Perform a reload so we don't have a 'hole' in our mem_root
  */
  servers_load(thd, &tables);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
589 590 591 592 593

end:
  rw_unlock(&THR_LOCK_servers);
  DBUG_RETURN(error);
}
594 595


patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
/*

  SYNOPSIS
    delete_server_record_in_cache()
      LEX_SERVER_OPTIONS *server_options

  NOTES
    This function's  argument is a LEX_SERVER_OPTIONS struct pointer. This
    function uses the "server_name" and "server_name_length" members of the
    lex->server_options to search for the server in the servers_cache. Upon
    returned the server (pointer to a FOREIGN_SERVER struct), it then deletes
    that server from the servers_cache hash.

  RETURN VALUE
    0 - no error

*/

614 615
static int 
delete_server_record_in_cache(LEX_SERVER_OPTIONS *server_options)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
616
{
617
  int error= ER_FOREIGN_SERVER_DOESNT_EXIST;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
  FOREIGN_SERVER *server;
  DBUG_ENTER("delete_server_record_in_cache");

  DBUG_PRINT("info",("trying to obtain server name %s length %d",
                     server_options->server_name,
                     server_options->server_name_length));


  if (!(server= (FOREIGN_SERVER *) hash_search(&servers_cache,
                                     (byte*) server_options->server_name,
                                     server_options->server_name_length)))
  {
    DBUG_PRINT("info", ("server_name %s length %d not found!",
                        server_options->server_name,
                        server_options->server_name_length));
633
    goto end;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
634 635 636 637 638 639 640 641 642
  }
  /*
    We succeded in deletion of the server to the table, now delete
    the server from the cache
  */
  DBUG_PRINT("info",("deleting server %s length %d",
                     server->server_name,
                     server->server_name_length));

643 644 645
  VOID(hash_delete(&servers_cache, (byte*) server));
  
  error= 0;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
646

647
end:
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
648 649 650
  DBUG_RETURN(error);
}

651

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
/*

  SYNOPSIS
    update_server()
      THD *thd
      FOREIGN_SERVER *existing
      FOREIGN_SERVER *altered

  NOTES
    This function takes as arguments a THD object pointer, and two pointers,
    one pointing to the existing FOREIGN_SERVER struct "existing" (which is
    the current record as it is) and another pointer pointing to the
    FOREIGN_SERVER struct with the members containing the modified/altered
    values that need to be updated in both the mysql.servers table and the 
    servers_cache. It opens a table, passes the table and the altered
    FOREIGN_SERVER pointer, which will be used to update the mysql.servers 
    table for the particular server via the call to update_server_record,
    and in the servers_cache via update_server_record_in_cache. 

671 672
    THR_LOCK_servers must be write locked.

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
673 674 675 676 677 678 679 680
  RETURN VALUE
    0 - no error
    >0 - error code

*/

int update_server(THD *thd, FOREIGN_SERVER *existing, FOREIGN_SERVER *altered)
{
681
  int error;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
682 683 684 685 686 687 688 689 690
  TABLE *table;
  TABLE_LIST tables;
  DBUG_ENTER("update_server");

  bzero((char*) &tables, sizeof(tables));
  tables.db= (char*)"mysql";
  tables.alias= tables.table_name= (char*)"servers";

  if (!(table= open_ltable(thd, &tables, TL_WRITE)))
691 692 693 694
  {
    error= my_errno;
    goto end;
  }
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
695 696 697 698

  if ((error= update_server_record(table, altered)))
    goto end;

699 700 701 702 703 704
  error= update_server_record_in_cache(existing, altered);

  /*
	Perform a reload so we don't have a 'hole' in our mem_root
  */
  servers_load(thd, &tables);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
705 706 707 708 709

end:
  DBUG_RETURN(error);
}

710

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
/*

  SYNOPSIS
    update_server_record_in_cache()
      FOREIGN_SERVER *existing
      FOREIGN_SERVER *altered

  NOTES
    This function takes as an argument the FOREIGN_SERVER structi pointer
    for the existing server and the FOREIGN_SERVER struct populated with only 
    the members which have been updated. It then "merges" the "altered" struct
    members to the existing server, the existing server then represents an
    updated server. Then, the existing record is deleted from the servers_cache
    HASH, then the updated record inserted, in essence replacing the old
    record.

727 728
    THR_LOCK_servers must be write locked.

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  RETURN VALUE
    0 - no error
    1 - error

*/

int update_server_record_in_cache(FOREIGN_SERVER *existing,
                                  FOREIGN_SERVER *altered)
{
  int error= 0;
  DBUG_ENTER("update_server_record_in_cache");

  /*
    update the members that haven't been change in the altered server struct
    with the values of the existing server struct
  */
  merge_server_struct(existing, altered);

  /*
    delete the existing server struct from the server cache
  */
  VOID(hash_delete(&servers_cache, (byte*)existing));

  /*
    Insert the altered server struct into the server cache
  */
  if (my_hash_insert(&servers_cache, (byte*)altered))
  {
    DBUG_PRINT("info", ("had a problem inserting server %s at %lx",
patg@radha.patg.net's avatar
patg@radha.patg.net committed
758
                        altered->server_name, (long unsigned int) altered));
759
    error= ER_OUT_OF_RESOURCES;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
760 761 762 763 764
  }

  DBUG_RETURN(error);
}

765

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
/*

  SYNOPSIS
    merge_server_struct()
      FOREIGN_SERVER *from
      FOREIGN_SERVER *to

  NOTES
    This function takes as its arguments two pointers each to an allocated
    FOREIGN_SERVER struct. The first FOREIGN_SERVER struct represents the struct
    that we will obtain values from (hence the name "from"), the second
    FOREIGN_SERVER struct represents which FOREIGN_SERVER struct we will be
    "copying" any members that have a value to (hence the name "to")

  RETURN VALUE
    VOID

*/

void merge_server_struct(FOREIGN_SERVER *from, FOREIGN_SERVER *to)
{
  DBUG_ENTER("merge_server_struct");
  if (!to->host)
    to->host= strdup_root(&mem, from->host);
  if (!to->db)
    to->db= strdup_root(&mem, from->db);
  if (!to->username)
    to->username= strdup_root(&mem, from->username);
  if (!to->password)
    to->password= strdup_root(&mem, from->password);
  if (to->port == -1)
    to->port= from->port;
798
  if (!to->socket && from->socket)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
799
    to->socket= strdup_root(&mem, from->socket);
800
  if (!to->scheme && from->scheme)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
801 802 803 804 805 806 807
    to->scheme= strdup_root(&mem, from->scheme);
  if (!to->owner)
    to->owner= strdup_root(&mem, from->owner);

  DBUG_VOID_RETURN;
}

808

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/*

  SYNOPSIS
    update_server_record()
      TABLE *table
      FOREIGN_SERVER *server

  NOTES
    This function takes as its arguments an open TABLE pointer, and a pointer
    to an allocated FOREIGN_SERVER structure representing an updated record
    which needs to be inserted. The primary key, server_name is stored to field
    0, then index_read_idx is called to read the index to that record, the
    record then being ready to be updated, if found. If not found an error is
    set and error message printed. If the record is found, store_record is
    called, then store_server_fields stores each field from the the members of
    the updated FOREIGN_SERVER struct.

  RETURN VALUE
    0 - no error

*/

831 832 833

static int 
update_server_record(TABLE *table, FOREIGN_SERVER *server)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
{
  int error=0;
  DBUG_ENTER("update_server_record");
  table->use_all_columns();
  /* set the field that's the PK to the value we're looking for */
  table->field[0]->store(server->server_name,
                         server->server_name_length,
                         system_charset_info);

  if ((error= table->file->index_read_idx(table->record[0], 0,
                                   (byte *)table->field[0]->ptr,
                                   table->key_info[0].key_length,
                                   HA_READ_KEY_EXACT)))
  {
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
      table->file->print_error(error, MYF(0));
    DBUG_PRINT("info",("server not found!"));
    error= ER_FOREIGN_SERVER_DOESNT_EXIST;
  }
  else
  {
    /* ok, so we can update since the record exists in the table */
    store_record(table,record[1]);
    store_server_fields(table, server);
    if ((error=table->file->ha_update_row(table->record[1],table->record[0])))
    {
      DBUG_PRINT("info",("problems with ha_update_row %d", error));
      goto end;
    }
  }

end:
  DBUG_RETURN(error);
}

869

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
/*

  SYNOPSIS
    delete_server_record()
      TABLE *table
      char *server_name
      int server_name_length

  NOTES

  RETURN VALUE
    0 - no error

*/

885 886 887
static int 
delete_server_record(TABLE *table,
                     char *server_name, int server_name_length)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
888
{
889
  int error;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
  DBUG_ENTER("delete_server_record");
  table->use_all_columns();

  /* set the field that's the PK to the value we're looking for */
  table->field[0]->store(server_name, server_name_length, system_charset_info);

  if ((error= table->file->index_read_idx(table->record[0], 0,
                                   (byte *)table->field[0]->ptr,
                                   table->key_info[0].key_length,
                                   HA_READ_KEY_EXACT)))
  {
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
      table->file->print_error(error, MYF(0));
    DBUG_PRINT("info",("server not found!"));
    error= ER_FOREIGN_SERVER_DOESNT_EXIST;
  }
  else
  {
    if ((error= table->file->ha_delete_row(table->record[0])))
      table->file->print_error(error, MYF(0));
  }

  DBUG_RETURN(error);
}

/*

  SYNOPSIS
    create_server()
        THD *thd
        LEX_SERVER_OPTIONS *server_options

  NOTES

  RETURN VALUE
    0 - no error

*/

int create_server(THD *thd, LEX_SERVER_OPTIONS *server_options)
{
931
  int error= ER_FOREIGN_SERVER_EXISTS;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
932 933 934 935 936 937
  FOREIGN_SERVER *server;

  DBUG_ENTER("create_server");
  DBUG_PRINT("info", ("server_options->server_name %s",
                      server_options->server_name));

938 939 940 941 942 943 944
  rw_wrlock(&THR_LOCK_servers);

  /* hit the memory first */
  if (hash_search(&servers_cache, (byte*) server_options->server_name,
				   server_options->server_name_length))
    goto end;

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
945 946 947
  server= (FOREIGN_SERVER *)alloc_root(&mem,
                                       sizeof(FOREIGN_SERVER));

948
  prepare_server_struct_for_insert(server_options, server);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
949

950
  error= insert_server(thd, server);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
951 952 953 954

  DBUG_PRINT("info", ("error returned %d", error));

end:
955
  rw_unlock(&THR_LOCK_servers);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
956 957 958
  DBUG_RETURN(error);
}

959

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
/*

  SYNOPSIS
    alter_server()
      THD *thd
      LEX_SERVER_OPTIONS *server_options

  NOTES

  RETURN VALUE
    0 - no error

*/

int alter_server(THD *thd, LEX_SERVER_OPTIONS *server_options)
{
976
  int error= ER_FOREIGN_SERVER_DOESNT_EXIST;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
977 978 979 980 981
  FOREIGN_SERVER *altered, *existing;
  DBUG_ENTER("alter_server");
  DBUG_PRINT("info", ("server_options->server_name %s",
                      server_options->server_name));

982
  rw_wrlock(&THR_LOCK_servers);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
983 984 985 986 987 988

  if (!(existing= (FOREIGN_SERVER *) hash_search(&servers_cache,
                                                 (byte*) server_options->server_name,
                                               server_options->server_name_length)))
    goto end;

989 990
  altered= (FOREIGN_SERVER *)alloc_root(&mem,
                                        sizeof(FOREIGN_SERVER));
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
991

992 993 994
  prepare_server_struct_for_update(server_options, existing, altered);

  error= update_server(thd, existing, altered);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
995 996 997

end:
  DBUG_PRINT("info", ("error returned %d", error));
998
  rw_unlock(&THR_LOCK_servers);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
999 1000 1001
  DBUG_RETURN(error);
}

1002

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
/*

  SYNOPSIS
    prepare_server_struct_for_insert()
      LEX_SERVER_OPTIONS *server_options
      FOREIGN_SERVER *server

  NOTES

  RETURN VALUE
1013
    none
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1014 1015 1016

*/

1017 1018 1019
static void
prepare_server_struct_for_insert(LEX_SERVER_OPTIONS *server_options,
                                 FOREIGN_SERVER *server)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
{
  char *unset_ptr= (char*)"";
  DBUG_ENTER("prepare_server_struct");

  /* these two MUST be set */
  server->server_name= strdup_root(&mem, server_options->server_name);
  server->server_name_length= server_options->server_name_length;

  server->host= server_options->host ?
    strdup_root(&mem, server_options->host) : unset_ptr;

  server->db= server_options->db ?
    strdup_root(&mem, server_options->db) : unset_ptr;

  server->username= server_options->username ?
    strdup_root(&mem, server_options->username) : unset_ptr;

  server->password= server_options->password ?
    strdup_root(&mem, server_options->password) : unset_ptr;

  /* set to 0 if not specified */
  server->port= server_options->port > -1 ?
    server_options->port : 0;

  server->socket= server_options->socket ?
    strdup_root(&mem, server_options->socket) : unset_ptr;

  server->scheme= server_options->scheme ?
    strdup_root(&mem, server_options->scheme) : unset_ptr;

  server->owner= server_options->owner ?
    strdup_root(&mem, server_options->owner) : unset_ptr;

1053
  DBUG_VOID_RETURN;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
}

/*

  SYNOPSIS
    prepare_server_struct_for_update()
      LEX_SERVER_OPTIONS *server_options

  NOTES

  RETURN VALUE
    0 - no error

*/

1069 1070 1071 1072
static void
prepare_server_struct_for_update(LEX_SERVER_OPTIONS *server_options,
                                 FOREIGN_SERVER *existing,
                                 FOREIGN_SERVER *altered)
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
{
  DBUG_ENTER("prepare_server_struct_for_update");

  altered->server_name= strdup_root(&mem, server_options->server_name);
  altered->server_name_length= server_options->server_name_length;
  DBUG_PRINT("info", ("existing name %s altered name %s",
                      existing->server_name, altered->server_name));

  /*
    The logic here is this: is this value set AND is it different
    than the existing value?
  */
  altered->host=
    (server_options->host && (strcmp(server_options->host, existing->host))) ?
     strdup_root(&mem, server_options->host) : 0;

  altered->db=
      (server_options->db && (strcmp(server_options->db, existing->db))) ?
        strdup_root(&mem, server_options->db) : 0;

  altered->username=
      (server_options->username &&
      (strcmp(server_options->username, existing->username))) ?
        strdup_root(&mem, server_options->username) : 0;

  altered->password=
      (server_options->password &&
      (strcmp(server_options->password, existing->password))) ?
        strdup_root(&mem, server_options->password) : 0;

  /*
    port is initialised to -1, so if unset, it will be -1
  */
  altered->port= (server_options->port > -1 &&
                 server_options->port != existing->port) ?
    server_options->port : -1;

  altered->socket=
    (server_options->socket &&
    (strcmp(server_options->socket, existing->socket))) ?
      strdup_root(&mem, server_options->socket) : 0;

  altered->scheme=
    (server_options->scheme &&
    (strcmp(server_options->scheme, existing->scheme))) ?
      strdup_root(&mem, server_options->scheme) : 0;

  altered->owner=
    (server_options->owner &&
    (strcmp(server_options->owner, existing->owner))) ?
      strdup_root(&mem, server_options->owner) : 0;

1125
  DBUG_VOID_RETURN;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
}

/*

  SYNOPSIS
    servers_free()
      bool end

  NOTES

  RETURN VALUE
    void

*/

void servers_free(bool end)
{
  DBUG_ENTER("servers_free");
1144
  if (!hash_inited(&servers_cache))
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1145
    DBUG_VOID_RETURN;
1146
  rwlock_destroy(&THR_LOCK_servers);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
  free_root(&mem,MYF(0));
  hash_free(&servers_cache);
  DBUG_VOID_RETURN;
}


/*

  SYNOPSIS
    get_server_by_name()
      const char *server_name

  NOTES

  RETURN VALUE
   FOREIGN_SERVER *

*/

FOREIGN_SERVER *get_server_by_name(const char *server_name)
{
  ulong error_num=0;
1169
  uint server_name_length;
patg@radha.patg.net's avatar
patg@radha.patg.net committed
1170
  FOREIGN_SERVER *server= 0;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  DBUG_ENTER("get_server_by_name");
  DBUG_PRINT("info", ("server_name %s", server_name));

  server_name_length= strlen(server_name);

  if (! server_name || !strlen(server_name))
  {
    DBUG_PRINT("info", ("server_name not defined!"));
    error_num= 1;
    DBUG_RETURN((FOREIGN_SERVER *)NULL);
  }

  DBUG_PRINT("info", ("locking servers_cache"));
1184
  rw_rdlock(&THR_LOCK_servers);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1185 1186 1187 1188 1189 1190 1191 1192 1193
  if (!(server= (FOREIGN_SERVER *) hash_search(&servers_cache,
                                               (byte*) server_name,
                                               server_name_length)))
  {
    DBUG_PRINT("info", ("server_name %s length %d not found!",
                        server_name, server_name_length));
    server= (FOREIGN_SERVER *) NULL;
  }
  DBUG_PRINT("info", ("unlocking servers_cache"));
1194
  rw_unlock(&THR_LOCK_servers);
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1195 1196 1197
  DBUG_RETURN(server);

}