Skip to content
/ server Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions mysql-test/suite/feedback/t/feedback_os_release.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--echo # MDEV-39126: Feedback plugin to use /etc/os-release
--source include/not_embedded.inc

# 1. Create the skip file unconditionally, using the MDEV prefix
perl;
my $dir = $ENV{'MYSQLTEST_VARDIR'};
my $skip_file = "$dir/tmp/mdev39126_skip_test.inc";
open(my $fh, '>', $skip_file) or die "Could not open $skip_file: $!";

if (! -f "/etc/os-release") {
print $fh "skip No /etc/os-release found;\n";
}
close($fh);
EOF

# Source the file and then clean it up immediately
--source $MYSQLTEST_VARDIR/tmp/mdev39126_skip_test.inc
--remove_file $MYSQLTEST_VARDIR/tmp/mdev39126_skip_test.inc

# Get the value from the plugin into an mtr variable
let $os_val = `SELECT variable_value FROM information_schema.feedback WHERE variable_name = 'os'`;

# Export the mtr variable to the environment so Perl can read it
let $ENV{'os_val'} = $os_val;

# 2. Compare the value directly in Perl to avoid creating a second file
perl;
my $os_val = $ENV{'os_val'};
my $expected = "";

if (open(my $fh, '<', '/etc/os-release')) {
while (my $line = <$fh>) {
chomp $line;
if ($line =~ /^PRETTY_NAME="?(.*?)"?$/) {
$expected = $1;
last;
}
}
close($fh);
}

# Print the result so it gets recorded in the .result file
if ($os_val eq $expected) {
print "os matches PRETTY_NAME\n";
} else {
print "Mismatch! Got: '$os_val', Expected: '$expected'\n";
}
EOF

--echo # End of 10.11 tests
61 changes: 47 additions & 14 deletions plugin/feedback/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,27 +266,59 @@ int prepare_linux_info()

#ifdef TARGET_OS_LINUX
/*
let's try to find what linux distribution it is
we read *[-_]{release,version} file in /etc.
Let's try to find what Linux distribution it is.
We first check for the modern and portable /etc/os-release:

Either it will be /etc/lsb-release, such as
==> /etc/os-release <==
PRETTY_NAME="Ubuntu 22.04.2 LTS"

If not found, we fall back to /etc/lsb-release:

==> /etc/lsb-release <==
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.04
DISTRIB_CODENAME=hardy
DISTRIB_DESCRIPTION="Ubuntu 8.04.4 LTS"

Or a one-liner with the description (/etc/SuSE-release has more
than one line, but the description is the first, so it can be
treated as a one-liner).
Or a one-liner with the description (/etc/SuSE-release has more
than one line, but the description is the first, so it can be
treated as a one-liner).

We'll read lsb-release first, and if it's not found will search
for other files (*-version *-release *_version *_release)
*/
We'll read os-release first, then lsb-release, and if neither is found
we will search for other files (*-version *-release *_version *_release).
*/
int fd;
have_distribution= false;
if ((fd= my_open("/etc/lsb-release", O_RDONLY, MYF(0))) != -1)

/* 1. First try the modern portable way: /etc/os-release */
if ((fd= my_open("/etc/os-release", O_RDONLY, MYF(0))) != -1)
{
size_t len= my_read(fd, (uchar*)distribution, sizeof(distribution)-1, MYF(0));
my_close(fd, MYF(0));
if (len != (size_t)-1)
{
distribution[len]= 0; // safety
char *found= strstr(distribution, "PRETTY_NAME=");
if (found)
{
have_distribution= true;
char *end= strstr(found, "\n");
if (end == NULL)
end= distribution + len;
found+= 12; // Length of "PRETTY_NAME="

if (*found == '"' && end[-1] == '"')
{
found++;
end--;
}
*end= 0;

char *to= strmov(distribution, "os-release: ");
memmove(to, found, end - found + 1);
}
}
}

/* 2. Fallback to older /etc/lsb-release if os-release is not found */
if (!have_distribution && (fd= my_open("/etc/lsb-release", O_RDONLY, MYF(0))) != -1)
{
/* Cool, LSB-compliant distribution! */
size_t len= my_read(fd, (uchar*)distribution, sizeof(distribution)-1, MYF(0));
Expand All @@ -301,7 +333,7 @@ int prepare_linux_info()
char *end= strstr(found, "\n");
if (end == NULL)
end= distribution + len;
found+= 20;
found+= 20; // Length of "DISTRIB_DESCRIPTION="

if (*found == '"' && end[-1] == '"')
{
Expand All @@ -316,6 +348,7 @@ int prepare_linux_info()
}
}


/* if not an LSB-compliant distribution */
for (uint i= 0; !have_distribution && i < array_elements(masks); i++)
{
Expand Down