~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2005 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
16
#include "config.h"
499.2.9 by Mark Atwood
fix mistakes
17
1122.2.13 by Monty Taylor
Header cleanup.
18
#include <dlfcn.h>
499.2.9 by Mark Atwood
fix mistakes
19
1502.3.1 by iwamatsu at nigauri
Add cstdio include to files needing it. Fixes the build on some debian
20
#include <cstdio>
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
21
#include <string>
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
22
#include <vector>
873.2.21 by Monty Taylor
Removed the HASH for plugin_hash.
23
#include <map>
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
24
#include <algorithm>
1625.1.7 by Monty Taylor
Added support for program_options based commandline argument passing.
25
#include <iostream>
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
26
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
27
#include <boost/program_options.hpp>
28
1410.3.4 by Djellel E. Difallah
update references to old my_'s
29
#include "drizzled/option.h"
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
30
#include "drizzled/my_hash.h"
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
31
#include "drizzled/internal/m_string.h"
1122.2.13 by Monty Taylor
Header cleanup.
32
1251.2.2 by Jay Pipes
Pulls MyISAM-specific server variables into the MyISAM
33
#include "drizzled/plugin.h"
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
34
#include "drizzled/module/load_list.h"
35
#include "drizzled/module/library.h"
36
#include "drizzled/module/registry.h"
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
37
#include "drizzled/module/option_context.h"
1122.2.13 by Monty Taylor
Header cleanup.
38
#include "drizzled/sql_parse.h"
39
#include "drizzled/show.h"
1185 by Brian Aker
Merge Engine changes.
40
#include "drizzled/cursor.h"
1122.2.13 by Monty Taylor
Header cleanup.
41
#include "drizzled/set_var.h"
42
#include "drizzled/session.h"
43
#include "drizzled/item/null.h"
44
#include "drizzled/error.h"
45
#include "drizzled/gettext.h"
1130.3.23 by Monty Taylor
Removed more server_includes.h from headers.
46
#include "drizzled/errmsg_print.h"
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
47
#include "drizzled/strfunc.h"
1241.9.31 by Monty Taylor
Moved global pthread variables into their own header.
48
#include "drizzled/pthread_globals.h"
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
49
#include "drizzled/util/tokenize.h"
1122.2.13 by Monty Taylor
Header cleanup.
50
51
/* FreeBSD 2.2.2 does not define RTLD_NOW) */
52
#ifndef RTLD_NOW
53
#define RTLD_NOW 1
54
#endif
1 by brian
clean slate
55
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
56
namespace po=boost::program_options;
57
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
58
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
59
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
60
/** These exist just to prevent symbols from being optimized out */
61
typedef drizzled::module::Manifest drizzled_builtin_list[];
62
extern drizzled_builtin_list PANDORA_BUILTIN_SYMBOLS_LIST;
63
drizzled::module::Manifest *drizzled_builtins[]=
64
{
65
  PANDORA_BUILTIN_SYMBOLS_LIST, NULL
66
};
67
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
68
namespace drizzled
69
{
992.1.39 by Monty Taylor
Removed the m4-based plugin system.
70
 
1122.2.13 by Monty Taylor
Header cleanup.
71
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
72
class sys_var_pluginvar;
73
static vector<sys_var_pluginvar *> plugin_sysvar_vec;
1 by brian
clean slate
74
1192.3.7 by Monty Taylor
Added code necessary for building plugins dynamically.
75
char *opt_plugin_add= NULL;
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
76
char *opt_plugin_remove= NULL;
1 by brian
clean slate
77
char *opt_plugin_load= NULL;
78
char *opt_plugin_dir_ptr;
79
char opt_plugin_dir[FN_REFLEN];
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
80
const char *opt_plugin_load_default= PANDORA_PLUGIN_LIST;
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
81
const char *builtin_plugins= PANDORA_BUILTIN_LIST;
1 by brian
clean slate
82
83
/* Note that 'int version' must be the first field of every plugin
84
   sub-structure (plugin->info).
85
*/
86
962.1.6 by Brian Aker
Cut down on shutdown loop of plugins (cutting stuff out in order to simplify
87
static bool initialized= false;
1 by brian
clean slate
88
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
89
1 by brian
clean slate
90
static bool reap_needed= false;
91
92
/*
93
  write-lock on LOCK_system_variables_hash is required before modifying
94
  the following variables/structures
95
*/
1485 by Brian Aker
Updates to confine memroot
96
static memory::Root plugin_mem_root(4096);
482 by Brian Aker
Remove uint.
97
static uint32_t global_variables_dynamic_size= 0;
1 by brian
clean slate
98
static HASH bookmark_hash;
99
100
101
/*
102
  hidden part of opaque value passed to variable check functions.
103
  Used to provide a object-like structure to non C++ consumers.
104
*/
1228.1.5 by Monty Taylor
Merged in some naming things.
105
struct st_item_value_holder : public drizzle_value
1 by brian
clean slate
106
{
107
  Item *item;
108
};
109
110
111
/*
112
  stored in bookmark_hash, this structure is never removed from the
520.1.22 by Brian Aker
Second pass of thd cleanup
113
  hash and is used to mark a single offset for a session local variable
1 by brian
clean slate
114
  even if plugins have been uninstalled and reinstalled, repeatedly.
115
  This structure is allocated from plugin_mem_root.
116
117
  The key format is as follows:
118
    1 byte         - variable type code
119
    name_len bytes - variable name
120
    '\0'           - end of key
121
*/
122
struct st_bookmark
123
{
482 by Brian Aker
Remove uint.
124
  uint32_t name_len;
1 by brian
clean slate
125
  int offset;
482 by Brian Aker
Remove uint.
126
  uint32_t version;
1 by brian
clean slate
127
  char key[1];
128
};
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:
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
137
  module::Module *plugin;
1228.1.5 by Monty Taylor
Merged in some naming things.
138
  drizzle_sys_var *plugin_var;
1 by brian
clean slate
139
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
140
  sys_var_pluginvar(const std::string name_arg,
1228.1.5 by Monty Taylor
Merged in some naming things.
141
                    drizzle_sys_var *plugin_var_arg)
1 by brian
clean slate
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; }
1273.13.24 by Brian Aker
Updating style, simplified code.
145
  bool check_type(sql_var_t type)
520.1.21 by Brian Aker
THD -> Session rename
146
  { return !(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) && type != OPT_GLOBAL; }
1 by brian
clean slate
147
  bool check_update_type(Item_result type);
148
  SHOW_TYPE show_type();
1273.13.24 by Brian Aker
Updating style, simplified code.
149
  unsigned char* real_value_ptr(Session *session, sql_var_t type);
1 by brian
clean slate
150
  TYPELIB* plugin_var_typelib(void);
1273.13.24 by Brian Aker
Updating style, simplified code.
151
  unsigned char* value_ptr(Session *session, sql_var_t type,
779.3.10 by Monty Taylor
Turned on -Wshadow.
152
                           const LEX_STRING *base);
520.1.22 by Brian Aker
Second pass of thd cleanup
153
  bool check(Session *session, set_var *var);
1273.13.24 by Brian Aker
Updating style, simplified code.
154
  bool check_default(sql_var_t)
77.1.46 by Monty Taylor
Finished the warnings work!
155
    { return is_readonly(); }
1273.13.24 by Brian Aker
Updating style, simplified code.
156
  void set_default(Session *session, sql_var_t);
520.1.22 by Brian Aker
Second pass of thd cleanup
157
  bool update(Session *session, set_var *var);
1 by brian
clean slate
158
};
159
160
161
/* prototypes */
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
162
static void plugin_prune_list(vector<string> &plugin_list,
163
                              const vector<string> &plugins_to_remove);
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
164
static bool plugin_load_list(module::Registry &registry,
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
165
                             memory::Root *tmp_root, int *argc, char **argv,
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
166
                             const set<string> &plugin_list,
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
167
                             po::options_description &long_options,
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
168
                             bool builtin= false);
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
169
static int test_plugin_options(memory::Root *, module::Module *,
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
170
                               int *, char **,
171
                               po::options_description &long_options);
520.1.22 by Brian Aker
Second pass of thd cleanup
172
static void unlock_variables(Session *session, struct system_variables *vars);
173
static void cleanup_variables(Session *session, struct system_variables *vars);
1 by brian
clean slate
174
static void plugin_vars_free_values(sys_var *vars);
175
176
/* declared in set_var.cc */
482 by Brian Aker
Remove uint.
177
extern sys_var *intern_find_sys_var(const char *str, uint32_t length, bool no_error);
520.1.22 by Brian Aker
Second pass of thd cleanup
178
extern bool throw_bounds_warning(Session *session, bool fixed, bool unsignd,
1022.2.38 by Monty Taylor
Changed name to std::string.
179
                                 const std::string &name, int64_t val);
180
181
static bool throw_bounds_warning(Session *session, bool fixed, bool unsignd,
182
                                 const char *name, int64_t val)
183
{
184
  const std::string name_str(name);
185
  return throw_bounds_warning(session, fixed, unsignd, name_str, val);
186
}
1 by brian
clean slate
187
188
/****************************************************************************
189
  Value type thunks, allows the C world to play in the C++ world
190
****************************************************************************/
191
1228.1.5 by Monty Taylor
Merged in some naming things.
192
static int item_value_type(drizzle_value *value)
1 by brian
clean slate
193
{
194
  switch (((st_item_value_holder*)value)->item->result_type()) {
195
  case INT_RESULT:
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
196
    return DRIZZLE_VALUE_TYPE_INT;
1 by brian
clean slate
197
  case REAL_RESULT:
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
198
    return DRIZZLE_VALUE_TYPE_REAL;
1 by brian
clean slate
199
  default:
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
200
    return DRIZZLE_VALUE_TYPE_STRING;
1 by brian
clean slate
201
  }
202
}
203
1228.1.5 by Monty Taylor
Merged in some naming things.
204
static const char *item_val_str(drizzle_value *value,
1 by brian
clean slate
205
                                char *buffer, int *length)
206
{
207
  String str(buffer, *length, system_charset_info), *res;
208
  if (!(res= ((st_item_value_holder*)value)->item->val_str(&str)))
209
    return NULL;
210
  *length= res->length();
211
  if (res->c_ptr_quick() == buffer)
212
    return buffer;
213
214
  /*
215
    Lets be nice and create a temporary string since the
216
    buffer was too small
217
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
218
  return current_session->strmake(res->c_ptr_quick(), res->length());
1 by brian
clean slate
219
}
220
221
1228.1.5 by Monty Taylor
Merged in some naming things.
222
static int item_val_int(drizzle_value *value, int64_t *buf)
1 by brian
clean slate
223
{
224
  Item *item= ((st_item_value_holder*)value)->item;
225
  *buf= item->val_int();
226
  if (item->is_null())
227
    return 1;
228
  return 0;
229
}
230
231
1228.1.5 by Monty Taylor
Merged in some naming things.
232
static int item_val_real(drizzle_value *value, double *buf)
1 by brian
clean slate
233
{
234
  Item *item= ((st_item_value_holder*)value)->item;
235
  *buf= item->val_real();
236
  if (item->is_null())
237
    return 1;
238
  return 0;
239
}
240
241
242
/****************************************************************************
243
  Plugin support code
244
****************************************************************************/
245
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
246
1 by brian
clean slate
247
248
249
/*
250
  NOTE
251
    Requires that a write-lock is held on LOCK_system_variables_hash
252
*/
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
253
static bool plugin_add(module::Registry &registry, memory::Root *tmp_root,
254
                       module::Library *library,
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
255
                       int *argc, char **argv,
256
                       po::options_description &long_options)
1 by brian
clean slate
257
{
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
258
  if (! initialized)
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
259
    return true;
908.2.1 by Monty Taylor
First pass at refactoring plugins - factored out sql_map.
260
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
261
  if (registry.find(library->getName()))
1 by brian
clean slate
262
  {
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
263
    errmsg_printf(ERRMSG_LVL_WARN, ER(ER_PLUGIN_EXISTS),
264
                  library->getName().c_str());
265
    return false;
1 by brian
clean slate
266
  }
1093.3.4 by Monty Taylor
Naming cleanups.
267
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
268
  module::Module *tmp= NULL;
1 by brian
clean slate
269
  /* Find plugin by name */
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
270
  const module::Manifest *manifest= library->getManifest();
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
271
1284.2.1 by Monty Taylor
Add a fatal error if a plugin has a name which has already been registered.
272
  if (registry.find(manifest->name))
273
  {
274
    errmsg_printf(ERRMSG_LVL_ERROR, 
275
                  _("Plugin '%s' contains the name '%s' in its manifest, which "
276
                    "has already been registered.\n"),
277
                  library->getName().c_str(),
278
                  manifest->name);
279
    return true;
280
  }
281
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
282
  tmp= new (std::nothrow) module::Module(manifest, library);
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
283
  if (tmp == NULL)
284
    return true;
285
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
286
  if (!test_plugin_options(tmp_root, tmp, argc, argv, long_options))
1 by brian
clean slate
287
  {
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
288
    registry.add(tmp);
289
    return false;
1 by brian
clean slate
290
  }
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
291
  errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_FIND_DL_ENTRY),
292
                library->getName().c_str());
