~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/loader.cc

[patch 112/129] Merge patch for revision 1925 from InnoDB SVN:
revno: 1925
revision-id: svn-v4:16c675df-0fcb-4bc9-8058-dcc011a37293:branches/zip:6169
parent: svn-v4:16c675df-0fcb-4bc9-8058-dcc011a37293:branches/zip:6163
committer: calvin
timestamp: Thu 2009-11-12 12:40:43 +0000
message:
  branches/zip: add test case for bug#46676
  
  This crash is reproducible with InnoDB plugin 1.0.4 + MySQL 5.1.37.
  But no longer reproducible after MySQL 5.1.38 (with plugin 1.0.5).
  Add test case to catch future regression.
added:
  mysql-test/innodb_bug46676.result 6169@16c675df-0fcb-4bc9-8058-dcc011a37293:branches%2Fzip%2Fmysql-test%2Finnodb_bug46676.result
  mysql-test/innodb_bug46676.test 6169@16c675df-0fcb-4bc9-8058-dcc011a37293:branches%2Fzip%2Fmysql-test%2Finnodb_bug46676.test
diff:
=== added file 'mysql-test/innodb_bug46676.result'

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#include "drizzled/pthread_globals.h"
48
48
#include "drizzled/util/tokenize.h"
49
49
 
50
 
#include <boost/foreach.hpp>
51
 
 
52
50
/* FreeBSD 2.2.2 does not define RTLD_NOW) */
53
51
#ifndef RTLD_NOW
54
52
#define RTLD_NOW 1
61
59
/** These exist just to prevent symbols from being optimized out */
62
60
typedef drizzled::module::Manifest drizzled_builtin_list[];
63
61
extern drizzled_builtin_list PANDORA_BUILTIN_SYMBOLS_LIST;
64
 
extern drizzled_builtin_list PANDORA_BUILTIN_LOAD_SYMBOLS_LIST;
65
62
drizzled::module::Manifest *drizzled_builtins[]=
66
63
{
67
64
  PANDORA_BUILTIN_SYMBOLS_LIST, NULL
68
65
};
69
 
drizzled::module::Manifest *drizzled_load_builtins[]=
70
 
{
71
 
  PANDORA_BUILTIN_LOAD_SYMBOLS_LIST, NULL
72
 
};
73
66
 
74
67
namespace drizzled
75
68
{
76
69
 
77
70
 
 
71
class sys_var_pluginvar;
 
72
static vector<sys_var_pluginvar *> plugin_sysvar_vec;
 
73
 
78
74
typedef vector<string> PluginOptions;
79
75
static PluginOptions opt_plugin_load;
80
76
static PluginOptions opt_plugin_add;
81
77
static PluginOptions opt_plugin_remove;
 
78
char opt_plugin_dir[FN_REFLEN];
82
79
const char *builtin_plugins= PANDORA_BUILTIN_LIST;
83
 
const char *builtin_load_plugins= PANDORA_BUILTIN_LOAD_LIST;
84
80
 
85
81
/* Note that 'int version' must be the first field of every plugin
86
82
   sub-structure (plugin->info).
127
123
static bookmark_unordered_map bookmark_hash;
128
124
 
129
125
 
 
126
/*
 
127
  sys_var class for access to all plugin variables visible to the user
 
128
*/
 
129
class sys_var_pluginvar: public sys_var
 
130
{
 
131
public:
 
132
  module::Module *plugin;
 
133
  drizzle_sys_var *plugin_var;
 
134
 
 
135
  sys_var_pluginvar(const std::string name_arg,
 
136
                    drizzle_sys_var *plugin_var_arg)
 
137
    :sys_var(name_arg), plugin_var(plugin_var_arg) {}
 
138
  sys_var_pluginvar *cast_pluginvar() { return this; }
 
139
  bool is_readonly() const { return plugin_var->flags & PLUGIN_VAR_READONLY; }
 
140
  bool check_type(sql_var_t type)
 
141
  { return !(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) && type != OPT_GLOBAL; }
 
142
  bool check_update_type(Item_result type);
 
143
  SHOW_TYPE show_type();
 
144
  unsigned char* real_value_ptr(Session *session, sql_var_t type);
 
145
  TYPELIB* plugin_var_typelib(void);
 
146
  unsigned char* value_ptr(Session *session, sql_var_t type,
 
147
                           const LEX_STRING *base);
 
148
  bool check(Session *session, set_var *var);
 
149
  bool check_default(sql_var_t)
 
150
    { return is_readonly(); }
 
151
  void set_default(Session *session, sql_var_t);
 
152
  bool update(Session *session, set_var *var);
 
153
};
 
154
 
130
155
 
131
156
/* prototypes */
132
157
static void plugin_prune_list(vector<string> &plugin_list,
138
163
                             bool builtin= false);
139
164
static int test_plugin_options(memory::Root *, module::Module *,
140
165
                               po::options_description &long_options);
141
 
static void unlock_variables(Session *session, drizzle_system_variables *vars);
142
 
static void cleanup_variables(drizzle_system_variables *vars);
 
166
static void unlock_variables(Session *session, struct system_variables *vars);
 
167
static void cleanup_variables(Session *session, struct system_variables *vars);
 
168
static void plugin_vars_free_values(sys_var *vars);
143
169
 
144
170
/* declared in set_var.cc */
145
171
extern sys_var *intern_find_sys_var(const char *str, uint32_t length, bool no_error);
 
172
extern bool throw_bounds_warning(Session *session, bool fixed, bool unsignd,
 
173
                                 const std::string &name, int64_t val);
 
174
 
 
175
static bool throw_bounds_warning(Session *session, bool fixed, bool unsignd,
 
176
                                 const char *name, int64_t val)
 
177
{
 
178
  const std::string name_str(name);
 
179
  return throw_bounds_warning(session, fixed, unsignd, name_str, val);
 
180
}
 
181
 
 
182
/****************************************************************************
 
183
  Value type thunks, allows the C world to play in the C++ world
 
184
****************************************************************************/
 
185
 
 
186
static int item_value_type(drizzle_value *value)
 
187
{
 
188
  switch (((st_item_value_holder*)value)->item->result_type()) {
 
189
  case INT_RESULT:
 
190
    return DRIZZLE_VALUE_TYPE_INT;
 
191
  case REAL_RESULT:
 
192
    return DRIZZLE_VALUE_TYPE_REAL;
 
193
  default:
 
194
    return DRIZZLE_VALUE_TYPE_STRING;
 
195
  }
 
196
}
 
197
 
 
198
static const char *item_val_str(drizzle_value *value,
 
199
                                char *buffer, int *length)
 
200
{
 
201
  String str(buffer, *length, system_charset_info), *res;
 
202
  if (!(res= ((st_item_value_holder*)value)->item->val_str(&str)))
 
203
    return NULL;
 
204
  *length= res->length();
 
205
  if (res->c_ptr_quick() == buffer)
 
206
    return buffer;
 
207
 
 
208
  /*
 
209
    Lets be nice and create a temporary string since the
 
210
    buffer was too small
 
211
  */
 
212
  return current_session->strmake(res->c_ptr_quick(), res->length());
 
213
}
 
214
 
 
215
 
 
216
static int item_val_int(drizzle_value *value, int64_t *buf)
 
217
{
 
218
  Item *item= ((st_item_value_holder*)value)->item;
 
219
  *buf= item->val_int();
 
220
  if (item->is_null())
 
221
    return 1;
 
222
  return 0;
 
223
}
 
224
 
 
225
 
 
226
static int item_val_real(drizzle_value *value, double *buf)
 
227
{
 
228
  Item *item= ((st_item_value_holder*)value)->item;
 
229
  *buf= item->val_real();
 
230
  if (item->is_null())
 
231
    return 1;
 
232
  return 0;
 
233
}
146
234
 
147
235
 
148
236
/****************************************************************************
199
287
}
200
288
 
201
289
 
 
290
static void delete_module(module::Module *module)
 
291
{
 
292
  /* Free allocated strings before deleting the plugin. */
 
293
  plugin_vars_free_values(module->system_vars);
 
294
  module->isInited= false;
 
295
  mysql_del_sys_var_chain(module->system_vars);
 
296
  delete module;
 
297
}
 
