sql_plugin.cc 92.6 KB
Newer Older
1 2 3 4
/* 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
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16 17

   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>
unknown's avatar
unknown committed
18
#include <my_getopt.h>
19 20 21
#define REPORT_TO_LOG  1
#define REPORT_TO_USER 2

unknown's avatar
unknown committed
22 23 24 25 26 27 28 29
#ifdef DBUG_OFF
#define plugin_ref_to_int(A) A
#define plugin_int_to_ref(A) A
#else
#define plugin_ref_to_int(A) (A ? A[0] : NULL)
#define plugin_int_to_ref(A) &(A)
#endif

unknown's avatar
unknown committed
30 31
extern struct st_mysql_plugin *mysqld_builtins[];

unknown's avatar
unknown committed
32
char *opt_plugin_load= NULL;
33 34
char *opt_plugin_dir_ptr;
char opt_plugin_dir[FN_REFLEN];
35
/*
unknown's avatar
unknown committed
36
  When you ad a new plugin type, add both a string and make sure that the
37 38
  init and deinit array are correctly updated.
*/
unknown's avatar
unknown committed
39
const LEX_STRING plugin_type_names[MYSQL_MAX_PLUGIN_TYPE_NUM]=
40
{
unknown's avatar
unknown committed
41 42
  { C_STRING_WITH_LEN("UDF") },
  { C_STRING_WITH_LEN("STORAGE ENGINE") },
43
  { C_STRING_WITH_LEN("FTPARSER") },
unknown's avatar
unknown committed
44 45
  { C_STRING_WITH_LEN("DAEMON") },
  { C_STRING_WITH_LEN("INFORMATION SCHEMA") }
46
};
unknown's avatar
unknown committed
47

unknown's avatar
unknown committed
48 49 50 51
extern int initialize_schema_table(st_plugin_int *plugin);
extern int finalize_schema_table(st_plugin_int *plugin);

/*
unknown's avatar
unknown committed
52
  The number of elements in both plugin_type_initialize and
unknown's avatar
unknown committed
53 54
  plugin_type_deinitialize should equal to the number of plugins
  defined.
unknown's avatar
unknown committed
55
*/
unknown's avatar
unknown committed
56 57
plugin_type_init plugin_type_initialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
unknown's avatar
unknown committed
58
  0,ha_initialize_handlerton,0,0,initialize_schema_table
unknown's avatar
unknown committed
59 60
};

unknown's avatar
unknown committed
61 62
plugin_type_init plugin_type_deinitialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
unknown's avatar
unknown committed
63
  0,ha_finalize_handlerton,0,0,finalize_schema_table
unknown's avatar
unknown committed
64 65
};

unknown's avatar
unknown committed
66
#ifdef HAVE_DLOPEN
67 68
static const char *plugin_interface_version_sym=
                   "_mysql_plugin_interface_version_";
69 70
static const char *sizeof_st_plugin_sym=
                   "_mysql_sizeof_struct_st_plugin_";
71
static const char *plugin_declarations_sym= "_mysql_plugin_declarations_";
unknown's avatar
unknown committed
72
static int min_plugin_interface_version= MYSQL_PLUGIN_INTERFACE_VERSION & ~0xFF;
unknown's avatar
unknown committed
73 74
#endif

75 76 77 78 79 80
/* 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,
81
  MYSQL_HANDLERTON_INTERFACE_VERSION,
82
  MYSQL_FTPARSER_INTERFACE_VERSION,
unknown's avatar
unknown committed
83 84
  MYSQL_DAEMON_INTERFACE_VERSION,
  MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION
85 86 87 88
};
static int cur_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
  0x0000, /* UDF: not implemented */
unknown's avatar
unknown committed
89
  MYSQL_HANDLERTON_INTERFACE_VERSION,
90
  MYSQL_FTPARSER_INTERFACE_VERSION,
unknown's avatar
unknown committed
91 92
  MYSQL_DAEMON_INTERFACE_VERSION,
  MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION
93
};
94

unknown's avatar
unknown committed
95 96 97 98 99 100 101
static bool initialized= 0;

/*
  A mutex LOCK_plugin must be acquired before accessing the
  following variables/structures.
  We are always manipulating ref count, so a rwlock here is unneccessary.
*/
unknown's avatar
unknown committed
102
pthread_mutex_t LOCK_plugin;
103 104 105
static DYNAMIC_ARRAY plugin_dl_array;
static DYNAMIC_ARRAY plugin_array;
static HASH plugin_hash[MYSQL_MAX_PLUGIN_TYPE_NUM];
unknown's avatar
unknown committed
106
static bool reap_needed= false;
107 108
static int plugin_array_version=0;

unknown's avatar
unknown committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/*
  write-lock on LOCK_system_variables_hash is required before modifying
  the following variables/structures
*/
static MEM_ROOT plugin_mem_root;
static uint global_variables_dynamic_size= 0;
static HASH bookmark_hash;


/*
  hidden part of opaque value passed to variable check functions.
  Used to provide a object-like structure to non C++ consumers.
*/
struct st_item_value_holder : public st_mysql_value
{
  Item *item;
};


/*
  stored in bookmark_hash, this structure is never removed from the
unknown's avatar
unknown committed
130
  hash and is used to mark a single offset for a thd local variable
unknown's avatar
unknown committed
131 132
  even if plugins have been uninstalled and reinstalled, repeatedly.
  This structure is allocated from plugin_mem_root.
unknown's avatar
unknown committed
133

unknown's avatar
unknown committed
134 135 136 137
  The key format is as follows:
    1 byte         - variable type code
    name_len bytes - variable name
    '\0'           - end of key
unknown's avatar
unknown committed
138 139 140 141 142 143
*/
struct st_bookmark
{
  uint name_len;
  int offset;
  uint version;
unknown's avatar
unknown committed
144
  char key[1];
unknown's avatar
unknown committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
};


/*
  skeleton of a plugin variable - portion of structure common to all.
*/
struct st_mysql_sys_var
{
  MYSQL_PLUGIN_VAR_HEADER;
};


/*
  sys_var class for access to all plugin variables visible to the user
*/
class sys_var_pluginvar: public sys_var
{
public:
  struct st_plugin_int *plugin;
  struct st_mysql_sys_var *plugin_var;

  static void *operator new(size_t size, MEM_ROOT *mem_root)
  { return (void*) alloc_root(mem_root, (uint) size); }
  static void operator delete(void *ptr_arg,size_t size)
  { TRASH(ptr_arg, size); }

  sys_var_pluginvar(const char *name_arg,
                    struct st_mysql_sys_var *plugin_var_arg)
unknown's avatar
unknown committed
173
    :sys_var(name_arg), plugin_var(plugin_var_arg) {}
unknown's avatar
unknown committed
174 175 176 177 178 179
  sys_var_pluginvar *cast_pluginvar() { return this; }
  bool is_readonly() const { return plugin_var->flags & PLUGIN_VAR_READONLY; }
  bool check_type(enum_var_type type)
  { return !(plugin_var->flags & PLUGIN_VAR_THDLOCAL) && type != OPT_GLOBAL; }
  bool check_update_type(Item_result type);
  SHOW_TYPE show_type();
unknown's avatar
unknown committed
180
  uchar* real_value_ptr(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
181
  TYPELIB* plugin_var_typelib(void);
unknown's avatar
unknown committed
182
  uchar* value_ptr(THD *thd, enum_var_type type, LEX_STRING *base);
unknown's avatar
unknown committed
183 184 185 186 187 188
  bool check(THD *thd, set_var *var);
  void set_default(THD *thd, enum_var_type type);
  bool update(THD *thd, set_var *var);
};


189
/* prototypes */
unknown's avatar
unknown committed
190 191 192
static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv);
static bool plugin_load_list(MEM_ROOT *tmp_root, int *argc, char **argv,
                             const char *list);
unknown's avatar
unknown committed
193 194 195 196
static int test_plugin_options(MEM_ROOT *, struct st_plugin_int *,
                               int *, char **, my_bool);
static bool register_builtin(struct st_mysql_plugin *, struct st_plugin_int *,
                             struct st_plugin_int **);
unknown's avatar
unknown committed
197 198
static void unlock_variables(THD *thd, struct system_variables *vars);
static void cleanup_variables(THD *thd, struct system_variables *vars);
199
static void plugin_vars_free_values(sys_var *vars);
unknown's avatar
unknown committed
200 201 202 203 204 205 206 207 208 209 210 211 212
static void plugin_opt_set_limits(struct my_option *options,
                                  const struct st_mysql_sys_var *opt);
#define my_intern_plugin_lock(A,B) intern_plugin_lock(A,B CALLER_INFO)
#define my_intern_plugin_lock_ci(A,B) intern_plugin_lock(A,B ORIG_CALLER_INFO)
static plugin_ref intern_plugin_lock(LEX *lex, plugin_ref plugin
                                     CALLER_INFO_PROTO);
static void intern_plugin_unlock(LEX *lex, plugin_ref plugin);
static void reap_plugins(void);


/* declared in set_var.cc */
extern sys_var *intern_find_sys_var(const char *str, uint length, bool no_error);

unknown's avatar
unknown committed
213 214 215 216 217
#ifdef EMBEDDED_LIBRARY
/* declared in sql_base.cc */
extern bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists);
#endif /* EMBEDDED_LIBRARY */

unknown's avatar
unknown committed
218 219 220 221 222 223 224 225 226 227 228 229 230

/****************************************************************************
  Value type thunks, allows the C world to play in the C++ world
****************************************************************************/

static int item_value_type(struct st_mysql_value *value)
{
  switch (((st_item_value_holder*)value)->item->result_type()) {
  case INT_RESULT:
    return MYSQL_VALUE_TYPE_INT;
  case REAL_RESULT:
    return MYSQL_VALUE_TYPE_REAL;
  default:
unknown's avatar
unknown committed
231
    return MYSQL_VALUE_TYPE_STRING;
unknown's avatar
unknown committed
232 233 234
  }
}

unknown's avatar
unknown committed
235
static const char *item_val_str(struct st_mysql_value *value,
unknown's avatar
unknown committed
236 237 238 239 240 241 242 243
                                char *buffer, int *length)
{
  String str(buffer, *length, system_charset_info), *res;
  if (!(res= ((st_item_value_holder*)value)->item->val_str(&str)))
    return NULL;
  *length= res->length();
  if (res->c_ptr_quick() == buffer)
    return buffer;
unknown's avatar
unknown committed
244

unknown's avatar
unknown committed
245
  /*
unknown's avatar
unknown committed
246
    Lets be nice and create a temporary string since the
unknown's avatar
unknown committed
247 248 249 250 251 252
    buffer was too small
  */
  return current_thd->strmake(res->c_ptr_quick(), res->length());
}


unknown's avatar
unknown committed
253
static int item_val_int(struct st_mysql_value *value, long long *buf)
unknown's avatar
unknown committed
254 255
{
  Item *item= ((st_item_value_holder*)value)->item;
unknown's avatar
unknown committed
256
  *buf= item->val_int();
unknown's avatar
unknown committed
257 258 259 260 261 262
  if (item->is_null())
    return 1;
  return 0;
}


unknown's avatar
unknown committed
263
static int item_val_real(struct st_mysql_value *value, double *buf)
unknown's avatar
unknown committed
264 265
{
  Item *item= ((st_item_value_holder*)value)->item;
unknown's avatar
unknown committed
266
  *buf= item->val_real();
unknown's avatar
unknown committed
267 268 269 270 271 272 273 274 275
  if (item->is_null())
    return 1;
  return 0;
}


/****************************************************************************
  Plugin support code
****************************************************************************/
276

unknown's avatar
unknown committed
277 278
#ifdef HAVE_DLOPEN

unknown's avatar
unknown committed
279
static struct st_plugin_dl *plugin_dl_find(const LEX_STRING *dl)
280 281
{
  uint i;
unknown's avatar
unknown committed
282
  struct st_plugin_dl *tmp;
283 284 285
  DBUG_ENTER("plugin_dl_find");
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
unknown's avatar
unknown committed
286
    tmp= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
287 288 289 290 291 292 293 294 295 296
    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);
}


297 298 299
static st_plugin_dl *plugin_dl_insert_or_reuse(struct st_plugin_dl *plugin_dl)
{
  uint i;
unknown's avatar
unknown committed
300
  struct st_plugin_dl *tmp;
301 302 303
  DBUG_ENTER("plugin_dl_insert_or_reuse");
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
unknown's avatar
unknown committed
304
    tmp= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
305 306 307 308 309 310
    if (! tmp->ref_count)
    {
      memcpy(tmp, plugin_dl, sizeof(struct st_plugin_dl));
      DBUG_RETURN(tmp);
    }
  }
311
  if (insert_dynamic(&plugin_dl_array, (uchar*)&plugin_dl))
312
    DBUG_RETURN(0);
unknown's avatar
unknown committed
313 314
  tmp= *dynamic_element(&plugin_dl_array, plugin_dl_array.elements - 1,
                        struct st_plugin_dl **)=
unknown's avatar
unknown committed
315
      (struct st_plugin_dl *) memdup_root(&plugin_mem_root, (uchar*)plugin_dl,
unknown's avatar
unknown committed
316 317
                                           sizeof(struct st_plugin_dl));
  DBUG_RETURN(tmp);
318
}
unknown's avatar
unknown committed
319 320
#endif /* HAVE_DLOPEN */

321

322 323
static inline void free_plugin_mem(struct st_plugin_dl *p)
{
324
#ifdef HAVE_DLOPEN
325 326
  if (p->handle)
    dlclose(p->handle);
327
#endif
328 329
  my_free(p->dl.str, MYF(MY_ALLOW_ZERO_PTR));
  if (p->version != MYSQL_PLUGIN_INTERFACE_VERSION)
330
    my_free((uchar*)p->plugins, MYF(MY_ALLOW_ZERO_PTR));
331
}
332

unknown's avatar
unknown committed
333

unknown's avatar
unknown committed
334
static st_plugin_dl *plugin_dl_add(const LEX_STRING *dl, int report)
335 336 337
{
#ifdef HAVE_DLOPEN
  char dlpath[FN_REFLEN];
338
  uint plugin_dir_len, dummy_errors, dlpathlen;
339 340 341 342 343 344 345 346 347 348
  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) ||
349 350
      check_string_char_length((LEX_STRING *) dl, "", NAME_CHAR_LEN,
                               system_charset_info, 1) ||
351 352 353 354 355 356 357 358 359 360 361 362 363 364
      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);
  }
365
  bzero(&plugin_dl, sizeof(plugin_dl));
366
  /* Compile dll path */
367 368 369
  dlpathlen=
    strxnmov(dlpath, sizeof(dlpath) - 1, opt_plugin_dir, "/", dl->str, NullS) -
    dlpath;
370 371 372 373
  plugin_dl.ref_count= 1;
  /* Open new dll handle */
  if (!(plugin_dl.handle= dlopen(dlpath, RTLD_NOW)))
  {
374 375 376 377 378 379 380
    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++;
    }
381
    if (report & REPORT_TO_USER)
382
      my_error(ER_CANT_OPEN_LIBRARY, MYF(0), dlpath, errno, errmsg);
383
    if (report & REPORT_TO_LOG)
384
      sql_print_error(ER(ER_CANT_OPEN_LIBRARY), dlpath, errno, errmsg);
385 386 387 388 389
    DBUG_RETURN(0);
  }
  /* Determine interface version */
  if (!(sym= dlsym(plugin_dl.handle, plugin_interface_version_sym)))
  {
390
    free_plugin_mem(&plugin_dl);
391 392 393 394 395 396 397 398 399 400 401
    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))
  {
402
    free_plugin_mem(&plugin_dl);
403 404 405 406 407 408 409 410 411 412 413
    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)))
  {
414
    free_plugin_mem(&plugin_dl);
415 416 417 418 419 420
    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);
  }
421 422 423

  if (plugin_dl.version != MYSQL_PLUGIN_INTERFACE_VERSION)
  {
424 425
    int i;
    uint sizeof_st_plugin;
426
    struct st_mysql_plugin *old, *cur;
427
    char *ptr= (char *)sym;
428 429 430 431 432 433 434 435 436 437 438 439 440

    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
441 442 443 444
      /*
        When the following assert starts failing, we'll have to switch
        to the upper branch of the #ifdef
      */
445
      DBUG_ASSERT(min_plugin_interface_version == 0);
446
      sizeof_st_plugin= (int)offsetof(struct st_mysql_plugin, version);
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
#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);
    }
466 467 468 469 470
    /*
      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.
    */
471 472 473
    for (i=0;
         (old=(struct st_mysql_plugin *)(ptr+i*sizeof_st_plugin))->info;
         i++)
474
      memcpy(cur+i, old, min(sizeof(cur[i]), sizeof_st_plugin));
475

476
    sym= cur;
477
  }
478
  plugin_dl.plugins= (struct st_mysql_plugin *)sym;
479

480 481
  /* Duplicate and convert dll name */
  plugin_dl.dl.length= dl->length * files_charset_info->mbmaxlen + 1;
482
  if (! (plugin_dl.dl.str= (char*) my_malloc(plugin_dl.dl.length, MYF(0))))
