Skip to content
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
20 changes: 18 additions & 2 deletions src/bvar/default_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <unistd.h> // getpagesize
#include <sys/types.h>
#include <sys/resource.h> // getrusage
#include <sys/utsname.h> // uname
#include <dirent.h> // dirent
#include <iomanip> // setw
#include <stdio.h>
Expand Down Expand Up @@ -617,11 +618,26 @@ static void get_cmdline(std::ostream& os, void*) {
struct ReadVersion {
std::string content;
ReadVersion() {
std::ostringstream oss;
if (butil::read_command_output(oss, "uname -ap") != 0) {
struct utsname buf;
if (uname(&buf) != 0) {
LOG(ERROR) << "Fail to read kernel version";
return;
}
#if defined(__APPLE__) && (defined(__aarch64__) || defined(__arm64__))
const char* processor = "arm";
#elif defined(__APPLE__) && defined(__x86_64__)
const char* processor = "i386";
#else
const char* processor = buf.machine;
#endif
std::ostringstream oss;
oss << buf.sysname << ' ' << buf.nodename << ' '
<< buf.release << ' ' << buf.version << ' '
<< buf.machine << ' ' << processor;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reconstructed output is not byte-equivalent to uname -ap as stated in the PR. On Linux, uname -ap appends the operating-system identifier as the final field, e.g. ... aarch64 GNU/Linux; this new code stops at machine processor and drops that OS field entirely. Additionally, uname(1) omits a field whose value is the literal string "unknown" (common for uname -p), whereas this code always appends processor (here equal to buf.machine, producing a duplicated machine value).

If exact parity with the previous output is required (e.g. for scrapers/keying off the string), either append the proper OS name field (GNU/Linux on Linux; none on macOS) and mirror uname's handling of "unknown", or update the PR description to not claim the output format is preserved. Since these fields are informational this may be acceptable, but the claim and the format should match.


🤖 This reply was automatically generated by brpc-oncall

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'GNU/Linux' has been added here. Please take a look at the following four lines of code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wwbmmm Please check the latest code,thanks

#if !defined(__APPLE__)
oss << " GNU/Linux";
#endif
oss << '\n';

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wwbmmm here

content.append(oss.str());
}
};
Expand Down
45 changes: 44 additions & 1 deletion test/bvar_variable_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

#include <pthread.h> // pthread_*
#include <unistd.h> // usleep

#include <sys/utsname.h> // uname
#include <string.h> // strlen
#include <cstddef>
#include <memory>
#include <thread>
Expand Down Expand Up @@ -462,6 +463,48 @@ TEST_F(VariableTest, dtor_waits_for_inflight_describe) {

ASSERT_TRUE(destructed.load());
}

TEST_F(VariableTest, uname_returns_valid_kernel_info) {
struct utsname buf;
ASSERT_EQ(0, uname(&buf));

// Each field should be non-empty
ASSERT_GT(strlen(buf.sysname), 0u);
ASSERT_GT(strlen(buf.nodename), 0u);
ASSERT_GT(strlen(buf.release), 0u);
ASSERT_GT(strlen(buf.version), 0u);
ASSERT_GT(strlen(buf.machine), 0u);

// Build the string the same way ReadVersion does in default_variables.cpp
#if defined(__APPLE__) && (defined(__aarch64__) || defined(__arm64__))
const char* processor = "arm";
#elif defined(__APPLE__) && defined(__x86_64__)
const char* processor = "i386";
#else
const char* processor = buf.machine;
#endif
std::ostringstream oss;
oss << buf.sysname << ' ' << buf.nodename << ' '
<< buf.release << ' ' << buf.version << ' '
<< buf.machine << ' ' << processor;
#if !defined(__APPLE__)
oss << " GNU/Linux";
#endif
oss << '\n';
std::string content = oss.str();
Comment on lines +467 to +494

// The result should contain all key fields
ASSERT_NE(content.find(buf.sysname), std::string::npos);
ASSERT_NE(content.find(buf.release), std::string::npos);
ASSERT_NE(content.find(buf.machine), std::string::npos);

// On Linux, sysname should be "Linux"; on macOS, "Darwin"
#if defined(__linux__)
ASSERT_STREQ(buf.sysname, "Linux");
#elif defined(__APPLE__)
ASSERT_STREQ(buf.sysname, "Darwin");
#endif
}
} // namespace

int main(int argc, char** argv) {
Expand Down
Loading