-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsys_vars.inl
3072 lines (2807 loc) · 99.9 KB
/
sys_vars.inl
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) 2002, 2011, 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 */
/**
@file
"private" interface to sys_var - server configuration variables.
This header is included only by the file that contains declarations
of sys_var variables (sys_vars.cc).
*/
#include "sys_vars_shared.h"
#include <my_getopt.h>
#include <my_bit.h>
#include <my_dir.h>
#include "keycaches.h"
#include "strfunc.h"
#include "tztime.h" // my_tz_find, my_tz_SYSTEM, struct Time_zone
#include "rpl_mi.h" // For Multi-Source Replication
#include "debug_sync.h"
#include "sql_acl.h" // check_global_access()
#include "optimizer_defaults.h" // create_optimizer_costs
/*
a set of mostly trivial (as in f(X)=X) defines below to make system variable
declarations more readable
*/
#define VALID_RANGE(X,Y) X,Y
#define DEFAULT(X) X
#define BLOCK_SIZE(X) X
#define COST_ADJUST(X) X
#define GLOBAL_VAR(X) sys_var::GLOBAL, (((char*)&(X))-(char*)&global_system_variables), sizeof(X)
#define SESSION_VAR(X) sys_var::SESSION, offsetof(SV, X), sizeof(((SV *)0)->X)
#define SESSION_ONLY(X) sys_var::ONLY_SESSION, offsetof(SV, X), sizeof(((SV *)0)->X)
#define NO_CMD_LINE CMD_LINE(NO_ARG, sys_var::NO_GETOPT)
#define CMD_LINE_HELP_ONLY CMD_LINE(NO_ARG, sys_var::GETOPT_ONLY_HELP)
/*
the define below means that there's no *second* mutex guard,
LOCK_global_system_variables always guards all system variables
*/
#define NO_MUTEX_GUARD ((PolyLock*)0)
#define IN_BINLOG sys_var::SESSION_VARIABLE_IN_BINLOG
#define NOT_IN_BINLOG sys_var::VARIABLE_NOT_IN_BINLOG
#define ON_READ(X) X
#define ON_CHECK(X) X
#define ON_UPDATE(X) X
#define READ_ONLY sys_var::READONLY+
#define AUTO_SET sys_var::AUTO_SET+
// this means that Sys_var_charptr initial value was malloc()ed
#define PREALLOCATED sys_var::ALLOCATED+
#define PARSED_EARLY sys_var::PARSE_EARLY+
#define NO_SET_STMT sys_var::NO_SET_STATEMENT+
extern const char *UNUSED_HELP;
/*
Sys_var_bit meaning is reversed, like in
@@foreign_key_checks <-> OPTION_NO_FOREIGN_KEY_CHECKS
*/
#define REVERSE(X) ~(X)
#define DEPRECATED(V, REPL) (check_deprecated_version<V>(), REPL)
#define DEPRECATED_NO_REPLACEMENT(V) DEPRECATED(V, "")
#define session_var(THD, TYPE) (*(TYPE*)session_var_ptr(THD))
#define global_var(TYPE) (*(TYPE*)global_var_ptr())
#if SIZEOF_OFF_T > 4
#define GET_HA_ROWS GET_ULL
#else
#define GET_HA_ROWS GET_ULONG
#endif
// Disable warning caused by SESSION_VAR() macro
#ifdef __clang__
#pragma clang diagnostic ignored "-Winvalid-offsetof"
#endif
/*
special assert for sysvars. Tells the name of the variable,
and fails even in non-debug builds.
It is supposed to be used *only* in Sys_var* constructors,
and has name_arg hard-coded to prevent incorrect usage.
*/
#define SYSVAR_ASSERT(X) \
while(!(X)) \
{ \
fprintf(stderr, "Sysvar '%s' failed '%s'\n", name_arg, #X); \
DBUG_ASSERT(0); \
exit(255); \
}
static const char *bool_values[3]= {"OFF", "ON", 0};
TYPELIB bool_typelib= CREATE_TYPELIB_FOR(bool_values);
template<class BASE, privilege_t GLOBAL_PRIV, privilege_t SESSION_PRIV>
class Sys_var_on_access: public BASE
{
using BASE::BASE;
bool on_check_access_global(THD *thd) const override
{
return check_global_access(thd, GLOBAL_PRIV);
}
bool on_check_access_session(THD *thd) const override
{
return check_global_access(thd, SESSION_PRIV);
}
};
template<class BASE, privilege_t GLOBAL_PRIV>
class Sys_var_on_access_global: public BASE
{
using BASE::BASE;
bool on_check_access_global(THD *thd) const override
{
return check_global_access(thd, GLOBAL_PRIV);
}
};
template<class BASE, privilege_t SESSION_PRIV>
class Sys_var_on_access_session: public BASE
{
using BASE::BASE;
bool on_check_access_session(THD *thd) const override
{
return check_global_access(thd, SESSION_PRIV);
}
};
/**
A small wrapper class to pass getopt arguments as a pair
to the Sys_var_* constructors. It improves type safety and helps
to catch errors in the argument order.
*/
struct CMD_LINE
{
int id;
enum get_opt_arg_type arg_type;
CMD_LINE(enum get_opt_arg_type getopt_arg_type, int getopt_id=0)
: id(getopt_id), arg_type(getopt_arg_type) {}
};
/**
Sys_var_integer template is used to generate Sys_var_* classes
for variables that represent the value as an integer number.
They are Sys_var_uint, Sys_var_ulong, Sys_var_harows, Sys_var_ulonglong,
Sys_var_int.
An integer variable has a minimal and maximal values, and a "block_size"
(any valid value of the variable must be divisible by the block_size).
Class specific constructor arguments: min, max, block_size
Backing store: int, uint, ulong, ha_rows, ulonglong, depending on the class
*/
template <typename T, ulong ARGT, enum enum_mysql_show_type SHOWT>
class Sys_var_integer: public sys_var
{
public:
Sys_var_integer(const char *name_arg,
const char *comment, int flag_args, ptrdiff_t off, size_t size,
CMD_LINE getopt,
T min_val, T max_val, T def_val, uint block_size, PolyLock *lock=0,
enum binlog_status_enum binlog_status_arg=VARIABLE_NOT_IN_BINLOG,
on_check_function on_check_func=0,
on_update_function on_update_func=0,
const char *substitute=0)
: sys_var(&all_sys_vars, name_arg, comment, flag_args, off, getopt.id,
getopt.arg_type, SHOWT, def_val, lock, binlog_status_arg,
on_check_func, on_update_func, substitute)
{
option.var_type|= ARGT;
option.min_value= min_val;
option.max_value= max_val;
option.block_size= block_size;
if ((option.u_max_value= (uchar**) max_var_ptr()))
{
*((T*) option.u_max_value)= max_val;
}
global_var(T)= def_val;
SYSVAR_ASSERT(size == sizeof(T));
SYSVAR_ASSERT(min_val < max_val);
SYSVAR_ASSERT(min_val <= def_val);
SYSVAR_ASSERT(max_val >= def_val);
SYSVAR_ASSERT(block_size > 0);
SYSVAR_ASSERT(def_val % block_size == 0);
}
bool do_check(THD *thd, set_var *var) override
{
my_bool fixed= FALSE, unused;
longlong v= var->value->val_int();
if ((ARGT == GET_HA_ROWS) || (ARGT == GET_UINT) ||
(ARGT == GET_ULONG) || (ARGT == GET_ULL))
{
ulonglong uv;
/*
if the value is signed and negative,
and a variable is unsigned, it is set to zero
*/
if ((fixed= (!var->value->unsigned_flag && v < 0)))
uv= 0;
else
uv= v;
var->save_result.ulonglong_value=
getopt_ull_limit_value(uv, &option, &unused);
if (max_var_ptr() && (T)var->save_result.ulonglong_value > get_max_var())
var->save_result.ulonglong_value= get_max_var();
fixed= fixed || var->save_result.ulonglong_value != uv;
}
else
{
/*
if the value is unsigned and has the highest bit set
and a variable is signed, it is set to max signed value
*/
if ((fixed= (var->value->unsigned_flag && v < 0)))
v= LONGLONG_MAX;
var->save_result.longlong_value=
getopt_ll_limit_value(v, &option, &unused);
if (max_var_ptr() && (T)var->save_result.longlong_value > get_max_var())
var->save_result.longlong_value= get_max_var();
fixed= fixed || var->save_result.longlong_value != v;
}
return throw_bounds_warning(thd, name.str, fixed,
var->value->unsigned_flag, v);
}
bool session_update(THD *thd, set_var *var) override
{
session_var(thd, T)= static_cast<T>(var->save_result.ulonglong_value);
return false;
}
bool global_update(THD *thd, set_var *var) override
{
global_var(T)= static_cast<T>(var->save_result.ulonglong_value);
return false;
}
void session_save_default(THD *thd, set_var *var) override
{ var->save_result.ulonglong_value= (ulonglong)*(T*)global_value_ptr(thd, 0); }
void global_save_default(THD *thd, set_var *var) override
{ var->save_result.ulonglong_value= option.def_value; }
private:
T get_max_var() { return *((T*) max_var_ptr()); }
const uchar *default_value_ptr(THD *thd) const override { return (uchar*) &option.def_value; }
};
typedef Sys_var_integer<int, GET_INT, SHOW_SINT> Sys_var_int;
typedef Sys_var_integer<uint, GET_UINT, SHOW_UINT> Sys_var_uint;
typedef Sys_var_integer<ulong, GET_ULONG, SHOW_ULONG> Sys_var_ulong;
typedef Sys_var_integer<ha_rows, GET_HA_ROWS, SHOW_HA_ROWS> Sys_var_harows;
typedef Sys_var_integer<ulonglong, GET_ULL, SHOW_ULONGLONG> Sys_var_ulonglong;
typedef Sys_var_integer<long, GET_LONG, SHOW_SLONG> Sys_var_long;
template<> const uchar *Sys_var_int::default_value_ptr(THD *thd) const
{
thd->sys_var_tmp.int_value= (int)option.def_value;
return (uchar*) &thd->sys_var_tmp.int_value;
}
template<> const uchar *Sys_var_uint::default_value_ptr(THD *thd) const
{
thd->sys_var_tmp.uint_value= (uint)option.def_value;
return (uchar*) &thd->sys_var_tmp.uint_value;
}
template<> const uchar *Sys_var_long::default_value_ptr(THD *thd) const
{
thd->sys_var_tmp.long_value= (long)option.def_value;
return (uchar*) &thd->sys_var_tmp.long_value;
}
template<> const uchar *Sys_var_ulong::default_value_ptr(THD *thd) const
{
thd->sys_var_tmp.ulong_value= (ulong)option.def_value;
return (uchar*) &thd->sys_var_tmp.ulong_value;
}
/**
Helper class for variables that take values from a TYPELIB
*/
class Sys_var_typelib: public sys_var
{
protected:
TYPELIB typelib;
virtual bool check_maximum(THD *thd, set_var *var,
const char *c_val, longlong i_val)
{ return FALSE; }
public:
Sys_var_typelib(const char *name_arg,
const char *comment, int flag_args, ptrdiff_t off,
CMD_LINE getopt,
SHOW_TYPE show_val_type_arg, const char *values[],
ulonglong def_val, PolyLock *lock,
enum binlog_status_enum binlog_status_arg,
on_check_function on_check_func, on_update_function on_update_func,
const char *substitute)
: sys_var(&all_sys_vars, name_arg, comment, flag_args, off, getopt.id,
getopt.arg_type, show_val_type_arg, def_val, lock,
binlog_status_arg, on_check_func,
on_update_func, substitute)
{
for (typelib.count= 0; values[typelib.count]; typelib.count++) /*no-op */;
typelib.name="";
typelib.type_names= values;
typelib.type_lengths= 0; // only used by Fields_enum and Field_set
option.typelib= &typelib;
}
bool do_check(THD *thd, set_var *var) override // works for enums and my_bool
{
char buff[STRING_BUFFER_USUAL_SIZE];
String str(buff, sizeof(buff), system_charset_info), *res;
if (var->value->result_type() == STRING_RESULT)
{
/*
Convert from the expression character set to ascii.
This is OK, as typelib values cannot have non-ascii characters.
*/
if (!(res= var->value->val_str_ascii(&str)))
return true;
else
if (!(var->save_result.ulonglong_value=
find_type(&typelib, res->ptr(), res->length(), false)))
return true;
else
var->save_result.ulonglong_value--;
return check_maximum(thd, var, res->ptr(), 0);
}
longlong tmp=var->value->val_int();
if (tmp < 0 || tmp >= typelib.count)
return true;
var->save_result.ulonglong_value= tmp;
return check_maximum(thd, var, 0, tmp);
}
};
/**
The class for ENUM variables - variables that take one value from a fixed
list of values.
Class specific constructor arguments:
char* values[] - 0-terminated list of strings of valid values
Backing store: ulong
@note
Do *not* use "enum FOO" variables as a backing store, there is no
guarantee that sizeof(enum FOO) == sizeof(uint), there is no guarantee
even that sizeof(enum FOO) == sizeof(enum BAR)
*/
class Sys_var_enum: public Sys_var_typelib
{
public:
Sys_var_enum(const char *name_arg,
const char *comment, int flag_args, ptrdiff_t off, size_t size,
CMD_LINE getopt,
const char *values[], uint def_val, PolyLock *lock=0,
enum binlog_status_enum binlog_status_arg=VARIABLE_NOT_IN_BINLOG,
on_check_function on_check_func=0,
on_update_function on_update_func=0,
const char *substitute=0)
: Sys_var_typelib(name_arg, comment, flag_args, off, getopt,
SHOW_CHAR, values, def_val, lock,
binlog_status_arg, on_check_func, on_update_func,
substitute)
{
option.var_type|= GET_ENUM;
option.min_value= 0;
option.max_value= ULONG_MAX;
global_var(ulong)= def_val;
if ((option.u_max_value= (uchar**)max_var_ptr()))
{
*((ulong *) option.u_max_value)= ULONG_MAX;
}
SYSVAR_ASSERT(def_val < typelib.count);
SYSVAR_ASSERT(size == sizeof(ulong));
}
bool check_maximum(THD *thd, set_var *var,
const char *c_val, longlong i_val) override
{
if (!max_var_ptr() ||
var->save_result.ulonglong_value <= get_max_var())
return FALSE;
var->save_result.ulonglong_value= get_max_var();
return c_val ? throw_bounds_warning(thd, name.str, c_val) :
throw_bounds_warning(thd, name.str, TRUE,
var->value->unsigned_flag, i_val);
}
bool session_update(THD *thd, set_var *var) override
{
session_var(thd, ulong)= static_cast<ulong>(var->save_result.ulonglong_value);
return false;
}
bool global_update(THD *thd, set_var *var) override
{
global_var(ulong)= static_cast<ulong>(var->save_result.ulonglong_value);
return false;
}
void session_save_default(THD *thd, set_var *var) override
{ var->save_result.ulonglong_value= global_var(ulong); }
void global_save_default(THD *thd, set_var *var) override
{ var->save_result.ulonglong_value= option.def_value; }
const uchar *valptr(THD *thd, ulong val) const
{ return reinterpret_cast<const uchar*>(typelib.type_names[val]); }
const uchar *session_value_ptr(THD *thd, const LEX_CSTRING *base) const override
{ return valptr(thd, session_var(thd, ulong)); }
const uchar *global_value_ptr(THD *thd, const LEX_CSTRING *base) const override
{ return valptr(thd, global_var(ulong)); }
const uchar *default_value_ptr(THD *thd) const override
{ return valptr(thd, (ulong)option.def_value); }
ulong get_max_var() { return *((ulong *) max_var_ptr()); }
};
/**
The class for boolean variables - a variant of ENUM variables
with the fixed list of values of { OFF , ON }
Backing store: my_bool
*/
class Sys_var_mybool: public Sys_var_typelib
{
public:
Sys_var_mybool(const char *name_arg,
const char *comment, int flag_args, ptrdiff_t off, size_t size,
CMD_LINE getopt,
my_bool def_val, PolyLock *lock=0,
enum binlog_status_enum binlog_status_arg=VARIABLE_NOT_IN_BINLOG,
on_check_function on_check_func=0,
on_update_function on_update_func=0,
const char *substitute=0)
: Sys_var_typelib(name_arg, comment, flag_args, off, getopt,
SHOW_MY_BOOL, bool_values, def_val, lock,
binlog_status_arg, on_check_func, on_update_func,
substitute)
{
option.var_type|= GET_BOOL;
global_var(my_bool)= def_val;
SYSVAR_ASSERT(def_val < 2);
SYSVAR_ASSERT(getopt.arg_type == OPT_ARG || getopt.id < 0);
SYSVAR_ASSERT(size == sizeof(my_bool));
}
bool session_update(THD *thd, set_var *var) override
{
session_var(thd, my_bool)= var->save_result.ulonglong_value != 0;
return false;
}
bool global_update(THD *thd, set_var *var) override
{
global_var(my_bool)= var->save_result.ulonglong_value != 0;
return false;
}
void session_save_default(THD *thd, set_var *var) override
{ var->save_result.ulonglong_value= (ulonglong)*(my_bool *)global_value_ptr(thd, 0); }
void global_save_default(THD *thd, set_var *var) override
{ var->save_result.ulonglong_value= option.def_value; }
const uchar *default_value_ptr(THD *thd) const override
{
thd->sys_var_tmp.my_bool_value=(my_bool) option.def_value;
return (uchar*) &thd->sys_var_tmp.my_bool_value;
}
};
/**
The class for string variables. The string can be in character_set_filesystem
or in character_set_system. The string can be allocated with my_malloc()
or not. The state of the initial value is specified in the constructor,
after that it's managed automatically. The value of NULL is supported.
Backing store: char*
@note
Note that the memory management for SESSION_VAR's is manual, the
value must be strdup'ed in THD::init() and freed in
plugin_thdvar_cleanup(), see e.g. redirect_url. TODO: it should be
done automatically when we'll have more session string variables to
justify it. Maybe some kind of a loop over all variables, like
sys_var_end() in set_var.cc?
*/
class Sys_var_charptr: public sys_var
{
const size_t max_length= 2000;
public:
Sys_var_charptr(const char *name_arg,
const char *comment, int flag_args, ptrdiff_t off, size_t size,
CMD_LINE getopt,
const char *def_val, PolyLock *lock=0,
enum binlog_status_enum binlog_status_arg=VARIABLE_NOT_IN_BINLOG,
on_check_function on_check_func=0,
on_update_function on_update_func=0,
const char *substitute=0)
: sys_var(&all_sys_vars, name_arg, comment, flag_args, off, getopt.id,
getopt.arg_type, SHOW_CHAR_PTR, (intptr)def_val,
lock, binlog_status_arg, on_check_func, on_update_func,
substitute)
{
/*
use GET_STR_ALLOC - if ALLOCATED it must be *always* allocated,
otherwise (GET_STR) you'll never know whether to free it or not.
(think of an exit because of an error right after my_getopt)
*/
option.var_type|= (flags & ALLOCATED) ? GET_STR_ALLOC : GET_STR;
global_var(const char*)= def_val;
SYSVAR_ASSERT(size == sizeof(char *));
}
void cleanup() override
{
if (flags & ALLOCATED)
{
my_free(global_var(char*));
global_var(char *)= NULL;
}
flags&= ~ALLOCATED;
}
static bool do_string_check(THD *thd, set_var *var, CHARSET_INFO *charset)
{
char buff[STRING_BUFFER_USUAL_SIZE], buff2[STRING_BUFFER_USUAL_SIZE];
String str(buff, sizeof(buff), charset);
String str2(buff2, sizeof(buff2), charset), *res;
if (!(res=var->value->val_str(&str)))
{
var->save_result.string_value.str= 0;
var->save_result.string_value.length= 0; // safety
}
else
{
uint32 unused;
if (String::needs_conversion(res->length(), res->charset(),
charset, &unused))
{
uint errors;
str2.copy(res->ptr(), res->length(), res->charset(), charset,
&errors);
res=&str2;
}
var->save_result.string_value.str= thd->strmake(res->ptr(), res->length());
var->save_result.string_value.length= res->length();
}
return false;
}
bool do_check(THD *thd, set_var *var) override
{
if (do_string_check(thd, var, charset(thd)))
return true;
if (var->save_result.string_value.length > max_length)
{
my_error(ER_WRONG_STRING_LENGTH, MYF(0), var->save_result.string_value.str,
name.str, (int) max_length);
return true;
}
return false;
}
char *update_prepare(set_var *var, myf my_flags)
{
char *new_val, *ptr= var->save_result.string_value.str;
size_t len=var->save_result.string_value.length;
if (ptr)
{
new_val= (char*)my_memdup(key_memory_Sys_var_charptr_value,
ptr, len+1, my_flags);
if (!new_val) return 0;
new_val[len]=0;
}
else
new_val= 0;
return new_val;
}
bool session_update(THD *thd, set_var *var) override
{
char *new_val= update_prepare(var, MYF(MY_WME | MY_THREAD_SPECIFIC));
my_free(session_var(thd, char*));
session_var(thd, char*)= new_val;
return (new_val == 0 && var->save_result.string_value.str != 0);
}
void global_update_finish(char *new_val)
{
if (flags & ALLOCATED)
my_free(global_var(char*));
flags|= ALLOCATED;
global_var(char*)= new_val;
}
bool global_update(THD *thd, set_var *var) override
{
char *new_val= update_prepare(var, MYF(MY_WME));
global_update_finish(new_val);
return (new_val == 0 && var->save_result.string_value.str != 0);
}
void session_save_default(THD *, set_var *var) override
{
var->save_result.string_value.str= global_var(char*);
var->save_result.string_value.length=
strlen(var->save_result.string_value.str);
}
void global_save_default(THD *thd, set_var *var) override
{
char *ptr= (char*)(intptr)option.def_value;
var->save_result.string_value.str= ptr;
var->save_result.string_value.length= ptr ? strlen(ptr) : 0;
}
};
class Sys_var_charptr_fscs: public Sys_var_charptr
{
using Sys_var_charptr::Sys_var_charptr;
public:
CHARSET_INFO *charset(THD *thd) const override
{
return thd->variables.character_set_filesystem;
}
};
#ifndef EMBEDDED_LIBRARY
class Sys_var_sesvartrack: public Sys_var_charptr
{
public:
Sys_var_sesvartrack(const char *name_arg,
const char *comment,
CMD_LINE getopt,
const char *def_val, PolyLock *lock= 0) :
Sys_var_charptr(name_arg, comment,
SESSION_VAR(session_track_system_variables),
getopt, def_val, lock,
VARIABLE_NOT_IN_BINLOG, 0, 0, 0)
{}
bool do_check(THD *thd, set_var *var) override
{
if (Sys_var_charptr::do_string_check(thd, var, charset(thd)) ||
sysvartrack_validate_value(thd, var->save_result.string_value.str,
var->save_result.string_value.length))
return TRUE;
return FALSE;
}
bool global_update(THD *thd, set_var *var) override
{
char *new_val= update_prepare(var, MYF(MY_WME));
if (new_val)
{
if (sysvartrack_global_update(thd, new_val,
var->save_result.string_value.length))
{
if (new_val)
my_free(new_val);
new_val= 0;
}
}
global_update_finish(new_val);
return (new_val == 0 && var->save_result.string_value.str != 0);
}
bool session_update(THD *thd, set_var *var) override
{ return thd->session_tracker.sysvars.update(thd, var); }
void session_save_default(THD *thd, set_var *var) override
{
var->save_result.string_value.str= global_var(char*);
var->save_result.string_value.length=
strlen(var->save_result.string_value.str);
/* parse and feel list with default values */
if (thd)
{
#ifdef DBUG_ASSERT_EXISTS
bool res=
#endif
sysvartrack_validate_value(thd,
var->save_result.string_value.str,
var->save_result.string_value.length);
DBUG_ASSERT(res == 0);
}
}
};
#endif //EMBEDDED_LIBRARY
class Sys_var_proxy_user: public sys_var
{
public:
Sys_var_proxy_user(const char *name_arg, const char *comment)
: sys_var(&all_sys_vars, name_arg, comment,
sys_var::READONLY+sys_var::ONLY_SESSION, 0, NO_GETOPT,
NO_ARG, SHOW_CHAR, 0, NULL, VARIABLE_NOT_IN_BINLOG,
NULL, NULL, NULL)
{
option.var_type|= GET_STR;
}
bool do_check(THD *thd, set_var *var) override
{
DBUG_ASSERT(FALSE);
return true;
}
bool session_update(THD *thd, set_var *var) override
{
DBUG_ASSERT(FALSE);
return true;
}
bool global_update(THD *thd, set_var *var) override
{
DBUG_ASSERT(FALSE);
return false;
}
void session_save_default(THD *thd, set_var *var) override
{ DBUG_ASSERT(FALSE); }
void global_save_default(THD *thd, set_var *var) override
{ DBUG_ASSERT(FALSE); }
protected:
const uchar *session_value_ptr(THD *thd, const LEX_CSTRING *base) const override
{
return thd->security_ctx->proxy_user[0] ?
(uchar *) &(thd->security_ctx->proxy_user[0]) : NULL;
}
};
class Sys_var_external_user : public Sys_var_proxy_user
{
public:
Sys_var_external_user(const char *name_arg, const char *comment_arg)
: Sys_var_proxy_user (name_arg, comment_arg)
{}
protected:
const uchar *session_value_ptr(THD *thd, const LEX_CSTRING *base) const override
{
return (uchar*)thd->security_ctx->external_user;
}
};
class Master_info;
class Sys_var_rpl_filter: public sys_var
{
private:
void (Rpl_filter::*get_filter_value)(String *str);
int (Rpl_filter::*set_filter_value)(const char *spec);
privilege_t m_access_global;
public:
Sys_var_rpl_filter(const char *name,
decltype(get_filter_value) get_filter_value,
decltype(set_filter_value) set_filter_value,
const char *comment,
privilege_t access_global)
: sys_var(&all_sys_vars, name, comment, sys_var::GLOBAL, 0, NO_GETOPT,
NO_ARG, SHOW_CHAR, 0, NULL, VARIABLE_NOT_IN_BINLOG,
NULL, NULL, NULL),
get_filter_value(get_filter_value), set_filter_value(set_filter_value),
m_access_global(access_global)
{
option.var_type|= GET_STR | GET_ASK_ADDR;
}
bool do_check(THD *thd, set_var *var) override
{
return Sys_var_charptr::do_string_check(thd, var, charset(thd));
}
void session_save_default(THD *, set_var *) override
{ DBUG_ASSERT(FALSE); }
void global_save_default(THD *thd, set_var *var) override
{
char *ptr= (char*)(intptr)option.def_value;
var->save_result.string_value.str= ptr;
var->save_result.string_value.length= ptr ? strlen(ptr) : 0;
}
bool session_update(THD *, set_var *) override
{
DBUG_ASSERT(FALSE);
return true;
}
bool global_update(THD *thd, set_var *var) override;
bool on_check_access_global(THD *thd) const override
{
return check_global_access(thd, m_access_global);
}
protected:
const uchar *global_value_ptr(THD *thd, const LEX_CSTRING *base)
const override;
};
class Sys_var_binlog_filter: public sys_var
{
private:
void (Rpl_filter::*get_filter_value)(String *str);
privilege_t m_access_global;
public:
Sys_var_binlog_filter(const char *name,
decltype(get_filter_value) get_filter_value,
const char *comment,
privilege_t access_global)
: sys_var(&all_sys_vars, name, comment, sys_var::READONLY+sys_var::GLOBAL, 0, NO_GETOPT,
NO_ARG, SHOW_CHAR, 0, NULL, VARIABLE_NOT_IN_BINLOG,
NULL, NULL, NULL), get_filter_value(get_filter_value),
m_access_global(access_global)
{
option.var_type|= GET_STR;
}
bool do_check(THD *thd, set_var *var) override
{
DBUG_ASSERT(FALSE);
return true;
}
void session_save_default(THD *, set_var *) override
{ DBUG_ASSERT(FALSE); }
void global_save_default(THD *thd, set_var *var) override
{ DBUG_ASSERT(FALSE); }
bool session_update(THD *, set_var *) override
{
DBUG_ASSERT(FALSE);
return true;
}
bool global_update(THD *thd, set_var *var) override
{
DBUG_ASSERT(FALSE);
return true;
}
bool on_check_access_global(THD *thd) const override
{
return check_global_access(thd, m_access_global);
}
protected:
const uchar *global_value_ptr(THD *thd, const LEX_CSTRING *base)
const override;
};
/**
The class for string variables. Useful for strings that aren't necessarily
\0-terminated. Otherwise the same as Sys_var_charptr.
Backing store: LEX_CSTRING
@note
Behaves exactly as Sys_var_charptr, only the backing store is
different.
Note that for global variables handle_options() only sets the
pointer, whereas the length must be updated manually to match, which
is done in mysqld.cc. See e.g. opt_init_connect. TODO: it should be
done automatically when we'll have more Sys_var_lexstring variables
to justify it. Maybe some kind of a loop over all variables, like
sys_var_end() in set_var.cc?
Note that as a subclass of Sys_var_charptr, the memory management
for session Sys_var_lexstring's is manual too, see notes of
Sys_var_charptr and for example default_master_connection.
*/
class Sys_var_lexstring: public Sys_var_charptr
{
public:
Sys_var_lexstring(const char *name_arg,
const char *comment, int flag_args, ptrdiff_t off, size_t size,
CMD_LINE getopt,
const char *def_val, PolyLock *lock=0,
enum binlog_status_enum binlog_status_arg=VARIABLE_NOT_IN_BINLOG,
on_check_function on_check_func=0,
on_update_function on_update_func=0,
const char *substitute=0)
: Sys_var_charptr(name_arg, comment, flag_args, off, sizeof(char*),
getopt, def_val, lock, binlog_status_arg,
on_check_func, on_update_func, substitute)
{
global_var(LEX_CSTRING).length= strlen(def_val);
SYSVAR_ASSERT(size == sizeof(LEX_CSTRING));
*const_cast<SHOW_TYPE*>(&show_val_type)= SHOW_LEX_STRING;
}
bool global_update(THD *thd, set_var *var) override
{
if (Sys_var_charptr::global_update(thd, var))
return true;
global_var(LEX_CSTRING).length= var->save_result.string_value.length;
return false;
}
bool session_update(THD *thd, set_var *var) override
{
if (Sys_var_charptr::session_update(thd, var))
return true;
session_var(thd, LEX_CSTRING).length= var->save_result.string_value.length;
return false;
}
};
#ifndef DBUG_OFF
/**
@@session.debug_dbug and @@global.debug_dbug variables.
@@dbug variable differs from other variables in one aspect:
if its value is not assigned in the session, it "points" to the global
value, and so when the global value is changed, the change
immediately takes effect in the session.
This semantics is intentional, to be able to debug one session from
another.
*/
class Sys_var_dbug: public sys_var
{
public:
Sys_var_dbug(const char *name_arg,
const char *comment, int flag_args,
CMD_LINE getopt,
const char *def_val, PolyLock *lock=0,
enum binlog_status_enum binlog_status_arg=VARIABLE_NOT_IN_BINLOG,
on_check_function on_check_func=0,
on_update_function on_update_func=0,
const char *substitute=0)
: sys_var(&all_sys_vars, name_arg, comment, flag_args,
(char*)¤t_dbug_option-(char*)&global_system_variables, getopt.id,
getopt.arg_type, SHOW_CHAR, (intptr)def_val,
lock, binlog_status_arg, on_check_func, on_update_func,
substitute)
{ option.var_type|= GET_STR; }
bool do_check(THD *thd, set_var *var) override
{
bool rc= Sys_var_charptr::do_string_check(thd, var, charset(thd));
if (var->save_result.string_value.str == nullptr)
var->save_result.string_value.str= const_cast<char*>("");
return rc;
}
bool session_update(THD *thd, set_var *var) override
{
const char *val= var->save_result.string_value.str;
if (!var->value)
DBUG_POP();
else
DBUG_SET(val);
return false;
}
bool global_update(THD *thd, set_var *var) override
{
const char *val= var->save_result.string_value.str;
DBUG_SET_INITIAL(val);
return false;
}
void session_save_default(THD *thd, set_var *var) override
{ }
void global_save_default(THD *thd, set_var *var) override
{
char *ptr= (char*)(intptr)option.def_value;
var->save_result.string_value.str= ptr;
var->save_result.string_value.length= safe_strlen(ptr);
}
const uchar *session_value_ptr(THD *thd, const LEX_CSTRING *base) const override
{
char buf[256];
DBUG_EXPLAIN(buf, sizeof(buf));
return (uchar*) thd->strdup(buf);
}
const uchar *global_value_ptr(THD *thd, const LEX_CSTRING *base) const override
{
char buf[256];
DBUG_EXPLAIN_INITIAL(buf, sizeof(buf));
return (uchar*) thd->strdup(buf);
}
const uchar *default_value_ptr(THD *thd) const override
{ return (uchar*)""; }
};
#endif
#define KEYCACHE_VAR(X) GLOBAL_VAR(dflt_key_cache_var.X)
#define keycache_var_ptr(KC, OFF) (((uchar*)(KC))+(OFF))
#define keycache_var(KC, OFF) (*(ulonglong*)keycache_var_ptr(KC, OFF))
typedef bool (*keycache_update_function)(THD *, KEY_CACHE *, ptrdiff_t, ulonglong);