483
  {
484
    free_plugin_mem(&plugin_dl);
485 486 487 488 489 490 491 492 493 494 495
    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 */
496
  if (! (tmp= plugin_dl_insert_or_reuse(&plugin_dl)))
497
  {
498
    free_plugin_mem(&plugin_dl);
499 500 501 502 503 504
    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);
  }
505
  DBUG_RETURN(tmp);
506 507 508 509 510 511 512 513 514 515 516
#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
517
static void plugin_dl_del(const LEX_STRING *dl)
518 519 520 521
{
#ifdef HAVE_DLOPEN
  uint i;
  DBUG_ENTER("plugin_dl_del");
unknown's avatar
unknown committed
522 523 524

  safe_mutex_assert_owner(&LOCK_plugin);

525 526
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
unknown's avatar
unknown committed
527 528
    struct st_plugin_dl *tmp= *dynamic_element(&plugin_dl_array, i,
                                               struct st_plugin_dl **);
529 530 531 532 533 534 535 536
    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)
      {
537
        free_plugin_mem(tmp);
538 539 540 541 542 543 544 545 546 547
        bzero(tmp, sizeof(struct st_plugin_dl));
      }
      break;
    }
  }
  DBUG_VOID_RETURN;
#endif
}


unknown's avatar
unknown committed
548
static struct st_plugin_int *plugin_find_internal(const LEX_STRING *name, int type)
549 550 551 552 553
{
  uint i;
  DBUG_ENTER("plugin_find_internal");
  if (! initialized)
    DBUG_RETURN(0);
unknown's avatar
unknown committed
554 555 556

  safe_mutex_assert_owner(&LOCK_plugin);

557 558 559 560 561
  if (type == MYSQL_ANY_PLUGIN)
  {
    for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
    {
      struct st_plugin_int *plugin= (st_plugin_int *)
562
        hash_search(&plugin_hash[i], (const uchar *)name->str, name->length);
563
      if (plugin)
564 565 566 567 568
        DBUG_RETURN(plugin);
    }
  }
  else
    DBUG_RETURN((st_plugin_int *)
569
        hash_search(&plugin_hash[type], (const uchar *)name->str, name->length));
570 571 572 573
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
574
static SHOW_COMP_OPTION plugin_status(const LEX_STRING *name, int type)
575
{
unknown's avatar
unknown committed
576
  SHOW_COMP_OPTION rc= SHOW_OPTION_NO;
577 578
  struct st_plugin_int *plugin;
  DBUG_ENTER("plugin_is_ready");
unknown's avatar
unknown committed
579 580 581 582 583 584 585 586
  pthread_mutex_lock(&LOCK_plugin);
  if ((plugin= plugin_find_internal(name, type)))
  {
    rc= SHOW_OPTION_DISABLED;
    if (plugin->state == PLUGIN_IS_READY)
      rc= SHOW_OPTION_YES;
  }
  pthread_mutex_unlock(&LOCK_plugin);
587 588 589 590
  DBUG_RETURN(rc);
}


unknown's avatar
unknown committed
591
bool plugin_is_ready(const LEX_STRING *name, int type)
592
{
unknown's avatar
unknown committed
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
  bool rc= FALSE;
  if (plugin_status(name, type) == SHOW_OPTION_YES)
    rc= TRUE;
  return rc;
}


SHOW_COMP_OPTION sys_var_have_plugin::get_option()
{
  LEX_STRING plugin_name= { (char *) plugin_name_str, plugin_name_len };
  return plugin_status(&plugin_name, plugin_type);
}


static plugin_ref intern_plugin_lock(LEX *lex, plugin_ref rc CALLER_INFO_PROTO)
{
  st_plugin_int *pi= plugin_ref_to_int(rc);
  DBUG_ENTER("intern_plugin_lock");
unknown's avatar
unknown committed
611

unknown's avatar
unknown committed
612
  safe_mutex_assert_owner(&LOCK_plugin);
unknown's avatar
unknown committed
613

unknown's avatar
unknown committed
614
  if (pi->state & (PLUGIN_IS_READY | PLUGIN_IS_UNINITIALIZED))
615
  {
unknown's avatar
unknown committed
616 617 618 619 620 621 622 623
    plugin_ref plugin;
#ifdef DBUG_OFF
    /* built-in plugins don't need ref counting */
    if (!pi->plugin_dl)
      DBUG_RETURN(pi);

    plugin= pi;
#else
unknown's avatar
unknown committed
624 625 626 627 628
    /*
      For debugging, we do an additional malloc which allows the
      memory manager and/or valgrind to track locked references and
      double unlocks to aid resolving reference counting.problems.
    */
unknown's avatar
unknown committed
629 630 631 632 633 634 635 636 637 638
    if (!(plugin= (plugin_ref) my_malloc_ci(sizeof(pi), MYF(MY_WME))))
      DBUG_RETURN(NULL);

    *plugin= pi;
#endif
    pi->ref_count++;
    DBUG_PRINT("info",("thd: 0x%lx, plugin: \"%s\", ref_count: %d",
                       (long) current_thd, pi->name.str, pi->ref_count));

    if (lex)
unknown's avatar
unknown committed
639
      insert_dynamic(&lex->plugins, (uchar*)&plugin);
unknown's avatar
unknown committed
640
    DBUG_RETURN(plugin);
641
  }
unknown's avatar
unknown committed
642 643 644 645 646 647
  DBUG_RETURN(NULL);
}


plugin_ref plugin_lock(THD *thd, plugin_ref *ptr CALLER_INFO_PROTO)
{
unknown's avatar
unknown committed
648
  LEX *lex= thd ? thd->lex : 0;
unknown's avatar
unknown committed
649 650 651 652 653 654 655 656 657 658 659 660
  plugin_ref rc;
  DBUG_ENTER("plugin_lock");
  pthread_mutex_lock(&LOCK_plugin);
  rc= my_intern_plugin_lock_ci(lex, *ptr);
  pthread_mutex_unlock(&LOCK_plugin);
  DBUG_RETURN(rc);
}


plugin_ref plugin_lock_by_name(THD *thd, const LEX_STRING *name, int type
                               CALLER_INFO_PROTO)
{
unknown's avatar
unknown committed
661
  LEX *lex= thd ? thd->lex : 0;
unknown's avatar
unknown committed
662 663
  plugin_ref rc= NULL;
  st_plugin_int *plugin;
664
  DBUG_ENTER("plugin_lock_by_name");
unknown's avatar
unknown committed
665 666 667 668
  pthread_mutex_lock(&LOCK_plugin);
  if ((plugin= plugin_find_internal(name, type)))
    rc= my_intern_plugin_lock_ci(lex, plugin_int_to_ref(plugin));
  pthread_mutex_unlock(&LOCK_plugin);
669 670 671 672
  DBUG_RETURN(rc);
}


673 674 675
static st_plugin_int *plugin_insert_or_reuse(struct st_plugin_int *plugin)
{
  uint i;
unknown's avatar
unknown committed
676
  struct st_plugin_int *tmp;
677 678 679
  DBUG_ENTER("plugin_insert_or_reuse");
  for (i= 0; i < plugin_array.elements; i++)
  {
unknown's avatar
unknown committed
680
    tmp= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
681 682 683 684 685 686
    if (tmp->state == PLUGIN_IS_FREED)
    {
      memcpy(tmp, plugin, sizeof(struct st_plugin_int));
      DBUG_RETURN(tmp);
    }
  }
687
  if (insert_dynamic(&plugin_array, (uchar*)&plugin))
688
    DBUG_RETURN(0);
unknown's avatar
unknown committed
689 690
  tmp= *dynamic_element(&plugin_array, plugin_array.elements - 1,
                        struct st_plugin_int **)=
unknown's avatar
unknown committed
691
       (struct st_plugin_int *) memdup_root(&plugin_mem_root, (uchar*)plugin,
unknown's avatar
unknown committed
692 693
                                            sizeof(struct st_plugin_int));
  DBUG_RETURN(tmp);
694 695
}

unknown's avatar
unknown committed
696 697 698 699 700 701

/*
  NOTE
    Requires that a write-lock is held on LOCK_system_variables_hash
*/
static bool plugin_add(MEM_ROOT *tmp_root,
unknown's avatar
unknown committed
702
                       const LEX_STRING *name, const LEX_STRING *dl,
unknown's avatar
unknown committed
703
                       int *argc, char **argv, int report)
704 705 706 707 708 709 710 711 712 713 714 715
{
  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);
  }
716 717
  /* Clear the whole struct to catch future extensions. */
  bzero((char*) &tmp, sizeof(tmp));
718 719 720 721 722 723 724 725 726 727 728 729
  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))
    {
730 731 732 733 734 735 736 737
      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
738 739
                 plugin_type_names[plugin->type].str,
                 " plugin is too different", NullS);
740 741 742 743 744 745
        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;
      }
746 747 748 749 750
      tmp.plugin= plugin;
      tmp.name.str= (char *)plugin->name;
      tmp.name.length= name_len;
      tmp.ref_count= 0;
      tmp.state= PLUGIN_IS_UNINITIALIZED;
unknown's avatar
unknown committed
751
      if (!test_plugin_options(tmp_root, &tmp, argc, argv, true))
752
      {
unknown's avatar
unknown committed
753 754 755
        if ((tmp_plugin_ptr= plugin_insert_or_reuse(&tmp)))
        {
          plugin_array_version++;
756
          if (!my_hash_insert(&plugin_hash[plugin->type], (uchar*)tmp_plugin_ptr))
unknown's avatar
unknown committed
757 758 759 760 761 762 763
          {
            init_alloc_root(&tmp_plugin_ptr->mem_root, 4096, 4096);
            DBUG_RETURN(FALSE);
          }
          tmp_plugin_ptr->state= PLUGIN_IS_FREED;
        }
        mysql_del_sys_var_chain(tmp.system_vars);
764 765
        goto err;
      }
unknown's avatar
unknown committed
766 767
      /* plugin was disabled */
      plugin_dl_del(dl);
768 769 770 771 772 773 774 775 776 777 778 779 780
      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
781
static void plugin_deinitialize(struct st_plugin_int *plugin, bool ref_check)
782
{
unknown's avatar
unknown committed
783 784 785 786 787
  /*
    we don't want to hold the LOCK_plugin mutex as it may cause
    deinitialization to deadlock if plugins have worker threads
    with plugin locks
  */
unknown's avatar
unknown committed
788
  safe_mutex_assert_not_owner(&LOCK_plugin);
789

unknown's avatar
unknown committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
  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 */
  }

810 811 812 813 814
  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",
815
                      plugin->name.str, plugin_type_names[plugin->plugin->type].str);
unknown's avatar
unknown committed
816
    }
817 818
  }
  else if (plugin->plugin->deinit)
unknown's avatar
unknown committed
819 820
  {
    DBUG_PRINT("info", ("Deinitializing plugin: '%s'", plugin->name.str));
821
    if (plugin->plugin->deinit(plugin))
822
    {
unknown's avatar
unknown committed
823 824
      DBUG_PRINT("warning", ("Plugin '%s' deinit function returned error.",
                             plugin->name.str));
825
    }
unknown's avatar
unknown committed
826 827
  }
  plugin->state= PLUGIN_IS_UNINITIALIZED;
unknown's avatar
unknown committed
828 829 830 831 832 833 834 835

  /*
    We do the check here because NDB has a worker THD which doesn't
    exit until NDB is shut down.
  */
  if (ref_check && plugin->ref_count)
    sql_print_error("Plugin '%s' has ref_count=%d after deinitialization.",
                    plugin->name.str, plugin->ref_count);
836 837 838
}


unknown's avatar
unknown committed
839 840 841
static void plugin_del(struct st_plugin_int *plugin)
{
  DBUG_ENTER("plugin_del(plugin)");
unknown's avatar
unknown committed
842
  safe_mutex_assert_owner(&LOCK_plugin);
843 844
  /* Free allocated strings before deleting the plugin. */
  plugin_vars_free_values(plugin->system_vars);
845
  hash_delete(&plugin_hash[plugin->plugin->type], (uchar*)plugin);
846 847
  if (plugin->plugin_dl)
    plugin_dl_del(&plugin->plugin_dl->dl);
unknown's avatar
unknown committed
848 849
  plugin->state= PLUGIN_IS_FREED;
  plugin_array_version++;
unknown's avatar
unknown committed
850 851 852 853
  rw_wrlock(&LOCK_system_variables_hash);
  mysql_del_sys_var_chain(plugin->system_vars);
  rw_unlock(&LOCK_system_variables_hash);
  free_root(&plugin->mem_root, MYF(0));
unknown's avatar
unknown committed
854 855 856
  DBUG_VOID_RETURN;
}

unknown's avatar
unknown committed
857 858
#ifdef NOT_USED

859 860 861
static void plugin_del(const LEX_STRING *name)
{
  struct st_plugin_int *plugin;
unknown's avatar
unknown committed
862
  DBUG_ENTER("plugin_del(name)");
863
  if ((plugin= plugin_find_internal(name, MYSQL_ANY_PLUGIN)))
unknown's avatar
unknown committed
864
    plugin_del(plugin);
865 866 867
  DBUG_VOID_RETURN;
}

unknown's avatar
unknown committed
868 869
#endif

unknown's avatar
unknown committed
870
static void reap_plugins(void)
871
{
unknown's avatar
unknown committed
872 873
  uint count, idx;
  struct st_plugin_int *plugin, **reap, **list;
unknown's avatar
unknown committed
874

unknown's avatar
unknown committed
875 876 877 878
  safe_mutex_assert_owner(&LOCK_plugin);

  if (!reap_needed)
    return;
unknown's avatar
unknown committed
879

unknown's avatar
unknown committed
880 881 882 883
  reap_needed= false;
  count= plugin_array.elements;
  reap= (struct st_plugin_int **)my_alloca(sizeof(plugin)*(count+1));
  *(reap++)= NULL;
unknown's avatar
unknown committed
884

unknown's avatar
unknown committed
885
  for (idx= 0; idx < count; idx++)
886
  {
unknown's avatar
unknown committed
887
    plugin= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
unknown's avatar
unknown committed
888 889 890 891 892 893 894
    if (plugin->state == PLUGIN_IS_DELETED && !plugin->ref_count)
    {
      /* change the status flag to prevent reaping by another thread */
      plugin->state= PLUGIN_IS_DYING;
      *(reap++)= plugin;
    }
  }
unknown's avatar
unknown committed
895

unknown's avatar
unknown committed
896 897 898 899
  pthread_mutex_unlock(&LOCK_plugin);

  list= reap;
  while ((plugin= *(--list)))
unknown's avatar
unknown committed
900
    plugin_deinitialize(plugin, true);
unknown's avatar
unknown committed
901

unknown's avatar
unknown committed
902
  pthread_mutex_lock(&LOCK_plugin);
unknown's avatar
unknown committed
903

unknown's avatar
unknown committed
904
  while ((plugin= *(--reap)))
unknown's avatar
unknown committed
905
    plugin_del(plugin);
unknown's avatar
unknown committed
906

unknown's avatar
unknown committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
  my_afree(reap);
}

static void intern_plugin_unlock(LEX *lex, plugin_ref plugin)
{
  int i;
  st_plugin_int *pi;
  DBUG_ENTER("intern_plugin_unlock");

  safe_mutex_assert_owner(&LOCK_plugin);

  if (!plugin)
    DBUG_VOID_RETURN;

  pi= plugin_ref_to_int(plugin);

#ifdef DBUG_OFF
  if (!pi->plugin_dl)
    DBUG_VOID_RETURN;
#else
unknown's avatar
unknown committed
927
  my_free((uchar*) plugin, MYF(MY_WME));
unknown's avatar
unknown committed
928 929 930 931 932 933
#endif

  DBUG_PRINT("info",("unlocking plugin, name= %s, ref_count= %d",
                     pi->name.str, pi->ref_count));
  if (lex)
  {
unknown's avatar
unknown committed
934 935 936 937 938
    /*
      Remove one instance of this plugin from the use list.
      We are searching backwards so that plugins locked last
      could be unlocked faster - optimizing for LIFO semantics.
    */
unknown's avatar
unknown committed
939 940 941 942 943 944
    for (i= lex->plugins.elements - 1; i >= 0; i--)
      if (plugin == *dynamic_element(&lex->plugins, i, plugin_ref*))
      {
        delete_dynamic_element(&lex->plugins, i);
        break;
      }
unknown's avatar
unknown committed
945
    DBUG_ASSERT(i >= 0);
946
  }
unknown's avatar
unknown committed
947 948 949 950 951 952 953 954 955 956 957 958 959

  DBUG_ASSERT(pi->ref_count);
  pi->ref_count--;

  if (pi->state == PLUGIN_IS_DELETED && !pi->ref_count)
    reap_needed= true;

  DBUG_VOID_RETURN;
}


void plugin_unlock(THD *thd, plugin_ref plugin)
{
unknown's avatar
unknown committed
960
  LEX *lex= thd ? thd->lex : 0;
unknown's avatar
unknown committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
  DBUG_ENTER("plugin_unlock");
  if (!plugin)
    DBUG_VOID_RETURN;
#ifdef DBUG_OFF
  /* built-in plugins don't need ref counting */
  if (!plugin_dlib(plugin))
    DBUG_VOID_RETURN;
#endif
  pthread_mutex_lock(&LOCK_plugin);
  intern_plugin_unlock(lex, plugin);
  reap_plugins();
  pthread_mutex_unlock(&LOCK_plugin);
  DBUG_VOID_RETURN;
}


