1 | /* |
2 | Copyright (c) 2005, 2013, Oracle and/or its affiliates. |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; version 2 of the License. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
16 | |
17 | #include "mariadb.h" |
18 | #include "sql_priv.h" |
19 | #include "sql_binlog.h" |
20 | #include "sql_parse.h" |
21 | #include "sql_acl.h" |
22 | #include "rpl_rli.h" |
23 | #include "slave.h" |
24 | #include "log_event.h" |
25 | |
26 | |
27 | /** |
28 | Check if the event type is allowed in a BINLOG statement. |
29 | |
30 | @retval 0 if the event type is ok. |
31 | @retval 1 if the event type is not ok. |
32 | */ |
33 | static int check_event_type(int type, Relay_log_info *rli) |
34 | { |
35 | Format_description_log_event *fd_event= |
36 | rli->relay_log.description_event_for_exec; |
37 | |
38 | /* |
39 | Convert event type id of certain old versions (see comment in |
40 | Format_description_log_event::Format_description_log_event(char*,...)). |
41 | */ |
42 | if (fd_event && fd_event->event_type_permutation) |
43 | { |
44 | IF_DBUG({ |
45 | int new_type= fd_event->event_type_permutation[type]; |
46 | DBUG_PRINT("info" , |
47 | ("converting event type %d to %d (%s)" , |
48 | type, new_type, |
49 | Log_event::get_type_str((Log_event_type)new_type))); |
50 | }, |
51 | (void)0); |
52 | type= fd_event->event_type_permutation[type]; |
53 | } |
54 | |
55 | switch (type) |
56 | { |
57 | case START_EVENT_V3: |
58 | case FORMAT_DESCRIPTION_EVENT: |
59 | /* |
60 | We need a preliminary FD event in order to parse the FD event, |
61 | if we don't already have one. |
62 | */ |
63 | if (!fd_event) |
64 | if (!(rli->relay_log.description_event_for_exec= |
65 | new Format_description_log_event(4))) |
66 | { |
67 | my_error(ER_OUTOFMEMORY, MYF(0), 1); |
68 | return 1; |
69 | } |
70 | |
71 | /* It is always allowed to execute FD events. */ |
72 | return 0; |
73 | |
74 | case TABLE_MAP_EVENT: |
75 | case WRITE_ROWS_EVENT_V1: |
76 | case UPDATE_ROWS_EVENT_V1: |
77 | case DELETE_ROWS_EVENT_V1: |
78 | case WRITE_ROWS_EVENT: |
79 | case UPDATE_ROWS_EVENT: |
80 | case DELETE_ROWS_EVENT: |
81 | case PRE_GA_WRITE_ROWS_EVENT: |
82 | case PRE_GA_UPDATE_ROWS_EVENT: |
83 | case PRE_GA_DELETE_ROWS_EVENT: |
84 | /* |
85 | Row events are only allowed if a Format_description_event has |
86 | already been seen. |
87 | */ |
88 | if (fd_event) |
89 | return 0; |
90 | else |
91 | { |
92 | my_error(ER_NO_FORMAT_DESCRIPTION_EVENT_BEFORE_BINLOG_STATEMENT, |
93 | MYF(0), Log_event::get_type_str((Log_event_type)type)); |
94 | return 1; |
95 | } |
96 | break; |
97 | |
98 | default: |
99 | /* |
100 | It is not meaningful to execute other events than row-events and |
101 | FD events. It would even be dangerous to execute Stop_log_event |
102 | and Rotate_log_event since they call Relay_log_info::flush(), which |
103 | is not allowed to call by other threads than the slave SQL |
104 | thread when the slave SQL thread is running. |
105 | */ |
106 | my_error(ER_ONLY_FD_AND_RBR_EVENTS_ALLOWED_IN_BINLOG_STATEMENT, |
107 | MYF(0), Log_event::get_type_str((Log_event_type)type)); |
108 | return 1; |
109 | } |
110 | } |
111 | |
112 | /** |
113 | Execute a BINLOG statement. |
114 | |
115 | To execute the BINLOG command properly the server needs to know |
116 | which format the BINLOG command's event is in. Therefore, the first |
117 | BINLOG statement seen must be a base64 encoding of the |
118 | Format_description_log_event, as outputted by mysqlbinlog. This |
119 | Format_description_log_event is cached in |
120 | rli->description_event_for_exec. |
121 | |
122 | @param thd Pointer to THD object for the client thread executing the |
123 | statement. |
124 | */ |
125 | |
126 | void mysql_client_binlog_statement(THD* thd) |
127 | { |
128 | DBUG_ENTER("mysql_client_binlog_statement" ); |
129 | DBUG_PRINT("info" ,("binlog base64: '%*s'" , |
130 | (int) (thd->lex->comment.length < 2048 ? |
131 | thd->lex->comment.length : 2048), |
132 | thd->lex->comment.str)); |
133 | |
134 | if (check_global_access(thd, SUPER_ACL)) |
135 | DBUG_VOID_RETURN; |
136 | |
137 | size_t coded_len= thd->lex->comment.length; |
138 | if (!coded_len) |
139 | { |
140 | my_error(ER_SYNTAX_ERROR, MYF(0)); |
141 | DBUG_VOID_RETURN; |
142 | } |
143 | size_t decoded_len= my_base64_needed_decoded_length((int)coded_len); |
144 | |
145 | /* |
146 | option_bits will be changed when applying the event. But we don't expect |
147 | it be changed permanently after BINLOG statement, so backup it first. |
148 | It will be restored at the end of this function. |
149 | */ |
150 | ulonglong thd_options= thd->variables.option_bits; |
151 | |
152 | /* |
153 | Allocation |
154 | */ |
155 | |
156 | int err; |
157 | Relay_log_info *rli; |
158 | rpl_group_info *rgi; |
159 | |
160 | rli= thd->rli_fake; |
161 | if (!rli && (rli= thd->rli_fake= new Relay_log_info(FALSE))) |
162 | rli->sql_driver_thd= thd; |
163 | if (!(rgi= thd->rgi_fake)) |
164 | rgi= thd->rgi_fake= new rpl_group_info(rli); |
165 | rgi->thd= thd; |
166 | |
167 | const char *error= 0; |
168 | char *buf= (char *) my_malloc(decoded_len, MYF(MY_WME)); |
169 | Log_event *ev = 0; |
170 | |
171 | /* |
172 | Out of memory check |
173 | */ |
174 | if (!(rli && buf)) |
175 | { |
176 | my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 1); /* needed 1 bytes */ |
177 | goto end; |
178 | } |
179 | |
180 | DBUG_ASSERT(rli->belongs_to_client()); |
181 | |
182 | for (char const *strptr= thd->lex->comment.str ; |
183 | strptr < thd->lex->comment.str + thd->lex->comment.length ; ) |
184 | { |
185 | char const *endptr= 0; |
186 | int bytes_decoded= my_base64_decode(strptr, coded_len, buf, &endptr, |
187 | MY_BASE64_DECODE_ALLOW_MULTIPLE_CHUNKS); |
188 | |
189 | #ifndef HAVE_valgrind |
190 | /* |
191 | This debug printout should not be used for valgrind builds |
192 | since it will read from unassigned memory. |
193 | */ |
194 | DBUG_PRINT("info" , |
195 | ("bytes_decoded: %d strptr: %p endptr: %p ('%c':%d)" , |
196 | bytes_decoded, strptr, endptr, *endptr, |
197 | *endptr)); |
198 | #endif |
199 | |
200 | if (bytes_decoded < 0) |
201 | { |
202 | my_error(ER_BASE64_DECODE_ERROR, MYF(0)); |
203 | goto end; |
204 | } |
205 | else if (bytes_decoded == 0) |
206 | break; // If no bytes where read, the string contained only whitespace |
207 | |
208 | DBUG_ASSERT(bytes_decoded > 0); |
209 | DBUG_ASSERT(endptr > strptr); |
210 | coded_len-= endptr - strptr; |
211 | strptr= endptr; |
212 | |
213 | /* |
214 | Now we have one or more events stored in the buffer. The size of |
215 | the buffer is computed based on how much base64-encoded data |
216 | there were, so there should be ample space for the data (maybe |
217 | even too much, since a statement can consist of a considerable |
218 | number of events). |
219 | |
220 | TODO: Switch to use a stream-based base64 encoder/decoder in |
221 | order to be able to read exactly what is necessary. |
222 | */ |
223 | |
224 | DBUG_PRINT("info" ,("binlog base64 decoded_len: %lu bytes_decoded: %d" , |
225 | (ulong) decoded_len, bytes_decoded)); |
226 | |
227 | /* |
228 | Now we start to read events of the buffer, until there are no |
229 | more. |
230 | */ |
231 | for (char *bufptr= buf ; bytes_decoded > 0 ; ) |
232 | { |
233 | /* |
234 | Checking that the first event in the buffer is not truncated. |
235 | */ |
236 | ulong event_len; |
237 | if (bytes_decoded < EVENT_LEN_OFFSET + 4 || |
238 | (event_len= uint4korr(bufptr + EVENT_LEN_OFFSET)) > |
239 | (uint) bytes_decoded) |
240 | { |
241 | my_error(ER_SYNTAX_ERROR, MYF(0)); |
242 | goto end; |
243 | } |
244 | DBUG_PRINT("info" , ("event_len=%lu, bytes_decoded=%d" , |
245 | event_len, bytes_decoded)); |
246 | |
247 | if (check_event_type(bufptr[EVENT_TYPE_OFFSET], rli)) |
248 | goto end; |
249 | |
250 | ev= Log_event::read_log_event(bufptr, event_len, &error, |
251 | rli->relay_log.description_event_for_exec, |
252 | 0); |
253 | |
254 | DBUG_PRINT("info" ,("binlog base64 err=%s" , error)); |
255 | if (!ev) |
256 | { |
257 | /* |
258 | This could actually be an out-of-memory, but it is more likely |
259 | caused by a bad statement |
260 | */ |
261 | my_error(ER_SYNTAX_ERROR, MYF(0)); |
262 | goto end; |
263 | } |
264 | |
265 | bytes_decoded -= event_len; |
266 | bufptr += event_len; |
267 | |
268 | DBUG_PRINT("info" ,("ev->get_type_code()=%d" , ev->get_type_code())); |
269 | ev->thd= thd; |
270 | /* |
271 | We go directly to the application phase, since we don't need |
272 | to check if the event shall be skipped or not. |
273 | |
274 | Neither do we have to update the log positions, since that is |
275 | not used at all: the rli_fake instance is used only for error |
276 | reporting. |
277 | */ |
278 | #if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION) |
279 | ulonglong save_skip_replication= |
280 | thd->variables.option_bits & OPTION_SKIP_REPLICATION; |
281 | thd->variables.option_bits= |
282 | (thd->variables.option_bits & ~OPTION_SKIP_REPLICATION) | |
283 | (ev->flags & LOG_EVENT_SKIP_REPLICATION_F ? |
284 | OPTION_SKIP_REPLICATION : 0); |
285 | |
286 | err= ev->apply_event(rgi); |
287 | |
288 | thd->variables.option_bits= |
289 | (thd->variables.option_bits & ~OPTION_SKIP_REPLICATION) | |
290 | save_skip_replication; |
291 | #else |
292 | err= 0; |
293 | #endif |
294 | /* |
295 | Format_description_log_event should not be deleted because it |
296 | will be used to read info about the relay log's format; it |
297 | will be deleted when the SQL thread does not need it, |
298 | i.e. when this thread terminates. |
299 | */ |
300 | if (ev->get_type_code() != FORMAT_DESCRIPTION_EVENT) |
301 | delete ev; |
302 | ev= 0; |
303 | if (err) |
304 | { |
305 | /* |
306 | TODO: Maybe a better error message since the BINLOG statement |
307 | now contains several events. |
308 | */ |
309 | my_error(ER_UNKNOWN_ERROR, MYF(0)); |
310 | goto end; |
311 | } |
312 | } |
313 | } |
314 | |
315 | |
316 | DBUG_PRINT("info" ,("binlog base64 execution finished successfully" )); |
317 | my_ok(thd); |
318 | |
319 | end: |
320 | thd->variables.option_bits= thd_options; |
321 | rgi->slave_close_thread_tables(thd); |
322 | my_free(buf); |
323 | DBUG_VOID_RETURN; |
324 | } |
325 | |