sql_plugin.cc 28.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (C) 2005 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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

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

#include "mysql_priv.h"
#include <my_pthread.h>
#define REPORT_TO_LOG  1
#define REPORT_TO_USER 2

unknown's avatar
WL#3201  
unknown committed
22 23
extern struct st_mysql_plugin *mysqld_builtins[];

24 25
char *opt_plugin_dir_ptr;
char opt_plugin_dir[FN_REFLEN];
26 27 28 29
/*
  When you ad a new plugin type, add both a string and make sure that the 
  init and deinit array are correctly updated.
*/
unknown's avatar
unknown committed
30
const LEX_STRING plugin_type_names[MYSQL_MAX_PLUGIN_TYPE_NUM]=
31
{
unknown's avatar
unknown committed
32 33
  { C_STRING_WITH_LEN("UDF") },
  { C_STRING_WITH_LEN("STORAGE ENGINE") },
34 35
  { C_STRING_WITH_LEN("FTPARSER") },
  { C_STRING_WITH_LEN("DAEMON") }
36
};
unknown's avatar
unknown committed
37 38 39

plugin_type_init plugin_type_initialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
40
  0,ha_initialize_handlerton,0,0
unknown's avatar
unknown committed
41 42
};

unknown's avatar
unknown committed
43 44
plugin_type_init plugin_type_deinitialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
45
  0,ha_finalize_handlerton,0,0
unknown's avatar
unknown committed
46 47
};

48 49
static const char *plugin_interface_version_sym=
                   "_mysql_plugin_interface_version_";
50 51
static const char *sizeof_st_plugin_sym=
                   "_mysql_sizeof_struct_st_plugin_";
52
static const char *plugin_declarations_sym= "_mysql_plugin_declarations_";
unknown's avatar
unknown committed
53
static int min_plugin_interface_version= MYSQL_PLUGIN_INTERFACE_VERSION & ~0xFF;
54 55 56 57 58 59
/* Note that 'int version' must be the first field of every plugin
   sub-structure (plugin->info).
*/
static int min_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
  0x0000,
60
  MYSQL_HANDLERTON_INTERFACE_VERSION,
61 62
  MYSQL_FTPARSER_INTERFACE_VERSION,
  MYSQL_DAEMON_INTERFACE_VERSION
63 64 65 66
};
static int cur_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
  0x0000, /* UDF: not implemented */
unknown's avatar
unknown committed
67
  MYSQL_HANDLERTON_INTERFACE_VERSION,
68 69
  MYSQL_FTPARSER_INTERFACE_VERSION,
  MYSQL_DAEMON_INTERFACE_VERSION
70
};
71

72 73 74 75 76 77
static DYNAMIC_ARRAY plugin_dl_array;
static DYNAMIC_ARRAY plugin_array;
static HASH plugin_hash[MYSQL_MAX_PLUGIN_TYPE_NUM];
static rw_lock_t THR_LOCK_plugin;
static bool initialized= 0;

78 79
static int plugin_array_version=0;

80 81 82 83
/* prototypes */
my_bool plugin_register_builtin(struct st_mysql_plugin *plugin);
void plugin_load(void);

unknown's avatar
unknown committed
84
static struct st_plugin_dl *plugin_dl_find(const LEX_STRING *dl)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
{
  uint i;
  DBUG_ENTER("plugin_dl_find");
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
    struct st_plugin_dl *tmp= dynamic_element(&plugin_dl_array, i,
                                              struct st_plugin_dl *);
    if (tmp->ref_count &&
        ! my_strnncoll(files_charset_info,
                       (const uchar *)dl->str, dl->length,
                       (const uchar *)tmp->dl.str, tmp->dl.length))
      DBUG_RETURN(tmp);
  }
  DBUG_RETURN(0);
}


102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
static st_plugin_dl *plugin_dl_insert_or_reuse(struct st_plugin_dl *plugin_dl)
{
  uint i;
  DBUG_ENTER("plugin_dl_insert_or_reuse");
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
    struct st_plugin_dl *tmp= dynamic_element(&plugin_dl_array, i,
                                              struct st_plugin_dl *);
    if (! tmp->ref_count)
    {
      memcpy(tmp, plugin_dl, sizeof(struct st_plugin_dl));
      DBUG_RETURN(tmp);
    }
  }
  if (insert_dynamic(&plugin_dl_array, (gptr)plugin_dl))
    DBUG_RETURN(0);
  DBUG_RETURN(dynamic_element(&plugin_dl_array, plugin_dl_array.elements - 1,
                              struct st_plugin_dl *));
}

122 123
static inline void free_plugin_mem(struct st_plugin_dl *p)
{
124
#ifdef HAVE_DLOPEN
125 126
  if (p->handle)
    dlclose(p->handle);
127
#endif
128 129 130 131
  my_free(p->dl.str, MYF(MY_ALLOW_ZERO_PTR));
  if (p->version != MYSQL_PLUGIN_INTERFACE_VERSION)
    my_free((gptr)p->plugins, MYF(MY_ALLOW_ZERO_PTR));
}
132

