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
|
/* Copyright (c) 2010, 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/rpl_info_file.h"
#include "my_config.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my_inttypes.h"
#include "my_loglevel.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "m_string.h"
#include "my_dbug.h"
#include "my_dir.h" // MY_STAT
#include "my_thread_local.h" // my_errno
#include "mysql/components/services/log_builtins.h"
#include "mysql/service_mysql_alloc.h"
#include "mysqld_error.h" // ER_*
#include "sql/dynamic_ids.h" // Server_ids
#include "sql/log.h"
#include "sql/mysqld.h" // mysql_data_home
#include "sql/psi_memory_key.h"
#include "sql_string.h"
long init_ulongvar_from_file(ulong *var, IO_CACHE *f, ulong default_val);
long init_strvar_from_file(char *var, size_t max_size, IO_CACHE *f,
const char *default_val);
long init_intvar_from_file(int *var, IO_CACHE *f, int default_val);
long init_floatvar_from_file(float *var, IO_CACHE *f, float default_val);
long init_dynarray_intvar_from_file(char *buffer, size_t size,
char **buffer_act, IO_CACHE *f);
Rpl_info_file::Rpl_info_file(const int nparam, const char *param_pattern_fname,
const char *param_info_fname, bool indexed_arg,
MY_BITMAP const *nullable_bitmap)
: Rpl_info_handler(nparam, nullable_bitmap),
info_fd(-1),
name_indexed(indexed_arg) {
DBUG_TRACE;
fn_format(pattern_fname, param_pattern_fname, mysql_data_home, "", 4 + 32);
fn_format(info_fname, param_info_fname, mysql_data_home, "", 4 + 32);
}
Rpl_info_file::~Rpl_info_file() {
DBUG_TRACE;
do_end_info();
}
int Rpl_info_file::do_init_info(uint instance) {
DBUG_TRACE;
char fname_local[FN_REFLEN];
char *pos = my_stpcpy(fname_local, pattern_fname);
if (name_indexed) sprintf(pos, "%u", instance);
fn_format(info_fname, fname_local, mysql_data_home, "", 4 + 32);
return do_init_info();
}
int Rpl_info_file::do_init_info() {
int error = 0;
DBUG_TRACE;
/* does info file exist ? */
enum_return_check ret_check = do_check_info();
if (ret_check == REPOSITORY_DOES_NOT_EXIST) {
/*
If someone removed the file from underneath our feet, just close
the old descriptor and re-create the old file
*/
if (info_fd >= 0) {
if (my_b_inited(&info_file)) end_io_cache(&info_file);
my_close(info_fd, MYF(MY_WME));
}
if ((info_fd = my_open(info_fname, O_CREAT | O_RDWR, MYF(MY_WME))) < 0) {
LogErr(ERROR_LEVEL, ER_RPL_FAILED_TO_CREATE_NEW_INFO_FILE, info_fname,
my_errno());
error = 1;
} else if (init_io_cache(&info_file, info_fd, IO_SIZE * 2, READ_CACHE, 0L,
false, MYF(MY_WME))) {
LogErr(ERROR_LEVEL, ER_RPL_FAILED_TO_CREATE_CACHE_FOR_INFO_FILE,
info_fname);
error = 1;
}
if (error) {
if (info_fd >= 0) my_close(info_fd, MYF(0));
info_fd = -1;
}
}
/* file exists */
else if (ret_check == REPOSITORY_EXISTS) {
if (info_fd >= 0)
reinit_io_cache(&info_file, READ_CACHE, 0L, false, false);
else {
if ((info_fd = my_open(info_fname, O_RDWR, MYF(MY_WME))) < 0) {
LogErr(ERROR_LEVEL, ER_RPL_FAILED_TO_OPEN_INFO_FILE, info_fname,
my_errno());
error = 1;
} else if (init_io_cache(&info_file, info_fd, IO_SIZE * 2, READ_CACHE, 0L,
false, MYF(MY_WME))) {
LogErr(ERROR_LEVEL, ER_RPL_FAILED_TO_CREATE_CACHE_FOR_INFO_FILE,
info_fname);
error = 1;
}
if (error) {
if (info_fd >= 0) my_close(info_fd, MYF(0));
info_fd = -1;
}
}
} else
error = 1;
return error;
}
int Rpl_info_file::do_prepare_info_for_read() {
cursor = 0;
prv_error = false;
prv_get_error = Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
return (reinit_io_cache(&info_file, READ_CACHE, 0L, false, false));
}
int Rpl_info_file::do_prepare_info_for_write() {
cursor = 0;
prv_error = false;
prv_get_error = Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
return (reinit_io_cache(&info_file, WRITE_CACHE, 0L, false, true));
}
inline enum_return_check do_check_repository_file(const char *fname) {
if (my_access(fname, F_OK)) return REPOSITORY_DOES_NOT_EXIST;
if (my_access(fname, F_OK | R_OK | W_OK)) return ERROR_CHECKING_REPOSITORY;
return REPOSITORY_EXISTS;
}
/*
The method verifies existence of an instance of the repository.
@param instance an index in the repository
@retval REPOSITORY_EXISTS when the check is successful
@retval REPOSITORY_DOES_NOT_EXIST otherwise
@note This method also verifies overall integrity
of the repositories to make sure they are indexed without any gaps.
*/
enum_return_check Rpl_info_file::do_check_info(uint instance) {
uint i;
enum_return_check last_check = REPOSITORY_EXISTS;
char fname_local[FN_REFLEN];
char *pos = nullptr;
for (i = 1; i <= instance && last_check == REPOSITORY_EXISTS; i++) {
pos = my_stpcpy(fname_local, pattern_fname);
if (name_indexed) sprintf(pos, "%u", i);
fn_format(fname_local, fname_local, mysql_data_home, "", 4 + 32);
last_check = do_check_repository_file(fname_local);
}
return last_check;
}
enum_return_check Rpl_info_file::do_check_info() {
return do_check_repository_file(info_fname);
}
/*
The function counts number of files in a range starting
from one. The range degenerates into one item when @c indexed is false.
Scanning ends once the next indexed file is not found.
@param nparam Number of fields
@param param_pattern
a string pattern to generate
the actual file name
@param indexed indicates whether the file is indexed and if so
there is a range to count in.
@param[in] nullable_bitmap
bitmap that holds the fields that are allowed to be
`NULL`.
@param[out] counter the number of discovered instances before the first
unsuccess in locating the next file.
@retval false All OK
@retval true An error
*/
bool Rpl_info_file::do_count_info(const int nparam, const char *param_pattern,
bool indexed,
MY_BITMAP const *nullable_bitmap,
uint *counter) {
uint i = 0;
Rpl_info_file *info = nullptr;
char fname_local[FN_REFLEN];
char *pos = nullptr;
enum_return_check last_check = REPOSITORY_EXISTS;
DBUG_TRACE;
if (!(info = new Rpl_info_file(nparam, param_pattern, "", indexed,
nullable_bitmap)))
return true;
for (i = 1; last_check == REPOSITORY_EXISTS; i++) {
pos = my_stpcpy(fname_local, param_pattern);
if (indexed) {
sprintf(pos, "%u", i);
}
fn_format(fname_local, fname_local, mysql_data_home, "", 4 + 32);
if ((last_check = do_check_repository_file(fname_local)) ==
REPOSITORY_EXISTS)
(*counter)++;
// just one loop pass for MI and RLI file
if (!indexed) break;
}
delete info;
return false;
}
int Rpl_info_file::do_flush_info(const bool force) {
int error = 0;
DBUG_TRACE;
if (flush_io_cache(&info_file)) error = 1;
if (!error && (force || (sync_period && ++(sync_counter) >= sync_period))) {
if (my_sync(info_fd, MYF(MY_WME))) error = 1;
sync_counter = 0;
}
return error;
}
void Rpl_info_file::do_end_info() {
DBUG_TRACE;
if (info_fd >= 0) {
if (my_b_inited(&info_file)) end_io_cache(&info_file);
my_close(info_fd, MYF(MY_WME));
info_fd = -1;
}
}
int Rpl_info_file::do_remove_info() {
MY_STAT stat_area;
int error = 0;
DBUG_TRACE;
if (my_stat(info_fname, &stat_area, MYF(0)) &&
my_delete(info_fname, MYF(MY_WME)))
error = 1;
return error;
}
int Rpl_info_file::do_clean_info() {
/*
There is nothing to do here. Maybe we can truncate the
file in the future. However, for now, there is no need.
*/
return 0;
}
int Rpl_info_file::do_reset_info(const int nparam, const char *param_pattern,
bool indexed,
MY_BITMAP const *nullable_bitmap) {
int error = false;
uint i = 0;
Rpl_info_file *info = nullptr;
char fname_local[FN_REFLEN];
char *pos = nullptr;
enum_return_check last_check = REPOSITORY_EXISTS;
DBUG_TRACE;
if (!(info = new Rpl_info_file(nparam, param_pattern, "", indexed,
nullable_bitmap)))
return true;
for (i = 1; last_check == REPOSITORY_EXISTS; i++) {
pos = my_stpcpy(fname_local, param_pattern);
if (indexed) {
sprintf(pos, "%u", i);
}
fn_format(fname_local, fname_local, mysql_data_home, "", 4 + 32);
if ((last_check = do_check_repository_file(fname_local)) ==
REPOSITORY_EXISTS)
if (my_delete(fname_local, MYF(MY_WME))) error = true;
// just one loop pass for MI and RLI file
if (!indexed) break;
}
delete info;
return error;
}
bool Rpl_info_file::do_set_info(const int pos, const char *value) {
if (value == nullptr) {
return do_set_info(pos, nullptr);
}
return (my_b_printf(&info_file, "%s\n", value) > (size_t)0 ? false : true);
}
bool Rpl_info_file::do_set_info(const int pos, const uchar *value,
const size_t size) {
if (value == nullptr) {
return do_set_info(pos, nullptr, size);
}
return (my_b_write(&info_file, value, size));
}
bool Rpl_info_file::do_set_info(const int, const ulong value) {
return (my_b_printf(&info_file, "%lu\n", value) > (size_t)0 ? false : true);
}
bool Rpl_info_file::do_set_info(const int, const int value) {
return (my_b_printf(&info_file, "%d\n", value) > (size_t)0 ? false : true);
}
bool Rpl_info_file::do_set_info(const int, const float value) {
/*
64 bytes provide enough space considering that the precision is 3
bytes (See the appropriate set function):
FLT_MAX The value of this macro is the maximum number representable
in type float. It is supposed to be at least 1E+37.
FLT_MIN Similar to the FLT_MAX, we have 1E-37.
If a file is manually and not properly changed, this function may
crash the server.
*/
char buffer[64];
sprintf(buffer, "%.3f", value);
return (my_b_printf(&info_file, "%s\n", buffer) > (size_t)0 ? false : true);
}
bool Rpl_info_file::do_set_info(const int, const Server_ids *value) {
bool error = true;
String buffer;
/*
This produces a line listing the total number and all the server_ids.
*/
if (const_cast<Server_ids *>(value)->pack_dynamic_ids(&buffer)) goto err;
error =
(my_b_printf(&info_file, "%s\n", buffer.c_ptr_safe()) > (size_t)0 ? false
: true);
err:
return error;
}
bool Rpl_info_file::do_set_info(const int pos, const std::nullptr_t) {
if (!this->is_field_nullable(pos)) return true;
return (my_b_printf(&info_file, "\n") > (size_t)0 ? false : true);
}
bool Rpl_info_file::do_set_info(const int pos, const std::nullptr_t,
const size_t) {
if (!this->is_field_nullable(pos)) return true;
return (my_b_printf(&info_file, "\n") > (size_t)0 ? false : true);
}
Rpl_info_handler::enum_field_get_status Rpl_info_file::check_for_error(
int pos, long n_read_bytes) {
if (n_read_bytes == 0 && this->is_field_nullable(pos))
return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_IS_NULL;
else if (n_read_bytes < 0)
return Rpl_info_handler::enum_field_get_status::FAILURE;
return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
}
Rpl_info_handler::enum_field_get_status Rpl_info_file::do_get_info(
const int pos, char *value, const size_t size, const char *default_value) {
long n_bytes_read =
init_strvar_from_file(value, size, &info_file, default_value);
return this->check_for_error(pos, n_bytes_read);
}
Rpl_info_handler::enum_field_get_status Rpl_info_file::do_get_info(
const int pos, uchar *value, const size_t size, const uchar *) {
long n_bytes_read = my_b_read(&info_file, value, size);
return this->check_for_error(pos, n_bytes_read);
}
Rpl_info_handler::enum_field_get_status Rpl_info_file::do_get_info(
const int pos, ulong *value, const ulong default_value) {
long n_bytes_read = init_ulongvar_from_file(value, &info_file, default_value);
return this->check_for_error(pos, n_bytes_read);
}
Rpl_info_handler::enum_field_get_status Rpl_info_file::do_get_info(
const int pos, int *value, const int default_value) {
long n_bytes_read =
init_intvar_from_file(value, &info_file, (int)default_value);
return this->check_for_error(pos, n_bytes_read);
}
Rpl_info_handler::enum_field_get_status Rpl_info_file::do_get_info(
const int pos, float *value, const float default_value) {
long n_bytes_read = init_floatvar_from_file(value, &info_file, default_value);
return this->check_for_error(pos, n_bytes_read);
}
Rpl_info_handler::enum_field_get_status Rpl_info_file::do_get_info(
const int pos, Server_ids *value, const Server_ids *) {
/*
Static buffer to use most of the times. However, if it is not big
enough to accommodate the server ids, a new buffer is allocated.
*/
const int array_size = 16 * (sizeof(long) * 3 + 1);
char buffer[array_size];
char *buffer_act = buffer;
long n_bytes_read = init_dynarray_intvar_from_file(buffer, sizeof(buffer),
&buffer_act, &info_file);
if (n_bytes_read > 0) value->unpack_dynamic_ids(buffer_act);
if (buffer != buffer_act) {
/*
Release the buffer allocated while reading the server ids
from the file.
*/
my_free(buffer_act);
}
return this->check_for_error(pos, n_bytes_read);
}
char *Rpl_info_file::do_get_description_info() { return info_fname; }
bool Rpl_info_file::do_is_transactional() { return false; }
bool Rpl_info_file::do_update_is_transactional() { return false; }
uint Rpl_info_file::do_get_rpl_info_type() { return INFO_REPOSITORY_FILE; }
/**
Tries to read a string of maximum size `max_size` from the `IO_CACHE` `f`
current line - being a line a string terminated by '\n'.
@param var Put the read values in this static buffer
@param max_size Size of the static buffer
@param f IO_CACHE of the replication info file.
@param default_val Default value to be assigned in case unable to read bytes.
@retval >= 0 Number or read bytes
@retval < 0 An error
*/
long init_strvar_from_file(char *var, size_t max_size, IO_CACHE *f,
const char *default_val) {
long length;
DBUG_TRACE;
if ((length = static_cast<long>(my_b_gets(f, var, max_size)))) {
char *last_p = var + length - 1;
if (*last_p == '\n') {
*last_p = 0; // if we stopped on newline, kill it
return length - 1;
} else {
/*
If we truncated a line or stopped on last char, remove all chars
up to and including newline.
*/
int c;
while (((c = my_b_get(f)) != '\n' && c != my_b_EOF))
;
}
return length;
} else if (default_val) {
strmake(var, default_val, max_size - 1);
return strlen(default_val);
}
return -1;
}
/**
Tries to read an integer value from the `IO_CACHE` `f` current line - being a
line a string terminated by '\n'.
@param var Put the read value in this parameter.
@param f IO_CACHE of the replication info file.
@param default_val Default value to be assigned in case unable to read bytes.
@retval >= 0 Number or read bytes
@retval < 0 An error
*/
long init_intvar_from_file(int *var, IO_CACHE *f, int default_val) {
/*
32 bytes provide enough space:
INT_MIN –2,147,483,648
INT_MAX +2,147,483,647
*/
char buf[32];
DBUG_TRACE;
long length;
if ((length = static_cast<long>(my_b_gets(f, buf, sizeof(buf))))) {
*var = atoi(buf);
return length;
} else if (default_val) {
*var = default_val;
return sizeof(default_val);
}
return -1;
}
/**
Tries to read an unsigned integer value from the `IO_CACHE` `f` current line -
being a line a string terminated by '\n'.
@param var Put the read value in this parameter.
@param f IO_CACHE of the replication info file.
@param default_val Default value to be assigned in case unable to read bytes.
@retval >= 0 Number or read bytes
@retval < 0 An error
*/
long init_ulongvar_from_file(ulong *var, IO_CACHE *f, ulong default_val) {
/*
32 bytes provide enough space:
ULONG_MAX 32 bit compiler +4,294,967,295
64 bit compiler +18,446,744,073,709,551,615
*/
char buf[32];
DBUG_TRACE;
long length;
if ((length = static_cast<long>(my_b_gets(f, buf, sizeof(buf))))) {
*var = strtoul(buf, nullptr, 10);
return length;
} else if (default_val) {
*var = default_val;
return sizeof(default_val);
}
return -1;
}
/**
Tries to read a float value from the `IO_CACHE` `f` current line - being a
line a string terminated by '\n'.
@param var Put the read value in this parameter.
@param f IO_CACHE of the replication info file.
@param default_val Default value to be assigned in case unable to read bytes.
@retval >= 0 Number or read bytes
@retval < 0 An error
*/
long init_floatvar_from_file(float *var, IO_CACHE *f, float default_val) {
/*
64 bytes provide enough space considering that the precision is 3
bytes (See the appropriate set function):
FLT_MAX The value of this macro is the maximum number representable
in type float. It is supposed to be at least 1E+37.
FLT_MIN Similar to the FLT_MAX, we have 1E-37.
If a file is manually and not properly changed, this function may
crash the server.
*/
char buf[64];
DBUG_TRACE;
long length;
if ((length = static_cast<long>(my_b_gets(f, buf, sizeof(buf))))) {
if (sscanf(buf, "%f", var) != 1)
return -1;
else
return length;
} else if (default_val != 0.0) {
*var = default_val;
return sizeof(default_val);
}
return -1;
}
/**
TODO - Improve this function to use String and avoid this weird computation
to calculate the size of the buffers.
Particularly, this function is responsible for restoring IGNORE_SERVER_IDS
list of servers whose events the slave is going to ignore (to not log them
in the relay log).
Items being read are supposed to be decimal output of values of a type
shorter or equal of @c long and separated by the single space.
@param buffer Put the read values in this static buffer
@param size Size of the static buffer
@param buffer_act Points to the final buffer as dynamic buffer may
be used if the static buffer is not big enough.
@param f IO_CACHE of the replication info file.
@retval >= 0 Number or read bytes
@retval < 0 An error
*/
long init_dynarray_intvar_from_file(char *buffer, size_t size,
char **buffer_act, IO_CACHE *f) {
char *buf = buffer; // actual buffer can be dynamic if static is short
char *buf_act = buffer;
char *last;
uint num_items; // number of items of `arr'
size_t read_size;
DBUG_TRACE;
if ((read_size = my_b_gets(f, buf_act, size)) == 0) {
return static_cast<long>(read_size); // no line in master.info
}
if (read_size + 1 == size && buf[size - 2] != '\n') {
/*
short read happened; allocate sufficient memory and make the 2nd read
*/
char buf_work[(sizeof(long) * 3 + 1) * 16];
memcpy(buf_work, buf, sizeof(buf_work));
num_items = atoi(my_strtok_r(buf_work, " ", &last));
size_t snd_size;
/*
max size upper bound approximate estimation bases on the formula:
(the items number + items themselves) *
(decimal size + space) - 1 + `\n' + '\0'
*/
size_t max_size = (1 + num_items) * (sizeof(long) * 3 + 1) + 1;
if (!(buf_act = (char *)my_malloc(key_memory_Rpl_info_file_buffer, max_size,
MYF(MY_WME))))
return -1;
*buffer_act = buf_act;
memcpy(buf_act, buf, read_size);
snd_size = my_b_gets(f, buf_act + read_size, max_size - read_size);
if (snd_size == 0 ||
((snd_size + 1 == max_size - read_size) && buf[max_size - 2] != '\n')) {
/*
failure to make the 2nd read or short read again
*/
return -1;
}
}
return static_cast<long>(read_size);
}
|