diff options
author | shaju <shaju@google.com> | 2023-03-20 10:09:25 -0700 |
---|---|---|
committer | shaju <shaju@google.com> | 2023-03-21 17:36:30 -0700 |
commit | e1f98527827bd6477797d5659af31e0a50fc40df (patch) | |
tree | 45fa016b0d8090d07aaccb91343138ca7e5422b4 | |
parent | 59be29d99b8632b1f2e6f1eb7b9e23eb53ba34f5 (diff) | |
download | adb-e1f98527827bd6477797d5659af31e0a50fc40df.tar.gz |
Now reporting OS info for `adb --version`
Bug: 272830514
Test: Treehugger + cursory test(see below)
$ ./adb.exe --version
Android Debug Bridge version 1.0.41
<snip>
Running on Windows 10.0.19044
$ adb --version
Android Debug Bridge version 1.0.41
<snip>
Running on Darwin 22.3.0 (arm64)
$ adb --version
Android Debug Bridge version 1.0.41
<snip>
Running on Linux5.19.11-1<snip>-amd64 (x86_64)
Change-Id: I0d2ce48d9b690910efc44792cea244b3d5f8da8f
-rw-r--r-- | adb.cpp | 13 | ||||
-rw-r--r-- | sysdeps.h | 3 | ||||
-rw-r--r-- | sysdeps_unix.cpp | 17 | ||||
-rw-r--r-- | sysdeps_win32.cpp | 28 |
4 files changed, 55 insertions, 6 deletions
@@ -95,12 +95,13 @@ static void DecrementActiveConnections() { std::string adb_version() { // Don't change the format of this --- it's parsed by ddmlib. return android::base::StringPrintf( - "Android Debug Bridge version %d.%d.%d\n" - "Version %s-%s\n" - "Installed as %s\n", - ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION, - PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str(), - android::base::GetExecutablePath().c_str()); + "Android Debug Bridge version %d.%d.%d\n" + "Version %s-%s\n" + "Installed as %s\n" + "Running on %s\n", + ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION, PLATFORM_TOOLS_VERSION, + android::build::GetBuildNumber().c_str(), android::base::GetExecutablePath().c_str(), + GetOSVersion().c_str()); } uint32_t calculate_apacket_checksum(const apacket* p) { @@ -789,6 +789,9 @@ static inline void disable_tcp_nagle(borrowed_fd fd) { // configured to drop after 10 missed keepalives. Returns true on success. bool set_tcp_keepalive(borrowed_fd fd, int interval_sec); +// Returns a human-readable OS version string. +extern std::string GetOSVersion(void); + #if defined(_WIN32) // Win32 defines ERROR, which we don't need, but which conflicts with google3 logging. #undef ERROR diff --git a/sysdeps_unix.cpp b/sysdeps_unix.cpp index e5657067..66757cd9 100644 --- a/sysdeps_unix.cpp +++ b/sysdeps_unix.cpp @@ -16,6 +16,10 @@ #include "sysdeps.h" +#include <sys/utsname.h> + +#include <android-base/stringprintf.h> + bool set_tcp_keepalive(borrowed_fd fd, int interval_sec) { int enable = (interval_sec > 0); if (adb_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable))) { @@ -90,3 +94,16 @@ Process adb_launch_process(std::string_view executable, std::vector<std::string> } exit(execv(copies.front().data(), rawArgs.data())); } + +// For Unix variants (Linux, OSX), the underlying uname() system call +// is utilized to extract out a version string comprising of: +// 1.) "Linux" or "Darwin" +// 2.) OS system release (e.g. "5.19.11") +// 3.) machine (e.g. "x86_64") +// like: "Linux 5.19.11-1<snip>1-amd64 (x86_64)" +std::string GetOSVersion(void) { + utsname name; + uname(&name); + + return android::base::StringPrintf("%s %s (%s)", name.sysname, name.release, name.machine); +} diff --git a/sysdeps_win32.cpp b/sysdeps_win32.cpp index a43a3fc6..bbf3ee5f 100644 --- a/sysdeps_win32.cpp +++ b/sysdeps_win32.cpp @@ -2353,6 +2353,34 @@ int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) { /**************************************************************************/ /**************************************************************************/ /***** *****/ +/***** Versioning support *****/ +/***** *****/ +/**************************************************************************/ +/**************************************************************************/ +std::string GetOSVersion() { + // We also considered GetVersionInfoEx(), however the internal + // API below is preferable since Windows-8 (and above) returns + // the manifest windows data (as opposed to the actual version info). + typedef FARPROC(WINAPI * RtlGetVersionPtr)(PRTL_OSVERSIONINFOW); + RtlGetVersionPtr RtlGetVersionInternal = reinterpret_cast<RtlGetVersionPtr>( + GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetVersion")); + + if (!RtlGetVersionInternal) { + return "<Could not get handle to RtlGetVersion in ntdll.dll>"; + } + + OSVERSIONINFO version; + version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + + RtlGetVersionInternal(static_cast<PRTL_OSVERSIONINFOW>(&version)); + + return android::base::StringPrintf("Windows %lu.%lu.%lu", version.dwMajorVersion, + version.dwMinorVersion, version.dwBuildNumber); +} + +/**************************************************************************/ +/**************************************************************************/ +/***** *****/ /***** Unicode support *****/ /***** *****/ /**************************************************************************/ |