293
  return true;
1 by brian
clean slate
294
}
295
296
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
297
static void delete_module(module::Module *module)
1 by brian
clean slate
298
{
299
  /* Free allocated strings before deleting the plugin. */
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
300
  plugin_vars_free_values(module->system_vars);
301
  module->isInited= false;
658 by Brian Aker
Part removal of my_pthread.h
302
  pthread_rwlock_wrlock(&LOCK_system_variables_hash);
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
303
  mysql_del_sys_var_chain(module->system_vars);
658 by Brian Aker
Part removal of my_pthread.h
304
  pthread_rwlock_unlock(&LOCK_system_variables_hash);
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
305
  delete module;
1 by brian
clean slate
306
}
307
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
308
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
309
static void reap_plugins(module::Registry &registry)
1 by brian
clean slate
310
{
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
311
  std::map<std::string, module::Module *>::const_iterator modules=
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
312
    registry.getModulesMap().begin();
1324.2.3 by Monty Taylor
Remove plugin deinit.
313
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
314
  while (modules != registry.getModulesMap().end())
1 by brian
clean slate
315
  {
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
316
    module::Module *module= (*modules).second;
1324.2.3 by Monty Taylor
Remove plugin deinit.
317
    delete_module(module);
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
318
    ++modules;
688 by Brian Aker
Second pass through memory allocation issues (this solves the ones around
319
  }
1324.2.3 by Monty Taylor
Remove plugin deinit.
320
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
321
  drizzle_del_plugin_sysvar();
1 by brian
clean slate
322
}
323
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
324
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
325
static void plugin_initialize_vars(module::Module *module)
1 by brian
clean slate
326
{
327
  /*
328
    set the plugin attribute of plugin's sys vars so they are pointing
329
    to the active plugin
330
  */
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
331
  if (module->system_vars)
1 by brian
clean slate
332
  {
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
333
    sys_var_pluginvar *var= module->system_vars->cast_pluginvar();
1 by brian
clean slate
334
    for (;;)
335
    {
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
336
      var->plugin= module;
1055.2.20 by Jay Pipes
Refactors sys_var class -- doxygenates and documents the class members and functions. Protects all member variables and adds public getters/setters.
337
      if (! var->getNext())
1 by brian
clean slate
338
        break;
1055.2.20 by Jay Pipes
Refactors sys_var class -- doxygenates and documents the class members and functions. Protects all member variables and adds public getters/setters.
339
      var= var->getNext()->cast_pluginvar();
1 by brian
clean slate
340
    }
341
  }
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
342
}
343
344
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
345
static bool plugin_initialize(module::Registry &registry,
346
                              module::Module *module)
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
347
{
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
348
  assert(module->isInited == false);
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
349
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
350
  module::Context loading_context(registry, module);
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
351
  if (module->getManifest().init)
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
352
  {
1324.2.1 by Monty Taylor
Create a plugin::Context object to carry information about the plugin module
353
    if (module->getManifest().init(loading_context))
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
354
    {
355
      errmsg_printf(ERRMSG_LVL_ERROR,
356
                    _("Plugin '%s' init function returned error.\n"),
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
357
                    module->getName().c_str());
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
358
      return true;
359
    }
360
  }
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
361
  module->isInited= true;
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
362
1 by brian
clean slate
363
965.1.1 by Brian Aker
Refactor plugin loading to remove mask (one step closer to getting rid of
364
  return false;
1 by brian
clean slate
365
}
366
367
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
368
static unsigned char *get_bookmark_hash_key(const unsigned char *buff,
369
                                            size_t *length, bool)
1 by brian
clean slate
370
{
371
  struct st_bookmark *var= (st_bookmark *)buff;
372
  *length= var->name_len + 1;
481 by Brian Aker
Remove all of uchar.
373
  return (unsigned char*) var->key;
1 by brian
clean slate
374
}
375
376
377
/*
378
  The logic is that we first load and initialize all compiled in plugins.
379
  From there we load up the dynamic types (assuming we have not been told to
380
  skip this part).
381
382
  Finally we initialize everything, aka the dynamic that have yet to initialize.
383
*/
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
384
bool plugin_init(module::Registry &registry,
1241.10.4 by Monty Taylor
Actually throw errors on failure to init plugins.
385
                 int *argc, char **argv,
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
386
                 bool skip_init,
387
                 po::options_description &long_options)
1 by brian
clean slate
388
{
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
389
  module::Module *module;
1485 by Brian Aker
Updates to confine memroot
390
  memory::Root tmp_root(4096);
1 by brian
clean slate
391
392
  if (initialized)
1241.10.4 by Monty Taylor
Actually throw errors on failure to init plugins.
393
    return false;
1 by brian
clean slate
394
395
  if (hash_init(&bookmark_hash, &my_charset_bin, 16, 0, 0,
396
                  get_bookmark_hash_key, NULL, HASH_UNIQUE))
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
397
  {
1487 by Brian Aker
More updates for memory::Root
398
    tmp_root.free_root(MYF(0));
1241.10.4 by Monty Taylor
Actually throw errors on failure to init plugins.
399
    return true;
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
400
  }
401
1 by brian
clean slate
402
403
  initialized= 1;
404
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
405
  vector<string> builtin_list;
406
  tokenize(builtin_plugins, builtin_list, ",", true);
1 by brian
clean slate
407
1241.10.4 by Monty Taylor
Actually throw errors on failure to init plugins.
408
  bool load_failed= false;
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
409
  vector<string> plugin_list;
410
  if (opt_plugin_load)
411
  {
412
    tokenize(opt_plugin_load, plugin_list, ",", true);
413
  }
414
  else
415
  {
416
    tokenize(opt_plugin_load_default, plugin_list, ",", true);
417
  }
418
  if (opt_plugin_add)
419
  {
420
    tokenize(opt_plugin_add, plugin_list, ",", true);
421
  }
422
423
  if (opt_plugin_remove)
424
  {
425
    vector<string> plugins_to_remove;
426
    tokenize(opt_plugin_remove, plugins_to_remove, ",", true);
427
    plugin_prune_list(plugin_list, plugins_to_remove);
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
428
    plugin_prune_list(builtin_list, plugins_to_remove);
429
  }
430
431
432
  /*
433
    First we register builtin plugins
434
  */
435
  const set<string> builtin_list_set(builtin_list.begin(), builtin_list.end());
436
  load_failed= plugin_load_list(registry, &tmp_root, argc, argv,
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
437
                                builtin_list_set, long_options, true);
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
438
  if (load_failed)
439
  {
440
    tmp_root.free_root(MYF(0));
441
    return true;
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
442
  }
1377.3.11 by Monty Taylor
Suppress duplicate plugin names.
443
444
  /* Uniquify the list */
445
  const set<string> plugin_list_set(plugin_list.begin(), plugin_list.end());
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
446
  
1 by brian
clean slate
447
  /* Register all dynamic plugins */
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
448
  load_failed= plugin_load_list(registry, &tmp_root, argc, argv,
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
449
                                plugin_list_set, long_options);
1241.10.4 by Monty Taylor
Actually throw errors on failure to init plugins.
450
  if (load_failed)
451
  {
1487 by Brian Aker
More updates for memory::Root
452
    tmp_root.free_root(MYF(0));
1241.10.4 by Monty Taylor
Actually throw errors on failure to init plugins.
453
    return true;
1 by brian
clean slate
454
  }
455
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
456
  if (skip_init)
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
457
  {
1487 by Brian Aker
More updates for memory::Root
458
    tmp_root.free_root(MYF(0));
1241.10.4 by Monty Taylor
Actually throw errors on failure to init plugins.
459
    return false;
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
460
  }
1 by brian
clean slate
461
462
  /*
463
    Now we initialize all remaining plugins
464
  */
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
465
  std::map<std::string, module::Module *>::const_iterator modules=
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
466
    registry.getModulesMap().begin();
467
    
468
  while (modules != registry.getModulesMap().end())
1 by brian
clean slate
469
  {
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
470
    module= (*modules).second;
471
    ++modules;
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
472
    if (module->isInited == false)
1 by brian
clean slate
473
    {
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
474
      plugin_initialize_vars(module);
1119.1.1 by Monty Taylor
--help doesn't need --datadir to work.
475
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
476
      if (plugin_initialize(registry, module))
1324.2.3 by Monty Taylor
Remove plugin deinit.
477
        delete_module(module);
1 by brian
clean slate
478
    }
479
  }
480
481
1487 by Brian Aker
More updates for memory::Root
482
  tmp_root.free_root(MYF(0));
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
483
1241.10.4 by Monty Taylor
Actually throw errors on failure to init plugins.
484
  return false;
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
485
}
486
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
487
class PrunePlugin :
488
  public unary_function<string, bool>
489
{
490
  const string to_match;
491
  PrunePlugin();
492
  PrunePlugin& operator=(const PrunePlugin&);
493
public:
494
  explicit PrunePlugin(const string &match_in) :
495
    to_match(match_in)
496
  { }
497
498
  result_type operator()(const string &match_against)
499
  {
500
    return match_against == to_match;
501
  }
502
};
503
504
static void plugin_prune_list(vector<string> &plugin_list,
505
                              const vector<string> &plugins_to_remove)
506
{
507
  for (vector<string>::const_iterator iter= plugins_to_remove.begin();
508
       iter != plugins_to_remove.end();
509
       ++iter)
510
  {
511
    plugin_list.erase(remove_if(plugin_list.begin(),
512
                                plugin_list.end(),
513
                                PrunePlugin(*iter)),
514
                      plugin_list.end());
515
  }
516
}
1 by brian
clean slate
517
518
/*
519
  called only by plugin_init()
520
*/
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
521
static bool plugin_load_list(module::Registry &registry,
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
522
                             memory::Root *tmp_root, int *argc, char **argv,
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
523
                             const set<string> &plugin_list,
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
524
                             po::options_description &long_options,
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
525
                             bool builtin)
1 by brian
clean slate
526
{
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
527
  module::Library *library= NULL;
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
528
1377.3.11 by Monty Taylor
Suppress duplicate plugin names.
529
  for (set<string>::const_iterator iter= plugin_list.begin();
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
530
       iter != plugin_list.end();
531
       ++iter)
1 by brian
clean slate
532
  {
1283.1.5 by Monty Taylor
Added --plugin-remove option, which prunes plugins from the list of plugins
533
    const string plugin_name(*iter);
1530.2.4 by Monty Taylor
Fixed style catches from Jay.
534
535
    library= registry.addLibrary(plugin_name, builtin);
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
536
    if (library == NULL)
537
    {
538
      errmsg_printf(ERRMSG_LVL_ERROR,
1284.2.1 by Monty Taylor
Add a fatal error if a plugin has a name which has already been registered.
539
                    _("Couldn't load plugin library named '%s'.\n"),
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
540
                    plugin_name.c_str());
541
      return true;
542
    }
543
1487 by Brian Aker
More updates for memory::Root
544
    tmp_root->free_root(MYF(memory::MARK_BLOCKS_FREE));
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
545
    if (plugin_add(registry, tmp_root, library, argc, argv, long_options))
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
546
    {
547
      registry.removeLibrary(plugin_name);
548
      errmsg_printf(ERRMSG_LVL_ERROR,
1284.2.1 by Monty Taylor
Add a fatal error if a plugin has a name which has already been registered.
549
                    _("Couldn't load plugin named '%s'.\n"),
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
550
                    plugin_name.c_str());
551
      return true;
552
553
    }
1 by brian
clean slate
554
  }
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
555
  return false;
1 by brian
clean slate
556
}
557
558
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
559
void module_shutdown(module::Registry &registry)
1 by brian
clean slate
560
{
561
562
  if (initialized)
563
  {
564
    reap_needed= true;
565
1110.1.3 by Monty Taylor
Added ListenHandler as a member of PluginRegistry.
566
    reap_plugins(registry);
962.1.6 by Brian Aker
Cut down on shutdown loop of plugins (cutting stuff out in order to simplify
567
    unlock_variables(NULL, &global_system_variables);
568
    unlock_variables(NULL, &max_system_variables);
1 by brian
clean slate
569
570
    cleanup_variables(NULL, &global_system_variables);
571
    cleanup_variables(NULL, &max_system_variables);
572
573
    initialized= 0;
574
  }
575
576
  /* Dispose of the memory */
577
578
  hash_free(&bookmark_hash);
1487 by Brian Aker
More updates for memory::Root
579
  plugin_mem_root.free_root(MYF(0));
1 by brian
clean slate
580
581
  global_variables_dynamic_size= 0;
582
}
583
584
/****************************************************************************
585
  Internal type declarations for variables support
586
****************************************************************************/
587
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
588
#undef DRIZZLE_SYSVAR_NAME
589
#define DRIZZLE_SYSVAR_NAME(name) name
1 by brian
clean slate
590
#define PLUGIN_VAR_TYPEMASK 0x007f
591
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
592
static const uint32_t EXTRA_OPTIONS= 1; /* handle the NULL option */
1 by brian
clean slate
593
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
594
typedef DECLARE_DRIZZLE_SYSVAR_BASIC(sysvar_bool_t, bool);
520.1.22 by Brian Aker
Second pass of thd cleanup
595
typedef DECLARE_DRIZZLE_SessionVAR_BASIC(sessionvar_bool_t, bool);
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
596
typedef DECLARE_DRIZZLE_SYSVAR_BASIC(sysvar_str_t, char *);
520.1.22 by Brian Aker
Second pass of thd cleanup
597
typedef DECLARE_DRIZZLE_SessionVAR_BASIC(sessionvar_str_t, char *);
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
598
520.1.22 by Brian Aker
Second pass of thd cleanup
599
typedef DECLARE_DRIZZLE_SessionVAR_TYPELIB(sessionvar_enum_t, unsigned long);
600
typedef DECLARE_DRIZZLE_SessionVAR_TYPELIB(sessionvar_set_t, uint64_t);
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
601
602
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_int_t, int);
603
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_long_t, long);
604
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_int64_t_t, int64_t);
605
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_uint_t, uint);
606
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_ulong_t, ulong);
607
typedef DECLARE_DRIZZLE_SYSVAR_SIMPLE(sysvar_uint64_t_t, uint64_t);
608
520.1.22 by Brian Aker
Second pass of thd cleanup
609
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_int_t, int);
610
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_long_t, long);
611
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_int64_t_t, int64_t);
612
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_uint_t, uint);
613
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_ulong_t, ulong);
614
typedef DECLARE_DRIZZLE_SessionVAR_SIMPLE(sessionvar_uint64_t_t, uint64_t);
1 by brian
clean slate
615
520.1.22 by Brian Aker
Second pass of thd cleanup
616
typedef bool *(*mysql_sys_var_ptr_p)(Session* a_session, int offset);
1 by brian
clean slate
617
618
619
/****************************************************************************
620
  default variable data check and update functions
621
****************************************************************************/
622
1228.1.5 by Monty Taylor
Merged in some naming things.
623
static int check_func_bool(Session *, drizzle_sys_var *var,
624
                           void *save, drizzle_value *value)