void plugin_unlock_list(THD *thd, plugin_ref *list, uint count)
{
unknown's avatar
unknown committed
979
  LEX *lex= thd ? thd->lex : 0;
unknown's avatar
unknown committed
980 981 982 983 984 985 986
  DBUG_ENTER("plugin_unlock_list");
  DBUG_ASSERT(list);
  pthread_mutex_lock(&LOCK_plugin);
  while (count--)
    intern_plugin_unlock(lex, *list++);
  reap_plugins();
  pthread_mutex_unlock(&LOCK_plugin);
987 988 989 990
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
991 992 993
static int plugin_initialize(struct st_plugin_int *plugin)
{
  DBUG_ENTER("plugin_initialize");
994

unknown's avatar
unknown committed
995
  safe_mutex_assert_owner(&LOCK_plugin);
unknown's avatar
unknown committed
996

997 998 999 1000 1001
  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.",
1002
                      plugin->name.str, plugin_type_names[plugin->plugin->type].str);
1003 1004 1005 1006 1007
      goto err;
    }
  }
  else if (plugin->plugin->init)
  {
1008
    if (plugin->plugin->init(plugin))
1009 1010 1011 1012 1013 1014 1015 1016 1017
    {
      sql_print_error("Plugin '%s' init function returned error.",
                      plugin->name.str);
      goto err;
    }
  }

  plugin->state= PLUGIN_IS_READY;

1018 1019 1020
  if (plugin->plugin->status_vars)
  {
#ifdef FIX_LATER
unknown's avatar
unknown committed
1021
    /*
1022
      We have a problem right now where we can not prepend without
unknown's avatar
unknown committed
1023
      breaking backwards compatibility. We will fix this shortly so
1024 1025 1026 1027 1028
      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
1029
      {plugin->plugin->name, (char*)plugin->plugin->status_vars, SHOW_ARRAY},
1030 1031 1032 1033 1034 1035 1036 1037 1038
      {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
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
  /*
    set the plugin attribute of plugin's sys vars so they are pointing
    to the active plugin
  */
  if (plugin->system_vars)
  {
    sys_var_pluginvar *var= plugin->system_vars->cast_pluginvar();
    for (;;)
    {
      var->plugin= plugin;
      if (!var->next)
        break;
      var= var->next->cast_pluginvar();
    }
  }
1054

unknown's avatar
unknown committed
1055 1056 1057 1058 1059
  DBUG_RETURN(0);
err:
  DBUG_RETURN(1);
}

unknown's avatar
unknown committed
1060

1061 1062 1063 1064 1065 1066
extern "C" uchar *get_plugin_hash_key(const uchar *, size_t *, my_bool);
extern "C" uchar *get_bookmark_hash_key(const uchar *, size_t *, my_bool);


uchar *get_plugin_hash_key(const uchar *buff, size_t *length,
                           my_bool not_used __attribute__((unused)))
1067 1068 1069
{
  struct st_plugin_int *plugin= (st_plugin_int *)buff;
  *length= (uint)plugin->name.length;
1070
  return((uchar *)plugin->name.str);
1071 1072 1073
}


1074 1075
uchar *get_bookmark_hash_key(const uchar *buff, size_t *length,
                             my_bool not_used __attribute__((unused)))
unknown's avatar
unknown committed
1076 1077 1078
{
  struct st_bookmark *var= (st_bookmark *)buff;
  *length= var->name_len + 1;
unknown's avatar
unknown committed
1079
  return (uchar*) var->key;
unknown's avatar
unknown committed
1080 1081 1082
}


1083 1084 1085
/*
  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
1086
  skip this part).
1087

unknown's avatar
unknown committed
1088
  Finally we initialize everything, aka the dynamic that have yet to initialize.
1089
*/
unknown's avatar
unknown committed
1090
int plugin_init(int *argc, char **argv, int flags)
unknown's avatar
unknown committed
1091
{
1092
  uint i;
unknown's avatar
unknown committed
1093
  bool def_enabled, is_myisam;
unknown's avatar
unknown committed
1094 1095
  struct st_mysql_plugin **builtins;
  struct st_mysql_plugin *plugin;
unknown's avatar
unknown committed
1096 1097
  struct st_plugin_int tmp, *plugin_ptr, **reap;
  MEM_ROOT tmp_root;
unknown's avatar
unknown committed
1098 1099 1100 1101 1102
  DBUG_ENTER("plugin_init");

  if (initialized)
    DBUG_RETURN(0);

unknown's avatar
unknown committed
1103 1104 1105 1106 1107 1108
  init_alloc_root(&plugin_mem_root, 4096, 4096);
  init_alloc_root(&tmp_root, 4096, 4096);

  if (hash_init(&bookmark_hash, &my_charset_bin, 16, 0, 0,
                  get_bookmark_hash_key, NULL, HASH_UNIQUE))
      goto err;
unknown's avatar
unknown committed
1109

unknown's avatar
unknown committed
1110 1111

  pthread_mutex_init(&LOCK_plugin, MY_MUTEX_INIT_FAST);
unknown's avatar
unknown committed
1112 1113

  if (my_init_dynamic_array(&plugin_dl_array,
unknown's avatar
unknown committed
1114
                            sizeof(struct st_plugin_dl *),16,16) ||
unknown's avatar
unknown committed
1115
      my_init_dynamic_array(&plugin_array,
unknown's avatar
unknown committed
1116
                            sizeof(struct st_plugin_int *),16,16))
unknown's avatar
unknown committed
1117 1118 1119 1120 1121
    goto err;

  for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
  {
    if (hash_init(&plugin_hash[i], system_charset_info, 16, 0, 0,
1122
                  get_plugin_hash_key, NULL, HASH_UNIQUE))
unknown's avatar
unknown committed
1123 1124
      goto err;
  }
unknown's avatar
unknown committed
1125

unknown's avatar
unknown committed
1126 1127 1128 1129
  pthread_mutex_lock(&LOCK_plugin);

  initialized= 1;

unknown's avatar
unknown committed
1130
  /*
1131 1132
    First we register builtin plugins
  */
unknown's avatar
unknown committed
1133 1134 1135 1136
  for (builtins= mysqld_builtins; *builtins; builtins++)
  {
    for (plugin= *builtins; plugin->info; plugin++)
    {
unknown's avatar
unknown committed
1137
      /* by default, only ndbcluster is disabled */
unknown's avatar
unknown committed
1138
      def_enabled=
unknown's avatar
unknown committed
1139 1140 1141
        my_strcasecmp(&my_charset_latin1, plugin->name, "NDBCLUSTER") != 0;
      bzero(&tmp, sizeof(tmp));
      tmp.plugin= plugin;
unknown's avatar
unknown committed
1142 1143
      tmp.name.str= (char *)plugin->name;
      tmp.name.length= strlen(plugin->name);
unknown's avatar
unknown committed
1144 1145 1146 1147

      free_root(&tmp_root, MYF(MY_MARK_BLOCKS_FREE));
      if (test_plugin_options(&tmp_root, &tmp, argc, argv, def_enabled))
        continue;
unknown's avatar
unknown committed
1148

unknown's avatar
unknown committed
1149 1150 1151 1152
      if (register_builtin(plugin, &tmp, &plugin_ptr))
        goto err_unlock;

      /* only initialize MyISAM and CSV at this stage */
unknown's avatar
unknown committed
1153
      if (!(is_myisam=
unknown's avatar
unknown committed
1154 1155 1156 1157 1158 1159 1160 1161
            !my_strcasecmp(&my_charset_latin1, plugin->name, "MyISAM")) &&
          my_strcasecmp(&my_charset_latin1, plugin->name, "CSV"))
        continue;

      if (plugin_initialize(plugin_ptr))
        goto err_unlock;

      /*
unknown's avatar
unknown committed
1162
        initialize the global default storage engine so that it may
unknown's avatar
unknown committed
1163 1164 1165
        not be null in any child thread.
      */
      if (is_myisam)
1166
      {
unknown's avatar
unknown committed
1167
        DBUG_ASSERT(!global_system_variables.table_plugin);
unknown's avatar
unknown committed
1168 1169
        global_system_variables.table_plugin=
          my_intern_plugin_lock(NULL, plugin_int_to_ref(plugin_ptr));
unknown's avatar
unknown committed
1170
        DBUG_ASSERT(plugin_ptr->ref_count == 1);
1171
      }
unknown's avatar
unknown committed
1172 1173
    }
  }
1174

unknown's avatar
unknown committed
1175 1176
  /* should now be set to MyISAM storage engine */
  DBUG_ASSERT(global_system_variables.table_plugin);
unknown's avatar
unknown committed
1177

unknown's avatar
unknown committed
1178
  pthread_mutex_unlock(&LOCK_plugin);
1179

1180
  /* Register all dynamic plugins */
unknown's avatar
unknown committed
1181 1182 1183 1184 1185 1186 1187 1188
  if (!(flags & PLUGIN_INIT_SKIP_DYNAMIC_LOADING))
  {
    if (opt_plugin_load &&
        plugin_load_list(&tmp_root, argc, argv, opt_plugin_load))
      goto err;
    if (!(flags & PLUGIN_INIT_SKIP_PLUGIN_TABLE))
      plugin_load(&tmp_root, argc, argv);
  }
1189

unknown's avatar
unknown committed
1190 1191
  if (flags & PLUGIN_INIT_SKIP_INITIALIZATION)
    goto end;
unknown's avatar
unknown committed
1192

1193 1194 1195
  /*
    Now we initialize all remaining plugins
  */
1196

unknown's avatar
unknown committed
1197 1198 1199 1200
  pthread_mutex_lock(&LOCK_plugin);
  reap= (st_plugin_int **) my_alloca((plugin_array.elements+1) * sizeof(void*));
  *(reap++)= NULL;

1201 1202
  for (i= 0; i < plugin_array.elements; i++)
  {
unknown's avatar
unknown committed
1203
    plugin_ptr= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
unknown's avatar
unknown committed
1204
    if (plugin_ptr->state == PLUGIN_IS_UNINITIALIZED)
1205
    {
unknown's avatar
unknown committed
1206
      if (plugin_initialize(plugin_ptr))
unknown's avatar
unknown committed
1207
      {
unknown's avatar
unknown committed
1208 1209
        plugin_ptr->state= PLUGIN_IS_DYING;
        *(reap++)= plugin_ptr;
unknown's avatar
unknown committed
1210
      }
1211 1212 1213
    }
  }

unknown's avatar
unknown committed
1214 1215
  /*
    Check if any plugins have to be reaped
unknown's avatar
unknown committed
1216
  */
unknown's avatar
unknown committed
1217 1218 1219
  while ((plugin_ptr= *(--reap)))
  {
    pthread_mutex_unlock(&LOCK_plugin);
unknown's avatar
unknown committed
1220
    plugin_deinitialize(plugin_ptr, true);
unknown's avatar
unknown committed
1221 1222 1223 1224 1225 1226 1227 1228 1229
    pthread_mutex_lock(&LOCK_plugin);
    plugin_del(plugin_ptr);
  }

  pthread_mutex_unlock(&LOCK_plugin);
  my_afree(reap);

end:
  free_root(&tmp_root, MYF(0));
1230

unknown's avatar
unknown committed
1231
  DBUG_RETURN(0);
1232

unknown's avatar
unknown committed
1233 1234
err_unlock:
  pthread_mutex_unlock(&LOCK_plugin);
unknown's avatar
unknown committed
1235
err:
unknown's avatar
unknown committed
1236
  free_root(&tmp_root, MYF(0));
unknown's avatar
unknown committed
1237 1238 1239 1240
  DBUG_RETURN(1);
}


unknown's avatar
unknown committed
1241
static bool register_builtin(struct st_mysql_plugin *plugin,
unknown's avatar
unknown committed
1242 1243
                             struct st_plugin_int *tmp,
                             struct st_plugin_int **ptr)
unknown's avatar
unknown committed
1244
{
unknown's avatar
unknown committed
1245
  DBUG_ENTER("register_builtin");
unknown's avatar
unknown committed
1246

unknown's avatar
unknown committed
1247 1248 1249
  tmp->state= PLUGIN_IS_UNINITIALIZED;
  tmp->ref_count= 0;
  tmp->plugin_dl= 0;
unknown's avatar
unknown committed
1250

1251
  if (insert_dynamic(&plugin_array, (uchar*)&tmp))
unknown's avatar
unknown committed
1252 1253
    DBUG_RETURN(1);

unknown's avatar
unknown committed
1254 1255
  *ptr= *dynamic_element(&plugin_array, plugin_array.elements - 1,
                         struct st_plugin_int **)=
unknown's avatar
unknown committed
1256
        (struct st_plugin_int *) memdup_root(&plugin_mem_root, (uchar*)tmp,
unknown's avatar
unknown committed
1257
                                             sizeof(struct st_plugin_int));
unknown's avatar
unknown committed
1258

unknown's avatar
unknown committed
1259
  if (my_hash_insert(&plugin_hash[plugin->type],(uchar*) *ptr))
unknown's avatar
unknown committed
1260 1261 1262 1263 1264 1265
    DBUG_RETURN(1);

  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
1266
#ifdef NOT_USED_YET
unknown's avatar
unknown committed
1267 1268
/*
  Register a plugin at run time. (note, this doesn't initialize a plugin)
unknown's avatar
unknown committed
1269
  Will be useful for embedded applications.
unknown's avatar
unknown committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283

  SYNOPSIS
    plugin_register_builtin()
    thd         current thread (used to store scratch data in mem_root)
    plugin      static plugin to install

  RETURN
    false - plugin registered successfully
*/
bool plugin_register_builtin(THD *thd, struct st_mysql_plugin *plugin)
{
  struct st_plugin_int tmp, *ptr;
  bool result= true;
  int dummy_argc= 0;
unknown's avatar
unknown committed
1284 1285
  DBUG_ENTER("plugin_register_builtin");

unknown's avatar
unknown committed
1286
  bzero(&tmp, sizeof(tmp));
unknown's avatar
unknown committed
1287 1288 1289 1290
  tmp.plugin= plugin;
  tmp.name.str= (char *)plugin->name;
  tmp.name.length= strlen(plugin->name);

unknown's avatar
unknown committed
1291
  pthread_mutex_lock(&LOCK_plugin);
unknown's avatar
unknown committed
1292
  rw_wrlock(&LOCK_system_variables_hash);
unknown's avatar
unknown committed
1293

unknown's avatar
unknown committed
1294 1295
  if (test_plugin_options(thd->mem_root, &tmp, &dummy_argc, NULL, true))
    goto end;
unknown's avatar
unknown committed
1296

unknown's avatar
unknown committed
1297 1298
  if ((result= register_builtin(plugin, &tmp, &ptr)))
    mysql_del_sys_var_chain(tmp.system_vars);
unknown's avatar
unknown committed
1299

unknown's avatar
unknown committed
1300 1301
end:
  rw_unlock(&LOCK_system_variables_hash);
unknown's avatar
unknown committed
1302
  pthread_mutex_unlock(&LOCK_plugin);
unknown's avatar
unknown committed
1303 1304

  DBUG_RETURN(result);;
unknown's avatar
unknown committed
1305
}
unknown's avatar
unknown committed
1306
#endif /* NOT_USED_YET */
unknown's avatar
unknown committed
1307 1308


unknown's avatar
unknown committed
1309 1310 1311 1312
/*
  called only by plugin_init()
*/
static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv)
1313 1314 1315 1316
{
  TABLE_LIST tables;
  TABLE *table;
  READ_RECORD read_record_info;
1317
  int error;
unknown's avatar
unknown committed
1318
  THD *new_thd;
unknown's avatar
unknown committed
1319 1320 1321
#ifdef EMBEDDED_LIBRARY
  bool table_exists;
#endif /* EMBEDDED_LIBRARY */
unknown's avatar
unknown committed
1322
  DBUG_ENTER("plugin_load");
1323

unknown's avatar
unknown committed
1324
  if (!(new_thd= new THD))
1325 1326 1327 1328 1329
  {
    sql_print_error("Can't allocate memory for plugin structures");
    delete new_thd;
    DBUG_VOID_RETURN;
  }
unknown's avatar
unknown committed
1330
  new_thd->thread_stack= (char*) &tables;
1331 1332 1333
  new_thd->store_globals();
  new_thd->db= my_strdup("mysql", MYF(0));
  new_thd->db_length= 5;
1334
  bzero((uchar*)&tables, sizeof(tables));
1335 1336 1337
  tables.alias= tables.table_name= (char*)"plugin";
  tables.lock_type= TL_READ;
  tables.db= new_thd->db;
unknown's avatar
unknown committed
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

#ifdef EMBEDDED_LIBRARY
  /*
    When building an embedded library, if the mysql.plugin table
    does not exist, we silently ignore the missing table
  */
  pthread_mutex_lock(&LOCK_open);
  if (check_if_table_exists(new_thd, &tables, &table_exists))
    table_exists= FALSE;
  pthread_mutex_unlock(&LOCK_open);
  if (!table_exists)
    goto end;
#endif /* EMBEDDED_LIBRARY */

1352 1353 1354
  if (simple_open_n_lock_tables(new_thd, &tables))
  {
    DBUG_PRINT("error",("Can't open plugin table"));
1355 1356
    sql_print_error("Can't open the mysql.plugin table. Please "
                    "run mysql_upgrade to create it.");
1357 1358 1359 1360
    goto end;
  }
  table= tables.table;
  init_read_record(&read_record_info, new_thd, table, NULL, 1, 0);
1361
  table->use_all_columns();
1362 1363 1364 1365 1366 1367 1368
  /*
    there're no other threads running yet, so we don't need a mutex.
    but plugin_add() before is designed to work in multi-threaded
    environment, and it uses safe_mutex_assert_owner(), so we lock
    the mutex here to satisfy the assert
  */
  pthread_mutex_lock(&LOCK_plugin);
1369 1370 1371
  while (!(error= read_record_info.read_record(&read_record_info)))
  {
    DBUG_PRINT("info", ("init plugin record"));
unknown's avatar
unknown committed
1372
    String str_name, str_dl;
unknown's avatar
unknown committed
1373 1374
    get_field(tmp_root, table->field[0], &str_name);
    get_field(tmp_root, table->field[1], &str_dl);
unknown's avatar
unknown committed
1375

unknown's avatar
unknown committed
1376 1377 1378
    LEX_STRING name= {(char *)str_name.ptr(), str_name.length()};
    LEX_STRING dl= {(char *)str_dl.ptr(), str_dl.length()};

unknown's avatar
unknown committed
1379
    if (plugin_add(tmp_root, &name, &dl, argc, argv, REPORT_TO_LOG))
unknown's avatar
unknown committed
1380 1381
      sql_print_warning("Couldn't load plugin named '%s' with soname '%s'.",
                        str_name.c_ptr(), str_dl.c_ptr());
1382
    free_root(tmp_root, MYF(MY_MARK_BLOCKS_FREE));
1383
  }
1384
  pthread_mutex_unlock(&LOCK_plugin);
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
  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:
  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;
}


