forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.cc
376 lines (317 loc) · 10.3 KB
/
bootstrap.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
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,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
#include "bootstrap.h"
#include "log.h" // sql_print_warning
#include "mysqld_thd_manager.h" // Global_THD_manager
#include "bootstrap_impl.h"
#include "sql_initialize.h"
#include "sql_class.h" // THD
#include "sql_connect.h" // close_connection
#include "sql_parse.h" // mysql_parse
#include "pfs_file_provider.h"
#include "mysql/psi/mysql_file.h"
static MYSQL_FILE *bootstrap_file= NULL;
static const char *bootstrap_query= NULL;
static int bootstrap_error= 0;
class Query_command_iterator: public Command_iterator
{
public:
Query_command_iterator(const char* query):
m_query(query), m_is_read(false) {}
virtual int next(std::string &query, int *read_error, int *query_source)
{
if (m_is_read)
return READ_BOOTSTRAP_EOF;
query= m_query;
m_is_read= true;
*read_error= 0;
*query_source= QUERY_SOURCE_COMPILED;
return READ_BOOTSTRAP_SUCCESS;
}
private:
const char *m_query; // Owned externally.
bool m_is_read;
};
int File_command_iterator::next(std::string &query, int *error,
int *query_source)
{
static char query_buffer[MAX_BOOTSTRAP_QUERY_SIZE];
size_t length= 0;
int rc;
*query_source= QUERY_SOURCE_FILE;
rc= read_bootstrap_query(query_buffer, &length, m_input, m_fgets_fn, error);
if (rc == READ_BOOTSTRAP_SUCCESS)
query.assign(query_buffer, length);
return rc;
}
char *mysql_file_fgets_fn(char *buffer, size_t size, MYSQL_FILE* input, int *error)
{
char *line= mysql_file_fgets(buffer, static_cast<int>(size), input);
if (error)
*error= (line == NULL) ? ferror(input->m_file) : 0;
return line;
}
File_command_iterator::File_command_iterator(const char *file_name)
{
is_allocated= false;
if (!(m_input= mysql_file_fopen(key_file_init, file_name,
O_RDONLY, MYF(MY_WME))))
return;
m_fgets_fn= mysql_file_fgets_fn;
is_allocated= true;
}
File_command_iterator::~File_command_iterator()
{
end();
}
void File_command_iterator::end(void)
{
if (is_allocated)
{
mysql_file_fclose(m_input, MYF(0));
is_allocated= false;
m_input= NULL;
}
}
Command_iterator *Command_iterator::current_iterator= NULL;
static void handle_bootstrap_impl(THD *thd)
{
std::string query;
DBUG_ENTER("handle_bootstrap");
File_command_iterator file_iter(bootstrap_file, mysql_file_fgets_fn);
Compiled_in_command_iterator comp_iter;
Query_command_iterator query_iter(bootstrap_query);
bool has_binlog_option= thd->variables.option_bits & OPTION_BIN_LOG;
int query_source, last_query_source= -1;
thd->thread_stack= (char*) &thd;
thd->security_context()->assign_user(STRING_WITH_LEN("boot"));
thd->security_context()->assign_priv_user("", 0);
thd->security_context()->assign_priv_host("", 0);
/*
Make the "client" handle multiple results. This is necessary
to enable stored procedures with SELECTs and Dynamic SQL
in init-file.
*/
thd->get_protocol_classic()->add_client_capability(
CLIENT_MULTI_RESULTS);
thd->init_for_queries();
/*
If a single bootstrap query is submitted, execute it regardless of the
command line options. If no query is submitted, read commands from the
executable or from file depending on option.
*/
if (bootstrap_query)
{
Command_iterator::current_iterator= &query_iter;
bootstrap_query= NULL;
}
else
{
if (opt_initialize)
Command_iterator::current_iterator= &comp_iter;
else
Command_iterator::current_iterator= &file_iter;
}
Command_iterator::current_iterator->begin();
for ( ; ; )
{
int error= 0;
int rc;
rc= Command_iterator::current_iterator->next(query, &error, &query_source);
/*
The server must avoid logging compiled statements into the binary log
(and generating GTIDs for them when GTID_MODE is ON) during bootstrap/
initialize procedures.
We will disable SQL_LOG_BIN session variable before processing compiled
statements, and will re-enable it before processing statements of the
initialization file.
*/
if (has_binlog_option && query_source != last_query_source)
{
switch (query_source)
{
case QUERY_SOURCE_COMPILED:
thd->variables.option_bits&= ~OPTION_BIN_LOG;
break;
case QUERY_SOURCE_FILE:
/*
Some compiled script might have disable binary logging session
variable during compiled scripts. Enabling it again as it was
enabled before applying the compiled statements.
*/
thd->variables.sql_log_bin= true;
thd->variables.option_bits|= OPTION_BIN_LOG;
break;
default:
DBUG_ASSERT(false);
break;
}
}
last_query_source= query_source;
if (rc == READ_BOOTSTRAP_EOF)
break;
/*
Check for bootstrap file errors. SQL syntax errors will be
caught below.
*/
if (rc != READ_BOOTSTRAP_SUCCESS)
{
/*
mysql_parse() may have set a successful error status for the previous
query. We must clear the error status to report the bootstrap error.
*/
thd->get_stmt_da()->reset_diagnostics_area();
/* Get the nearest query text for reference. */
const char *err_ptr= query.c_str() + (query.length() <= MAX_BOOTSTRAP_ERROR_LEN ?
0 : (query.length() - MAX_BOOTSTRAP_ERROR_LEN));
switch (rc)
{
case READ_BOOTSTRAP_ERROR:
my_printf_error(ER_UNKNOWN_ERROR,
"Bootstrap file error, return code (%d). "
"Nearest query: '%s'", MYF(0), error, err_ptr);
break;
case READ_BOOTSTRAP_QUERY_SIZE:
my_printf_error(ER_UNKNOWN_ERROR, "Bootstrap file error. Query size "
"exceeded %d bytes near '%s'.", MYF(0),
MAX_BOOTSTRAP_LINE_SIZE, err_ptr);
break;
default:
DBUG_ASSERT(false);
break;
}
thd->send_statement_status();
bootstrap_error= 1;
break;
}
char *query_copy= static_cast<char*>(thd->alloc(query.length() + 1));
if (query_copy == NULL)
{
bootstrap_error= 1;
break;
}
memcpy(query_copy, query.c_str(), query.length());
query_copy[query.length()]= '\0';
thd->set_query(query_copy, query.length());
thd->set_query_id(next_query_id());
DBUG_PRINT("query",("%-.4096s",thd->query().str));
#if defined(ENABLED_PROFILING)
thd->profiling.start_new_query();
thd->profiling.set_query_source(thd->query().str, thd->query().length);
#endif
thd->set_time();
Parser_state parser_state;
if (parser_state.init(thd, thd->query().str, thd->query().length))
{
thd->send_statement_status();
bootstrap_error= 1;
break;
}
mysql_parse(thd, &parser_state);
bootstrap_error= thd->is_error();
thd->send_statement_status();
#if defined(ENABLED_PROFILING)
thd->profiling.finish_current_query();
#endif
if (bootstrap_error)
break;
free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
thd->get_transaction()->free_memory(MYF(MY_KEEP_PREALLOC));
/*
If the last statement has enabled the session binary logging while
processing queries that are compiled and must not be binary logged,
we must disable binary logging again.
*/
if (last_query_source == QUERY_SOURCE_COMPILED &&
thd->variables.option_bits & OPTION_BIN_LOG)
thd->variables.option_bits&= ~OPTION_BIN_LOG;
}
Command_iterator::current_iterator->end();
/*
We should re-enable SQL_LOG_BIN session if it was enabled by default
but disabled during bootstrap/initialization.
*/
if (has_binlog_option)
{
thd->variables.sql_log_bin= true;
thd->variables.option_bits|= OPTION_BIN_LOG;
}
DBUG_VOID_RETURN;
}
/**
Execute commands from bootstrap_file.
Used when creating the initial grant tables.
*/
namespace {
extern "C" void *handle_bootstrap(void *arg)
{
THD *thd=(THD*) arg;
mysql_thread_set_psi_id(thd->thread_id());
/* The following must be called before DBUG_ENTER */
thd->thread_stack= (char*) &thd;
if (my_thread_init() || thd->store_globals())
{
#ifndef EMBEDDED_LIBRARY
close_connection(thd, ER_OUT_OF_RESOURCES);
#endif
thd->fatal_error();
bootstrap_error= 1;
thd->get_protocol_classic()->end_net();
}
else
{
Global_THD_manager *thd_manager= Global_THD_manager::get_instance();
thd_manager->add_thd(thd);
handle_bootstrap_impl(thd);
thd->get_protocol_classic()->end_net();
thd->release_resources();
thd_manager->remove_thd(thd);
}
my_thread_end();
return 0;
}
} // namespace
int bootstrap(MYSQL_FILE *file)
{
DBUG_ENTER("bootstrap");
THD *thd= new THD;
thd->bootstrap= 1;
thd->get_protocol_classic()->init_net(NULL);
thd->security_context()->set_master_access(~(ulong)0);
thd->set_new_thread_id();
bootstrap_file=file;
my_thread_attr_t thr_attr;
my_thread_attr_init(&thr_attr);
#ifndef _WIN32
pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM);
#endif
my_thread_attr_setdetachstate(&thr_attr, MY_THREAD_CREATE_JOINABLE);
my_thread_handle thread_handle;
// What about setting THD::real_id?
int error= mysql_thread_create(key_thread_bootstrap,
&thread_handle, &thr_attr, handle_bootstrap, thd);
if (error)
{
sql_print_warning("Can't create thread to handle bootstrap (errno= %d)",
error);
DBUG_RETURN(-1);
}
/* Wait for thread to die */
my_thread_join(&thread_handle, NULL);
delete thd;
DBUG_RETURN(bootstrap_error);
}
int bootstrap_single_query(const char* query)
{
bootstrap_query= query;
return bootstrap(NULL);
}