unknown's avatar
unknown committed
133
static st_plugin_dl *plugin_dl_add(const LEX_STRING *dl, int report)
134 135 136
{
#ifdef HAVE_DLOPEN
  char dlpath[FN_REFLEN];
137
  uint plugin_dir_len, dummy_errors, dlpathlen;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  struct st_plugin_dl *tmp, plugin_dl;
  void *sym;
  DBUG_ENTER("plugin_dl_add");
  plugin_dir_len= strlen(opt_plugin_dir);
  /*
    Ensure that the dll doesn't have a path.
    This is done to ensure that only approved libraries from the
    plugin directory are used (to make this even remotely secure).
  */
  if (my_strchr(files_charset_info, dl->str, dl->str + dl->length, FN_LIBCHAR) ||
      dl->length > NAME_LEN ||
      plugin_dir_len + dl->length + 1 >= FN_REFLEN)
  {
    if (report & REPORT_TO_USER)
      my_error(ER_UDF_NO_PATHS, MYF(0));
    if (report & REPORT_TO_LOG)
      sql_print_error(ER(ER_UDF_NO_PATHS));
    DBUG_RETURN(0);
  }
  /* If this dll is already loaded just increase ref_count. */
  if ((tmp= plugin_dl_find(dl)))
  {
    tmp->ref_count++;
    DBUG_RETURN(tmp);
  }
163
  bzero(&plugin_dl, sizeof(plugin_dl));
164
  /* Compile dll path */
165 166 167
  dlpathlen=
    strxnmov(dlpath, sizeof(dlpath) - 1, opt_plugin_dir, "/", dl->str, NullS) -
    dlpath;
168 169 170 171
  plugin_dl.ref_count= 1;
  /* Open new dll handle */
  if (!(plugin_dl.handle= dlopen(dlpath, RTLD_NOW)))
  {
172 173 174 175 176 177 178
    const char *errmsg=dlerror();
    if (!strncmp(dlpath, errmsg, dlpathlen))
    { // if errmsg starts from dlpath, trim this prefix.
      errmsg+=dlpathlen;
      if (*errmsg == ':') errmsg++;
      if (*errmsg == ' ') errmsg++;
    }
179
    if (report & REPORT_TO_USER)
180
      my_error(ER_CANT_OPEN_LIBRARY, MYF(0), dlpath, errno, errmsg);
181
    if (report & REPORT_TO_LOG)
182
      sql_print_error(ER(ER_CANT_OPEN_LIBRARY), dlpath, errno, errmsg);
183 184 185 186 187
    DBUG_RETURN(0);
  }
  /* Determine interface version */
  if (!(sym= dlsym(plugin_dl.handle, plugin_interface_version_sym)))
  {
188
    free_plugin_mem(&plugin_dl);
189 190 191 192 193 194 195 196 197 198 199
    if (report & REPORT_TO_USER)
      my_error(ER_CANT_FIND_DL_ENTRY, MYF(0), plugin_interface_version_sym);
    if (report & REPORT_TO_LOG)
      sql_print_error(ER(ER_CANT_FIND_DL_ENTRY), plugin_interface_version_sym);
    DBUG_RETURN(0);
  }
  plugin_dl.version= *(int *)sym;
  /* Versioning */
  if (plugin_dl.version < min_plugin_interface_version ||
      (plugin_dl.version >> 8) > (MYSQL_PLUGIN_INTERFACE_VERSION >> 8))
  {
200
    free_plugin_mem(&plugin_dl);
201 202 203 204 205 206 207 208 209 210 211
    if (report & REPORT_TO_USER)
      my_error(ER_CANT_OPEN_LIBRARY, MYF(0), dlpath, 0,
               "plugin interface version mismatch");
    if (report & REPORT_TO_LOG)
      sql_print_error(ER(ER_CANT_OPEN_LIBRARY), dlpath, 0,
                      "plugin interface version mismatch");
    DBUG_RETURN(0);
  }
  /* Find plugin declarations */
  if (!(sym= dlsym(plugin_dl.handle, plugin_declarations_sym)))
  {
212
    free_plugin_mem(&plugin_dl);
213 214 215 216 217 218
    if (report & REPORT_TO_USER)
      my_error(ER_CANT_FIND_DL_ENTRY, MYF(0), plugin_declarations_sym);
    if (report & REPORT_TO_LOG)
      sql_print_error(ER(ER_CANT_FIND_DL_ENTRY), plugin_declarations_sym);
    DBUG_RETURN(0);
  }
219 220 221

  if (plugin_dl.version != MYSQL_PLUGIN_INTERFACE_VERSION)
  {
unknown's avatar
unknown committed
222 223
    int i;
    uint sizeof_st_plugin;
224
    struct st_mysql_plugin *old, *cur;
225
    char *ptr= (char *)sym;
226 227 228 229 230 231 232 233 234 235 236 237 238

    if ((sym= dlsym(plugin_dl.handle, sizeof_st_plugin_sym)))
      sizeof_st_plugin= *(int *)sym;
    else
    {
#ifdef ERROR_ON_NO_SIZEOF_PLUGIN_SYMBOL
      free_plugin_mem(&plugin_dl);
      if (report & REPORT_TO_USER)
        my_error(ER_CANT_FIND_DL_ENTRY, MYF(0), sizeof_st_plugin_sym);
      if (report & REPORT_TO_LOG)
        sql_print_error(ER(ER_CANT_FIND_DL_ENTRY), sizeof_st_plugin_sym);
      DBUG_RETURN(0);
#else
239 240 241 242
      /*
        When the following assert starts failing, we'll have to switch
        to the upper branch of the #ifdef
      */
243
      DBUG_ASSERT(min_plugin_interface_version == 0);
244
      sizeof_st_plugin= (int)offsetof(struct st_mysql_plugin, version);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
#endif
    }

    for (i= 0;
         ((struct st_mysql_plugin *)(ptr+i*sizeof_st_plugin))->info;
         i++)
      /* no op */;

    cur= (struct st_mysql_plugin*)
          my_malloc(i*sizeof(struct st_mysql_plugin), MYF(MY_ZEROFILL|MY_WME));
    if (!cur)
    {
      free_plugin_mem(&plugin_dl);
      if (report & REPORT_TO_USER)
        my_error(ER_OUTOFMEMORY, MYF(0), plugin_dl.dl.length);
      if (report & REPORT_TO_LOG)
        sql_print_error(ER(ER_OUTOFMEMORY), plugin_dl.dl.length);
      DBUG_RETURN(0);
    }
264 265 266 267 268
    /*
      All st_plugin fields not initialized in the plugin explicitly, are
      set to 0. It matches C standard behaviour for struct initializers that
      have less values than the struct definition.
    */
269 270 271
    for (i=0;
         (old=(struct st_mysql_plugin *)(ptr+i*sizeof_st_plugin))->info;
         i++)
272
      memcpy(cur+i, old, min(sizeof(cur[i]), sizeof_st_plugin));
273

274
    sym= cur;
275
  }
276
  plugin_dl.plugins= (struct st_mysql_plugin *)sym;
277

278 279 280 281
  /* Duplicate and convert dll name */
  plugin_dl.dl.length= dl->length * files_charset_info->mbmaxlen + 1;
  if (! (plugin_dl.dl.str= my_malloc(plugin_dl.dl.length, MYF(0))))
  {
282
    free_plugin_mem(&plugin_dl);
283 284 285 286 287 288 289 290 291 292 293
    if (report & REPORT_TO_USER)
      my_error(ER_OUTOFMEMORY, MYF(0), plugin_dl.dl.length);
    if (report & REPORT_TO_LOG)
      sql_print_error(ER(ER_OUTOFMEMORY), plugin_dl.dl.length);
    DBUG_RETURN(0);
  }
  plugin_dl.dl.length= copy_and_convert(plugin_dl.dl.str, plugin_dl.dl.length,
    files_charset_info, dl->str, dl->length, system_charset_info,
    &dummy_errors);
  plugin_dl.dl.str[plugin_dl.dl.length]= 0;
  /* Add this dll to array */
294
  if (! (tmp= plugin_dl_insert_or_reuse(&plugin_dl)))
295
  {
296
    free_plugin_mem(&plugin_dl);
297 298 299 300 301 302
    if (report & REPORT_TO_USER)
      my_error(ER_OUTOFMEMORY, MYF(0), sizeof(struct st_plugin_dl));
    if (report & REPORT_TO_LOG)
      sql_print_error(ER(ER_OUTOFMEMORY), sizeof(struct st_plugin_dl));
    DBUG_RETURN(0);
  }
303
  DBUG_RETURN(tmp);
304 305 306 307 308 309 310 311 312 313 314
#else
  DBUG_ENTER("plugin_dl_add");
  if (report & REPORT_TO_USER)
    my_error(ER_FEATURE_DISABLED, MYF(0), "plugin", "HAVE_DLOPEN");
  if (report & REPORT_TO_LOG)
    sql_print_error(ER(ER_FEATURE_DISABLED), "plugin", "HAVE_DLOPEN");
  DBUG_RETURN(0);
#endif
}