unknown's avatar
unknown committed
1398 1399 1400 1401 1402
/*
  called only by plugin_init()
*/
static bool plugin_load_list(MEM_ROOT *tmp_root, int *argc, char **argv,
                             const char *list)
1403
{
unknown's avatar
unknown committed
1404 1405 1406 1407 1408 1409 1410
  char buffer[FN_REFLEN];
  LEX_STRING name= {buffer, 0}, dl= {NULL, 0}, *str= &name;
  struct st_plugin_dl *plugin_dl;
  struct st_mysql_plugin *plugin;
  char *p= buffer;
  DBUG_ENTER("plugin_load_list");
  while (list)
1411
  {
unknown's avatar
unknown committed
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    if (p == buffer + sizeof(buffer) - 1)
      break;
    switch ((*(p++)= *(list++))) {
    case '\0':
      list= NULL; /* terminate the loop */
      /* fall through */
#ifndef __WIN__
    case ':':     /* can't use this as delimiter as it may be drive letter */
#endif
    case ';':
      name.str[name.length]= '\0';
      if (str != &dl)  // load all plugins in named module
      {
        dl= name;
        if ((plugin_dl= plugin_dl_add(&dl, REPORT_TO_LOG)))
        {
          for (plugin= plugin_dl->plugins; plugin->info; plugin++)
          {
            name.str= (char *) plugin->name;
            name.length= strlen(name.str);
unknown's avatar
unknown committed
1432

unknown's avatar
unknown committed
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
            free_root(tmp_root, MYF(MY_MARK_BLOCKS_FREE));
            if (plugin_add(tmp_root, &name, &dl, argc, argv, REPORT_TO_LOG))
              goto error;
          }
          plugin_dl_del(&dl); // reduce ref count
        }
      }
      else
      {
        free_root(tmp_root, MYF(MY_MARK_BLOCKS_FREE));
        if (plugin_add(tmp_root, &name, &dl, argc, argv, REPORT_TO_LOG))
          goto error;
      }
      name.length= dl.length= 0;
      dl.str= NULL; name.str= p= buffer;
      str= &name;
      continue;
    case '=':
    case '#':
      if (str == &name)
      {
        str= &dl;
        str->str= p;
        continue;
      }
    default:
      str->length++;
      continue;
    }
1462
  }
unknown's avatar
unknown committed
1463 1464 1465 1466 1467 1468
  DBUG_RETURN(FALSE);
error:
  sql_print_error("Couldn't load plugin named '%s' with soname '%s'.",
                  name.str, dl.str);
  DBUG_RETURN(TRUE);
}
1469

unknown's avatar
unknown committed
1470

1471
void plugin_shutdown(void)
1472
{
unknown's avatar
unknown committed
1473
  uint i, count= plugin_array.elements, free_slots= 0;
unknown's avatar
unknown committed
1474 1475
  struct st_plugin_int **plugins, *plugin;
  struct st_plugin_dl **dl;
1476 1477
  DBUG_ENTER("plugin_shutdown");

1478
  if (initialized)
1479
  {
unknown's avatar
unknown committed
1480
    pthread_mutex_lock(&LOCK_plugin);
1481

unknown's avatar
unknown committed
1482
    reap_needed= true;
unknown's avatar
unknown committed
1483

unknown's avatar
unknown committed
1484 1485 1486 1487 1488 1489 1490 1491 1492
    /*
      We want to shut down plugins in a reasonable order, this will
      become important when we have plugins which depend upon each other.
      Circular references cannot be reaped so they are forced afterwards.
      TODO: Have an additional step here to notify all active plugins that
      shutdown is requested to allow plugins to deinitialize in parallel.
    */
    while (reap_needed && (count= plugin_array.elements))
    {
unknown's avatar
unknown committed
1493
      reap_plugins();
unknown's avatar
unknown committed
1494
      for (i= free_slots= 0; i < count; i++)
unknown's avatar
unknown committed
1495
      {
unknown's avatar
unknown committed
1496
        plugin= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
unknown's avatar
unknown committed
1497 1498
        switch (plugin->state) {
        case PLUGIN_IS_READY:
unknown's avatar
unknown committed
1499 1500
          plugin->state= PLUGIN_IS_DELETED;
          reap_needed= true;
unknown's avatar
unknown committed
1501 1502 1503 1504 1505
          break;
        case PLUGIN_IS_FREED:
        case PLUGIN_IS_UNINITIALIZED:
          free_slots++;
          break;
unknown's avatar
unknown committed
1506 1507
        }
      }
unknown's avatar
unknown committed
1508 1509 1510 1511 1512 1513 1514 1515
      if (!reap_needed)
      {
        /*
          release any plugin references held.
        */
        unlock_variables(NULL, &global_system_variables);
        unlock_variables(NULL, &max_system_variables);
      }
unknown's avatar
unknown committed
1516
    }
unknown's avatar
unknown committed
1517

unknown's avatar
unknown committed
1518 1519
    if (count > free_slots)
      sql_print_warning("Forcing shutdown of %d plugins", count - free_slots);
unknown's avatar
unknown committed
1520

unknown's avatar
unknown committed
1521 1522 1523 1524 1525 1526 1527
    plugins= (struct st_plugin_int **) my_alloca(sizeof(void*) * (count+1));

    /*
      If we have any plugins which did not die cleanly, we force shutdown
    */
    for (i= 0; i < count; i++)
    {
unknown's avatar
unknown committed
1528
      plugins[i]= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
unknown's avatar
unknown committed
1529 1530 1531 1532 1533
      /* change the state to ensure no reaping races */
      if (plugins[i]->state == PLUGIN_IS_DELETED)
        plugins[i]->state= PLUGIN_IS_DYING;
    }
    pthread_mutex_unlock(&LOCK_plugin);
unknown's avatar
unknown committed
1534

unknown's avatar
unknown committed
1535 1536 1537 1538 1539 1540
    /*
      We loop through all plugins and call deinit() if they have one.
    */
    for (i= 0; i < count; i++)
      if (!(plugins[i]->state & (PLUGIN_IS_UNINITIALIZED | PLUGIN_IS_FREED)))
      {
unknown's avatar
unknown committed
1541
        sql_print_information("Plugin '%s' will be forced to shutdown",
unknown's avatar
unknown committed
1542
                              plugins[i]->name.str);
unknown's avatar
unknown committed
1543 1544 1545 1546
        /*
          We are forcing deinit on plugins so we don't want to do a ref_count
          check until we have processed all the plugins.
        */
unknown's avatar
unknown committed
1547 1548 1549
        plugin_deinitialize(plugins[i], false);
      }

unknown's avatar
unknown committed
1550 1551 1552 1553 1554 1555
    /*
      It's perfectly safe not to lock LOCK_plugin, as there're no
      concurrent threads anymore. But some functions called from here
      use safe_mutex_assert_owner(), so we lock the mutex to satisfy it
    */
    pthread_mutex_lock(&LOCK_plugin);
unknown's avatar
unknown committed
1556

unknown's avatar
unknown committed
1557 1558 1559 1560 1561
    /*
      We defer checking ref_counts until after all plugins are deinitialized
      as some may have worker threads holding on to plugin references.
    */
    for (i= 0; i < count; i++)
unknown's avatar
unknown committed
1562
    {
unknown's avatar
unknown committed
1563 1564 1565 1566 1567
      if (plugins[i]->ref_count)
        sql_print_error("Plugin '%s' has ref_count=%d after shutdown.",
                        plugins[i]->name.str, plugins[i]->ref_count);
      if (plugins[i]->state & PLUGIN_IS_UNINITIALIZED)
        plugin_del(plugins[i]);
unknown's avatar
unknown committed
1568
    }
1569

unknown's avatar
unknown committed
1570 1571 1572
    /*
      Now we can deallocate all memory.
    */
unknown's avatar
unknown committed
1573

unknown's avatar
unknown committed
1574 1575
    cleanup_variables(NULL, &global_system_variables);
    cleanup_variables(NULL, &max_system_variables);
unknown's avatar
unknown committed
1576
    pthread_mutex_unlock(&LOCK_plugin);
unknown's avatar
unknown committed
1577 1578 1579

    initialized= 0;
    pthread_mutex_destroy(&LOCK_plugin);
1580

unknown's avatar
unknown committed
1581
    my_afree(plugins);
1582 1583
  }

unknown's avatar
unknown committed
1584
  /* Dispose of the memory */
unknown's avatar
unknown committed
1585

1586 1587 1588
  for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
    hash_free(&plugin_hash[i]);
  delete_dynamic(&plugin_array);
unknown's avatar
unknown committed
1589 1590 1591 1592

  count= plugin_dl_array.elements;
  dl= (struct st_plugin_dl **)my_alloca(sizeof(void*) * count);
  for (i= 0; i < count; i++)
unknown's avatar
unknown committed
1593
    dl[i]= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
1594
  for (i= 0; i < plugin_dl_array.elements; i++)
unknown's avatar
unknown committed
1595 1596
    free_plugin_mem(dl[i]);
  my_afree(dl);
1597
  delete_dynamic(&plugin_dl_array);
unknown's avatar
unknown committed
1598

unknown's avatar
unknown committed
1599 1600
  hash_free(&bookmark_hash);
  free_root(&plugin_mem_root, MYF(0));
unknown's avatar
unknown committed
1601

unknown's avatar
unknown committed
1602 1603
  global_variables_dynamic_size= 0;

1604 1605 1606 1607
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
1608
bool mysql_install_plugin(THD *thd, const LEX_STRING *name, const LEX_STRING *dl)
1609 1610 1611
{
  TABLE_LIST tables;
  TABLE *table;
1612 1613
  int error, argc;
  char *argv[2];
1614 1615
  struct st_plugin_int *tmp;
  DBUG_ENTER("mysql_install_plugin");
unknown's avatar
unknown committed
1616

1617 1618 1619 1620 1621
  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);
1622

unknown's avatar
unknown committed
1623
  /* need to open before acquiring LOCK_plugin or it will deadlock */
1624
  if (! (table = open_ltable(thd, &tables, TL_WRITE, 0)))
unknown's avatar
unknown committed
1625 1626
    DBUG_RETURN(TRUE);

unknown's avatar
unknown committed
1627 1628
  pthread_mutex_lock(&LOCK_plugin);
  rw_wrlock(&LOCK_system_variables_hash);
1629 1630 1631
  /* handle_options() assumes arg0 (program name) always exists */
  argv[0]= const_cast<char*>(""); // without a cast gcc emits a warning
  argv[1]= 0;
1632 1633
  argc= 1;
  error= plugin_add(thd->mem_root, name, dl, &argc, argv, REPORT_TO_USER);
unknown's avatar
unknown committed
1634 1635 1636
  rw_unlock(&LOCK_system_variables_hash);

  if (error || !(tmp= plugin_find_internal(name, MYSQL_ANY_PLUGIN)))
1637
    goto err;
1638

unknown's avatar
unknown committed
1639
  if (plugin_initialize(tmp))
1640
  {
unknown's avatar
unknown committed
1641 1642
    my_error(ER_CANT_INITIALIZE_UDF, MYF(0), name->str,
             "Plugin initialization function failed.");
unknown's avatar
unknown committed
1643
    goto deinit;
1644
  }
unknown's avatar
unknown committed
1645

unknown's avatar
unknown committed
1646
  table->use_all_columns();
1647 1648 1649
  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);
1650
  error= table->file->ha_write_row(table->record[0]);
1651 1652 1653 1654 1655
  if (error)
  {
    table->file->print_error(error, MYF(0));
    goto deinit;
  }
1656

unknown's avatar
unknown committed
1657
  pthread_mutex_unlock(&LOCK_plugin);
1658 1659
  DBUG_RETURN(FALSE);
deinit:
unknown's avatar
unknown committed
1660 1661 1662
  tmp->state= PLUGIN_IS_DELETED;
  reap_needed= true;
  reap_plugins();
unknown's avatar
unknown committed
1663
err:
unknown's avatar
unknown committed
1664
  pthread_mutex_unlock(&LOCK_plugin);
1665 1666 1667 1668
  DBUG_RETURN(TRUE);
}


unknown's avatar
unknown committed
1669
bool mysql_uninstall_plugin(THD *thd, const LEX_STRING *name)
1670 1671 1672 1673 1674
{
  TABLE *table;
  TABLE_LIST tables;
  struct st_plugin_int *plugin;
  DBUG_ENTER("mysql_uninstall_plugin");
unknown's avatar
unknown committed
1675 1676 1677 1678 1679

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

unknown's avatar
unknown committed
1680
  /* need to open before acquiring LOCK_plugin or it will deadlock */
1681
  if (! (table= open_ltable(thd, &tables, TL_WRITE, 0)))
unknown's avatar
unknown committed
1682 1683
    DBUG_RETURN(TRUE);

unknown's avatar
unknown committed
1684
  pthread_mutex_lock(&LOCK_plugin);
unknown's avatar
unknown committed
1685
  if (!(plugin= plugin_find_internal(name, MYSQL_ANY_PLUGIN)))
1686 1687 1688 1689
  {
    my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "PLUGIN", name->str);
    goto err;
  }
unknown's avatar
unknown committed
1690 1691 1692 1693 1694 1695 1696
  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;
  }
1697

unknown's avatar
unknown committed
1698
  plugin->state= PLUGIN_IS_DELETED;
unknown's avatar
unknown committed
1699 1700 1701 1702
  if (plugin->ref_count)
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
                 "Plugin is busy and will be uninstalled on shutdown");
  else
unknown's avatar
unknown committed
1703 1704 1705
    reap_needed= true;
  reap_plugins();
  pthread_mutex_unlock(&LOCK_plugin);
unknown's avatar
unknown committed
1706

1707
  table->use_all_columns();
1708
  table->field[0]->store(name->str, name->length, system_charset_info);
1709 1710 1711 1712
  if (! table->file->index_read_idx_map(table->record[0], 0,
                                        (uchar *)table->field[0]->ptr,
                                        HA_WHOLE_KEY,
                                        HA_READ_KEY_EXACT))
1713 1714
  {
    int error;
1715
    if ((error= table->file->ha_delete_row(table->record[0])))
1716 1717
    {
      table->file->print_error(error, MYF(0));
unknown's avatar
unknown committed
1718
      DBUG_RETURN(TRUE);
1719 1720 1721 1722
    }
  }
  DBUG_RETURN(FALSE);
err:
unknown's avatar
unknown committed
1723
  pthread_mutex_unlock(&LOCK_plugin);
1724 1725
  DBUG_RETURN(TRUE);
}
unknown's avatar
unknown committed
1726

unknown's avatar
unknown committed
1727 1728

bool plugin_foreach_with_mask(THD *thd, plugin_foreach_func *func,
1729
                       int type, uint state_mask, void *arg)
