sql_plugin.cc 98.8 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
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>
antony@ppcg5.local's avatar
antony@ppcg5.local committed
18
#include <my_getopt.h>
19 20 21
#define REPORT_TO_LOG  1
#define REPORT_TO_USER 2

antony@ppcg5.local's avatar
antony@ppcg5.local 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

acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
30 31
extern struct st_mysql_plugin *mysqld_builtins[];

32 33 34 35 36 37 38 39 40 41 42 43
/**
  @note The order of the enumeration is critical.
  @see construct_options
*/
static const char *global_plugin_typelib_names[]=
  { "OFF", "ON", "FORCE", NULL };
enum enum_plugin_load_policy {PLUGIN_OFF, PLUGIN_ON, PLUGIN_FORCE};
static TYPELIB global_plugin_typelib=
  { array_elements(global_plugin_typelib_names)-1,
    "", global_plugin_typelib_names, NULL };


antony@ppcg5.local's avatar
antony@ppcg5.local committed
44
char *opt_plugin_load= NULL;
45 46
char *opt_plugin_dir_ptr;
char opt_plugin_dir[FN_REFLEN];
47
/*
serg@janus.mylan's avatar
serg@janus.mylan committed
48
  When you ad a new plugin type, add both a string and make sure that the
49 50
  init and deinit array are correctly updated.
*/
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
51
const LEX_STRING plugin_type_names[MYSQL_MAX_PLUGIN_TYPE_NUM]=
52
{
andrey@example.com's avatar
andrey@example.com committed
53 54
  { C_STRING_WITH_LEN("UDF") },
  { C_STRING_WITH_LEN("STORAGE ENGINE") },
55
  { C_STRING_WITH_LEN("FTPARSER") },
brian@zim.(none)'s avatar
brian@zim.(none) committed
56 57
  { C_STRING_WITH_LEN("DAEMON") },
  { C_STRING_WITH_LEN("INFORMATION SCHEMA") }
58
};
59

brian@zim.(none)'s avatar
brian@zim.(none) committed
60 61 62 63
extern int initialize_schema_table(st_plugin_int *plugin);
extern int finalize_schema_table(st_plugin_int *plugin);

/*
serg@janus.mylan's avatar
serg@janus.mylan committed
64
  The number of elements in both plugin_type_initialize and
brian@zim.(none)'s avatar
brian@zim.(none) committed
65 66
  plugin_type_deinitialize should equal to the number of plugins
  defined.
serg@janus.mylan's avatar
serg@janus.mylan committed
67
*/
68 69
plugin_type_init plugin_type_initialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
brian@zim.(none)'s avatar
brian@zim.(none) committed
70
  0,ha_initialize_handlerton,0,0,initialize_schema_table
71 72
};

73 74
plugin_type_init plugin_type_deinitialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
brian@zim.(none)'s avatar
brian@zim.(none) committed
75
  0,ha_finalize_handlerton,0,0,finalize_schema_table
76 77
};

78
#ifdef HAVE_DLOPEN
79 80
static const char *plugin_interface_version_sym=
                   "_mysql_plugin_interface_version_";
81 82
static const char *sizeof_st_plugin_sym=
                   "_mysql_sizeof_struct_st_plugin_";
83
static const char *plugin_declarations_sym= "_mysql_plugin_declarations_";
serg@janus.mylan's avatar
serg@janus.mylan committed
84
static int min_plugin_interface_version= MYSQL_PLUGIN_INTERFACE_VERSION & ~0xFF;
85 86
#endif

87 88 89 90 91 92
/* 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,
93
  MYSQL_HANDLERTON_INTERFACE_VERSION,
94
  MYSQL_FTPARSER_INTERFACE_VERSION,
brian@zim.(none)'s avatar
brian@zim.(none) committed
95 96
  MYSQL_DAEMON_INTERFACE_VERSION,
  MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION
97 98 99 100
};
static int cur_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
  0x0000, /* UDF: not implemented */
serg@serg.mylan's avatar
serg@serg.mylan committed
101
  MYSQL_HANDLERTON_INTERFACE_VERSION,
102
  MYSQL_FTPARSER_INTERFACE_VERSION,
brian@zim.(none)'s avatar
brian@zim.(none) committed
103 104
  MYSQL_DAEMON_INTERFACE_VERSION,
  MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION
105
};
106

serg@janus.mylan's avatar
serg@janus.mylan committed
107 108 109 110 111 112 113
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.
*/
114
pthread_mutex_t LOCK_plugin;
115 116 117
static DYNAMIC_ARRAY plugin_dl_array;
static DYNAMIC_ARRAY plugin_array;
static HASH plugin_hash[MYSQL_MAX_PLUGIN_TYPE_NUM];
antony@ppcg5.local's avatar
antony@ppcg5.local committed
118
static bool reap_needed= false;
119 120
static int plugin_array_version=0;

antony@ppcg5.local's avatar
antony@ppcg5.local committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/*
  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
serg@janus.mylan's avatar
serg@janus.mylan committed
142
  hash and is used to mark a single offset for a thd local variable
antony@ppcg5.local's avatar
antony@ppcg5.local committed
143 144
  even if plugins have been uninstalled and reinstalled, repeatedly.
  This structure is allocated from plugin_mem_root.
serg@janus.mylan's avatar
serg@janus.mylan committed
145

antony@ppcg5.local's avatar
antony@ppcg5.local committed
146 147 148 149
  The key format is as follows:
    1 byte         - variable type code
    name_len bytes - variable name
    '\0'           - end of key
antony@ppcg5.local's avatar
antony@ppcg5.local committed
150 151 152 153 154 155
*/
struct st_bookmark
{
  uint name_len;
  int offset;
  uint version;
serg@janus.mylan's avatar
serg@janus.mylan committed
156
  char key[1];
antony@ppcg5.local's avatar
antony@ppcg5.local committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
};


/*
  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)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
185
    :sys_var(name_arg), plugin_var(plugin_var_arg) {}
antony@ppcg5.local's avatar
antony@ppcg5.local committed
186 187 188 189 190 191
  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();
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
192
  uchar* real_value_ptr(THD *thd, enum_var_type type);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
193
  TYPELIB* plugin_var_typelib(void);
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
194
  uchar* value_ptr(THD *thd, enum_var_type type, LEX_STRING *base);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
195
  bool check(THD *thd, set_var *var);
serg@janus.mylan's avatar
serg@janus.mylan committed
196
  bool check_default(enum_var_type type) { return is_readonly(); }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
197 198 199 200 201
  void set_default(THD *thd, enum_var_type type);
  bool update(THD *thd, set_var *var);
};


202
/* prototypes */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
203 204 205
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);
serg@janus.mylan's avatar
serg@janus.mylan committed
206
static int test_plugin_options(MEM_ROOT *, struct st_plugin_int *,
207
                               int *, char **);
serg@janus.mylan's avatar
serg@janus.mylan committed
208 209
static bool register_builtin(struct st_mysql_plugin *, struct st_plugin_int *,
                             struct st_plugin_int **);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
210 211
static void unlock_variables(THD *thd, struct system_variables *vars);
static void cleanup_variables(THD *thd, struct system_variables *vars);
212
static void plugin_vars_free_values(sys_var *vars);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
213 214 215 216 217 218 219 220 221 222 223 224
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);
225 226
extern bool throw_bounds_warning(THD *thd, bool fixed, bool unsignd,
                                 const char *name, longlong val);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
227

antony@ppcg5.local's avatar
antony@ppcg5.local committed
228 229 230 231 232
#ifdef EMBEDDED_LIBRARY
/* declared in sql_base.cc */
extern bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists);
#endif /* EMBEDDED_LIBRARY */

antony@ppcg5.local's avatar
antony@ppcg5.local committed
233 234 235 236 237 238 239 240 241 242 243 244 245

/****************************************************************************
  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:
serg@janus.mylan's avatar
serg@janus.mylan committed
246
    return MYSQL_VALUE_TYPE_STRING;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
247 248 249
  }
}

serg@janus.mylan's avatar
serg@janus.mylan committed
250
static const char *item_val_str(struct st_mysql_value *value,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
251 252 253 254 255 256 257 258
                                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;
serg@janus.mylan's avatar
serg@janus.mylan committed
259

antony@ppcg5.local's avatar
antony@ppcg5.local committed
260
  /*
serg@janus.mylan's avatar
serg@janus.mylan committed
261
    Lets be nice and create a temporary string since the
antony@ppcg5.local's avatar
antony@ppcg5.local committed
262 263 264 265 266 267
    buffer was too small
  */
  return current_thd->strmake(res->c_ptr_quick(), res->length());
}


serg@janus.mylan's avatar
serg@janus.mylan committed
268
static int item_val_int(struct st_mysql_value *value, long long *buf)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
269 270
{
  Item *item= ((st_item_value_holder*)value)->item;
serg@janus.mylan's avatar
serg@janus.mylan committed
271
  *buf= item->val_int();
antony@ppcg5.local's avatar
antony@ppcg5.local committed
272 273 274 275 276 277
  if (item->is_null())
    return 1;
  return 0;
}


serg@janus.mylan's avatar
serg@janus.mylan committed
278
static int item_val_real(struct st_mysql_value *value, double *buf)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
279 280
{
  Item *item= ((st_item_value_holder*)value)->item;
serg@janus.mylan's avatar
serg@janus.mylan committed
281
  *buf= item->val_real();
antony@ppcg5.local's avatar
antony@ppcg5.local committed
282 283 284 285 286 287 288 289 290
  if (item->is_null())
    return 1;
  return 0;
}


/****************************************************************************
  Plugin support code
****************************************************************************/
291

292 293
#ifdef HAVE_DLOPEN

acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
294
static struct st_plugin_dl *plugin_dl_find(const LEX_STRING *dl)
295 296
{
  uint i;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
297
  struct st_plugin_dl *tmp;
298 299 300
  DBUG_ENTER("plugin_dl_find");
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
301
    tmp= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
302 303 304 305 306 307 308 309 310 311
    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);
}


312 313 314
static st_plugin_dl *plugin_dl_insert_or_reuse(struct st_plugin_dl *plugin_dl)
{
  uint i;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
315
  struct st_plugin_dl *tmp;
316 317 318
  DBUG_ENTER("plugin_dl_insert_or_reuse");
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
319
    tmp= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
320 321 322 323 324 325
    if (! tmp->ref_count)
    {
      memcpy(tmp, plugin_dl, sizeof(struct st_plugin_dl));
      DBUG_RETURN(tmp);
    }
  }
326
  if (insert_dynamic(&plugin_dl_array, (uchar*)&plugin_dl))
327
    DBUG_RETURN(0);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
328 329
  tmp= *dynamic_element(&plugin_dl_array, plugin_dl_array.elements - 1,
                        struct st_plugin_dl **)=
istruewing@chilla.local's avatar
istruewing@chilla.local committed
330
      (struct st_plugin_dl *) memdup_root(&plugin_mem_root, (uchar*)plugin_dl,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
331 332
                                           sizeof(struct st_plugin_dl));
  DBUG_RETURN(tmp);
333
}
334 335
#endif /* HAVE_DLOPEN */

336

337 338
static inline void free_plugin_mem(struct st_plugin_dl *p)
{
339
#ifdef HAVE_DLOPEN
340 341
  if (p->handle)
    dlclose(p->handle);
342
#endif
343 344
  my_free(p->dl.str, MYF(MY_ALLOW_ZERO_PTR));
  if (p->version != MYSQL_PLUGIN_INTERFACE_VERSION)
345
    my_free((uchar*)p->plugins, MYF(MY_ALLOW_ZERO_PTR));
346
}
347

antony@ppcg5.local's avatar
antony@ppcg5.local committed
348

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

  if (plugin_dl.version != MYSQL_PLUGIN_INTERFACE_VERSION)
  {
439 440
    int i;
    uint sizeof_st_plugin;
441
    struct st_mysql_plugin *old, *cur;
442
    char *ptr= (char *)sym;
443 444 445 446 447 448 449 450 451 452 453 454 455

    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
456 457 458 459
      /*
        When the following assert starts failing, we'll have to switch
        to the upper branch of the #ifdef
      */
460
      DBUG_ASSERT(min_plugin_interface_version == 0);
461
      sizeof_st_plugin= (int)offsetof(struct st_mysql_plugin, version);
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
#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);
    }