1 by brian
clean slate
625
{
626
  char buff[STRING_BUFFER_USUAL_SIZE];
627
  const char *strvalue= "NULL", *str;
628
  int result, length;
53.2.2 by Monty Taylor
Updated everything that needs updating to compile with -std=gnu99 -pedantic
629
  int64_t tmp;
1 by brian
clean slate
630
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
631
  if (value->value_type(value) == DRIZZLE_VALUE_TYPE_STRING)
1 by brian
clean slate
632
  {
633
    length= sizeof(buff);
634
    if (!(str= value->val_str(value, buff, &length)) ||
635
        (result= find_type(&bool_typelib, str, length, 1)-1) < 0)
636
    {
637
      if (str)
638
        strvalue= str;
639
      goto err;
640
    }
641
  }
642
  else
643
  {
644
    if (value->val_int(value, &tmp) < 0)
645
      goto err;
646
    if (tmp > 1)
647
    {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
648
      internal::llstr(tmp, buff);
1 by brian
clean slate
649
      strvalue= buff;
650
      goto err;
651
    }
652
    result= (int) tmp;
653
  }
654
  *(int*)save= -result;
655
  return 0;
656
err:
657
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->name, strvalue);
658
  return 1;
659
}
660
661
1228.1.5 by Monty Taylor
Merged in some naming things.
662
static int check_func_int(Session *session, drizzle_sys_var *var,
663
                          void *save, drizzle_value *value)
1 by brian
clean slate
664
{
143 by Brian Aker
Bool cleanup.
665
  bool fixed;
53.2.2 by Monty Taylor
Updated everything that needs updating to compile with -std=gnu99 -pedantic
666
  int64_t tmp;
1410.3.4 by Djellel E. Difallah
update references to old my_'s
667
  struct option options;
1 by brian
clean slate
668
  value->val_int(value, &tmp);
669
  plugin_opt_set_limits(&options, var);
670
671
  if (var->flags & PLUGIN_VAR_UNSIGNED)
895 by Brian Aker
Completion (?) of uint conversion.
672
    *(uint32_t *)save= (uint32_t) getopt_ull_limit_value((uint64_t) tmp, &options,
1 by brian
clean slate
673
                                                   &fixed);
674
  else
675
    *(int *)save= (int) getopt_ll_limit_value(tmp, &options, &fixed);
676
520.1.22 by Brian Aker
Second pass of thd cleanup
677
  return throw_bounds_warning(session, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
152 by Brian Aker
longlong replacement
678
                              var->name, (int64_t) tmp);
1 by brian
clean slate
679
}
680
681
1228.1.5 by Monty Taylor
Merged in some naming things.
682
static int check_func_long(Session *session, drizzle_sys_var *var,
683
                          void *save, drizzle_value *value)
1 by brian
clean slate
684
{
143 by Brian Aker
Bool cleanup.
685
  bool fixed;
53.2.2 by Monty Taylor
Updated everything that needs updating to compile with -std=gnu99 -pedantic
686
  int64_t tmp;
1410.3.4 by Djellel E. Difallah
update references to old my_'s
687
  struct option options;
1 by brian
clean slate
688
  value->val_int(value, &tmp);
689
  plugin_opt_set_limits(&options, var);
690
691
  if (var->flags & PLUGIN_VAR_UNSIGNED)
151 by Brian Aker
Ulonglong to uint64_t
692
    *(ulong *)save= (ulong) getopt_ull_limit_value((uint64_t) tmp, &options,
1 by brian
clean slate
693
                                                   &fixed);
694
  else
695
    *(long *)save= (long) getopt_ll_limit_value(tmp, &options, &fixed);
696
520.1.22 by Brian Aker
Second pass of thd cleanup
697
  return throw_bounds_warning(session, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
152 by Brian Aker
longlong replacement
698
                              var->name, (int64_t) tmp);
1 by brian
clean slate
699
}
700
701
1228.1.5 by Monty Taylor
Merged in some naming things.
702
static int check_func_int64_t(Session *session, drizzle_sys_var *var,
703
                               void *save, drizzle_value *value)
1 by brian
clean slate
704
{
143 by Brian Aker
Bool cleanup.
705
  bool fixed;
53.2.2 by Monty Taylor
Updated everything that needs updating to compile with -std=gnu99 -pedantic
706
  int64_t tmp;
1410.3.4 by Djellel E. Difallah
update references to old my_'s
707
  struct option options;
1 by brian
clean slate
708
  value->val_int(value, &tmp);
709
  plugin_opt_set_limits(&options, var);
710
711
  if (var->flags & PLUGIN_VAR_UNSIGNED)
151 by Brian Aker
Ulonglong to uint64_t
712
    *(uint64_t *)save= getopt_ull_limit_value((uint64_t) tmp, &options,
1 by brian
clean slate
713
                                               &fixed);
714
  else
152 by Brian Aker
longlong replacement
715
    *(int64_t *)save= getopt_ll_limit_value(tmp, &options, &fixed);
1 by brian
clean slate
716
520.1.22 by Brian Aker
Second pass of thd cleanup
717
  return throw_bounds_warning(session, fixed, var->flags & PLUGIN_VAR_UNSIGNED,
152 by Brian Aker
longlong replacement
718
                              var->name, (int64_t) tmp);
1 by brian
clean slate
719
}
720
1228.1.5 by Monty Taylor
Merged in some naming things.
721
static int check_func_str(Session *session, drizzle_sys_var *,
722
                          void *save, drizzle_value *value)
1 by brian
clean slate
723
{
724
  char buff[STRING_BUFFER_USUAL_SIZE];
725
  const char *str;
726
  int length;
727
728
  length= sizeof(buff);
729
  if ((str= value->val_str(value, buff, &length)))
520.1.22 by Brian Aker
Second pass of thd cleanup
730
    str= session->strmake(str, length);
1 by brian
clean slate
731
  *(const char**)save= str;
732
  return 0;
733
}
734
735
1228.1.5 by Monty Taylor
Merged in some naming things.
736
static void update_func_bool(Session *, drizzle_sys_var *,
1 by brian
clean slate
737
                             void *tgt, const void *save)
738
{
199 by Brian Aker
my_bool...
739
  *(bool *) tgt= *(int *) save ? 1 : 0;
1 by brian
clean slate
740
}
741
742
1228.1.5 by Monty Taylor
Merged in some naming things.
743
static void update_func_int(Session *, drizzle_sys_var *,
1 by brian
clean slate
744
                             void *tgt, const void *save)
745
{
746
  *(int *)tgt= *(int *) save;
747
}
748
749
1228.1.5 by Monty Taylor
Merged in some naming things.
750
static void update_func_long(Session *, drizzle_sys_var *,
1 by brian
clean slate
751
                             void *tgt, const void *save)
752
{
753
  *(long *)tgt= *(long *) save;
754
}
755
756
1228.1.5 by Monty Taylor
Merged in some naming things.
757
static void update_func_int64_t(Session *, drizzle_sys_var *,
77.1.46 by Monty Taylor
Finished the warnings work!
758
                                 void *tgt, const void *save)
1 by brian
clean slate
759
{
152 by Brian Aker
longlong replacement
760
  *(int64_t *)tgt= *(uint64_t *) save;
1 by brian
clean slate
761
}
762
763
1228.1.5 by Monty Taylor
Merged in some naming things.
764
static void update_func_str(Session *, drizzle_sys_var *var,
1 by brian
clean slate
765
                             void *tgt, const void *save)