unknown's avatar
unknown committed
1730
{
1731 1732 1733
  uint idx, total;
  struct st_plugin_int *plugin, **plugins;
  int version=plugin_array_version;
1734 1735
  DBUG_ENTER("plugin_foreach_with_mask");

unknown's avatar
unknown committed
1736 1737 1738
  if (!initialized)
    DBUG_RETURN(FALSE);

1739
  state_mask= ~state_mask; // do it only once
1740

unknown's avatar
unknown committed
1741
  pthread_mutex_lock(&LOCK_plugin);
unknown's avatar
unknown committed
1742
  total= type == MYSQL_ANY_PLUGIN ? plugin_array.elements
unknown's avatar
unknown committed
1743 1744 1745
                                  : plugin_hash[type].records;
  /*
    Do the alloca out here in case we do have a working alloca:
unknown's avatar
unknown committed
1746
        leaving the nested stack frame invalidates alloca allocation.
unknown's avatar
unknown committed
1747
  */
unknown's avatar
unknown committed
1748
  plugins=(struct st_plugin_int **)my_alloca(total*sizeof(plugin));
unknown's avatar
unknown committed
1749 1750
  if (type == MYSQL_ANY_PLUGIN)
  {
1751
    for (idx= 0; idx < total; idx++)
unknown's avatar
unknown committed
1752
    {
unknown's avatar
unknown committed
1753
      plugin= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
unknown's avatar
unknown committed
1754
      plugins[idx]= !(plugin->state & state_mask) ? plugin : NULL;
unknown's avatar
unknown committed
1755 1756 1757 1758
    }
  }
  else
  {
unknown's avatar
unknown committed
1759
    HASH *hash= plugin_hash + type;
1760
    for (idx= 0; idx < total; idx++)
unknown's avatar
unknown committed
1761 1762
    {
      plugin= (struct st_plugin_int *) hash_element(hash, idx);
unknown's avatar
unknown committed
1763
      plugins[idx]= !(plugin->state & state_mask) ? plugin : NULL;
unknown's avatar
unknown committed
1764 1765
    }
  }
unknown's avatar
unknown committed
1766
  pthread_mutex_unlock(&LOCK_plugin);
1767 1768 1769 1770 1771

  for (idx= 0; idx < total; idx++)
  {
    if (unlikely(version != plugin_array_version))
    {
unknown's avatar
unknown committed
1772
      pthread_mutex_lock(&LOCK_plugin);
1773
      for (uint i=idx; i < total; i++)
1774
        if (plugins[i] && plugins[i]->state & state_mask)
1775
          plugins[i]=0;
unknown's avatar
unknown committed
1776
      pthread_mutex_unlock(&LOCK_plugin);
1777
    }
unknown's avatar
unknown committed
1778
    plugin= plugins[idx];
1779
    /* It will stop iterating on first engine error when "func" returns TRUE */
unknown's avatar
unknown committed
1780
    if (plugin && func(thd, plugin_int_to_ref(plugin), arg))
1781 1782 1783 1784
        goto err;
  }

  my_afree(plugins);
unknown's avatar
unknown committed
1785 1786
  DBUG_RETURN(FALSE);
err:
1787
  my_afree(plugins);
unknown's avatar
unknown committed
1788 1789
  DBUG_RETURN(TRUE);
}
1790

unknown's avatar
unknown committed
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806

/****************************************************************************
  Internal type declarations for variables support
****************************************************************************/

#undef MYSQL_SYSVAR_NAME
#define MYSQL_SYSVAR_NAME(name) name
#define PLUGIN_VAR_TYPEMASK 0x007f

#define EXTRA_OPTIONS 3 /* options for: 'foo', 'plugin-foo' and NULL */

typedef DECLARE_MYSQL_SYSVAR_BASIC(sysvar_bool_t, my_bool);
typedef DECLARE_MYSQL_THDVAR_BASIC(thdvar_bool_t, my_bool);
typedef DECLARE_MYSQL_SYSVAR_BASIC(sysvar_str_t, char *);
typedef DECLARE_MYSQL_THDVAR_BASIC(thdvar_str_t, char *);

unknown's avatar
unknown committed
1807 1808 1809 1810
typedef DECLARE_MYSQL_SYSVAR_TYPELIB(sysvar_enum_t, unsigned long);
typedef DECLARE_MYSQL_THDVAR_TYPELIB(thdvar_enum_t, unsigned long);
typedef DECLARE_MYSQL_SYSVAR_TYPELIB(sysvar_set_t, ulonglong);
typedef DECLARE_MYSQL_THDVAR_TYPELIB(thdvar_set_t, ulonglong);
unknown's avatar
unknown committed
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827

typedef DECLARE_MYSQL_SYSVAR_SIMPLE(sysvar_int_t, int);
typedef DECLARE_MYSQL_SYSVAR_SIMPLE(sysvar_long_t, long);
typedef DECLARE_MYSQL_SYSVAR_SIMPLE(sysvar_longlong_t, longlong);
typedef DECLARE_MYSQL_SYSVAR_SIMPLE(sysvar_uint_t, uint);
typedef DECLARE_MYSQL_SYSVAR_SIMPLE(sysvar_ulong_t, ulong);
typedef DECLARE_MYSQL_SYSVAR_SIMPLE(sysvar_ulonglong_t, ulonglong);

typedef DECLARE_MYSQL_THDVAR_SIMPLE(thdvar_int_t, int);
typedef DECLARE_MYSQL_THDVAR_SIMPLE(thdvar_long_t, long);
typedef DECLARE_MYSQL_THDVAR_SIMPLE(thdvar_longlong_t, longlong);
typedef DECLARE_MYSQL_THDVAR_SIMPLE(thdvar_uint_t, uint);
typedef DECLARE_MYSQL_THDVAR_SIMPLE(thdvar_ulong_t, ulong);
typedef DECLARE_MYSQL_THDVAR_SIMPLE(thdvar_ulonglong_t, ulonglong);

#define SET_PLUGIN_VAR_RESOLVE(opt)\
  *(mysql_sys_var_ptr_p*)&((opt)->resolve)= mysql_sys_var_ptr
unknown's avatar
unknown committed
1828
typedef uchar *(*mysql_sys_var_ptr_p)(void* a_thd, int offset);
unknown's avatar
unknown committed
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840


/****************************************************************************
  default variable data check and update functions
****************************************************************************/

static int check_func_bool(THD *thd, struct st_mysql_sys_var *var,
                           void *save, st_mysql_value *value)
{
  char buff[STRING_BUFFER_USUAL_SIZE];
  const char *strvalue= "NULL", *str;
  int result, length;
unknown's avatar
unknown committed
1841
  long long tmp;
unknown's avatar
unknown committed
1842

unknown's avatar
unknown committed
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
  if (value->value_type(value) == MYSQL_VALUE_TYPE_STRING)
  {
    length= sizeof(buff);
    if (!(str= value->val_str(value, buff, &length)) ||
        (result= find_type(&bool_typelib, str, length, 1)-1) < 0)
    {
      if (str)
        strvalue= str;
      goto err;
    }
  }
  else
  {
unknown's avatar
unknown committed
1856
    if (value->val_int(value, &tmp) < 0)
unknown's avatar
unknown committed
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
      goto err;
    if (tmp > 1)
    {
      llstr(tmp, buff);
      strvalue= buff;
      goto err;
    }
    result= (int) tmp;
  }
  *(int*)save= -result;
  return 0;
err:
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->name, strvalue);
  return 1;
}


static int check_func_int(THD *thd, struct st_mysql_sys_var *var,
                          void *save, st_mysql_value *value)
{
unknown's avatar
unknown committed
1877
  long long tmp;
unknown's avatar
unknown committed
1878
  struct my_option options;
unknown's avatar
unknown committed
1879
  value->val_int(value, &tmp);
unknown's avatar
unknown committed
1880 1881
  plugin_opt_set_limits(&options, var);
  *(int *)save= (int) getopt_ull_limit_value(tmp, &options);
unknown's avatar
unknown committed
1882
  return (thd->variables.sql_mode & MODE_STRICT_ALL_TABLES) &&
unknown's avatar
unknown committed
1883 1884 1885 1886 1887 1888 1889
         (*(int *)save != (int) tmp);
}


static int check_func_long(THD *thd, struct st_mysql_sys_var *var,
                          void *save, st_mysql_value *value)
{
unknown's avatar
unknown committed
1890
  long long tmp;
unknown's avatar
unknown committed
1891
  struct my_option options;
unknown's avatar
unknown committed
1892
  value->val_int(value, &tmp);
unknown's avatar
unknown committed
1893 1894
  plugin_opt_set_limits(&options, var);
  *(long *)save= (long) getopt_ull_limit_value(tmp, &options);
unknown's avatar
unknown committed
1895
  return (thd->variables.sql_mode & MODE_STRICT_ALL_TABLES) &&
unknown's avatar
unknown committed
1896 1897 1898 1899 1900 1901 1902
         (*(long *)save != (long) tmp);
}


static int check_func_longlong(THD *thd, struct st_mysql_sys_var *var,
                          void *save, st_mysql_value *value)
{
unknown's avatar
unknown committed
1903
  long long tmp;
unknown's avatar
unknown committed
1904
  struct my_option options;
unknown's avatar
unknown committed
1905
  value->val_int(value, &tmp);
unknown's avatar
unknown committed
1906 1907
  plugin_opt_set_limits(&options, var);
  *(ulonglong *)save= getopt_ull_limit_value(tmp, &options);
unknown's avatar
unknown committed
1908
  return (thd->variables.sql_mode & MODE_STRICT_ALL_TABLES) &&
unknown's avatar
unknown committed
1909
         (*(long long *)save != tmp);
unknown's avatar
unknown committed
1910 1911 1912 1913 1914 1915 1916 1917
}

static int check_func_str(THD *thd, struct st_mysql_sys_var *var,
                          void *save, st_mysql_value *value)
{
  char buff[STRING_BUFFER_USUAL_SIZE];
  const char *str;
  int length;
unknown's avatar
unknown committed
1918

unknown's avatar
unknown committed
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
  length= sizeof(buff);
  if ((str= value->val_str(value, buff, &length)))
    str= thd->strmake(str, length);
  *(const char**)save= str;
  return 0;
}


static int check_func_enum(THD *thd, struct st_mysql_sys_var *var,
                           void *save, st_mysql_value *value)
{
  char buff[STRING_BUFFER_USUAL_SIZE];
  const char *strvalue= "NULL", *str;
  TYPELIB *typelib;
unknown's avatar
unknown committed
1933
  long long tmp;
unknown's avatar
unknown committed
1934 1935
  long result;
  int length;
unknown's avatar
unknown committed
1936

unknown's avatar
unknown committed
1937
  if (var->flags & PLUGIN_VAR_THDLOCAL)
unknown's avatar
unknown committed
1938
    typelib= ((thdvar_enum_t*) var)->typelib;
unknown's avatar
unknown committed
1939
  else
unknown's avatar
unknown committed
1940
    typelib= ((sysvar_enum_t*) var)->typelib;
unknown's avatar
unknown committed
1941

unknown's avatar
unknown committed
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
  if (value->value_type(value) == MYSQL_VALUE_TYPE_STRING)
  {
    length= sizeof(buff);
    if (!(str= value->val_str(value, buff, &length)))
      goto err;
    if ((result= find_type(typelib, str, length, 1)-1) < 0)
    {
      strvalue= str;
      goto err;
    }
  }
  else
  {
unknown's avatar
unknown committed
1955
    if (value->val_int(value, &tmp))
unknown's avatar
unknown committed
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
      goto err;
    if (tmp >= typelib->count)
    {
      llstr(tmp, buff);
      strvalue= buff;
      goto err;
    }
    result= (long) tmp;
  }
  *(long*)save= result;
  return 0;
err:
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->name, strvalue);
  return 1;
}


static int check_func_set(THD *thd, struct st_mysql_sys_var *var,
                          void *save, st_mysql_value *value)
{
  char buff[STRING_BUFFER_USUAL_SIZE], *error= 0;
  const char *strvalue= "NULL", *str;
  TYPELIB *typelib;
unknown's avatar
unknown committed
1979
  ulonglong result;
unknown's avatar
unknown committed
1980 1981 1982
  uint error_len;
  bool not_used;
  int length;
unknown's avatar
unknown committed
1983

unknown's avatar
unknown committed
1984
  if (var->flags & PLUGIN_VAR_THDLOCAL)
unknown's avatar
unknown committed
1985
    typelib= ((thdvar_set_t*) var)->typelib;
unknown's avatar
unknown committed
1986
  else
unknown's avatar
unknown committed
1987
    typelib= ((sysvar_set_t*)var)->typelib;
unknown's avatar
unknown committed
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004

  if (value->value_type(value) == MYSQL_VALUE_TYPE_STRING)
  {
    length= sizeof(buff);
    if (!(str= value->val_str(value, buff, &length)))
      goto err;
    result= find_set(typelib, str, length, NULL,
                     &error, &error_len, &not_used);
    if (error_len)
    {
      strmake(buff, error, min(sizeof(buff), error_len));
      strvalue= buff;
      goto err;
    }
  }
  else
  {
unknown's avatar
unknown committed
2005
    if (value->val_int(value, (long long *)&result))
unknown's avatar
unknown committed
2006
      goto err;
unknown's avatar
unknown committed
2007
    if (unlikely((result >= (ULL(1) << typelib->count)) &&
unknown's avatar
unknown committed
2008 2009
                 (typelib->count < sizeof(long)*8)))
    {
unknown's avatar
unknown committed
2010
      llstr(result, buff);
unknown's avatar
unknown committed
2011 2012 2013 2014
      strvalue= buff;
      goto err;
    }
  }
unknown's avatar
unknown committed
2015
  *(ulonglong*)save= result;
unknown's avatar
unknown committed
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
  return 0;
err:
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->name, strvalue);
  return 1;
}


static void update_func_bool(THD *thd, struct st_mysql_sys_var *var,
                             void *tgt, void *save)
{
  *(my_bool *) tgt= *(int *) save ? 1 : 0;
}


static void update_func_int(THD *thd, struct st_mysql_sys_var *var,
                             void *tgt, void *save)
{
  *(int *)tgt= *(int *) save;
}


static void update_func_long(THD *thd, struct st_mysql_sys_var *var,
                             void *tgt, void *save)
{
  *(long *)tgt= *(long *) save;
}


static void update_func_longlong(THD *thd, struct st_mysql_sys_var *var,
                             void *tgt, void *save)
{
  *(longlong *)tgt= *(ulonglong *) save;
}


static void update_func_str(THD *thd, struct st_mysql_sys_var *var,
                             void *tgt, void *save)
{
  char *old= *(char **) tgt;
  *(char **)tgt= *(char **) save;
  if (var->flags & PLUGIN_VAR_MEMALLOC)
  {
    *(char **)tgt= my_strdup(*(char **) save, MYF(0));
    my_free(old, MYF(0));
  }
}


/****************************************************************************
  System Variables support
****************************************************************************/


sys_var *find_sys_var(THD *thd, const char *str, uint length)
{
  sys_var *var;
  sys_var_pluginvar *pi= NULL;
  plugin_ref plugin;
  DBUG_ENTER("find_sys_var");
unknown's avatar
unknown committed
2075

unknown's avatar
unknown committed
2076
  pthread_mutex_lock(&LOCK_plugin);
unknown's avatar
unknown committed
2077 2078 2079 2080
  rw_rdlock(&LOCK_system_variables_hash);
  if ((var= intern_find_sys_var(str, length, false)) &&
      (pi= var->cast_pluginvar()))
  {
unknown's avatar
unknown committed
2081
    rw_unlock(&LOCK_system_variables_hash);
unknown's avatar
unknown committed
2082
    LEX *lex= thd ? thd->lex : 0;
unknown's avatar
unknown committed
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
    if (!(plugin= my_intern_plugin_lock(lex, plugin_int_to_ref(pi->plugin))))
      var= NULL; /* failed to lock it, it must be uninstalling */
    else
    if (!(plugin_state(plugin) & PLUGIN_IS_READY))
    {
      /* initialization not completed */
      var= NULL;
      intern_plugin_unlock(lex, plugin);
    }
  }
unknown's avatar
unknown committed
2093 2094 2095
  else
    rw_unlock(&LOCK_system_variables_hash);
  pthread_mutex_unlock(&LOCK_plugin);
unknown's avatar
unknown committed
2096

unknown's avatar
unknown committed
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
  /*
    If the variable exists but the plugin it is associated with is not ready
    then the intern_plugin_lock did not raise an error, so we do it here.
  */
  if (pi && !var)
    my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
  DBUG_RETURN(var);
}


/*
  called by register_var, construct_options and test_plugin_options.
  Returns the 'bookmark' for the named variable.
  LOCK_system_variables_hash should be at least read locked
*/
static st_bookmark *find_bookmark(const char *plugin, const char *name,
                                  int flags)
{
  st_bookmark *result= NULL;
  uint namelen, length, pluginlen= 0;
  char *varname, *p;
unknown's avatar
unknown committed
2118

unknown's avatar
unknown committed
2119 2120
  if (!(flags & PLUGIN_VAR_THDLOCAL))
    return NULL;
unknown's avatar
unknown committed
2121

unknown's avatar
unknown committed
2122 2123 2124 2125 2126
  namelen= strlen(name);
  if (plugin)
    pluginlen= strlen(plugin) + 1;
  length= namelen + pluginlen + 2;
  varname= (char*) my_alloca(length);
unknown's avatar
unknown committed
2127

unknown's avatar
unknown committed
2128 2129 2130 2131 2132
  if (plugin)
  {
    strxmov(varname + 1, plugin, "_", name, NullS);
    for (p= varname + 1; *p; p++)
      if (*p == '-')
unknown's avatar
unknown committed
2133
        *p= '_';
unknown's avatar
unknown committed
2134 2135 2136 2137 2138
  }
  else
    memcpy(varname + 1, name, namelen + 1);

  varname[0]= flags & PLUGIN_VAR_TYPEMASK;
unknown's avatar
unknown committed
2139 2140

  result= (st_bookmark*) hash_search(&bookmark_hash,
unknown's avatar
unknown committed
2141
                                     (const uchar*) varname, length - 1);
unknown's avatar
unknown committed
2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161

  my_afree(varname);
  return result;
}