481 482 483 484 485
    /*
      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.
    */
486 487 488
    for (i=0;
         (old=(struct st_mysql_plugin *)(ptr+i*sizeof_st_plugin))->info;
         i++)
489
      memcpy(cur+i, old, min(sizeof(cur[i]), sizeof_st_plugin));
490

491
    sym= cur;
492
  }
493
  plugin_dl.plugins= (struct st_mysql_plugin *)sym;
494

495 496
  /* Duplicate and convert dll name */
  plugin_dl.dl.length= dl->length * files_charset_info->mbmaxlen + 1;
497
  if (! (plugin_dl.dl.str= (char*) my_malloc(plugin_dl.dl.length, MYF(0))))
498
  {
499
    free_plugin_mem(&plugin_dl);
500 501 502 503 504 505 506 507 508 509 510
    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 */
511
  if (! (tmp= plugin_dl_insert_or_reuse(&plugin_dl)))
512
  {
513
    free_plugin_mem(&plugin_dl);
514 515 516 517 518 519
    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);
  }
520
  DBUG_RETURN(tmp);
521 522 523 524 525 526 527 528 529 530 531
#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
}


acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
532
static void plugin_dl_del(const LEX_STRING *dl)
533 534 535 536
{
#ifdef HAVE_DLOPEN
  uint i;
  DBUG_ENTER("plugin_dl_del");
antony@ppcg5.local's avatar
antony@ppcg5.local committed
537 538 539

  safe_mutex_assert_owner(&LOCK_plugin);

540 541
  for (i= 0; i < plugin_dl_array.elements; i++)
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
542 543
    struct st_plugin_dl *tmp= *dynamic_element(&plugin_dl_array, i,
                                               struct st_plugin_dl **);
544 545 546 547 548 549 550 551
    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)
      {
552
        free_plugin_mem(tmp);
553 554 555 556 557 558 559 560 561 562
        bzero(tmp, sizeof(struct st_plugin_dl));
      }
      break;
    }
  }
  DBUG_VOID_RETURN;
#endif
}


acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
563
static struct st_plugin_int *plugin_find_internal(const LEX_STRING *name, int type)
564 565 566 567 568
{
  uint i;
  DBUG_ENTER("plugin_find_internal");
  if (! initialized)
    DBUG_RETURN(0);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
569 570 571

  safe_mutex_assert_owner(&LOCK_plugin);

572 573 574 575 576
  if (type == MYSQL_ANY_PLUGIN)
  {
    for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
    {
      struct st_plugin_int *plugin= (st_plugin_int *)
577
        hash_search(&plugin_hash[i], (const uchar *)name->str, name->length);
578
      if (plugin)
579 580 581 582 583
        DBUG_RETURN(plugin);
    }
  }
  else
    DBUG_RETURN((st_plugin_int *)
584
        hash_search(&plugin_hash[type], (const uchar *)name->str, name->length));
585 586 587 588
  DBUG_RETURN(0);
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
589
static SHOW_COMP_OPTION plugin_status(const LEX_STRING *name, int type)
590
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
591
  SHOW_COMP_OPTION rc= SHOW_OPTION_NO;
592 593
  struct st_plugin_int *plugin;
  DBUG_ENTER("plugin_is_ready");
antony@ppcg5.local's avatar
antony@ppcg5.local committed
594 595 596 597 598 599 600 601
  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);
602 603 604 605
  DBUG_RETURN(rc);
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
606
bool plugin_is_ready(const LEX_STRING *name, int type)
607
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
  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");
serg@janus.mylan's avatar
serg@janus.mylan committed
626

antony@ppcg5.local's avatar
antony@ppcg5.local committed
627
  safe_mutex_assert_owner(&LOCK_plugin);
serg@janus.mylan's avatar
serg@janus.mylan committed
628

antony@ppcg5.local's avatar
antony@ppcg5.local committed
629
  if (pi->state & (PLUGIN_IS_READY | PLUGIN_IS_UNINITIALIZED))
630
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
631 632 633 634 635 636 637 638
    plugin_ref plugin;
#ifdef DBUG_OFF
    /* built-in plugins don't need ref counting */
    if (!pi->plugin_dl)
      DBUG_RETURN(pi);

    plugin= pi;
#else
antony@ppcg5.local's avatar
antony@ppcg5.local committed
639 640 641 642 643
    /*
      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.
    */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
644 645 646 647 648 649 650 651 652 653
    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)
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
654
      insert_dynamic(&lex->plugins, (uchar*)&plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
655
    DBUG_RETURN(plugin);
656
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
657 658 659 660 661 662
  DBUG_RETURN(NULL);
}


plugin_ref plugin_lock(THD *thd, plugin_ref *ptr CALLER_INFO_PROTO)
{
serg@janus.mylan's avatar
serg@janus.mylan committed
663
  LEX *lex= thd ? thd->lex : 0;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
664 665 666 667 668 669 670 671 672 673 674 675
  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)
{
serg@janus.mylan's avatar
serg@janus.mylan committed
676
  LEX *lex= thd ? thd->lex : 0;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
677 678
  plugin_ref rc= NULL;
  st_plugin_int *plugin;
679
  DBUG_ENTER("plugin_lock_by_name");
antony@ppcg5.local's avatar
antony@ppcg5.local committed
680 681 682 683
  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);
684 685 686 687
  DBUG_RETURN(rc);
}


688 689 690
static st_plugin_int *plugin_insert_or_reuse(struct st_plugin_int *plugin)
{
  uint i;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
691
  struct st_plugin_int *tmp;
692 693 694
  DBUG_ENTER("plugin_insert_or_reuse");
  for (i= 0; i < plugin_array.elements; i++)
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
695
    tmp= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
696 697 698 699 700 701
    if (tmp->state == PLUGIN_IS_FREED)
    {
      memcpy(tmp, plugin, sizeof(struct st_plugin_int));
      DBUG_RETURN(tmp);
    }
  }
702
  if (insert_dynamic(&plugin_array, (uchar*)&plugin))
703
    DBUG_RETURN(0);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
704 705
  tmp= *dynamic_element(&plugin_array, plugin_array.elements - 1,
                        struct st_plugin_int **)=
istruewing@chilla.local's avatar
istruewing@chilla.local committed
706
       (struct st_plugin_int *) memdup_root(&plugin_mem_root, (uchar*)plugin,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
707 708
                                            sizeof(struct st_plugin_int));
  DBUG_RETURN(tmp);
709 710
}

antony@ppcg5.local's avatar
antony@ppcg5.local committed
711 712 713 714 715 716

/*
  NOTE
    Requires that a write-lock is held on LOCK_system_variables_hash
*/
static bool plugin_add(MEM_ROOT *tmp_root,
serg@janus.mylan's avatar
serg@janus.mylan committed
717
                       const LEX_STRING *name, const LEX_STRING *dl,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
718
                       int *argc, char **argv, int report)
719 720 721 722 723 724 725 726 727 728 729 730
{
  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);
  }
731 732
  /* Clear the whole struct to catch future extensions. */
  bzero((char*) &tmp, sizeof(tmp));
733 734 735 736 737 738 739 740 741 742 743 744
  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))
    {
745 746 747 748 749 750 751 752
      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 ",
serg@serg.mylan's avatar
serg@serg.mylan committed
753 754
                 plugin_type_names[plugin->type].str,
                 " plugin is too different", NullS);
755 756 757 758 759 760
        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;
      }
761 762 763 764 765
      tmp.plugin= plugin;
      tmp.name.str= (char *)plugin->name;
      tmp.name.length= name_len;
      tmp.ref_count= 0;
      tmp.state= PLUGIN_IS_UNINITIALIZED;
766
      if (test_plugin_options(tmp_root, &tmp, argc, argv))
767 768 769
        tmp.state= PLUGIN_IS_DISABLED;

      if ((tmp_plugin_ptr= plugin_insert_or_reuse(&tmp)))
770
      {
771 772
        plugin_array_version++;
        if (!my_hash_insert(&plugin_hash[plugin->type], (uchar*)tmp_plugin_ptr))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
773
        {
774 775
          init_alloc_root(&tmp_plugin_ptr->mem_root, 4096, 4096);
          DBUG_RETURN(FALSE);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
776
        }
777
        tmp_plugin_ptr->state= PLUGIN_IS_FREED;
778
      }
779 780 781
      mysql_del_sys_var_chain(tmp.system_vars);
      goto err;

antony@ppcg5.local's avatar
antony@ppcg5.local committed
782 783
      /* plugin was disabled */
      plugin_dl_del(dl);
784 785 786 787 788 789 790 791 792 793 794 795 796
      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);
}


serg@janus.mylan's avatar
serg@janus.mylan committed
797
static void plugin_deinitialize(struct st_plugin_int *plugin, bool ref_check)
798
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
799 800 801 802 803
  /*
    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
  */
serg@janus.mylan's avatar
serg@janus.mylan committed
804
  safe_mutex_assert_not_owner(&LOCK_plugin);
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
  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 */
  }

826 827 828 829 830
  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",
831
                      plugin->name.str, plugin_type_names[plugin->plugin->type].str);
serg@janus.mylan's avatar
serg@janus.mylan committed
832
    }
833 834
  }
  else if (plugin->plugin->deinit)
835 836
  {
    DBUG_PRINT("info", ("Deinitializing plugin: '%s'", plugin->name.str));
837
    if (plugin->plugin->deinit(plugin))
838
    {
839 840
      DBUG_PRINT("warning", ("Plugin '%s' deinit function returned error.",
                             plugin->name.str));
841
    }
842 843
  }
  plugin->state= PLUGIN_IS_UNINITIALIZED;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
844 845 846 847 848 849 850 851

  /*
    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);
852 853 854
}


855 856 857
static void plugin_del(struct st_plugin_int *plugin)
{
  DBUG_ENTER("plugin_del(plugin)");
antony@ppcg5.local's avatar
antony@ppcg5.local committed
858
  safe_mutex_assert_owner(&LOCK_plugin);
859 860
  /* Free allocated strings before deleting the plugin. */
  plugin_vars_free_values(plugin->system_vars);
861
  hash_delete(&plugin_hash[plugin->plugin->type], (uchar*)plugin);
862 863
  if (plugin->plugin_dl)
    plugin_dl_del(&plugin->plugin_dl->dl);
864 865
  plugin->state= PLUGIN_IS_FREED;
  plugin_array_version++;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
866 867 868 869
  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));
870 871 872
  DBUG_VOID_RETURN;
}

873 874
#ifdef NOT_USED

875 876 877
static void plugin_del(const LEX_STRING *name)
{
  struct st_plugin_int *plugin;
878
  DBUG_ENTER("plugin_del(name)");
879
  if ((plugin= plugin_find_internal(name, MYSQL_ANY_PLUGIN)))
880
    plugin_del(plugin);
881 882 883
  DBUG_VOID_RETURN;
}

884 885
#endif

antony@ppcg5.local's avatar
antony@ppcg5.local committed
886
static void reap_plugins(void)
887
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
888 889
  uint count, idx;
  struct st_plugin_int *plugin, **reap, **list;
serg@janus.mylan's avatar
serg@janus.mylan committed
890

antony@ppcg5.local's avatar
antony@ppcg5.local committed
891 892 893 894
  safe_mutex_assert_owner(&LOCK_plugin);

  if (!reap_needed)
    return;
serg@janus.mylan's avatar
serg@janus.mylan committed
895

antony@ppcg5.local's avatar
antony@ppcg5.local committed
896 897 898 899
  reap_needed= false;
  count= plugin_array.elements;
  reap= (struct st_plugin_int **)my_alloca(sizeof(plugin)*(count+1));
  *(reap++)= NULL;
serg@janus.mylan's avatar
serg@janus.mylan committed
900

antony@ppcg5.local's avatar
antony@ppcg5.local committed
901
  for (idx= 0; idx < count; idx++)
902
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
903
    plugin= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
904 905 906 907 908 909 910
    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;
    }
  }
serg@janus.mylan's avatar
serg@janus.mylan committed
911

antony@ppcg5.local's avatar
antony@ppcg5.local committed
912 913 914 915
  pthread_mutex_unlock(&LOCK_plugin);

  list= reap;
  while ((plugin= *(--list)))
serg@janus.mylan's avatar
serg@janus.mylan committed
916
    plugin_deinitialize(plugin, true);
