Add shared server section lookup

https://github.com/webmin/webmin/pull/2827#pullrequestreview-5047577760
This commit is contained in:
Ilia Ross
2026-08-28 14:46:07 +02:00
parent 4e6ba5229e
commit 7f237871ba
6 changed files with 34 additions and 20 deletions

View File

@@ -14,11 +14,7 @@ if (!$conf) {
&ui_print_footer("", $text{'index_return'});
exit;
}
# Prefer the main server section over generic MariaDB sections that can
# belong to a plugin-specific include file
($mysqld) = grep { $_->{'name'} eq 'mysqld' } @$conf;
($mysqld) = grep { $_->{'name'} eq 'mariadbd' } @$conf if (!$mysqld);
($mysqld) = grep { $_->{'name'} eq 'mariadb' } @$conf if (!$mysqld);
$mysqld = &get_mysqld_config_section($conf);
$mysqld || &error($text{'cnf_emysqld'});
$mems = $mysqld->{'members'};
$local = &is_mysql_local();

View File

@@ -13,11 +13,7 @@ if (!$conf) {
&ui_print_footer("", $text{'index_return'});
exit;
}
# Prefer the main server section over generic MariaDB sections that can
# belong to a plugin-specific include file
($mysqld) = grep { $_->{'name'} eq 'mysqld' } @$conf;
($mysqld) = grep { $_->{'name'} eq 'mariadbd' } @$conf if (!$mysqld);
($mysqld) = grep { $_->{'name'} eq 'mariadb' } @$conf if (!$mysqld);
$mysqld = &get_mysqld_config_section($conf);
$mysqld || &error($text{'cnf_emysqld'});
$mems = $mysqld->{'members'};

View File

@@ -1358,6 +1358,19 @@ if (!scalar(@mysql_config_cache)) {
return \@mysql_config_cache;
}
# get_mysqld_config_section(&config)
# Returns the preferred server section from a parsed MySQL configuration,
# avoiding generic MariaDB sections when a dedicated server section exists
sub get_mysqld_config_section
{
my ($conf) = @_;
foreach my $name ('mysqld', 'mariadbd', 'mariadb') {
my ($section) = grep { $_->{'name'} eq $name } @$conf;
return $section if ($section);
}
return undef;
}
# parse_mysql_config(file)
# Reads one MySQL config file
sub parse_mysql_config

View File

@@ -12,11 +12,7 @@ foreach my $l (&get_all_mysqld_files()) {
&lock_file($l);
}
$conf = &get_mysql_config();
# Prefer the main server section over generic MariaDB sections that can
# belong to a plugin-specific include file
($mysqld) = grep { $_->{'name'} eq 'mysqld' } @$conf;
($mysqld) = grep { $_->{'name'} eq 'mariadbd' } @$conf if (!$mysqld);
($mysqld) = grep { $_->{'name'} eq 'mariadb' } @$conf if (!$mysqld);
$mysqld = &get_mysqld_config_section($conf);
$mysqld || &error($text{'cnf_emysqld'});
$mems = $mysqld->{'members'};

View File

@@ -11,11 +11,7 @@ foreach my $l (&get_all_mysqld_files()) {
&lock_file($l);
}
$conf = &get_mysql_config();
# Prefer the main server section over generic MariaDB sections that can
# belong to a plugin-specific include file
($mysqld) = grep { $_->{'name'} eq 'mysqld' } @$conf;
($mysqld) = grep { $_->{'name'} eq 'mariadbd' } @$conf if (!$mysqld);
($mysqld) = grep { $_->{'name'} eq 'mariadb' } @$conf if (!$mysqld);
$mysqld = &get_mysqld_config_section($conf);
$mysqld || &error($text{'cnf_emysqld'});
$mems = $mysqld->{'members'};

View File

@@ -153,4 +153,21 @@ subtest 'retention maximum is variant specific' => sub {
'the MariaDB maximum displays as 99 days');
};
subtest 'preferred MySQL server configuration section' => sub {
my $mariadb = { 'name' => 'mariadb' };
my $mariadbd = { 'name' => 'mariadbd' };
my $mysqld = { 'name' => 'mysqld' };
my $client = { 'name' => 'client' };
is(main::get_mysqld_config_section(
[ $mariadb, $mariadbd, $mysqld ]), $mysqld,
'mysqld is preferred regardless of file order');
is(main::get_mysqld_config_section([ $mariadb, $mariadbd ]), $mariadbd,
'mariadbd is preferred when mysqld is absent');
is(main::get_mysqld_config_section([ $client, $mariadb ]), $mariadb,
'mariadb is used as the final server-section fallback');
ok(!defined(main::get_mysqld_config_section([ $client ])),
'no server section returns undef');
};
done_testing();