766
{
767
  char *old= *(char **) tgt;
768
  *(char **)tgt= *(char **) save;
769
  if (var->flags & PLUGIN_VAR_MEMALLOC)
770
  {
656.1.19 by Monty Taylor
Removed my_strdup from drizzled/
771
    *(char **)tgt= strdup(*(char **) save);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
772
    free(old);
656.1.52 by Monty Taylor
Added more return value checks.
773
    /*
774
     * There isn't a _really_ good thing to do here until this whole set_var
775
     * mess gets redesigned
776
     */
777
    if (tgt == NULL)
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
778
      errmsg_printf(ERRMSG_LVL_ERROR, _("Out of memory."));
656.1.52 by Monty Taylor
Added more return value checks.
779
1 by brian
clean slate
780
  }
781
}
782
783
784
/****************************************************************************
785
  System Variables support
786
****************************************************************************/
787
788
970.1.1 by Brian Aker
Removed dead passed variables.
789
sys_var *find_sys_var(Session *, const char *str, uint32_t length)
1 by brian
clean slate
790
{
791
  sys_var *var;
792
  sys_var_pluginvar *pi= NULL;
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
793
  module::Module *module;
1 by brian
clean slate
794
658 by Brian Aker
Part removal of my_pthread.h
795
  pthread_rwlock_rdlock(&LOCK_system_variables_hash);
1 by brian
clean slate
796
  if ((var= intern_find_sys_var(str, length, false)) &&
797
      (pi= var->cast_pluginvar()))
798
  {
658 by Brian Aker
Part removal of my_pthread.h
799
    pthread_rwlock_unlock(&LOCK_system_variables_hash);
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
800
    if (!(module= pi->plugin))
1 by brian
clean slate
801
      var= NULL; /* failed to lock it, it must be uninstalling */
1192.2.3 by Monty Taylor
Renamed plugin::Handle to plugin::Module for clarity.
802
    else if (module->isInited == false)
1 by brian
clean slate
803
    {
804
      var= NULL;
805
    }
806
  }
807
  else
658 by Brian Aker
Part removal of my_pthread.h
808
    pthread_rwlock_unlock(&LOCK_system_variables_hash);
1 by brian
clean slate
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
    my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
816
  return(var);
1 by brian
clean slate
817
}
818
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
819
static const string make_bookmark_name(const string &plugin, const char *name, int flags)
820
{
821
  /* Embed the flags into the first char of the string */
822
  string varname(1, static_cast<char>(flags & PLUGIN_VAR_TYPEMASK));
823
  varname.append(plugin);
824
  varname.push_back('_');
825
  varname.append(name);
826
827
  for (string::iterator p= varname.begin() + 1; p != varname.end(); ++p)
828
  {
829
    if (*p == '-')
830
    {
831
      *p= '_';
832
    }
833
  }
834
  return varname;
835
}
1 by brian
clean slate
836
837
/*
838
  called by register_var, construct_options and test_plugin_options.
839
  Returns the 'bookmark' for the named variable.
840
  LOCK_system_variables_hash should be at least read locked
841
*/
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
842
static st_bookmark *find_bookmark(const string &plugin, const char *name, int flags)
1 by brian
clean slate
843
{
844
  st_bookmark *result= NULL;
845
520.1.21 by Brian Aker
THD -> Session rename
846
  if (!(flags & PLUGIN_VAR_SessionLOCAL))
1 by brian
clean slate
847
    return NULL;
848
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
849
  const string varname(make_bookmark_name(plugin, name, flags));
850
1 by brian
clean slate
851
852
  result= (st_bookmark*) hash_search(&bookmark_hash,
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
853
                                     (const unsigned char*) varname.c_str(), varname.size() - 1);
1 by brian
clean slate
854
855
  return result;
856
}
857
858
859
/*
520.1.22 by Brian Aker
Second pass of thd cleanup
860
  returns a bookmark for session-local variables, creating if neccessary.
861
  returns null for non session-local variables.
1 by brian
clean slate
862
  Requires that a write lock is obtained on LOCK_system_variables_hash
863
*/
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
864
static st_bookmark *register_var(const string &plugin, const char *name,
1 by brian
clean slate
865
                                 int flags)
866
{
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
867
  if (!(flags & PLUGIN_VAR_SessionLOCAL))
868
    return NULL;
869
870
  uint32_t size= 0, offset, new_size;
1 by brian
clean slate
871
  st_bookmark *result;
872
873
  switch (flags & PLUGIN_VAR_TYPEMASK) {
874
  case PLUGIN_VAR_BOOL:
937.2.12 by Stewart Smith
ensure correct alignment of all plugin variables. Manifesting as innodb_mysql test failure with SIGBUS on Sparc
875
    size= ALIGN_SIZE(sizeof(bool));
1 by brian
clean slate
876
    break;
877
  case PLUGIN_VAR_INT:
937.2.12 by Stewart Smith
ensure correct alignment of all plugin variables. Manifesting as innodb_mysql test failure with SIGBUS on Sparc
878
    size= ALIGN_SIZE(sizeof(int));
1 by brian
clean slate
879
    break;
880
  case PLUGIN_VAR_LONG:
937.2.12 by Stewart Smith
ensure correct alignment of all plugin variables. Manifesting as innodb_mysql test failure with SIGBUS on Sparc
881
    size= ALIGN_SIZE(sizeof(long));
1 by brian
clean slate
882
    break;
883
  case PLUGIN_VAR_LONGLONG:
937.2.12 by Stewart Smith
ensure correct alignment of all plugin variables. Manifesting as innodb_mysql test failure with SIGBUS on Sparc
884
    size= ALIGN_SIZE(sizeof(uint64_t));
1 by brian
clean slate
885
    break;
886
  case PLUGIN_VAR_STR:
937.2.12 by Stewart Smith
ensure correct alignment of all plugin variables. Manifesting as innodb_mysql test failure with SIGBUS on Sparc
887
    size= ALIGN_SIZE(sizeof(char*));
1 by brian
clean slate
888
    break;
889
  default:
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
890
    assert(0);
1 by brian
clean slate
891
    return NULL;
892
  };
893
894
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
895
  if (!(result= find_bookmark(plugin, name, flags)))
1 by brian
clean slate
896
  {
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
897
    const string varname(make_bookmark_name(plugin, name, flags));
898
1485 by Brian Aker
Updates to confine memroot
899
    result= static_cast<st_bookmark*>(plugin_mem_root.alloc_root(sizeof(struct st_bookmark) + varname.size() + 1));
1283.1.3 by Monty Taylor
Hrm. Sometimes initializing memory is nice.
900
    memset(result->key, 0, varname.size()+1);
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
901
    memcpy(result->key, varname.c_str(), varname.size());
902
    result->name_len= varname.size() - 2;
1 by brian
clean slate
903
    result->offset= -1;
904
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
905
    assert(size && !(size & (size-1))); /* must be power of 2 */
1 by brian
clean slate
906
907
    offset= global_system_variables.dynamic_variables_size;
908
    offset= (offset + size - 1) & ~(size - 1);
909
    result->offset= (int) offset;
910
911
    new_size= (offset + size + 63) & ~63;
912
913
    if (new_size > global_variables_dynamic_size)
914
    {
656.1.46 by Monty Taylor
More malloc return cleanups.
915
      char* tmpptr= NULL;
916
      if (!(tmpptr=
917
              (char *)realloc(global_system_variables.dynamic_variables_ptr,
918
                              new_size)))
919
        return NULL;
920
      global_system_variables.dynamic_variables_ptr= tmpptr;
921
      tmpptr= NULL;
922
      if (!(tmpptr=
923
              (char *)realloc(max_system_variables.dynamic_variables_ptr,
924
                              new_size)))
925
        return NULL;
926
      max_system_variables.dynamic_variables_ptr= tmpptr;
656.1.26 by Monty Taylor
Finally removed all of the my_malloc stuff.
927
           
1 by brian
clean slate
928
      /*
929
        Clear the new variable value space. This is required for string
930
        variables. If their value is non-NULL, it must point to a valid
931
        string.
932
      */
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
933
      memset(global_system_variables.dynamic_variables_ptr +
934
             global_variables_dynamic_size, 0,
935
             new_size - global_variables_dynamic_size);
936
      memset(max_system_variables.dynamic_variables_ptr +
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
937
             global_variables_dynamic_size, 0,
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
938
             new_size - global_variables_dynamic_size);
1 by brian
clean slate
939
      global_variables_dynamic_size= new_size;
940
    }
941
942
    global_system_variables.dynamic_variables_head= offset;
943
    max_system_variables.dynamic_variables_head= offset;
944
    global_system_variables.dynamic_variables_size= offset + size;
945
    max_system_variables.dynamic_variables_size= offset + size;
946
    global_system_variables.dynamic_variables_version++;
947
    max_system_variables.dynamic_variables_version++;
948
949
    result->version= global_system_variables.dynamic_variables_version;
950
951
    /* this should succeed because we have already checked if a dup exists */
481 by Brian Aker
Remove all of uchar.
952
    if (my_hash_insert(&bookmark_hash, (unsigned char*) result))
1 by brian
clean slate
953
    {
954
      fprintf(stderr, "failed to add placeholder to hash");
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
955
      assert(0);
1 by brian
clean slate
956
    }
957
  }
958
  return result;
959
}
960
961
962
/*
520.1.22 by Brian Aker
Second pass of thd cleanup
963
  returns a pointer to the memory which holds the session-local variable or
964
  a pointer to the global variable if session==null.
1 by brian
clean slate
965
  If required, will sync with global variables if the requested variable
966
  has not yet been allocated in the current thread.
967
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
968
static unsigned char *intern_sys_var_ptr(Session* session, int offset, bool global_lock)
1 by brian
clean slate
969
{
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
970
  assert(offset >= 0);
895 by Brian Aker
Completion (?) of uint conversion.
971
  assert((uint32_t)offset <= global_system_variables.dynamic_variables_head);
1 by brian
clean slate
972
520.1.22 by Brian Aker
Second pass of thd cleanup
973
  if (!session)
481 by Brian Aker
Remove all of uchar.
974
    return (unsigned char*) global_system_variables.dynamic_variables_ptr + offset;
1 by brian
clean slate
975
976
  /*
977
    dynamic_variables_head points to the largest valid offset
978
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
979
  if (!session->variables.dynamic_variables_ptr ||
895 by Brian Aker
Completion (?) of uint conversion.
980
      (uint32_t)offset > session->variables.dynamic_variables_head)
1 by brian
clean slate
981
  {
482 by Brian Aker
Remove uint.
982
    uint32_t idx;
1 by brian
clean slate
983
658 by Brian Aker
Part removal of my_pthread.h
984
    pthread_rwlock_rdlock(&LOCK_system_variables_hash);
1 by brian
clean slate
985
656.1.46 by Monty Taylor
More malloc return cleanups.
986
    char *tmpptr= NULL;
987
    if (!(tmpptr= (char *)realloc(session->variables.dynamic_variables_ptr,
988
                                  global_variables_dynamic_size)))
989
      return NULL;
990
    session->variables.dynamic_variables_ptr= tmpptr;
1 by brian
clean slate
991
992
    if (global_lock)
993
      pthread_mutex_lock(&LOCK_global_system_variables);
994
1241.9.62 by Monty Taylor
Removed plugin/myisam/myisam.h from session.h
995
    //safe_mutex_assert_owner(&LOCK_global_system_variables);
1 by brian
clean slate
996
520.1.22 by Brian Aker
Second pass of thd cleanup
997
    memcpy(session->variables.dynamic_variables_ptr +
998
             session->variables.dynamic_variables_size,
1 by brian
clean slate
999
           global_system_variables.dynamic_variables_ptr +
520.1.22 by Brian Aker
Second pass of thd cleanup
1000
             session->variables.dynamic_variables_size,
1 by brian
clean slate
1001
           global_system_variables.dynamic_variables_size -
520.1.22 by Brian Aker
Second pass of thd cleanup
1002
             session->variables.dynamic_variables_size);
1 by brian
clean slate
1003
1004
    /*
1005
      now we need to iterate through any newly copied 'defaults'
1006
      and if it is a string type with MEMALLOC flag, we need to strdup
1007
    */