serg@janus.mylan's avatar
serg@janus.mylan committed
917

antony@ppcg5.local's avatar
antony@ppcg5.local committed
918
  pthread_mutex_lock(&LOCK_plugin);
serg@janus.mylan's avatar
serg@janus.mylan committed
919

antony@ppcg5.local's avatar
antony@ppcg5.local committed
920
  while ((plugin= *(--reap)))
921
    plugin_del(plugin);
serg@janus.mylan's avatar
serg@janus.mylan committed
922

antony@ppcg5.local's avatar
antony@ppcg5.local committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
  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
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
943
  my_free((uchar*) plugin, MYF(MY_WME));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
944 945 946 947 948 949
#endif

  DBUG_PRINT("info",("unlocking plugin, name= %s, ref_count= %d",
                     pi->name.str, pi->ref_count));
  if (lex)
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
950 951 952 953 954
    /*
      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.
    */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
955 956 957 958 959 960
    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;
      }
serg@janus.mylan's avatar
serg@janus.mylan committed
961
    DBUG_ASSERT(i >= 0);
962
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
963 964 965 966 967 968 969 970 971 972 973 974 975

  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)
{
serg@janus.mylan's avatar
serg@janus.mylan committed
976
  LEX *lex= thd ? thd->lex : 0;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
  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)
{
serg@janus.mylan's avatar
serg@janus.mylan committed
995
  LEX *lex= thd ? thd->lex : 0;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
996 997 998 999 1000 1001 1002
  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);
1003 1004 1005 1006
  DBUG_VOID_RETURN;
}


1007 1008 1009
static int plugin_initialize(struct st_plugin_int *plugin)
{
  DBUG_ENTER("plugin_initialize");
1010

serg@janus.mylan's avatar
serg@janus.mylan committed
1011
  safe_mutex_assert_owner(&LOCK_plugin);
1012 1013 1014 1015 1016
  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.",
1017
                      plugin->name.str, plugin_type_names[plugin->plugin->type].str);
1018 1019 1020 1021 1022
      goto err;
    }
  }
  else if (plugin->plugin->init)
  {
1023
    if (plugin->plugin->init(plugin))
1024 1025 1026 1027 1028 1029 1030 1031 1032
    {
      sql_print_error("Plugin '%s' init function returned error.",
                      plugin->name.str);
      goto err;
    }
  }

  plugin->state= PLUGIN_IS_READY;

1033 1034 1035
  if (plugin->plugin->status_vars)
  {
#ifdef FIX_LATER
1036
    /*
1037
      We have a problem right now where we can not prepend without
1038
      breaking backwards compatibility. We will fix this shortly so
1039 1040 1041 1042 1043
      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]= {
1044
      {plugin->plugin->name, (char*)plugin->plugin->status_vars, SHOW_ARRAY},
1045 1046 1047 1048 1049 1050 1051 1052 1053
      {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 */
  }

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
  /*
    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();
    }
  }
1069

1070 1071 1072 1073 1074
  DBUG_RETURN(0);
err:
  DBUG_RETURN(1);
}

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1075

1076 1077 1078 1079 1080 1081
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)))
1082 1083 1084
{
  struct st_plugin_int *plugin= (st_plugin_int *)buff;
  *length= (uint)plugin->name.length;
1085
  return((uchar *)plugin->name.str);
1086 1087 1088
}


1089 1090
uchar *get_bookmark_hash_key(const uchar *buff, size_t *length,
                             my_bool not_used __attribute__((unused)))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1091 1092 1093
{
  struct st_bookmark *var= (st_bookmark *)buff;
  *length= var->name_len + 1;
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
1094
  return (uchar*) var->key;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1095 1096
}

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
static inline void convert_dash_to_underscore(char *str, int len)
{
  for (char *p= str; p <= str+len; p++)
    if (*p == '-')
      *p= '_';
}

static inline void convert_underscore_to_dash(char *str, int len)
{
  for (char *p= str; p <= str+len; p++)
    if (*p == '_')
      *p= '-';
}

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1111

1112 1113 1114
/*
  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
1115
  skip this part).
1116

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1117
  Finally we initialize everything, aka the dynamic that have yet to initialize.
1118
*/
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1119
int plugin_init(int *argc, char **argv, int flags)
1120
{
1121
  uint i;
1122
  bool is_myisam;
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
1123 1124
  struct st_mysql_plugin **builtins;
  struct st_mysql_plugin *plugin;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1125 1126
  struct st_plugin_int tmp, *plugin_ptr, **reap;
  MEM_ROOT tmp_root;
1127
  bool reaped_mandatory_plugin= FALSE;
1128 1129 1130 1131 1132
  DBUG_ENTER("plugin_init");

  if (initialized)
    DBUG_RETURN(0);

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1133 1134 1135 1136 1137 1138
  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;
serg@janus.mylan's avatar
serg@janus.mylan committed
1139

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1140 1141

  pthread_mutex_init(&LOCK_plugin, MY_MUTEX_INIT_FAST);
1142 1143

  if (my_init_dynamic_array(&plugin_dl_array,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1144
                            sizeof(struct st_plugin_dl *),16,16) ||
1145
      my_init_dynamic_array(&plugin_array,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1146
                            sizeof(struct st_plugin_int *),16,16))
1147 1148 1149 1150 1151
    goto err;

  for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
  {
    if (hash_init(&plugin_hash[i], system_charset_info, 16, 0, 0,
1152
                  get_plugin_hash_key, NULL, HASH_UNIQUE))
1153 1154
      goto err;
  }
1155

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1156 1157 1158 1159
  pthread_mutex_lock(&LOCK_plugin);

  initialized= 1;

1160
  /*
1161 1162
    First we register builtin plugins
  */
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
1163 1164 1165 1166
  for (builtins= mysqld_builtins; *builtins; builtins++)
  {
    for (plugin= *builtins; plugin->info; plugin++)
    {
1167
      if (opt_ignore_builtin_innodb &&
1168 1169 1170
          !my_strnncoll(&my_charset_latin1, (const uchar*) plugin->name,
                        6, (const uchar*) "InnoDB", 6))
        continue;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1171 1172
      bzero(&tmp, sizeof(tmp));
      tmp.plugin= plugin;
serg@janus.mylan's avatar
serg@janus.mylan committed
1173 1174
      tmp.name.str= (char *)plugin->name;
      tmp.name.length= strlen(plugin->name);
1175
      tmp.state= 0;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1176
      free_root(&tmp_root, MYF(MY_MARK_BLOCKS_FREE));
1177
      if (test_plugin_options(&tmp_root, &tmp, argc, argv))
1178 1179 1180
        tmp.state= PLUGIN_IS_DISABLED;
      else
        tmp.state= PLUGIN_IS_UNINITIALIZED;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1181 1182 1183 1184
      if (register_builtin(plugin, &tmp, &plugin_ptr))
        goto err_unlock;

      /* only initialize MyISAM and CSV at this stage */
serg@janus.mylan's avatar
serg@janus.mylan committed
1185
      if (!(is_myisam=
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1186 1187 1188 1189
            !my_strcasecmp(&my_charset_latin1, plugin->name, "MyISAM")) &&
          my_strcasecmp(&my_charset_latin1, plugin->name, "CSV"))
        continue;

1190 1191
      if (plugin_ptr->state == PLUGIN_IS_UNINITIALIZED &&
          plugin_initialize(plugin_ptr))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1192 1193 1194
        goto err_unlock;

      /*
serg@janus.mylan's avatar
serg@janus.mylan committed
1195
        initialize the global default storage engine so that it may
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1196 1197 1198
        not be null in any child thread.
      */
      if (is_myisam)
1199
      {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1200
        DBUG_ASSERT(!global_system_variables.table_plugin);
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
1201 1202
        global_system_variables.table_plugin=
          my_intern_plugin_lock(NULL, plugin_int_to_ref(plugin_ptr));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1203
        DBUG_ASSERT(plugin_ptr->ref_count == 1);
1204
      }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
1205 1206
    }
  }
1207

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1208 1209
  /* should now be set to MyISAM storage engine */
  DBUG_ASSERT(global_system_variables.table_plugin);
serg@janus.mylan's avatar
serg@janus.mylan committed
1210

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1211
  pthread_mutex_unlock(&LOCK_plugin);
1212

1213
  /* Register all dynamic plugins */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1214 1215
  if (!(flags & PLUGIN_INIT_SKIP_DYNAMIC_LOADING))
  {
1216 1217
    if (opt_plugin_load)
      plugin_load_list(&tmp_root, argc, argv, opt_plugin_load);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1218 1219 1220
    if (!(flags & PLUGIN_INIT_SKIP_PLUGIN_TABLE))
      plugin_load(&tmp_root, argc, argv);
  }
1221

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1222 1223
  if (flags & PLUGIN_INIT_SKIP_INITIALIZATION)
    goto end;
1224

1225 1226 1227
  /*
    Now we initialize all remaining plugins
  */
1228

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1229 1230 1231 1232
  pthread_mutex_lock(&LOCK_plugin);
  reap= (st_plugin_int **) my_alloca((plugin_array.elements+1) * sizeof(void*));
  *(reap++)= NULL;

1233 1234
  for (i= 0; i < plugin_array.elements; i++)
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1235
    plugin_ptr= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1236
    if (plugin_ptr->state == PLUGIN_IS_UNINITIALIZED)
1237
    {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1238
      if (plugin_initialize(plugin_ptr))
1239
      {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1240 1241
        plugin_ptr->state= PLUGIN_IS_DYING;
        *(reap++)= plugin_ptr;
1242
      }
1243 1244 1245
    }
  }

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1246 1247
  /*
    Check if any plugins have to be reaped
serg@janus.mylan's avatar
serg@janus.mylan committed
1248
  */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1249 1250 1251
  while ((plugin_ptr= *(--reap)))
  {
    pthread_mutex_unlock(&LOCK_plugin);
1252 1253
    if (plugin_ptr->is_mandatory)
      reaped_mandatory_plugin= TRUE;
serg@janus.mylan's avatar
serg@janus.mylan committed
1254
    plugin_deinitialize(plugin_ptr, true);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1255 1256 1257 1258 1259 1260
    pthread_mutex_lock(&LOCK_plugin);
    plugin_del(plugin_ptr);
  }

  pthread_mutex_unlock(&LOCK_plugin);
  my_afree(reap);
1261 1262
  if (reaped_mandatory_plugin)
    goto err;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1263 1264 1265

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

1267
  DBUG_RETURN(0);
1268

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1269 1270
err_unlock:
  pthread_mutex_unlock(&LOCK_plugin);
1271
err:
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1272
  free_root(&tmp_root, MYF(0));
1273 1274 1275 1276
  DBUG_RETURN(1);
}


serg@janus.mylan's avatar
serg@janus.mylan committed
1277
static bool register_builtin(struct st_mysql_plugin *plugin,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1278 1279
                             struct st_plugin_int *tmp,
                             struct st_plugin_int **ptr)
1280
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1281
  DBUG_ENTER("register_builtin");
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1282 1283
  tmp->ref_count= 0;
  tmp->plugin_dl= 0;
1284

1285
  if (insert_dynamic(&plugin_array, (uchar*)&tmp))
1286 1287
    DBUG_RETURN(1);

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1288 1289
  *ptr= *dynamic_element(&plugin_array, plugin_array.elements - 1,
                         struct st_plugin_int **)=
istruewing@chilla.local's avatar
istruewing@chilla.local committed
1290
        (struct st_plugin_int *) memdup_root(&plugin_mem_root, (uchar*)tmp,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1291
                                             sizeof(struct st_plugin_int));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1292

jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
1293
  if (my_hash_insert(&plugin_hash[plugin->type],(uchar*) *ptr))
