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
|
/* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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-1301 USA */
#include "sql/sql_plugin_var.h"
#include <limits.h>
#include <string>
#include <unordered_map>
#include <utility>
#include "m_ctype.h"
#include "m_string.h"
#include "map_helpers.h"
#include "my_dbug.h"
#include "my_list.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/status_var.h"
#include "sql/current_thd.h"
#include "sql/item.h"
#include "sql/mysqld.h"
#include "sql/psi_memory_key.h"
#include "sql/set_var.h"
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/sql_plugin.h"
#include "sql/strfunc.h" // find_type
#include "sql/sys_vars_shared.h"
#include "sql/system_variables.h"
#include "sql_string.h"
#include "template_utils.h"
#include "typelib.h"
/**
Set value for global variable with PLUGIN_VAR_MEMALLOC flag.
@param[in] thd Thread context.
@param[in] var Plugin variable.
@param[in,out] dest Destination memory pointer.
@param[in] value '\0'-terminated new value.
@return Completion status
@retval false Success
@retval true Failure
*/
bool plugin_var_memalloc_global_update(THD *thd, SYS_VAR *var, char **dest,
const char *value) {
char *old_value = *dest;
DBUG_EXECUTE_IF("simulate_bug_20292712", my_sleep(1000););
DBUG_TRACE;
if (value && !(value = my_strdup(key_memory_global_system_variables, value,
MYF(MY_WME))))
return true;
var->update(thd, var, (void **)dest, (const void *)&value);
if (old_value) my_free(old_value);
return false;
}
/**
Set value for thread local variable with PLUGIN_VAR_MEMALLOC flag.
@param[in] thd Thread context.
@param[in] var Plugin variable.
@param[in,out] dest Destination memory pointer.
@param[in] value '\0'-terminated new value.
Most plugin variable values are stored on dynamic_variables_ptr.
Releasing memory occupied by these values is as simple as freeing
dynamic_variables_ptr.
An exception to the rule are PLUGIN_VAR_MEMALLOC variables, which
are stored on individual memory hunks. All of these hunks has to
be freed when it comes to cleanup.
It may happen that a plugin was uninstalled and descriptors of
it's variables are lost. In this case it is impossible to locate
corresponding values.
In addition to allocating and setting variable value, new element
is added to dynamic_variables_allocs list. When thread is done, it
has to call plugin_var_memalloc_free() to release memory used by
PLUGIN_VAR_MEMALLOC variables.
If var is NULL, variable update function is not called. This is
needed when we take snapshot of system variables during thread
initialization.
@note List element and variable value are stored on the same memory
hunk. List element is followed by variable value.
@return Completion status
@retval false Success
@retval true Failure
*/
bool plugin_var_memalloc_session_update(THD *thd, SYS_VAR *var, char **dest,
const char *value)
{
LIST *old_element = nullptr;
struct System_variables *vars = &thd->variables;
DBUG_TRACE;
if (value) {
size_t length = strlen(value) + 1;
LIST *element;
if (!(element = (LIST *)my_malloc(key_memory_THD_variables,
sizeof(LIST) + length, MYF(MY_WME))))
return true;
memcpy(element + 1, value, length);
value = (const char *)(element + 1);
vars->dynamic_variables_allocs =
list_add(vars->dynamic_variables_allocs, element);
}
if (*dest) old_element = (LIST *)(*dest - sizeof(LIST));
if (var)
var->update(thd, var, (void **)dest, (const void *)&value);
else
*dest = const_cast<char *>(value);
if (old_element) {
vars->dynamic_variables_allocs =
list_delete(vars->dynamic_variables_allocs, old_element);
my_free(old_element);
}
return false;
}
SHOW_TYPE pluginvar_show_type(SYS_VAR *plugin_var) {
switch (plugin_var->flags & PLUGIN_VAR_WITH_SIGN_TYPEMASK) {
case PLUGIN_VAR_BOOL:
return SHOW_MY_BOOL;
case PLUGIN_VAR_INT:
return SHOW_SIGNED_INT;
case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED:
return SHOW_INT;
case PLUGIN_VAR_LONG:
return SHOW_SIGNED_LONG;
case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED:
return SHOW_LONG;
case PLUGIN_VAR_LONGLONG:
return SHOW_SIGNED_LONGLONG;
case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED:
return SHOW_LONGLONG;
case PLUGIN_VAR_STR:
return SHOW_CHAR_PTR;
case PLUGIN_VAR_ENUM:
case PLUGIN_VAR_SET:
return SHOW_CHAR;
case PLUGIN_VAR_DOUBLE:
return SHOW_DOUBLE;
default:
assert(0);
return SHOW_UNDEF;
}
}
/*
returns a pointer to the memory which holds the thd-local variable or
a pointer to the global variable if thd==null.
If required, will sync with global variables if the requested variable
has not yet been allocated in the current thread.
*/
uchar *intern_sys_var_ptr(THD *thd, int offset, bool global_lock) {
assert(offset >= 0);
assert((uint)offset <= global_system_variables.dynamic_variables_head);
if (!thd)
return (uchar *)global_system_variables.dynamic_variables_ptr + offset;
/*
dynamic_variables_head points to the largest valid offset
*/
if (!thd->variables.dynamic_variables_ptr ||
(uint)offset > thd->variables.dynamic_variables_head) {
/* Current THD only. Don't trigger resync on remote THD. */
if (current_thd == thd)
alloc_and_copy_thd_dynamic_variables(thd, global_lock);
else
return (uchar *)global_system_variables.dynamic_variables_ptr + offset;
}
return (uchar *)thd->variables.dynamic_variables_ptr + offset;
}
/****************************************************************************
Value type thunks, allows the C world to play in the C++ world
****************************************************************************/
int item_value_type(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;
}
}
const char *item_val_str(st_mysql_value *value, char *buffer, int *length) {
String str(buffer, *length, system_charset_info), *res;
if (!(res = ((st_item_value_holder *)value)->item->val_str(&str)))
return nullptr;
*length = static_cast<int>(res->length());
if (res->c_ptr_quick() == buffer) return buffer;
/*
Lets be nice and create a temporary string since the
buffer was too small
*/
return current_thd->strmake(res->c_ptr_quick(), res->length());
}
int item_val_int(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;
}
int item_is_unsigned(st_mysql_value *value) {
Item *item = ((st_item_value_holder *)value)->item;
return item->unsigned_flag;
}
int item_val_real(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;
}
bool sys_var_pluginvar::check_update_type(Item_result type) {
switch (plugin_var->flags & PLUGIN_VAR_TYPEMASK) {
case PLUGIN_VAR_INT:
case PLUGIN_VAR_LONG:
case PLUGIN_VAR_LONGLONG:
return type != INT_RESULT;
case PLUGIN_VAR_STR:
return type != STRING_RESULT;
case PLUGIN_VAR_ENUM:
case PLUGIN_VAR_BOOL:
case PLUGIN_VAR_SET:
return type != STRING_RESULT && type != INT_RESULT;
case PLUGIN_VAR_DOUBLE:
return type != INT_RESULT && type != REAL_RESULT &&
type != DECIMAL_RESULT;
default:
return true;
}
}
uchar *sys_var_pluginvar::real_value_ptr(THD *thd, enum_var_type type) {
assert(thd || (type == OPT_GLOBAL) || (type == OPT_PERSIST));
if (plugin_var->flags & PLUGIN_VAR_THDLOCAL) {
/* scope of OPT_PERSIST is always GLOBAL */
if (type == OPT_GLOBAL || type == OPT_PERSIST) thd = nullptr;
return intern_sys_var_ptr(thd, *(int *)(plugin_var + 1), false);
}
return *(uchar **)(plugin_var + 1);
}
TYPELIB *sys_var_pluginvar::plugin_var_typelib(void) {
switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_THDLOCAL)) {
case PLUGIN_VAR_ENUM:
return ((sysvar_enum_t *)plugin_var)->typelib;
case PLUGIN_VAR_SET:
return ((sysvar_set_t *)plugin_var)->typelib;
case PLUGIN_VAR_ENUM | PLUGIN_VAR_THDLOCAL:
return ((thdvar_enum_t *)plugin_var)->typelib;
case PLUGIN_VAR_SET | PLUGIN_VAR_THDLOCAL:
return ((thdvar_set_t *)plugin_var)->typelib;
default:
return nullptr;
}
return nullptr; /* Keep compiler happy */
}
uchar *sys_var_pluginvar::do_value_ptr(THD *running_thd, THD *target_thd,
enum_var_type type, std::string_view) {
uchar *result;
result = real_value_ptr(target_thd, type);
if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_ENUM)
result = pointer_cast<uchar *>(const_cast<char *>(
get_type(plugin_var_typelib(), *pointer_cast<ulong *>(result))));
else if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_SET)
result = (uchar *)set_to_string(running_thd, nullptr, *(ulonglong *)result,
plugin_var_typelib()->type_names);
return result;
}
bool sys_var_pluginvar::do_check(THD *thd, set_var *var) {
st_item_value_holder value;
assert(plugin_var->check);
value.value_type = item_value_type;
value.val_str = item_val_str;
value.val_int = item_val_int;
value.val_real = item_val_real;
value.is_unsigned = item_is_unsigned;
value.item = var->value;
return plugin_var->check(thd, plugin_var, &var->save_result, &value);
}
bool sys_var_pluginvar::session_update(THD *thd, set_var *var) {
bool rc = false;
assert(!is_readonly());
assert(plugin_var->flags & PLUGIN_VAR_THDLOCAL);
assert(thd == current_thd);
mysql_mutex_lock(&LOCK_global_system_variables);
void *tgt = real_value_ptr(thd, var->type);
const void *src = var->value ? (void *)&var->save_result
: (void *)real_value_ptr(thd, OPT_GLOBAL);
mysql_mutex_unlock(&LOCK_global_system_variables);
if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
plugin_var->flags & PLUGIN_VAR_MEMALLOC)
rc = plugin_var_memalloc_session_update(thd, plugin_var,
static_cast<char **>(tgt),
*static_cast<char *const *>(src));
else
plugin_var->update(thd, plugin_var, tgt, src);
return rc;
}
bool sys_var_pluginvar::global_update(THD *thd, set_var *var) {
bool rc = false;
assert(!is_readonly());
mysql_mutex_assert_owner(&LOCK_global_system_variables);
void *tgt = real_value_ptr(thd, var->type);
const void *src = &var->save_result;
if (!var->value) {
switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_THDLOCAL)) {
case PLUGIN_VAR_INT:
src = &((sysvar_uint_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_LONG:
src = &((sysvar_ulong_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_LONGLONG:
src = &((sysvar_ulonglong_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_ENUM:
src = &((sysvar_enum_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_SET:
src = &((sysvar_set_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_BOOL:
src = &((sysvar_bool_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_STR:
src = &((sysvar_str_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_DOUBLE:
src = &((sysvar_double_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_INT | PLUGIN_VAR_THDLOCAL:
src = &((thdvar_uint_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_LONG | PLUGIN_VAR_THDLOCAL:
src = &((thdvar_ulong_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_THDLOCAL:
src = &((thdvar_ulonglong_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_ENUM | PLUGIN_VAR_THDLOCAL:
src = &((thdvar_enum_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_SET | PLUGIN_VAR_THDLOCAL:
src = &((thdvar_set_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_BOOL | PLUGIN_VAR_THDLOCAL:
src = &((thdvar_bool_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_STR | PLUGIN_VAR_THDLOCAL:
src = &((thdvar_str_t *)plugin_var)->def_val;
break;
case PLUGIN_VAR_DOUBLE | PLUGIN_VAR_THDLOCAL:
src = &((thdvar_double_t *)plugin_var)->def_val;
break;
default:
assert(0);
}
}
if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_STR &&
plugin_var->flags & PLUGIN_VAR_MEMALLOC) {
rc = plugin_var_memalloc_global_update(thd, plugin_var,
static_cast<char **>(tgt),
*static_cast<char *const *>(src));
} else {
plugin_var->update(thd, plugin_var, tgt, src);
return (thd->is_error() ? 1 : 0);
}
return rc;
}
longlong sys_var_pluginvar::get_min_value() {
switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_THDLOCAL)) {
case PLUGIN_VAR_INT:
return ((sysvar_uint_t *)plugin_var)->min_val;
case PLUGIN_VAR_LONG:
return ((sysvar_ulong_t *)plugin_var)->min_val;
case PLUGIN_VAR_LONGLONG:
return ((sysvar_ulonglong_t *)plugin_var)->min_val;
case PLUGIN_VAR_DOUBLE:
return ((sysvar_double_t *)plugin_var)->min_val;
case PLUGIN_VAR_INT | PLUGIN_VAR_THDLOCAL:
return ((thdvar_uint_t *)plugin_var)->min_val;
case PLUGIN_VAR_LONG | PLUGIN_VAR_THDLOCAL:
return ((thdvar_ulong_t *)plugin_var)->min_val;
case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_THDLOCAL:
return ((thdvar_ulonglong_t *)plugin_var)->min_val;
case PLUGIN_VAR_DOUBLE | PLUGIN_VAR_THDLOCAL:
return ((thdvar_double_t *)plugin_var)->min_val;
}
return 0;
}
ulonglong sys_var_pluginvar::get_max_value() {
switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_THDLOCAL)) {
case PLUGIN_VAR_INT:
return ((sysvar_uint_t *)plugin_var)->max_val;
case PLUGIN_VAR_LONG:
return ((sysvar_ulong_t *)plugin_var)->max_val;
case PLUGIN_VAR_LONGLONG:
return ((sysvar_ulonglong_t *)plugin_var)->max_val;
case PLUGIN_VAR_DOUBLE:
return ((sysvar_double_t *)plugin_var)->max_val;
case PLUGIN_VAR_INT | PLUGIN_VAR_THDLOCAL:
return ((thdvar_uint_t *)plugin_var)->max_val;
case PLUGIN_VAR_LONG | PLUGIN_VAR_THDLOCAL:
return ((thdvar_ulong_t *)plugin_var)->max_val;
case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_THDLOCAL:
return ((thdvar_ulonglong_t *)plugin_var)->max_val;
case PLUGIN_VAR_DOUBLE | PLUGIN_VAR_THDLOCAL:
return ((thdvar_double_t *)plugin_var)->max_val;
}
return 0;
}
/**
Enforce the NO DEFAULT policy for plugin system variables
A plugin variable does not explicitly call the plugin supplied check function
when setting the default value, e.g. SET @<plugin_var@> = DEFAULT.
But when the PLUGIN_VAR_NODEFAULT is set setting the default value is
prohibited.
This function gets called after the actual check done by
sys_var_pluginvar::do_check() so it does not need to check again.
it only needs to enforce the PLUGIN_VAR_NODEFAULT flag.
There's no need for special error hence just returning true is enough.
@sa sys_var::on_check_function, sys_var::check,
sys_var_pluginvar::do_check(), PLUGIN_VAR_NODEFAULT
@param self the sys_var structure for the variable being set
@param var the data about the value being set
@return is the setting valid
@retval true not valid
@retval false valid
*/
bool sys_var_pluginvar::on_check_pluginvar(sys_var *self [[maybe_unused]],
THD *, set_var *var) {
/* This handler is installed only if NO_DEFAULT is specified */
assert(((sys_var_pluginvar *)self)->plugin_var->flags & PLUGIN_VAR_NODEFAULT);
return (!var->value);
}
void sys_var_pluginvar::saved_value_to_string(THD *, set_var *var,
char *def_val) {
if (!var->value) {
switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_THDLOCAL)) {
case PLUGIN_VAR_INT:
var->save_result.ulonglong_value =
((sysvar_uint_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, -10);
return;
case PLUGIN_VAR_LONG:
var->save_result.ulonglong_value =
((sysvar_ulong_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, -10);
return;
case PLUGIN_VAR_LONGLONG:
var->save_result.ulonglong_value =
((sysvar_ulonglong_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, 10);
return;
case PLUGIN_VAR_ENUM:
var->save_result.ulonglong_value =
((sysvar_enum_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, 10);
return;
case PLUGIN_VAR_BOOL:
var->save_result.ulonglong_value =
((sysvar_bool_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, 10);
return;
case PLUGIN_VAR_STR:
if (((sysvar_str_t *)plugin_var)->def_val != nullptr)
strcpy(def_val, ((sysvar_str_t *)plugin_var)->def_val);
else /* no default: consider empty */
def_val[0] = 0;
return;
case PLUGIN_VAR_DOUBLE:
var->save_result.double_value =
((sysvar_double_t *)plugin_var)->def_val;
my_fcvt(var->save_result.double_value, 6, def_val, nullptr);
return;
case PLUGIN_VAR_INT | PLUGIN_VAR_THDLOCAL:
var->save_result.ulonglong_value =
((thdvar_uint_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, -10);
return;
case PLUGIN_VAR_LONG | PLUGIN_VAR_THDLOCAL:
var->save_result.ulonglong_value =
((thdvar_ulong_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, -10);
return;
case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_THDLOCAL:
var->save_result.ulonglong_value =
((thdvar_ulonglong_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, -10);
return;
case PLUGIN_VAR_ENUM | PLUGIN_VAR_THDLOCAL:
var->save_result.ulonglong_value =
((thdvar_enum_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, 10);
return;
case PLUGIN_VAR_BOOL | PLUGIN_VAR_THDLOCAL:
var->save_result.ulonglong_value =
((thdvar_bool_t *)plugin_var)->def_val;
longlong10_to_str(var->save_result.ulonglong_value, def_val, 10);
return;
case PLUGIN_VAR_STR | PLUGIN_VAR_THDLOCAL:
if (((thdvar_str_t *)plugin_var)->def_val != nullptr)
strcpy(def_val, ((thdvar_str_t *)plugin_var)->def_val);
else /* no default: consider empty */
def_val[0] = 0;
return;
case PLUGIN_VAR_DOUBLE | PLUGIN_VAR_THDLOCAL:
var->save_result.double_value =
((thdvar_double_t *)plugin_var)->def_val;
my_fcvt(var->save_result.double_value, 6, def_val, nullptr);
return;
default:
assert(0);
}
}
}
/****************************************************************************
default variable data check and update functions
****************************************************************************/
int check_func_bool(THD *, SYS_VAR *, void *save, st_mysql_value *value) {
char buff[STRING_BUFFER_USUAL_SIZE];
const char *str;
int result, length;
long long tmp;
if (value->value_type(value) == MYSQL_VALUE_TYPE_STRING) {
length = sizeof(buff);
if (!(str = value->val_str(value, buff, &length)) ||
(result = find_type(&bool_typelib, str, length, true) - 1) < 0)
goto err;
} else {
if (value->val_int(value, &tmp) < 0) goto err;
if (tmp > 1 || tmp < 0) goto err;
result = (int)tmp;
}
*(bool *)save = (result != 0);
return 0;
err:
return 1;
}
int check_func_int(THD *thd, SYS_VAR *var, void *save, st_mysql_value *value) {
bool fixed1, fixed2;
long long orig, val;
struct my_option options;
value->val_int(value, &orig);
val = orig;
plugin_opt_set_limits(&options, var);
if (var->flags & PLUGIN_VAR_UNSIGNED) {
if ((fixed1 = (!value->is_unsigned(value) && val < 0))) val = 0;
*(uint *)save =
(uint)getopt_ull_limit_value((ulonglong)val, &options, &fixed2);
} else {
if ((fixed1 = (value->is_unsigned(value) && val < 0))) val = LLONG_MAX;
*(int *)save = (int)getopt_ll_limit_value(val, &options, &fixed2);
}
return throw_bounds_warning(thd, var->name, fixed1 || fixed2,
value->is_unsigned(value), orig);
}
int check_func_long(THD *thd, SYS_VAR *var, void *save, st_mysql_value *value) {
bool fixed1, fixed2;
long long orig, val;
struct my_option options;
value->val_int(value, &orig);
val = orig;
plugin_opt_set_limits(&options, var);
if (var->flags & PLUGIN_VAR_UNSIGNED) {
if ((fixed1 = (!value->is_unsigned(value) && val < 0))) val = 0;
*(ulong *)save =
(ulong)getopt_ull_limit_value((ulonglong)val, &options, &fixed2);
} else {
if ((fixed1 = (value->is_unsigned(value) && val < 0))) val = LLONG_MAX;
*(long *)save = (long)getopt_ll_limit_value(val, &options, &fixed2);
}
return throw_bounds_warning(thd, var->name, fixed1 || fixed2,
value->is_unsigned(value), orig);
}
int check_func_longlong(THD *thd, SYS_VAR *var, void *save,
st_mysql_value *value) {
bool fixed1, fixed2;
long long orig, val;
struct my_option options;
value->val_int(value, &orig);
val = orig;
plugin_opt_set_limits(&options, var);
if (var->flags & PLUGIN_VAR_UNSIGNED) {
if ((fixed1 = (!value->is_unsigned(value) && val < 0))) val = 0;
*(ulonglong *)save =
getopt_ull_limit_value((ulonglong)val, &options, &fixed2);
} else {
if ((fixed1 = (value->is_unsigned(value) && val < 0))) val = LLONG_MAX;
*(longlong *)save = getopt_ll_limit_value(val, &options, &fixed2);
}
return throw_bounds_warning(thd, var->name, fixed1 || fixed2,
value->is_unsigned(value), orig);
}
int check_func_str(THD *thd, SYS_VAR *, void *save, st_mysql_value *value) {
char buff[STRING_BUFFER_USUAL_SIZE];
const char *str;
int length;
length = sizeof(buff);
if ((str = value->val_str(value, buff, &length)))
str = thd->strmake(str, length);
*(const char **)save = str;
return 0;
}
int check_func_enum(THD *, SYS_VAR *var, void *save, st_mysql_value *value) {
char buff[STRING_BUFFER_USUAL_SIZE];
const char *str;
TYPELIB *typelib;
long long tmp;
long result;
int length;
if (var->flags & PLUGIN_VAR_THDLOCAL)
typelib = ((thdvar_enum_t *)var)->typelib;
else
typelib = ((sysvar_enum_t *)var)->typelib;
if (value->value_type(value) == MYSQL_VALUE_TYPE_STRING) {
length = sizeof(buff);
if (!(str = value->val_str(value, buff, &length))) goto err;
if ((result = (long)find_type(typelib, str, length, false) - 1) < 0)
goto err;
} else {
if (value->val_int(value, &tmp)) goto err;
if (tmp < 0 || tmp >= static_cast<long long>(typelib->count)) goto err;
result = (long)tmp;
}
*(long *)save = result;
return 0;
err:
return 1;
}
int check_func_set(THD *, SYS_VAR *var, void *save, st_mysql_value *value) {
const char *str;
TYPELIB *typelib;
ulonglong result;
uint error_len = 0; // init as only set on error
bool not_used;
int length;
if (var->flags & PLUGIN_VAR_THDLOCAL)
typelib = ((thdvar_set_t *)var)->typelib;
else
typelib = ((sysvar_set_t *)var)->typelib;
if (value->value_type(value) == MYSQL_VALUE_TYPE_STRING) {
char buff[STRING_BUFFER_USUAL_SIZE];
const char *error = nullptr;
length = sizeof(buff);
if (!(str = value->val_str(value, buff, &length))) goto err;
result =
find_set(typelib, str, length, nullptr, &error, &error_len, ¬_used);
if (error_len) goto err;
} else {
if (value->val_int(value, (long long *)&result)) goto err;
if (unlikely((result >= (1ULL << typelib->count)) &&
(typelib->count < sizeof(long) * 8)))
goto err;
}
*(ulonglong *)save = result;
return 0;
err:
return 1;
}
int check_func_double(THD *thd, SYS_VAR *var, void *save,
st_mysql_value *value) {
double v;
bool fixed;
struct my_option option;
value->val_real(value, &v);
plugin_opt_set_limits(&option, var);
*(double *)save = getopt_double_limit_value(v, &option, &fixed);
return throw_bounds_warning(thd, var->name, fixed, v);
}
void update_func_bool(THD *, SYS_VAR *, void *tgt, const void *save) {
*static_cast<bool *>(tgt) = *static_cast<const bool *>(save);
}
void update_func_int(THD *, SYS_VAR *, void *tgt, const void *save) {
*static_cast<int *>(tgt) = *static_cast<const int *>(save);
}
void update_func_long(THD *, SYS_VAR *, void *tgt, const void *save) {
*static_cast<long *>(tgt) = *static_cast<const long *>(save);
}
void update_func_longlong(THD *, SYS_VAR *, void *tgt, const void *save) {
*static_cast<longlong *>(tgt) = *static_cast<const ulonglong *>(save);
}
void update_func_str(THD *, SYS_VAR *, void *tgt, const void *save) {
*static_cast<char **>(tgt) = *static_cast<char *const *>(save);
}
void update_func_double(THD *, SYS_VAR *, void *tgt, const void *save) {
*static_cast<double *>(tgt) = *static_cast<const double *>(save);
}
/*
called by register_var, construct_options and test_plugin_options.
Returns the 'bookmark' for the named variable.
LOCK_system_variables_hash should be at least read locked
*/
st_bookmark *find_bookmark(const char *plugin, const char *name, int flags) {
size_t namelen, length, pluginlen = 0;
char *varname, *p;
if (!(flags & PLUGIN_VAR_THDLOCAL)) return nullptr;
namelen = strlen(name);
if (plugin) pluginlen = strlen(plugin) + 1;
length = namelen + pluginlen + 2;
varname = (char *)my_alloca(length);
if (plugin) {
strxmov(varname + 1, plugin, "_", name, NullS);
for (p = varname + 1; *p; p++)
if (*p == '-') *p = '_';
} else
memcpy(varname + 1, name, namelen + 1);
varname[0] = flags & PLUGIN_VAR_TYPEMASK;
const auto it = get_bookmark_hash()->find(std::string(varname, length - 1));
if (it == get_bookmark_hash()->end()) return nullptr;
return it->second;
}
void plugin_opt_set_limits(struct my_option *options, const SYS_VAR *opt) {
switch (opt->flags &
(PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_THDLOCAL)) {
/* global system variables */
case PLUGIN_VAR_INT:
OPTION_SET_LIMITS(GET_INT, options,
pointer_cast<const sysvar_int_t *>(opt));
break;
case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED:
OPTION_SET_LIMITS(GET_UINT, options,
pointer_cast<const sysvar_uint_t *>(opt));
break;
case PLUGIN_VAR_LONG:
OPTION_SET_LIMITS(GET_LONG, options,
pointer_cast<const sysvar_long_t *>(opt));
break;
case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED:
OPTION_SET_LIMITS(GET_ULONG, options,
pointer_cast<const sysvar_ulong_t *>(opt));
break;
case PLUGIN_VAR_LONGLONG:
OPTION_SET_LIMITS(GET_LL, options,
pointer_cast<const sysvar_longlong_t *>(opt));
break;
case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED:
OPTION_SET_LIMITS(GET_ULL, options,
pointer_cast<const sysvar_ulonglong_t *>(opt));
break;
case PLUGIN_VAR_ENUM:
options->var_type = GET_ENUM;
options->typelib = pointer_cast<const sysvar_enum_t *>(opt)->typelib;
options->def_value = pointer_cast<const sysvar_enum_t *>(opt)->def_val;
options->min_value = options->block_size = 0;
options->max_value = options->typelib->count - 1;
break;
case PLUGIN_VAR_SET:
options->var_type = GET_SET;
options->typelib = pointer_cast<const sysvar_set_t *>(opt)->typelib;
options->def_value = pointer_cast<const sysvar_set_t *>(opt)->def_val;
options->min_value = options->block_size = 0;
options->max_value = (1ULL << options->typelib->count) - 1;
break;
case PLUGIN_VAR_BOOL:
options->var_type = GET_BOOL;
options->def_value = pointer_cast<const sysvar_bool_t *>(opt)->def_val;
break;
case PLUGIN_VAR_STR:
options->var_type =
((opt->flags & PLUGIN_VAR_MEMALLOC) ? GET_STR_ALLOC : GET_STR);
options->def_value =
(intptr)pointer_cast<const sysvar_str_t *>(opt)->def_val;
break;
case PLUGIN_VAR_DOUBLE:
OPTION_SET_LIMITS_DOUBLE(options,
pointer_cast<const sysvar_double_t *>(opt));
break;
/* threadlocal variables */
case PLUGIN_VAR_INT | PLUGIN_VAR_THDLOCAL:
OPTION_SET_LIMITS(GET_INT, options,
pointer_cast<const thdvar_int_t *>(opt));
break;
case PLUGIN_VAR_INT | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_THDLOCAL:
OPTION_SET_LIMITS(GET_UINT, options,
pointer_cast<const thdvar_uint_t *>(opt));
break;
case PLUGIN_VAR_LONG | PLUGIN_VAR_THDLOCAL:
OPTION_SET_LIMITS(GET_LONG, options,
pointer_cast<const thdvar_long_t *>(opt));
break;
case PLUGIN_VAR_LONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_THDLOCAL:
OPTION_SET_LIMITS(GET_ULONG, options,
pointer_cast<const thdvar_ulong_t *>(opt));
break;
case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_THDLOCAL:
OPTION_SET_LIMITS(GET_LL, options,
pointer_cast<const thdvar_longlong_t *>(opt));
break;
case PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_THDLOCAL:
OPTION_SET_LIMITS(GET_ULL, options,
pointer_cast<const thdvar_ulonglong_t *>(opt));
break;
case PLUGIN_VAR_DOUBLE | PLUGIN_VAR_THDLOCAL:
OPTION_SET_LIMITS_DOUBLE(options,
pointer_cast<const thdvar_double_t *>(opt));
break;
case PLUGIN_VAR_ENUM | PLUGIN_VAR_THDLOCAL:
options->var_type = GET_ENUM;
options->typelib = pointer_cast<const thdvar_enum_t *>(opt)->typelib;
options->def_value = pointer_cast<const thdvar_enum_t *>(opt)->def_val;
options->min_value = options->block_size = 0;
options->max_value = options->typelib->count - 1;
break;
case PLUGIN_VAR_SET | PLUGIN_VAR_THDLOCAL:
options->var_type = GET_SET;
options->typelib = pointer_cast<const thdvar_set_t *>(opt)->typelib;
options->def_value = pointer_cast<const thdvar_set_t *>(opt)->def_val;
options->min_value = options->block_size = 0;
options->max_value = (1ULL << options->typelib->count) - 1;
break;
case PLUGIN_VAR_BOOL | PLUGIN_VAR_THDLOCAL:
options->var_type = GET_BOOL;
options->def_value = pointer_cast<const thdvar_bool_t *>(opt)->def_val;
break;
case PLUGIN_VAR_STR | PLUGIN_VAR_THDLOCAL:
options->var_type =
((opt->flags & PLUGIN_VAR_MEMALLOC) ? GET_STR_ALLOC : GET_STR);
options->def_value =
(intptr)pointer_cast<const thdvar_str_t *>(opt)->def_val;
break;
default:
assert(0);
}
options->arg_type = REQUIRED_ARG;
if (opt->flags & PLUGIN_VAR_NOCMDARG) options->arg_type = NO_ARG;
if (opt->flags & PLUGIN_VAR_OPCMDARG) options->arg_type = OPT_ARG;
}
|