unknown's avatar
unknown committed
315
static void plugin_dl_del(const LEX_STRING *dl)
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
{
#ifdef HAVE_DLOPEN
  uint i;
  DBUG_ENTER("plugin_dl_del");
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
    struct st_plugin_dl *tmp= dynamic_element(&plugin_dl_array, i,
                                              struct st_plugin_dl *);
    if (tmp->ref_count &&
        ! my_strnncoll(files_charset_info,
                       (const uchar *)dl->str, dl->length,
                       (const uchar *)tmp->dl.str, tmp->dl.length))
    {
      /* Do not remove this element, unless no other plugin uses this dll. */
      if (! --tmp->ref_count)
      {
332
        free_plugin_mem(tmp);
333 334 335 336 337 338 339 340 341 342
        bzero(tmp, sizeof(struct st_plugin_dl));
      }
      break;
    }
  }
  DBUG_VOID_RETURN;
#endif
}


unknown's avatar
unknown committed
343
static struct st_plugin_int *plugin_find_internal(const LEX_STRING *name, int type)
344 345 346 347 348 349 350 351 352 353
{
  uint i;
  DBUG_ENTER("plugin_find_internal");
  if (! initialized)
    DBUG_RETURN(0);
  if (type == MYSQL_ANY_PLUGIN)
  {
    for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
    {
      struct st_plugin_int *plugin= (st_plugin_int *)
unknown's avatar
unknown committed
354
        hash_search(&plugin_hash[i], (const byte *)name->str, name->length);
355
      if (plugin)
356 357 358 359 360
        DBUG_RETURN(plugin);
    }
  }
  else
    DBUG_RETURN((st_plugin_int *)
unknown's avatar
unknown committed
361
        hash_search(&plugin_hash[type], (const byte *)name->str, name->length));
362 363 364 365
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
366
my_bool plugin_is_ready(const LEX_STRING *name, int type)
367 368 369 370 371 372 373 374 375 376 377 378 379
{
  my_bool rc= FALSE;
  struct st_plugin_int *plugin;
  DBUG_ENTER("plugin_is_ready");
  rw_rdlock(&THR_LOCK_plugin);
  if ((plugin= plugin_find_internal(name, type)) &&
      plugin->state == PLUGIN_IS_READY)
    rc= TRUE;
  rw_unlock(&THR_LOCK_plugin);
  DBUG_RETURN(rc);
}


unknown's avatar
unknown committed
380
struct st_plugin_int *plugin_lock(const LEX_STRING *name, int type)
381 382
{
  struct st_plugin_int *rc;
383
  DBUG_ENTER("plugin_lock");
384 385 386
  rw_wrlock(&THR_LOCK_plugin);
  if ((rc= plugin_find_internal(name, type)))
  {
387
    if (rc->state & (PLUGIN_IS_READY | PLUGIN_IS_UNINITIALIZED))
388 389 390 391 392 393 394 395 396
      rc->ref_count++;
    else
      rc= 0;
  }
  rw_unlock(&THR_LOCK_plugin);
  DBUG_RETURN(rc);
}


397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static st_plugin_int *plugin_insert_or_reuse(struct st_plugin_int *plugin)
{
  uint i;
  DBUG_ENTER("plugin_insert_or_reuse");
  for (i= 0; i < plugin_array.elements; i++)
  {
    struct st_plugin_int *tmp= dynamic_element(&plugin_array, i,
                                               struct st_plugin_int *);
    if (tmp->state == PLUGIN_IS_FREED)
    {
      memcpy(tmp, plugin, sizeof(struct st_plugin_int));
      DBUG_RETURN(tmp);
    }
  }
  if (insert_dynamic(&plugin_array, (gptr)plugin))
    DBUG_RETURN(0);
  DBUG_RETURN(dynamic_element(&plugin_array, plugin_array.elements - 1,
                              struct st_plugin_int *));
}

unknown's avatar
unknown committed
417
static my_bool plugin_add(const LEX_STRING *name, const LEX_STRING *dl, int report)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
{
  struct st_plugin_int tmp;
  struct st_mysql_plugin *plugin;
  DBUG_ENTER("plugin_add");
  if (plugin_find_internal(name, MYSQL_ANY_PLUGIN))
  {
    if (report & REPORT_TO_USER)
      my_error(ER_UDF_EXISTS, MYF(0), name->str);
    if (report & REPORT_TO_LOG)
      sql_print_error(ER(ER_UDF_EXISTS), name->str);
    DBUG_RETURN(TRUE);
  }
  if (! (tmp.plugin_dl= plugin_dl_add(dl, report)))
    DBUG_RETURN(TRUE);
  /* Find plugin by name */
  for (plugin= tmp.plugin_dl->plugins; plugin->info; plugin++)
  {
    uint name_len= strlen(plugin->name);
    if (plugin->type >= 0 && plugin->type < MYSQL_MAX_PLUGIN_TYPE_NUM &&
        ! my_strnncoll(system_charset_info,
                       (const uchar *)name->str, name->length,
                       (const uchar *)plugin->name,
                       name_len))
    {
442 443 444 445 446 447 448 449
      struct st_plugin_int *tmp_plugin_ptr;
      if (*(int*)plugin->info <
          min_plugin_info_interface_version[plugin->type] ||
          ((*(int*)plugin->info) >> 8) >
          (cur_plugin_info_interface_version[plugin->type] >> 8))
      {
        char buf[256];
        strxnmov(buf, sizeof(buf) - 1, "API version for ",
unknown's avatar
unknown committed
450 451
                 plugin_type_names[plugin->type].str,
                 " plugin is too different", NullS);
452 453 454 455 456 457
        if (report & REPORT_TO_USER)
          my_error(ER_CANT_OPEN_LIBRARY, MYF(0), dl->str, 0, buf);
        if (report & REPORT_TO_LOG)
          sql_print_error(ER(ER_CANT_OPEN_LIBRARY), dl->str, 0, buf);
        goto err;
      }
458 459 460 461 462
      tmp.plugin= plugin;
      tmp.name.str= (char *)plugin->name;
      tmp.name.length= name_len;
      tmp.ref_count= 0;
      tmp.state= PLUGIN_IS_UNINITIALIZED;
463 464
      if (! (tmp_plugin_ptr= plugin_insert_or_reuse(&tmp)))
        goto err;
465
      plugin_array_version++;
466
      if (my_hash_insert(&plugin_hash[plugin->type], (byte*)tmp_plugin_ptr))
467
      {
468
        tmp_plugin_ptr->state= PLUGIN_IS_FREED;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
        goto err;
      }
      DBUG_RETURN(FALSE);
    }
  }
  if (report & REPORT_TO_USER)
    my_error(ER_CANT_FIND_DL_ENTRY, MYF(0), name->str);
  if (report & REPORT_TO_LOG)
    sql_print_error(ER(ER_CANT_FIND_DL_ENTRY), name->str);
err:
  plugin_dl_del(dl);
  DBUG_RETURN(TRUE);
}


unknown's avatar
unknown committed
484
void plugin_deinitialize(struct st_plugin_int *plugin)
485
{
486

unknown's avatar
unknown committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  if (plugin->plugin->status_vars)
  {
#ifdef FIX_LATER
    /*
      We have a problem right now where we can not prepend without
      breaking backwards compatibility. We will fix this shortly so
      that engines have "use names" and we wil use those for
      CREATE TABLE, and use the plugin name then for adding automatic
      variable names.
    */
    SHOW_VAR array[2]= {
      {plugin->plugin->name, (char*)plugin->plugin->status_vars, SHOW_ARRAY},
      {0, 0, SHOW_UNDEF}
    };
    remove_status_vars(array);
#else
    remove_status_vars(plugin->plugin->status_vars);
#endif /* FIX_LATER */
  }

507 508 509 510 511
  if (plugin_type_deinitialize[plugin->plugin->type])
  {
    if ((*plugin_type_deinitialize[plugin->plugin->type])(plugin))
    {
      sql_print_error("Plugin '%s' of type %s failed deinitialization",
512
                      plugin->name.str, plugin_type_names[plugin->plugin->type].str);
513 514 515
    } 
  }
  else if (plugin->plugin->deinit)
unknown's avatar
unknown committed
516 517
  {
    DBUG_PRINT("info", ("Deinitializing plugin: '%s'", plugin->name.str));
518
    if (plugin->plugin->deinit(plugin))
519
    {
unknown's avatar
unknown committed
520 521
      DBUG_PRINT("warning", ("Plugin '%s' deinit function returned error.",
                             plugin->name.str));
522
    }
unknown's avatar
unknown committed
523 524
  }
  plugin->state= PLUGIN_IS_UNINITIALIZED;
525 526 527
}


unknown's avatar
unknown committed
528 529 530 531 532 533 534 535 536 537
static void plugin_del(struct st_plugin_int *plugin)
{
  DBUG_ENTER("plugin_del(plugin)");
  hash_delete(&plugin_hash[plugin->plugin->type], (byte*)plugin);
  plugin_dl_del(&plugin->plugin_dl->dl);
  plugin->state= PLUGIN_IS_FREED;
  plugin_array_version++;
  DBUG_VOID_RETURN;
}

538 539 540
static void plugin_del(const LEX_STRING *name)
{
  struct st_plugin_int *plugin;
unknown's avatar
unknown committed
541
  DBUG_ENTER("plugin_del(name)");
542
  if ((plugin= plugin_find_internal(name, MYSQL_ANY_PLUGIN)))
unknown's avatar
unknown committed
543
    plugin_del(plugin);
544 545 546 547 548
  DBUG_VOID_RETURN;
}

void plugin_unlock(struct st_plugin_int *plugin)
{
549
  DBUG_ENTER("plugin_unlock");
550 551 552 553 554
  rw_wrlock(&THR_LOCK_plugin);
  DBUG_ASSERT(plugin && plugin->ref_count);
  plugin->ref_count--;
  if (plugin->state == PLUGIN_IS_DELETED && ! plugin->ref_count)
  {
unknown's avatar
unknown committed
555 556
    plugin_deinitialize(plugin);
    plugin_del(plugin);
557 558 559 560 561 562
  }
  rw_unlock(&THR_LOCK_plugin);
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
563 564 565
static int plugin_initialize(struct st_plugin_int *plugin)
{
  DBUG_ENTER("plugin_initialize");
566

567 568 569 570 571
  if (plugin_type_initialize[plugin->plugin->type])
  {
    if ((*plugin_type_initialize[plugin->plugin->type])(plugin))
    {
      sql_print_error("Plugin '%s' registration as a %s failed.",
572
                      plugin->name.str, plugin_type_names[plugin->plugin->type].str);
573 574 575 576 577
      goto err;
    }
  }
  else if (plugin->plugin->init)
  {
578
    if (plugin->plugin->init(plugin))
579 580 581 582 583 584 585 586 587
    {
      sql_print_error("Plugin '%s' init function returned error.",
                      plugin->name.str);
      goto err;
    }
  }

  plugin->state= PLUGIN_IS_READY;

588 589 590
  if (plugin->plugin->status_vars)
  {
#ifdef FIX_LATER
unknown's avatar
unknown committed
591
    /*
592
      We have a problem right now where we can not prepend without
unknown's avatar
unknown committed
593
      breaking backwards compatibility. We will fix this shortly so
594 595 596 597 598
      that engines have "use names" and we wil use those for
      CREATE TABLE, and use the plugin name then for adding automatic
      variable names.
    */
    SHOW_VAR array[2]= {
unknown's avatar
unknown committed
599
      {plugin->plugin->name, (char*)plugin->plugin->status_vars, SHOW_ARRAY},
600 601 602 603 604 605 606 607 608
      {0, 0, SHOW_UNDEF}
    };
    if (add_status_vars(array)) // add_status_vars makes a copy
      goto err;
#else
    add_status_vars(plugin->plugin->status_vars); // add_status_vars makes a copy
#endif /* FIX_LATER */
  }

unknown's avatar
unknown committed
609 610 611 612 613
  DBUG_RETURN(0);
err:
  DBUG_RETURN(1);
}

614 615 616 617 618 619 620 621 622
static byte *get_hash_key(const byte *buff, uint *length,
                   my_bool not_used __attribute__((unused)))
{
  struct st_plugin_int *plugin= (st_plugin_int *)buff;
  *length= (uint)plugin->name.length;
  return((byte *)plugin->name.str);
}


623 624 625
/*
  The logic is that we first load and initialize all compiled in plugins.
  From there we load up the dynamic types (assuming we have not been told to
unknown's avatar
unknown committed
626
  skip this part).
627 628 629 630

  Finally we inializie everything, aka the dynamic that have yet to initialize.
*/
int plugin_init(int skip_dynamic_loading)
unknown's avatar
unknown committed
631
{
632
  uint i;
unknown's avatar
WL#3201  
unknown committed
633 634
  struct st_mysql_plugin **builtins;
  struct st_mysql_plugin *plugin;
unknown's avatar
unknown committed
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  DBUG_ENTER("plugin_init");

  if (initialized)
    DBUG_RETURN(0);

  my_rwlock_init(&THR_LOCK_plugin, NULL);

  if (my_init_dynamic_array(&plugin_dl_array,
                            sizeof(struct st_plugin_dl),16,16) ||
      my_init_dynamic_array(&plugin_array,
                            sizeof(struct st_plugin_int),16,16))
    goto err;

  for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
  {
    if (hash_init(&plugin_hash[i], system_charset_info, 16, 0, 0,
                  get_hash_key, NULL, 0))
      goto err;
  }
unknown's avatar
unknown committed
654

unknown's avatar
unknown committed
655
  /*
656 657
    First we register builtin plugins
  */
unknown's avatar
WL#3201  
unknown committed
658 659 660 661
  for (builtins= mysqld_builtins; *builtins; builtins++)
  {
    for (plugin= *builtins; plugin->info; plugin++)
    {
662 663 664 665 666 667 668 669 670 671
//      if (!(strcmp(plugin->name, "MyISAM")))
      {
        if (plugin_register_builtin(plugin))
          goto err;
        struct st_plugin_int *tmp= dynamic_element(&plugin_array,
                                                   plugin_array.elements-1,
                                                   struct st_plugin_int *);
        if (plugin_initialize(tmp))
          goto err;
      }
unknown's avatar
WL#3201  
unknown committed
672 673
    }
  }
674

675 676 677 678
  /* Register all dynamic plugins */
  if (!skip_dynamic_loading)
    plugin_load();

unknown's avatar
unknown committed
679 680
  initialized= 1;

681 682 683 684 685 686 687 688 689 690
  /*
    Now we initialize all remaining plugins
  */
  for (i= 0; i < plugin_array.elements; i++)
  {
    struct st_plugin_int *tmp= dynamic_element(&plugin_array, i,
                                               struct st_plugin_int *);
    if (tmp->state == PLUGIN_IS_UNINITIALIZED)
    {
      if (plugin_initialize(tmp))
unknown's avatar
unknown committed
691 692 693 694
      {
        plugin_deinitialize(tmp);
        plugin_del(tmp);
      }
695 696 697
    }
  }

unknown's avatar
unknown committed
698
  DBUG_RETURN(0);
699

unknown's avatar
unknown committed
700 701 702 703 704 705 706 707 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
err:
  DBUG_RETURN(1);
}


my_bool plugin_register_builtin(struct st_mysql_plugin *plugin)
{
  struct st_plugin_int tmp;
  DBUG_ENTER("plugin_register_builtin");

  tmp.plugin= plugin;
  tmp.name.str= (char *)plugin->name;
  tmp.name.length= strlen(plugin->name);
  tmp.state= PLUGIN_IS_UNINITIALIZED;

  /* Cannot be unloaded */
  tmp.ref_count= 1;
  tmp.plugin_dl= 0;

  if (insert_dynamic(&plugin_array, (gptr)&tmp))
    DBUG_RETURN(1);

  if (my_hash_insert(&plugin_hash[plugin->type],
                     (byte*)dynamic_element(&plugin_array,
                                            plugin_array.elements - 1,
                                            struct st_plugin_int *)))
    DBUG_RETURN(1);

  DBUG_RETURN(0);
}


void plugin_load(void)
733 734 735 736
{
  TABLE_LIST tables;
  TABLE *table;
  READ_RECORD read_record_info;
737
  int error;
738
  MEM_ROOT mem;
unknown's avatar
unknown committed
739 740
  THD *new_thd;
  DBUG_ENTER("plugin_load");
741

unknown's avatar
unknown committed
742
  if (!(new_thd= new THD))
743 744 745 746 747 748
  {
    sql_print_error("Can't allocate memory for plugin structures");
    delete new_thd;
    DBUG_VOID_RETURN;
  }
  init_sql_alloc(&mem, 1024, 0);
unknown's avatar
unknown committed
749
  new_thd->thread_stack= (char*) &tables;
750 751 752 753 754 755 756 757 758 759
  new_thd->store_globals();
  new_thd->db= my_strdup("mysql", MYF(0));
  new_thd->db_length= 5;
  bzero((gptr)&tables, sizeof(tables));
  tables.alias= tables.table_name= (char*)"plugin";
  tables.lock_type= TL_READ;
  tables.db= new_thd->db;
  if (simple_open_n_lock_tables(new_thd, &tables))
  {
    DBUG_PRINT("error",("Can't open plugin table"));
760
    sql_print_error("Can't open the mysql.plugin table. Please run the mysql_upgrade script to create it.");
761 762 763 764
    goto end;
  }
  table= tables.table;
  init_read_record(&read_record_info, new_thd, table, NULL, 1, 0);
765
  table->use_all_columns();
766 767 768
  while (!(error= read_record_info.read_record(&read_record_info)))
  {
    DBUG_PRINT("info", ("init plugin record"));
unknown's avatar
unknown committed
769 770 771 772 773 774 775
    String str_name, str_dl;
    get_field(&mem, table->field[0], &str_name);
    get_field(&mem, table->field[1], &str_dl);
    
    LEX_STRING name= {(char *)str_name.ptr(), str_name.length()};
    LEX_STRING dl= {(char *)str_dl.ptr(), str_dl.length()};

776
    if (plugin_add(&name, &dl, REPORT_TO_LOG))
unknown's avatar
unknown committed
777 778
      sql_print_warning("Couldn't load plugin named '%s' with soname '%s'.",
                        str_name.c_ptr(), str_dl.c_ptr());
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
  }
  if (error > 0)
    sql_print_error(ER(ER_GET_ERRNO), my_errno);
  end_read_record(&read_record_info);
  new_thd->version--; // Force close to free memory
end:
  free_root(&mem, MYF(0));
  close_thread_tables(new_thd);
  delete new_thd;
  /* Remember that we don't have a THD */
  my_pthread_setspecific_ptr(THR_THD, 0);
  DBUG_VOID_RETURN;
}


794
void plugin_shutdown(void)
795 796
{
  uint i;
797 798 799 800 801 802 803 804 805
  DBUG_ENTER("plugin_shutdown");

  /*
    We loop through all plugins and call deinit() if they have one.
  */
  for (i= 0; i < plugin_array.elements; i++)
  {
    struct st_plugin_int *tmp= dynamic_element(&plugin_array, i,
                                               struct st_plugin_int *);
unknown's avatar
unknown committed
806
    plugin_deinitialize(tmp);
807 808 809

  }

810 811 812 813 814 815 816
  for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
    hash_free(&plugin_hash[i]);
  delete_dynamic(&plugin_array);
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
    struct st_plugin_dl *tmp= dynamic_element(&plugin_dl_array, i,
                                              struct st_plugin_dl *);
817
    free_plugin_mem(tmp);
818 819 820 821 822 823 824 825 826 827 828
  }
  delete_dynamic(&plugin_dl_array);
  if (initialized)
  {
    initialized= 0;
    rwlock_destroy(&THR_LOCK_plugin);
  }
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
829
my_bool mysql_install_plugin(THD *thd, const LEX_STRING *name, const LEX_STRING *dl)
830 831 832 833 834 835
{
  TABLE_LIST tables;
  TABLE *table;
  int error;
  struct st_plugin_int *tmp;
  DBUG_ENTER("mysql_install_plugin");
unknown's avatar
unknown committed
836

837 838 839 840 841
  bzero(&tables, sizeof(tables));
  tables.db= (char *)"mysql";
  tables.table_name= tables.alias= (char *)"plugin";
  if (check_table_access(thd, INSERT_ACL, &tables, 0))
    DBUG_RETURN(TRUE);
842

unknown's avatar
unknown committed
843 844 845 846
  /* need to open before acquiring THR_LOCK_plugin or it will deadlock */
  if (! (table = open_ltable(thd, &tables, TL_WRITE)))
    DBUG_RETURN(TRUE);

847 848 849 850
  rw_wrlock(&THR_LOCK_plugin);
  if (plugin_add(name, dl, REPORT_TO_USER))
    goto err;
  tmp= plugin_find_internal(name, MYSQL_ANY_PLUGIN);
851

unknown's avatar
unknown committed
852
  if (plugin_initialize(tmp))
853
  {
unknown's avatar
unknown committed
854 855
    my_error(ER_CANT_INITIALIZE_UDF, MYF(0), name->str,
             "Plugin initialization function failed.");
unknown's avatar
unknown committed
856
    goto deinit;
857
  }
unknown's avatar
unknown committed
858

unknown's avatar
unknown committed
859
  table->use_all_columns();
860 861 862
  restore_record(table, s->default_values);
  table->field[0]->store(name->str, name->length, system_charset_info);
  table->field[1]->store(dl->str, dl->length, files_charset_info);
863
  error= table->file->ha_write_row(table->record[0]);
864 865 866 867 868
  if (error)
  {
    table->file->print_error(error, MYF(0));
    goto deinit;
  }
869

870 871 872
  rw_unlock(&THR_LOCK_plugin);
  DBUG_RETURN(FALSE);
deinit:
unknown's avatar
unknown committed
873 874
  plugin_deinitialize(tmp);
  plugin_del(tmp);
unknown's avatar
unknown committed
875
err:
876 877 878 879 880
  rw_unlock(&THR_LOCK_plugin);
  DBUG_RETURN(TRUE);
}


unknown's avatar
unknown committed
881
my_bool mysql_uninstall_plugin(THD *thd, const LEX_STRING *name)
882 883 884 885 886
{
  TABLE *table;
  TABLE_LIST tables;
  struct st_plugin_int *plugin;
  DBUG_ENTER("mysql_uninstall_plugin");
unknown's avatar
unknown committed
887 888 889 890 891 892 893 894 895

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

  /* need to open before acquiring THR_LOCK_plugin or it will deadlock */
  if (! (table= open_ltable(thd, &tables, TL_WRITE)))
    DBUG_RETURN(TRUE);

896
  rw_wrlock(&THR_LOCK_plugin);
unknown's avatar
unknown committed
897
  if (!(plugin= plugin_find_internal(name, MYSQL_ANY_PLUGIN)))
898 899 900 901
  {
    my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "PLUGIN", name->str);
    goto err;
  }
unknown's avatar
unknown committed
902 903 904 905 906 907 908
  if (!plugin->plugin_dl)
  {
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
                 "Built-in plugins cannot be deleted,.");
    my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "PLUGIN", name->str);
    goto err;
  }