1294 1295 1296 1297 1298 1299
    DBUG_RETURN(1);

  DBUG_RETURN(0);
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
1300
#ifdef NOT_USED_YET
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1301 1302
/*
  Register a plugin at run time. (note, this doesn't initialize a plugin)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1303
  Will be useful for embedded applications.
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

  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;
1318 1319
  DBUG_ENTER("plugin_register_builtin");

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1320
  bzero(&tmp, sizeof(tmp));
1321 1322 1323 1324
  tmp.plugin= plugin;
  tmp.name.str= (char *)plugin->name;
  tmp.name.length= strlen(plugin->name);

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1325
  pthread_mutex_lock(&LOCK_plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1326
  rw_wrlock(&LOCK_system_variables_hash);
1327

1328
  if (test_plugin_options(thd->mem_root, &tmp, &dummy_argc, NULL))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1329
    goto end;
1330
  tmp.state= PLUGIN_IS_UNINITIALIZED;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1331 1332
  if ((result= register_builtin(plugin, &tmp, &ptr)))
    mysql_del_sys_var_chain(tmp.system_vars);
1333

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1334 1335
end:
  rw_unlock(&LOCK_system_variables_hash);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1336
  pthread_mutex_unlock(&LOCK_plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1337 1338

  DBUG_RETURN(result);;
1339
}
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1340
#endif /* NOT_USED_YET */
1341 1342


antony@ppcg5.local's avatar
antony@ppcg5.local committed
1343 1344 1345 1346
/*
  called only by plugin_init()
*/
static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv)
1347 1348 1349 1350
{
  TABLE_LIST tables;
  TABLE *table;
  READ_RECORD read_record_info;
1351
  int error;
1352
  THD *new_thd;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1353 1354 1355
#ifdef EMBEDDED_LIBRARY
  bool table_exists;
#endif /* EMBEDDED_LIBRARY */
1356
  DBUG_ENTER("plugin_load");
1357

1358
  if (!(new_thd= new THD))
1359 1360 1361 1362 1363
  {
    sql_print_error("Can't allocate memory for plugin structures");
    delete new_thd;
    DBUG_VOID_RETURN;
  }
monty@mysql.com's avatar
monty@mysql.com committed
1364
  new_thd->thread_stack= (char*) &tables;
1365
  new_thd->store_globals();
1366
  lex_start(new_thd);
1367 1368
  new_thd->db= my_strdup("mysql", MYF(0));
  new_thd->db_length= 5;
1369
  bzero((uchar*)&tables, sizeof(tables));
1370 1371 1372
  tables.alias= tables.table_name= (char*)"plugin";
  tables.lock_type= TL_READ;
  tables.db= new_thd->db;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386

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

1387 1388 1389
  if (simple_open_n_lock_tables(new_thd, &tables))
  {
    DBUG_PRINT("error",("Can't open plugin table"));
1390 1391
    sql_print_error("Can't open the mysql.plugin table. Please "
                    "run mysql_upgrade to create it.");
1392 1393 1394
    goto end;
  }
  table= tables.table;
1395
  init_read_record(&read_record_info, new_thd, table, NULL, 1, 0, FALSE);
1396
  table->use_all_columns();
1397 1398 1399 1400 1401 1402 1403
  /*
    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);
1404 1405 1406
  while (!(error= read_record_info.read_record(&read_record_info)))
  {
    DBUG_PRINT("info", ("init plugin record"));
1407
    String str_name, str_dl;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1408 1409
    get_field(tmp_root, table->field[0], &str_name);
    get_field(tmp_root, table->field[1], &str_dl);
serg@janus.mylan's avatar
serg@janus.mylan committed
1410

1411 1412 1413
    LEX_STRING name= {(char *)str_name.ptr(), str_name.length()};
    LEX_STRING dl= {(char *)str_dl.ptr(), str_dl.length()};

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1414
    if (plugin_add(tmp_root, &name, &dl, argc, argv, REPORT_TO_LOG))
1415 1416
      sql_print_warning("Couldn't load plugin named '%s' with soname '%s'.",
                        str_name.c_ptr(), str_dl.c_ptr());
1417
    free_root(tmp_root, MYF(MY_MARK_BLOCKS_FREE));
1418
  }
1419
  pthread_mutex_unlock(&LOCK_plugin);
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
  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;
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
1433 1434 1435 1436 1437
/*
  called only by plugin_init()
*/
static bool plugin_load_list(MEM_ROOT *tmp_root, int *argc, char **argv,
                             const char *list)
1438
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1439 1440 1441 1442 1443 1444 1445
  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)
1446
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1447
    if (p == buffer + sizeof(buffer) - 1)
1448 1449 1450 1451 1452
    {
      sql_print_error("plugin-load parameter too long");
      DBUG_RETURN(TRUE);
    }

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1453 1454 1455 1456 1457 1458 1459 1460
    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 ';':
1461 1462
      str->str[str->length]= '\0';
      if (str == &name)  // load all plugins in named module
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1463
      {
1464 1465 1466 1467 1468 1469
        if (!name.length)
        {
          p--;    /* reset pointer */
          continue;
        }

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1470
        dl= name;
1471
        pthread_mutex_lock(&LOCK_plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1472 1473 1474 1475 1476 1477
        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);
serg@janus.mylan's avatar
serg@janus.mylan committed
1478

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
            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));
1489
        pthread_mutex_lock(&LOCK_plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1490 1491 1492
        if (plugin_add(tmp_root, &name, &dl, argc, argv, REPORT_TO_LOG))
          goto error;
      }
1493
      pthread_mutex_unlock(&LOCK_plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1494 1495 1496 1497 1498 1499 1500 1501
      name.length= dl.length= 0;
      dl.str= NULL; name.str= p= buffer;
      str= &name;
      continue;
    case '=':
    case '#':
      if (str == &name)
      {
1502
        name.str[name.length]= '\0';
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1503 1504 1505 1506 1507 1508 1509 1510
        str= &dl;
        str->str= p;
        continue;
      }
    default:
      str->length++;
      continue;
    }
1511
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1512 1513
  DBUG_RETURN(FALSE);
error:
1514
  pthread_mutex_unlock(&LOCK_plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1515 1516 1517 1518
  sql_print_error("Couldn't load plugin named '%s' with soname '%s'.",
                  name.str, dl.str);
  DBUG_RETURN(TRUE);
}
1519

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1520

1521
void plugin_shutdown(void)
1522
{
1523
  uint i, count= plugin_array.elements;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1524 1525
  struct st_plugin_int **plugins, *plugin;
  struct st_plugin_dl **dl;
1526 1527
  DBUG_ENTER("plugin_shutdown");

1528
  if (initialized)
1529
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1530
    pthread_mutex_lock(&LOCK_plugin);
1531

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1532
    reap_needed= true;
serg@janus.mylan's avatar
serg@janus.mylan committed
1533

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1534 1535 1536 1537 1538 1539 1540 1541 1542
    /*
      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))
    {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1543
      reap_plugins();
1544
      for (i= 0; i < count; i++)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1545
      {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1546
        plugin= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
1547 1548
        if (plugin->state == PLUGIN_IS_READY)
        {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1549 1550 1551 1552
          plugin->state= PLUGIN_IS_DELETED;
          reap_needed= true;
        }
      }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1553 1554 1555 1556 1557 1558 1559 1560
      if (!reap_needed)
      {
        /*
          release any plugin references held.
        */
        unlock_variables(NULL, &global_system_variables);
        unlock_variables(NULL, &max_system_variables);
      }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1561
    }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1562

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1563 1564 1565 1566 1567 1568 1569
    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++)
    {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1570
      plugins[i]= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1571 1572 1573 1574 1575
      /* 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);
serg@janus.mylan's avatar
serg@janus.mylan committed
1576

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1577 1578 1579 1580
    /*
      We loop through all plugins and call deinit() if they have one.
    */
    for (i= 0; i < count; i++)
1581 1582
      if (!(plugins[i]->state & (PLUGIN_IS_UNINITIALIZED | PLUGIN_IS_FREED |
                                 PLUGIN_IS_DISABLED)))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1583
      {
1584 1585
        sql_print_warning("Plugin '%s' will be forced to shutdown",
                          plugins[i]->name.str);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1586 1587 1588 1589
        /*
          We are forcing deinit on plugins so we don't want to do a ref_count
          check until we have processed all the plugins.
        */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1590 1591 1592
        plugin_deinitialize(plugins[i], false);
      }

serg@janus.mylan's avatar
serg@janus.mylan committed
1593 1594 1595 1596 1597 1598
    /*
      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);
serg@janus.mylan's avatar
serg@janus.mylan committed
1599

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1600 1601 1602 1603 1604
    /*
      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++)
serg@janus.mylan's avatar
serg@janus.mylan committed
1605
    {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1606 1607 1608 1609 1610
      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]);
serg@janus.mylan's avatar
serg@janus.mylan committed
1611
    }
1612

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1613 1614 1615
    /*
      Now we can deallocate all memory.
    */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1616

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1617 1618
    cleanup_variables(NULL, &global_system_variables);
    cleanup_variables(NULL, &max_system_variables);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1619
    pthread_mutex_unlock(&LOCK_plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1620 1621 1622

    initialized= 0;
    pthread_mutex_destroy(&LOCK_plugin);
1623

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1624
    my_afree(plugins);
1625 1626
  }

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1627
  /* Dispose of the memory */
serg@janus.mylan's avatar
serg@janus.mylan committed
1628

1629 1630 1631
  for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
    hash_free(&plugin_hash[i]);
  delete_dynamic(&plugin_array);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1632 1633 1634 1635

  count= plugin_dl_array.elements;
  dl= (struct st_plugin_dl **)my_alloca(sizeof(void*) * count);
  for (i= 0; i < count; i++)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1636
    dl[i]= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
1637
  for (i= 0; i < plugin_dl_array.elements; i++)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1638 1639
    free_plugin_mem(dl[i]);
  my_afree(dl);
1640
  delete_dynamic(&plugin_dl_array);
serg@janus.mylan's avatar
serg@janus.mylan committed
1641

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1642 1643
  hash_free(&bookmark_hash);
  free_root(&plugin_mem_root, MYF(0));
serg@janus.mylan's avatar
serg@janus.mylan committed
1644

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1645 1646
  global_variables_dynamic_size= 0;

1647 1648 1649 1650
  DBUG_VOID_RETURN;
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
1651
bool mysql_install_plugin(THD *thd, const LEX_STRING *name, const LEX_STRING *dl)
1652 1653 1654
{
  TABLE_LIST tables;
  TABLE *table;
1655 1656
  int error, argc=orig_argc;
  char **argv=orig_argv;
1657 1658
  struct st_plugin_int *tmp;
  DBUG_ENTER("mysql_install_plugin");
1659

1660 1661 1662
  bzero(&tables, sizeof(tables));
  tables.db= (char *)"mysql";
  tables.table_name= tables.alias= (char *)"plugin";
1663
  if (check_table_access(thd, INSERT_ACL, &tables, 1, FALSE))
1664
    DBUG_RETURN(TRUE);
1665

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1666
  /* need to open before acquiring LOCK_plugin or it will deadlock */
1667
  if (! (table = open_ltable(thd, &tables, TL_WRITE, 0)))
1668 1669
    DBUG_RETURN(TRUE);

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1670 1671
  pthread_mutex_lock(&LOCK_plugin);
  rw_wrlock(&LOCK_system_variables_hash);
1672

1673
  my_load_defaults(MYSQL_CONFIG_NAME, load_default_groups, &argc, &argv, NULL);
1674
  error= plugin_add(thd->mem_root, name, dl, &argc, argv, REPORT_TO_USER);
1675 1676
  if (argv)
    free_defaults(argv);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1677 1678 1679
  rw_unlock(&LOCK_system_variables_hash);

  if (error || !(tmp= plugin_find_internal(name, MYSQL_ANY_PLUGIN)))
1680
    goto err;
1681

1682
  if (tmp->state == PLUGIN_IS_DISABLED)
1683
  {
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_CANT_INITIALIZE_UDF, ER(ER_CANT_INITIALIZE_UDF),
                        name->str, "Plugin is disabled");
  }
  else
  {
    DBUG_ASSERT(tmp->state == PLUGIN_IS_UNINITIALIZED);
    if (plugin_initialize(tmp))
    {
      my_error(ER_CANT_INITIALIZE_UDF, MYF(0), name->str,
               "Plugin initialization function failed.");
      goto deinit;
    }
1697
  }
1698

1699 1700 1701 1702 1703 1704
  /*
    We do not replicate the INSTALL PLUGIN statement. Disable binlogging
    of the insert into the plugin table, so that it is not replicated in
    row based mode.
  */
  tmp_disable_binlog(thd);
1705
  table->use_all_columns();
1706 1707 1708
  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);
1709
  error= table->file->ha_write_row(table->record[0]);
1710
  reenable_binlog(thd);
1711 1712 1713 1714 1715
  if (error)
  {
    table->file->print_error(error, MYF(0));
    goto deinit;
  }
