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;
#if !defined(__APPLE__)
oss << " GNU/Linux";
#endif
oss << '\n';
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