909

unknown's avatar
unknown committed
910 911 912 913
  if (plugin->ref_count)
  {
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
                 "Plugin is busy and will be uninstalled on shutdown");
unknown's avatar
WL#3201  
unknown committed
914
    plugin->state= PLUGIN_IS_DELETED;
unknown's avatar
unknown committed
915 916 917 918 919 920
  }
  else
  {
    plugin_deinitialize(plugin);
    plugin_del(plugin);
  }
unknown's avatar
unknown committed
921

922
  table->use_all_columns();
923 924 925 926 927 928 929
  table->field[0]->store(name->str, name->length, system_charset_info);
  if (! table->file->index_read_idx(table->record[0], 0,
                                    (byte *)table->field[0]->ptr,
                                    table->key_info[0].key_length,
                                    HA_READ_KEY_EXACT))
  {
    int error;
930
    if ((error= table->file->ha_delete_row(table->record[0])))
931 932 933 934 935 936 937 938 939 940 941
    {
      table->file->print_error(error, MYF(0));
      goto err;
    }
  }
  rw_unlock(&THR_LOCK_plugin);
  DBUG_RETURN(FALSE);
err:
  rw_unlock(&THR_LOCK_plugin);
  DBUG_RETURN(TRUE);
}
unknown's avatar
unknown committed
942

