~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/loader.cc

  • Committer: Andrew Hutchings
  • Date: 2010-10-20 15:31:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1907.
  • Revision ID: andrew@linuxjedi.co.uk-20101020153127-w9djuz9omzezg2kz
Add error message for global sort buffer constraint

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