298
 
 
299
 
202
300
static void reap_plugins(module::Registry &registry)
203
301
{
204
302
  std::map<std::string, module::Module *>::const_iterator modules=
207
305
  while (modules != registry.getModulesMap().end())
208
306
  {
209
307
    module::Module *module= (*modules).second;
210
 
    delete module;
 
308
    delete_module(module);
211
309
    ++modules;
212
310
  }
 
311
 
 
312
  drizzle_del_plugin_sysvar();
 
313
}
 
314
 
 
315
 
 
316
static void plugin_initialize_vars(module::Module *module)
 
317
{
 
318
  /*
 
319
    set the plugin attribute of plugin's sys vars so they are pointing
 
320
    to the active plugin
 
321
  */
 
322
  if (module->system_vars)
 
323
  {
 
324
    sys_var_pluginvar *var= module->system_vars->cast_pluginvar();
 
325
    for (;;)
 
326
    {
 
327
      var->plugin= module;
 
328
      if (! var->getNext())
 
329
        break;
 
330
      var= var->getNext()->cast_pluginvar();
 
331
    }
 
332
  }
213
333
}
214
334
 
215
335
 
235
355
  return false;
236
356
}
237
357
 
238
 
 
239
 
inline static void dashes_to_underscores(std::string &name_in,
240
 
                                         char from= '-', char to= '_')
241
 
{
242
 
  for (string::iterator p= name_in.begin();
243
 
       p != name_in.end();
244
 
       ++p)
245
 
  {
246
 
    if (*p == from)
247
 
    {
248
 
      *p= to;
249
 
    }
250
 
  }
251
 
}
252
 
 
253
 
inline static void underscores_to_dashes(std::string &name_in)
254
 
{
255
 
  return dashes_to_underscores(name_in, '_', '-');
256
 
}
257
 
 
258
358
static void compose_plugin_options(vector<string> &target,
259
359
                                   vector<string> options)
260
360
{
264
364
  {
265
365
    tokenize(*it, target, ",", true);
266
366
  }
267
 
  for (vector<string>::iterator it= target.begin();
268
 
       it != target.end();
269
 
       ++it)
270
 
  {
271
 
    dashes_to_underscores(*it);
272
 
  }
273
367
}
274
368
 
275
369
void compose_plugin_add(vector<string> options)
304
398
 
305
399
  initialized= 1;
306
400
 
307
 
  PluginOptions builtin_load_list;
308
 
  tokenize(builtin_load_plugins, builtin_load_list, ",", true);
309
 
 
310
401
  PluginOptions builtin_list;
311
402
  tokenize(builtin_plugins, builtin_list, ",", true);
312
403
 
314
405
 
315
406
  if (opt_plugin_add.size() > 0)
316
407
  {
317
 
    for (PluginOptions::iterator iter= opt_plugin_add.begin();
318
 
         iter != opt_plugin_add.end();
319
 
         ++iter)
320
 
    {
321
 
      if (find(builtin_list.begin(),
322
 
               builtin_list.end(), *iter) != builtin_list.end())
323
 
      {
324
 
        builtin_load_list.push_back(*iter);
325
 
      }
326
 
      else
327
 
      {
328
 
        opt_plugin_load.push_back(*iter);
329
 
      }
330
 
    }
 
408
    opt_plugin_load.insert(opt_plugin_load.end(),
 
409
                           opt_plugin_add.begin(),
 
410
                           opt_plugin_add.end());
331
411
  }
332
412
 
333
413
  if (opt_plugin_remove.size() > 0)
334
414
  {
335
415
    plugin_prune_list(opt_plugin_load, opt_plugin_remove);
336
 
    plugin_prune_list(builtin_load_list, opt_plugin_remove);
 
416
    plugin_prune_list(builtin_list, opt_plugin_remove);
337
417
  }
338
418
 
339
419
 
340
420
  /*
341
421
    First we register builtin plugins
342
422
  */
343
 
  const set<string> builtin_list_set(builtin_load_list.begin(),
344
 
                                     builtin_load_list.end());
 
423
  const set<string> builtin_list_set(builtin_list.begin(), builtin_list.end());
345
424
  load_failed= plugin_load_list(registry, &tmp_root,
346
425
                                builtin_list_set, long_options, true);
347
426
  if (load_failed)
370
449
 
371
450
bool plugin_finalize(module::Registry &registry)
372
451
{
 
452
 
373
453
  /*
374
454
    Now we initialize all remaining plugins
375
455
  */
382
462
    ++modules;
383
463
    if (module->isInited == false)
384
464
    {
 
465
      plugin_initialize_vars(module);
 
466
 
385
467
      if (plugin_initialize(registry, module))
386
468
      {
387
469
        registry.remove(module);
388
 
        delete module;
 
470
        delete_module(module);
389
471
        return true;
390
472
      }
391
473
    }
392
474
  }
393
 
 
394
 
  BOOST_FOREACH(plugin::Plugin::map::value_type value, registry.getPluginsMap())
395
 
  {
396
 
    value.second->prime();
397
 
  }
398
 
 
399
475
  return false;
400
476
}
401
477
 
482
558
    unlock_variables(NULL, &global_system_variables);
483
559
    unlock_variables(NULL, &max_system_variables);
484
560
 
485
 
    cleanup_variables(&global_system_variables);
486
 
    cleanup_variables(&max_system_variables);
 
561
    cleanup_variables(NULL, &global_system_variables);
 
562
    cleanup_variables(NULL, &max_system_variables);
487
563
 
488
564
    initialized= 0;
489
565
  }
494
570
  global_variables_dynamic_size= 0;
495
571
}
496
572
 
 
573
/****************************************************************************
 
574
  Internal type declarations for variables support
 
575
****************************************************************************/
 
576
 
 
577
#undef DRIZZLE_SYSVAR_NAME
 
578
#define DRIZZLE_SYSVAR_NAME(name) name
 
579
#define PLUGIN_VAR_TYPEMASK 0x007f
 
580
 
 
581
static const uint32_t EXTRA_OPTIONS= 1; /* handle the NULL option */
 
582
 
 
583
typedef DECLARE_DRIZZLE_SYSVAR_BOOL(sysvar_bool_t);
 
584
typedef DECLARE_DRIZZLE_SessionVAR_BOOL(sessionvar_bool_t);
 
585
typedef DECLARE_DRIZZLE_SYSVAR_BASIC(sysvar_str_t, char *);
 
586
typedef DECLARE_DRIZZLE_SessionVAR_BASIC(sessionvar_str_t, char *);
 
587
 
 
588
typedef DECLARE_DRIZZLE_SessionVAR_TYPELIB(sessionvar_enum_t, unsigned long);
 
589
typedef DECLARE_DRIZZLE_SessionVAR_TYPELIB(sessionvar_set_t, uint64_t);
 
590
 
 
591
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_int_t, int);
 
592
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_long_t, long);
 
593
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_int64_t_t, int64_t);
 
594
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_uint_t, uint);
 
595
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_ulong_t, ulong);
 
596
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_uint64_t_t, uint64_t);
 
597
 
 
598
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_int_t, int);
 
599
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_long_t, long);
 
600
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_int64_t_t, int64_t);
 
601
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_uint_t, uint);
 
602
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_ulong_t, ulong);
 
603
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_uint64_t_t, uint64_t);
 
604
 
 
605
typedef bool *(*mysql_sys_var_ptr_p)(Session* a_session, int offset);
 
606
 
 
607
 
 
608
/****************************************************************************
 
609
  default variable data check and update functions
 
610
****************************************************************************/
 
611
 
 
612
static int check_func_bool(Session *, drizzle_sys_var *var,
 
613
                           void *save, drizzle_value *value)
 