/*
  returns a bookmark for thd-local variables, creating if neccessary.
  returns null for non thd-local variables.
  Requires that a write lock is obtained on LOCK_system_variables_hash
*/
static st_bookmark *register_var(const char *plugin, const char *name,
                                 int flags)
{
  uint length= strlen(plugin) + strlen(name) + 3, size= 0, offset, new_size;
  st_bookmark *result;
  char *varname, *p;

  if (!(flags & PLUGIN_VAR_THDLOCAL))
    return NULL;
unknown's avatar
unknown committed
2162

unknown's avatar
unknown committed
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
  switch (flags & PLUGIN_VAR_TYPEMASK) {
  case PLUGIN_VAR_BOOL:
    size= sizeof(my_bool);
    break;
  case PLUGIN_VAR_INT:
    size= sizeof(int);
    break;
  case PLUGIN_VAR_LONG:
    size= sizeof(long);
    break;
  case PLUGIN_VAR_LONGLONG:
    size= sizeof(ulonglong);
    break;
  case PLUGIN_VAR_STR:
    size= sizeof(char*);
    break;
  default:
    DBUG_ASSERT(0);
    return NULL;
  };

  varname= ((char*) my_alloca(length));
  strxmov(varname + 1, plugin, "_", name, NullS);
  for (p= varname + 1; *p; p++)
    if (*p == '-')
      *p= '_';

  if (!(result= find_bookmark(NULL, varname + 1, flags)))
  {
unknown's avatar
unknown committed
2192
    result= (st_bookmark*) alloc_root(&plugin_mem_root,
unknown's avatar
unknown committed
2193
                                      sizeof(struct st_bookmark) + length-1);
unknown's avatar
unknown committed
2194
    varname[0]= flags & PLUGIN_VAR_TYPEMASK;
unknown's avatar
unknown committed
2195
    memcpy(result->key, varname, length);
unknown's avatar
unknown committed
2196 2197
    result->name_len= length - 2;
    result->offset= -1;
unknown's avatar
unknown committed
2198

unknown's avatar
unknown committed
2199 2200 2201 2202 2203
    DBUG_ASSERT(size && !(size & (size-1))); /* must be power of 2 */

    offset= global_system_variables.dynamic_variables_size;
    offset= (offset + size - 1) & ~(size - 1);
    result->offset= (int) offset;
unknown's avatar
unknown committed
2204

unknown's avatar
unknown committed
2205 2206 2207 2208
    new_size= (offset + size + 63) & ~63;

    if (new_size > global_variables_dynamic_size)
    {
unknown's avatar
unknown committed
2209
      global_system_variables.dynamic_variables_ptr= (char*)
unknown's avatar
unknown committed
2210 2211
        my_realloc(global_system_variables.dynamic_variables_ptr, new_size,
                   MYF(MY_WME | MY_FAE | MY_ALLOW_ZERO_PTR));
unknown's avatar
unknown committed
2212
      max_system_variables.dynamic_variables_ptr= (char*)
unknown's avatar
unknown committed
2213 2214
        my_realloc(max_system_variables.dynamic_variables_ptr, new_size,
                   MYF(MY_WME | MY_FAE | MY_ALLOW_ZERO_PTR));
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
      /*
        Clear the new variable value space. This is required for string
        variables. If their value is non-NULL, it must point to a valid
        string.
      */
      bzero(global_system_variables.dynamic_variables_ptr +
            global_variables_dynamic_size,
            new_size - global_variables_dynamic_size);
      bzero(max_system_variables.dynamic_variables_ptr +
            global_variables_dynamic_size,
            new_size - global_variables_dynamic_size);
unknown's avatar
unknown committed
2226 2227
      global_variables_dynamic_size= new_size;
    }
unknown's avatar
unknown committed
2228

unknown's avatar
unknown committed
2229 2230 2231 2232 2233 2234
    global_system_variables.dynamic_variables_head= offset;
    max_system_variables.dynamic_variables_head= offset;
    global_system_variables.dynamic_variables_size= offset + size;
    max_system_variables.dynamic_variables_size= offset + size;
    global_system_variables.dynamic_variables_version++;
    max_system_variables.dynamic_variables_version++;
unknown's avatar
unknown committed
2235

unknown's avatar
unknown committed
2236 2237 2238
    result->version= global_system_variables.dynamic_variables_version;

    /* this should succeed because we have already checked if a dup exists */
unknown's avatar
unknown committed
2239
    if (my_hash_insert(&bookmark_hash, (uchar*) result))
unknown's avatar
unknown committed
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
    {
      fprintf(stderr, "failed to add placeholder to hash");
      DBUG_ASSERT(0);
    }
  }
  my_afree(varname);
  return result;
}


/*
  returns a pointer to the memory which holds the thd-local variable or
  a pointer to the global variable if thd==null.
  If required, will sync with global variables if the requested variable
  has not yet been allocated in the current thread.
*/
unknown's avatar
unknown committed
2256
static uchar *intern_sys_var_ptr(THD* thd, int offset, bool global_lock)
unknown's avatar
unknown committed
2257 2258 2259
{
  DBUG_ASSERT(offset >= 0);
  DBUG_ASSERT((uint)offset <= global_system_variables.dynamic_variables_head);
unknown's avatar
unknown committed
2260

unknown's avatar
unknown committed
2261
  if (!thd)
unknown's avatar
unknown committed
2262
    return (uchar*) global_system_variables.dynamic_variables_ptr + offset;
unknown's avatar
unknown committed
2263

unknown's avatar
unknown committed
2264
  /*
unknown's avatar
unknown committed
2265
    dynamic_variables_head points to the largest valid offset
unknown's avatar
unknown committed
2266 2267 2268 2269 2270
  */
  if (!thd->variables.dynamic_variables_ptr ||
      (uint)offset > thd->variables.dynamic_variables_head)
  {
    uint idx;
unknown's avatar
unknown committed
2271

unknown's avatar
unknown committed
2272
    rw_rdlock(&LOCK_system_variables_hash);
unknown's avatar
unknown committed
2273

unknown's avatar
unknown committed
2274
    thd->variables.dynamic_variables_ptr= (char*)
unknown's avatar
unknown committed
2275 2276 2277 2278 2279 2280 2281 2282 2283
      my_realloc(thd->variables.dynamic_variables_ptr,
                 global_variables_dynamic_size,
                 MYF(MY_WME | MY_FAE | MY_ALLOW_ZERO_PTR));

    if (global_lock)
      pthread_mutex_lock(&LOCK_global_system_variables);

    safe_mutex_assert_owner(&LOCK_global_system_variables);

unknown's avatar
unknown committed
2284
    memcpy(thd->variables.dynamic_variables_ptr +
unknown's avatar
unknown committed
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
             thd->variables.dynamic_variables_size,
           global_system_variables.dynamic_variables_ptr +
             thd->variables.dynamic_variables_size,
           global_system_variables.dynamic_variables_size -
             thd->variables.dynamic_variables_size);

    /*
      now we need to iterate through any newly copied 'defaults'
      and if it is a string type with MEMALLOC flag, we need to strdup
    */
    for (idx= 0; idx < bookmark_hash.records; idx++)
    {
      sys_var_pluginvar *pi;
      sys_var *var;
      st_bookmark *v= (st_bookmark*) hash_element(&bookmark_hash,idx);
unknown's avatar
unknown committed
2300

unknown's avatar
unknown committed
2301
      if (v->version <= thd->variables.dynamic_variables_version ||
unknown's avatar
unknown committed
2302
          !(var= intern_find_sys_var(v->key + 1, v->name_len, true)) ||
unknown's avatar
unknown committed
2303
          !(pi= var->cast_pluginvar()) ||
unknown's avatar
unknown committed
2304
          v->key[0] != (pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
unknown's avatar
unknown committed
2305 2306 2307
        continue;

      /* Here we do anything special that may be required of the data types */
unknown's avatar
unknown committed
2308

unknown's avatar
unknown committed
2309 2310 2311
      if ((pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
          pi->plugin_var->flags & PLUGIN_VAR_MEMALLOC)
      {
unknown's avatar
unknown committed
2312 2313 2314
         char **pp= (char**) (thd->variables.dynamic_variables_ptr +
                             *(int*)(pi->plugin_var + 1));
         if ((*pp= *(char**) (global_system_variables.dynamic_variables_ptr +
unknown's avatar
unknown committed
2315 2316 2317
                             *(int*)(pi->plugin_var + 1))))
           *pp= my_strdup(*pp, MYF(MY_WME|MY_FAE));
      }
unknown's avatar
unknown committed
2318
    }
unknown's avatar
unknown committed
2319

unknown's avatar
unknown committed
2320 2321 2322
    if (global_lock)
      pthread_mutex_unlock(&LOCK_global_system_variables);

unknown's avatar
unknown committed
2323
    thd->variables.dynamic_variables_version=
unknown's avatar
unknown committed
2324
           global_system_variables.dynamic_variables_version;
unknown's avatar
unknown committed
2325
    thd->variables.dynamic_variables_head=
unknown's avatar
unknown committed
2326
           global_system_variables.dynamic_variables_head;
unknown's avatar
unknown committed
2327
    thd->variables.dynamic_variables_size=
unknown's avatar
unknown committed
2328 2329 2330 2331
           global_system_variables.dynamic_variables_size;

    rw_unlock(&LOCK_system_variables_hash);
  }
unknown's avatar
unknown committed
2332
  return (uchar*)thd->variables.dynamic_variables_ptr + offset;
unknown's avatar
unknown committed
2333 2334
}

unknown's avatar
unknown committed
2335
static uchar *mysql_sys_var_ptr(void* a_thd, int offset)
unknown's avatar
unknown committed
2336 2337 2338 2339 2340
{
  return intern_sys_var_ptr((THD *)a_thd, offset, true);
}


unknown's avatar
unknown committed
2341
void plugin_thdvar_init(THD *thd)
unknown's avatar
unknown committed
2342
{
unknown's avatar
unknown committed
2343 2344 2345 2346 2347 2348 2349 2350 2351
  plugin_ref old_table_plugin= thd->variables.table_plugin;
  DBUG_ENTER("plugin_thdvar_init");
  
  thd->variables.table_plugin= NULL;
  cleanup_variables(thd, &thd->variables);
  
  thd->variables= global_system_variables;
  thd->variables.table_plugin= NULL;

unknown's avatar
unknown committed
2352 2353 2354 2355 2356
  /* we are going to allocate these lazily */
  thd->variables.dynamic_variables_version= 0;
  thd->variables.dynamic_variables_size= 0;
  thd->variables.dynamic_variables_ptr= 0;

unknown's avatar
unknown committed
2357
  pthread_mutex_lock(&LOCK_plugin);  
unknown's avatar
unknown committed
2358
  thd->variables.table_plugin=
unknown's avatar
unknown committed
2359 2360 2361 2362
        my_intern_plugin_lock(NULL, global_system_variables.table_plugin);
  intern_plugin_unlock(NULL, old_table_plugin);
  pthread_mutex_unlock(&LOCK_plugin);
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
2363 2364
}

unknown's avatar
unknown committed
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377

/*
  Unlocks all system variables which hold a reference
*/
static void unlock_variables(THD *thd, struct system_variables *vars)
{
  intern_plugin_unlock(NULL, vars->table_plugin);
  vars->table_plugin= NULL;
}


/*
  Frees memory used by system variables
2378 2379 2380

  Unlike plugin_vars_free_values() it frees all variables of all plugins,
  it's used on shutdown.
unknown's avatar
unknown committed
2381 2382
*/
static void cleanup_variables(THD *thd, struct system_variables *vars)
unknown's avatar
unknown committed
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
{
  st_bookmark *v;
  sys_var_pluginvar *pivar;
  sys_var *var;
  int flags;
  uint idx;

  rw_rdlock(&LOCK_system_variables_hash);
  for (idx= 0; idx < bookmark_hash.records; idx++)
  {
    v= (st_bookmark*) hash_element(&bookmark_hash, idx);
    if (v->version > vars->dynamic_variables_version ||
unknown's avatar
unknown committed
2395
        !(var= intern_find_sys_var(v->key + 1, v->name_len, true)) ||
unknown's avatar
unknown committed
2396
        !(pivar= var->cast_pluginvar()) ||
unknown's avatar
unknown committed
2397
        v->key[0] != (pivar->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
unknown's avatar
unknown committed
2398 2399 2400
      continue;

    flags= pivar->plugin_var->flags;
unknown's avatar
unknown committed
2401

unknown's avatar
unknown committed
2402 2403 2404 2405 2406 2407 2408
    if ((flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
        flags & PLUGIN_VAR_THDLOCAL && flags & PLUGIN_VAR_MEMALLOC)
    {
      char **ptr= (char**) pivar->real_value_ptr(thd, OPT_SESSION);
      my_free(*ptr, MYF(MY_WME | MY_FAE | MY_ALLOW_ZERO_PTR));
      *ptr= NULL;
    }
unknown's avatar
unknown committed
2409
  }
unknown's avatar
unknown committed
2410
  rw_unlock(&LOCK_system_variables_hash);
unknown's avatar
unknown committed
2411

unknown's avatar
unknown committed
2412 2413
  DBUG_ASSERT(vars->table_plugin == NULL);

unknown's avatar
unknown committed
2414 2415 2416 2417
  my_free(vars->dynamic_variables_ptr, MYF(MY_ALLOW_ZERO_PTR));
  vars->dynamic_variables_ptr= NULL;
  vars->dynamic_variables_size= 0;
  vars->dynamic_variables_version= 0;
unknown's avatar
unknown committed
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
}


void plugin_thdvar_cleanup(THD *thd)
{
  uint idx;
  plugin_ref *list;
  DBUG_ENTER("plugin_thdvar_cleanup");

  pthread_mutex_lock(&LOCK_plugin);

unknown's avatar
unknown committed
2429 2430
  unlock_variables(thd, &thd->variables);
  cleanup_variables(thd, &thd->variables);
unknown's avatar
unknown committed
2431

unknown's avatar
unknown committed
2432
  if ((idx= thd->lex->plugins.elements))
unknown's avatar
unknown committed
2433
  {
unknown's avatar
unknown committed
2434
    list= ((plugin_ref*) thd->lex->plugins.buffer) + idx - 1;
unknown's avatar
unknown committed
2435
    DBUG_PRINT("info",("unlocking %d plugins", idx));
unknown's avatar
unknown committed
2436
    while ((uchar*) list >= thd->lex->plugins.buffer)
unknown's avatar
unknown committed
2437 2438 2439 2440 2441 2442
      intern_plugin_unlock(NULL, *list--);
  }

  reap_plugins();
  pthread_mutex_unlock(&LOCK_plugin);

unknown's avatar
unknown committed
2443
  reset_dynamic(&thd->lex->plugins);
unknown's avatar
unknown committed
2444 2445 2446 2447 2448

  DBUG_VOID_RETURN;
}


2449 2450 2451
/**
  @brief Free values of thread variables of a plugin.

unknown's avatar
unknown committed
2452
  This must be called before a plugin is deleted. Otherwise its
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
  variables are no longer accessible and the value space is lost. Note
  that only string values with PLUGIN_VAR_MEMALLOC are allocated and
  must be freed.

  @param[in]        vars        Chain of system variables of a plugin
*/

static void plugin_vars_free_values(sys_var *vars)
{
  DBUG_ENTER("plugin_vars_free_values");

  for (sys_var *var= vars; var; var= var->next)
  {
    sys_var_pluginvar *piv= var->cast_pluginvar();
    if (piv &&
        ((piv->plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR) &&
        (piv->plugin_var->flags & PLUGIN_VAR_MEMALLOC))
    {
      /* Free the string from global_system_variables. */
      char **valptr= (char**) piv->real_value_ptr(NULL, OPT_GLOBAL);
      DBUG_PRINT("plugin", ("freeing value for: '%s'  addr: 0x%lx",
                            var->name, (long) valptr));
      my_free(*valptr, MYF(MY_WME | MY_FAE | MY_ALLOW_ZERO_PTR));
      *valptr= NULL;
    }
  }
  DBUG_VOID_RETURN;
}


unknown's avatar
unknown committed
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
bool sys_var_pluginvar::check_update_type(Item_result type)
{
  if (is_readonly())
    return 1;
  switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
  case PLUGIN_VAR_INT:
  case PLUGIN_VAR_LONG:
  case PLUGIN_VAR_LONGLONG:
    return type != INT_RESULT;
  case PLUGIN_VAR_STR:
    return type != STRING_RESULT;
  default:
    return 0;
  }
}
unknown's avatar
unknown committed
2498

unknown's avatar
unknown committed
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520

SHOW_TYPE sys_var_pluginvar::show_type()
{
  switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
  case PLUGIN_VAR_BOOL:
    return SHOW_MY_BOOL;
  case PLUGIN_VAR_INT:
    return SHOW_INT;
  case PLUGIN_VAR_LONG:
    return SHOW_LONG;
  case PLUGIN_VAR_LONGLONG:
    return SHOW_LONGLONG;
  case PLUGIN_VAR_STR:
    return SHOW_CHAR_PTR;
  case PLUGIN_VAR_ENUM:
  case PLUGIN_VAR_SET:
    return SHOW_CHAR;
  default:
    DBUG_ASSERT(0);
    return SHOW_UNDEF;
  }
}
unknown's avatar
unknown committed
2521

unknown's avatar
unknown committed
2522

unknown's avatar
unknown committed
2523
uchar* sys_var_pluginvar::real_value_ptr(THD *thd, enum_var_type type)
unknown's avatar
unknown committed
2524
{
2525
  DBUG_ASSERT(thd || (type == OPT_GLOBAL));
unknown's avatar
unknown committed
2526 2527
  if (plugin_var->flags & PLUGIN_VAR_THDLOCAL)
  {
2528
    if (type == OPT_GLOBAL)
unknown's avatar
unknown committed
2529
      thd= NULL;
unknown's avatar
unknown committed
2530

unknown's avatar
unknown committed
2531 2532
    return intern_sys_var_ptr(thd, *(int*) (plugin_var+1), false);
  }
unknown's avatar
unknown committed
2533
  return *(uchar**) (plugin_var+1);
unknown's avatar
unknown committed
2534
}
unknown's avatar
unknown committed
2535

unknown's avatar
unknown committed
2536 2537 2538

TYPELIB* sys_var_pluginvar::plugin_var_typelib(void)
{
unknown's avatar
unknown committed
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
  switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_THDLOCAL)) {
  case PLUGIN_VAR_ENUM:
    return ((sysvar_enum_t *)plugin_var)->typelib;
  case PLUGIN_VAR_SET:
    return ((sysvar_set_t *)plugin_var)->typelib;
  case PLUGIN_VAR_ENUM | PLUGIN_VAR_THDLOCAL:
    return ((thdvar_enum_t *)plugin_var)->typelib;
  case PLUGIN_VAR_SET | PLUGIN_VAR_THDLOCAL:
    return ((thdvar_set_t *)plugin_var)->typelib;
  default:
    return NULL;
unknown's avatar
unknown committed
2550 2551 2552
  }
  return NULL;
}
unknown's avatar
unknown committed
2553

unknown's avatar
unknown committed
2554

unknown's avatar
unknown committed
2555
uchar* sys_var_pluginvar::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
2556 2557
                                   LEX_STRING *base)
{
unknown's avatar
unknown committed
2558
  uchar* result;
unknown's avatar
unknown committed
2559 2560 2561

  result= real_value_ptr(thd, type);

2562
  if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_ENUM)
unknown's avatar
unknown committed
2563
    result= (uchar*) get_type(plugin_var_typelib(), *(ulong*)result);
2564
  else if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_SET)
unknown's avatar
unknown committed
2565 2566 2567 2568
  {
    char buffer[STRING_BUFFER_USUAL_SIZE];
    String str(buffer, sizeof(buffer), system_charset_info);
    TYPELIB *typelib= plugin_var_typelib();
unknown's avatar
unknown committed
2569
    ulonglong mask= 1, value= *(ulonglong*) result;
unknown's avatar
unknown committed
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
    uint i;

    str.length(0);
    for (i= 0; i < typelib->count; i++, mask<<=1)
    {
      if (!(value & mask))
        continue;
      str.append(typelib->type_names[i], typelib->type_lengths[i]);
      str.append(',');
    }

unknown's avatar
unknown committed
2581
    result= (uchar*) "";
unknown's avatar
unknown committed
2582
    if (str.length())
unknown's avatar
unknown committed
2583
      result= (uchar*) thd->strmake(str.ptr(), str.length()-1);
unknown's avatar
unknown committed
2584 2585 2586 2587 2588 2589 2590 2591 2592
  }
  return result;
}