1716

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1717
  pthread_mutex_unlock(&LOCK_plugin);
1718 1719
  DBUG_RETURN(FALSE);
deinit:
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1720 1721 1722
  tmp->state= PLUGIN_IS_DELETED;
  reap_needed= true;
  reap_plugins();
1723
err:
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1724
  pthread_mutex_unlock(&LOCK_plugin);
1725 1726 1727 1728
  DBUG_RETURN(TRUE);
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
1729
bool mysql_uninstall_plugin(THD *thd, const LEX_STRING *name)
1730 1731 1732 1733 1734
{
  TABLE *table;
  TABLE_LIST tables;
  struct st_plugin_int *plugin;
  DBUG_ENTER("mysql_uninstall_plugin");
1735 1736 1737 1738

  bzero(&tables, sizeof(tables));
  tables.db= (char *)"mysql";
  tables.table_name= tables.alias= (char *)"plugin";
1739 1740
  if (check_table_access(thd, DELETE_ACL, &tables, 1, FALSE))
    DBUG_RETURN(TRUE);
1741

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1742
  /* need to open before acquiring LOCK_plugin or it will deadlock */
1743
  if (! (table= open_ltable(thd, &tables, TL_WRITE, 0)))
1744 1745
    DBUG_RETURN(TRUE);

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1746
  pthread_mutex_lock(&LOCK_plugin);
1747
  if (!(plugin= plugin_find_internal(name, MYSQL_ANY_PLUGIN)))
1748 1749 1750 1751
  {
    my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "PLUGIN", name->str);
    goto err;
  }
1752 1753
  if (!plugin->plugin_dl)
  {
1754 1755
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                 WARN_PLUGIN_DELETE_BUILTIN, ER(WARN_PLUGIN_DELETE_BUILTIN));
1756 1757 1758
    my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "PLUGIN", name->str);
    goto err;
  }
1759

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1760
  plugin->state= PLUGIN_IS_DELETED;
1761
  if (plugin->ref_count)
1762 1763
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                 WARN_PLUGIN_BUSY, ER(WARN_PLUGIN_BUSY));
1764
  else
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1765 1766 1767
    reap_needed= true;
  reap_plugins();
  pthread_mutex_unlock(&LOCK_plugin);
1768

1769
  table->use_all_columns();
1770
  table->field[0]->store(name->str, name->length, system_charset_info);
1771 1772 1773 1774
  if (! table->file->index_read_idx_map(table->record[0], 0,
                                        (uchar *)table->field[0]->ptr,
                                        HA_WHOLE_KEY,
                                        HA_READ_KEY_EXACT))
1775 1776
  {
    int error;
1777 1778 1779 1780 1781 1782 1783 1784 1785
    /*
      We do not replicate the UNINSTALL PLUGIN statement. Disable binlogging
      of the delete from the plugin table, so that it is not replicated in
      row based mode.
    */
    tmp_disable_binlog(thd);
    error= table->file->ha_delete_row(table->record[0]);
    reenable_binlog(thd);
    if (error)
1786 1787
    {
      table->file->print_error(error, MYF(0));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1788
      DBUG_RETURN(TRUE);
1789 1790 1791 1792
    }
  }
  DBUG_RETURN(FALSE);
err:
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1793
  pthread_mutex_unlock(&LOCK_plugin);
1794 1795
  DBUG_RETURN(TRUE);
}
1796

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1797 1798

bool plugin_foreach_with_mask(THD *thd, plugin_foreach_func *func,
1799
                       int type, uint state_mask, void *arg)