1008
    for (idx= 0; idx < bookmark_hash.records; idx++)
1009
    {
1010
      sys_var_pluginvar *pi;
1011
      sys_var *var;
1012
      st_bookmark *v= (st_bookmark*) hash_element(&bookmark_hash,idx);
1013
520.1.22 by Brian Aker
Second pass of thd cleanup
1014
      if (v->version <= session->variables.dynamic_variables_version ||
1 by brian
clean slate
1015
          !(var= intern_find_sys_var(v->key + 1, v->name_len, true)) ||
1016
          !(pi= var->cast_pluginvar()) ||
1017
          v->key[0] != (pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
1018
        continue;
1019
1020
      /* Here we do anything special that may be required of the data types */
1021
1022
      if ((pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
1023
          pi->plugin_var->flags & PLUGIN_VAR_MEMALLOC)
1024
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
1025
         char **pp= (char**) (session->variables.dynamic_variables_ptr +
1 by brian
clean slate
1026
                             *(int*)(pi->plugin_var + 1));
1027
         if ((*pp= *(char**) (global_system_variables.dynamic_variables_ptr +
1028
                             *(int*)(pi->plugin_var + 1))))
656.1.19 by Monty Taylor
Removed my_strdup from drizzled/
1029
           *pp= strdup(*pp);
656.1.52 by Monty Taylor
Added more return value checks.
1030
         if (*pp == NULL)
1031
           return NULL;
1 by brian
clean slate
1032
      }
1033
    }
1034
1035
    if (global_lock)
1036
      pthread_mutex_unlock(&LOCK_global_system_variables);
1037
520.1.22 by Brian Aker
Second pass of thd cleanup
1038
    session->variables.dynamic_variables_version=
1 by brian
clean slate
1039
           global_system_variables.dynamic_variables_version;
520.1.22 by Brian Aker
Second pass of thd cleanup
1040
    session->variables.dynamic_variables_head=
1 by brian
clean slate
1041
           global_system_variables.dynamic_variables_head;
520.1.22 by Brian Aker
Second pass of thd cleanup
1042
    session->variables.dynamic_variables_size=
1 by brian
clean slate
1043
           global_system_variables.dynamic_variables_size;
1044
658 by Brian Aker
Part removal of my_pthread.h
1045
    pthread_rwlock_unlock(&LOCK_system_variables_hash);
1 by brian
clean slate
1046
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1047
  return (unsigned char*)session->variables.dynamic_variables_ptr + offset;
1048
}
1049
1050
static bool *mysql_sys_var_ptr_bool(Session* a_session, int offset)
1051
{
1052
  return (bool *)intern_sys_var_ptr(a_session, offset, true);
1053
}
1054
1055
static int *mysql_sys_var_ptr_int(Session* a_session, int offset)
1056
{
1057
  return (int *)intern_sys_var_ptr(a_session, offset, true);
1058
}
1059
1060
static long *mysql_sys_var_ptr_long(Session* a_session, int offset)
1061
{
1062
  return (long *)intern_sys_var_ptr(a_session, offset, true);
1063
}
1064
1065
static int64_t *mysql_sys_var_ptr_int64_t(Session* a_session, int offset)
1066
{
1067
  return (int64_t *)intern_sys_var_ptr(a_session, offset, true);
1068
}
1069
1070
static char **mysql_sys_var_ptr_str(Session* a_session, int offset)
1071
{
1072
  return (char **)intern_sys_var_ptr(a_session, offset, true);
1073
}
1074
1075
void plugin_sessionvar_init(Session *session)
1076
{
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
1077
  session->variables.storage_engine= NULL;
520.1.22 by Brian Aker
Second pass of thd cleanup
1078
  cleanup_variables(session, &session->variables);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1079
520.1.22 by Brian Aker
Second pass of thd cleanup
1080
  session->variables= global_system_variables;
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
1081
  session->variables.storage_engine= NULL;
1 by brian
clean slate
1082
1083
  /* we are going to allocate these lazily */
520.1.22 by Brian Aker
Second pass of thd cleanup
1084
  session->variables.dynamic_variables_version= 0;
1085
  session->variables.dynamic_variables_size= 0;
1086
  session->variables.dynamic_variables_ptr= 0;
1 by brian
clean slate
1087
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
1088
  session->variables.storage_engine= global_system_variables.storage_engine;
1 by brian
clean slate
1089
}
1090
1091
1092
/*
1093
  Unlocks all system variables which hold a reference
1094
*/
654 by Brian Aker
Remove unused (yet more)
1095
static void unlock_variables(Session *, struct system_variables *vars)
1 by brian
clean slate
1096
{
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
1097
  vars->storage_engine= NULL;
1 by brian
clean slate
1098
}
1099
1100
1101
/*
1102
  Frees memory used by system variables
1103
1104
  Unlike plugin_vars_free_values() it frees all variables of all plugins,
1105
  it's used on shutdown.
1106
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
1107
static void cleanup_variables(Session *session, struct system_variables *vars)
1 by brian
clean slate
1108
{
1109
  st_bookmark *v;
1110
  sys_var_pluginvar *pivar;
1111
  sys_var *var;
1112
  int flags;
482 by Brian Aker
Remove uint.
1113
  uint32_t idx;
1 by brian
clean slate
1114
658 by Brian Aker
Part removal of my_pthread.h
1115
  pthread_rwlock_rdlock(&LOCK_system_variables_hash);
1 by brian
clean slate
1116
  for (idx= 0; idx < bookmark_hash.records; idx++)
1117
  {
1118
    v= (st_bookmark*) hash_element(&bookmark_hash, idx);
1119
    if (v->version > vars->dynamic_variables_version ||
1120
        !(var= intern_find_sys_var(v->key + 1, v->name_len, true)) ||
1121
        !(pivar= var->cast_pluginvar()) ||
1122
        v->key[0] != (pivar->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
1123
      continue;
1124
1125
    flags= pivar->plugin_var->flags;
1126
1127
    if ((flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
520.1.21 by Brian Aker
THD -> Session rename
1128
        flags & PLUGIN_VAR_SessionLOCAL && flags & PLUGIN_VAR_MEMALLOC)
1 by brian
clean slate
1129
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1130
      char **ptr= (char**) pivar->real_value_ptr(session, OPT_SESSION);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
1131
      free(*ptr);
1 by brian
clean slate
1132
      *ptr= NULL;
1133
    }
1134
  }
658 by Brian Aker
Part removal of my_pthread.h
1135
  pthread_rwlock_unlock(&LOCK_system_variables_hash);
1 by brian
clean slate
1136
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
1137
  assert(vars->storage_engine == NULL);
1 by brian
clean slate
1138
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
1139
  free(vars->dynamic_variables_ptr);
1 by brian
clean slate
1140
  vars->dynamic_variables_ptr= NULL;
1141
  vars->dynamic_variables_size= 0;
1142
  vars->dynamic_variables_version= 0;
1143
}
1144
1145
520.1.22 by Brian Aker
Second pass of thd cleanup
1146
void plugin_sessionvar_cleanup(Session *session)
1 by brian
clean slate
1147
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1148
  unlock_variables(session, &session->variables);
1149
  cleanup_variables(session, &session->variables);
1 by brian
clean slate
1150
}
1151
1152
1153
/**
1154
  @brief Free values of thread variables of a plugin.
1155
1156
  This must be called before a plugin is deleted. Otherwise its
1157
  variables are no longer accessible and the value space is lost. Note
1158
  that only string values with PLUGIN_VAR_MEMALLOC are allocated and
1159
  must be freed.
1160
1161
  @param[in]        vars        Chain of system variables of a plugin
1162
*/
1163
1164
static void plugin_vars_free_values(sys_var *vars)
1165
{
1166
1055.2.20 by Jay Pipes
Refactors sys_var class -- doxygenates and documents the class members and functions. Protects all member variables and adds public getters/setters.
1167
  for (sys_var *var= vars; var; var= var->getNext())
1 by brian
clean slate
1168
  {
1169
    sys_var_pluginvar *piv= var->cast_pluginvar();
1170
    if (piv &&
1171
        ((piv->plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR) &&
1172
        (piv->plugin_var->flags & PLUGIN_VAR_MEMALLOC))
1173
    {
1174
      /* Free the string from global_system_variables. */
1175
      char **valptr= (char**) piv->real_value_ptr(NULL, OPT_GLOBAL);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
1176
      free(*valptr);
1 by brian
clean slate
1177
      *valptr= NULL;
1178
    }
1179
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1180
  return;
1 by brian
clean slate
1181
}
1182
1183
1184
bool sys_var_pluginvar::check_update_type(Item_result type)
1185
{
1186
  if (is_readonly())
1187
    return 1;
1188
  switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
1189
  case PLUGIN_VAR_INT:
1190
  case PLUGIN_VAR_LONG:
1191
  case PLUGIN_VAR_LONGLONG:
1192
    return type != INT_RESULT;
1193
  case PLUGIN_VAR_STR:
1194
    return type != STRING_RESULT;
1195
  default:
1196
    return 0;
1197
  }
1198
}
1199
1200
1201
SHOW_TYPE sys_var_pluginvar::show_type()
1202
{
1203
  switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
1204
  case PLUGIN_VAR_BOOL:
1205
    return SHOW_MY_BOOL;
1206
  case PLUGIN_VAR_INT:
1207
    return SHOW_INT;
1208
  case PLUGIN_VAR_LONG:
1209
    return SHOW_LONG;
1210
  case PLUGIN_VAR_LONGLONG:
1211
    return SHOW_LONGLONG;
1212
  case PLUGIN_VAR_STR:
1213
    return SHOW_CHAR_PTR;
1214
  default:
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1215
    assert(0);
1 by brian
clean slate
1216
    return SHOW_UNDEF;
1217
  }
1218
}
1219
1220
1273.13.24 by Brian Aker
Updating style, simplified code.
1221
unsigned char* sys_var_pluginvar::real_value_ptr(Session *session, sql_var_t type)
1 by brian
clean slate
1222
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1223
  assert(session || (type == OPT_GLOBAL));
520.1.21 by Brian Aker
THD -> Session rename
1224
  if (plugin_var->flags & PLUGIN_VAR_SessionLOCAL)
1 by brian
clean slate
1225
  {
1226
    if (type == OPT_GLOBAL)
520.1.22 by Brian Aker
Second pass of thd cleanup
1227
      session= NULL;
1 by brian
clean slate
1228
520.1.22 by Brian Aker
Second pass of thd cleanup
1229
    return intern_sys_var_ptr(session, *(int*) (plugin_var+1), false);
1 by brian
clean slate
1230
  }
481 by Brian Aker
Remove all of uchar.
1231
  return *(unsigned char**) (plugin_var+1);
1 by brian
clean slate
1232
}
1233
1234
1235
TYPELIB* sys_var_pluginvar::plugin_var_typelib(void)
1236
{
520.1.21 by Brian Aker
THD -> Session rename
1237
  switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_SessionLOCAL)) {
1280.1.6 by Brian Aker
removw DRIZZLE_SYSVAR_ENUM
1238
  case PLUGIN_VAR_SessionLOCAL:
520.1.22 by Brian Aker
Second pass of thd cleanup
1239
    return ((sessionvar_enum_t *)plugin_var)->typelib;
1 by brian
clean slate
1240
  default:
1241
    return NULL;
1242
  }
1243
  return NULL;
