drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
loader.cc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include <config.h>
17
18#include <dlfcn.h>
19
20#include <cstdio>
21#include <string>
22#include <vector>
23#include <map>
24#include <algorithm>
25#include <iostream>
26
27#include <boost/program_options.hpp>
28
29#include <drizzled/option.h>
30#include <drizzled/internal/m_string.h>
31
32#include <drizzled/plugin.h>
33#include <drizzled/module/load_list.h>
34#include <drizzled/module/library.h>
35#include <drizzled/module/registry.h>
37#include <drizzled/sql_parse.h>
38#include <drizzled/show.h>
39#include <drizzled/cursor.h>
40#include <drizzled/set_var.h>
41#include <drizzled/session.h>
42#include <drizzled/item/null.h>
43#include <drizzled/error.h>
44#include <drizzled/gettext.h>
45#include <drizzled/errmsg_print.h>
46#include <drizzled/pthread_globals.h>
47#include <drizzled/util/tokenize.h>
48#include <drizzled/system_variables.h>
49
50namespace po=boost::program_options;
51
52using namespace std;
53
55typedef drizzled::module::Manifest drizzled_builtin_list[];
56extern drizzled_builtin_list PANDORA_BUILTIN_SYMBOLS_LIST;
57extern drizzled_builtin_list PANDORA_BUILTIN_LOAD_SYMBOLS_LIST;
58drizzled::module::Manifest *drizzled_builtins[]=
59{
60 PANDORA_BUILTIN_SYMBOLS_LIST, NULL
61};
62drizzled::module::Manifest *drizzled_load_builtins[]=
63{
64 PANDORA_BUILTIN_LOAD_SYMBOLS_LIST, NULL
65};
66
67namespace drizzled {
68
69
70typedef vector<string> PluginOptions;
71static PluginOptions opt_plugin_load;
72static PluginOptions opt_plugin_add;
73static PluginOptions opt_plugin_remove;
74const char *builtin_plugins= PANDORA_BUILTIN_LIST;
75const char *builtin_load_plugins= PANDORA_BUILTIN_LOAD_LIST;
76
77/* Note that 'int version' must be the first field of every plugin
78 sub-structure (plugin->info).
79*/
80
81static bool initialized= false;
82
83
84static bool reap_needed= false;
85
86/*
87 write-lock on LOCK_system_variables_hash is required before modifying
88 the following variables/structures
89*/
90static memory::Root plugin_mem_root(4096);
91static uint32_t global_variables_dynamic_size= 0;
92
93
94/*
95 hidden part of opaque value passed to variable check functions.
96 Used to provide a object-like structure to non C++ consumers.
97*/
99{
100 Item *item;
101};
102
103/* prototypes */
104static void plugin_prune_list(vector<string> &plugin_list, const vector<string> &plugins_to_remove);
105static bool plugin_load_list(module::Registry &registry,
106 memory::Root *tmp_root,
107 const set<string> &plugin_list,
108 po::options_description &long_options,
109 bool builtin= false);
110static int test_plugin_options(memory::Root*, module::Module*, po::options_description&long_options);
111static void unlock_variables(Session *session, drizzle_system_variables *vars);
112static void cleanup_variables(drizzle_system_variables *vars);
113
114
115/****************************************************************************
116 Plugin support code
117****************************************************************************/
118
119
120
121
122/*
123 NOTE
124 Requires that a write-lock is held on LOCK_system_variables_hash
125*/
126static bool plugin_add(module::Registry &registry, memory::Root *tmp_root,
127 module::Library *library,
128 po::options_description &long_options)
129{
130 if (! initialized)
131 return true;
132
133 if (registry.find(library->getName()))
134 {
135 errmsg_printf(error::WARN, ER(ER_PLUGIN_EXISTS),
136 library->getName().c_str());
137 return false;
138 }
139
140 /* Find plugin by name */
141 const module::Manifest *manifest= library->getManifest();
142
143 if (registry.find(manifest->name))
144 {
145 errmsg_printf(error::ERROR,
146 _("Plugin '%s' contains the name '%s' in its manifest, which "
147 "has already been registered.\n"),
148 library->getName().c_str(),
149 manifest->name);
150 return true;
151 }
152
153 module::Module* tmp= new module::Module(manifest, library);
154
155 if (!test_plugin_options(tmp_root, tmp, long_options))
156 {
157 registry.add(tmp);
158 return false;
159 }
160 errmsg_printf(error::ERROR, ER(ER_CANT_FIND_DL_ENTRY),
161 library->getName().c_str());
162 return true;
163}
164
165
166static void reap_plugins(module::Registry &registry)
167{
168 for (module::Registry::ModuleMap::const_reference module : registry.getModulesMap())
169 delete module.second;
170}
171
172
173static bool plugin_initialize(module::Registry &registry,
174 module::Module *module)
175{
176 assert(module->isInited == false);
177
178 module::Context loading_context(registry, module);
179 if (module->getManifest().init)
180 {
181 if (module->getManifest().init(loading_context))
182 {
183 errmsg_printf(error::ERROR,
184 _("Plugin '%s' init function returned error.\n"),
185 module->getName().c_str());
186 return true;
187 }
188 }
189 module->isInited= true;
190 return false;
191}
192
193static void compose_plugin_options(vector<string> &target,
194 vector<string> options)
195{
196 for (vector<string>::reference it : options)
197 tokenize(it, target, ",", true);
198 for (vector<string>::reference it : target)
199 std::replace(it.begin(), it.end(), '-', '_');
200}
201
202void compose_plugin_add(const vector<string>& options)
203{
204 compose_plugin_options(opt_plugin_add, options);
205}
206
207void compose_plugin_remove(const vector<string>& options)
208{
209 compose_plugin_options(opt_plugin_remove, options);
210}
211
212void notify_plugin_load(const string& in_plugin_load)
213{
214 tokenize(in_plugin_load, opt_plugin_load, ",", true);
215}
216
217/*
218 The logic is that we first load and initialize all compiled in plugins.
219 From there we load up the dynamic types (assuming we have not been told to
220 skip this part).
221
222 Finally we initialize everything, aka the dynamic that have yet to initialize.
223*/
224bool plugin_init(module::Registry &registry,
225 po::options_description &long_options)
226{
227 if (initialized)
228 return false;
229
230 initialized= true;
231
232 PluginOptions builtin_load_list;
233 tokenize(builtin_load_plugins, builtin_load_list, ",", true);
234
235 PluginOptions builtin_list;
236 tokenize(builtin_plugins, builtin_list, ",", true);
237
238 bool load_failed= false;
239
240 if (opt_plugin_add.size() > 0)
241 {
242 for (PluginOptions::iterator iter= opt_plugin_add.begin();
243 iter != opt_plugin_add.end();
244 ++iter)
245 {
246 if (find(builtin_list.begin(),
247 builtin_list.end(), *iter) != builtin_list.end())
248 {
249 builtin_load_list.push_back(*iter);
250 }
251 else
252 {
253 opt_plugin_load.push_back(*iter);
254 }
255 }
256 }
257
258 if (opt_plugin_remove.size() > 0)
259 {
260 plugin_prune_list(opt_plugin_load, opt_plugin_remove);
261 plugin_prune_list(builtin_load_list, opt_plugin_remove);
262 }
263
264 memory::Root tmp_root(4096);
265 /*
266 First we register builtin plugins
267 */
268 const set<string> builtin_list_set(builtin_load_list.begin(),
269 builtin_load_list.end());
270 load_failed= plugin_load_list(registry, &tmp_root,
271 builtin_list_set, long_options, true);
272 if (load_failed)
273 {
274 tmp_root.free_root(MYF(0));
275 return true;
276 }
277
278 /* Uniquify the list */
279 const set<string> plugin_list_set(opt_plugin_load.begin(),
280 opt_plugin_load.end());
281
282 /* Register all dynamic plugins */
283 load_failed= plugin_load_list(registry, &tmp_root,
284 plugin_list_set, long_options);
285 if (load_failed)
286 {
287 tmp_root.free_root(MYF(0));
288 return true;
289 }
290
291 tmp_root.free_root(MYF(0));
292
293 return false;
294}
295
296bool plugin_finalize(module::Registry &registry)
297{
298 /*
299 Now we initialize all remaining plugins
300 */
301 for (module::Registry::ModuleList::const_reference module : registry.getList())
302 {
303 if (not module->isInited && plugin_initialize(registry, module))
304 {
305 registry.remove(module);
306 delete module;
307 return true;
308 }
309 }
310 for (plugin::Plugin::map::const_reference value : registry.getPluginsMap())
311 {
312 value.second->prime();
313 }
314 return false;
315}
316
317/*
318 Window of opportunity for plugins to issue any queries with the database up and running but with no user's connected.
319*/
320void plugin_startup_window(module::Registry &registry, drizzled::Session &session)
321{
322 for (plugin::Plugin::map::const_reference value : registry.getPluginsMap())
323 {
324 value.second->startup(session);
325 }
326}
327
328class PrunePlugin
329{
330 const string to_match;
331public:
332 explicit PrunePlugin(const string &match_in) :
333 to_match(match_in)
334 { }
335
336 bool operator()(const string &match_against)
337 {
338 return match_against == to_match;
339 }
340};
341
342static void plugin_prune_list(vector<string> &plugin_list,
343 const vector<string> &plugins_to_remove)
344{
345 for (vector<string>::const_iterator iter= plugins_to_remove.begin();
346 iter != plugins_to_remove.end();
347 ++iter)
348 {
349 plugin_list.erase(remove_if(plugin_list.begin(),
350 plugin_list.end(),
351 PrunePlugin(*iter)),
352 plugin_list.end());
353 }
354}
355
356/*
357 called only by plugin_init()
358*/
359static bool plugin_load_list(module::Registry &registry,
360 memory::Root *tmp_root,
361 const set<string> &plugin_list,
362 po::options_description &long_options,
363 bool builtin)
364{
365 for (const string& plugin_name : plugin_list)
366 {
367 module::Library* library= registry.addLibrary(plugin_name, builtin);
368 if (library == NULL)
369 {
370 errmsg_printf(error::ERROR,
371 _("Couldn't load plugin library named '%s'.\n"),
372 plugin_name.c_str());
373 return true;
374 }
375
376 tmp_root->free_root(MYF(memory::MARK_BLOCKS_FREE));
377 if (plugin_add(registry, tmp_root, library, long_options))
378 {
379 registry.removeLibrary(plugin_name);
380 errmsg_printf(error::ERROR,
381 _("Couldn't load plugin named '%s'.\n"),
382 plugin_name.c_str());
383 return true;
384 }
385 }
386 return false;
387}
388
389void module_shutdown(module::Registry &registry)
390{
391 if (initialized)
392 {
393 reap_needed= true;
394
395 reap_plugins(registry);
396 unlock_variables(NULL, &global_system_variables);
397 unlock_variables(NULL, &max_system_variables);
398
399 cleanup_variables(&global_system_variables);
400 cleanup_variables(&max_system_variables);
401
402 initialized= 0;
403 }
404
405 /* Dispose of the memory */
406 plugin_mem_root.free_root(MYF(0));
407
408 global_variables_dynamic_size= 0;
409}
410
411
412/****************************************************************************
413 System Variables support
414****************************************************************************/
415
416
417
418void plugin_sessionvar_init(Session *session)
419{
420 session->variables.storage_engine= NULL;
421 cleanup_variables(&session->variables);
422
423 session->variables= global_system_variables;
424 session->variables.storage_engine= NULL;
425
426 /* we are going to allocate these lazily */
427 session->variables.dynamic_variables_version= 0;
428 session->variables.dynamic_variables_size= 0;
429 session->variables.dynamic_variables_ptr= 0;
430
431 session->variables.storage_engine= global_system_variables.storage_engine;
432}
433
434
435/*
436 Unlocks all system variables which hold a reference
437*/
438static void unlock_variables(Session *, struct drizzle_system_variables *vars)
439{
440 vars->storage_engine= NULL;
441}
442
443
444/*
445 Frees memory used by system variables
446
447 Unlike plugin_vars_free_values() it frees all variables of all plugins,
448 it's used on shutdown.
449*/
450static void cleanup_variables(drizzle_system_variables *vars)
451{
452 assert(vars->storage_engine == NULL);
453
454 free(vars->dynamic_variables_ptr);
455 vars->dynamic_variables_ptr= NULL;
456 vars->dynamic_variables_size= 0;
457 vars->dynamic_variables_version= 0;
458}
459
460
461void plugin_sessionvar_cleanup(Session *session)
462{
463 unlock_variables(session, &session->variables);
464 cleanup_variables(&session->variables);
465}
466
467
468
469/*
470 SYNOPSIS
471 test_plugin_options()
472 tmp_root temporary scratch space
473 plugin internal plugin structure
474 default_enabled default plugin enable status
475 RETURNS:
476 0 SUCCESS - plugin should be enabled/loaded
477 NOTE:
478 Requires that a write-lock is held on LOCK_system_variables_hash
479*/
480static int test_plugin_options(memory::Root *,
481 module::Module *test_module,
482 po::options_description &long_options)
483{
484
485 if (test_module->getManifest().init_options != NULL)
486 {
487 string plugin_section_title("Options used by ");
488 plugin_section_title.append(test_module->getName());
489 po::options_description module_options(plugin_section_title);
490 module::option_context opt_ctx(test_module->getName(),
491 module_options.add_options());
492 test_module->getManifest().init_options(opt_ctx);
493 long_options.add(module_options);
494 }
495
496 return 0;
497}
498
499
500/****************************************************************************
501 Help Verbose text with Plugin System Variables
502****************************************************************************/
503
505{
506public:
507 bool operator() (const option &a, const option &b)
508 {
509 return my_charset_utf8_general_ci.strcasecmp(a.name, b.name);
510 }
511};
512
513
515{
516 module::Registry &registry= module::Registry::singleton();
517 vector<option> all_options;
518 memory::Root mem_root(4096);
519
520
521 if (initialized)
522 {
523 std::map<std::string, module::Module *>::const_iterator modules=
524 registry.getModulesMap().begin();
525
526 while (modules != registry.getModulesMap().end())
527 {
528 module::Module *p= (*modules).second;
529 ++modules;
530
531 /* If we have an init_options function, we are registering
532 commmand line options that way, so don't do them this way */
533 if (p->getManifest().init_options != NULL)
534 continue;
535
536 }
537 }
538
539 for (;main_options->id; main_options++)
540 {
541 if (main_options->comment)
542 {
543 all_options.push_back(*main_options);
544 }
545 }
546
552
553 /* main_options now points to the empty option terminator */
554 all_options.push_back(*main_options);
555
556 my_print_help(&*(all_options.begin()));
557
558 mem_root.free_root(MYF(0));
559}
560
561} /* namespace drizzled */
562
void free_root(myf MyFLAGS)
Deallocate everything used by alloc_root or just move used blocks to free list if called with MY_USED...
Definition root.cc:287
TODO: Rename this file - func.h is stupid.
void my_print_help_inc_plugins(option *main_options)
Definition loader.cc:514
STL namespace.
An Proxy Wrapper around options_description_easy_init.