~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/loader.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:30:27 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103033027-lskb6gxwwforfz71
fix docs warning: underline/overline too short for replace.rst

Show diffs side-by-side

added added

removed removed

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