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
|
/* Copyright (c) 2000, 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/derror.h"
#include <fcntl.h>
#include <stddef.h>
#include <sys/types.h>
#include "m_ctype.h"
#include "m_string.h"
#include "my_byteorder.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/service_mysql_alloc.h"
#include "mysqld_error.h"
#include "sql/current_thd.h"
#include "sql/log.h"
#include "sql/mysqld.h" // lc_messages_dir
#include "sql/psi_memory_key.h"
#include "sql/sql_class.h" // THD
#include "sql/sql_locale.h"
#include "sql/system_variables.h"
#include "storage/perfschema/pfs_error.h"
CHARSET_INFO *error_message_charset_info;
static const char *ERRMSG_FILE = "errmsg.sys";
/**
Find the error message record for a given MySQL error code in the
array of registered messages.
The result is an index for said array; this value should not be
considered stable between subsequent invocations of the server.
@param mysql_errno the error code to look for
@retval -1 no message registered for this error code
@retval >=0 index
*/
int mysql_errno_to_builtin(uint mysql_errno) {
int offset = 0; // Position where the current section starts in the array.
int i;
int temp_errno = (int)mysql_errno;
for (i = 0; i < NUM_SECTIONS; i++) {
if (temp_errno >= errmsg_section_start[i] &&
temp_errno < (errmsg_section_start[i] + errmsg_section_size[i]))
return mysql_errno - errmsg_section_start[i] + offset;
offset += errmsg_section_size[i];
}
return -1; /* General error */
}
/*
Error messages are stored sequentially in an array.
But logically error messages are organized in sections where
each section contains errors that are consecutively numbered.
This function maps from a "logical" mysql_errno to an array
index and returns the string.
*/
const char *MY_LOCALE_ERRMSGS::lookup(int mysql_errno) {
int offset = 0; // Position where the current section starts in the array.
for (int i = 0; i < NUM_SECTIONS; i++) {
if (mysql_errno >= errmsg_section_start[i] &&
mysql_errno < (errmsg_section_start[i] + errmsg_section_size[i]))
return errmsgs[mysql_errno - errmsg_section_start[i] + offset];
offset += errmsg_section_size[i];
}
return "Invalid error code";
}
#ifndef CHECK_ERRMSG_FORMAT
const char *ER_DEFAULT(int mysql_errno) {
return my_default_lc_messages->errmsgs->lookup(mysql_errno);
}
const char *ER_THD(const THD *thd, int mysql_errno) {
return thd->variables.lc_messages->errmsgs->lookup(mysql_errno);
}
#endif // CHECK_ERRMSG_FORMAT
const char *ER_DEFAULT_NONCONST(int mysql_errno) {
return my_default_lc_messages->errmsgs->lookup(mysql_errno);
}
const char *ER_THD_NONCONST(const THD *thd, int mysql_errno) {
return thd->variables.lc_messages->errmsgs->lookup(mysql_errno);
}
/**
Get the error-message corresponding to the given MySQL error-code,
or nullptr if no message is available for that code (this may indicate
a bug in the caller).
If error messages have been loaded, return the appropriate message
in the configured default language; otherwise, retrieve the message
from the compiled-in set (in English).
@param mysql_errno MySQL error-code
@retval an error-message if available, or nullptr
*/
static const char *error_message_fetch(int mysql_errno) {
if ((my_default_lc_messages != nullptr) &&
(my_default_lc_messages->errmsgs->is_loaded()))
return ER_DEFAULT_NONCONST(mysql_errno);
{
server_error *sqlstate_map = &error_names_array[1];
int i = mysql_errno_to_builtin(mysql_errno);
if (i >= 0) return sqlstate_map[i].text;
}
return nullptr;
}
/**
Get the error-message corresponding to the given MySQL error-code,
or nullptr if no message is available for that code (this may indicate
a bug in the caller).
Use this variant for messages intended for the server's error-log.
If error messages have been loaded, return the appropriate message
in the configured default language; otherwise, retrieve the message
from the compiled-in set (in English).
@param mysql_errno MySQL error-code
@retval an error-message if available, or nullptr
*/
const char *error_message_for_error_log(int mysql_errno) {
return error_message_fetch(mysql_errno);
}
/**
Get the error-message corresponding to the given MySQL error-code,
or nullptr if no message is available for that code (this may indicate
a bug in the caller).
Use this variant for messages intended for sending to a client.
If the session language is known, the message will be returned
in that language if available; otherwise, we will fall back on
the configured default language if loaded, or finally on the
built-in default, English.
@param mysql_errno MySQL error-code
@retval an error-message if available, or nullptr
*/
const char *error_message_for_client(int mysql_errno) {
if (current_thd) return ER_THD_NONCONST(current_thd, mysql_errno);
return error_message_fetch(mysql_errno);
}
bool init_errmessage() {
DBUG_TRACE;
/* Read messages from file. */
(void)my_default_lc_messages->errmsgs->read_texts();
if (!my_default_lc_messages->errmsgs->is_loaded())
return true; /* Fatal error, not able to allocate memory. */
/* Register messages for use with my_error(). */
for (int i = 0; i < NUM_SECTIONS; i++) {
if (my_error_register(
error_message_for_client, errmsg_section_start[i],
errmsg_section_start[i] + errmsg_section_size[i] - 1)) {
my_default_lc_messages->errmsgs->destroy();
return true;
}
}
return false;
}
void deinit_errmessage() {
for (int i = 0; i < NUM_SECTIONS; i++) {
my_error_unregister(errmsg_section_start[i],
errmsg_section_start[i] + errmsg_section_size[i] - 1);
}
}
/**
Read text from packed textfile in language-directory.
@retval false On success
@retval true On failure
@note If we can't read messagefile then it's panic- we can't continue.
*/
bool MY_LOCALE_ERRMSGS::read_texts() {
uint no_of_errmsgs;
size_t length;
File file;
char name[FN_REFLEN];
char lang_path[FN_REFLEN];
uchar *start_of_errmsgs = nullptr;
uchar head[32];
uint error_messages = 0;
DBUG_TRACE;
for (int i = 0; i < NUM_SECTIONS; i++)
error_messages += errmsg_section_size[i];
convert_dirname(lang_path, language, NullS);
(void)my_load_path(lang_path, lang_path, lc_messages_dir);
if ((file = mysql_file_open(key_file_ERRMSG,
fn_format(name, ERRMSG_FILE, lang_path, "", 4),
O_RDONLY, MYF(0))) < 0) {
/*
Trying pre-5.5 semantics of the --language parameter.
It included the language-specific part, e.g.:
--language=/path/to/english/
*/
if ((file = mysql_file_open(
key_file_ERRMSG,
fn_format(name, ERRMSG_FILE, lc_messages_dir, "", 4), O_RDONLY,
MYF(0))) < 0) {
LogErr(ERROR_LEVEL, ER_ERRMSG_CANT_FIND_FILE, name);
goto open_err;
}
LogErr(WARNING_LEVEL, ER_ERRMSG_LOADING_55_STYLE, lc_messages_dir);
}
// Read the header from the file
if (mysql_file_read(file, (uchar *)head, 32, MYF(MY_NABP))) goto read_err;
if (head[0] != (uchar)254 || head[1] != (uchar)254 || head[2] != 3 ||
head[3] != 1 || head[4] != 1)
goto read_err;
error_message_charset_info = system_charset_info;
length = uint4korr(head + 6);
no_of_errmsgs = uint4korr(head + 10);
if (no_of_errmsgs < error_messages) {
LogErr(ERROR_LEVEL, ER_ERRMSG_MISSING_IN_FILE, name, no_of_errmsgs,
error_messages);
(void)mysql_file_close(file, MYF(MY_WME));
goto open_err;
}
// Free old language and allocate for the new one
my_free(errmsgs);
if (!(errmsgs = static_cast<const char **>(
my_malloc(key_memory_errmsgs_server,
length + no_of_errmsgs * sizeof(char *), MYF(0))))) {
LogErr(ERROR_LEVEL, ER_ERRMSG_OOM, name);
(void)mysql_file_close(file, MYF(MY_WME));
return true;
}
// Get pointer to Section2.
start_of_errmsgs = (uchar *)(errmsgs + no_of_errmsgs);
/*
Temporarily read message offsets into Section2.
We cannot read these 4 byte offsets directly into Section1,
as pointer size vary between processor architecture.
*/
if (mysql_file_read(file, start_of_errmsgs, (size_t)no_of_errmsgs * 4,
MYF(MY_NABP)))
goto read_err_init;
// Copy the message offsets to Section1.
{
const uchar *pos = start_of_errmsgs;
for (uint i = 0; i < no_of_errmsgs; i++) {
errmsgs[i] = pointer_cast<char *>(start_of_errmsgs) + uint4korr(pos);
pos += 4;
}
}
// Copy all the error text messages into Section2.
if (mysql_file_read(file, start_of_errmsgs, length, MYF(MY_NABP)))
goto read_err_init;
(void)mysql_file_close(file, MYF(0));
return false;
read_err_init:
/*
At this point, we've already thrown away any old, valid setup
we may have had, and we have a half set up message-set.
Release the mess and fall through to init from built-ins below!
*/
my_free(errmsgs);
errmsgs = nullptr;
read_err:
LogErr(ERROR_LEVEL, ER_ERRMSG_CANT_READ, name);
(void)mysql_file_close(file, MYF(MY_WME));
open_err:
/*
We may have failed now, but we may still have succeeded earlier,
so check whether we've got errmsgs from the previous time!
*/
if (!errmsgs) {
/*
If we can't read the messages from disk, allocate space just for
the pointers, and set up pointers to reference our built-in defaults
for the messages. Since (messages + pointers) is allocated and freed
as one contiguous memory block, this will still be released correctly
at shutdown.
*/
if ((errmsgs = static_cast<const char **>(
my_malloc(key_memory_errmsgs_server,
error_messages * sizeof(char *), MYF(0))))) {
server_error *sqlstate_map = &error_names_array[1];
for (uint i = 0; i < error_messages; ++i)
errmsgs[i] = sqlstate_map[i].text;
}
}
return true;
} /* read_texts */
void MY_LOCALE_ERRMSGS::destroy() {
my_free(errmsgs);
errmsgs = nullptr;
}
|