-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsql_plugin.cc
4558 lines (3946 loc) · 139 KB
/
sql_plugin.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2005, 2018, Oracle and/or its affiliates.
Copyright (c) 2010, 2020, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
#include "sql_plugin.h" // SHOW_MY_BOOL
#include "sql_priv.h"
#include "unireg.h"
#include "sql_class.h" // set_var.h: THD
#include "sys_vars_shared.h"
#include "sql_locale.h"
#include "sql_plugin.h"
#include "sql_parse.h" // check_table_access
#include "sql_base.h" // close_mysql_tables
#include "key.h" // key_copy
#include "sql_table.h"
#include "sql_show.h" // remove_status_vars, add_status_vars
#include "strfunc.h" // find_set
#include "records.h" // init_read_record, end_read_record
#include <my_pthread.h>
#include <my_getopt.h>
#include "sql_audit.h"
#include <mysql/plugin_auth.h>
#include "lock.h" // MYSQL_LOCK_IGNORE_TIMEOUT
#include <mysql/plugin_auth.h>
#include <mysql/plugin_password_validation.h>
#include <mysql/plugin_encryption.h>
#include <mysql/plugin_data_type.h>
#include <mysql/plugin_function.h>
#include "sql_plugin_compat.h"
#include "wsrep_mysqld.h"
static PSI_memory_key key_memory_plugin_mem_root;
static PSI_memory_key key_memory_plugin_int_mem_root;
static PSI_memory_key key_memory_mysql_plugin;
static PSI_memory_key key_memory_mysql_plugin_dl;
static PSI_memory_key key_memory_plugin_bookmark;
#ifdef HAVE_LINK_H
#include <link.h>
#endif
extern struct st_maria_plugin *mysql_optional_plugins[];
extern struct st_maria_plugin *mysql_mandatory_plugins[];
/**
@note The order of the enumeration is critical.
@see construct_options
*/
const char *global_plugin_typelib_names[]=
{ "OFF", "ON", "FORCE", "FORCE_PLUS_PERMANENT", NULL };
static TYPELIB global_plugin_typelib=
CREATE_TYPELIB_FOR(global_plugin_typelib_names);
static I_List<i_string> opt_plugin_load_list;
I_List<i_string> *opt_plugin_load_list_ptr= &opt_plugin_load_list;
char *opt_plugin_dir_ptr;
char opt_plugin_dir[FN_REFLEN];
ulong plugin_maturity;
static LEX_CSTRING MYSQL_PLUGIN_NAME= {STRING_WITH_LEN("plugin") };
/*
not really needed now, this map will become essential when we add more
maturity levels. We cannot change existing maturity constants,
so the next value - even if it will be MariaDB_PLUGIN_MATURITY_VERY_BUGGY -
will inevitably be larger than MariaDB_PLUGIN_MATURITY_STABLE.
To be able to compare them we use this mapping array
*/
uint plugin_maturity_map[]=
{ 0, 1, 2, 3, 4, 5, 6 };
/*
When you add a new plugin type, add both a string and make sure that the
init and deinit array are correctly updated.
*/
const LEX_CSTRING plugin_type_names[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
{ STRING_WITH_LEN("UDF") },
{ STRING_WITH_LEN("STORAGE ENGINE") },
{ STRING_WITH_LEN("FTPARSER") },
{ STRING_WITH_LEN("DAEMON") },
{ STRING_WITH_LEN("INFORMATION SCHEMA") },
{ STRING_WITH_LEN("AUDIT") },
{ STRING_WITH_LEN("REPLICATION") },
{ STRING_WITH_LEN("AUTHENTICATION") },
{ STRING_WITH_LEN("PASSWORD VALIDATION") },
{ STRING_WITH_LEN("ENCRYPTION") },
{ STRING_WITH_LEN("DATA TYPE") },
{ STRING_WITH_LEN("FUNCTION") }
};
extern int initialize_schema_table(void *plugin);
extern int finalize_schema_table(void *plugin);
extern int initialize_audit_plugin(void *plugin);
extern int finalize_audit_plugin(void *plugin);
extern int initialize_encryption_plugin(void *plugin);
extern int finalize_encryption_plugin(void *plugin);
extern int initialize_data_type_plugin(void *plugin);
/*
The number of elements in both plugin_type_initialize and
plugin_type_deinitialize should equal to the number of plugins
defined.
*/
plugin_type_init plugin_type_initialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
0, ha_initialize_handlerton, 0, 0,initialize_schema_table,
initialize_audit_plugin, 0, 0, 0, initialize_encryption_plugin,
initialize_data_type_plugin, 0
};
plugin_type_init plugin_type_deinitialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
0, ha_finalize_handlerton, 0, 0, finalize_schema_table,
finalize_audit_plugin, 0, 0, 0, finalize_encryption_plugin, 0,
0 // FUNCTION
};
/*
Defines in which order plugin types have to be initialized.
Essentially, we want to initialize MYSQL_KEY_MANAGEMENT_PLUGIN before
MYSQL_STORAGE_ENGINE_PLUGIN, and that before MYSQL_INFORMATION_SCHEMA_PLUGIN
*/
static int plugin_type_initialization_order[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
MYSQL_DAEMON_PLUGIN,
MariaDB_ENCRYPTION_PLUGIN,
MariaDB_DATA_TYPE_PLUGIN,
MariaDB_FUNCTION_PLUGIN,
MYSQL_STORAGE_ENGINE_PLUGIN,
MYSQL_INFORMATION_SCHEMA_PLUGIN,
MYSQL_FTPARSER_PLUGIN,
MYSQL_AUTHENTICATION_PLUGIN,
MariaDB_PASSWORD_VALIDATION_PLUGIN,
MYSQL_AUDIT_PLUGIN,
MYSQL_REPLICATION_PLUGIN,
MYSQL_UDF_PLUGIN
};
#ifdef HAVE_DLOPEN
static const char *plugin_interface_version_sym=
"_mysql_plugin_interface_version_";
static const char *sizeof_st_plugin_sym=
"_mysql_sizeof_struct_st_plugin_";
static const char *plugin_declarations_sym= "_mysql_plugin_declarations_";
static int min_plugin_interface_version= MYSQL_PLUGIN_INTERFACE_VERSION & ~0xFF;
static const char *maria_plugin_interface_version_sym=
"_maria_plugin_interface_version_";
static const char *maria_sizeof_st_plugin_sym=
"_maria_sizeof_struct_st_plugin_";
static const char *maria_plugin_declarations_sym=
"_maria_plugin_declarations_";
static int min_maria_plugin_interface_version=
MARIA_PLUGIN_INTERFACE_VERSION & ~0xFF;
#endif
/* Note that 'int version' must be the first field of every plugin
sub-structure (plugin->info).
*/
static int min_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
0x0000,
MYSQL_HANDLERTON_INTERFACE_VERSION,
MYSQL_FTPARSER_INTERFACE_VERSION,
MYSQL_DAEMON_INTERFACE_VERSION,
MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION,
MYSQL_AUDIT_INTERFACE_VERSION,
MYSQL_REPLICATION_INTERFACE_VERSION,
MIN_AUTHENTICATION_INTERFACE_VERSION,
MariaDB_PASSWORD_VALIDATION_INTERFACE_VERSION,
MariaDB_ENCRYPTION_INTERFACE_VERSION,
MariaDB_DATA_TYPE_INTERFACE_VERSION,
MariaDB_FUNCTION_INTERFACE_VERSION
};
static int cur_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
0x0000, /* UDF: not implemented */
MYSQL_HANDLERTON_INTERFACE_VERSION,
MYSQL_FTPARSER_INTERFACE_VERSION,
MYSQL_DAEMON_INTERFACE_VERSION,
MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION,
MYSQL_AUDIT_INTERFACE_VERSION,
MYSQL_REPLICATION_INTERFACE_VERSION,
MYSQL_AUTHENTICATION_INTERFACE_VERSION,
MariaDB_PASSWORD_VALIDATION_INTERFACE_VERSION,
MariaDB_ENCRYPTION_INTERFACE_VERSION,
MariaDB_DATA_TYPE_INTERFACE_VERSION,
MariaDB_FUNCTION_INTERFACE_VERSION
};
static struct
{
Lex_ident_plugin plugin_name;
enum enum_plugin_load_option override;
} override_plugin_load_policy[]={
/*
If the performance schema is compiled in,
treat the storage engine plugin as 'mandatory',
to suppress any plugin-level options such as '--performance-schema'.
This is specific to the performance schema, and is done on purpose:
the server-level option '--performance-schema' controls the overall
performance schema initialization, which consists of much more that
the underlying storage engine initialization.
See mysqld.cc, set_vars.cc.
Suppressing ways to interfere directly with the storage engine alone
prevents awkward situations where:
- the user wants the performance schema functionality, by using
'--enable-performance-schema' (the server option),
- yet disable explicitly a component needed for the functionality
to work, by using '--skip-performance-schema' (the plugin)
*/
{ "performance_schema"_Lex_ident_plugin, PLUGIN_FORCE }
/* we disable few other plugins by default */
,{ "feedback"_Lex_ident_plugin, PLUGIN_OFF }
};
/* support for Services */
#include "sql_plugin_services.inl"
/*
A mutex LOCK_plugin must be acquired before accessing the
following variables/structures.
We are always manipulating ref count, so a rwlock here is unnecessary.
*/
mysql_mutex_t LOCK_plugin;
static DYNAMIC_ARRAY plugin_dl_array;
static DYNAMIC_ARRAY plugin_array;
static HASH plugin_hash[MYSQL_MAX_PLUGIN_TYPE_NUM];
static MEM_ROOT plugin_mem_root;
static bool reap_needed= false;
volatile int global_plugin_version= 1;
static bool initialized= 0;
ulong dlopen_count;
/*
write-lock on LOCK_system_variables_hash is required before modifying
the following variables/structures
*/
static MEM_ROOT plugin_vars_mem_root;
static size_t global_variables_dynamic_size= 0;
static HASH bookmark_hash;
/*
hidden part of opaque value passed to variable check functions.
Used to provide a object-like structure to non C++ consumers.
*/
struct st_item_value_holder : public st_mysql_value
{
Item *item;
};
/*
stored in bookmark_hash, this structure is never removed from the
hash and is used to mark a single offset for a thd local variable
even if plugins have been uninstalled and reinstalled, repeatedly.
This structure is allocated from plugin_mem_root.
The key format is as follows:
1 byte - variable type code
name_len bytes - variable name
'\0' - end of key
*/
struct st_bookmark
{
uint name_len;
int offset;
uint version;
bool loaded;
char key[1];
};
/*
skeleton of a plugin variable - portion of structure common to all.
*/
struct st_mysql_sys_var
{
MYSQL_PLUGIN_VAR_HEADER;
};
enum install_status { INSTALL_GOOD, INSTALL_FAIL_WARN_OK, INSTALL_FAIL_NOT_OK };
/*
sys_var class for access to all plugin variables visible to the user
*/
class sys_var_pluginvar: public sys_var, public Sql_alloc
{
public:
struct st_plugin_int *plugin;
struct st_mysql_sys_var *plugin_var;
sys_var_pluginvar(sys_var_chain *chain, const char *name_arg,
st_plugin_int *p, st_mysql_sys_var *plugin_var_arg,
const char *substitute);
sys_var_pluginvar *cast_pluginvar() override { return this; }
uchar* real_value_ptr(THD *thd, enum_var_type type) const;
TYPELIB* plugin_var_typelib(void) const;
const uchar* do_value_ptr(THD *thd, enum_var_type type, const LEX_CSTRING *base) const;
const uchar* session_value_ptr(THD *thd, const LEX_CSTRING *base) const override
{ return do_value_ptr(thd, OPT_SESSION, base); }
const uchar* global_value_ptr(THD *thd, const LEX_CSTRING *base) const override
{ return do_value_ptr(thd, OPT_GLOBAL, base); }
const uchar *default_value_ptr(THD *thd) const override
{ return do_value_ptr(thd, OPT_DEFAULT, 0); }
bool do_check(THD *thd, set_var *var) override;
void session_save_default(THD *thd, set_var *var) override {}
void global_save_default(THD *thd, set_var *var) override {}
bool session_update(THD *thd, set_var *var) override;
bool global_update(THD *thd, set_var *var) override;
bool session_is_default(THD *thd) override;
};
/* prototypes */
static void plugin_load(MEM_ROOT *tmp_root);
static bool plugin_load_list(MEM_ROOT *, const char *);
static int test_plugin_options(MEM_ROOT *, struct st_plugin_int *,
int *, char **);
static bool register_builtin(struct st_maria_plugin *, struct st_plugin_int *,
struct st_plugin_int **);
static void unlock_variables(THD *thd, struct system_variables *vars);
static void cleanup_variables(struct system_variables *vars);
static void plugin_vars_free_values(st_mysql_sys_var **vars);
static void restore_ptr_backup(uint n, st_ptr_backup *backup);
static void intern_plugin_unlock(LEX *lex, plugin_ref plugin);
static void reap_plugins(void);
bool plugin_is_forced(struct st_plugin_int *p)
{
return p->load_option == PLUGIN_FORCE ||
p->load_option == PLUGIN_FORCE_PLUS_PERMANENT;
}
/**
Check if the provided path is valid in the sense that it does cause
a relative reference outside the directory.
@note Currently, this function only check if there are any
characters in FN_DIRSEP in the string, but it might change in the
future.
@code
check_valid_path("../foo.so") -> true
check_valid_path("foo.so") -> false
@endcode
*/
bool check_valid_path(const char *path, size_t len)
{
size_t prefix= my_strcspn(files_charset_info, path, path + len, FN_DIRSEP);
return prefix < len;
}
static void fix_dl_name(MEM_ROOT *root, LEX_CSTRING *dl)
{
const Lex_ident_plugin so_ext(STRING_WITH_LEN(SO_EXT));
if (dl->length < so_ext.length ||
!so_ext.streq(Lex_cstring(dl->str + dl->length - so_ext.length,
so_ext.length)))
{
size_t s_size= dl->length + so_ext.length + 1;
char *s= (char*)alloc_root(root, s_size);
memcpy(s, dl->str, dl->length);
safe_strcpy(s + dl->length, s_size - dl->length, SO_EXT);
dl->str= s;
dl->length+= so_ext.length;
}
}
/****************************************************************************
Value type thunks, allows the C world to play in the C++ world
****************************************************************************/
static int item_value_type(struct st_mysql_value *value)
{
switch (((st_item_value_holder*)value)->item->result_type()) {
case INT_RESULT:
return MYSQL_VALUE_TYPE_INT;
case REAL_RESULT:
return MYSQL_VALUE_TYPE_REAL;
default:
return MYSQL_VALUE_TYPE_STRING;
}
}
static const char *item_val_str(struct st_mysql_value *value,
char *buffer, int *length)
{
size_t org_length= *length;
String str(buffer, org_length, system_charset_info), *res;
if (!(res= ((st_item_value_holder*)value)->item->val_str(&str)))
return NULL;
*length= res->length();
if (res->ptr() == buffer && res->length() < org_length)
{
buffer[res->length()]= 0;
return buffer;
}
/*
Lets be nice and create a temporary string since the
buffer was too small
*/
return current_thd->strmake(res->ptr(), res->length());
}
static int item_val_int(struct st_mysql_value *value, long long *buf)
{
Item *item= ((st_item_value_holder*)value)->item;
*buf= item->val_int();
if (item->is_null())
return 1;
return 0;
}
static int item_is_unsigned(struct st_mysql_value *value)
{
Item *item= ((st_item_value_holder*)value)->item;
return item->unsigned_flag;
}
static int item_val_real(struct st_mysql_value *value, double *buf)
{
Item *item= ((st_item_value_holder*)value)->item;
*buf= item->val_real();
if (item->is_null())
return 1;
return 0;
}
/****************************************************************************
Plugin support code
****************************************************************************/
#ifdef HAVE_DLOPEN
static struct st_plugin_dl *plugin_dl_find(const LEX_CSTRING *dl)
{
size_t i;
struct st_plugin_dl *tmp;
DBUG_ENTER("plugin_dl_find");
for (i= 0; i < plugin_dl_array.elements; i++)
{
tmp= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
if (tmp->ref_count &&
! files_charset_info->strnncoll(dl->str, dl->length,
tmp->dl.str, tmp->dl.length))
DBUG_RETURN(tmp);
}
DBUG_RETURN(0);
}
static st_plugin_dl *plugin_dl_insert_or_reuse(struct st_plugin_dl *plugin_dl)
{
size_t i;
struct st_plugin_dl *tmp;
DBUG_ENTER("plugin_dl_insert_or_reuse");
for (i= 0; i < plugin_dl_array.elements; i++)
{
tmp= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
if (! tmp->ref_count)
{
memcpy(tmp, plugin_dl, sizeof(struct st_plugin_dl));
DBUG_RETURN(tmp);
}
}
if (insert_dynamic(&plugin_dl_array, (uchar*)&plugin_dl))
DBUG_RETURN(0);
tmp= *dynamic_element(&plugin_dl_array, plugin_dl_array.elements - 1,
struct st_plugin_dl **)=
(struct st_plugin_dl *) memdup_root(&plugin_mem_root, (uchar*)plugin_dl,
sizeof(struct st_plugin_dl));
DBUG_RETURN(tmp);
}
#else
static struct st_plugin_dl *plugin_dl_find(const LEX_STRING *)
{
return 0;
}
#endif /* HAVE_DLOPEN */
static void free_plugin_mem(struct st_plugin_dl *p)
{
#ifdef HAVE_DLOPEN
if (p->ptr_backup)
{
DBUG_ASSERT(p->nbackups);
DBUG_ASSERT(p->handle);
restore_ptr_backup(p->nbackups, p->ptr_backup);
my_free(p->ptr_backup);
}
if (p->handle)
dlclose(p->handle);
#endif
my_free(const_cast<char*>(p->dl.str));
if (p->allocated)
my_free(p->plugins);
}
/**
Reads data from mysql plugin interface
@param plugin_dl Structure where the data should be put
@param sym Reverence on version info
@param dlpath Path to the module
@param MyFlags Where errors should be reported (0 or ME_ERROR_LOG)
@retval FALSE OK
@retval TRUE ERROR
*/
#ifdef HAVE_DLOPEN
static my_bool read_mysql_plugin_info(struct st_plugin_dl *plugin_dl,
void *sym, char *dlpath, myf MyFlags)
{
DBUG_ENTER("read_maria_plugin_info");
/* Determine interface version */
if (!sym)
{
my_error(ER_CANT_FIND_DL_ENTRY, MyFlags, plugin_interface_version_sym, dlpath);
DBUG_RETURN(TRUE);
}
plugin_dl->mariaversion= 0;
plugin_dl->mysqlversion= *(int *)sym;
/* Versioning */
if (plugin_dl->mysqlversion < min_plugin_interface_version ||
(plugin_dl->mysqlversion >> 8) > (MYSQL_PLUGIN_INTERFACE_VERSION >> 8))
{
my_error(ER_CANT_OPEN_LIBRARY, MyFlags, dlpath, ENOEXEC,
"plugin interface version mismatch");
DBUG_RETURN(TRUE);
}
/* Find plugin declarations */
if (!(sym= dlsym(plugin_dl->handle, plugin_declarations_sym)))
{
my_error(ER_CANT_FIND_DL_ENTRY, MyFlags, plugin_declarations_sym, dlpath);
DBUG_RETURN(TRUE);
}
/* convert mysql declaration to maria one */
{
int i;
uint sizeof_st_plugin;
struct st_mysql_plugin *old;
struct st_maria_plugin *cur;
char *ptr= (char *)sym;
if ((sym= dlsym(plugin_dl->handle, sizeof_st_plugin_sym)))
sizeof_st_plugin= *(int *)sym;
else
{
DBUG_ASSERT(min_plugin_interface_version == 0);
sizeof_st_plugin= (int)offsetof(struct st_mysql_plugin, version);
}
for (i= 0;
((struct st_mysql_plugin *)(ptr + i * sizeof_st_plugin))->info;
i++)
/* no op */;
cur= (struct st_maria_plugin*)
my_malloc(key_memory_mysql_plugin, (i + 1) * sizeof(struct st_maria_plugin),
MYF(MY_ZEROFILL|MY_WME));
if (!cur)
{
my_error(ER_OUTOFMEMORY, MyFlags,
static_cast<int>(plugin_dl->dl.length));
DBUG_RETURN(TRUE);
}
/*
All st_plugin fields not initialized in the plugin explicitly, are
set to 0. It matches C standard behaviour for struct initializers that
have less values than the struct definition.
*/
for (i=0;
(old= (struct st_mysql_plugin *)(ptr + i * sizeof_st_plugin))->info;
i++)
{
cur[i].type= old->type;
cur[i].info= old->info;
cur[i].name= old->name;
cur[i].author= old->author;
cur[i].descr= old->descr;
cur[i].license= old->license;
cur[i].init= old->init;
cur[i].deinit= old->deinit;
cur[i].version= old->version;
cur[i].status_vars= old->status_vars;
cur[i].system_vars= old->system_vars;
/*
Something like this should be added to process
new mysql plugin versions:
if (plugin_dl->mysqlversion > 0x0101)
{
cur[i].newfield= CONSTANT_MEANS_UNKNOWN;
}
else
{
cur[i].newfield= old->newfield;
}
*/
/* Maria only fields */
cur[i].version_info= "Unknown";
cur[i].maturity= MariaDB_PLUGIN_MATURITY_UNKNOWN;
}
plugin_dl->allocated= true;
plugin_dl->plugins= (struct st_maria_plugin *)cur;
}
DBUG_RETURN(FALSE);
}
/**
Reads data from maria plugin interface
@param plugin_dl Structure where the data should be put
@param sym Reverence on version info
@param dlpath Path to the module
@param MyFlags Where errors should be reported (0 or ME_ERROR_LOG)
@retval FALSE OK
@retval TRUE ERROR
*/
static my_bool read_maria_plugin_info(struct st_plugin_dl *plugin_dl,
void *sym, char *dlpath, myf MyFlags)
{
DBUG_ENTER("read_maria_plugin_info");
/* Determine interface version */
if (!(sym))
{
/*
Actually this branch impossible because in case of absence of maria
version we try mysql version.
*/
my_error(ER_CANT_FIND_DL_ENTRY, MyFlags,
maria_plugin_interface_version_sym, dlpath);
DBUG_RETURN(TRUE);
}
plugin_dl->mariaversion= *(int *)sym;
plugin_dl->mysqlversion= 0;
/* Versioning */
if (plugin_dl->mariaversion < min_maria_plugin_interface_version ||
(plugin_dl->mariaversion >> 8) > (MARIA_PLUGIN_INTERFACE_VERSION >> 8))
{
my_error(ER_CANT_OPEN_LIBRARY, MyFlags, dlpath, ENOEXEC,
"plugin interface version mismatch");
DBUG_RETURN(TRUE);
}
/* Find plugin declarations */
if (!(sym= dlsym(plugin_dl->handle, maria_plugin_declarations_sym)))
{
my_error(ER_CANT_FIND_DL_ENTRY, MyFlags, maria_plugin_declarations_sym, dlpath);
DBUG_RETURN(TRUE);
}
if (plugin_dl->mariaversion != MARIA_PLUGIN_INTERFACE_VERSION)
{
uint sizeof_st_plugin;
struct st_maria_plugin *old, *cur;
char *ptr= (char *)sym;
if ((sym= dlsym(plugin_dl->handle, maria_sizeof_st_plugin_sym)))
sizeof_st_plugin= *(int *)sym;
else
{
my_error(ER_CANT_FIND_DL_ENTRY, MyFlags, maria_sizeof_st_plugin_sym,
dlpath);
DBUG_RETURN(TRUE);
}
if (sizeof_st_plugin != sizeof(st_mysql_plugin))
{
int i;
for (i= 0;
((struct st_maria_plugin *)(ptr + i * sizeof_st_plugin))->info;
i++)
/* no op */;
cur= (struct st_maria_plugin*)
my_malloc(key_memory_mysql_plugin, (i + 1) * sizeof(struct st_maria_plugin),
MYF(MY_ZEROFILL|MY_WME));
if (!cur)
{
my_error(ER_OUTOFMEMORY, MyFlags,
static_cast<int>(plugin_dl->dl.length));
DBUG_RETURN(TRUE);
}
/*
All st_plugin fields not initialized in the plugin explicitly, are
set to 0. It matches C standard behaviour for struct initializers that
have less values than the struct definition.
*/
for (i=0;
(old= (struct st_maria_plugin *)(ptr + i * sizeof_st_plugin))->info;
i++)
memcpy(cur + i, old, MY_MIN(sizeof(cur[i]), sizeof_st_plugin));
sym= cur;
plugin_dl->allocated= true;
}
else
sym= ptr;
}
plugin_dl->plugins= (struct st_maria_plugin *)sym;
DBUG_RETURN(FALSE);
}
#endif /* HAVE_DLOPEN */
static st_plugin_dl *plugin_dl_add(const LEX_CSTRING *dl, myf MyFlags)
{
#ifdef HAVE_DLOPEN
char dlpath[FN_REFLEN];
size_t plugin_dir_len,i;
uint dummy_errors;
struct st_plugin_dl *tmp= 0, plugin_dl;
void *sym;
st_ptr_backup tmp_backup[array_elements(list_of_services)];
DBUG_ENTER("plugin_dl_add");
DBUG_PRINT("enter", ("dl->str: '%s', dl->length: %d",
dl->str, (int) dl->length));
mysql_mutex_assert_owner(&LOCK_plugin);
plugin_dir_len= strlen(opt_plugin_dir);
/*
Ensure that the dll doesn't have a path.
This is done to ensure that only approved libraries from the
plugin directory are used (to make this even remotely secure).
*/
if (check_string_char_length((LEX_CSTRING *) dl, 0, NAME_CHAR_LEN,
system_charset_info, 1) ||
check_valid_path(dl->str, dl->length) ||
plugin_dir_len + dl->length + 1 >= FN_REFLEN)
{
my_error(ER_UDF_NO_PATHS, MyFlags);
DBUG_RETURN(0);
}
/* If this dll is already loaded just increase ref_count. */
if ((tmp= plugin_dl_find(dl)))
{
tmp->ref_count++;
DBUG_RETURN(tmp);
}
bzero(&plugin_dl, sizeof(plugin_dl));
/* Compile dll path */
strxnmov(dlpath, sizeof(dlpath) - 1, opt_plugin_dir, "/", dl->str, NullS);
(void) unpack_filename(dlpath, dlpath);
plugin_dl.ref_count= 1;
/* Open new dll handle */
if (!(plugin_dl.handle= dlopen(dlpath, RTLD_NOW)))
{
my_error(ER_CANT_OPEN_LIBRARY, MyFlags, dlpath, errno, my_dlerror(dlpath));
goto ret;
}
dlopen_count++;
#ifdef HAVE_LINK_H
if (global_system_variables.log_warnings > 2)
{
struct link_map *lm = (struct link_map*) plugin_dl.handle;
sql_print_information("Loaded '%s' with offset 0x%zx", dl->str, (size_t)lm->l_addr);
}
#endif
/* Checks which plugin interface present and reads info */
if (!(sym= dlsym(plugin_dl.handle, maria_plugin_interface_version_sym)))
{
if (read_mysql_plugin_info(&plugin_dl,
dlsym(plugin_dl.handle,
plugin_interface_version_sym),
dlpath,
MyFlags))
goto ret;
}
else
{
if (read_maria_plugin_info(&plugin_dl, sym, dlpath, MyFlags))
goto ret;
}
/* link the services in */
for (i= 0; i < array_elements(list_of_services); i++)
{
if ((sym= dlsym(plugin_dl.handle, list_of_services[i].name)))
{
void **ptr= (void **)sym;
uint ver= (uint)(intptr)*ptr;
if (ver > list_of_services[i].version ||
(ver >> 8) < (list_of_services[i].version >> 8))
{
char buf[MYSQL_ERRMSG_SIZE];
my_snprintf(buf, sizeof(buf),
"service '%s' interface version mismatch",
list_of_services[i].name);
my_error(ER_CANT_OPEN_LIBRARY, MyFlags, dlpath, ENOEXEC, buf);
goto ret;
}
tmp_backup[plugin_dl.nbackups++].save(ptr);
*ptr= list_of_services[i].service;
}
}
if (plugin_dl.nbackups)
{
size_t bytes= plugin_dl.nbackups * sizeof(plugin_dl.ptr_backup[0]);
plugin_dl.ptr_backup= (st_ptr_backup *)my_malloc(key_memory_mysql_plugin_dl,
bytes, MYF(0));
if (!plugin_dl.ptr_backup)
{
restore_ptr_backup(plugin_dl.nbackups, tmp_backup);
my_error(ER_OUTOFMEMORY, MyFlags, bytes);
goto ret;
}
memcpy(plugin_dl.ptr_backup, tmp_backup, bytes);
}
/* Duplicate and convert dll name */
plugin_dl.dl.length= dl->length * files_charset_info->mbmaxlen + 1;
if (! (plugin_dl.dl.str= (char*) my_malloc(key_memory_mysql_plugin_dl,
plugin_dl.dl.length, MYF(0))))
{
my_error(ER_OUTOFMEMORY, MyFlags,
static_cast<int>(plugin_dl.dl.length));
goto ret;
}
plugin_dl.dl.length= copy_and_convert((char*) plugin_dl.dl.str,
plugin_dl.dl.length,
files_charset_info, dl->str,
dl->length, system_charset_info,
&dummy_errors);
((char*) plugin_dl.dl.str)[plugin_dl.dl.length]= 0;
/* Add this dll to array */
if (! (tmp= plugin_dl_insert_or_reuse(&plugin_dl)))
{
my_error(ER_OUTOFMEMORY, MyFlags,
static_cast<int>(sizeof(struct st_plugin_dl)));
goto ret;
}
ret:
if (!tmp)
free_plugin_mem(&plugin_dl);
DBUG_RETURN(tmp);
#else
DBUG_ENTER("plugin_dl_add");
my_error(ER_FEATURE_DISABLED, MyFlags, "plugin", "HAVE_DLOPEN");
DBUG_RETURN(0);
#endif
}
static void plugin_dl_del(struct st_plugin_dl *plugin_dl)
{
DBUG_ENTER("plugin_dl_del");
if (!plugin_dl)
DBUG_VOID_RETURN;
mysql_mutex_assert_owner(&LOCK_plugin);
/* Do not remove this element, unless no other plugin uses this dll. */
if (! --plugin_dl->ref_count)
{
free_plugin_mem(plugin_dl);
bzero(plugin_dl, sizeof(struct st_plugin_dl));
}
DBUG_VOID_RETURN;
}
static struct st_plugin_int *plugin_find_internal(const LEX_CSTRING *name,
int type)
{
uint i;
DBUG_ENTER("plugin_find_internal");
if (! initialized)
DBUG_RETURN(0);
mysql_mutex_assert_owner(&LOCK_plugin);
if (type == MYSQL_ANY_PLUGIN)
{
for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++)
{
struct st_plugin_int *plugin= (st_plugin_int *)
my_hash_search(&plugin_hash[i], (const uchar *)name->str, name->length);
if (plugin)
DBUG_RETURN(plugin);
}
}
else
DBUG_RETURN((st_plugin_int *)
my_hash_search(&plugin_hash[type], (const uchar *)name->str,
name->length));
DBUG_RETURN(0);
}
static SHOW_COMP_OPTION plugin_status(const LEX_CSTRING *name, int type)
{
SHOW_COMP_OPTION rc= SHOW_OPTION_NO;
struct st_plugin_int *plugin;
DBUG_ENTER("plugin_is_ready");
mysql_mutex_lock(&LOCK_plugin);
if ((plugin= plugin_find_internal(name, type)))
{
rc= SHOW_OPTION_DISABLED;
if (plugin->state == PLUGIN_IS_READY)
rc= SHOW_OPTION_YES;
}
mysql_mutex_unlock(&LOCK_plugin);
DBUG_RETURN(rc);
}
bool plugin_is_ready(const LEX_CSTRING *name, int type)
{
bool rc= FALSE;
if (plugin_status(name, type) == SHOW_OPTION_YES)
rc= TRUE;
return rc;
}
SHOW_COMP_OPTION plugin_status(const char *name, size_t len, int type)
{
LEX_CSTRING plugin_name= { name, len };
return plugin_status(&plugin_name, type);
}
/*
If LEX is passed non-NULL, an automatic unlock of the plugin will happen
in the LEX destructor.
*/
static plugin_ref intern_plugin_lock(LEX *lex, plugin_ref rc,
uint state_mask= PLUGIN_IS_READY |
PLUGIN_IS_UNINITIALIZED |
PLUGIN_IS_DELETED)
{
st_plugin_int *pi= plugin_ref_to_int(rc);
DBUG_ENTER("intern_plugin_lock");
mysql_mutex_assert_owner(&LOCK_plugin);
if (pi->state & state_mask)
{
plugin_ref plugin;
#ifdef DBUG_OFF
/*
In optimized builds we don't do reference counting for built-in
(plugin->plugin_dl == 0) plugins.
*/
if (!pi->plugin_dl)
DBUG_RETURN(pi);
plugin= pi;
#else
/*
For debugging, we do an additional malloc which allows the
memory manager and/or valgrind to track locked references and
double unlocks to aid resolving reference counting problems.
*/
if (!(plugin= (plugin_ref) my_malloc(PSI_NOT_INSTRUMENTED, sizeof(pi),
MYF(MY_WME))))
DBUG_RETURN(NULL);
*plugin= pi;
#endif