1244
}
1245
1246
1273.13.24 by Brian Aker
Updating style, simplified code.
1247
unsigned char* sys_var_pluginvar::value_ptr(Session *session, sql_var_t type, const LEX_STRING *)
1 by brian
clean slate
1248
{
481 by Brian Aker
Remove all of uchar.
1249
  unsigned char* result;
1 by brian
clean slate
1250
520.1.22 by Brian Aker
Second pass of thd cleanup
1251
  result= real_value_ptr(session, type);
1 by brian
clean slate
1252
1253
  return result;
1254
}
1255
1256
520.1.22 by Brian Aker
Second pass of thd cleanup
1257
bool sys_var_pluginvar::check(Session *session, set_var *var)
1 by brian
clean slate
1258
{
1259
  st_item_value_holder value;
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1260
  assert(is_readonly() || plugin_var->check);
1 by brian
clean slate
1261
1262
  value.value_type= item_value_type;
1263
  value.val_str= item_val_str;
1264
  value.val_int= item_val_int;
1265
  value.val_real= item_val_real;
1266
  value.item= var->value;
1267
1268
  return is_readonly() ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1269
         plugin_var->check(session, plugin_var, &var->save_result, &value);
1 by brian
clean slate
1270
}
1271
1272
1273.13.24 by Brian Aker
Updating style, simplified code.
1273
void sys_var_pluginvar::set_default(Session *session, sql_var_t type)
1 by brian
clean slate
1274
{
1275
  const void *src;
1276
  void *tgt;
1277
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1278
  assert(is_readonly() || plugin_var->update);
1 by brian
clean slate
1279
1280
  if (is_readonly())
1281
    return;
1282
1283
  pthread_mutex_lock(&LOCK_global_system_variables);
520.1.22 by Brian Aker
Second pass of thd cleanup
1284
  tgt= real_value_ptr(session, type);
1 by brian
clean slate
1285
  src= ((void **) (plugin_var + 1) + 1);
1286
520.1.21 by Brian Aker
THD -> Session rename
1287
  if (plugin_var->flags & PLUGIN_VAR_SessionLOCAL)
1 by brian
clean slate
1288
  {
1289
    if (type != OPT_GLOBAL)
520.1.22 by Brian Aker
Second pass of thd cleanup
1290
      src= real_value_ptr(session, OPT_GLOBAL);
1 by brian
clean slate
1291
    else
1292
    switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
1293
	case PLUGIN_VAR_INT:
520.1.22 by Brian Aker
Second pass of thd cleanup
1294
	  src= &((sessionvar_uint_t*) plugin_var)->def_val;
1 by brian
clean slate
1295
	  break;
1296
	case PLUGIN_VAR_LONG:
520.1.22 by Brian Aker
Second pass of thd cleanup
1297
	  src= &((sessionvar_ulong_t*) plugin_var)->def_val;
1 by brian
clean slate
1298
	  break;
1299
	case PLUGIN_VAR_LONGLONG:
520.1.22 by Brian Aker
Second pass of thd cleanup
1300
	  src= &((sessionvar_uint64_t_t*) plugin_var)->def_val;
1 by brian
clean slate
1301
	  break;
1302
	case PLUGIN_VAR_BOOL:
520.1.22 by Brian Aker
Second pass of thd cleanup
1303
	  src= &((sessionvar_bool_t*) plugin_var)->def_val;
1 by brian
clean slate
1304
	  break;
1305
	case PLUGIN_VAR_STR:
520.1.22 by Brian Aker
Second pass of thd cleanup
1306
	  src= &((sessionvar_str_t*) plugin_var)->def_val;
1 by brian
clean slate
1307
	  break;
1308
	default:
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1309
	  assert(0);
1 by brian
clean slate
1310
	}
1311
  }
1312
520.1.22 by Brian Aker
Second pass of thd cleanup
1313
  /* session must equal current_session if PLUGIN_VAR_SessionLOCAL flag is set */
520.1.21 by Brian Aker
THD -> Session rename
1314
  assert(!(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1315
              session == current_session);
1 by brian
clean slate
1316
520.1.21 by Brian Aker
THD -> Session rename
1317
  if (!(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) || type == OPT_GLOBAL)
1 by brian
clean slate
1318
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1319
    plugin_var->update(session, plugin_var, tgt, src);
1 by brian
clean slate
1320
    pthread_mutex_unlock(&LOCK_global_system_variables);
1321
  }
1322
  else
1323
  {
1324
    pthread_mutex_unlock(&LOCK_global_system_variables);
520.1.22 by Brian Aker
Second pass of thd cleanup
1325
    plugin_var->update(session, plugin_var, tgt, src);
1 by brian
clean slate
1326
  }