bool sys_var_pluginvar::check(THD *thd, set_var *var)
{
  st_item_value_holder value;
  DBUG_ASSERT(is_readonly() || plugin_var->check);
unknown's avatar
unknown committed
2593

unknown's avatar
unknown committed
2594 2595 2596 2597 2598
  value.value_type= item_value_type;
  value.val_str= item_val_str;
  value.val_int= item_val_int;
  value.val_real= item_val_real;
  value.item= var->value;
unknown's avatar
unknown committed
2599

unknown's avatar
unknown committed
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
  return is_readonly() ||
         plugin_var->check(thd, plugin_var, &var->save_result, &value);
}


void sys_var_pluginvar::set_default(THD *thd, enum_var_type type)
{
  void *tgt, *src;

  DBUG_ASSERT(is_readonly() || plugin_var->update);

  if (is_readonly())
    return;

  tgt= real_value_ptr(thd, type);
  src= ((void **) (plugin_var + 1) + 1);

  if (plugin_var->flags & PLUGIN_VAR_THDLOCAL)
  {
    src= ((int*) (plugin_var + 1) + 1);
    if (type != OPT_GLOBAL)
      src= real_value_ptr(thd, OPT_GLOBAL);
  }

  /* thd must equal current_thd if PLUGIN_VAR_THDLOCAL flag is set */
unknown's avatar
unknown committed
2625
  DBUG_ASSERT(!(plugin_var->flags & PLUGIN_VAR_THDLOCAL) ||
unknown's avatar
unknown committed
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
              thd == current_thd);

  if (!(plugin_var->flags & PLUGIN_VAR_THDLOCAL) || type == OPT_GLOBAL)
  {
    pthread_mutex_lock(&LOCK_plugin);
    plugin_var->update(thd, plugin_var, tgt, src);
    pthread_mutex_unlock(&LOCK_plugin);
  }
  else
    plugin_var->update(thd, plugin_var, tgt, src);
}
unknown's avatar
unknown committed
2637

unknown's avatar
unknown committed
2638 2639 2640 2641 2642 2643 2644 2645