614
{
 
615
  char buff[STRING_BUFFER_USUAL_SIZE];
 
616
  const char *strvalue= "NULL", *str;
 
617
  int result, length;
 
618
  int64_t tmp;
 
619
 
 
620
  if (value->value_type(value) == DRIZZLE_VALUE_TYPE_STRING)
 
621
  {
 
622
    length= sizeof(buff);
 
623
    if (!(str= value->val_str(value, buff, &length)) ||
 
624
        (result= find_type(&bool_typelib, str, length, 1)-1) < 0)
 
625
    {
 
626
      if (str)
 
627
        strvalue= str;
 
628
      goto err;
 
629
    }
 
630
  }
 
631
  else
 
632
  {
 
633
    if (value->val_int(value, &tmp) < 0)
 
634
      goto err;
 
635
    if (tmp > 1)
 
636
    {
 
637
      internal::llstr(tmp, buff);
 
638
      strvalue= buff;
 
639
      goto err;
 
640
    }
 
641
    result= (int) tmp;
 
642
  }
 
643
  *(int*)save= -result;
 
644
  return 0;
 
645
err:
 
646
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->name, strvalue);
 
647
  return 1;
 
648
}
 
649
 
 
650
 
 
651
static int check_func_int(Session *session, drizzle_sys_var *var,
 
652
                          void *save, drizzle_value *value)
 
653
{
 
654
  bool fixed;
 
655
  int64_t tmp;
 
656
  struct option options;
 
657
  value->val_int(value, &tmp);
 
658
  plugin_opt_set_limits(&options, var);
 
659
 
 
660
  if (var->flags & PLUGIN_VAR_UNSIGNED)
 
661
    *(uint32_t *)save= (uint32_t) getopt_ull_limit_value((uint64_t) tmp, &options,
 
662
                                                   &fixed);
 
663
  else
 
664
    *(int *)save= (int) getopt_ll_limit_value(tmp, &options, &fixed);
 
665
 
 
666
  return throw_bounds_warning(session, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
 
667
                              var->name, (int64_t) tmp);
 
668
}
 
669
 
 
670
 
 
671
static int check_func_long(Session *session, drizzle_sys_var *var,
 
672
                          void *save, drizzle_value *value)
 
673
{
 
674
  bool fixed;
 
675
  int64_t tmp;
 
676
  struct option options;
 
677
  value->val_int(value, &tmp);
 
678
  plugin_opt_set_limits(&options, var);
 
679
 
 
680
  if (var->flags & PLUGIN_VAR_UNSIGNED)
 
681
    *(ulong *)save= (ulong) getopt_ull_limit_value((uint64_t) tmp, &options,
 
682
                                                   &fixed);
 
683
  else
 
684
    *(long *)save= (long) getopt_ll_limit_value(tmp, &options, &fixed);
 
685
 
 
686
  return throw_bounds_warning(session, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
 
687
                              var->name, (int64_t) tmp);
 
688
}
 
689
 
 
690
 
 
691
static int check_func_int64_t(Session *session, drizzle_sys_var *var,
 
692
                               void *save, drizzle_value *value)
 
693
{
 
694
  bool fixed;
 
695
  int64_t tmp;
 
696
  struct option options;
 
697
  value->val_int(value, &tmp);
 
698
  plugin_opt_set_limits(&options, var);
 
699
 
 
700
  if (var->flags & PLUGIN_VAR_UNSIGNED)
 
701
    *(uint64_t *)save= getopt_ull_limit_value((uint64_t) tmp, &options,
 
702
                                               &fixed);
 
703
  else
 
704
    *(int64_t *)save= getopt_ll_limit_value(tmp, &options, &fixed);
 
705
 
 
706
  return throw_bounds_warning(session, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
 
707
                              var->name, (int64_t) tmp);
 
708
}
 
709
 
 
710
static int check_func_str(Session *session, drizzle_sys_var *,
 
711
                          void *save, drizzle_value *value)
 
712
{
 
713
  char buff[STRING_BUFFER_USUAL_SIZE];
 
714
  const char *str;
 
715
  int length;
 
716
 
 
717
  length= sizeof(buff);
 
718
  if ((str= value->val_str(value, buff, &length)))
 
719
    str= session->strmake(str, length);
 
720
  *(const char**)save= str;
 
721
  return 0;
 
722
}
 
723
 
 
724
 
 
725
static void update_func_bool(Session *, drizzle_sys_var *,
 
726
                             void *tgt, const void *save)
 
727
{
 
728
  *(bool *) tgt= *(int *) save ? 1 : 0;
 
729
}
 
730
 
 
731
 
 
732
static void update_func_int(Session *, drizzle_sys_var *,
 
733
                             void *tgt, const void *save)
 
734
{
 
735
  *(int *)tgt= *(int *) save;
 
736
}
 
737
 
 
738
 
 
739
static void update_func_long(Session *, drizzle_sys_var *,
 
740
                             void *tgt, const void *save)
 
741
{
 
742
  *(long *)tgt= *(long *) save;
 
743
}
 
744
 
 
745
 
 
746
static void update_func_int64_t(Session *, drizzle_sys_var *,
 
747
                                 void *tgt, const void *save)
 
748
{
 
749
  *(int64_t *)tgt= *(uint64_t *) save;
 
750
}
 
751
 
 
752
 
 
753
static void update_func_str(Session *, drizzle_sys_var *var,
 
754
                             void *tgt, const void *save)
 
755
{
 
756
  char *old= *(char **) tgt;
 
757
  *(char **)tgt= *(char **) save;
 
758
  if (var->flags & PLUGIN_VAR_MEMALLOC)
 
759
  {
 
760
    *(char **)tgt= strdup(*(char **) save);
 
761
    free(old);
 
762
    /*
 
763
     * There isn't a _really_ good thing to do here until this whole set_var
 
764
     * mess gets redesigned
 
765
     */
 
766
    if (tgt == NULL)
 
767
      errmsg_printf(ERRMSG_LVL_ERROR, _("Out of memory."));
 
768
 
 
769
  }
 
770
}
 
771
 
497
772
 
498
773
/****************************************************************************
499
774
  System Variables support
500
775
****************************************************************************/
501
776
 
502
777
 
503
 
sys_var *find_sys_var(const char *str, uint32_t length)
504
 
{
505
 
  return intern_find_sys_var(str, length, false);
506
 
}
507
 
 
 
778
sys_var *find_sys_var(Session *, const char *str, uint32_t length)
 
779
{
 
780
  sys_var *var;
 
781
  sys_var_pluginvar *pi= NULL;
 
782
  module::Module *module;
 
783
 
 
784
  if ((var= intern_find_sys_var(str, length, false)) &&
 
785
      (pi= var->cast_pluginvar()))
 
786
  {
 
787
    if (!(module= pi->plugin))
 
788
      var= NULL; /* failed to lock it, it must be uninstalling */
 
789
    else if (module->isInited == false)
 
790
    {
 
791
      var= NULL;
 
792
    }
 
793
  }
 
794
 
 
795
  /*
 
796
    If the variable exists but the plugin it is associated with is not ready
 
797
    then the intern_plugin_lock did not raise an error, so we do it here.
 
798
  */
 
799
  if (pi && !var)
 
800
    my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
 
801
  return(var);
 
802
}
 
803
 
 
804
static const string make_bookmark_name(const string &plugin, const char *name)
 
805
{
 
806
  string varname(plugin);
 
807
  varname.push_back('_');
 
808
  varname.append(name);
 
809
 
 
810
  for (string::iterator p= varname.begin() + 1; p != varname.end(); ++p)
 
811
  {
 
812
    if (*p == '-')
 
813
    {
 
814
      *p= '_';
 
815
    }
 
816
  }
 
817
  return varname;
 
818
}
 
819
 
 
820
/*
 
821
  called by register_var, construct_options and test_plugin_options.
 
822
  Returns the 'bookmark' for the named variable.
 
823
  LOCK_system_variables_hash should be at least read locked
 
824
*/
 
825
static Bookmark *find_bookmark(const string &plugin, const char *name, int flags)
 
826
{
 
827
  if (!(flags & PLUGIN_VAR_SessionLOCAL))
 
828
    return NULL;
 
829
 
 
830
  const string varname(make_bookmark_name(plugin, name));
 
831
 
 
832
  bookmark_unordered_map::iterator iter= bookmark_hash.find(varname);
 
833
  if (iter != bookmark_hash.end())
 
834
  {
 
835
    return &((*iter).second);
 
836
  }
 
837
  return NULL;
 
838
}
 
