Debian Codegraph @

extra/mariadb_migrate_config_file.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
2 Copyright (c) 2011, 2018, 2026 MariaDB Corporation
3 Copyright (c) 2024, Väinö Mäkelä
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
17
18 #define VER "1.0"
19 #include <my_global.h>
20 #include "mariadbd_options.h"
21 #include <my_global.h>
22 #include <my_sys.h>
23 #include <my_dir.h>
24 #include <mysql/psi/mysql_file.h>
25 #include <m_string.h>
26 #include <my_getopt.h>
27 #include <my_default.h>
28 #include <mysql_version.h>
29 #include <stdarg.h>
30 #include <welcome_copyright_notice.h>
31
32 #if!defined (X_OK) && defined(_WIN32)
33 #define X_OK 0 /* Test existence of file with access() */
34 #endif
35
36
37 #define load_default_groups mariadbd_groups
38 #include <mysqld_default_groups.h>
39 #undef load_default_groups
40
41 const char *mysqld_groups[]=
42 {
43 "mysqld", "server", 0, 0
44 };
45
46 const char *client_server_group= "client-server";
47
48 /* The following is from my_getopt.c */
49 static const char *special_opt_prefix[]=
50 {"skip", "disable", "enable", "maximum", "loose", "autoset", 0};
51 static const uint special_opt_prefix_lengths[]=
52 { 4, 7, 6, 7, 5, 7, 0};
53
54 #ifndef WEXITSTATUS
55 # ifdef _WIN32
56 # define WEXITSTATUS(stat_val) (stat_val)
57 # else
58 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
59 # endif
60 #endif
61
62 struct upgrade_ctx
63 {
64 MEM_ROOT *alloc;
65 TYPELIB *group; /* Default groups and extra groups */
66 DYNAMIC_ARRAY updated_files;
67 my_bool failed;
68 };
69
70
71 enum edit_mode
72 {
73 EDIT_MODE_NONE,
74 EDIT_MODE_REMOVE,
75 EDIT_MODE_COMMENT,
76 EDIT_MODE_INLINE,
77 EDIT_MODE_LAST,
78 };
79
80 static const char *edit_mode_values[]=
81 {
82 "", "remove", "comment", "inline", "last", NullS
83 };
84 static TYPELIB edit_mode_typelib=
85 {array_elements(edit_mode_values) - 1, "", edit_mode_values, NULL, NULL};
86
87 enum from_mode
88 {
89 FROM_MYSQL,
90 FROM_MARIADB,
91 };
92
93 static const char *from_mode_values[]=
94 {
95 "MySQL", "MariaDB", NullS
96 };
97 static TYPELIB from_mode_typelib=
98 {array_elements(from_mode_values) - 1, "", from_mode_values, NULL, NULL};
99
100 struct RENAMED_OPTIONS
101 {
102 const char *from, *to;
103 };
104
105 struct RENAMED_OPTIONS renamed_options[]=
106 {
107 {"innodb_stats_sample_pages", "innodb_stats_transient_sample_pages" },
108 {NullS, NullS},
109 };
110
111
112 LEX_CSTRING mysql_name= {STRING_WITH_LEN("mysql")};
113 LEX_CSTRING mysqld_name= {STRING_WITH_LEN("mysqld")};
114 LEX_CSTRING mariadb_name= {STRING_WITH_LEN("mariadb")};
115
116 static const char *opt_error_group="mysqld-5.7";
117 static const char *opt_unsupported_group="options-not-recognized-by-mariadbd";
118 static my_bool opt_update;
119 static my_bool opt_backup;
120 static my_bool opt_print;
121 static my_bool opt_no_myisam_options;
122 static my_bool opt_add_skip_slave_start;
123 static my_bool opt_add_mariadb_replication_compatibility;
124 static my_bool opt_add_mysql_replication_compatibility;
125 static my_bool opt_add_client_server_group;
126 static my_bool opt_silent;
127 static my_bool opt_update_paths;
128 static my_bool opt_copy_mysqld_groups;
129 static my_bool add_new_options_once=0, new_options_added= 0;
130 static uint audit_plugin_warning= 0;
131 static const char **groups_to_use;
132 static const char *opt_mariadbd= "";
133 static ulong opt_edit_mode, opt_convert_from;
134 static size_t global_copied_lines=0, global_unsupported_lines= 0;
135 static size_t global_mariadbd_additions= 0;
136
137 static PSI_memory_key key_memory_upgrade_config;
138 static PSI_file_key key_file_cnf;
139 static size_t global_update_count= 0;
140 static my_bool give_error_for_missing_files= 0;
141 static my_bool opt_mariadbd_testing;
142 static const char *mariadb_replication_compatibility_comment=
143 "# The following options enable MySQL to replicate to MariaDB";
144 static const char *mysql_replication_compatibility_comment=
145 "# The following options enable MariaDB to replicate to MySQL";
146
147 /* Options found that require special handling */
148 static longlong value_port;
149 static char *value_socket= 0;
150 static my_bool found_skip_slave_start= FALSE;
151 static my_bool found_innodb_log_files_in_group= 0;
152 static my_bool found_innodb_log_file_size= 0;
153 static my_bool found_character_set_server= 0;
154 static my_bool found_client_server_group= 0;
155 static my_bool found_mariadb_replication_compatibility= 0;
156 static my_bool found_mysql_replication_compatibility= 0;
157
158 static uint value_innodb_log_files_in_group= 0;
159 static ulonglong value_innodb_log_file_size= 0;
160 static char suffix_innodb_log_file_size;
161
162
163 #ifdef _WIN32
164 static const char *f_extensions[]= { ".ini", ".cnf", 0 };
165 #else
166 static const char *f_extensions[]= { ".cnf", 0 };
167 #endif
168
169 static uint count_array_elements(DYNAMIC_ARRAY *array);
170 static my_bool delete_line(DYNAMIC_ARRAY *array, char *line);
171
172 /**
173 Remove the -upgrade-config-orig backups of the updated files if
174 successful and replace the updated files with the backups if not.
175 */
176
177 static int finish_updated_files(struct upgrade_ctx *ctx, my_bool success)
178 {
179 size_t i;
180 if (!opt_silent && success && ctx->updated_files.elements)
181 fputs("Updated files:\n", stdout);
182 for (i= 0; i < ctx->updated_files.elements; i++)
183 {
184 char *name = *(char **)dynamic_array_ptr(&ctx->updated_files, i);
185 const char *suffix = "-upgrade-config-orig";
186 size_t orig_len = strlen(name) + strlen(suffix) + 1;
187 char *orig = my_malloc(key_memory_upgrade_config, orig_len, MYF(MY_WME));
188 if (!orig)
189 return 1;
190 strmov(strmov(orig, name), suffix);
191 if (success)
192 {
193 fprintf(stdout, "%s\n", name);
194 my_delete(orig, MYF(0));
195 }
196 else
197 {
198 if (my_redel(name, orig, 0, MYF(MY_WME | MY_REDEL_MAKE_BACKUP)))
199 {
200 my_free(orig);
201 return 1;
202 }
203 }
204 my_free(orig);
205 my_free(name);
206 }
207 delete_dynamic(&ctx->updated_files);
208 return 0;
209 }
210
211
212 /**
213 Run a command using the shell, storing its output in the supplied dynamic
214 string.
215 */
216 static int run_command(char* cmd,
217 DYNAMIC_STRING *ds_res)
218 {
219 char buf[512]= {0};
220 FILE *res_file;
221 int error;
222
223 if (!(res_file= my_popen(cmd, "r")))
224 {
225 fprintf(stderr, "popen(\"%s\", \"r\") failed\n", cmd);
226 return -1;
227 }
228
229 while (fgets(buf, sizeof(buf), res_file))
230 {
231 #ifdef _WIN32
232 /* Strip '\r' off newlines. */
233 size_t len = strlen(buf);
234 if (len > 1 && buf[len - 2] == '\r' && buf[len - 1] == '\n')
235 {
236 buf[len - 2] = '\n';
237 buf[len - 1] = 0;
238 }
239 #endif
240 dynstr_append(ds_res, buf);
241 }
242
243 error= my_pclose(res_file);
244 return WEXITSTATUS(error);
245 }
246
247
248 /**
249 Run `mariadbd --help --verbose` with the supplied arguments and write its
250 stderr output to ds_res.
251 */
252 static int run_mariadbd(const char *mariadbd_path,
253 DYNAMIC_STRING *ds_res,
254 const char **defaults_args,
255 int defaults_args_count)
256 {
257 int ret;
258 int i;
259 DYNAMIC_STRING ds_cmdline;
260
261 if (init_dynamic_string(&ds_cmdline, IF_WIN("\"", ""), FN_REFLEN, FN_REFLEN))
262 {
263 fputs("Out of memory\n", stderr);
264 return -1;
265 }
266
267 dynstr_append_os_quoted(&ds_cmdline, mariadbd_path, NullS);
268 dynstr_append(&ds_cmdline, " ");
269
270 for (i= 0; i < defaults_args_count; i++)
271 {
272 dynstr_append_os_quoted(&ds_cmdline, defaults_args[i], NullS);
273 dynstr_append(&ds_cmdline, " ");
274 }
275
276 dynstr_append(&ds_cmdline, "--help ");
277 dynstr_append(&ds_cmdline, "--verbose ");
278 dynstr_append(&ds_cmdline, "--silent-startup ");
279 dynstr_append(&ds_cmdline, "--log-warnings=0 ");
280 dynstr_append(&ds_cmdline, "2>&1 ");
281 dynstr_append(&ds_cmdline, IF_WIN("1>NUL", "1>/dev/null"));
282
283 #ifdef _WIN32
284 dynstr_append(&ds_cmdline, "\"");
285 #endif
286
287 ret= run_command(ds_cmdline.str, ds_res);
288 dynstr_free(&ds_cmdline);
289 return ret;
290 }
291
292
293 /**
294 Test whether mariadbd can be launched with --no-defaults.
295 */
296 static int test_mariadbd(const char *mariadbd_name)
297 {
298 DYNAMIC_STRING ds_tmp;
299 const char *defaults_argument = "--no-defaults";
300 int err= 0;
301
302 if (init_dynamic_string(&ds_tmp, "", 32, 32))
303 {
304 fputs("Out of memory\n", stderr);
305 return -1;
306 }
307
308 if (run_mariadbd(mariadbd_name,
309 &ds_tmp,
310 &defaults_argument,
311 1))
312 {
313 fprintf(stderr, "Can't execute %s\n", mariadbd_name);
314 err= 1;
315 }
316
317 dynstr_free(&ds_tmp);
318 return err;
319 }
320
321
322 static int compare_options(const void *a, const void *b)
323 {
324 const char *first= *(const char **)a;
325 const char *second= *(const char **)b;
326 return strcmp(first, second);
327 }
328
329
330 static my_bool mariadbd_option_exists(const char *option)
331 {
332 return bsearch(&option, mariadbd_valid_options,
333 array_elements(mariadbd_valid_options),
334 sizeof(mariadbd_valid_options[0]),
335 compare_options) != NULL;
336 }
337
338
339 static my_bool mariadbd_valid_enum_value(const char *option, const char *value)
340 {
341 const char **option_ptr= bsearch(&option, mariadbd_enum_options,
342 array_elements(mariadbd_enum_options),
343 sizeof(mariadbd_enum_options[0]),
344 compare_options);
345 longlong val;
346 int error;
347 if (!option_ptr)
348 return TRUE;
349 if (find_type(value,
350 mariadbd_enum_typelibs[option_ptr - mariadbd_enum_options],
351 FIND_TYPE_BASIC) != 0)
352 return TRUE;
353 val= my_strtoll10(value, (char**) 0, &error);
354 return (error == 0 && val < mariadbd_enum_typelibs[option_ptr -
355 mariadbd_enum_options]->count);
356 }
357
358
359 /**
360 Check whether the given value is a valid set value for the given
361 option.
362 Returns 0 on success and the first invalid set index on
363 failure. If the option is not a set option, returns 0.
364 */
365
366 static int mariadbd_check_set_value(const char *option, const char *value)
367 {
368 const char **option_ptr= bsearch(&option, mariadbd_set_options,
369 array_elements(mariadbd_set_options),
370 sizeof(mariadbd_set_options[0]),
371 compare_options);
372 TYPELIB *typelib;
373 int error_pos= 0;
374 if (!option_ptr)
375 return 0;
376 typelib = mariadbd_set_typelibs[option_ptr - mariadbd_set_options];
377 find_typeset(value, typelib, &error_pos);
378 if (error_pos)
379 {
380 ulonglong num;
381 char *endptr;
382
383 if (!my_strcasecmp_latin1(value, "all"))
384 return 0;
385
386 num= strtol(value, &endptr, 10);
387 if (!*endptr)
388 {
389 if ((num >> 1) >= (1ULL << (typelib->count - 1)))
390 return 1;
391 return 0;
392 }
393 }
394 return error_pos;
395 }
396
397
398 enum plugin_check_result
399 {
400 PLUGINS_OK,
401 AUDIT_PLUGIN,
402 };
403
404 static enum plugin_check_result check_plugins(const char *option,
405 const char *value)
406 {
407 if (!strcmp(option, "plugin_load") || !strcmp(option, "plugin_load_add"))
408 {
409 if (!strcmp(value, "audit_log"))
410 return AUDIT_PLUGIN;
411 return PLUGINS_OK;
412 }
413 return PLUGINS_OK;
414 }
415
416
417 static my_bool check_plugin_option(struct upgrade_ctx *ctx,
418 const char *option,
419 const char *value,
420 char *name,
421 uint line)
422 {
423
424 enum plugin_check_result plugin_check_result;
425
426 if ((plugin_check_result= check_plugins(option, value)) != PLUGINS_OK)
427 {
428 if (!opt_print)
429 {
430 switch (plugin_check_result) {
431 case PLUGINS_OK: break; /* Impossible */
432 case AUDIT_PLUGIN:
433 if (!audit_plugin_warning++ && !opt_silent)
434 fprintf(stdout, "In %s at line %d: Please replace audit_log "
435 "with the server_audit plugin\n", name, line);
436 }
437 return 1;
438 }
439 }
440 return 0;
441 }
442
443 /*
444 Skip over keyword and get argument after keyword
445
446 SYNOPSIS
447 get_argument()
448 keyword Include directive keyword
449 kwlen Length of keyword
450 ptr Pointer to the keword in the line under process
451 line line number
452
453 RETURN
454 0 error
455 # Returns pointer to the argument after the keyword.
456 */
457
458 static char *get_argument(const char *keyword, size_t kwlen,
459 char *ptr, char *name, uint line)
460 {
461 char *end;
462
463 /* Skip over "include / includedir keyword" and following whitespace */
464
465 for (ptr+= kwlen - 1;
466 my_isspace(&my_charset_latin1, ptr[0]);
467 ptr++)
468 {}
469
470 /*
471 Trim trailing whitespace from directory name
472 The -1 below is for the newline added by fgets()
473 Note that my_isspace() is true for \r and \n
474 */
475 for (end= ptr + strlen(ptr) - 1;
476 my_isspace(&my_charset_latin1, *(end - 1));
477 end--)
478 {}
479 end[0]= 0; /* Cut off end space */
480
481 /* Print error msg if there is nothing after !include* directive */
482 if (end <= ptr)
483 {
484 fprintf(stderr,
485 "error: Wrong '!%s' directive in config file: %s at line %d\n",
486 keyword, name, line);
487 return 0;
488 }
489 return ptr;
490 }
491
492
493 static char *find_end_comment(char *ptr)
494 {
495 char quote= 0; /* we are inside quote marks */
496 char escape= 0; /* symbol is protected by escape chagacter */
497
498 for (; *ptr; ptr++)
499 {
500 if ((*ptr == '\'' || *ptr == '\"') && !escape)
501 {
502 if (!quote)
503 quote= *ptr;
504 else if (quote == *ptr)
505 quote= 0;
506 }
507 /* We are not inside a string */
508 if (!quote && *ptr == '#')
509 return ptr;
510 escape= (quote && *ptr == '\\' && !escape);
511 }
512 return ptr;
513 }
514
515 struct generator
516 {
517 /* the memory root for allocating line strings */
518 MEM_ROOT alloc;
519 /* array of strings for the main content */
520 DYNAMIC_ARRAY main;
521 /*
522 array of strings for options that should be added to the error_group
523 section
524 */
525 DYNAMIC_ARRAY old_version;
526 /*
527 array of strings for options that should be added to the unsupported_group
528 section
529 */
530 DYNAMIC_ARRAY unsupported_version;
531 /* array of strings to be added to the mariadbd section */
532 DYNAMIC_ARRAY mariadbd_additions;
533 /* array of strings to be added to the client_server section */
534 DYNAMIC_ARRAY client_server;
535
536 struct upgrade_ctx *ctx; /* Current context */
537
538 const char *name; /* config file name */
539 int start_of_group; /* Start of current group */
540 int mariadbd_group_start; /* Start of last mariadbd group */
541 uint line; /* Current line number */
542 /*
543 one past the last index of the original mariadbd section in the main array
544 */
545 int mariadbd_group_end;
546 /* the current group from which options are read */
547 const char *input_group;
548 /*
549 The current group in the generated output. This may be different from
550 input_group if lines are moved between groups.
551 */
552 char *output_group;
553 /* whether last main line is empty or not */
554 my_bool prev_empty;
555 /* If we should copy [mysqld-xxx] options to [mariadbd] */
556 my_bool copy_code_to_mariadb;
557 /* If current file is valid */
558 my_bool file_valid;
559 };
560
561 static void generator_init(struct generator *generator)
562 {
563 init_alloc_root(key_memory_upgrade_config, &generator->alloc, 512, 0, MYF(0));
564 init_dynamic_array2(key_memory_upgrade_config,
565 &generator->main,
566 sizeof(char *),
567 NULL,
568 0,
569 0,
570 MYF(0));
571 init_dynamic_array2(key_memory_upgrade_config,
572 &generator->old_version,
573 sizeof(char *),
574 NULL,
575 0,
576 0,
577 MYF(0));
578 init_dynamic_array2(key_memory_upgrade_config,
579 &generator->unsupported_version,
580 sizeof(char *),
581 NULL,
582 0,
583 0,
584 MYF(0));
585 init_dynamic_array2(key_memory_upgrade_config,
586 &generator->mariadbd_additions,
587 sizeof(char *),
588 NULL,
589 0,
590 0,
591 MYF(0));
592 init_dynamic_array2(key_memory_upgrade_config,
593 &generator->client_server,
594 sizeof(char *),
595 NULL,
596 0,
597 0,
598 MYF(0));
599 generator->mariadbd_group_end= -1;
600 generator->input_group= NULL;
601 generator->output_group= NULL;
602 /* don't add extra newlines to the beginning */
603 generator->prev_empty= TRUE;
604 generator->copy_code_to_mariadb= 0;
605 }
606
607
608 /*
609 Delete data in mariadbd_additions that was found in last [mariadbd] group
610
611 This is needed to ensure that we don't generate new lines into the
612 [mariadbd] group if we run the program twice on it
613 */
614
615 static void delete_dup_data_from_mariadbd_group(struct generator *generator)
616 {
617 if (generator->mariadbd_group_end >= 0)
618 {
619 for (int i= generator->mariadbd_group_start;
620 i < generator->mariadbd_group_end;
621 i++)
622 {
623 char *element= *(char **) dynamic_array_ptr(&generator->main, i);
624 delete_line(&generator->mariadbd_additions, element);
625 }
626 }
627 }
628
629
630 static my_bool change_output_group(struct generator *generator,
631 const char *group)
632 {
633 char *group_dupe= my_strdup(key_memory_upgrade_config, group, MYF(0));
634 if (!group_dupe)
635 return TRUE;
636 my_free(generator->output_group);
637 generator->output_group= group_dupe;
638 return FALSE;
639 }
640
641 /*
642 Generate output lines for one section
643 Returns number of lines added
644 */
645
646
647 static int generate_write_section(FILE *target_file,
648 struct generator *generator,
649 DYNAMIC_ARRAY *array,
650 const char *group_name)
651 {
652 int lines= 0;
653 if (array->elements > 0)
654 {
655 if (!generator->prev_empty)
656 fputc('\n', target_file);
657 fprintf(target_file, "[%s]\n", group_name);
658 change_output_group(generator, group_name);
659 for (uint i= 0; i < array->elements; i++)
660 {
661 char *element= *(char **)dynamic_array_ptr(array, i);
662 if (element)
663 {
664 fputs(element, target_file);
665 lines++;
666 }
667 }
668 generator->prev_empty= 0;
669 }
670 return lines;
671 }
672
673
674 /*
675 Generate output lines
676 Returns true if file changed
677 */
678
679 static my_bool generator_write(MYSQL_FILE *f, struct generator *generator)
680 {
681 FILE *target_file= f ? f->m_file : stdout;
682 size_t i;
683 size_t lines_handled= 0;
684 size_t new_lines;
685
686 delete_dup_data_from_mariadbd_group(generator);
687
688 if (generator->mariadbd_group_end >= 0)
689 {
690 /*
691 There was a mariadbd group in the file. Put all generated mariadbd
692 options into this group.
693 */
694 for (i= 0; i < (size_t)generator->mariadbd_group_end; i++)
695 {
696 char *element= *(char **) dynamic_array_ptr(&generator->main, i);
697 fputs(element, target_file);
698 }
699 for (i= 0; i < generator->mariadbd_additions.elements; i++)
700 {
701 char *element= (*(char **)
702 dynamic_array_ptr(&generator->mariadbd_additions, i));
703 if (element) /* Ignore deleted elements */
704 {
705 fputs(element, target_file);
706 generator->prev_empty= element[0] == '\n';
707 lines_handled++;
708 }
709 }
710 for (i= (size_t)generator->mariadbd_group_end;
711 i < generator->main.elements;
712 i++)
713 {
714 char *element= *(char **) dynamic_array_ptr(&generator->main, i);
715 fputs(element, target_file);
716 generator->prev_empty= element[0] == '\n';
717 }
718 }
719 else
720 {
721 for (i= 0; i < generator->main.elements; i++)
722 {
723 char *element= *(char **) dynamic_array_ptr(&generator->main, i);
724 fputs(element, target_file);
725 generator->prev_empty= element[0] == '\n';
726 }
727
728 if (count_array_elements(&generator->mariadbd_additions) > 0)
729 lines_handled+= generate_write_section(target_file,
730 generator,
731 &generator->mariadbd_additions,
732 "mariadbd");
733 }
734 generate_write_section(target_file, generator,
735 &generator->old_version, opt_error_group);
736 generate_write_section(target_file, generator,
737 &generator->unsupported_version,
738 opt_unsupported_group);
739 generate_write_section(target_file, generator,
740 &generator->client_server,
741 client_server_group);
742
743 new_lines= (lines_handled + generator->old_version.elements +
744 generator->unsupported_version.elements +
745 generator->client_server.elements);
746 if (new_lines)
747 {
748 global_update_count+= new_lines;
749 generator->copy_code_to_mariadb= 0;
750 }
751 return new_lines != 0;
752 }
753
754
755 static void generator_count_lines(struct generator *generator)
756 {
757 int mariadbd_additions;
758
759 delete_dup_data_from_mariadbd_group(generator);
760 mariadbd_additions= count_array_elements(&generator->mariadbd_additions);
761 global_mariadbd_additions+= mariadbd_additions;
762 global_update_count+= (mariadbd_additions +
763 generator->old_version.elements +
764 generator->unsupported_version.elements +
765 generator->client_server.elements);
766 global_copied_lines+= generator->old_version.elements;
767 global_unsupported_lines+= generator->unsupported_version.elements;
768 }
769
770
771 static void report_line_changes()
772 {
773 if (!opt_print && !opt_silent && opt_edit_mode == EDIT_MODE_NONE)
774 {
775 if (global_mariadbd_additions)
776 fprintf(stdout, "%lld line/lines would be copied to [mariadbd] group\n",
777 (longlong) global_mariadbd_additions);
778 if (global_copied_lines > 0)
779 fprintf(stdout, "%lld line/lines would be copied to [%s] group\n",
780 (longlong) global_copied_lines, opt_error_group);
781 if (global_unsupported_lines)
782 fprintf(stdout, "%lld line/lines would be copied to [%s] group\n",
783 (longlong) global_unsupported_lines, opt_unsupported_group);
784 }
785 }
786
787
788 static void generator_free(struct generator *generator)
789 {
790 free_root(&generator->alloc, MYF(0));
791 delete_dynamic(&generator->main);
792 delete_dynamic(&generator->old_version);
793 delete_dynamic(&generator->unsupported_version);
794 delete_dynamic(&generator->mariadbd_additions);
795 delete_dynamic(&generator->client_server);
796 my_free(generator->output_group);
797 }
798
799
800 /* Called on include of new default file */
801
802 static void generator_reset(struct generator *generator)
803 {
804 generator->main.elements= 0;
805 generator->old_version.elements= 0;
806 generator->unsupported_version.elements= 0;
807 generator->mariadbd_additions.elements= 0;
808 generator->mariadbd_group_end= -1;
809 }
810
811
812 static char *print_alloc(MEM_ROOT *alloc, const char *format, va_list args)
813 {
814 int length;
815 char *p= NULL;
816 va_list args2;
817 va_copy(args2, args);
818
819 length= vsnprintf(p, 0, format, args) + 1;
820 p= alloc_root(alloc, length);
821 if (!p)
822 return NULL;
823 vsnprintf(p, length, format, args2);
824 va_end(args2);
825 return p;
826 }
827
828 static my_bool vadd_line(MEM_ROOT *alloc, DYNAMIC_ARRAY *array,
829 const char *format, va_list args)
830 {
831 char *p;
832 p= print_alloc(alloc, format, args);
833 if (!p)
834 return TRUE;
835 return insert_dynamic(array, &p);
836 }
837
838 static my_bool add_line(MEM_ROOT *alloc, DYNAMIC_ARRAY *array,
839 const char *format, ...)
840 {
841 va_list args;
842 my_bool res;
843 va_start(args, format);
844 res= vadd_line(alloc, array, format, args);
845 va_end(args);
846 return res;
847 }
848
849 /*
850 Delete elements from array if it already exits.
851
852 Delete is done by setting pointer to string element to 0;
853 */
854
855 static my_bool delete_line(DYNAMIC_ARRAY *array, char *line)
856 {
857 for (uint i= 0; i < array->elements ; i++)
858 {
859 char *element= *dynamic_element(array, i, char**);
860 if (element && !strcmp(element, line))
861 {
862 *((char**) dynamic_array_ptr(array, i))= NullS;
863 return 1;
864 }
865 }
866 return 0;
867 }
868
869
870 /* Count not null elements in the array */
871
872 static uint count_array_elements(DYNAMIC_ARRAY *array)
873 {
874 uint count= 0;
875 for (uint i= 0; i < array->elements ; i++)
876 if (*dynamic_element(array, i, char**))
877 count++;
878 return count;
879 }
880
881 enum line_type
882 {
883 LINE_TYPE_EMPTY,
884 LINE_TYPE_OPTION,
885 LINE_TYPE_OTHER,
886 LINE_TYPE_PASSTHROUGH
887 };
888
889
890 static my_bool add_main_line(struct generator *generator, enum line_type type,
891 const char *format, ...)
892 {
893 va_list args;
894 my_bool res;
895 /* Ensure that --print without edit doesn't loose space */
896 my_bool delay_empty= (type == LINE_TYPE_EMPTY &&
897 opt_edit_mode != EDIT_MODE_NONE);
898
899 /* generator->input_group is empty for comments at top of include files */
900 if (!delay_empty && generator->input_group &&
901 (!generator->output_group ||
902 strcmp(generator->input_group, generator->output_group)))
903 {
904 if (add_line(&generator->alloc, &generator->main,
905 generator->prev_empty ? "[%s]\n" : "\n[%s]\n",
906 generator->input_group))
907 return TRUE;
908 if (change_output_group(generator, generator->input_group))
909 return TRUE;
910 generator->start_of_group= (int) generator->main.elements;
911 }
912 va_start(args, format);
913 res= vadd_line(&generator->alloc, &generator->main, format, args);
914 va_end(args);
915 if (!res)
916 {
917 if (type == LINE_TYPE_OPTION &&
918 !strcmp(generator->input_group, "mariadbd"))
919 {
920 generator->mariadbd_group_start= generator->start_of_group;
921 generator->mariadbd_group_end= (int) generator->main.elements;
922 }
923
924 generator->prev_empty= (type == LINE_TYPE_EMPTY);
925 }
926 return res;
927 }
928
929
930 /*
931 Change file paths from /mysql ending with /, \, . space or \n to /mariadbd
932
933 If string contains 'from' return allocated string otherwise return
934 NullS
935 */
936
937 char *change_file_path(const char *line, LEX_CSTRING *from, LEX_CSTRING *to)
938 {
939 const char *pos;
940
941 for (pos= line ; (pos= strstr(pos, from->str)); pos++)
942 {
943 char end= pos[from->length];
944 if (pos != line && (pos[-1] == '/' || pos[-1] == '\\') &&
945 (end == pos[-1] || end == 0 || my_isspace(&my_charset_latin1, end) ||
946 end == '.'))
947 {
948 /* Found path, change 'from' to 'to' */
949 char *path= my_malloc(key_memory_upgrade_config,
950 strlen(line) - from->length + to->length + 1,
951 MYF(MY_WME));
952 if (!path)
953 break;
954 memcpy(path, line, (size_t) (pos - line));
955 strxmov(path + (pos - line), to->str, pos + from->length, NullS);
956 return path;
957 }
958 }
959 return NullS;
960 }
961
962
963
964 /* Change path with warning */
965
966 static char *path_changed(struct generator *generator, const char *line)
967 {
968 char *new_line;
969 if (!(new_line= change_file_path(line, &mysql_name, &mariadb_name)) &&
970 !(new_line= change_file_path(line, &mysqld_name, &mariadb_name)))
971 return 0;
972
973 if (!opt_print && opt_edit_mode == EDIT_MODE_NONE)
974 {
975 fprintf(stdout, "In %s at line %d: '%.*s' would be "
976 "replaced with '%.*s'\n", generator->name, generator->line,
977 (int) strlen(line)-1, line,
978 (int) strlen(new_line)-1, new_line);
979 global_update_count++;
980 // generator->ctx->failed= 1;
981 }
982 return new_line;
983 }
984
985
986 /*
987 find new name of renamed option
988 */
989
990 const char *renamed_option(const char *option)
991 {
992 struct RENAMED_OPTIONS *options;
993 for (options= renamed_options; options->from ; options++)
994 if (!strcmp(option, options->from))
995 return options->to;
996 return 0;
997 }
998
999
1000 void error_unknown_option(struct generator *generator,
1001 struct upgrade_ctx *ctx,
1002 const char *option, const char *value,
1003 const char *org_line)
1004 {
1005 const char *renamed;
1006
1007 if (!opt_print && opt_edit_mode == EDIT_MODE_NONE &&
1008 !generator->copy_code_to_mariadb)
1009 {
1010 if ((renamed= renamed_option(option)))
1011 {
1012 if (value)
1013 fprintf(stdout, "In %s at line %u: '%.*s' would be "
1014 "replaced with '%s=%.*s'\n", generator->name, generator->line,
1015 (int) strlen(org_line)-1, org_line, renamed,
1016 (int) strlen(value)-1, value);
1017 else
1018 fprintf(stdout, "In %s at line %u: '%.*s' would be "
1019 "replaced with '%s'\n", generator->name, generator->line,
1020 (int) strlen(org_line)-1, org_line, renamed);
1021 }
1022 else
1023 {
1024 fprintf(stdout, "In %s at line %u: Invalid option %s\n", generator->name,
1025 generator->line, option);
1026 }
1027 // ctx->failed= 1;
1028 global_update_count++;
1029 }
1030 if (!strncmp(option, STRING_WITH_LEN("audit_log")) &&
1031 !audit_plugin_warning++ && !opt_silent)
1032 fprintf(stdout, "Warning: Please replace audit_log with the server_audit "
1033 "plugin\n");
1034 }
1035
1036
1037 /*
1038 Store value for socket= option if used
1039 @return 1 if socket found
1040 @return 0 no socket.
1041 */
1042
1043 my_bool check_if_socket(const char *option, const char *value)
1044 {
1045 if (!strcmp(option, "socket"))
1046 {
1047 const char *end;
1048 my_free(value_socket);
1049
1050 /* The string may have an '\n' if paths has changed */
1051 if (!(end= strchr(value, '\n')))
1052 end= strend(value);
1053 value_socket= my_strndup(key_memory_upgrade_config, value, (end-value),
1054 MYF(MY_WME));
1055 return 1; /* Socket found */
1056 }
1057 return 0;
1058 }
1059
1060 /**
1061 Write the given line to generator output as specified by opt_edit_mode.
1062 Returns false on success and true on failure.
1063 */
1064 static my_bool generator_add_line(struct generator *generator, const char *line,
1065 enum line_type type, my_bool is_valid)
1066 {
1067 if (generator->copy_code_to_mariadb && is_valid && type != LINE_TYPE_EMPTY)
1068 {
1069 /* Copy lines from [mysqld-XXX] group to [mariadbd] */
1070 const char *pos= line, *new_line= 0;
1071 if (opt_update_paths &&
1072 (new_line= path_changed(generator, line)))
1073 pos= new_line;
1074 add_line(&generator->alloc, &generator->mariadbd_additions, "%s", pos);
1075 my_free((char*) new_line);
1076 }
1077 if (generator->copy_code_to_mariadb)
1078 is_valid= 1; /* Print line to [mysqld-XXX] groups */
1079
1080 switch (opt_edit_mode) {
1081 case EDIT_MODE_REMOVE:
1082 global_update_count+= MY_TEST(!is_valid);
1083 return is_valid ? add_main_line(generator, type, "%s", line) : FALSE;
1084 case EDIT_MODE_LAST:
1085 if (is_valid || type == LINE_TYPE_EMPTY)
1086 return add_main_line(generator, type, "%s", line);
1087 if (strcmp(generator->input_group, "mariadbd"))
1088 return add_line(&generator->alloc, &generator->old_version, "%s", line);
1089 return add_line(&generator->alloc, &generator->unsupported_version, "%s",
1090 line);
1091 case EDIT_MODE_COMMENT:
1092 global_update_count+= MY_TEST(!is_valid);
1093 return add_main_line(generator, type, is_valid ? "%s" : "#%s", line);
1094 case EDIT_MODE_INLINE:
1095 {
1096 const char *old_input_group= generator->input_group;
1097 my_bool res= TRUE;
1098 global_update_count+= MY_TEST(!is_valid);
1099 if (!is_valid)
1100 generator->input_group= (strcmp(generator->input_group, "mariadbd") ?
1101 opt_error_group :
1102 opt_unsupported_group);
1103 if (!add_main_line(generator, is_valid ? type : LINE_TYPE_OTHER, "%s",
1104 line))
1105 res= FALSE;
1106 generator->input_group= old_input_group;
1107 return res;
1108 }
1109 case EDIT_MODE_NONE:
1110 return opt_print ? add_main_line(generator, type, "%s", line) : 0;
1111 }
1112 return TRUE;
1113 }
1114
1115
1116 static int process_default_file_with_ext(struct upgrade_ctx *ctx,
1117 const char *dir, const char *ext,
1118 const char *config_file,
1119 int recursion_level)
1120 {
1121 char name[FN_REFLEN + 10], buff[4096], curr_gr[4096], *ptr, *end, **tmp_ext;
1122 char tmp_name[FN_REFLEN + 30], restored_name[FN_REFLEN + 30];
1123 char option_buff[4096+2], conv_option[128], tmp[FN_REFLEN];
1124 char *value, *option, *option_value_start, *new_line, *end_of_option;
1125 const char *renamed;
1126 static const char includedir_keyword[]= "includedir";
1127 static const char include_keyword[]= "include";
1128 const int max_recursion_level= 10;
1129 MYSQL_FILE *fp;
1130 MYSQL_FILE *tmp_fp= NULL;
1131 my_bool group_is_mariadbd= FALSE;
1132 my_bool group_is_mysqld= FALSE;
1133 uint option_start;
1134 enum { NONE, PARSE, SKIP } found_group= NONE;
1135 my_bool skip_prefix= 0, generator_inited= 0;
1136 size_t i;
1137 MY_DIR *search_dir;
1138 FILEINFO *search_file;
1139 struct generator generator;
1140
1141 if (recursion_level == 0)
1142 {
1143 value_port= 0;
1144 my_free(value_socket);
1145 value_socket= 0;
1146 found_skip_slave_start= FALSE;
1147 found_innodb_log_files_in_group= 0;
1148 found_innodb_log_file_size= 0;
1149 found_character_set_server= 0;
1150 value_innodb_log_files_in_group= 0;
1151 value_innodb_log_file_size= 0;
1152 suffix_innodb_log_file_size= 0;
1153 found_mariadb_replication_compatibility= 0;
1154 found_mysql_replication_compatibility= 0;
1155 found_client_server_group= 0;
1156 }
1157
1158 if (safe_strlen(dir) + strlen(config_file) >= FN_REFLEN-3)
1159 return 0; /* Ignore wrong paths */
1160 if (dir)
1161 {
1162 end=convert_dirname(name, dir, NullS);
1163 if (dir[0] == FN_HOMELIB) /* Add . to filenames in home */
1164 *end++='.';
1165 strxmov(end,config_file,ext,NullS);
1166 }
1167 else
1168 {
1169 strmov(name,config_file);
1170 }
1171 fn_format(name,name,"","",4);
1172 #if !defined(_WIN32)
1173 {
1174 MY_STAT stat_info;
1175 if (!my_stat(name,&stat_info,
1176 MYF(give_error_for_missing_files ? MY_WME : 0)))
1177 return 1;
1178 /*
1179 Ignore world-writable regular files (exceptions apply).
1180 This is mainly done to protect us to not read a file that may be
1181 modified by anyone.
1182
1183 Also check access so that read only mounted (EROFS)
1184 or immutable files (EPERM) that are suitable protections.
1185
1186 The main case we are allowing is a container readonly volume mount
1187 from a filesystem that doesn't have unix permissions. This will
1188 have a 0777 permission and access will set errno = EROFS.
1189
1190 Note if a ROFS has a file with permissions 04n6, access sets errno
1191 EACCESS, rather the ROFS, so in this case we'll error, even though
1192 the ROFS is protecting the file.
1193
1194 An ideal, race free, implementation would do fstat / fstatvfs / ioctl
1195 for permission, read only filesystem, and immutability resprectively.
1196 */
1197 if ((stat_info.st_mode & S_IWOTH) &&
1198 (stat_info.st_mode & S_IFMT) == S_IFREG &&
1199 (access(name, W_OK) == 0 || (errno != EROFS && errno != EPERM)))
1200 {
1201 fprintf(stderr, "Warning: World-writable config file '%s' is ignored\n",
1202 name);
1203 return 0;
1204 }
1205 }
1206 #endif
1207 if (!(fp= mysql_file_fopen(key_file_cnf, name, O_RDONLY, MYF(MY_WME))))
1208 return 1; /* Ignore wrong files */
1209
1210 if (opt_update)
1211 {
1212 strmov(strmov(tmp_name, name), "-upgrade-config");
1213 strmov(strmov(restored_name, name), "-upgrade-config-orig");
1214 if (!(tmp_fp= mysql_file_fopen(key_file_cnf, tmp_name,
1215 O_RDWR | O_CREAT | O_TRUNC, MYF(0))))
1216 {
1217 fprintf(stderr, "error: Failed to open %s for writing: %s\n", tmp_name,
1218 strerror(errno));
1219 goto err;
1220 }
1221 }
1222
1223 generator_init(&generator);
1224 generator_inited= 1;
1225 generator.name= name;
1226 generator.ctx= ctx;
1227 generator.file_valid= TRUE;
1228 generator.line= 0;
1229
1230 if (opt_print || (opt_edit_mode != EDIT_MODE_NONE && !opt_update))
1231 fprintf(stdout, "### File %s:\n", name);
1232 while (mysql_file_fgets(buff, sizeof(buff) - 3, fp))
1233 {
1234 my_bool line_valid= TRUE;
1235 int invalid_set_index;
1236 generator.line++;
1237 /* Ignore comment and empty lines */
1238 for (ptr= buff; my_isspace(&my_charset_latin1, *ptr); ptr++)
1239 {}
1240
1241 /* Ensure line ends with \n (delete_line code depends one this) */
1242 end= strend(ptr);
1243 if (end != ptr && end[-1] != '\n')
1244 {
1245 end[0]= '\n';
1246 end[1]= 0;
1247 }
1248
1249 if (*ptr == '#' || *ptr == ';' || !*ptr)
1250 {
1251 if (is_prefix(ptr, mariadb_replication_compatibility_comment))
1252 {
1253 /* File is already updated */
1254 found_mariadb_replication_compatibility= 0;
1255 }
1256 if (is_prefix(ptr, mysql_replication_compatibility_comment))
1257 {
1258 /* File is already updated */
1259 found_mysql_replication_compatibility= 0;
1260 }
1261 generator_add_line(&generator, buff,
1262 *ptr ? LINE_TYPE_OTHER : LINE_TYPE_EMPTY, line_valid);
1263 continue;
1264 }
1265
1266 /* Configuration File Directives */
1267 if (*ptr == '!' || *ptr == '?')
1268 {
1269 /* Write out current cached items before include directive */
1270 my_bool prev_empty= generator.prev_empty;
1271 if (opt_print || opt_update)
1272 {
1273 if (generator_write(tmp_fp, &generator))
1274 generator.file_valid= FALSE;
1275 }
1276 else
1277 generator_count_lines(&generator);
1278 generator_reset(&generator);
1279 /* Ensure that when using --print we get !include next */
1280 if (prev_empty && !generator.prev_empty)
1281 generator_add_line(&generator, "\n", LINE_TYPE_EMPTY, 1);
1282 generator_add_line(&generator, buff, LINE_TYPE_PASSTHROUGH, 1);
1283 generator_write(tmp_fp, &generator);
1284 generator_reset(&generator);
1285
1286 if (recursion_level >= max_recursion_level)
1287 {
1288 for (end= ptr + strlen(ptr) - 1;
1289 my_isspace(&my_charset_latin1, *(end - 1));
1290 end--)
1291 {}
1292 end[0]= 0;
1293 fprintf(stderr,
1294 "Warning: skipping '%s' directive as maximum include"
1295 "recursion level was reached in file %s at line %d\n",
1296 ptr, name, generator.line);
1297 continue;
1298 }
1299
1300 /* skip over `!' and following whitespace */
1301 for (++ptr; my_isspace(&my_charset_latin1, ptr[0]); ptr++)
1302 {}
1303
1304 if ((!strncmp(ptr, includedir_keyword,
1305 sizeof(includedir_keyword) - 1)) &&
1306 my_isspace(&my_charset_latin1, ptr[sizeof(includedir_keyword) - 1]))
1307 {
1308 if (!(ptr= get_argument(includedir_keyword,
1309 sizeof(includedir_keyword),
1310 ptr, name, generator.line)))
1311 goto err;
1312
1313 if (!(search_dir= my_dir(ptr, MYF(MY_WME | MY_WANT_SORT))))
1314 goto err;
1315
1316 for (i= 0; i < search_dir->number_of_files; i++)
1317 {
1318 search_file= search_dir->dir_entry + i;
1319 ext= fn_ext2(search_file->name);
1320
1321 /* check extension */
1322 for (tmp_ext= (char**) f_extensions; *tmp_ext; tmp_ext++)
1323 {
1324 if (!strcmp(ext, *tmp_ext))
1325 break;
1326 }
1327
1328 if (*tmp_ext)
1329 {
1330 fn_format(tmp, search_file->name, ptr, "",
1331 MY_UNPACK_FILENAME | MY_SAFE_PATH);
1332
1333 process_default_file_with_ext(ctx, "", "", tmp,
1334 recursion_level + 1);
1335 if (opt_print || (opt_edit_mode != EDIT_MODE_NONE && !opt_update))
1336 fprintf(stdout, "### Back in %s:\n", name);
1337 }
1338 }
1339
1340 my_dirend(search_dir);
1341 }
1342 else if ((!strncmp(ptr, include_keyword, sizeof(include_keyword) - 1)) &&
1343 my_isspace(&my_charset_latin1, ptr[sizeof(include_keyword)-1]))
1344 {
1345 if (!(ptr= get_argument(include_keyword,
1346 sizeof(include_keyword), ptr,
1347 name, generator.line)))
1348 goto err;
1349
1350 process_default_file_with_ext(ctx, "", "", ptr, recursion_level + 1);
1351 if (opt_print || (opt_edit_mode != EDIT_MODE_NONE && !opt_update))
1352 fprintf(stdout, "### Back in %s:\n", name);
1353 }
1354 continue;
1355 }
1356
1357 if (*ptr == '[') /* Group name */
1358 {
1359 if (!(end=(char *) strchr(++ptr,']')))
1360 {
1361 fprintf(stderr,
1362 "error: Wrong group definition in config file: %s at line %d\n",
1363 name, generator.line);
1364 goto err;
1365 }
1366 /* Remove end space */
1367 for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
1368 end[0]=0;
1369
1370 strmake(curr_gr, ptr, MY_MIN((size_t) (end-ptr), sizeof(curr_gr)-1));
1371 group_is_mariadbd= !strcmp(curr_gr, "mariadbd");
1372 group_is_mysqld= (!strcmp(curr_gr, "mysqld") ||
1373 !strcmp(curr_gr, "server"));
1374 found_client_server_group|= !strcmp(curr_gr, client_server_group);
1375
1376 generator.copy_code_to_mariadb= 0;
1377 found_group= find_type(curr_gr, ctx->group, FIND_TYPE_NO_PREFIX)
1378 ? PARSE : SKIP;
1379 /* Ensure we process all groups with the mysqld prefix */
1380 if (found_group == SKIP)
1381 {
1382 if (!strncmp(curr_gr, "mysqld-", 7) && opt_copy_mysqld_groups)
1383 {
1384 found_group= PARSE;
1385 /* Copy all valid code from the mysqld- group to the mariadbd group */
1386 generator.copy_code_to_mariadb= 1;
1387 }
1388 }
1389
1390 generator.input_group= curr_gr;
1391 if (found_group == PARSE && group_is_mariadbd)
1392 {
1393 /**
1394 Force the mariadbd group to be emitted to allow potentially adding
1395 new lines to it.
1396 */
1397 generator_add_line(&generator, "", LINE_TYPE_OPTION, TRUE);
1398 }
1399 continue;
1400 }
1401 switch (found_group) {
1402 case NONE:
1403 fprintf(stderr,
1404 "error: Found option without preceding group in config "
1405 "file: %s at line: %d\n",
1406 name, generator.line);
1407 goto err;
1408 case PARSE:
1409 if (opt_print && opt_edit_mode == EDIT_MODE_NONE)
1410 {
1411 generator_add_line(&generator, buff, LINE_TYPE_OPTION, TRUE);
1412 continue;
1413 }
1414 break;
1415 case SKIP:
1416 if (group_is_mariadbd &&
1417 (opt_edit_mode == EDIT_MODE_INLINE ||
1418 opt_edit_mode == EDIT_MODE_LAST))
1419 {
1420 /* Remove lines with the /mysql/ path */
1421 char *pos= change_file_path(buff, &mariadb_name, &mysql_name);
1422 if (pos)
1423 {
1424 delete_line(&generator.mariadbd_additions, pos);
1425 my_free(pos);
1426 }
1427 delete_line(&generator.mariadbd_additions, buff);
1428 }
1429 generator_add_line(&generator, buff, LINE_TYPE_OPTION, line_valid);
1430 continue;
1431 }
1432
1433 end= end_of_option= find_end_comment(ptr);
1434 if ((value= strchr(ptr, '=')) && value < end_of_option)
1435 end= value;
1436 else
1437 value= 0;
1438
1439 for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--)
1440 ;
1441 ptr= strmake(option_buff, ptr, (size_t) (end-ptr));
1442
1443 /* Change '-' to '_' in the option */
1444 {
1445 char *from, *from_end, *to;
1446 from_end= option_buff + sizeof(conv_option)-1;
1447 for (from= option_buff, to= conv_option;
1448 *from && from < from_end;
1449 from++, to++)
1450 *to= (*from == '-') ? '_' : *from;
1451 *to= 0;
1452 option_start= 0;
1453 skip_prefix= 0;
1454
1455 /* Ignore some prefix */
1456 for (uint i= 0; special_opt_prefix[i] ; i++)
1457 {
1458 if (!strncmp(conv_option, special_opt_prefix[i],
1459 special_opt_prefix_lengths[i]) &&
1460 conv_option[special_opt_prefix_lengths[i]] == '_' &&
1461 conv_option[special_opt_prefix_lengths[i]+1])
1462 {
1463 option_start= special_opt_prefix_lengths[i]+1;
1464 skip_prefix= i == 0; /* skip- */
1465 break;
1466 }
1467 }
1468 }
1469 option= conv_option + option_start;
1470
1471 if (value)
1472 {
1473 /* Remove pre- and end space */
1474 char *value_end;
1475 my_bool tmp;
1476
1477 for (value++ ; my_isspace(&my_charset_latin1,*value); value++) ;
1478 value_end= end_of_option;
1479 /*
1480 We don't have to test for value_end >= value as we know there is
1481 an '=' before
1482 */
1483 for ( ; my_isspace(&my_charset_latin1,value_end[-1]) ; value_end--) ;
1484 if (value_end < value) /* Empty string */
1485 value_end= value;
1486
1487 /* remove quotes around argument */
1488 if ((*value == '\"' || *value == '\'') && /* First char is quote */
1489 (value + 1 < value_end ) && /* String is longer than 1 */
1490 *value == value_end[-1] ) /* First char is equal to last char */
1491 {
1492 value++;
1493 value_end--;
1494 }
1495 *ptr++= 0;
1496 option_value_start= ptr;
1497
1498 tmp= mariadbd_option_exists(option);
1499 if (!tmp && skip_prefix && mariadbd_option_exists(conv_option))
1500 {
1501 /* Option only exists with the 'skip-' prefix */
1502 tmp= 1;
1503 option= conv_option; /* Option with skip- prefix */
1504 }
1505 if (!tmp)
1506 {
1507 if (generator.copy_code_to_mariadb) /* [mysqld-#] group */
1508 {
1509 /* Changes will be detected in generator_write() */
1510 generator_add_line(&generator, buff, LINE_TYPE_OPTION, FALSE);
1511 continue;
1512 }
1513 line_valid= FALSE;
1514 generator.file_valid= FALSE;
1515
1516 error_unknown_option(&generator, ctx, conv_option, value, buff);
1517 if (!opt_print && opt_edit_mode == EDIT_MODE_NONE)
1518 continue;
1519 }
1520 for ( ; value != value_end; value++)
1521 {
1522 if (*value == '\\' && value != value_end-1)
1523 {
1524 switch(*++value) {
1525 case 'n':
1526 *ptr++='\n';
1527 break;
1528 case 't':
1529 *ptr++= '\t';
1530 break;
1531 case 'r':
1532 *ptr++ = '\r';
1533 break;
1534 case 'b':
1535 *ptr++ = '\b';
1536 break;
1537 case 's':
1538 *ptr++= ' '; /* space */
1539 break;
1540 case '\"':
1541 *ptr++= '\"';
1542 break;
1543 case '\'':
1544 *ptr++= '\'';
1545 break;
1546 case '\\':
1547 *ptr++= '\\';
1548 break;
1549 default: /* Unknown; Keep '\' */
1550 *ptr++= '\\';
1551 *ptr++= *value;
1552 break;
1553 }
1554 }
1555 else
1556 *ptr++= *value;
1557 }
1558 *ptr=0;
1559 if (!mariadbd_valid_enum_value(option, option_value_start))
1560 {
1561 line_valid= FALSE;
1562 generator.file_valid= FALSE;
1563 if (!opt_print && opt_edit_mode == EDIT_MODE_NONE)
1564 {
1565 fprintf(stdout,
1566 "In %s at line %d: Invalid enum value '%s' for option %s\n",
1567 name, generator.line, option_value_start, option);
1568 global_update_count++;
1569 // ctx->failed= 1;
1570 continue;
1571 }
1572 }
1573 else if ((invalid_set_index=
1574 mariadbd_check_set_value(option, option_value_start)))
1575 {
1576 line_valid= FALSE;
1577 generator.file_valid= FALSE;
1578 if (!opt_print && opt_edit_mode == EDIT_MODE_NONE)
1579 {
1580 fprintf(stdout, "In %s at line %d: Invalid set value '%s' at "
1581 "index %d for option %s\n",
1582 name, generator.line, option_value_start, invalid_set_index,
1583 option);
1584 global_update_count++;
1585 // ctx->failed= 1;
1586 continue;
1587 }
1588 }
1589 else if (check_plugin_option(ctx, option, option_value_start, name,
1590 generator.line))
1591 {
1592 line_valid= FALSE;
1593 generator.file_valid= FALSE;
1594 }
1595 else if (group_is_mysqld && line_valid &&
1596 opt_update_paths &&
1597 (new_line= path_changed(&generator, buff)))
1598 {
1599 line_valid= FALSE;
1600
1601 /* We have alread validated that buff contains an '=' */
1602 check_if_socket(option, strchr(new_line, '=')+1);
1603 if ((opt_edit_mode == EDIT_MODE_INLINE ||
1604 opt_edit_mode == EDIT_MODE_LAST))
1605 {
1606 generator_add_line(&generator, buff, LINE_TYPE_OPTION, FALSE);
1607 add_line(&generator.alloc, &generator.mariadbd_additions, "%s",
1608 new_line);
1609 my_free(new_line);
1610 continue;
1611 }
1612 my_free(new_line);
1613 }
1614 else if (!group_is_mariadbd &&
1615 !strcmp(option, "key_buffer_size") &&
1616 (opt_no_myisam_options || opt_convert_from == FROM_MYSQL) &&
1617 ((!opt_print && opt_edit_mode == EDIT_MODE_NONE) ||
1618 opt_edit_mode == EDIT_MODE_INLINE ||
1619 opt_edit_mode == EDIT_MODE_LAST))
1620 {
1621 generator.file_valid= FALSE;
1622 if (opt_edit_mode == EDIT_MODE_NONE)
1623 {
1624 {
1625 fprintf(stdout, "In %s at line %d: key_buffer_size needs "
1626 "modifications. If you want to git rid of this warning and "
1627 "keep as it now, move it to the [mariadbd] group\n",
1628 name, generator.line);
1629 global_update_count++;
1630 }
1631 continue;
1632 }
1633 generator_add_line(&generator, buff, LINE_TYPE_OPTION, FALSE);
1634 if (opt_no_myisam_options)
1635 {
1636 add_line(&generator.alloc, &generator.mariadbd_additions,
1637 "key_buffer_size=64K # Recommended value if no "
1638 "MyISAM tables\n");
1639 snprintf(conv_option, sizeof(conv_option)-1,
1640 "aria_pagecache_buffer_size=%s",
1641 option_value_start);
1642 add_line(&generator.alloc, &generator.mariadbd_additions, "%s\n",
1643 conv_option);
1644 if (!opt_silent)
1645 fprintf(stdout, "Note: '%s' added to [mariadbd] group\n",
1646 conv_option);
1647 }
1648 else if (opt_convert_from == FROM_MYSQL)
1649 {
1650 add_line(&generator.alloc, &generator.mariadbd_additions, "%s", buff);
1651 add_line(&generator.alloc, &generator.mariadbd_additions,
1652 "#key_buffer_size=64K "
1653 "# Recommended value if no MyISAM tables\n");
1654 snprintf(conv_option, sizeof(conv_option)-1,
1655 "aria_pagecache_buffer_size=%s",
1656 option_value_start);
1657 add_line(&generator.alloc, &generator.mariadbd_additions, "%s\n",
1658 conv_option);
1659 if (!opt_silent)
1660 fprintf(stdout, "Note: %s added to [mariadbd] group\n",
1661 conv_option);
1662 }
1663 continue;
1664 }
1665 else if (!group_is_mariadbd &&
1666 !strcmp(option, "innodb_log_files_in_group"))
1667 {
1668 found_innodb_log_files_in_group= 1;
1669 value_innodb_log_files_in_group= atoi(option_value_start);
1670 if (!value_innodb_log_files_in_group)
1671 value_innodb_log_files_in_group= 1; /* Ensure it is not 0 */
1672 }
1673 else if (!strcmp(option, "innodb_log_file_size"))
1674 {
1675 if (group_is_mariadbd)
1676 {
1677 /* Already handled. Don't add it again */
1678 found_innodb_log_file_size= 0;
1679 found_innodb_log_files_in_group= 0;
1680 }
1681 else
1682 {
1683 int error;
1684 char *int_end= 0;
1685 value_innodb_log_file_size= my_strtoll10(option_value_start,
1686 &int_end, &error);
1687 found_innodb_log_file_size= (value_innodb_log_file_size != 0 &&
1688 error == 0);
1689 suffix_innodb_log_file_size= *int_end;
1690 }
1691 }
1692 else if (!strcmp(option, "character_set_server") ||
1693 !strcmp(option, "collation_server"))
1694 {
1695 found_character_set_server= 1;
1696 }
1697 else if (group_is_mysqld && !strcmp(option, "port"))
1698 {
1699 int error;
1700 char *int_end= 0;
1701 value_port= my_strtoll10(option_value_start, &int_end, &error);
1702 }
1703 else if (group_is_mysqld && check_if_socket(option, option_value_start))
1704 {
1705 }
1706 else if ((renamed= renamed_option(option)))
1707 {
1708 generator_add_line(&generator, buff, LINE_TYPE_OPTION, FALSE);
1709 add_line(&generator.alloc, &generator.mariadbd_additions,
1710 "%s=%s\n", renamed, option_value_start);
1711 continue;
1712 }
1713 } /* !value */
1714 else if (!group_is_mariadbd && !strcmp(option, "skip_slave_start"))
1715 {
1716 found_skip_slave_start= TRUE;
1717 }
1718 else if (!mariadbd_option_exists(option) &&
1719 !(skip_prefix && mariadbd_option_exists(conv_option)))
1720 {
1721 line_valid= FALSE;
1722 generator.file_valid= FALSE;
1723
1724 error_unknown_option(&generator, ctx, conv_option, NullS, buff);
1725
1726 if (!opt_print && opt_edit_mode == EDIT_MODE_NONE &&
1727 !generator.copy_code_to_mariadb)
1728 continue;
1729 }
1730 else if ((renamed= renamed_option(option)))
1731 {
1732 generator_add_line(&generator, buff, LINE_TYPE_OPTION, FALSE);
1733 add_line(&generator.alloc, &generator.mariadbd_additions,
1734 "%s\n", renamed);
1735 continue;
1736 }
1737 generator_add_line(&generator, buff, LINE_TYPE_OPTION, line_valid);
1738 }
1739 mysql_file_fclose(fp, MYF(0));
1740 fp= 0;
1741
1742 if (recursion_level == 0)
1743 {
1744 if (found_innodb_log_file_size && found_innodb_log_files_in_group)
1745 {
1746 generator.file_valid= FALSE;
1747 snprintf(conv_option, sizeof(conv_option)-1,
1748 "innodb_log_file_size=%llu%c",
1749 value_innodb_log_file_size * value_innodb_log_files_in_group,
1750 (my_islower(&my_charset_latin1, suffix_innodb_log_file_size) ||
1751 my_isupper(&my_charset_latin1, suffix_innodb_log_file_size)) ?
1752 suffix_innodb_log_file_size : ' ');
1753 add_line(&generator.alloc, &generator.mariadbd_additions, "%s\n", conv_option);
1754 if (!opt_silent)
1755 fprintf(stdout, "Note: '%s' added to [mariadbd] group based on "
1756 "innodb_log_file_size * innodb_log_files_in_group\n", conv_option);
1757 found_innodb_log_file_size= found_innodb_log_files_in_group= 0;
1758 }
1759 if (opt_add_client_server_group && !found_client_server_group)
1760 {
1761 if (value_port)
1762 add_line(&generator.alloc, &generator.client_server,
1763 "port=%lld\n", value_port);
1764 if (value_socket)
1765 {
1766 add_line(&generator.alloc, &generator.client_server,
1767 "socket=%s\n", value_socket);
1768 my_free(value_socket);
1769 value_socket= 0;
1770 }
1771 }
1772 /* The following options should only be added once */
1773 if (!add_new_options_once || !new_options_added)
1774 {
1775 new_options_added= 1;
1776
1777 /* Add generated options to mariadbd section */
1778 if (opt_add_skip_slave_start && !found_skip_slave_start)
1779 {
1780 generator.file_valid= FALSE;
1781 add_line(&generator.alloc, &generator.mariadbd_additions,
1782 "skip_slave_start\n");
1783 found_skip_slave_start= 0;
1784 }
1785 if (opt_add_mariadb_replication_compatibility &&
1786 !found_mariadb_replication_compatibility)
1787 {
1788 generator.file_valid= FALSE;
1789 /* We do this line by line to get correct row numbers for reporting */
1790 add_line(&generator.alloc, &generator.old_version,
1791 "%s\n", mariadb_replication_compatibility_comment);
1792 add_line(&generator.alloc, &generator.old_version,
1793 "loose-binlog_row_value_options=\"\"\n");
1794 add_line(&generator.alloc, &generator.old_version,
1795 "loose-binlog_transaction_compression=0\n");
1796 add_line(&generator.alloc, &generator.old_version,
1797 "# Not required, but recommended for cross_replication\n");
1798 add_line(&generator.alloc, &generator.old_version,
1799 "binlog_format=row\n");
1800 }
1801 if (opt_add_mysql_replication_compatibility &&
1802 !found_mysql_replication_compatibility)
1803 {
1804 add_line(&generator.alloc, &generator.mariadbd_additions,
1805 "%s\n", mysql_replication_compatibility_comment);
1806 add_line(&generator.alloc, &generator.mariadbd_additions,
1807 "encrypt_binlog=0\n");
1808 add_line(&generator.alloc, &generator.mariadbd_additions,
1809 "log_bin_compress=0\n");
1810 add_line(&generator.alloc, &generator.mariadbd_additions,
1811 "# Not required, but recommended for cross_replication\n");
1812 add_line(&generator.alloc, &generator.mariadbd_additions,
1813 "binlog_format=row\n");
1814 if (!found_character_set_server)
1815 {
1816 add_line(&generator.alloc, &generator.mariadbd_additions,
1817 "# Character set to be able to replicate new tables to "
1818 "MySQL 8.0\n");
1819 add_line(&generator.alloc, &generator.mariadbd_additions,
1820 "character_set_server=utf8mb4\n");
1821 add_line(&generator.alloc, &generator.mariadbd_additions,
1822 "collation_server=utf8mb4_0900_ai_ci\n");
1823 if (!opt_silent)
1824 fprintf(stdout, "Note: Added MySQL default character sets and "
1825 "collation to [mariadbd] to be able to replicate to "
1826 "MySQL\n");
1827 found_character_set_server= 0;
1828 }
1829 }
1830 }
1831 }
1832 if (opt_print || opt_update)
1833 {
1834 if (generator_write(tmp_fp, &generator))
1835 generator.file_valid= 0;
1836 }
1837 else
1838 generator_count_lines(&generator);
1839 generator_free(&generator);
1840 if (recursion_level == 0)
1841 {
1842 my_free(value_socket);
1843 value_socket= 0;
1844 }
1845
1846 if (tmp_fp)
1847 {
1848 mysql_file_fclose(tmp_fp, MYF(0));
1849 tmp_fp= 0;
1850 if (generator.file_valid)
1851 {
1852 my_delete(tmp_name, MYF(0));
1853 }
1854 else
1855 {
1856 myf redel_flags = opt_backup ? MYF(MY_REDEL_MAKE_BACKUP) : MYF(0);
1857 char *duped_name= my_strdup(key_memory_upgrade_config, name, MYF(0));
1858 if (!duped_name || insert_dynamic(&ctx->updated_files, &duped_name))
1859 goto err;
1860 /*
1861 Copy the file in case running mariadbd fails and the update must be
1862 reverted.
1863 */
1864 if (my_copy(name, restored_name, MYF(0)))
1865 {
1866 fprintf(stderr, "error: Failed to copy %s to %s: %s",
1867 name, restored_name, strerror(errno));
1868 return -1;
1869 }
1870 if (my_redel(name, tmp_name, time(NULL), redel_flags))
1871 {
1872 fprintf(stderr, "error: Failed to rename %s to %s: %s",
1873 tmp_name, name, strerror(errno));
1874 return -1;
1875 }
1876 }
1877 }
1878 if (opt_print || (opt_edit_mode != EDIT_MODE_NONE && !opt_update))
1879 fputc('\n', stdout);
1880 return(0);
1881
1882 err:
1883 if (fp)
1884 mysql_file_fclose(fp, MYF(0));
1885 if (tmp_fp)
1886 mysql_file_fclose(tmp_fp, MYF(0));
1887 if (generator_inited)
1888 generator_free(&generator);
1889 if (recursion_level == 0)
1890 {
1891 my_free(value_socket);
1892 value_socket= 0;
1893 }
1894 return -1; /* Fatal error */
1895 }
1896
1897
1898 static int process_default_file(struct upgrade_ctx *ctx,
1899 const char *dir,
1900 const char *config_file)
1901 {
1902 char **ext;
1903 const char *empty_list[]= { "", 0 };
1904 my_bool have_ext= fn_ext(config_file)[0] != 0;
1905 const char **exts_to_use= have_ext ? empty_list : f_extensions;
1906
1907 for (ext= (char**) exts_to_use; *ext; ext++)
1908 {
1909 int error;
1910 if ((error= process_default_file_with_ext(ctx, dir, *ext, config_file,
1911 0)) < 0)
1912 return error;
1913 }
1914 return 0;
1915 }
1916
1917
1918 static int process_option_files(const char *conf_file,
1919 struct upgrade_ctx *ctx,
1920 const char **default_directories)
1921 {
1922 const char **dirs;
1923 int error= 0;
1924
1925 if (my_defaults_group_suffix)
1926 {
1927 /* Handle --defaults-group-suffix= */
1928 uint i;
1929 const char **extra_groups;
1930 const size_t instance_len= strlen(my_defaults_group_suffix);
1931 char *ptr;
1932 TYPELIB *group= ctx->group;
1933
1934 if (!(extra_groups=
1935 (const char**)alloc_root(ctx->alloc,
1936 (2*group->count+1)*sizeof(char*))))
1937 return 2;
1938
1939 for (i= 0; i < group->count; i++)
1940 {
1941 size_t len;
1942 extra_groups[i]= group->type_names[i]; /** copy group */
1943
1944 len= strlen(extra_groups[i]);
1945 if (!(ptr= alloc_root(ctx->alloc, (uint) (len+instance_len+1))))
1946 return 2;
1947
1948 extra_groups[i+group->count]= ptr;
1949
1950 /** Construct new group */
1951 memcpy(ptr, extra_groups[i], len);
1952 memcpy(ptr+len, my_defaults_group_suffix, instance_len+1);
1953 }
1954
1955 group->count*= 2;
1956 group->type_names= extra_groups;
1957 group->type_names[group->count]= 0;
1958 }
1959
1960 if (my_defaults_file)
1961 {
1962 if ((error= process_default_file_with_ext(ctx, "", "",
1963 my_defaults_file, 0)) < 0)
1964 goto err;
1965 if (error > 0)
1966 {
1967 fprintf(stderr, "Could not open required defaults file: %s\n",
1968 my_defaults_file);
1969 goto err;
1970 }
1971 }
1972 else if (dirname_length(conf_file) || !default_directories)
1973 {
1974 if ((error= process_default_file(ctx, NullS, conf_file)) < 0)
1975 goto err;
1976 }
1977 else
1978 {
1979 for (dirs= default_directories ; *dirs; dirs++)
1980 {
1981 if (**dirs)
1982 {
1983 if (process_default_file(ctx, *dirs, conf_file) < 0)
1984 goto err;
1985 }
1986 else if (my_defaults_extra_file)
1987 {
1988 if ((error= process_default_file_with_ext(ctx, "", "",
1989 my_defaults_extra_file,
1990 0)) < 0)
1991 goto err; /* Fatal error */
1992 if (error > 0)
1993 {
1994 fprintf(stderr, "Could not open required defaults file: %s\n",
1995 my_defaults_extra_file);
1996 goto err;
1997 }
1998 }
1999 }
2000 }
2001
2002 return 0;
2003
2004 err:
2005 fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
2006 return 1;
2007 }
2008
2009
2010 /*
2011 Process defaults
2012
2013 @args default_args --default options. Sent forward to mariadbd
2014 @args default_args_count Number of default options
2015
2016 @return 0 ok
2017 @return 1 general error
2018 @return 2 memory allocation error
2019 */
2020
2021 static int process_defaults(const char *conf_file,
2022 const char **groups,
2023 const char **dirs,
2024 const char **defaults_args,
2025 int defaults_args_count)
2026 {
2027 int error= 0;
2028 MEM_ROOT alloc;
2029 TYPELIB group;
2030 struct upgrade_ctx ctx;
2031 char executable[FN_REFLEN];
2032
2033 init_alloc_root(key_memory_upgrade_config, &alloc, 512, 0, MYF(0));
2034
2035 group.count= 0;
2036 group.name= "defaults";
2037 group.type_names= groups;
2038
2039 for (; *groups ; groups++)
2040 group.count++;
2041
2042 ctx.alloc= &alloc;
2043 ctx.group= &group;
2044 init_dynamic_array2(key_memory_upgrade_config,
2045 &ctx.updated_files,
2046 sizeof(char *),
2047 NULL,
2048 0,
2049 0,
2050 MYF(0));
2051 ctx.failed= 0;
2052
2053 if ((error= process_option_files(conf_file, &ctx, dirs)))
2054 {
2055 finish_updated_files(&ctx, FALSE);
2056 free_root(&alloc,MYF(0));
2057 return error;
2058 }
2059
2060 if (opt_mariadbd_testing)
2061 {
2062 DYNAMIC_STRING mariadbd_output;
2063 const char *mariadbd_name= IF_WIN("mariadbd.exe", "mariadbd");
2064 const char *used_name;
2065
2066 if (opt_mariadbd)
2067 used_name= opt_mariadbd;
2068 else
2069 {
2070 used_name= mariadbd_name;
2071 if (!(error= (find_file_in_path(executable, mariadbd_name) != 0)))
2072 used_name= executable;
2073 }
2074 if (!error)
2075 error= access(used_name, X_OK);
2076
2077 if (error)
2078 {
2079 fprintf(stderr,
2080 "Cannot execute '%s' to test if the new option files "
2081 "works (error %d)\n"
2082 "Try specifiying the full path for the MariaDB server with "
2083 "--mariadbd=...\n",
2084 used_name, (int) errno);
2085 finish_updated_files(&ctx, 1);
2086 return 1;
2087 }
2088
2089 if (init_dynamic_string(&mariadbd_output, "", 32, 32))
2090 goto err;
2091 if (!test_mariadbd(used_name) &&
2092 (error= run_mariadbd(used_name, &mariadbd_output, defaults_args,
2093 defaults_args_count)))
2094 {
2095 if (ctx.updated_files.elements)
2096 fprintf(stderr, "error: Failed to run %s with the updated files, "
2097 "reverting\n", used_name);
2098 else
2099 fprintf(stderr, "error: Failed to run %s with the original files.\n",
2100 used_name);
2101 if (error > 0)
2102 fprintf(stderr, "mariadbd output:\n%s", mariadbd_output.str);
2103 }
2104 dynstr_free(&mariadbd_output);
2105 }
2106
2107 if (finish_updated_files(&ctx, error == 0))
2108 error= 1;
2109 free_root(&alloc, MYF(0));
2110 return error;
2111
2112 err:
2113 finish_updated_files(&ctx, FALSE);
2114 free_root(&alloc, MYF(0));
2115 return 1;
2116 }
2117
2118
2119 static const char *config_file="my"; /* Default config file */
2120
2121 enum upgrade_config_options
2122 {
2123 OPT_UPDATE = 256,
2124 OPT_BACKUP,
2125 OPT_ERROR_GROUP,
2126 OPT_UNSUPPORTED_GROUP,
2127 OPT_EDIT,
2128 OPT_PRINT,
2129 OPT_NO_MYISAM_OPTIONS,
2130 OPT_ADD_SKIP_SLAVE_START,
2131 OPT_FIX_ALL,
2132 OPT_CONVERT_FROM,
2133 OPT_UPDATE_PATHS,
2134 OPT_VERIFY,
2135 OPT_MARIADBD,
2136 OPT_ADD_MYSQL_REPLICATION_COMPATIBILITY,
2137 OPT_ADD_MARIADB_REPLICATION_COMPATIBILITY,
2138 OPT_COPY_MYSQLD_GROUPS,
2139 OPT_GENERATE_CLIENT_SERVER_GROUP
2140 };
2141
2142 static struct my_option my_long_options[] =
2143 {
2144 {"add-skip-slave-start", OPT_ADD_SKIP_SLAVE_START,
2145 "Add skip_slave_start to the [mariadbd] group if not present.",
2146 &opt_add_skip_slave_start, &opt_add_skip_slave_start,
2147 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2148 {"add-mariadb-replication-compatibility",
2149 OPT_ADD_MARIADB_REPLICATION_COMPATIBILITY,
2150 "Add options to [error-group] to enable MySQL to replicate to MariaDB. "
2151 "Options added are: "
2152 "binlog-row-value-options=\"\" binlog_transaction_compression=0 and "
2153 "binlog_format=row.",
2154 &opt_add_mariadb_replication_compatibility,
2155 &opt_add_mariadb_replication_compatibility,
2156 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2157 {"add-mysql-replication-compatibility",
2158 OPT_ADD_MYSQL_REPLICATION_COMPATIBILITY,
2159 "Add options to [mariadbd] section to enable MariaDB to replicate to MySQL.",
2160 &opt_add_mysql_replication_compatibility,
2161 &opt_add_mysql_replication_compatibility,
2162 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2163 {"add-client-server-group",
2164 OPT_ADD_MYSQL_REPLICATION_COMPATIBILITY,
2165 "Add a [client-server] group if it does not exists. This will contain the "
2166 "last port and socket found in the [mysqld] or [server] groups.",
2167 &opt_add_client_server_group,
2168 &opt_add_client_server_group,
2169 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2170 {"backup", OPT_BACKUP,
2171 "Backup the updated configuration files. The backup file name are of the "
2172 "type: 'original_name'-timestamp.BAK",
2173 &opt_backup, &opt_backup, 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2174 {"copy-mysqld-groups", OPT_COPY_MYSQLD_GROUPS,
2175 "Copy valid MariaDB options from [mysqld-###] groups to the [mariadbd] "
2176 "group.",
2177 &opt_copy_mysqld_groups, &opt_copy_mysqld_groups, 0, GET_BOOL, NO_ARG, TRUE,
2178 0, 0, 0, 0, 0},
2179 {"error-group", OPT_ERROR_GROUP,
2180 "Group to use for options not supported by MariaDB when using "
2181 "--edit=[inline|last]. Should be set usually to something like "
2182 "'mysqld-#.##' when moving from MySQL and 'unknown' when upgrading "
2183 "MariaDB or if there are already [mariadbd] groups in the config files.",
2184 &opt_error_group, &opt_error_group,
2185 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
2186 {"edit", OPT_EDIT,
2187 "Select what to do with invalid options. '' (default) no changes. "
2188 "'remove' removes them. 'comment' comments them with '#'. "
2189 "inline' adds [error_group] before unknown options. "
2190 "'last' moves incompatible or mariadbd specific options to the end of "
2191 "the given file under [error_group], [unsupported_group] or [mariadbd].",
2192 &opt_edit_mode, &opt_edit_mode, &edit_mode_typelib, GET_ENUM,
2193 REQUIRED_ARG, EDIT_MODE_NONE, 0, 0, 0, 0, 0},
2194 {"fix-all", OPT_FIX_ALL,
2195 "Same as --no-myisam --from=mysql --add-skip-slave-start "
2196 "--add-mysql-replication-compatibility "
2197 "--add-mariadb-replication-compatibility --add-client-server-group "
2198 "--edit=last",
2199 0, 0, 0, GET_NO_ARG, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2200 {"from", OPT_CONVERT_FROM,
2201 "Options are MySQL (default) or MariaDB. Used to specify if your are "
2202 "upgrading from MySQL or an earlier MariaDB version. "
2203 "When upgrading from MySQL, only the MySQL groups [mysqld] and [server] "
2204 "are processed. If MariaDB is specified, all MariaDB entries read by "
2205 "the current MariaDB version (" MYSQL_SERVER_VERSION ") will be processed. "
2206 "Note that when upgrading from an earlier MariaDB version, mariadb-upgrade "
2207 "is the preferred tool! This tool is in this case mainly useful to check "
2208 "if there is any incompatibilities in the MariaDB configuration files.",
2209 &opt_convert_from, &opt_convert_from, &from_mode_typelib, GET_ENUM,
2210 REQUIRED_ARG, FROM_MYSQL, 0, 0, 0, 0, 0},
2211 {"help", '?', "Display this help message and exit.",
2212 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
2213 {"mariadbd", OPT_MARIADBD,
2214 "Path to the mariadbd server. Used to verify if the config file changes "
2215 "succeeded.",
2216 &opt_mariadbd, &opt_mariadbd, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
2217 {"no-myisam", OPT_NO_MYISAM_OPTIONS,
2218 "Assume no usage of MyISAM tables. Makes the MyISAM buffers very small "
2219 "to save memory. "
2220 "Useful when moving to MariaDB when there are no user data in MyISAM "
2221 "tables. This option only has an effect if the config file contains "
2222 "settings of the key-buffer-size variable.",
2223 &opt_no_myisam_options, &opt_no_myisam_options, 0, GET_BOOL, NO_ARG,
2224 FALSE, 0, 0, 0, 0, 0},
2225 {"print", OPT_PRINT, "Print upgraded files to stdout. The 'edit' option "
2226 "decides the output format.",
2227 &opt_print, &opt_print, 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2228 {"silent", 's', "Print less information",
2229 &opt_silent, &opt_silent, 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2230 {"update", OPT_UPDATE, "Update the configuration files in place.",
2231 &opt_update, &opt_update, 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2232 {"update-paths", OPT_UPDATE_PATHS,
2233 "When used with --from=mysql, change file paths having /mysql or /mysqld "
2234 "ending with '/', '.', space, or new line to /mariadbd. This is intended "
2235 "to be used when you want to run MySQL and MariaDB on the same system.",
2236 &opt_update_paths, &opt_update_paths,
2237 0, GET_BOOL, NO_ARG, FALSE, 0, 0, 0, 0, 0},
2238 {"unsupported-group", OPT_UNSUPPORTED_GROUP,
2239 "Group to use for options in [mariadbd] group that is not supported by "
2240 "MariaDB when using '--edit=[inline|last] --from=mariadb'.",
2241 &opt_unsupported_group, &opt_unsupported_group,
2242 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
2243 {"verify", OPT_VERIFY,
2244 "Verify updates by testing the used config files through mariadbd. "
2245 "Disabled if --print is used. "
2246 "Note that verify only works if one uses --defaults-file=# or if one does "
2247 "not specify any configuration files on the command line. "
2248 "When using verify the mariadbd option has also to be used.",
2249 &opt_mariadbd_testing, &opt_mariadbd_testing, 0, GET_BOOL, NO_ARG, FALSE,
2250 0, 0, 0, 0, 0},
2251 {"version", 'V', "Output version information and exit.",
2252 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
2253 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
2254 };
2255
2256 static void cleanup_and_exit(int exit_code) __attribute__ ((noreturn));
2257 static void cleanup_and_exit(int exit_code)
2258 {
2259 my_end(0);
2260 exit(exit_code);
2261 }
2262
2263
2264 static void print_to_width(const char *line, uint width)
2265 {
2266 const char *start= line, *space= 0;
2267 fputs("- ", stdout);
2268
2269 for (start= line ; *line ; line++)
2270 {
2271 if ((uint)(line-start) >= width)
2272 {
2273 if (!space)
2274 space= line; /* No space */
2275 for ( ; start < space ; start++)
2276 putchar(*start);
2277 fputs("\n ", stdout);
2278 while (*start == ' ')
2279 start++;
2280 space= 0;
2281 }
2282 if (*line == ' ')
2283 space= line;
2284 }
2285 printf("%s:\n", start);
2286 }
2287
2288
2289 static void pe(const char *example, const char *description)
2290 {
2291 print_to_width(description, 77);
2292 fprintf(stdout, "%s %s\n\n",
2293 my_progname + dirname_length(my_progname),
2294 example);
2295 }
2296
2297
2298 static void usage() __attribute__ ((noreturn));
2299 static void usage()
2300 {
2301 print_version();
2302 puts("This software comes with ABSOLUTELY NO WARRANTY. This is free "
2303 "software,\nand you are welcome to modify and redistribute it under "
2304 "the GPL license\n");
2305 puts("Display or change unrecognized MariaDB options in MariaDB/MySQL\n"
2306 "configuration files. This is useful when planning an upgrade from\n"
2307 "MySQL to MariaDB or to check if there are some unsupported\n"
2308 "options in the option files.\n"
2309 "This program can print errors, print the changed files, update the\n"
2310 "configuration files in place.\n"
2311 "When running with the options:\n"
2312 "--update --edit=last --error-group=mysqld-5.7\n"
2313 "The configuration files will be changed so that they should work\n"
2314 "with both MySQL 5.7 and MariaDB. You should provide the currently\n"
2315 "used MySQL version if --error-group is used.\n");
2316
2317 printf("Usage: %s [--print | --update] [OPTIONS] [OPTION_FILES]\n\n",
2318 my_progname);
2319 puts("If OPTION_FILES is not provided, all default MariaDB configuration\n"
2320 "files will be printed/upgraded");
2321
2322 my_print_default_files(config_file);
2323 puts("");
2324 my_print_help(my_long_options);
2325
2326 puts("");
2327 puts("If moving from MYSQL, the recommended usage is to use the following "
2328 "3 steps:");
2329 puts("");
2330 puts("- Check if there is anything that needs changing:");
2331 puts("mariadb-migrate-config-file --from=mysql");
2332 puts("");
2333 puts("- Check how the new configuration files would look like:");
2334 puts("mariadb-migrate-config-file --from=mysql --edit=last --print");
2335 puts("");
2336 puts("- Modify all configuration files keeping backups of the originals:");
2337 puts("mariadb-migrate-config-file --from=mysql --edit=last --update "
2338 "--backup --error-group=mysqld-5.7\n");
2339
2340 puts("Examples:");
2341 pe("", "Go through all my.cnf and included files and print not supported "
2342 "options. The groups checked are [server], [mysqld] and [mysqld-###]. "
2343 "Options valid for MariaDB in [mysqld-###] groups are copied to "
2344 "the [mariadbd] group");
2345 pe("--from=mariadb",
2346 "Go through all my.cnf and include files and print options not supported "
2347 "by the current MariaDB version. All MariaDB groups read by mariadbd are "
2348 "checked");
2349 pe("~/.my.cnf", "Print not supported options in ~/.my.cnf");
2350 pe("--defaults-file=/etc/my.cnf --verify=1 --mariadbd=#path-to-mariadbd#",
2351 "Go through /etc/my.cnf and print all not supported options. Check also "
2352 "the by running it through the MariaDB server");
2353 pe("--print", "Print the current content of all my.cnf and included files "
2354 "(no changes)");
2355 pe("--print --edit=last --update-paths",
2356 "As print, but change all paths from /mysql to /mariadbd. Useful to "
2357 "check the effect of --update-paths");
2358 pe("--print --edit=last",
2359 "Print modified version of all my.cnf and included files where supported "
2360 "options from [mysqld-###] groups are copied to a [mariadbd] group at "
2361 "the end of the file. Not supported options from [mysqld] and [server] "
2362 "are moved to [mysqld-5.7] (default value of 'error_group')");
2363 pe("--print --edit=inline --error-group=mysqld-8.0",
2364 "Print modified version of all my.cnf and included files where supported "
2365 "options from [mysqld-###] groups are copied inline to [mariadbd] "
2366 "groups. Not supported options in the [mysqld] and [server] groups are "
2367 "moved to [mysqld-8.0]");
2368 pe("--print --edit=remove",
2369 "Print modified version of all my.cnf and includedfiles where supported "
2370 "options from [mysqld-###] groups are copied to a [mariadbd] group. "
2371 "Not supported options from [mysqld] and [server] are removed");
2372 pe("--print --edit=comment",
2373 "As '--remove', but not supported options are commented");
2374 pe("--edit=update --edit=inline --backup",
2375 "Modify all my.cnf and included files. Supported options from "
2376 "[mysqld-###] groups are copied to [mariadbd] groups. Not supported "
2377 "options in the [mysqld] and [server] groups are moved to a "
2378 "[mysqld-5.7] group. "
2379 "The original of the changed config files are stored as backup files. "
2380 "If no option files are specified the mariadbd server will be started "
2381 "using them to verify that the new options files works with it");
2382 pe("--update --edit=inline --error_group=unknown --from=mariadb",
2383 "Move all unknown mariadbd options to the group [unknown]");
2384 pe("--fix-all",
2385 "Use all common options when converting from MySQL to MariaDB. "
2386 "See definition for --fix-all for more details");
2387
2388 my_print_variables(my_long_options);
2389 cleanup_and_exit(0);
2390 }
2391
2392
2393 static my_bool
2394 get_one_option(const struct my_option *opt __attribute__((unused)),
2395 const char *argument __attribute__((unused)),
2396 const char *filename __attribute__((unused)))
2397 {
2398 switch (opt->id) {
2399 case 'I':
2400 case '?':
2401 usage();
2402 case 'V':
2403 print_version();
2404 cleanup_and_exit(0);
2405 case OPT_FIX_ALL:
2406 opt_no_myisam_options= TRUE;
2407 opt_add_skip_slave_start= TRUE;
2408 opt_edit_mode= EDIT_MODE_LAST;
2409 opt_convert_from= FROM_MYSQL;
2410 opt_add_mariadb_replication_compatibility= 1;
2411 opt_add_mysql_replication_compatibility= 1;
2412 opt_add_client_server_group= 1;
2413 break;
2414 }
2415 return 0;
2416 }
2417
2418
2419 static int get_options(int *argc,char ***argv)
2420 {
2421 int ho_error;
2422
2423 if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
2424 cleanup_and_exit(ho_error);
2425
2426 if (opt_update && opt_edit_mode == EDIT_MODE_NONE)
2427 {
2428 fputs("error: --update requires the '--edit=xxx' option\n", stderr);
2429 cleanup_and_exit(1);
2430 }
2431 if ((!opt_error_group || !opt_unsupported_group) &&
2432 (opt_edit_mode == EDIT_MODE_INLINE ||
2433 opt_edit_mode == EDIT_MODE_LAST))
2434 {
2435 fputs("error: Selected --edit mode requires --error-group and "
2436 "--unsupported-group\n", stderr);
2437 cleanup_and_exit(1);
2438 }
2439 if (opt_print && opt_update)
2440 {
2441 fputs("error: --print and --update can't be specified simultaneously\n",
2442 stderr);
2443 cleanup_and_exit(1);
2444 }
2445 if (!opt_print && !opt_update && opt_edit_mode != EDIT_MODE_NONE &&
2446 !opt_silent)
2447 {
2448 puts("Note: edit_mode used without print/update. --print assumed");
2449 opt_print= 1;
2450 }
2451 if (opt_print && opt_edit_mode == EDIT_MODE_NONE && opt_update_paths)
2452 {
2453 puts("Note: --print --update-paths without edit mode set will not "
2454 "show updated paths");
2455 }
2456 if (opt_print)
2457 {
2458 opt_silent= 1; /* Don't print notes with --print */
2459 opt_mariadbd_testing= 0;
2460 }
2461 if (opt_update_paths && opt_convert_from != FROM_MYSQL)
2462 opt_update_paths= 0;
2463
2464 groups_to_use= (opt_convert_from == FROM_MYSQL ?
2465 mysqld_groups : mariadbd_groups);
2466 return 0;
2467 }
2468
2469
2470 int main(int argc, char **argv)
2471 {
2472 int count, error= 0, args_used;
2473 char *tmp_arguments[4];
2474 char **arguments, **org_argv;
2475 const char **default_directories;
2476 MY_INIT(argv[0]);
2477
2478 org_argv= argv;
2479 args_used= get_defaults_options(argv);
2480
2481 /* Copy defaults-xxx arguments & program name */
2482 count= args_used;
2483 arguments= tmp_arguments;
2484 memcpy((char*) arguments, (char*) org_argv, count*sizeof(*org_argv));
2485 arguments[count]= 0;
2486
2487 /*
2488 We already process --defaults* options at the beginning in
2489 get_defaults_options(). So skip --defaults* options and
2490 pass remaining options to handle_options().
2491 */
2492 org_argv+=args_used-1;
2493 argc-=args_used-1;
2494
2495 /* Check out the args */
2496 if (get_options(&argc, &org_argv))
2497 cleanup_and_exit(1);
2498
2499 if (argc)
2500 {
2501 give_error_for_missing_files= 1;
2502 /* We cannot test a list of provided option files */
2503 opt_mariadbd_testing= 0;
2504 for (; argc-- ; org_argv++)
2505 {
2506 count= args_used;
2507 arguments= tmp_arguments;
2508 if ((error= my_load_defaults(*org_argv, groups_to_use,
2509 &count, &arguments, 0)))
2510 goto handle_error;
2511 error= process_defaults(*org_argv, groups_to_use,
2512 0,
2513 (const char **)(argv + 1),
2514 args_used - 1);
2515 free_defaults(arguments);
2516 if (error)
2517 goto handle_error;
2518 }
2519 }
2520 else
2521 {
2522 /* Write 'add options' only to the first found config file */
2523 add_new_options_once= 1;
2524 if ((error= my_load_defaults(config_file, groups_to_use,
2525 &count, &arguments, &default_directories)))
2526 goto handle_error;
2527 /*
2528 The defaults-xxx arguments are still intact at the beginning of argv and
2529 can be passed to mariadbd for testing the updated configuration files.
2530 */
2531 error= process_defaults(config_file, groups_to_use,
2532 default_directories,
2533 (const char **)(argv + 1),
2534 args_used - 1);
2535 free_defaults(arguments);
2536 if (error)
2537 goto handle_error;
2538 }
2539
2540 report_line_changes();
2541
2542 if (!opt_silent && !opt_print)
2543 {
2544 if (global_update_count)
2545 {
2546 if (opt_update)
2547 fprintf(stdout, "%d issue/issues fixed\n", (int) global_update_count);
2548 else
2549 fprintf(stdout, "%d issue/issues found\n", (int) global_update_count);
2550 }
2551 else
2552 printf("No issues found\n");
2553 }
2554 cleanup_and_exit(error != 0 ||
2555 (!opt_update && !opt_print && global_update_count != 0) ?
2556 2 : 0);
2557
2558 handle_error:
2559 DBUG_ASSERT(error);
2560 if (error == 4) /* --print-defaults */
2561 {
2562 free_defaults(arguments);
2563 cleanup_and_exit(0);
2564 }
2565 cleanup_and_exit(2);
2566 }