bool sys_var_pluginvar::update(THD *thd, set_var *var)
{
  void *tgt;

  DBUG_ASSERT(is_readonly() || plugin_var->update);

  /* thd must equal current_thd if PLUGIN_VAR_THDLOCAL flag is set */
unknown's avatar
unknown committed
2646
  DBUG_ASSERT(!(plugin_var->flags & PLUGIN_VAR_THDLOCAL) ||
unknown's avatar
unknown committed
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
              thd == current_thd);

  if (is_readonly())
    return 1;

  pthread_mutex_lock(&LOCK_global_system_variables);
  tgt= real_value_ptr(thd, var->type);

  if (!(plugin_var->flags & PLUGIN_VAR_THDLOCAL) || var->type == OPT_GLOBAL)
  {
    /* variable we are updating has global scope, so we unlock after updating */
    plugin_var->update(thd, plugin_var, tgt, &var->save_result);
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
  {
    pthread_mutex_unlock(&LOCK_global_system_variables);
    plugin_var->update(thd, plugin_var, tgt, &var->save_result);
  }
 return 0;
}


#define OPTION_SET_LIMITS(type, options, opt) \
  options->var_type= type; \
  options->def_value= (opt)->def_val; \
  options->min_value= (opt)->min_val; \
  options->max_value= (opt)->max_val; \
2675
  options->block_size= (long) (opt)->blk_sz
unknown's avatar
unknown committed
2676 2677 2678 2679 2680


static void plugin_opt_set_limits(struct my_option *options,
                                  const struct st_mysql_sys_var *opt)
{
unknown's avatar
unknown committed
2681
  switch (opt->flags & (PLUGIN_VAR_TYPEMASK |
unknown's avatar
unknown committed
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
                        PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_THDLOCAL)) {
  /* global system variables */
  case PLUGIN_VAR_INT:
    OPTION_SET_LIMITS(GET_INT, options, (sysvar_int_t*) opt);
    break;
  case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED:
    OPTION_SET_LIMITS(GET_UINT, options, (sysvar_uint_t*) opt);
    break;
  case PLUGIN_VAR_LONG:
    OPTION_SET_LIMITS(GET_LONG, options, (sysvar_long_t*) opt);
    break;
  case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED:
    OPTION_SET_LIMITS(GET_ULONG, options, (sysvar_ulong_t*) opt);
    break;
  case PLUGIN_VAR_LONGLONG:
    OPTION_SET_LIMITS(GET_LL, options, (sysvar_longlong_t*) opt);
    break;
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED:
    OPTION_SET_LIMITS(GET_ULL, options, (sysvar_ulonglong_t*) opt);
    break;
  case PLUGIN_VAR_ENUM:
    options->var_type= GET_ENUM;
unknown's avatar
unknown committed
2704
    options->typelib= ((sysvar_enum_t*) opt)->typelib;
unknown's avatar
unknown committed
2705 2706 2707 2708 2709 2710
    options->def_value= *(ulong*) ((int*) (opt + 1) + 1);
    options->min_value= options->block_size= 0;
    options->max_value= options->typelib->count - 1;
    break;
  case PLUGIN_VAR_SET:
    options->var_type= GET_SET;
unknown's avatar
unknown committed
2711 2712
    options->typelib= ((sysvar_set_t*) opt)->typelib;
    options->def_value= *(ulonglong*) ((int*) (opt + 1) + 1);
unknown's avatar
unknown committed
2713 2714 2715 2716 2717 2718 2719 2720
    options->min_value= options->block_size= 0;
    options->max_value= (ULL(1) << options->typelib->count) - 1;
    break;
  case PLUGIN_VAR_BOOL:
    options->var_type= GET_BOOL;
    options->def_value= *(my_bool*) ((void**)(opt + 1) + 1);
    break;
  case PLUGIN_VAR_STR:
2721 2722 2723
    options->var_type= ((opt->flags & PLUGIN_VAR_MEMALLOC) ?
                        GET_STR_ALLOC : GET_STR);
    options->def_value= (ulonglong)(intptr) *((char**) ((void**) (opt + 1) + 1));
unknown's avatar
unknown committed
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745
    break;
  /* threadlocal variables */
  case PLUGIN_VAR_INT | PLUGIN_VAR_THDLOCAL:
    OPTION_SET_LIMITS(GET_INT, options, (thdvar_int_t*) opt);
    break;
  case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_THDLOCAL:
    OPTION_SET_LIMITS(GET_UINT, options, (thdvar_uint_t*) opt);
    break;
  case PLUGIN_VAR_LONG | PLUGIN_VAR_THDLOCAL:
    OPTION_SET_LIMITS(GET_LONG, options, (thdvar_long_t*) opt);
    break;
  case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_THDLOCAL:
    OPTION_SET_LIMITS(GET_ULONG, options, (thdvar_ulong_t*) opt);
    break;
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_THDLOCAL:
    OPTION_SET_LIMITS(GET_LL, options, (thdvar_longlong_t*) opt);
    break;
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_THDLOCAL:
    OPTION_SET_LIMITS(GET_ULL, options, (thdvar_ulonglong_t*) opt);
    break;
  case PLUGIN_VAR_ENUM | PLUGIN_VAR_THDLOCAL:
    options->var_type= GET_ENUM;
unknown's avatar
unknown committed
2746
    options->typelib= ((thdvar_enum_t*) opt)->typelib;
unknown's avatar
unknown committed
2747 2748 2749 2750 2751 2752
    options->def_value= *(ulong*) ((int*) (opt + 1) + 1);
    options->min_value= options->block_size= 0;
    options->max_value= options->typelib->count - 1;
    break;
  case PLUGIN_VAR_SET | PLUGIN_VAR_THDLOCAL:
    options->var_type= GET_SET;
unknown's avatar
unknown committed
2753 2754
    options->typelib= ((thdvar_set_t*) opt)->typelib;
    options->def_value= *(ulonglong*) ((int*) (opt + 1) + 1);
unknown's avatar
unknown committed
2755 2756 2757 2758 2759 2760 2761 2762
    options->min_value= options->block_size= 0;
    options->max_value= (ULL(1) << options->typelib->count) - 1;
    break;
  case PLUGIN_VAR_BOOL | PLUGIN_VAR_THDLOCAL:
    options->var_type= GET_BOOL;
    options->def_value= *(my_bool*) ((int*) (opt + 1) + 1);
    break;
  case PLUGIN_VAR_STR | PLUGIN_VAR_THDLOCAL:
2763 2764 2765
    options->var_type= ((opt->flags & PLUGIN_VAR_MEMALLOC) ?
                        GET_STR_ALLOC : GET_STR);
    options->def_value= (intptr) *((char**) ((void**) (opt + 1) + 1));
unknown's avatar
unknown committed
2766 2767 2768 2769 2770 2771 2772 2773
    break;
  default:
    DBUG_ASSERT(0);
  }
  options->arg_type= REQUIRED_ARG;
  if (opt->flags & PLUGIN_VAR_NOCMDARG)
    options->arg_type= NO_ARG;
  if (opt->flags & PLUGIN_VAR_OPCMDARG)
unknown's avatar
unknown committed
2774
    options->arg_type= OPT_ARG;
unknown's avatar
unknown committed
2775 2776
}

2777 2778
extern "C" my_bool get_one_plugin_option(int optid, const struct my_option *,
                                         char *);
unknown's avatar
unknown committed
2779

2780
my_bool get_one_plugin_option(int optid __attribute__((unused)),
unknown's avatar
unknown committed
2781 2782 2783 2784 2785 2786 2787
                              const struct my_option *opt,
                              char *argument)
{
  return 0;
}


unknown's avatar
unknown committed
2788 2789 2790
static int construct_options(MEM_ROOT *mem_root, struct st_plugin_int *tmp,
                             my_option *options, my_bool **enabled,
                             bool can_disable)
unknown's avatar
unknown committed
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
{
  const char *plugin_name= tmp->plugin->name;
  uint namelen= strlen(plugin_name), optnamelen;
  uint buffer_length= namelen * 4 + (can_disable ? 75 : 10);
  char *name= (char*) alloc_root(mem_root, buffer_length) + 1;
  char *optname, *p;
  int index= 0, offset= 0;
  st_mysql_sys_var *opt, **plugin_option;
  st_bookmark *v;
  DBUG_ENTER("construct_options");
2801
  DBUG_PRINT("plugin", ("plugin: '%s'  enabled: %d  can_disable: %d",
unknown's avatar
unknown committed
2802
                        plugin_name, **enabled, can_disable));
unknown's avatar
unknown committed
2803

unknown's avatar
unknown committed
2804 2805 2806 2807
  /* support --skip-plugin-foo syntax */
  memcpy(name, plugin_name, namelen + 1);
  my_casedn_str(&my_charset_latin1, name);
  strxmov(name + namelen + 1, "plugin-", name, NullS);
2808
  /* Now we have namelen + 1 + 7 + namelen + 1 == namelen * 2 + 9. */
unknown's avatar
unknown committed
2809

unknown's avatar
unknown committed
2810 2811 2812
  for (p= name + namelen*2 + 8; p > name; p--)
    if (*p == '_')
      *p= '-';
unknown's avatar
unknown committed
2813

unknown's avatar
unknown committed
2814 2815 2816
  if (can_disable)
  {
    strxmov(name + namelen*2 + 10, "Enable ", plugin_name, " plugin. "
unknown's avatar
unknown committed
2817
            "Disable with --skip-", name," (will save memory).", NullS);
2818 2819 2820 2821
    /*
      Now we have namelen * 2 + 10 (one char unused) + 7 + namelen + 9 +
      20 + namelen + 20 + 1 == namelen * 4 + 67.
    */
unknown's avatar
unknown committed
2822 2823 2824 2825

    options[0].comment= name + namelen*2 + 10;
  }

2826 2827 2828 2829
  /*
    NOTE: 'name' is one char above the allocated buffer!
    NOTE: This code assumes that 'my_bool' and 'char' are of same size.
  */
unknown's avatar
unknown committed
2830 2831
  *((my_bool *)(name -1))= **enabled;
  *enabled= (my_bool *)(name - 1);
unknown's avatar
unknown committed
2832 2833


unknown's avatar
unknown committed
2834 2835 2836 2837 2838 2839
  options[1].name= (options[0].name= name) + namelen + 1;
  options[0].id= options[1].id= 256; /* must be >255. dup id ok */
  options[0].var_type= options[1].var_type= GET_BOOL;
  options[0].arg_type= options[1].arg_type= NO_ARG;
  options[0].def_value= options[1].def_value= **enabled;
  options[0].value= options[0].u_max_value=
unknown's avatar
unknown committed
2840
  options[1].value= options[1].u_max_value= (uchar**) (name - 1);
unknown's avatar
unknown committed
2841 2842 2843 2844 2845 2846 2847
  options+= 2;

  /*
    Two passes as the 2nd pass will take pointer addresses for use
    by my_getopt and register_var() in the first pass uses realloc
  */

unknown's avatar
unknown committed
2848
  for (plugin_option= tmp->plugin->system_vars;
unknown's avatar
unknown committed
2849 2850
       plugin_option && *plugin_option; plugin_option++, index++)
  {
unknown's avatar
unknown committed
2851
    opt= *plugin_option;
unknown's avatar
unknown committed
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872
    if (!(opt->flags & PLUGIN_VAR_THDLOCAL))
      continue;
    if (!(register_var(name, opt->name, opt->flags)))
      continue;
    switch (opt->flags & PLUGIN_VAR_TYPEMASK) {
    case PLUGIN_VAR_BOOL:
      SET_PLUGIN_VAR_RESOLVE((thdvar_bool_t *) opt);
      break;
    case PLUGIN_VAR_INT:
      SET_PLUGIN_VAR_RESOLVE((thdvar_int_t *) opt);
      break;
    case PLUGIN_VAR_LONG:
      SET_PLUGIN_VAR_RESOLVE((thdvar_long_t *) opt);
      break;
    case PLUGIN_VAR_LONGLONG:
      SET_PLUGIN_VAR_RESOLVE((thdvar_longlong_t *) opt);
      break;
    case PLUGIN_VAR_STR:
      SET_PLUGIN_VAR_RESOLVE((thdvar_str_t *) opt);
      break;
    case PLUGIN_VAR_ENUM:
unknown's avatar
unknown committed
2873 2874
      SET_PLUGIN_VAR_RESOLVE((thdvar_enum_t *) opt);
      break;
unknown's avatar
unknown committed
2875
    case PLUGIN_VAR_SET:
unknown's avatar
unknown committed
2876
      SET_PLUGIN_VAR_RESOLVE((thdvar_set_t *) opt);
unknown's avatar
unknown committed
2877 2878 2879
      break;
    default:
      sql_print_error("Unknown variable type code 0x%x in plugin '%s'.",
unknown's avatar
unknown committed
2880
                      opt->flags, plugin_name);
unknown's avatar
unknown committed
2881 2882 2883 2884
      DBUG_RETURN(-1);
    };
  }

unknown's avatar
unknown committed
2885
  for (plugin_option= tmp->plugin->system_vars;
unknown's avatar
unknown committed
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
       plugin_option && *plugin_option; plugin_option++, index++)
  {
    switch ((opt= *plugin_option)->flags & PLUGIN_VAR_TYPEMASK) {
    case PLUGIN_VAR_BOOL:
      if (!opt->check)
        opt->check= check_func_bool;
      if (!opt->update)
        opt->update= update_func_bool;
      break;
    case PLUGIN_VAR_INT:
      if (!opt->check)
        opt->check= check_func_int;
      if (!opt->update)
        opt->update= update_func_int;
      break;
    case PLUGIN_VAR_LONG:
      if (!opt->check)
        opt->check= check_func_long;
      if (!opt->update)
        opt->update= update_func_long;
      break;
    case PLUGIN_VAR_LONGLONG:
      if (!opt->check)
        opt->check= check_func_longlong;
      if (!opt->update)
        opt->update= update_func_longlong;
      break;
    case PLUGIN_VAR_STR:
      if (!opt->check)
        opt->check= check_func_str;
      if (!opt->update)
      {
        opt->update= update_func_str;
2919
        if (!(opt->flags & PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_READONLY))
unknown's avatar
unknown committed
2920
        {
2921
          opt->flags|= PLUGIN_VAR_READONLY;
unknown's avatar
unknown committed
2922 2923 2924 2925 2926
          sql_print_warning("Server variable %s of plugin %s was forced "
                            "to be read-only: string variable without "
                            "update_func and PLUGIN_VAR_MEMALLOC flag",
                            opt->name, plugin_name);
        }
unknown's avatar
unknown committed
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
      }
      break;
    case PLUGIN_VAR_ENUM:
      if (!opt->check)
        opt->check= check_func_enum;
      if (!opt->update)
        opt->update= update_func_long;
      break;
    case PLUGIN_VAR_SET:
      if (!opt->check)
        opt->check= check_func_set;
      if (!opt->update)
unknown's avatar
unknown committed
2939
        opt->update= update_func_longlong;
unknown's avatar
unknown committed
2940 2941 2942
      break;
    default:
      sql_print_error("Unknown variable type code 0x%x in plugin '%s'.",
unknown's avatar
unknown committed
2943
                      opt->flags, plugin_name);
unknown's avatar
unknown committed
2944 2945
      DBUG_RETURN(-1);
    }
unknown's avatar
unknown committed
2946

unknown's avatar
unknown committed
2947 2948 2949 2950 2951 2952
    if (opt->flags & PLUGIN_VAR_NOCMDOPT)
      continue;

    if (!opt->name)
    {
      sql_print_error("Missing variable name in plugin '%s'.",
unknown's avatar
unknown committed
2953
                      plugin_name);
unknown's avatar
unknown committed
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
      DBUG_RETURN(-1);
    }

    if (!(v= find_bookmark(name, opt->name, opt->flags)))
    {
      optnamelen= strlen(opt->name);
      optname= (char*) alloc_root(mem_root, namelen + optnamelen + 2);
      strxmov(optname, name, "-", opt->name, NullS);
      optnamelen= namelen + optnamelen + 1;
    }
    else
unknown's avatar
unknown committed
2965
      optname= (char*) memdup_root(mem_root, v->key + 1, (optnamelen= v->name_len) + 1);
unknown's avatar
unknown committed
2966 2967 2968 2969 2970 2971 2972 2973

    /* convert '_' to '-' */
    for (p= optname; *p; p++)
      if (*p == '_')
        *p= '-';

    options->name= optname;
    options->comment= opt->comment;
unknown's avatar
unknown committed
2974
    options->app_type= opt;
unknown's avatar
unknown committed
2975
    options->id= (options-1)->id + 1;
unknown's avatar
unknown committed
2976

unknown's avatar
unknown committed
2977 2978 2979 2980
    if (opt->flags & PLUGIN_VAR_THDLOCAL)
      *(int*)(opt + 1)= offset= v->offset;

    plugin_opt_set_limits(options, opt);
unknown's avatar
unknown committed
2981

unknown's avatar
unknown committed
2982 2983 2984 2985
    if ((opt->flags & PLUGIN_VAR_TYPEMASK) != PLUGIN_VAR_ENUM &&
        (opt->flags & PLUGIN_VAR_TYPEMASK) != PLUGIN_VAR_SET)
    {
      if (opt->flags & PLUGIN_VAR_THDLOCAL)
unknown's avatar
unknown committed
2986
        options->value= options->u_max_value= (uchar**)
unknown's avatar
unknown committed
2987 2988
          (global_system_variables.dynamic_variables_ptr + offset);
      else
unknown's avatar
unknown committed
2989
        options->value= options->u_max_value= *(uchar***) (opt + 1);
unknown's avatar
unknown committed
2990 2991 2992 2993
    }

    options[1]= options[0];
    options[1].name= p= (char*) alloc_root(mem_root, optnamelen + 8);
unknown's avatar
unknown committed
2994
    options[1].comment= 0; // hidden
unknown's avatar
unknown committed
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
    strxmov(p, "plugin-", optname, NullS);

    options+= 2;
  }

  DBUG_RETURN(0);
}


static my_option *construct_help_options(MEM_ROOT *mem_root,
                                         struct st_plugin_int *p)
{
  st_mysql_sys_var **opt;
  my_option *opts;
unknown's avatar
unknown committed
3009
  my_bool dummy, can_disable;
unknown's avatar
unknown committed
3010 3011 3012 3013
  my_bool *dummy2= &dummy;
  uint count= EXTRA_OPTIONS;
  DBUG_ENTER("construct_help_options");

unknown's avatar
unknown committed
3014
  for (opt= p->plugin->system_vars; opt && *opt; opt++, count+= 2);
unknown's avatar
unknown committed
3015 3016 3017 3018 3019 3020

  if (!(opts= (my_option*) alloc_root(mem_root, sizeof(my_option) * count)))
    DBUG_RETURN(NULL);

  bzero(opts, sizeof(my_option) * count);

3021 3022
  dummy= TRUE; /* plugin is enabled. */

unknown's avatar
unknown committed
3023 3024 3025 3026 3027
  can_disable=
      my_strcasecmp(&my_charset_latin1, p->name.str, "MyISAM") &&
      my_strcasecmp(&my_charset_latin1, p->name.str, "MEMORY");

  if (construct_options(mem_root, p, opts, &dummy2, can_disable))
unknown's avatar
unknown committed
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052
    DBUG_RETURN(NULL);

  DBUG_RETURN(opts);
}


/*
  SYNOPSIS
    test_plugin_options()
    tmp_root                    temporary scratch space
    plugin                      internal plugin structure
    argc                        user supplied arguments
    argv                        user supplied arguments
    default_enabled             default plugin enable status
  RETURNS:
    0 SUCCESS - plugin should be enabled/loaded
  NOTE:
    Requires that a write-lock is held on LOCK_system_variables_hash
*/
static int test_plugin_options(MEM_ROOT *tmp_root, struct st_plugin_int *tmp,
                               int *argc, char **argv, my_bool default_enabled)
{
  struct sys_var_chain chain= { NULL, NULL };
  my_bool enabled_saved= default_enabled, can_disable;
  my_bool *enabled= &default_enabled;
unknown's avatar
unknown committed
3053
  MEM_ROOT *mem_root= alloc_root_inited(&tmp->mem_root) ?
unknown's avatar
unknown committed
3054 3055
                      &tmp->mem_root : &plugin_mem_root;
  st_mysql_sys_var **opt;
3056
  my_option *opts= NULL;
unknown's avatar
unknown committed
3057 3058 3059 3060 3061 3062 3063
  char *p, *varname;
  int error;
  st_mysql_sys_var *o;
  sys_var *v;
  struct st_bookmark *var;
  uint len, count= EXTRA_OPTIONS;
  DBUG_ENTER("test_plugin_options");
unknown's avatar
unknown committed
3064
  DBUG_ASSERT(tmp->plugin && tmp->name.str);
unknown's avatar
unknown committed
3065 3066 3067 3068 3069

  for (opt= tmp->plugin->system_vars; opt && *opt; opt++)
    count+= 2; /* --{plugin}-{optname} and --plugin-{plugin}-{optname} */

  can_disable=
unknown's avatar
unknown committed
3070 3071
      my_strcasecmp(&my_charset_latin1, tmp->name.str, "MyISAM") &&
      my_strcasecmp(&my_charset_latin1, tmp->name.str, "MEMORY");
unknown's avatar
unknown committed
3072

unknown's avatar
unknown committed
3073
  if (count > EXTRA_OPTIONS || (*argc > 1))
unknown's avatar
unknown committed
3074
  {
unknown's avatar
unknown committed
3075 3076
    if (!(opts= (my_option*) alloc_root(tmp_root, sizeof(my_option) * count)))
    {
unknown's avatar
unknown committed
3077
      sql_print_error("Out of memory for plugin '%s'.", tmp->name.str);
unknown's avatar
unknown committed
3078 3079 3080
      DBUG_RETURN(-1);
    }
    bzero(opts, sizeof(my_option) * count);
unknown's avatar
unknown committed
3081

unknown's avatar
unknown committed
3082 3083
    if (construct_options(tmp_root, tmp, opts, &enabled, can_disable))
    {
unknown's avatar
unknown committed
3084
      sql_print_error("Bad options for plugin '%s'.", tmp->name.str);
unknown's avatar
unknown committed
3085 3086 3087
      DBUG_RETURN(-1);
    }

3088
    error= handle_options(argc, &argv, opts, get_one_plugin_option);
unknown's avatar
unknown committed
3089
    (*argc)++; /* add back one for the program name */
unknown's avatar
unknown committed
3090

unknown's avatar
unknown committed
3091 3092
    if (error)
    {
unknown's avatar
unknown committed
3093
       sql_print_error("Parsing options for plugin '%s' failed.",
unknown's avatar
unknown committed
3094
                       tmp->name.str);
unknown's avatar
unknown committed
3095
       goto err;
unknown's avatar
unknown committed
3096 3097 3098 3099 3100
    }
  }

  if (!*enabled && !can_disable)
  {
unknown's avatar
unknown committed
3101
    sql_print_warning("Plugin '%s' cannot be disabled", tmp->name.str);
unknown's avatar
unknown committed
3102 3103 3104
    *enabled= TRUE;
  }

unknown's avatar
unknown committed
3105 3106
  error= 1;

unknown's avatar
unknown committed
3107
  if (*enabled)
unknown's avatar
unknown committed
3108
  {
unknown's avatar
unknown committed
3109 3110 3111 3112
    for (opt= tmp->plugin->system_vars; opt && *opt; opt++)
    {
      if (((o= *opt)->flags & PLUGIN_VAR_NOSYSVAR))
        continue;
unknown's avatar
unknown committed
3113

unknown's avatar
unknown committed
3114
      if ((var= find_bookmark(tmp->name.str, o->name, o->flags)))
unknown's avatar
unknown committed
3115
        v= new (mem_root) sys_var_pluginvar(var->key + 1, o);
unknown's avatar
unknown committed
3116 3117
      else
      {
unknown's avatar
unknown committed
3118
        len= tmp->name.length + strlen(o->name) + 2;
unknown's avatar
unknown committed
3119
        varname= (char*) alloc_root(mem_root, len);
unknown's avatar
unknown committed
3120
        strxmov(varname, tmp->name.str, "-", o->name, NullS);
unknown's avatar
unknown committed
3121 3122 3123 3124 3125 3126
        my_casedn_str(&my_charset_latin1, varname);

        for (p= varname; *p; p++)
          if (*p == '-')
            *p= '_';

unknown's avatar
unknown committed
3127
        v= new (mem_root) sys_var_pluginvar(varname, o);
unknown's avatar
unknown committed
3128 3129 3130
      }
      DBUG_ASSERT(v); /* check that an object was actually constructed */

unknown's avatar
unknown committed
3131 3132
      /*
        Add to the chain of variables.
unknown's avatar
unknown committed
3133
        Done like this for easier debugging so that the
unknown's avatar
unknown committed
3134 3135
        pointer to v is not lost on optimized builds.
      */
unknown's avatar
unknown committed
3136 3137 3138 3139 3140 3141 3142 3143
      v->chain_sys_var(&chain);
    }
    if (chain.first)
    {
      chain.last->next = NULL;
      if (mysql_add_sys_var_chain(chain.first, NULL))
      {
        sql_print_error("Plugin '%s' has conflicting system variables",
unknown's avatar
unknown committed
3144
                        tmp->name.str);
unknown's avatar
unknown committed
3145
        goto err;
unknown's avatar
unknown committed
3146 3147 3148 3149 3150 3151
      }
      tmp->system_vars= chain.first;
    }
    DBUG_RETURN(0);
  }

3152
  if (enabled_saved && global_system_variables.log_warnings)
unknown's avatar
unknown committed
3153
    sql_print_information("Plugin '%s' disabled by command line option",
unknown's avatar
unknown committed
3154
                          tmp->name.str);
unknown's avatar
unknown committed
3155
err:
3156 3157
  if (opts)
    my_cleanup_options(opts);
unknown's avatar
unknown committed
3158
  DBUG_RETURN(error);
unknown's avatar
unknown committed
3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180
}


/****************************************************************************
  Help Verbose text with Plugin System Variables
****************************************************************************/

static int option_cmp(my_option *a, my_option *b)
{
  return my_strcasecmp(&my_charset_latin1, a->name, b->name);
}


void my_print_help_inc_plugins(my_option *main_options, uint size)
{
  DYNAMIC_ARRAY all_options;
  struct st_plugin_int *p;
  MEM_ROOT mem_root;
  my_option *opt;

  init_alloc_root(&mem_root, 4096, 4096);
  my_init_dynamic_array(&all_options, sizeof(my_option), size, size/4);
unknown's avatar
unknown committed
3181

unknown's avatar
unknown committed
3182 3183 3184
  if (initialized)
    for (uint idx= 0; idx < plugin_array.elements; idx++)
    {
unknown's avatar
unknown committed
3185
      p= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
unknown's avatar
unknown committed
3186 3187 3188 3189 3190 3191 3192 3193

      if (!p->plugin->system_vars ||
          !(opt= construct_help_options(&mem_root, p)))
        continue;

      /* Only options with a non-NULL comment are displayed in help text */
      for (;opt->id; opt++)
        if (opt->comment)
unknown's avatar
unknown committed
3194
          insert_dynamic(&all_options, (uchar*) opt);
unknown's avatar
unknown committed
3195
    }
unknown's avatar
unknown committed
3196

unknown's avatar
unknown committed
3197
  for (;main_options->id; main_options++)
unknown's avatar
unknown committed
3198
    insert_dynamic(&all_options, (uchar*) main_options);
unknown's avatar
unknown committed
3199

unknown's avatar
unknown committed
3200
  sort_dynamic(&all_options, (qsort_cmp) option_cmp);
unknown's avatar
unknown committed
3201

unknown's avatar
unknown committed
3202
  /* main_options now points to the empty option terminator */
unknown's avatar
unknown committed
3203
  insert_dynamic(&all_options, (uchar*) main_options);
unknown's avatar
unknown committed
3204

unknown's avatar
unknown committed
3205 3206 3207 3208 3209 3210 3211
  my_print_help((my_option*) all_options.buffer);
  my_print_variables((my_option*) all_options.buffer);

  delete_dynamic(&all_options);
  free_root(&mem_root, MYF(0));
}