839
 
 
840
 
 
841
/*
 
842
  returns a bookmark for session-local variables, creating if neccessary.
 
843
  returns null for non session-local variables.
 
844
  Requires that a write lock is obtained on LOCK_system_variables_hash
 
845
*/
 
846
static Bookmark *register_var(const string &plugin, const char *name,
 
847
                                 int flags)
 
848
{
 
849
  if (!(flags & PLUGIN_VAR_SessionLOCAL))
 
850
    return NULL;
 
851
 
 
852
  uint32_t size= 0, offset, new_size;
 
853
  Bookmark *result= NULL;
 
854
 
 
855
  switch (flags & PLUGIN_VAR_TYPEMASK) {
 
856
  case PLUGIN_VAR_BOOL:
 
857
    size= ALIGN_SIZE(sizeof(bool));
 
858
    break;
 
859
  case PLUGIN_VAR_INT:
 
860
    size= ALIGN_SIZE(sizeof(int));
 
861
    break;
 
862
  case PLUGIN_VAR_LONG:
 
863
    size= ALIGN_SIZE(sizeof(long));
 
864
    break;
 
865
  case PLUGIN_VAR_LONGLONG:
 
866
    size= ALIGN_SIZE(sizeof(uint64_t));
 
867
    break;
 
868
  case PLUGIN_VAR_STR:
 
869
    size= ALIGN_SIZE(sizeof(char*));
 
870
    break;
 
871
  default:
 
872
    assert(0);
 
873
    return NULL;
 
874
  };
 
875
 
 
876
 
 
877
  if (!(result= find_bookmark(plugin, name, flags)))
 
878
  {
 
879
    const string varname(make_bookmark_name(plugin, name));
 
880
 
 
881
    Bookmark new_bookmark;
 
882
    new_bookmark.key= varname;
 
883
    new_bookmark.offset= -1;
 
884
 
 
885
    assert(size && !(size & (size-1))); /* must be power of 2 */
 
886
 
 
887
    offset= global_system_variables.dynamic_variables_size;
 
888
    offset= (offset + size - 1) & ~(size - 1);
 
889
    new_bookmark.offset= (int) offset;
 
890
 
 
891
    new_size= (offset + size + 63) & ~63;
 
892
 
 
893
    if (new_size > global_variables_dynamic_size)
 
894
    {
 
895
      char* tmpptr= NULL;
 
896
      if (!(tmpptr=
 
897
              (char *)realloc(global_system_variables.dynamic_variables_ptr,
 
898
                              new_size)))
 
899
        return NULL;
 
900
      global_system_variables.dynamic_variables_ptr= tmpptr;
 
901
      tmpptr= NULL;
 
902
      if (!(tmpptr=
 
903
              (char *)realloc(max_system_variables.dynamic_variables_ptr,
 
904
                              new_size)))
 
905
        return NULL;
 
906
      max_system_variables.dynamic_variables_ptr= tmpptr;
 
907
           
 
908
      /*
 
909
        Clear the new variable value space. This is required for string
 
910
        variables. If their value is non-NULL, it must point to a valid
 
911
        string.
 
912
      */
 
913
      memset(global_system_variables.dynamic_variables_ptr +
 
914
             global_variables_dynamic_size, 0,
 
915
             new_size - global_variables_dynamic_size);
 
916
      memset(max_system_variables.dynamic_variables_ptr +
 
917
             global_variables_dynamic_size, 0,
 
918
             new_size - global_variables_dynamic_size);
 
919
      global_variables_dynamic_size= new_size;
 
920
    }
 
921
 
 
922
    global_system_variables.dynamic_variables_head= offset;
 
923
    max_system_variables.dynamic_variables_head= offset;
 
924
    global_system_variables.dynamic_variables_size= offset + size;
 
925
    max_system_variables.dynamic_variables_size= offset + size;
 
926
    global_system_variables.dynamic_variables_version++;
 
927
    max_system_variables.dynamic_variables_version++;
 
928
 
 
929
    new_bookmark.version= global_system_variables.dynamic_variables_version;
 
930
    new_bookmark.type_code= flags;
 
931
 
 
932
    /* this should succeed because we have already checked if a dup exists */
 
933
    bookmark_hash.insert(make_pair(varname, new_bookmark));
 
934
    result= find_bookmark(plugin, name, flags);
 
935
  }
 
936
  return result;
 
937
}
 
938
 
 
939
 
 
940
/*
 
941
  returns a pointer to the memory which holds the session-local variable or
 
942
  a pointer to the global variable if session==null.
 
943
  If required, will sync with global variables if the requested variable
 
944
  has not yet been allocated in the current thread.
 
945
*/
 
946
static unsigned char *intern_sys_var_ptr(Session* session, int offset, bool global_lock)
 