943 944
my_bool plugin_foreach_with_mask(THD *thd, plugin_foreach_func *func,
                       int type, uint state_mask, void *arg)
unknown's avatar
unknown committed
945
{
946 947 948
  uint idx, total;
  struct st_plugin_int *plugin, **plugins;
  int version=plugin_array_version;
949 950 951
  DBUG_ENTER("plugin_foreach_with_mask");

  state_mask= ~state_mask; // do it only once
952

953
  rw_rdlock(&THR_LOCK_plugin);
954 955 956 957 958 959 960
  total= type == MYSQL_ANY_PLUGIN ? plugin_array.elements
                                  : plugin_hash[type].records;
  /*
    Do the alloca out here in case we do have a working alloca:
	leaving the nested stack frame invalidates alloca allocation.
  */
  plugins=(struct st_plugin_int **)my_alloca(total*sizeof(*plugins));
unknown's avatar
unknown committed
961 962
  if (type == MYSQL_ANY_PLUGIN)
  {
963
    for (idx= 0; idx < total; idx++)
unknown's avatar
unknown committed
964 965
    {
      plugin= dynamic_element(&plugin_array, idx, struct st_plugin_int *);
966
      plugins[idx]= !(plugin->state & state_mask) ? plugin : NULL;
unknown's avatar
unknown committed
967 968 969 970
    }
  }
  else
  {
971
    HASH *hash= plugin_hash + type;
972
    for (idx= 0; idx < total; idx++)
unknown's avatar
unknown committed
973 974
    {
      plugin= (struct st_plugin_int *) hash_element(hash, idx);
975
      if (plugin->state & state_mask)
976
        continue;
977
      plugins[idx]= !(plugin->state & state_mask) ? plugin : NULL;
unknown's avatar
unknown committed
978 979 980
    }
  }
  rw_unlock(&THR_LOCK_plugin);
981 982 983 984 985 986 987

  for (idx= 0; idx < total; idx++)
  {
    if (unlikely(version != plugin_array_version))
    {
      rw_rdlock(&THR_LOCK_plugin);
      for (uint i=idx; i < total; i++)
988
        if (plugins[i]->state & state_mask)
989 990 991
          plugins[i]=0;
      rw_unlock(&THR_LOCK_plugin);
    }
unknown's avatar
bugfix  
unknown committed
992
    plugin= plugins[idx];
993 994 995 996 997
    if (plugin && func(thd, plugin, arg))
        goto err;
  }

  my_afree(plugins);
unknown's avatar
unknown committed
998 999
  DBUG_RETURN(FALSE);
err:
1000
  my_afree(plugins);
unknown's avatar
unknown committed
1001 1002
  DBUG_RETURN(TRUE);
}
1003