1800
{
1801 1802 1803
  uint idx, total;
  struct st_plugin_int *plugin, **plugins;
  int version=plugin_array_version;
1804 1805
  DBUG_ENTER("plugin_foreach_with_mask");

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1806 1807 1808
  if (!initialized)
    DBUG_RETURN(FALSE);

1809
  state_mask= ~state_mask; // do it only once
1810

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1811
  pthread_mutex_lock(&LOCK_plugin);
serg@janus.mylan's avatar
serg@janus.mylan committed
1812
  total= type == MYSQL_ANY_PLUGIN ? plugin_array.elements
1813 1814 1815
                                  : plugin_hash[type].records;
  /*
    Do the alloca out here in case we do have a working alloca:
serg@janus.mylan's avatar
serg@janus.mylan committed
1816
        leaving the nested stack frame invalidates alloca allocation.
1817
  */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1818
  plugins=(struct st_plugin_int **)my_alloca(total*sizeof(plugin));
1819 1820
  if (type == MYSQL_ANY_PLUGIN)
  {
1821
    for (idx= 0; idx < total; idx++)
1822
    {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1823
      plugin= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
1824
      plugins[idx]= !(plugin->state & state_mask) ? plugin : NULL;
1825 1826 1827 1828
    }
  }
  else
  {
1829
    HASH *hash= plugin_hash + type;
1830
    for (idx= 0; idx < total; idx++)
1831 1832
    {
      plugin= (struct st_plugin_int *) hash_element(hash, idx);
1833
      plugins[idx]= !(plugin->state & state_mask) ? plugin : NULL;
1834 1835
    }
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1836
  pthread_mutex_unlock(&LOCK_plugin);
1837 1838 1839 1840 1841

  for (idx= 0; idx < total; idx++)
  {
    if (unlikely(version != plugin_array_version))
    {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1842
      pthread_mutex_lock(&LOCK_plugin);
1843
      for (uint i=idx; i < total; i++)
1844
        if (plugins[i] && plugins[i]->state & state_mask)
1845
          plugins[i]=0;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1846
      pthread_mutex_unlock(&LOCK_plugin);
1847
    }
serg@janus.mylan's avatar
serg@janus.mylan committed
1848
    plugin= plugins[idx];
1849
    /* It will stop iterating on first engine error when "func" returns TRUE */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1850
    if (plugin && func(thd, plugin_int_to_ref(plugin), arg))
1851 1852 1853 1854
        goto err;
  }

  my_afree(plugins);
1855 1856
  DBUG_RETURN(FALSE);
err:
1857
  my_afree(plugins);
1858 1859
  DBUG_RETURN(TRUE);
}
1860

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876

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

1877 1878 1879 1880
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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897

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
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
1898
typedef uchar *(*mysql_sys_var_ptr_p)(void* a_thd, int offset);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910


/****************************************************************************
  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;
serg@janus.mylan's avatar
serg@janus.mylan committed
1911
  long long tmp;
serg@janus.mylan's avatar
serg@janus.mylan committed
1912

antony@ppcg5.local's avatar
antony@ppcg5.local committed
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
  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
  {
serg@janus.mylan's avatar
serg@janus.mylan committed
1926
    if (value->val_int(value, &tmp) < 0)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1927 1928 1929 1930 1931 1932 1933 1934 1935
      goto err;
    if (tmp > 1)
    {
      llstr(tmp, buff);
      strvalue= buff;
      goto err;
    }
    result= (int) tmp;
  }
1936
  *(my_bool *) save= -result;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
  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)
{
1947
  my_bool fixed;
serg@janus.mylan's avatar
serg@janus.mylan committed
1948
  long long tmp;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1949
  struct my_option options;
serg@janus.mylan's avatar
serg@janus.mylan committed
1950
  value->val_int(value, &tmp);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1951
  plugin_opt_set_limits(&options, var);
1952 1953 1954 1955 1956 1957 1958

  if (var->flags & PLUGIN_VAR_UNSIGNED)
    *(uint *)save= (uint) getopt_ull_limit_value((ulonglong) tmp, &options,
                                                   &fixed);
  else
    *(int *)save= (int) getopt_ll_limit_value(tmp, &options, &fixed);

1959 1960
  return throw_bounds_warning(thd, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
                              var->name, (longlong) tmp);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1961 1962 1963 1964 1965 1966
}


static int check_func_long(THD *thd, struct st_mysql_sys_var *var,
                          void *save, st_mysql_value *value)
{
1967
  my_bool fixed;
serg@janus.mylan's avatar
serg@janus.mylan committed
1968
  long long tmp;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1969
  struct my_option options;
serg@janus.mylan's avatar
serg@janus.mylan committed
1970
  value->val_int(value, &tmp);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1971
  plugin_opt_set_limits(&options, var);
1972 1973 1974 1975 1976 1977 1978

  if (var->flags & PLUGIN_VAR_UNSIGNED)
    *(ulong *)save= (ulong) getopt_ull_limit_value((ulonglong) tmp, &options,
                                                   &fixed);
  else
    *(long *)save= (long) getopt_ll_limit_value(tmp, &options, &fixed);

1979 1980
  return throw_bounds_warning(thd, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
                              var->name, (longlong) tmp);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1981 1982 1983 1984
}


static int check_func_longlong(THD *thd, struct st_mysql_sys_var *var,
1985
                               void *save, st_mysql_value *value)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1986
{
1987
  my_bool fixed;
serg@janus.mylan's avatar
serg@janus.mylan committed
1988
  long long tmp;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1989
  struct my_option options;
serg@janus.mylan's avatar
serg@janus.mylan committed
1990
  value->val_int(value, &tmp);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1991
  plugin_opt_set_limits(&options, var);
1992 1993 1994 1995 1996 1997 1998

  if (var->flags & PLUGIN_VAR_UNSIGNED)
    *(ulonglong *)save= getopt_ull_limit_value((ulonglong) tmp, &options,
                                               &fixed);
  else
    *(longlong *)save= getopt_ll_limit_value(tmp, &options, &fixed);

1999 2000
  return throw_bounds_warning(thd, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
                              var->name, (longlong) tmp);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2001 2002 2003 2004 2005 2006 2007 2008
}

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;
serg@janus.mylan's avatar
serg@janus.mylan committed
2009

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
  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;
serg@janus.mylan's avatar
serg@janus.mylan committed
2024
  long long tmp;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2025 2026
  long result;
  int length;
serg@janus.mylan's avatar
serg@janus.mylan committed
2027

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2028
  if (var->flags & PLUGIN_VAR_THDLOCAL)
2029
    typelib= ((thdvar_enum_t*) var)->typelib;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2030
  else
2031
    typelib= ((sysvar_enum_t*) var)->typelib;
serg@janus.mylan's avatar
serg@janus.mylan committed
2032

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2033 2034 2035 2036 2037
  if (value->value_type(value) == MYSQL_VALUE_TYPE_STRING)
  {
    length= sizeof(buff);
    if (!(str= value->val_str(value, buff, &length)))
      goto err;
2038
    if ((result= (long)find_type(typelib, str, length, 1)-1) < 0)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2039 2040 2041 2042 2043 2044 2045
    {
      strvalue= str;
      goto err;
    }
  }
  else
  {
serg@janus.mylan's avatar
serg@janus.mylan committed
2046
    if (value->val_int(value, &tmp))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
      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;
2070
  ulonglong result;
2071
  uint error_len= 0;                            // init as only set on error
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2072 2073
  bool not_used;
  int length;
serg@janus.mylan's avatar
serg@janus.mylan committed
2074

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2075
  if (var->flags & PLUGIN_VAR_THDLOCAL)
2076
    typelib= ((thdvar_set_t*) var)->typelib;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2077
  else
2078
    typelib= ((sysvar_set_t*)var)->typelib;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088

  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)
    {
2089
      strmake(buff, error, min(sizeof(buff) - 1, error_len));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2090 2091 2092 2093 2094 2095
      strvalue= buff;
      goto err;
    }
  }
  else
  {
2096
    if (value->val_int(value, (long long *)&result))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2097
      goto err;
2098
    if (unlikely((result >= (ULL(1) << typelib->count)) &&
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2099 2100
                 (typelib->count < sizeof(long)*8)))
    {
2101
      llstr(result, buff);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2102 2103 2104 2105
      strvalue= buff;
      goto err;
    }
  }
2106
  *(ulonglong*)save= result;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2107 2108 2109 2110 2111 2112 2113 2114
  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,
2115
                             void *tgt, const void *save)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2116
{
2117
  *(my_bool *) tgt= *(my_bool *) save ? TRUE : FALSE;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2118 2119 2120 2121
}


static void update_func_int(THD *thd, struct st_mysql_sys_var *var,
2122
                             void *tgt, const void *save)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2123 2124 2125 2126 2127 2128
{
  *(int *)tgt= *(int *) save;
}


static void update_func_long(THD *thd, struct st_mysql_sys_var *var,
2129
                             void *tgt, const void *save)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2130 2131 2132 2133 2134 2135
{
  *(long *)tgt= *(long *) save;
}


static void update_func_longlong(THD *thd, struct st_mysql_sys_var *var,
2136
                             void *tgt, const void *save)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2137 2138 2139 2140 2141 2142
{
  *(longlong *)tgt= *(ulonglong *) save;
}


static void update_func_str(THD *thd, struct st_mysql_sys_var *var,
2143
                             void *tgt, const void *save)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
{
  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");
serg@janus.mylan's avatar
serg@janus.mylan committed
2166

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2167
  pthread_mutex_lock(&LOCK_plugin);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2168 2169 2170 2171
  rw_rdlock(&LOCK_system_variables_hash);
  if ((var= intern_find_sys_var(str, length, false)) &&
      (pi= var->cast_pluginvar()))
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2172
    rw_unlock(&LOCK_system_variables_hash);
serg@janus.mylan's avatar
serg@janus.mylan committed
2173
    LEX *lex= thd ? thd->lex : 0;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
    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);
    }
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2184 2185 2186
  else
    rw_unlock(&LOCK_system_variables_hash);
  pthread_mutex_unlock(&LOCK_plugin);
serg@janus.mylan's avatar
serg@janus.mylan committed
2187

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
  /*
    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;
serg@janus.mylan's avatar
serg@janus.mylan committed
2209

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2210 2211
  if (!(flags & PLUGIN_VAR_THDLOCAL))
    return NULL;
serg@janus.mylan's avatar
serg@janus.mylan committed
2212

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2213 2214 2215 2216 2217
  namelen= strlen(name);
  if (plugin)
    pluginlen= strlen(plugin) + 1;
  length= namelen + pluginlen + 2;
  varname= (char*) my_alloca(length);
serg@janus.mylan's avatar
serg@janus.mylan committed
2218

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2219 2220 2221 2222 2223
  if (plugin)
  {
    strxmov(varname + 1, plugin, "_", name, NullS);
    for (p= varname + 1; *p; p++)
      if (*p == '-')
serg@janus.mylan's avatar
serg@janus.mylan committed
2224
        *p= '_';
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2225 2226 2227 2228 2229
  }
  else
    memcpy(varname + 1, name, namelen + 1);

  varname[0]= flags & PLUGIN_VAR_TYPEMASK;
serg@janus.mylan's avatar
serg@janus.mylan committed
2230 2231

  result= (st_bookmark*) hash_search(&bookmark_hash,
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2232
                                     (const uchar*) varname, length - 1);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252

  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;
serg@janus.mylan's avatar
serg@janus.mylan committed
2253

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2254 2255 2256 2257 2258 2259 2260 2261
  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:
serg@janus.mylan's avatar
serg@janus.mylan committed
2262
  case PLUGIN_VAR_ENUM:
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2263 2264 2265
    size= sizeof(long);
    break;
  case PLUGIN_VAR_LONGLONG:
serg@janus.mylan's avatar
serg@janus.mylan committed
2266
  case PLUGIN_VAR_SET:
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
    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)))
  {
serg@janus.mylan's avatar
serg@janus.mylan committed
2285
    result= (st_bookmark*) alloc_root(&plugin_mem_root,
serg@janus.mylan's avatar
serg@janus.mylan committed
2286
                                      sizeof(struct st_bookmark) + length-1);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2287
    varname[0]= flags & PLUGIN_VAR_TYPEMASK;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2288
    memcpy(result->key, varname, length);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2289 2290
    result->name_len= length - 2;
    result->offset= -1;
serg@janus.mylan's avatar
serg@janus.mylan committed
2291

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2292 2293 2294 2295 2296
    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;
serg@janus.mylan's avatar
serg@janus.mylan committed
2297

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2298 2299 2300 2301
    new_size= (offset + size + 63) & ~63;

    if (new_size > global_variables_dynamic_size)
    {
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2302
      global_system_variables.dynamic_variables_ptr= (char*)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2303 2304
        my_realloc(global_system_variables.dynamic_variables_ptr, new_size,
                   MYF(MY_WME | MY_FAE | MY_ALLOW_ZERO_PTR));
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2305
      max_system_variables.dynamic_variables_ptr= (char*)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2306 2307
        my_realloc(max_system_variables.dynamic_variables_ptr, new_size,
                   MYF(MY_WME | MY_FAE | MY_ALLOW_ZERO_PTR));
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
      /*
        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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2319 2320
      global_variables_dynamic_size= new_size;
    }
serg@janus.mylan's avatar
serg@janus.mylan committed
2321

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2322 2323 2324 2325 2326 2327
    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++;
serg@janus.mylan's avatar
serg@janus.mylan committed
2328

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2329 2330 2331
    result->version= global_system_variables.dynamic_variables_version;

    /* this should succeed because we have already checked if a dup exists */
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2332
    if (my_hash_insert(&bookmark_hash, (uchar*) result))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
    {
      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.
*/
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2349
static uchar *intern_sys_var_ptr(THD* thd, int offset, bool global_lock)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2350 2351 2352
{
  DBUG_ASSERT(offset >= 0);
  DBUG_ASSERT((uint)offset <= global_system_variables.dynamic_variables_head);
serg@janus.mylan's avatar
serg@janus.mylan committed
2353

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2354
  if (!thd)
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2355
    return (uchar*) global_system_variables.dynamic_variables_ptr + offset;
serg@janus.mylan's avatar
serg@janus.mylan committed
2356

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2357
  /*
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2358
    dynamic_variables_head points to the largest valid offset
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2359 2360 2361 2362 2363
  */
  if (!thd->variables.dynamic_variables_ptr ||
      (uint)offset > thd->variables.dynamic_variables_head)
  {
    uint idx;
serg@janus.mylan's avatar
serg@janus.mylan committed
2364

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2365
    rw_rdlock(&LOCK_system_variables_hash);
serg@janus.mylan's avatar
serg@janus.mylan committed
2366

jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2367
    thd->variables.dynamic_variables_ptr= (char*)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2368 2369 2370 2371 2372 2373 2374 2375 2376
      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);

serg@janus.mylan's avatar
serg@janus.mylan committed
2377
    memcpy(thd->variables.dynamic_variables_ptr +
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
             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);
serg@janus.mylan's avatar
serg@janus.mylan committed
2393

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2394
      if (v->version <= thd->variables.dynamic_variables_version ||
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2395
          !(var= intern_find_sys_var(v->key + 1, v->name_len, true)) ||
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2396
          !(pi= var->cast_pluginvar()) ||
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2397
          v->key[0] != (pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2398 2399 2400
        continue;

      /* Here we do anything special that may be required of the data types */
serg@janus.mylan's avatar
serg@janus.mylan committed
2401

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2402 2403 2404
      if ((pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
          pi->plugin_var->flags & PLUGIN_VAR_MEMALLOC)
      {
serg@janus.mylan's avatar
serg@janus.mylan committed
2405 2406 2407
         char **pp= (char**) (thd->variables.dynamic_variables_ptr +
                             *(int*)(pi->plugin_var + 1));
         if ((*pp= *(char**) (global_system_variables.dynamic_variables_ptr +
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2408 2409 2410
                             *(int*)(pi->plugin_var + 1))))
           *pp= my_strdup(*pp, MYF(MY_WME|MY_FAE));
      }
serg@janus.mylan's avatar
serg@janus.mylan committed
2411
    }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2412

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2413 2414 2415
    if (global_lock)
      pthread_mutex_unlock(&LOCK_global_system_variables);

serg@janus.mylan's avatar
serg@janus.mylan committed
2416
    thd->variables.dynamic_variables_version=
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2417
           global_system_variables.dynamic_variables_version;
serg@janus.mylan's avatar
serg@janus.mylan committed
2418
    thd->variables.dynamic_variables_head=
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2419
           global_system_variables.dynamic_variables_head;
serg@janus.mylan's avatar
serg@janus.mylan committed
2420
    thd->variables.dynamic_variables_size=
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2421 2422 2423 2424
           global_system_variables.dynamic_variables_size;

    rw_unlock(&LOCK_system_variables_hash);
  }
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2425
  return (uchar*)thd->variables.dynamic_variables_ptr + offset;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2426 2427
}

jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2428
static uchar *mysql_sys_var_ptr(void* a_thd, int offset)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2429 2430 2431 2432 2433
{
  return intern_sys_var_ptr((THD *)a_thd, offset, true);
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
2434
void plugin_thdvar_init(THD *thd)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2435
{
2436 2437 2438 2439 2440 2441 2442 2443 2444
  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;

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2445 2446 2447 2448 2449
  /* 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;

2450
  pthread_mutex_lock(&LOCK_plugin);  
serg@janus.mylan's avatar
serg@janus.mylan committed
2451
  thd->variables.table_plugin=
2452 2453 2454 2455
        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;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2456 2457
}

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470

/*
  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
2471 2472 2473

  Unlike plugin_vars_free_values() it frees all variables of all plugins,
  it's used on shutdown.
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2474 2475
*/
static void cleanup_variables(THD *thd, struct system_variables *vars)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
{
  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 ||
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2488
        !(var= intern_find_sys_var(v->key + 1, v->name_len, true)) ||
serg@janus.mylan's avatar
serg@janus.mylan committed
2489
        !(pivar= var->cast_pluginvar()) ||
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2490
        v->key[0] != (pivar->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2491 2492 2493
      continue;

    flags= pivar->plugin_var->flags;
serg@janus.mylan's avatar
serg@janus.mylan committed
2494

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2495 2496 2497 2498 2499 2500 2501
    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;
    }
serg@janus.mylan's avatar
serg@janus.mylan committed
2502
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2503
  rw_unlock(&LOCK_system_variables_hash);
serg@janus.mylan's avatar
serg@janus.mylan committed
2504

serg@janus.mylan's avatar
serg@janus.mylan committed
2505 2506
  DBUG_ASSERT(vars->table_plugin == NULL);

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2507 2508 2509 2510
  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;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
}


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

  pthread_mutex_lock(&LOCK_plugin);

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2522 2523
  unlock_variables(thd, &thd->variables);
  cleanup_variables(thd, &thd->variables);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2524

serg@janus.mylan's avatar
serg@janus.mylan committed
2525
  if ((idx= thd->lex->plugins.elements))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2526
  {
serg@janus.mylan's avatar
serg@janus.mylan committed
2527
    list= ((plugin_ref*) thd->lex->plugins.buffer) + idx - 1;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2528
    DBUG_PRINT("info",("unlocking %d plugins", idx));
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2529
    while ((uchar*) list >= thd->lex->plugins.buffer)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2530 2531 2532 2533 2534 2535
      intern_plugin_unlock(NULL, *list--);
  }

  reap_plugins();
  pthread_mutex_unlock(&LOCK_plugin);

serg@janus.mylan's avatar
serg@janus.mylan committed
2536
  reset_dynamic(&thd->lex->plugins);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2537 2538 2539 2540 2541

  DBUG_VOID_RETURN;
}


2542 2543 2544
/**
  @brief Free values of thread variables of a plugin.

kostja@bodhi.(none)'s avatar
kostja@bodhi.(none) committed
2545
  This must be called before a plugin is deleted. Otherwise its
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
  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;
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
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;
  }
}
serg@janus.mylan's avatar
serg@janus.mylan committed
2591

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613

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;
  }
}
serg@janus.mylan's avatar
serg@janus.mylan committed
2614

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2615

jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2616
uchar* sys_var_pluginvar::real_value_ptr(THD *thd, enum_var_type type)
serg@janus.mylan's avatar
serg@janus.mylan committed
2617
{
2618
  DBUG_ASSERT(thd || (type == OPT_GLOBAL));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2619 2620
  if (plugin_var->flags & PLUGIN_VAR_THDLOCAL)
  {
2621
    if (type == OPT_GLOBAL)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2622
      thd= NULL;
serg@janus.mylan's avatar
serg@janus.mylan committed
2623

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2624 2625
    return intern_sys_var_ptr(thd, *(int*) (plugin_var+1), false);
  }
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2626
  return *(uchar**) (plugin_var+1);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2627
}
serg@janus.mylan's avatar
serg@janus.mylan committed
2628

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2629 2630 2631

TYPELIB* sys_var_pluginvar::plugin_var_typelib(void)
{
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
  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;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2643 2644 2645
  }
  return NULL;
}
serg@janus.mylan's avatar
serg@janus.mylan committed
2646

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2647

jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2648
uchar* sys_var_pluginvar::value_ptr(THD *thd, enum_var_type type,
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2649 2650
                                   LEX_STRING *base)
{
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2651
  uchar* result;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2652 2653 2654

  result= real_value_ptr(thd, type);

2655
  if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_ENUM)
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2656
    result= (uchar*) get_type(plugin_var_typelib(), *(ulong*)result);
2657
  else if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_SET)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2658 2659 2660 2661
  {
    char buffer[STRING_BUFFER_USUAL_SIZE];
    String str(buffer, sizeof(buffer), system_charset_info);
    TYPELIB *typelib= plugin_var_typelib();
2662
    ulonglong mask= 1, value= *(ulonglong*) result;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2663 2664 2665 2666 2667 2668 2669
    uint i;

    str.length(0);
    for (i= 0; i < typelib->count; i++, mask<<=1)
    {
      if (!(value & mask))
        continue;
2670 2671 2672
      str.append(typelib->type_names[i], typelib->type_lengths
                                       ? typelib->type_lengths[i]
                                       : strlen(typelib->type_names[i]));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2673 2674 2675
      str.append(',');
    }

jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2676
    result= (uchar*) "";
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2677
    if (str.length())
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
2678
      result= (uchar*) thd->strmake(str.ptr(), str.length()-1);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2679 2680 2681 2682 2683 2684 2685 2686 2687
  }
  return result;
}


bool sys_var_pluginvar::check(THD *thd, set_var *var)
{
  st_item_value_holder value;
  DBUG_ASSERT(is_readonly() || plugin_var->check);
serg@janus.mylan's avatar
serg@janus.mylan committed
2688

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2689 2690 2691 2692 2693
  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;
serg@janus.mylan's avatar
serg@janus.mylan committed
2694

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2695 2696 2697 2698 2699 2700 2701
  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)
{
2702 2703
  const void *src;
  void *tgt;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2704 2705 2706 2707 2708 2709

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

  if (is_readonly())
    return;

serg@janus.mylan's avatar
serg@janus.mylan committed
2710
  pthread_mutex_lock(&LOCK_global_system_variables);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2711 2712 2713 2714 2715 2716 2717
  tgt= real_value_ptr(thd, type);
  src= ((void **) (plugin_var + 1) + 1);

  if (plugin_var->flags & PLUGIN_VAR_THDLOCAL)
  {
    if (type != OPT_GLOBAL)
      src= real_value_ptr(thd, OPT_GLOBAL);
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
    else
    switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
	case PLUGIN_VAR_INT:
	  src= &((thdvar_uint_t*) plugin_var)->def_val;
	  break;
	case PLUGIN_VAR_LONG:
	  src= &((thdvar_ulong_t*) plugin_var)->def_val;
	  break;
	case PLUGIN_VAR_LONGLONG:
	  src= &((thdvar_ulonglong_t*) plugin_var)->def_val;
	  break;
	case PLUGIN_VAR_ENUM:
	  src= &((thdvar_enum_t*) plugin_var)->def_val;
	  break;
	case PLUGIN_VAR_SET:
	  src= &((thdvar_set_t*) plugin_var)->def_val;
	  break;
	case PLUGIN_VAR_BOOL:
	  src= &((thdvar_bool_t*) plugin_var)->def_val;
	  break;
	case PLUGIN_VAR_STR:
	  src= &((thdvar_str_t*) plugin_var)->def_val;
	  break;
	default:
	  DBUG_ASSERT(0);
	}
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2744 2745 2746
  }

  /* thd must equal current_thd if PLUGIN_VAR_THDLOCAL flag is set */
serg@janus.mylan's avatar
serg@janus.mylan committed
2747
  DBUG_ASSERT(!(plugin_var->flags & PLUGIN_VAR_THDLOCAL) ||
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2748 2749 2750 2751 2752
              thd == current_thd);

  if (!(plugin_var->flags & PLUGIN_VAR_THDLOCAL) || type == OPT_GLOBAL)
  {
    plugin_var->update(thd, plugin_var, tgt, src);
serg@janus.mylan's avatar
serg@janus.mylan committed
2753
    pthread_mutex_unlock(&LOCK_global_system_variables);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2754 2755
  }
  else
serg@janus.mylan's avatar
serg@janus.mylan committed
2756 2757
  {
    pthread_mutex_unlock(&LOCK_global_system_variables);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2758
    plugin_var->update(thd, plugin_var, tgt, src);
serg@janus.mylan's avatar
serg@janus.mylan committed
2759
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2760
}
serg@janus.mylan's avatar
serg@janus.mylan committed
2761

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2762 2763 2764 2765 2766 2767 2768 2769

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 */
serg@janus.mylan's avatar
serg@janus.mylan committed
2770
  DBUG_ASSERT(!(plugin_var->flags & PLUGIN_VAR_THDLOCAL) ||
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
              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; \
2799
  options->block_size= (long) (opt)->blk_sz
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2800 2801 2802 2803 2804


static void plugin_opt_set_limits(struct my_option *options,
                                  const struct st_mysql_sys_var *opt)
{
2805 2806
  options->sub_size= 0;

serg@janus.mylan's avatar
serg@janus.mylan committed
2807
  switch (opt->flags & (PLUGIN_VAR_TYPEMASK |
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
                        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;
2830
    options->typelib= ((sysvar_enum_t*) opt)->typelib;
2831
    options->def_value= ((sysvar_enum_t*) opt)->def_val;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2832 2833 2834 2835 2836
    options->min_value= options->block_size= 0;
    options->max_value= options->typelib->count - 1;
    break;
  case PLUGIN_VAR_SET:
    options->var_type= GET_SET;
2837
    options->typelib= ((sysvar_set_t*) opt)->typelib;
2838
    options->def_value= ((sysvar_set_t*) opt)->def_val;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2839 2840 2841 2842 2843
    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;
2844
    options->def_value= ((sysvar_bool_t*) opt)->def_val;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2845 2846
    break;
  case PLUGIN_VAR_STR:
2847 2848
    options->var_type= ((opt->flags & PLUGIN_VAR_MEMALLOC) ?
                        GET_STR_ALLOC : GET_STR);
2849
    options->def_value= (intptr) ((sysvar_str_t*) opt)->def_val;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
    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;
2872
    options->typelib= ((thdvar_enum_t*) opt)->typelib;
2873
    options->def_value= ((thdvar_enum_t*) opt)->def_val;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2874 2875 2876 2877 2878
    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;
2879
    options->typelib= ((thdvar_set_t*) opt)->typelib;
2880
    options->def_value= ((thdvar_set_t*) opt)->def_val;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2881 2882 2883 2884 2885
    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;
2886
    options->def_value= ((thdvar_bool_t*) opt)->def_val;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2887 2888
    break;
  case PLUGIN_VAR_STR | PLUGIN_VAR_THDLOCAL:
2889 2890
    options->var_type= ((opt->flags & PLUGIN_VAR_MEMALLOC) ?
                        GET_STR_ALLOC : GET_STR);
2891
    options->def_value= (intptr) ((thdvar_str_t*) opt)->def_val;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2892 2893 2894 2895 2896 2897 2898 2899
    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)
serg@janus.mylan's avatar
serg@janus.mylan committed
2900
    options->arg_type= OPT_ARG;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2901 2902
}

2903 2904
extern "C" my_bool get_one_plugin_option(int optid, const struct my_option *,
                                         char *);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2905

2906
my_bool get_one_plugin_option(int optid __attribute__((unused)),
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2907 2908 2909 2910 2911 2912 2913
                              const struct my_option *opt,
                              char *argument)
{
  return 0;
}


2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
/**
  Creates a set of my_option objects associated with a specified plugin-
  handle.

  @param mem_root Memory allocator to be used.
  @param tmp A pointer to a plugin handle
  @param[out] options A pointer to a pre-allocated static array

  The set is stored in the pre-allocated static array supplied to the function.
  The size of the array is calculated as (number_of_plugin_varaibles*2+3). The
  reason is that each option can have a prefix '--plugin-' in addtion to the
  shorter form '--&lt;plugin-name&gt;'. There is also space allocated for
  terminating NULL pointers.

  @return
    @retval -1 An error occurred
    @retval 0 Success
*/

serg@janus.mylan's avatar
serg@janus.mylan committed
2933
static int construct_options(MEM_ROOT *mem_root, struct st_plugin_int *tmp,
2934
                             my_option *options)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2935 2936
{
  const char *plugin_name= tmp->plugin->name;
2937 2938 2939 2940 2941 2942 2943
  const LEX_STRING plugin_dash = { C_STRING_WITH_LEN("plugin-") };
  uint plugin_name_len= strlen(plugin_name);
  uint optnamelen;
  const int max_comment_len= 180;
  char *comment= (char *) alloc_root(mem_root, max_comment_len + 1);
  char *optname;

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2944 2945 2946
  int index= 0, offset= 0;
  st_mysql_sys_var *opt, **plugin_option;
  st_bookmark *v;
2947 2948 2949 2950

  /** Used to circumvent the const attribute on my_option::name */
  char *plugin_name_ptr, *plugin_name_with_prefix_ptr;

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2951
  DBUG_ENTER("construct_options");
serg@janus.mylan's avatar
serg@janus.mylan committed
2952

2953 2954 2955 2956 2957
  options[0].name= plugin_name_ptr= (char*) alloc_root(mem_root,
                                                       plugin_name_len + 1);
  strcpy(plugin_name_ptr, plugin_name);
  my_casedn_str(&my_charset_latin1, plugin_name_ptr);
  convert_underscore_to_dash(plugin_name_ptr, plugin_name_len);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2958
  /* support --skip-plugin-foo syntax */
2959 2960 2961 2962
  options[1].name= plugin_name_with_prefix_ptr= (char*) alloc_root(mem_root,
                                                plugin_name_len +
                                                plugin_dash.length + 1);
  strxmov(plugin_name_with_prefix_ptr, plugin_dash.str, options[0].name, NullS);
serg@janus.mylan's avatar
serg@janus.mylan committed
2963

2964 2965 2966 2967 2968
  options[0].id= options[1].id= 256; /* must be >255. dup id ok */
  options[0].var_type= options[1].var_type= GET_ENUM;
  options[0].arg_type= options[1].arg_type= OPT_ARG;
  options[0].def_value= options[1].def_value= 1; /* ON */
  options[0].typelib= options[1].typelib= &global_plugin_typelib;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2969

2970 2971 2972 2973
  strxnmov(comment, max_comment_len, "Enable or disable ", plugin_name,
          " plugin. Possible values are ON, OFF, FORCE (don't start "
          "if the plugin fails to load).", NullS);
  options[0].comment= comment;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2974

2975
  /*
2976 2977 2978 2979
    Allocate temporary space for the value of the tristate.
    This option will have a limited lifetime and is not used beyond
    server initialization.
    GET_ENUM value is an integer.
2980
  */
2981 2982 2983 2984
  options[0].value= options[1].value= (uchar **)alloc_root(mem_root,
                                                          sizeof(int));
  *((uint*) options[0].value)= *((uint*) options[1].value)=
    (uint) options[0].def_value;
serg@janus.mylan's avatar
serg@janus.mylan committed
2985

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2986 2987 2988 2989 2990 2991 2992
  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
  */

serg@janus.mylan's avatar
serg@janus.mylan committed
2993
  for (plugin_option= tmp->plugin->system_vars;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2994 2995
       plugin_option && *plugin_option; plugin_option++, index++)
  {
serg@janus.mylan's avatar
serg@janus.mylan committed
2996
    opt= *plugin_option;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2997 2998
    if (!(opt->flags & PLUGIN_VAR_THDLOCAL))
      continue;
2999
    if (!(register_var(plugin_name_ptr, opt->name, opt->flags)))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
      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:
3018 3019
      SET_PLUGIN_VAR_RESOLVE((thdvar_enum_t *) opt);
      break;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3020
    case PLUGIN_VAR_SET:
3021
      SET_PLUGIN_VAR_RESOLVE((thdvar_set_t *) opt);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3022 3023 3024
      break;
    default:
      sql_print_error("Unknown variable type code 0x%x in plugin '%s'.",
serg@janus.mylan's avatar
serg@janus.mylan committed
3025
                      opt->flags, plugin_name);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3026 3027 3028 3029
      DBUG_RETURN(-1);
    };
  }

serg@janus.mylan's avatar
serg@janus.mylan committed
3030
  for (plugin_option= tmp->plugin->system_vars;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063
       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;
3064
        if (!(opt->flags & (PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_READONLY)))
serg@janus.mylan's avatar
serg@janus.mylan committed
3065
        {
3066
          opt->flags|= PLUGIN_VAR_READONLY;
serg@janus.mylan's avatar
serg@janus.mylan committed
3067 3068 3069 3070 3071
          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);
        }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
      }
      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)
3084
        opt->update= update_func_longlong;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3085 3086 3087
      break;
    default:
      sql_print_error("Unknown variable type code 0x%x in plugin '%s'.",
serg@janus.mylan's avatar
serg@janus.mylan committed
3088
                      opt->flags, plugin_name);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3089 3090
      DBUG_RETURN(-1);
    }
serg@janus.mylan's avatar
serg@janus.mylan committed
3091

3092 3093
    if ((opt->flags & (PLUGIN_VAR_NOCMDOPT | PLUGIN_VAR_THDLOCAL))
                    == PLUGIN_VAR_NOCMDOPT)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3094 3095 3096 3097 3098
      continue;

    if (!opt->name)
    {
      sql_print_error("Missing variable name in plugin '%s'.",
serg@janus.mylan's avatar
serg@janus.mylan committed
3099
                      plugin_name);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3100 3101 3102
      DBUG_RETURN(-1);
    }

3103
    if (!(opt->flags & PLUGIN_VAR_THDLOCAL))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3104 3105
    {
      optnamelen= strlen(opt->name);
3106 3107 3108
      optname= (char*) alloc_root(mem_root, plugin_name_len + optnamelen + 2);
      strxmov(optname, plugin_name_ptr, "-", opt->name, NullS);
      optnamelen= plugin_name_len + optnamelen + 1;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3109 3110
    }
    else
3111 3112
    {
      /* this should not fail because register_var should create entry */
3113
      if (!(v= find_bookmark(plugin_name_ptr, opt->name, opt->flags)))
3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
      {
        sql_print_error("Thread local variable '%s' not allocated "
                        "in plugin '%s'.", opt->name, plugin_name);
        DBUG_RETURN(-1);
      }

      *(int*)(opt + 1)= offset= v->offset;

      if (opt->flags & PLUGIN_VAR_NOCMDOPT)
        continue;

      optname= (char*) memdup_root(mem_root, v->key + 1, 
                                   (optnamelen= v->name_len) + 1);
    }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3128

3129
    convert_underscore_to_dash(optname, optnamelen);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3130 3131 3132

    options->name= optname;
    options->comment= opt->comment;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3133
    options->app_type= opt;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3134
    options->id= (options-1)->id + 1;
serg@janus.mylan's avatar
serg@janus.mylan committed
3135

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3136
    plugin_opt_set_limits(options, opt);
serg@janus.mylan's avatar
serg@janus.mylan committed
3137

3138 3139 3140 3141 3142
    if (opt->flags & PLUGIN_VAR_THDLOCAL)
      options->value= options->u_max_value= (uchar**)
        (global_system_variables.dynamic_variables_ptr + offset);
    else
      options->value= options->u_max_value= *(uchar***) (opt + 1);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3143

3144
    char *option_name_ptr;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3145
    options[1]= options[0];
3146 3147 3148 3149 3150
    options[1].name= option_name_ptr= (char*) alloc_root(mem_root,
                                                        plugin_dash.length +
                                                        optnamelen + 1);
    options[1].comment= 0; /* Hidden from the help text */
    strxmov(option_name_ptr, plugin_dash.str, optname, NullS);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166

    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;
  uint count= EXTRA_OPTIONS;
  DBUG_ENTER("construct_help_options");

3167 3168
  for (opt= p->plugin->system_vars; opt && *opt; opt++, count+= 2)
    ;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3169 3170 3171 3172 3173 3174

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

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

3175
  if (construct_options(mem_root, p, opts))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3176 3177 3178 3179 3180 3181
    DBUG_RETURN(NULL);

  DBUG_RETURN(opts);
}


3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199
/**
  Create and register system variables supplied from the plugin and
  assigns initial values from corresponding command line arguments.

  @param tmp_root Temporary scratch space
  @param[out] plugin Internal plugin structure
  @param argc Number of command line arguments
  @param argv Command line argument vector

  The plugin will be updated with a policy on how to handle errors during
  initialization.

  @note Requires that a write-lock is held on LOCK_system_variables_hash

  @return How initialization of the plugin should be handled.
    @retval  0 Initialization should proceed.
    @retval  1 Plugin is disabled.
    @retval -1 An error has occurred.
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3200
*/
3201

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3202
static int test_plugin_options(MEM_ROOT *tmp_root, struct st_plugin_int *tmp,
3203
                               int *argc, char **argv)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3204 3205
{
  struct sys_var_chain chain= { NULL, NULL };
3206 3207 3208 3209
  my_bool can_disable;
  bool disable_plugin;
  enum_plugin_load_policy plugin_load_policy= PLUGIN_ON;

serg@janus.mylan's avatar
serg@janus.mylan committed
3210
  MEM_ROOT *mem_root= alloc_root_inited(&tmp->mem_root) ?
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3211 3212
                      &tmp->mem_root : &plugin_mem_root;
  st_mysql_sys_var **opt;
3213
  my_option *opts= NULL;
3214
  char *varname;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3215 3216 3217 3218 3219 3220
  int error;
  st_mysql_sys_var *o;
  sys_var *v;
  struct st_bookmark *var;
  uint len, count= EXTRA_OPTIONS;
  DBUG_ENTER("test_plugin_options");
serg@janus.mylan's avatar
serg@janus.mylan committed
3221
  DBUG_ASSERT(tmp->plugin && tmp->name.str);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3222

3223 3224 3225 3226 3227 3228 3229 3230
  /*
    The 'federated' and 'ndbcluster' storage engines are always disabled by
    default.
  */
  if (!(my_strcasecmp(&my_charset_latin1, tmp->name.str, "federated") &&
      my_strcasecmp(&my_charset_latin1, tmp->name.str, "ndbcluster")))
    plugin_load_policy= PLUGIN_OFF;

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3231 3232 3233 3234
  for (opt= tmp->plugin->system_vars; opt && *opt; opt++)
    count+= 2; /* --{plugin}-{optname} and --plugin-{plugin}-{optname} */

  if (count > EXTRA_OPTIONS || (*argc > 1))
serg@janus.mylan's avatar
serg@janus.mylan committed
3235
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3236 3237
    if (!(opts= (my_option*) alloc_root(tmp_root, sizeof(my_option) * count)))
    {
serg@janus.mylan's avatar
serg@janus.mylan committed
3238
      sql_print_error("Out of memory for plugin '%s'.", tmp->name.str);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3239 3240 3241
      DBUG_RETURN(-1);
    }
    bzero(opts, sizeof(my_option) * count);
serg@janus.mylan's avatar
serg@janus.mylan committed
3242

3243
    if (construct_options(tmp_root, tmp, opts))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3244
    {
serg@janus.mylan's avatar
serg@janus.mylan committed
3245
      sql_print_error("Bad options for plugin '%s'.", tmp->name.str);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3246 3247 3248
      DBUG_RETURN(-1);
    }

3249 3250 3251 3252 3253 3254
    /*
      We adjust the default value to account for the hardcoded exceptions
      we have set for the federated and ndbcluster storage engines.
    */
    opts[0].def_value= opts[1].def_value= (int)plugin_load_policy;

3255
    error= handle_options(argc, &argv, opts, get_one_plugin_option);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3256
    (*argc)++; /* add back one for the program name */
serg@janus.mylan's avatar
serg@janus.mylan committed
3257

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3258 3259
    if (error)
    {
serg@janus.mylan's avatar
serg@janus.mylan committed
3260
       sql_print_error("Parsing options for plugin '%s' failed.",
serg@janus.mylan's avatar
serg@janus.mylan committed
3261
                       tmp->name.str);
3262
       goto err;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3263
    }
3264 3265 3266 3267 3268
    /*
     Set plugin loading policy from option value. First element in the option
     list is always the <plugin name> option value.
    */
    plugin_load_policy= (enum_plugin_load_policy)*(uint*)opts[0].value;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3269 3270
  }

3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281
  disable_plugin= (plugin_load_policy == PLUGIN_OFF);
  /*
    The 'MyISAM' and 'Memory' storage engines currently can't be disabled.
  */
  can_disable=
    my_strcasecmp(&my_charset_latin1, tmp->name.str, "MyISAM") &&
    my_strcasecmp(&my_charset_latin1, tmp->name.str, "MEMORY");

  tmp->is_mandatory= (plugin_load_policy == PLUGIN_FORCE) || !can_disable;

  if (disable_plugin && !can_disable)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3282
  {
serg@janus.mylan's avatar
serg@janus.mylan committed
3283
    sql_print_warning("Plugin '%s' cannot be disabled", tmp->name.str);
3284
    disable_plugin= FALSE;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3285 3286
  }

3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298
  /*
    If the plugin is disabled it should not be initialized.
  */
  if (disable_plugin)
  {
    if (global_system_variables.log_warnings)
      sql_print_information("Plugin '%s' is disabled.",
                            tmp->name.str);
    if (opts)
      my_cleanup_options(opts);
    DBUG_RETURN(1);
  }
3299

3300 3301
  error= 1;
  for (opt= tmp->plugin->system_vars; opt && *opt; opt++)
serg@janus.mylan's avatar
serg@janus.mylan committed
3302
  {
3303 3304 3305 3306 3307
    if (((o= *opt)->flags & PLUGIN_VAR_NOSYSVAR))
      continue;
    if ((var= find_bookmark(tmp->name.str, o->name, o->flags)))
      v= new (mem_root) sys_var_pluginvar(var->key + 1, o);
    else
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3308
    {
3309 3310 3311 3312
      len= tmp->name.length + strlen(o->name) + 2;
      varname= (char*) alloc_root(mem_root, len);
      strxmov(varname, tmp->name.str, "-", o->name, NullS);
      my_casedn_str(&my_charset_latin1, varname);
3313
      convert_dash_to_underscore(varname, len-1);
3314
      v= new (mem_root) sys_var_pluginvar(varname, o);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3315
    }
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327
    DBUG_ASSERT(v); /* check that an object was actually constructed */
    /*
      Add to the chain of variables.
      Done like this for easier debugging so that the
      pointer to v is not lost on optimized builds.
    */
    v->chain_sys_var(&chain);
  } /* end for */
  if (chain.first)
  {
    chain.last->next = NULL;
    if (mysql_add_sys_var_chain(chain.first, NULL))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3328
    {
3329 3330 3331
      sql_print_error("Plugin '%s' has conflicting system variables",
                      tmp->name.str);
      goto err;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3332
    }
3333
    tmp->system_vars= chain.first;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3334
  }
3335 3336
  DBUG_RETURN(0);
  
3337
err:
3338 3339
  if (opts)
    my_cleanup_options(opts);
3340
  DBUG_RETURN(error);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362
}


/****************************************************************************
  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);
serg@janus.mylan's avatar
serg@janus.mylan committed
3363

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3364 3365 3366
  if (initialized)
    for (uint idx= 0; idx < plugin_array.elements; idx++)
    {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3367
      p= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3368 3369 3370 3371 3372 3373 3374 3375

      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)
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
3376
          insert_dynamic(&all_options, (uchar*) opt);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3377
    }
serg@janus.mylan's avatar
serg@janus.mylan committed
3378

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3379
  for (;main_options->id; main_options++)
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
3380
    insert_dynamic(&all_options, (uchar*) main_options);
serg@janus.mylan's avatar
serg@janus.mylan committed
3381

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3382
  sort_dynamic(&all_options, (qsort_cmp) option_cmp);
serg@janus.mylan's avatar
serg@janus.mylan committed
3383

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3384
  /* main_options now points to the empty option terminator */
jani@linux-th5m.site's avatar
jani@linux-th5m.site committed
3385
  insert_dynamic(&all_options, (uchar*) main_options);
serg@janus.mylan's avatar
serg@janus.mylan committed
3386

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3387 3388 3389 3390 3391 3392 3393
  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));
}