947
{
 
948
  assert(offset >= 0);
 
949
  assert((uint32_t)offset <= global_system_variables.dynamic_variables_head);
 
950
 
 
951
  if (!session)
 
952
    return (unsigned char*) global_system_variables.dynamic_variables_ptr + offset;
 
953
 
 
954
  /*
 
955
    dynamic_variables_head points to the largest valid offset
 
956
  */
 
957
  if (!session->variables.dynamic_variables_ptr ||
 
958
      (uint32_t)offset > session->variables.dynamic_variables_head)
 
959
  {
 
960
    char *tmpptr= NULL;
 
961
    if (!(tmpptr= (char *)realloc(session->variables.dynamic_variables_ptr,
 
962
                                  global_variables_dynamic_size)))
 
963
      return NULL;
 
964
    session->variables.dynamic_variables_ptr= tmpptr;
 
965
 
 
966
    if (global_lock)
 
967
      LOCK_global_system_variables.lock();
 
968
 
 
969
    //safe_mutex_assert_owner(&LOCK_global_system_variables);
 
970
 
 
971
    memcpy(session->variables.dynamic_variables_ptr +
 
972
             session->variables.dynamic_variables_size,
 
973
           global_system_variables.dynamic_variables_ptr +
 
974
             session->variables.dynamic_variables_size,
 
975
           global_system_variables.dynamic_variables_size -
 
976
             session->variables.dynamic_variables_size);
 
977
 
 
978
    /*
 
979
      now we need to iterate through any newly copied 'defaults'
 
980
      and if it is a string type with MEMALLOC flag, we need to strdup
 
981
    */
 
982
    bookmark_unordered_map::iterator iter= bookmark_hash.begin();
 
983
    for (; iter != bookmark_hash.end() ; ++iter)
 
984
    {
 
985
      sys_var_pluginvar *pi;
 
986
      sys_var *var;
 
987
      const Bookmark &v= (*iter).second;
 
988
      const string var_name((*iter).first);
 
989
 
 
990
      if (v.version <= session->variables.dynamic_variables_version ||
 
991
          !(var= intern_find_sys_var(var_name.c_str(), var_name.size(), true)) ||
 
992
          !(pi= var->cast_pluginvar()) ||
 
993
          v.type_code != (pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
 
994
        continue;
 
995
 
 
996
      /* Here we do anything special that may be required of the data types */
 
997
 
 
998
      if ((pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
 
999
          pi->plugin_var->flags & PLUGIN_VAR_MEMALLOC)
 
1000
      {
 
1001
         char **pp= (char**) (session->variables.dynamic_variables_ptr +
 
1002
                             *(int*)(pi->plugin_var + 1));
 
1003
         if ((*pp= *(char**) (global_system_variables.dynamic_variables_ptr +
 
1004
                             *(int*)(pi->plugin_var + 1))))
 
1005
           *pp= strdup(*pp);
 
1006
         if (*pp == NULL)
 
1007
           return NULL;
 
1008
      }
 
1009
    }
 
1010
 
 
1011
    if (global_lock)
 
1012
      LOCK_global_system_variables.unlock();
 
1013
 
 
1014
    session->variables.dynamic_variables_version=
 
1015
           global_system_variables.dynamic_variables_version;
 
1016
    session->variables.dynamic_variables_head=
 
1017
           global_system_variables.dynamic_variables_head;
 
1018
    session->variables.dynamic_variables_size=
 
1019
           global_system_variables.dynamic_variables_size;
 
1020
  }
 
1021
  return (unsigned char*)session->variables.dynamic_variables_ptr + offset;
 
1022
}
 
1023
 
 
1024
static bool *mysql_sys_var_ptr_bool(Session* a_session, int offset)
 
1025
{
 
1026
  return (bool *)intern_sys_var_ptr(a_session, offset, true);
 
1027
}
 
1028
 
 
1029
static int *mysql_sys_var_ptr_int(Session* a_session, int offset)
 
1030
{
 
1031
  return (int *)intern_sys_var_ptr(a_session, offset, true);
 
1032
}
 
1033
 
 
1034
static long *mysql_sys_var_ptr_long(Session* a_session, int offset)
 
1035
{
 
1036
  return (long *)intern_sys_var_ptr(a_session, offset, true);
 
1037
}
 
1038
 
 
1039
static int64_t *mysql_sys_var_ptr_int64_t(Session* a_session, int offset)
 
1040
{
 
1041
  return (int64_t *)intern_sys_var_ptr(a_session, offset, true);
 
1042
}
 
1043
 
 
1044
static char **mysql_sys_var_ptr_str(Session* a_session, int offset)
 
1045
{
 
1046
  return (char **)intern_sys_var_ptr(a_session, offset, true);
 
1047
}
508
1048
 
509
1049
void plugin_sessionvar_init(Session *session)
510
1050
{
511
1051
  session->variables.storage_engine= NULL;
512
 
  cleanup_variables(&session->variables);
 
1052
  cleanup_variables(session, &session->variables);
513
1053
 
514
1054
  session->variables= global_system_variables;
515
1055
  session->variables.storage_engine= NULL;
526
1066
/*
527
1067
  Unlocks all system variables which hold a reference
528
1068
*/
529
 
static void unlock_variables(Session *, struct drizzle_system_variables *vars)
 
1069
static void unlock_variables(Session *, struct system_variables *vars)
530
1070
{
531
1071
  vars->storage_engine= NULL;
532
1072
}
538
1078
  Unlike plugin_vars_free_values() it frees all variables of all plugins,
539
1079
  it's used on shutdown.
540
1080
*/
541
 
static void cleanup_variables(drizzle_system_variables *vars)
 
1081
static void cleanup_variables(Session *session, struct system_variables *vars)
542
1082
{
 
1083
  sys_var_pluginvar *pivar;
 
1084
  sys_var *var;
 
1085
  int flags;
 
1086
 
 
1087
  bookmark_unordered_map::iterator iter= bookmark_hash.begin();
 
1088
  for (; iter != bookmark_hash.end() ; ++iter)
 
1089
  {
 
1090
    const Bookmark &v= (*iter).second;
 
1091
    const string key_name((*iter).first);
 
1092
    if (v.version > vars->dynamic_variables_version ||
 
1093
        !(var= intern_find_sys_var(key_name.c_str(), key_name.size(), true)) ||
 
1094
        !(pivar= var->cast_pluginvar()) ||
 
1095
        v.type_code != (pivar->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
 
1096
      continue;
 
1097
 
 
1098
    flags= pivar->plugin_var->flags;
 
1099
 
 
1100
    if ((flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
 
1101
        flags & PLUGIN_VAR_SessionLOCAL && flags & PLUGIN_VAR_MEMALLOC)
 
1102
    {
 
1103
      char **ptr= (char**) pivar->real_value_ptr(session, OPT_SESSION);
 
1104
      free(*ptr);
 
1105
      *ptr= NULL;
 
1106
    }
 
1107
  }
 
1108
 
543
1109
  assert(vars->storage_engine == NULL);
544
1110
 
545
1111
  free(vars->dynamic_variables_ptr);
552
1118
void plugin_sessionvar_cleanup(Session *session)
553
1119
{
554
1120
  unlock_variables(session, &session->variables);
555
 
  cleanup_variables(&session->variables);
556
 
}
557
 
 
558
 
 
 
1121
  cleanup_variables(session, &session->variables);
 
1122
}
 
1123
 
 
1124
 
 
1125
/**
 
1126
  @brief Free values of thread variables of a plugin.
 
1127
 
 
1128
  This must be called before a plugin is deleted. Otherwise its
 
1129
  variables are no longer accessible and the value space is lost. Note
 
1130
  that only string values with PLUGIN_VAR_MEMALLOC are allocated and
 
1131
  must be freed.
 
1132
 
 
1133
  @param[in]        vars        Chain of system variables of a plugin
 
1134
*/
 
1135
 
 
1136
static void plugin_vars_free_values(sys_var *vars)
 
1137
{
 
1138
 
 
1139
  for (sys_var *var= vars; var; var= var->getNext())
 
1140
  {
 
1141
    sys_var_pluginvar *piv= var->cast_pluginvar();
 
1142
    if (piv &&
 
1143
        ((piv->plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR) &&
 
1144
        (piv->plugin_var->flags & PLUGIN_VAR_MEMALLOC))
 
1145
    {
 
1146
      /* Free the string from global_system_variables. */
 
1147
      char **valptr= (char**) piv->real_value_ptr(NULL, OPT_GLOBAL);
 
1148
      free(*valptr);
 
1149
      *valptr= NULL;
 
1150
    }
 
1151
  }
 
1152
  return;
 
1153
}
 
1154
 
 
1155
 
 
1156
bool sys_var_pluginvar::check_update_type(Item_result type)
 
1157
{
 
1158
  if (is_readonly())
 
1159
    return 1;
 
1160
  switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
 
1161
  case PLUGIN_VAR_INT:
 
1162
  case PLUGIN_VAR_LONG:
 
1163
  case PLUGIN_VAR_LONGLONG:
 
1164
    return type != INT_RESULT;
 
1165
  case PLUGIN_VAR_STR:
 
1166
    return type != STRING_RESULT;
 
1167
  default:
 
1168
    return 0;
 
1169
  }
 
1170
}
 
1171
 
 
1172
 
 
1173
SHOW_TYPE sys_var_pluginvar::show_type()
 
1174
{
 
1175
  switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
 
1176
  case PLUGIN_VAR_BOOL:
 
1177
    return SHOW_MY_BOOL;
 
1178
  case PLUGIN_VAR_INT:
 
1179
    return SHOW_INT;
 
1180
  case PLUGIN_VAR_LONG:
 
1181
    return SHOW_LONG;
 
1182
  case PLUGIN_VAR_LONGLONG:
 
1183
    return SHOW_LONGLONG;
 
1184
  case PLUGIN_VAR_STR:
 
1185
    return SHOW_CHAR_PTR;
 
1186
  default:
 
1187
    assert(0);
 
1188
    return SHOW_UNDEF;
 
1189
  }
 
1190
}
 
1191
 
 
1192
 
 
1193
unsigned char* sys_var_pluginvar::real_value_ptr(Session *session, sql_var_t type)
 
1194
{
 
1195
  assert(session || (type == OPT_GLOBAL));
 
1196
  if (plugin_var->flags & PLUGIN_VAR_SessionLOCAL)
 
1197
  {
 
1198
    if (type == OPT_GLOBAL)
 
1199
      session= NULL;
 
1200
 
 
1201
    return intern_sys_var_ptr(session, *(int*) (plugin_var+1), false);
 
1202
  }
 
1203
  return *(unsigned char**) (plugin_var+1);
 
1204
}
 
1205
 
 
1206
 
 
1207
TYPELIB* sys_var_pluginvar::plugin_var_typelib(void)
 
1208
{
 
1209
  switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_SessionLOCAL)) {
 
1210
  case PLUGIN_VAR_SessionLOCAL:
 
1211
    return ((sessionvar_enum_t *)plugin_var)->typelib;
 
1212
  default:
 
1213
    return NULL;
 
1214
  }
 
1215
  return NULL;
 
1216
}
 
1217
 
 
1218
 
 
1219
unsigned char* sys_var_pluginvar::value_ptr(Session *session, sql_var_t type, const LEX_STRING *)
 
1220
{
 
1221
  unsigned char* result;
 
1222
 
 
1223
  result= real_value_ptr(session, type);
 
1224
 
 
1225
  return result;
 
1226
}
 
1227
 
 
1228
 
 
1229
bool sys_var_pluginvar::check(Session *session, set_var *var)
 
1230
{
 
1231
  st_item_value_holder value;
 
1232
  assert(is_readonly() || plugin_var->check);
 
1233
 
 
1234
  value.value_type= item_value_type;
 
1235
  value.val_str= item_val_str;
 
1236
  value.val_int= item_val_int;
 
1237
  value.val_real= item_val_real;
 
1238
  value.item= var->value;
 
1239
 
 
1240
  return is_readonly() ||
 
1241
         plugin_var->check(session, plugin_var, &var->save_result, &value);
 
1242
}
 
1243
 
 
1244
 
 
1245
void sys_var_pluginvar::set_default(Session *session, sql_var_t type)
 
1246
{
 
1247
  const void *src;
 
1248
  void *tgt;
 
1249
 
 
1250
  assert(is_readonly() || plugin_var->update);
 
1251
 
 
1252
  if (is_readonly())
 
1253
    return;
 
1254
 
 
1255
  LOCK_global_system_variables.lock();
 
1256
  tgt= real_value_ptr(session, type);
 
1257
  src= ((void **) (plugin_var + 1) + 1);
 
1258
 
 
1259
  if (plugin_var->flags & PLUGIN_VAR_SessionLOCAL)
 
1260
  {
 
1261
    if (type != OPT_GLOBAL)
 
1262
      src= real_value_ptr(session, OPT_GLOBAL);
 
1263
    else
 
1264
    switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
 
1265
        case PLUGIN_VAR_INT:
 
1266
          src= &((sessionvar_uint_t*) plugin_var)->def_val;
 
1267
          break;
 
1268
        case PLUGIN_VAR_LONG:
 
1269
          src= &((sessionvar_ulong_t*) plugin_var)->def_val;
 
1270
          break;
 
1271
        case PLUGIN_VAR_LONGLONG:
 
1272
          src= &((sessionvar_uint64_t_t*) plugin_var)->def_val;
 
1273
          break;
 
1274
        case PLUGIN_VAR_BOOL:
 
1275
          src= &((sessionvar_bool_t*) plugin_var)->def_val;
 
1276
          break;
 
1277
        case PLUGIN_VAR_STR:
 
1278
          src= &((sessionvar_str_t*) plugin_var)->def_val;
 
1279
          break;
 
1280
        default:
 
1281
          assert(0);
 
1282
        }
 
1283
  }
 
1284
 
 
1285
  /* session must equal current_session if PLUGIN_VAR_SessionLOCAL flag is set */
 
1286
  assert(!(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) ||
 
1287
              session == current_session);
 
1288
 
 
1289
  if (!(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) || type == OPT_GLOBAL)
 
1290
  {
 
1291
    plugin_var->update(session, plugin_var, tgt, src);
 
1292
    LOCK_global_system_variables.unlock();
 
1293
  }
 
1294
  else
 
1295
  {
 
1296
    LOCK_global_system_variables.unlock();
 
1297
    plugin_var->update(session, plugin_var, tgt, src);
 
1298
  }
 
1299
}
 
1300
 
 
1301
 
 
1302
bool sys_var_pluginvar::update(Session *session, set_var *var)
 
1303
{
 
1304
  void *tgt;
 
1305
 
 
1306
  assert(is_readonly() || plugin_var->update);
 
1307
 
 
1308
  /* session must equal current_session if PLUGIN_VAR_SessionLOCAL flag is set */
 
1309
  assert(!(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) ||
 
1310
              session == current_session);
 
1311
 
 
1312
  if (is_readonly())
 
1313
    return 1;
 
1314
 
 
1315
  LOCK_global_system_variables.lock();
 
1316
  tgt= real_value_ptr(session, var->type);
 
1317
 
 
1318
  if (!(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) || var->type == OPT_GLOBAL)
 
1319
  {
 
1320
    /* variable we are updating has global scope, so we unlock after updating */
 
1321
    plugin_var->update(session, plugin_var, tgt, &var->save_result);
 
1322
    LOCK_global_system_variables.unlock();
 
1323
  }
 
1324
  else
 
1325
  {
 
1326
    LOCK_global_system_variables.unlock();
 
1327
    plugin_var->update(session, plugin_var, tgt, &var->save_result);
 
1328
  }
 
1329
 return 0;
 
1330
}
 
1331
 
 
1332
 
 
1333
#define OPTION_SET_LIMITS(type, options, opt) \
 
1334
  options->var_type= type;                    \
 
1335
  options->def_value= (opt)->def_val;         \
 
1336
  options->min_value= (opt)->min_val;         \
 
1337
  options->max_value= (opt)->max_val;         \
 
1338
  options->block_size= (long) (opt)->blk_sz
 
1339
 
 
1340
 
 
1341
void plugin_opt_set_limits(struct option *options,
 
1342
                                                                                                         const drizzle_sys_var *opt)
 
1343
{
 
1344
  options->sub_size= 0;
 
1345
 
 
1346
  switch (opt->flags & (PLUGIN_VAR_TYPEMASK |
 
1347
                        PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_SessionLOCAL)) {
 
1348
  /* global system variables */
 
1349
  case PLUGIN_VAR_INT:
 
1350
    OPTION_SET_LIMITS(GET_INT, options, (sysvar_int_t*) opt);
 
1351
    break;
 
1352
  case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED:
 
1353
    OPTION_SET_LIMITS(GET_UINT, options, (sysvar_uint_t*) opt);
 
1354
    break;
 
1355
  case PLUGIN_VAR_LONG:
 
1356
    OPTION_SET_LIMITS(GET_LONG, options, (sysvar_long_t*) opt);
 
1357
    break;
 
1358
  case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED:
 
1359
    OPTION_SET_LIMITS(GET_ULONG_IS_FAIL, options, (sysvar_ulong_t*) opt);
 
1360
    break;
 
1361
  case PLUGIN_VAR_LONGLONG:
 
1362
    OPTION_SET_LIMITS(GET_LL, options, (sysvar_int64_t_t*) opt);
 
1363
    break;
 
1364
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED:
 
1365
    OPTION_SET_LIMITS(GET_ULL, options, (sysvar_uint64_t_t*) opt);
 
1366
    break;
 
1367
  case PLUGIN_VAR_BOOL:
 
1368
    options->var_type= GET_BOOL;
 
1369
    options->def_value= ((sysvar_bool_t*) opt)->def_val;
 
1370
    break;
 
1371
  case PLUGIN_VAR_STR:
 
1372
    options->var_type= ((opt->flags & PLUGIN_VAR_MEMALLOC) ?
 
1373
                        GET_STR_ALLOC : GET_STR);
 
1374
    options->def_value= (intptr_t) ((sysvar_str_t*) opt)->def_val;
 
1375
    break;
 
1376
  /* threadlocal variables */
 
1377
  case PLUGIN_VAR_INT | PLUGIN_VAR_SessionLOCAL:
 
1378
    OPTION_SET_LIMITS(GET_INT, options, (sessionvar_int_t*) opt);
 
1379
    break;
 
1380
  case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_SessionLOCAL:
 
1381
    OPTION_SET_LIMITS(GET_UINT, options, (sessionvar_uint_t*) opt);
 
1382
    break;
 
1383
  case PLUGIN_VAR_LONG | PLUGIN_VAR_SessionLOCAL:
 
1384
    OPTION_SET_LIMITS(GET_LONG, options, (sessionvar_long_t*) opt);
 
1385
    break;
 
1386
  case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_SessionLOCAL:
 
1387
    OPTION_SET_LIMITS(GET_ULONG_IS_FAIL, options, (sessionvar_ulong_t*) opt);
 
1388
    break;
 
1389
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_SessionLOCAL:
 
1390
    OPTION_SET_LIMITS(GET_LL, options, (sessionvar_int64_t_t*) opt);
 
1391
    break;
 
1392
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_SessionLOCAL:
 
1393
    OPTION_SET_LIMITS(GET_ULL, options, (sessionvar_uint64_t_t*) opt);
 
1394
    break;
 
1395
  case PLUGIN_VAR_BOOL | PLUGIN_VAR_SessionLOCAL:
 
1396
    options->var_type= GET_BOOL;
 
1397
    options->def_value= ((sessionvar_bool_t*) opt)->def_val;
 
1398
    break;
 
1399
  case PLUGIN_VAR_STR | PLUGIN_VAR_SessionLOCAL:
 
1400
    options->var_type= ((opt->flags & PLUGIN_VAR_MEMALLOC) ?
 
1401
                        GET_STR_ALLOC : GET_STR);
 
1402
    options->def_value= (intptr_t) ((sessionvar_str_t*) opt)->def_val;
 
1403
    break;
 
1404
  default:
 
1405
    assert(0);
 
1406
  }
 
1407
  options->arg_type= REQUIRED_ARG;
 
1408
  if (opt->flags & PLUGIN_VAR_NOCMDARG)
 
1409
    options->arg_type= NO_ARG;
 
1410
  if (opt->flags & PLUGIN_VAR_OPCMDARG)
 
1411
    options->arg_type= OPT_ARG;
 
1412
}
 
1413
 
 
1414
static int construct_options(memory::Root *mem_root, module::Module *tmp,
 
1415
                             option *options)
 
1416
{
 
1417
  
 
1418
  int localoptionid= 256;
 
1419
  const string plugin_name(tmp->getManifest().name);
 
1420
 
 
1421
  size_t namelen= plugin_name.size(), optnamelen;
 
1422
 
 
1423
  char *optname, *p;
 
1424
  int index= 0, offset= 0;
 
1425
  drizzle_sys_var *opt, **plugin_option;
 
1426
  Bookmark *v;
 
1427
 
 
1428
  string name(plugin_name);
 
1429
  transform(name.begin(), name.end(), name.begin(), ::tolower);
 
1430
 
 
1431
  for (string::iterator iter= name.begin(); iter != name.end(); ++iter)
 
1432
  {
 
1433
    if (*iter == '_')
 
1434
      *iter= '-';
 
1435
  }
 
1436
 
 
1437
  /*
 
1438
    Two passes as the 2nd pass will take pointer addresses for use
 
1439
    by my_getopt and register_var() in the first pass uses realloc
 
1440
  */
 
1441
 
 
1442
  for (plugin_option= tmp->getManifest().system_vars;
 
1443
       plugin_option && *plugin_option; plugin_option++, index++)
 
1444
  {
 
1445
    opt= *plugin_option;
 
1446
    if (!(opt->flags & PLUGIN_VAR_SessionLOCAL))
 
1447
      continue;
 
1448
    if (!(register_var(name, opt->name, opt->flags)))
 
1449
      continue;
 
1450
    switch (opt->flags & PLUGIN_VAR_TYPEMASK) {
 
1451
    case PLUGIN_VAR_BOOL:
 
1452
      (((sessionvar_bool_t *)opt)->resolve)= mysql_sys_var_ptr_bool;
 
1453
      break;
 
1454
    case PLUGIN_VAR_INT:
 
1455
      (((sessionvar_int_t *)opt)->resolve)= mysql_sys_var_ptr_int;
 
1456
      break;
 
1457
    case PLUGIN_VAR_LONG:
 
1458
      (((sessionvar_long_t *)opt)->resolve)= mysql_sys_var_ptr_long;
 
1459
      break;
 
1460
    case PLUGIN_VAR_LONGLONG:
 
1461
      (((sessionvar_int64_t_t *)opt)->resolve)= mysql_sys_var_ptr_int64_t;
 
1462
      break;
 
1463
    case PLUGIN_VAR_STR:
 
1464
      (((sessionvar_str_t *)opt)->resolve)= mysql_sys_var_ptr_str;
 
1465
      break;
 
1466
    default:
 
1467
      errmsg_printf(ERRMSG_LVL_ERROR, _("Unknown variable type code 0x%x in plugin '%s'."),
 
1468
                      opt->flags, plugin_name.c_str());
 
1469
      return(-1);
 
1470
    };
 
1471
  }
 
1472
 
 
1473
  for (plugin_option= tmp->getManifest().system_vars;
 
1474
       plugin_option && *plugin_option; plugin_option++, index++)
 
1475
  {
 
1476
    switch ((opt= *plugin_option)->flags & PLUGIN_VAR_TYPEMASK) {
 
1477
    case PLUGIN_VAR_BOOL:
 
1478
      if (!opt->check)
 
1479
        opt->check= check_func_bool;
 
1480
      if (!opt->update)
 
1481
        opt->update= update_func_bool;
 
1482
      break;
 
1483
    case PLUGIN_VAR_INT:
 
1484
      if (!opt->check)
 
1485
        opt->check= check_func_int;
 
1486
      if (!opt->update)
 
1487
        opt->update= update_func_int;
 
1488
      break;
 
1489
    case PLUGIN_VAR_LONG:
 
1490
      if (!opt->check)
 
1491
        opt->check= check_func_long;
 
1492
      if (!opt->update)
 
1493
        opt->update= update_func_long;
 
1494
      break;
 
1495
    case PLUGIN_VAR_LONGLONG:
 
1496
      if (!opt->check)
 
1497
        opt->check= check_func_int64_t;
 
1498
      if (!opt->update)
 
1499
        opt->update= update_func_int64_t;
 
1500
      break;
 
1501
    case PLUGIN_VAR_STR:
 
1502
      if (!opt->check)
 
1503
        opt->check= check_func_str;
 
1504
      if (!opt->update)
 
1505
      {
 
1506
        opt->update= update_func_str;
 
1507
        if ((opt->flags & (PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_READONLY)) == false)
 
1508
        {
 
1509
          opt->flags|= PLUGIN_VAR_READONLY;
 
1510
          errmsg_printf(ERRMSG_LVL_WARN, _("Server variable %s of plugin %s was forced "
 
1511
                            "to be read-only: string variable without "
 
1512
                            "update_func and PLUGIN_VAR_MEMALLOC flag"),
 
1513
                            opt->name, plugin_name.c_str());
 
1514
        }
 
1515
      }
 
1516
      break;
 
1517
    default:
 
1518
      errmsg_printf(ERRMSG_LVL_ERROR, _("Unknown variable type code 0x%x in plugin '%s'."),
 
1519
                      opt->flags, plugin_name.c_str());
 
1520
      return(-1);
 
1521
    }
 
1522
 
 
1523
    if ((opt->flags & (PLUGIN_VAR_NOCMDOPT | PLUGIN_VAR_SessionLOCAL))
 
1524
                    == PLUGIN_VAR_NOCMDOPT)
 
1525
      continue;
 
1526
 
 
1527
    if (!opt->name)
 
1528
    {
 
1529
      errmsg_printf(ERRMSG_LVL_ERROR, _("Missing variable name in plugin '%s'."),
 
1530
                    plugin_name.c_str());
 
1531
      return(-1);
 
1532
    }
 
1533
 
 
1534
    if (!(opt->flags & PLUGIN_VAR_SessionLOCAL))
 
1535
    {
 
1536
      optnamelen= strlen(opt->name);
 
1537
      optname= (char*) mem_root->alloc_root(namelen + optnamelen + 2);
 
1538
      sprintf(optname, "%s-%s", name.c_str(), opt->name);
 
1539
      optnamelen= namelen + optnamelen + 1;
 
1540
    }
 
1541
    else
 
1542
    {
 
1543
      /* this should not fail because register_var should create entry */
 
1544
      if (!(v= find_bookmark(name, opt->name, opt->flags)))
 
1545
      {
 
1546
        errmsg_printf(ERRMSG_LVL_ERROR, _("Thread local variable '%s' not allocated "
 
1547
                      "in plugin '%s'."), opt->name, plugin_name.c_str());
 
1548
        return(-1);
 
1549
      }
 
1550
 
 
1551
      *(int*)(opt + 1)= offset= v->offset;
 
1552
 
 
1553
      if (opt->flags & PLUGIN_VAR_NOCMDOPT)
 
1554
        continue;
 
1555
 
 
1556
      optname= (char*) mem_root->memdup_root(v->key.c_str(), (optnamelen= v->key.size()) + 1);
 
1557
    }
 
1558
 
 
1559
    /* convert '_' to '-' */
 
1560
    for (p= optname; *p; p++)
 
1561
      if (*p == '_')
 
1562
        *p= '-';
 
1563
 
 
1564
    options->name= optname;
 
1565
    options->comment= opt->comment;
 
1566
    options->app_type= opt;
 
1567
    options->id= localoptionid++;
 
1568
 
 
1569
    plugin_opt_set_limits(options, opt);
 
1570
 
 
1571
    if (opt->flags & PLUGIN_VAR_SessionLOCAL)
 
1572
      options->value= options->u_max_value= (char**)
 
1573
        (global_system_variables.dynamic_variables_ptr + offset);
 
1574
    else
 
1575
      options->value= options->u_max_value= *(char***) (opt + 1);
 
1576
 
 
1577
    options++;
 
1578
  }
 
1579
 
 
1580
  return(0);
 
1581
}
 
1582
 
 
1583
 
 
1584
static option *construct_help_options(memory::Root *mem_root, module::Module *p)
 
1585
{
 
1586
  drizzle_sys_var **opt;
 
1587
  option *opts;
 
1588
  uint32_t count= EXTRA_OPTIONS;
 
1589
 
 
1590
  for (opt= p->getManifest().system_vars; opt && *opt; opt++, count++) {};
 
1591
 
 
1592
  opts= (option*)mem_root->alloc_root((sizeof(option) * count));
 
1593
  if (opts == NULL)
 
1594
    return NULL;
 
1595
 
 
1596
  memset(opts, 0, sizeof(option) * count);
 
1597
 
 
1598
  if (construct_options(mem_root, p, opts))
 
1599
    return NULL;
 
1600
 
 
1601
  return(opts);
 
1602
}
 
1603
 
 
1604
void drizzle_add_plugin_sysvar(sys_var_pluginvar *var)
 
1605
{
 
1606
  plugin_sysvar_vec.push_back(var);
 
1607
}
 
1608
 
 
1609
void drizzle_del_plugin_sysvar()
 
1610
{
 
1611
  vector<sys_var_pluginvar *>::iterator iter= plugin_sysvar_vec.begin();
 
1612
  while(iter != plugin_sysvar_vec.end())
 
1613
  {
 
1614
    delete *iter;
 
1615
    ++iter;
 
1616
  }
 
1617
  plugin_sysvar_vec.clear();
 
1618
}
559
1619
 
560
1620
/*
561
1621
  SYNOPSIS
568
1628
  NOTE:
569
1629
    Requires that a write-lock is held on LOCK_system_variables_hash
570
1630
*/
571
 
static int test_plugin_options(memory::Root *,
 
1631
static int test_plugin_options(memory::Root *module_root,
572
1632
                               module::Module *test_module,
573
1633
                               po::options_description &long_options)
574
1634
{
 
1635
  struct sys_var_chain chain= { NULL, NULL };
 
1636
  drizzle_sys_var **opt;
 
1637
  option *opts= NULL;
 
1638
  int error;
 
1639
  drizzle_sys_var *o;
 
1640
  Bookmark *var;
 
1641
  uint32_t len, count= EXTRA_OPTIONS;
575
1642
 
576
1643
  if (test_module->getManifest().init_options != NULL)
577
1644
  {
582
1649
                                   module_options.add_options());
583
1650
    test_module->getManifest().init_options(opt_ctx);
584
1651
    long_options.add(module_options);
585
 
  }
586
 
 
587
 
  return 0;
 
1652
 
 
1653
  }
 
1654
 
 
1655
  for (opt= test_module->getManifest().system_vars; opt && *opt; opt++)
 
1656
  {
 
1657
    count++;
 
1658
  }
 
1659
 
 
1660
  if (count > EXTRA_OPTIONS)
 
1661
  {
 
1662
    if (!(opts= (option*) module_root->alloc_root(sizeof(option) * count)))
 
1663
    {
 
1664
      errmsg_printf(ERRMSG_LVL_ERROR,
 
1665
                    _("Out of memory for plugin '%s'."),
 
1666
                    test_module->getName().c_str());
 
1667
      return(-1);
 
1668
    }
 
1669
    memset(opts, 0, sizeof(option) * count);
 
1670
 
 
1671
    if (construct_options(module_root, test_module, opts))
 
1672
    {
 
1673
      errmsg_printf(ERRMSG_LVL_ERROR,
 
1674
                    _("Bad options for plugin '%s'."),
 
1675
                    test_module->getName().c_str());
 
1676
      return(-1);
 
1677
    }
 
1678
 
 
1679
  }
 
1680
 
 
1681
  error= 1;
 
1682
 
 
1683
  {
 
1684
    for (opt= test_module->getManifest().system_vars; opt && *opt; opt++)
 
1685
    {
 
1686
      sys_var *v;
 
1687
      if (((o= *opt)->flags & PLUGIN_VAR_NOSYSVAR))
 
1688
        continue;
 
1689
 
 
1690
      if ((var= find_bookmark(test_module->getName(), o->name, o->flags)))
 
1691
      {
 
1692
        v= new sys_var_pluginvar(var->key.c_str(), o);
 
1693
      }
 
1694
      else
 
1695
      {
 
1696
        len= test_module->getName().length() + strlen(o->name) + 2;
 
1697
        string vname(test_module->getName());
 
1698
        vname.push_back('-');
 
1699
        vname.append(o->name);
 
1700
        transform(vname.begin(), vname.end(), vname.begin(), ::tolower);
 
1701
        string::iterator p= vname.begin();      
 
1702
        while  (p != vname.end())
 
1703
        {
 
1704
          if (*p == '-')
 
1705
            *p= '_';
 
1706
          ++p;
 
1707
        }
 
1708
 
 
1709
        v= new sys_var_pluginvar(vname, o);
 
1710
      }
 
1711
      assert(v); /* check that an object was actually constructed */
 
1712
 
 
1713
      drizzle_add_plugin_sysvar(static_cast<sys_var_pluginvar *>(v));
 
1714
      /*
 
1715
        Add to the chain of variables.
 
1716
        Done like this for easier debugging so that the
 
1717
        pointer to v is not lost on optimized builds.
 
1718
      */
 
1719
      v->chain_sys_var(&chain);
 
1720
    }
 
1721
    if (chain.first)
 
1722
    {
 
1723
      chain.last->setNext(NULL);
 
1724
      if (mysql_add_sys_var_chain(chain.first, NULL))
 
1725
      {
 
1726
        errmsg_printf(ERRMSG_LVL_ERROR,
 
1727
                      _("Plugin '%s' has conflicting system variables"),
 
1728
                      test_module->getName().c_str());
 
1729
        goto err;
 
1730
      }
 
1731
      test_module->system_vars= chain.first;
 
1732
    }
 
1733
    return(0);
 
1734
  }
 
1735
 
 
1736
err:
 
1737
  if (opts)
 
1738
    my_cleanup_options(opts);
 
1739
  return(error);
588
1740
}
589
1741
 
590
1742
 
607
1759
  module::Registry &registry= module::Registry::singleton();
608
1760
  vector<option> all_options;
609
1761
  memory::Root mem_root(4096);
 
1762
  option *opt= NULL;
610
1763
 
611
1764
 
612
1765
  if (initialized)
624
1777
      if (p->getManifest().init_options != NULL)
625
1778
        continue;
626
1779
 
 
1780
      if (p->getManifest().system_vars == NULL)
 
1781
        continue;
 
1782
 
 
1783
      opt= construct_help_options(&mem_root, p);
 
1784
      if (opt == NULL)
 
1785
        continue;
 
1786
 
 
1787
      /* Only options with a non-NULL comment are displayed in help text */
 
1788
      for (;opt->id; opt++)
 
1789
      {
 
1790
        if (opt->comment)
 
1791
        {
 
1792
          all_options.push_back(*opt);
 
1793
          
 
1794
        }
 
1795
      }
627
1796
    }
628
1797
  }
629
1798