From 7b6d6786848c0d62609c228c9751801b09db6476 Mon Sep 17 00:00:00 2001 From: weimeng Date: Thu, 3 Sep 2026 21:26:18 +0800 Subject: [PATCH 1/5] Replace popen('uname -ap') with uname() syscall in bvar kernel_version --- src/bvar/default_variables.cpp | 9 +++++++-- test/bvar_variable_unittest.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/bvar/default_variables.cpp b/src/bvar/default_variables.cpp index db74db11f5..a7e003efd5 100644 --- a/src/bvar/default_variables.cpp +++ b/src/bvar/default_variables.cpp @@ -20,6 +20,7 @@ #include // getpagesize #include #include // getrusage +#include // uname #include // dirent #include // setw #include @@ -617,11 +618,15 @@ 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; } + std::ostringstream oss; + oss << buf.sysname << ' ' << buf.nodename << ' ' + << buf.release << ' ' << buf.version << ' ' + << buf.machine << ' ' << buf.machine; content.append(oss.str()); } }; diff --git a/test/bvar_variable_unittest.cpp b/test/bvar_variable_unittest.cpp index f2a3edb777..b9e2be0b34 100644 --- a/test/bvar_variable_unittest.cpp +++ b/test/bvar_variable_unittest.cpp @@ -19,6 +19,7 @@ #include // pthread_* #include // usleep +#include // uname #include #include @@ -462,6 +463,37 @@ 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 + std::ostringstream oss; + oss << buf.sysname << ' ' << buf.nodename << ' ' + << buf.release << ' ' << buf.version << ' ' + << buf.machine << ' ' << buf.machine; + std::string content = oss.str(); + + // 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) { From 3c8355552930901b141c187e4de7dfaa6fc0dbe8 Mon Sep 17 00:00:00 2001 From: weimeng Date: Sat, 5 Sep 2026 20:43:27 +0800 Subject: [PATCH 2/5] Update processor variable assignment for Apple platforms Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/bvar/default_variables.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bvar/default_variables.cpp b/src/bvar/default_variables.cpp index a7e003efd5..2f75e4743c 100644 --- a/src/bvar/default_variables.cpp +++ b/src/bvar/default_variables.cpp @@ -623,10 +623,17 @@ struct ReadVersion { 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 << ' ' << buf.machine; + << buf.machine << ' ' << processor << '\n'; content.append(oss.str()); } }; From 48776599d42fba24791aee5d064001aaaa08b1d1 Mon Sep 17 00:00:00 2001 From: weimeng Date: Sat, 5 Sep 2026 21:18:52 +0800 Subject: [PATCH 3/5] update unittest add --- test/bvar_variable_unittest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bvar_variable_unittest.cpp b/test/bvar_variable_unittest.cpp index b9e2be0b34..75ec3891ed 100644 --- a/test/bvar_variable_unittest.cpp +++ b/test/bvar_variable_unittest.cpp @@ -20,7 +20,7 @@ #include // pthread_* #include // usleep #include // uname - +#include // strlen #include #include #include From 4f67f879f7c77c32e81f7c43f9b2fec41a7a55b6 Mon Sep 17 00:00:00 2001 From: weimeng Date: Sat, 5 Sep 2026 21:21:30 +0800 Subject: [PATCH 4/5] update unittest for bvar_variable kernel_version --- test/bvar_variable_unittest.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/bvar_variable_unittest.cpp b/test/bvar_variable_unittest.cpp index 75ec3891ed..958253c5bd 100644 --- a/test/bvar_variable_unittest.cpp +++ b/test/bvar_variable_unittest.cpp @@ -476,10 +476,17 @@ TEST_F(VariableTest, uname_returns_valid_kernel_info) { 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 << ' ' << buf.machine; + << buf.machine << ' ' << processor << '\n'; std::string content = oss.str(); // The result should contain all key fields From 432abde21c5c59bfa5bc5660f91af28005b5e701 Mon Sep 17 00:00:00 2001 From: weimeng Date: Sat, 5 Sep 2026 21:45:31 +0800 Subject: [PATCH 5/5] Fix kernel_version to match uname -ap output format across platforms --- src/bvar/default_variables.cpp | 6 +++++- test/bvar_variable_unittest.cpp | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/bvar/default_variables.cpp b/src/bvar/default_variables.cpp index 2f75e4743c..a32273a5d0 100644 --- a/src/bvar/default_variables.cpp +++ b/src/bvar/default_variables.cpp @@ -633,7 +633,11 @@ struct ReadVersion { std::ostringstream oss; oss << buf.sysname << ' ' << buf.nodename << ' ' << buf.release << ' ' << buf.version << ' ' - << buf.machine << ' ' << processor << '\n'; + << buf.machine << ' ' << processor; +#if !defined(__APPLE__) + oss << " GNU/Linux"; +#endif + oss << '\n'; content.append(oss.str()); } }; diff --git a/test/bvar_variable_unittest.cpp b/test/bvar_variable_unittest.cpp index 958253c5bd..d29e50c8c5 100644 --- a/test/bvar_variable_unittest.cpp +++ b/test/bvar_variable_unittest.cpp @@ -486,7 +486,11 @@ TEST_F(VariableTest, uname_returns_valid_kernel_info) { std::ostringstream oss; oss << buf.sysname << ' ' << buf.nodename << ' ' << buf.release << ' ' << buf.version << ' ' - << buf.machine << ' ' << processor << '\n'; + << buf.machine << ' ' << processor; +#if !defined(__APPLE__) + oss << " GNU/Linux"; +#endif + oss << '\n'; std::string content = oss.str(); // The result should contain all key fields