1327
}
1328
1329
520.1.22 by Brian Aker
Second pass of thd cleanup
1330
bool sys_var_pluginvar::update(Session *session, set_var *var)
1 by brian
clean slate
1331
{
1332
  void *tgt;
1333
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1334
  assert(is_readonly() || plugin_var->update);
1 by brian
clean slate
1335
520.1.22 by Brian Aker
Second pass of thd cleanup
1336
  /* session must equal current_session if PLUGIN_VAR_SessionLOCAL flag is set */
520.1.21 by Brian Aker
THD -> Session rename
1337
  assert(!(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1338
              session == current_session);
1 by brian
clean slate
1339
1340
  if (is_readonly())
1341
    return 1;
1342
1343
  pthread_mutex_lock(&LOCK_global_system_variables);
520.1.22 by Brian Aker
Second pass of thd cleanup
1344
  tgt= real_value_ptr(session, var->type);
1 by brian
clean slate
1345
520.1.21 by Brian Aker
THD -> Session rename
1346
  if (!(plugin_var->flags & PLUGIN_VAR_SessionLOCAL) || var->type == OPT_GLOBAL)
1 by brian
clean slate
1347
  {
1348
    /* variable we are updating has global scope, so we unlock after updating */
520.1.22 by Brian Aker
Second pass of thd cleanup
1349
    plugin_var->update(session, plugin_var, tgt, &var->save_result);
1 by brian
clean slate
1350
    pthread_mutex_unlock(&LOCK_global_system_variables);
1351
  }
1352
  else
1353
  {
1354
    pthread_mutex_unlock(&LOCK_global_system_variables);
520.1.22 by Brian Aker
Second pass of thd cleanup
1355
    plugin_var->update(session, plugin_var, tgt, &var->save_result);
1 by brian
clean slate
1356
  }
1357
 return 0;
1358
}
1359
1360
1361
#define OPTION_SET_LIMITS(type, options, opt) \
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
1362
  options->var_type= type;                    \
1363
  options->def_value= (opt)->def_val;         \
1364
  options->min_value= (opt)->min_val;         \
1365
  options->max_value= (opt)->max_val;         \
1 by brian
clean slate
1366
  options->block_size= (long) (opt)->blk_sz
1367
1368
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1369
void plugin_opt_set_limits(struct option *options,
1251.2.2 by Jay Pipes
Pulls MyISAM-specific server variables into the MyISAM
1370
													 const drizzle_sys_var *opt)
1 by brian
clean slate
1371
{
1372
  options->sub_size= 0;
1373
1374
  switch (opt->flags & (PLUGIN_VAR_TYPEMASK |
520.1.21 by Brian Aker
THD -> Session rename
1375
                        PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_SessionLOCAL)) {
1 by brian
clean slate
1376
  /* global system variables */
1377
  case PLUGIN_VAR_INT:
1378
    OPTION_SET_LIMITS(GET_INT, options, (sysvar_int_t*) opt);
1379
    break;
1380
  case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED:
1381
    OPTION_SET_LIMITS(GET_UINT, options, (sysvar_uint_t*) opt);
1382
    break;
1383
  case PLUGIN_VAR_LONG:
1384
    OPTION_SET_LIMITS(GET_LONG, options, (sysvar_long_t*) opt);
1385
    break;
1386
  case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED:
896.4.16 by Stewart Smith
for getopt, replace GET_ULONG with GET_UINT32.
1387
    OPTION_SET_LIMITS(GET_ULONG_IS_FAIL, options, (sysvar_ulong_t*) opt);
1 by brian
clean slate
1388
    break;
1389
  case PLUGIN_VAR_LONGLONG:
152 by Brian Aker
longlong replacement
1390
    OPTION_SET_LIMITS(GET_LL, options, (sysvar_int64_t_t*) opt);
1 by brian
clean slate
1391
    break;
1392
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED:
151 by Brian Aker
Ulonglong to uint64_t
1393
    OPTION_SET_LIMITS(GET_ULL, options, (sysvar_uint64_t_t*) opt);
1 by brian
clean slate
1394
    break;
1395
  case PLUGIN_VAR_BOOL:
1396
    options->var_type= GET_BOOL;
1397
    options->def_value= ((sysvar_bool_t*) opt)->def_val;
1398
    break;
1399
  case PLUGIN_VAR_STR:
1400
    options->var_type= ((opt->flags & PLUGIN_VAR_MEMALLOC) ?
1401
                        GET_STR_ALLOC : GET_STR);
157 by Brian Aker
Second pass cleanup on removal of my_uint types
1402
    options->def_value= (intptr_t) ((sysvar_str_t*) opt)->def_val;
1 by brian
clean slate
1403
    break;
1404
  /* threadlocal variables */
520.1.21 by Brian Aker
THD -> Session rename
1405
  case PLUGIN_VAR_INT | PLUGIN_VAR_SessionLOCAL:
520.1.22 by Brian Aker
Second pass of thd cleanup
1406
    OPTION_SET_LIMITS(GET_INT, options, (sessionvar_int_t*) opt);
1 by brian
clean slate
1407
    break;
520.1.21 by Brian Aker
THD -> Session rename
1408
  case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_SessionLOCAL:
520.1.22 by Brian Aker
Second pass of thd cleanup
1409
    OPTION_SET_LIMITS(GET_UINT, options, (sessionvar_uint_t*) opt);
1 by brian
clean slate
1410
    break;
520.1.21 by Brian Aker
THD -> Session rename
1411
  case PLUGIN_VAR_LONG | PLUGIN_VAR_SessionLOCAL:
520.1.22 by Brian Aker
Second pass of thd cleanup
1412
    OPTION_SET_LIMITS(GET_LONG, options, (sessionvar_long_t*) opt);
1 by brian
clean slate
1413
    break;
520.1.21 by Brian Aker
THD -> Session rename
1414
  case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_SessionLOCAL:
896.4.16 by Stewart Smith
for getopt, replace GET_ULONG with GET_UINT32.
1415
    OPTION_SET_LIMITS(GET_ULONG_IS_FAIL, options, (sessionvar_ulong_t*) opt);
1 by brian
clean slate
1416
    break;
520.1.21 by Brian Aker
THD -> Session rename
1417
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_SessionLOCAL:
520.1.22 by Brian Aker
Second pass of thd cleanup
1418
    OPTION_SET_LIMITS(GET_LL, options, (sessionvar_int64_t_t*) opt);
1 by brian
clean slate
1419
    break;
520.1.21 by Brian Aker
THD -> Session rename
1420
  case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_SessionLOCAL:
520.1.22 by Brian Aker
Second pass of thd cleanup
1421
    OPTION_SET_LIMITS(GET_ULL, options, (sessionvar_uint64_t_t*) opt);
1 by brian
clean slate
1422
    break;
520.1.21 by Brian Aker
THD -> Session rename
1423
  case PLUGIN_VAR_BOOL | PLUGIN_VAR_SessionLOCAL:
1 by brian
clean slate
1424
    options->var_type= GET_BOOL;
520.1.22 by Brian Aker
Second pass of thd cleanup
1425
    options->def_value= ((sessionvar_bool_t*) opt)->def_val;
1 by brian
clean slate
1426
    break;
520.1.21 by Brian Aker
THD -> Session rename
1427
  case PLUGIN_VAR_STR | PLUGIN_VAR_SessionLOCAL:
1 by brian
clean slate
1428
    options->var_type= ((opt->flags & PLUGIN_VAR_MEMALLOC) ?
1429
                        GET_STR_ALLOC : GET_STR);
520.1.22 by Brian Aker
Second pass of thd cleanup
1430
    options->def_value= (intptr_t) ((sessionvar_str_t*) opt)->def_val;
1 by brian
clean slate
1431
    break;
1432
  default:
51.1.62 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1433
    assert(0);
1 by brian
clean slate
1434
  }
1435
  options->arg_type= REQUIRED_ARG;
1436
  if (opt->flags & PLUGIN_VAR_NOCMDARG)
1437
    options->arg_type= NO_ARG;
1438
  if (opt->flags & PLUGIN_VAR_OPCMDARG)
1439
    options->arg_type= OPT_ARG;
1440
}
1441
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1442
static int get_one_plugin_option(int, const struct option *, char *)
1 by brian
clean slate
1443
{
1444
  return 0;
1445
}
1446
1447
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
1448
static int construct_options(memory::Root *mem_root, module::Module *tmp,
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1449
                             option *options)
1 by brian
clean slate
1450
{
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1451
  
1452
  int localoptionid= 256;
1453
  const string plugin_name(tmp->getManifest().name);
1454
1455
  size_t namelen= plugin_name.size(), optnamelen;
1456
1 by brian
clean slate
1457
  char *optname, *p;
1458
  int index= 0, offset= 0;
1228.1.5 by Monty Taylor
Merged in some naming things.
1459
  drizzle_sys_var *opt, **plugin_option;
1 by brian
clean slate
1460
  st_bookmark *v;
1461
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1462
  string name(plugin_name);
1463
  transform(name.begin(), name.end(), name.begin(), ::tolower);
1464
1465
  for (string::iterator iter= name.begin(); iter != name.end(); ++iter)
1466
  {
1467
    if (*iter == '_')
1468
      *iter= '-';
1469
  }
1 by brian
clean slate
1470
1471
  /*
1472
    Two passes as the 2nd pass will take pointer addresses for use
1473
    by my_getopt and register_var() in the first pass uses realloc
1474
  */
1475
1093.3.4 by Monty Taylor
Naming cleanups.
1476
  for (plugin_option= tmp->getManifest().system_vars;
1 by brian
clean slate
1477
       plugin_option && *plugin_option; plugin_option++, index++)
1478
  {
1479
    opt= *plugin_option;
520.1.21 by Brian Aker
THD -> Session rename
1480
    if (!(opt->flags & PLUGIN_VAR_SessionLOCAL))
1 by brian
clean slate
1481
      continue;
1482
    if (!(register_var(name, opt->name, opt->flags)))
1483
      continue;
1484
    switch (opt->flags & PLUGIN_VAR_TYPEMASK) {
1485
    case PLUGIN_VAR_BOOL:
520.1.22 by Brian Aker
Second pass of thd cleanup
1486
      (((sessionvar_bool_t *)opt)->resolve)= mysql_sys_var_ptr_bool;
1 by brian
clean slate
1487
      break;
1488
    case PLUGIN_VAR_INT:
520.1.22 by Brian Aker
Second pass of thd cleanup
1489
      (((sessionvar_int_t *)opt)->resolve)= mysql_sys_var_ptr_int;
1 by brian
clean slate
1490
      break;
1491
    case PLUGIN_VAR_LONG:
520.1.22 by Brian Aker
Second pass of thd cleanup
1492
      (((sessionvar_long_t *)opt)->resolve)= mysql_sys_var_ptr_long;
1 by brian
clean slate
1493
      break;
1494
    case PLUGIN_VAR_LONGLONG:
520.1.22 by Brian Aker
Second pass of thd cleanup
1495
      (((sessionvar_int64_t_t *)opt)->resolve)= mysql_sys_var_ptr_int64_t;
1 by brian
clean slate
1496
      break;
1497
    case PLUGIN_VAR_STR:
520.1.22 by Brian Aker
Second pass of thd cleanup
1498
      (((sessionvar_str_t *)opt)->resolve)= mysql_sys_var_ptr_str;
1 by brian
clean slate
1499
      break;
1500
    default:
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
1501
      errmsg_printf(ERRMSG_LVL_ERROR, _("Unknown variable type code 0x%x in plugin '%s'."),
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1502
                      opt->flags, plugin_name.c_str());
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1503
      return(-1);
1 by brian
clean slate
1504
    };
1505
  }
1506
1093.3.4 by Monty Taylor
Naming cleanups.
1507
  for (plugin_option= tmp->getManifest().system_vars;
1 by brian
clean slate
1508
       plugin_option && *plugin_option; plugin_option++, index++)
1509
  {
1510
    switch ((opt= *plugin_option)->flags & PLUGIN_VAR_TYPEMASK) {
1511
    case PLUGIN_VAR_BOOL:
1512
      if (!opt->check)
1513
        opt->check= check_func_bool;
1514
      if (!opt->update)
1515
        opt->update= update_func_bool;
1516
      break;
1517
    case PLUGIN_VAR_INT:
1518
      if (!opt->check)
1519
        opt->check= check_func_int;
1520
      if (!opt->update)
1521
        opt->update= update_func_int;
1522
      break;
1523
    case PLUGIN_VAR_LONG:
1524
      if (!opt->check)
1525
        opt->check= check_func_long;
1526
      if (!opt->update)
1527
        opt->update= update_func_long;
1528
      break;
1529
    case PLUGIN_VAR_LONGLONG:
1530
      if (!opt->check)
152 by Brian Aker
longlong replacement
1531
        opt->check= check_func_int64_t;
1 by brian
clean slate
1532
      if (!opt->update)
152 by Brian Aker
longlong replacement
1533
        opt->update= update_func_int64_t;
1 by brian
clean slate
1534
      break;
1535
    case PLUGIN_VAR_STR:
1536
      if (!opt->check)
1537
        opt->check= check_func_str;
1538
      if (!opt->update)
1539
      {
1540
        opt->update= update_func_str;
9 by Brian Aker
Warnings cleanup
1541
        if ((opt->flags & (PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_READONLY)) == false)
1 by brian
clean slate
1542
        {
1543
          opt->flags|= PLUGIN_VAR_READONLY;
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
1544
          errmsg_printf(ERRMSG_LVL_WARN, _("Server variable %s of plugin %s was forced "
1 by brian
clean slate
1545
                            "to be read-only: string variable without "
338 by Monty Taylor
Tagged more strings.
1546
                            "update_func and PLUGIN_VAR_MEMALLOC flag"),
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1547
                            opt->name, plugin_name.c_str());
1 by brian
clean slate
1548
        }
1549
      }
1550
      break;
1551
    default:
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
1552
      errmsg_printf(ERRMSG_LVL_ERROR, _("Unknown variable type code 0x%x in plugin '%s'."),
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1553
                      opt->flags, plugin_name.c_str());
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1554
      return(-1);
1 by brian
clean slate
1555
    }
1556
520.1.21 by Brian Aker
THD -> Session rename
1557
    if ((opt->flags & (PLUGIN_VAR_NOCMDOPT | PLUGIN_VAR_SessionLOCAL))
1 by brian
clean slate
1558
                    == PLUGIN_VAR_NOCMDOPT)
1559
      continue;
1560
1561
    if (!opt->name)
1562
    {
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
1563
      errmsg_printf(ERRMSG_LVL_ERROR, _("Missing variable name in plugin '%s'."),
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1564
                    plugin_name.c_str());
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1565
      return(-1);
1 by brian
clean slate
1566
    }
1567
520.1.21 by Brian Aker
THD -> Session rename
1568
    if (!(opt->flags & PLUGIN_VAR_SessionLOCAL))
1 by brian
clean slate
1569
    {
1570
      optnamelen= strlen(opt->name);
1485 by Brian Aker
Updates to confine memroot
1571
      optname= (char*) mem_root->alloc_root(namelen + optnamelen + 2);
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1572
      sprintf(optname, "%s-%s", name.c_str(), opt->name);
1 by brian
clean slate
1573
      optnamelen= namelen + optnamelen + 1;
1574
    }
1575
    else
1576
    {
1577
      /* this should not fail because register_var should create entry */
1578
      if (!(v= find_bookmark(name, opt->name, opt->flags)))
1579
      {
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
1580
        errmsg_printf(ERRMSG_LVL_ERROR, _("Thread local variable '%s' not allocated "
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1581
                      "in plugin '%s'."), opt->name, plugin_name.c_str());
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1582
        return(-1);
1 by brian
clean slate
1583
      }
1584
1585
      *(int*)(opt + 1)= offset= v->offset;
1586
1587
      if (opt->flags & PLUGIN_VAR_NOCMDOPT)
1588
        continue;
1589
1487 by Brian Aker
More updates for memory::Root
1590
      optname= (char*) mem_root->memdup_root(v->key + 1, (optnamelen= v->name_len) + 1);
1 by brian
clean slate
1591
    }
1592
1593
    /* convert '_' to '-' */
1594
    for (p= optname; *p; p++)
1595
      if (*p == '_')
1596
        *p= '-';
1597
1598
    options->name= optname;
1599
    options->comment= opt->comment;
1600
    options->app_type= opt;
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1601
    options->id= localoptionid++;
1 by brian
clean slate
1602
1603
    plugin_opt_set_limits(options, opt);
1604
520.1.21 by Brian Aker
THD -> Session rename
1605
    if (opt->flags & PLUGIN_VAR_SessionLOCAL)
77.1.78 by Monty Taylor
One last bunch of warnings edits.
1606
      options->value= options->u_max_value= (char**)
1 by brian
clean slate
1607
        (global_system_variables.dynamic_variables_ptr + offset);
1608
    else
77.1.78 by Monty Taylor
One last bunch of warnings edits.
1609
      options->value= options->u_max_value= *(char***) (opt + 1);
1 by brian
clean slate
1610
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1611
    options++;
1 by brian
clean slate
1612
  }
1613
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1614
  return(0);
1 by brian
clean slate
1615
}
1616
1617
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
1618
static option *construct_help_options(memory::Root *mem_root, module::Module *p)
1 by brian
clean slate
1619
{
1228.1.5 by Monty Taylor
Merged in some naming things.
1620
  drizzle_sys_var **opt;
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1621
  option *opts;
482 by Brian Aker
Remove uint.
1622
  uint32_t count= EXTRA_OPTIONS;
1 by brian
clean slate
1623
1283.1.1 by Monty Taylor
Remove code which adds skip options for plugins, since that's not how we
1624
  for (opt= p->getManifest().system_vars; opt && *opt; opt++, count++) {};
1 by brian
clean slate
1625
1485 by Brian Aker
Updates to confine memroot
1626
  opts= (option*)mem_root->alloc_root((sizeof(option) * count));
1093.3.5 by Monty Taylor
Removed a dynamic array.
1627
  if (opts == NULL)
1019.1.6 by Brian Aker
A number of random cleanups.
1628
    return NULL;
1 by brian
clean slate
1629
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1630
  memset(opts, 0, sizeof(option) * count);
1 by brian
clean slate
1631
1241.10.5 by Monty Taylor
Removed hardcoded option options around myisam and memory.
1632
  if (construct_options(mem_root, p, opts))
1019.1.6 by Brian Aker
A number of random cleanups.
1633
    return NULL;
1 by brian
clean slate
1634
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1635
  return(opts);
1 by brian
clean slate
1636
}
1637
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
1638
void drizzle_add_plugin_sysvar(sys_var_pluginvar *var)
1639
{
1640
  plugin_sysvar_vec.push_back(var);
1641
}
1642
1643
void drizzle_del_plugin_sysvar()
1644
{
1645
  vector<sys_var_pluginvar *>::iterator iter= plugin_sysvar_vec.begin();
1646
  while(iter != plugin_sysvar_vec.end())
1647
  {
1648
    delete *iter;
1649
    ++iter;
1650
  }
1651
  plugin_sysvar_vec.clear();
1652
}
1 by brian
clean slate
1653
1654
/*
1655
  SYNOPSIS
1656
    test_plugin_options()
1657
    tmp_root                    temporary scratch space
1658
    plugin                      internal plugin structure
1659
    argc                        user supplied arguments
1660
    argv                        user supplied arguments
1661
    default_enabled             default plugin enable status
1662
  RETURNS:
1663
    0 SUCCESS - plugin should be enabled/loaded
1664
  NOTE:
1665
    Requires that a write-lock is held on LOCK_system_variables_hash
1666
*/
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1667
static int test_plugin_options(memory::Root *module_root,
1668
                               module::Module *test_module,
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
1669
                               int *argc, char **argv,
1670
                               po::options_description &long_options)
1 by brian
clean slate
1671
{
1672
  struct sys_var_chain chain= { NULL, NULL };
1228.1.5 by Monty Taylor
Merged in some naming things.
1673
  drizzle_sys_var **opt;
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1674
  option *opts= NULL;
1 by brian
clean slate
1675
  int error;
1228.1.5 by Monty Taylor
Merged in some naming things.
1676
  drizzle_sys_var *o;
1 by brian
clean slate
1677
  struct st_bookmark *var;
482 by Brian Aker
Remove uint.
1678
  uint32_t len, count= EXTRA_OPTIONS;
1 by brian
clean slate
1679
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1680
  if (test_module->getManifest().init_options != NULL)
1681
  {
1682
    po::options_description module_options("Options used by plugins");
1683
    module::option_context opt_ctx(test_module->getName(),
1684
                                   module_options.add_options());
1685
    test_module->getManifest().init_options(opt_ctx);
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
1686
    long_options.add(module_options);
1625.1.7 by Monty Taylor
Added support for program_options based commandline argument passing.
1687
1626.2.2 by Monty Taylor
Moved ownership of the variable_map to the module (avoids copying, also
1688
    po::variables_map &vm= test_module->getVariableMap();
1625.1.7 by Monty Taylor
Added support for program_options based commandline argument passing.
1689
1690
    po::parsed_options parsed= po::command_line_parser(*argc, argv).
1691
      options(module_options).allow_unregistered().run();
1692
1693
    po::store(parsed, vm);
1694
1695
    vector<string> to_pass_further= po::collect_unrecognized(parsed.options,
1696
                                                             po::include_positional);
1697
1698
    /* Copy the left over options back into argv for further processing.
1699
       Once the core is using program options, this whole thing will be done
1700
       differently.
1701
     */
1702
    for (vector<string>::iterator iter= to_pass_further.begin();
1703
         iter != to_pass_further.end();
1704
         ++iter)
1705
    {
1706
      size_t pos= iter-to_pass_further.begin()+1;
1707
      memcpy(argv[pos], (*iter).c_str(), (*iter).size()+1);
1708
      cout << "arg: *" << argv[pos] << "*" << endl;
1709
    }
1710
    *argc= to_pass_further.size() + 1;
1711
1712
    po::notify(vm);
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1713
  }
1625.1.7 by Monty Taylor
Added support for program_options based commandline argument passing.
1714
  else
1715
  {
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1716
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
1717
    for (opt= test_module->getManifest().system_vars; opt && *opt; opt++)
1718
    {
1719
      count++;
1720
    }
1721
1722
    if (count > EXTRA_OPTIONS || (*argc > 1))
1723
    {
1724
      if (!(opts= (option*) module_root->alloc_root(sizeof(option) * count)))
1725
      {
1726
        errmsg_printf(ERRMSG_LVL_ERROR,
1727
                      _("Out of memory for plugin '%s'."),
1728
                      test_module->getName().c_str());
1729
        return(-1);
1730
      }
1731
      memset(opts, 0, sizeof(option) * count);
1732
1733
      if (construct_options(module_root, test_module, opts))
1734
      {
1735
        errmsg_printf(ERRMSG_LVL_ERROR,
1736
                      _("Bad options for plugin '%s'."),
1737
                      test_module->getName().c_str());
1738
        return(-1);
1739
      }
1740
1741
      error= handle_options(argc, &argv, opts, get_one_plugin_option);
1742
      (*argc)++; /* add back one for the program name */
1743
1744
      if (error)
1745
      {
1746
        errmsg_printf(ERRMSG_LVL_ERROR,
1747
                      _("Parsing options for plugin '%s' failed."),
1748
                      test_module->getName().c_str());
1749
        goto err;
1750
      }
1751
    }
1625.1.7 by Monty Taylor
Added support for program_options based commandline argument passing.
1752
  }
1 by brian
clean slate
1753
1754
  error= 1;
1755
779.3.53 by Monty Taylor
Revert the fail.
1756
  {
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1757
    for (opt= test_module->getManifest().system_vars; opt && *opt; opt++)
779.3.53 by Monty Taylor
Revert the fail.
1758
    {
1106.1.2 by Brian Aker
Additional fixes from Monty
1759
      sys_var *v;
779.3.53 by Monty Taylor
Revert the fail.
1760
      if (((o= *opt)->flags & PLUGIN_VAR_NOSYSVAR))
1761
        continue;
1762
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1763
      if ((var= find_bookmark(test_module->getName(), o->name, o->flags)))
1764
      {
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
1765
        v= new sys_var_pluginvar(var->key + 1, o);
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1766
      }
779.3.53 by Monty Taylor
Revert the fail.
1767
      else
1768
      {
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1769
        len= test_module->getName().length() + strlen(o->name) + 2;
1770
        string vname(test_module->getName());
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
1771
        vname.push_back('-');
1772
        vname.append(o->name);
1773
        transform(vname.begin(), vname.end(), vname.begin(), ::tolower);
1774
        string::iterator p= vname.begin();      
1775
        while  (p != vname.end())
1776
        {
779.3.53 by Monty Taylor
Revert the fail.
1777
          if (*p == '-')
1778
            *p= '_';
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
1779
          ++p;
1780
        }
779.3.53 by Monty Taylor
Revert the fail.
1781
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
1782
        v= new sys_var_pluginvar(vname, o);
779.3.53 by Monty Taylor
Revert the fail.
1783
      }
1784
      assert(v); /* check that an object was actually constructed */
1785
1101.1.27 by Monty Taylor
Fixed the plugin string valgrind leak.
1786
      drizzle_add_plugin_sysvar(static_cast<sys_var_pluginvar *>(v));
779.3.53 by Monty Taylor
Revert the fail.
1787
      /*
1788
        Add to the chain of variables.
1789
        Done like this for easier debugging so that the
1790
        pointer to v is not lost on optimized builds.
1791
      */
1792
      v->chain_sys_var(&chain);
1793
    }
1794
    if (chain.first)
1795
    {
1055.2.20 by Jay Pipes
Refactors sys_var class -- doxygenates and documents the class members and functions. Protects all member variables and adds public getters/setters.
1796
      chain.last->setNext(NULL);
779.3.53 by Monty Taylor
Revert the fail.
1797
      if (mysql_add_sys_var_chain(chain.first, NULL))
1798
      {
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1799
        errmsg_printf(ERRMSG_LVL_ERROR,
1800
                      _("Plugin '%s' has conflicting system variables"),
1801
                      test_module->getName().c_str());
779.3.53 by Monty Taylor
Revert the fail.
1802
        goto err;
1803
      }
1625.1.5 by Monty Taylor
Put in first bits to use program_options for plugin loading in parallel to
1804
      test_module->system_vars= chain.first;
779.3.53 by Monty Taylor
Revert the fail.
1805
    }
1806
    return(0);
1807
  }
1808
1 by brian
clean slate
1809
err:
1810
  if (opts)
1811
    my_cleanup_options(opts);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1812
  return(error);
1 by brian
clean slate
1813
}
1814
1815
1816
/****************************************************************************
1817
  Help Verbose text with Plugin System Variables
1818
****************************************************************************/
1819
1093.3.5 by Monty Taylor
Removed a dynamic array.
1820
class OptionCmp
1821
{
1822
public:
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1823
  bool operator() (const option &a, const option &b)
1093.3.5 by Monty Taylor
Removed a dynamic array.
1824
  {
1825
    return my_strcasecmp(&my_charset_utf8_general_ci, a.name, b.name);
1826
  }
1827
};
1828
1829
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
1830
void my_print_help_inc_plugins(option *main_options,
1831
                               po::options_description &long_options)
1093.3.5 by Monty Taylor
Removed a dynamic array.
1832
{
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
1833
  module::Registry &registry= module::Registry::singleton();
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1834
  vector<option> all_options;
1485 by Brian Aker
Updates to confine memroot
1835
  memory::Root mem_root(4096);
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1836
  option *opt= NULL;
1 by brian
clean slate
1837
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
1838
1 by brian
clean slate
1839
  if (initialized)
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
1840
  {
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
1841
    std::map<std::string, module::Module *>::const_iterator modules=
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
1842
      registry.getModulesMap().begin();
1843
    
1844
    while (modules != registry.getModulesMap().end())
1 by brian
clean slate
1845
    {
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
1846
      module::Module *p= (*modules).second;
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
1847
      ++modules;
1 by brian
clean slate
1848
1633.1.3 by Monty Taylor
Removed duplicate entries from the help output.
1849
      /* If we have an init_options function, we are registering
1850
         commmand line options that way, so don't do them this way */
1851
      if (p->getManifest().init_options != NULL)
1852
        continue;
1853
1093.3.5 by Monty Taylor
Removed a dynamic array.
1854
      if (p->getManifest().system_vars == NULL)
1855
        continue;
1856
1857
      opt= construct_help_options(&mem_root, p);
1858
      if (opt == NULL)
1 by brian
clean slate
1859
        continue;
1860
1861
      /* Only options with a non-NULL comment are displayed in help text */
1862
      for (;opt->id; opt++)
1093.3.5 by Monty Taylor
Removed a dynamic array.
1863
      {
1 by brian
clean slate
1864
        if (opt->comment)
1093.3.5 by Monty Taylor
Removed a dynamic array.
1865
        {
1866
          all_options.push_back(*opt);
1867
          
1868
        }
1869
      }
1 by brian
clean slate
1870
    }
1228.1.3 by Monty Taylor
All of the outstanding plugin loader system cleanups:
1871
  }
1 by brian
clean slate
1872
1873
  for (;main_options->id; main_options++)
1093.3.5 by Monty Taylor
Removed a dynamic array.
1874
  {
1875
    if (main_options->comment)
1876
    {
1877
      all_options.push_back(*main_options);
1878
    }
1879
  }
1 by brian
clean slate
1880
1093.3.5 by Monty Taylor
Removed a dynamic array.
1881
  /** 
1410.3.4 by Djellel E. Difallah
update references to old my_'s
1882
   * @TODO: Fix the option building so that it doens't break sort
1093.3.5 by Monty Taylor
Removed a dynamic array.
1883
   *
1884
   * sort(all_options.begin(), all_options.end(), OptionCmp());
1885
   */
1 by brian
clean slate
1886
1887
  /* main_options now points to the empty option terminator */
1093.3.5 by Monty Taylor
Removed a dynamic array.
1888
  all_options.push_back(*main_options);
1889
1890
  my_print_help(&*(all_options.begin()));
1633.1.1 by Monty Taylor
Added in support for program_options output in --help output.
1891
  cout << long_options << endl;
1093.3.5 by Monty Taylor
Removed a dynamic array.
1892
  my_print_variables(&*(all_options.begin()));
1893
1487 by Brian Aker
More updates for memory::Root
1894
  mem_root.free_root(MYF(0));
1 by brian
clean slate
1895
}
1896
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1897
} /* namespace drizzled */
1530.2.3 by Monty Taylor
Changed the builtin plugin code path to work exactly the same as dynamic.
1898
1899