1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "base/system/sys_info.h"
#include <stddef.h>
#include <stdint.h>
#include <sys/utsname.h>
#include "base/compiler_specific.h"
#include "base/environment.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
namespace base {
const char kLsbReleaseKey[] = "LSB_RELEASE";
const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch
namespace {
const char* const kLinuxStandardBaseVersionKeys[] = {
"CHROMEOS_RELEASE_VERSION",
"GOOGLE_RELEASE",
"DISTRIB_RELEASE",
};
const char kChromeOsReleaseNameKey[] = "CHROMEOS_RELEASE_NAME";
const char* const kChromeOsReleaseNames[] = {
"Chrome OS",
"Chromium OS",
};
const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
const char kLsbReleaseSourceKey[] = "lsb-release";
const char kLsbReleaseSourceEnv[] = "env";
const char kLsbReleaseSourceFile[] = "file";
} // namespace
class ChromeOSVersionInfo {
public:
ChromeOSVersionInfo() {
std::string lsb_release, lsb_release_time_str;
std::unique_ptr<Environment> env(Environment::Create());
std::optional<std::string> lsb_release_var = env->GetVar(kLsbReleaseKey);
std::optional<std::string> lsb_release_time_var =
env->GetVar(kLsbReleaseTimeKey);
bool parsed_from_env =
lsb_release_var.has_value() && lsb_release_time_var.has_value();
if (parsed_from_env) {
lsb_release = std::move(lsb_release_var.value());
lsb_release_time_str = std::move(lsb_release_time_var.value());
double us = 0;
if (StringToDouble(lsb_release_time_str, &us)) {
lsb_release_time_ = Time::FromSecondsSinceUnixEpoch(us);
}
} else {
// If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not
// set, fall back to a blocking read of the lsb_release file. This should
// only happen in non Chrome OS environments.
ScopedAllowBlocking allow_blocking;
FilePath path(kLinuxStandardBaseReleaseFile);
ReadFileToString(path, &lsb_release);
File::Info fileinfo;
if (GetFileInfo(path, &fileinfo)) {
lsb_release_time_ = fileinfo.creation_time;
}
}
ParseLsbRelease(lsb_release);
// For debugging:
lsb_release_map_[kLsbReleaseSourceKey] =
parsed_from_env ? kLsbReleaseSourceEnv : kLsbReleaseSourceFile;
}
// The test-only instance should not parse the lsb-release file, because that
// file exists on the linux test bots, but contains irrelevant values.
enum ForTest { FOR_TEST };
explicit ChromeOSVersionInfo(ForTest for_test) {}
bool GetLsbReleaseValue(const std::string& key, std::string* value) {
LsbReleaseMap::const_iterator iter = lsb_release_map_.find(key);
if (iter == lsb_release_map_.end()) {
return false;
}
*value = iter->second;
return true;
}
void GetVersionNumbers(int32_t* major_version,
int32_t* minor_version,
int32_t* bugfix_version) {
*major_version = major_version_;
*minor_version = minor_version_;
*bugfix_version = bugfix_version_;
}
const Time& lsb_release_time() const LIFETIME_BOUND {
return lsb_release_time_;
}
void set_lsb_release_time(const Time& time) { lsb_release_time_ = time; }
bool is_running_on_chromeos() const { return is_running_on_chromeos_; }
void ParseLsbRelease(const std::string& lsb_release) {
// Parse and cache lsb_release key pairs. There should only be a handful
// of entries so the overhead for this will be small, and it can be
// useful for debugging.
base::StringPairs pairs;
SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
for (size_t i = 0; i < pairs.size(); ++i) {
std::string key, value;
TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value);
if (key.empty()) {
continue;
}
lsb_release_map_[key] = value;
}
// Parse the version from the first matching recognized version key.
std::string version;
for (size_t i = 0; i < std::size(kLinuxStandardBaseVersionKeys); ++i) {
std::string key = kLinuxStandardBaseVersionKeys[i];
if (GetLsbReleaseValue(key, &version) && !version.empty()) {
break;
}
}
StringTokenizer tokenizer(version, ".");
if (tokenizer.GetNext()) {
StringToInt(tokenizer.token_piece(), &major_version_);
}
if (tokenizer.GetNext()) {
StringToInt(tokenizer.token_piece(), &minor_version_);
}
if (tokenizer.GetNext()) {
StringToInt(tokenizer.token_piece(), &bugfix_version_);
}
// Check release name for Chrome OS.
std::string release_name;
if (GetLsbReleaseValue(kChromeOsReleaseNameKey, &release_name)) {
for (size_t i = 0; i < std::size(kChromeOsReleaseNames); ++i) {
if (release_name == kChromeOsReleaseNames[i]) {
is_running_on_chromeos_ = true;
break;
}
}
}
}
private:
using LsbReleaseMap = std::map<std::string, std::string>;
Time lsb_release_time_;
LsbReleaseMap lsb_release_map_;
int32_t major_version_ = 0;
int32_t minor_version_ = 0;
int32_t bugfix_version_ = 0;
bool is_running_on_chromeos_ = false;
};
ChromeOSVersionInfo* g_chromeos_version_info_for_test = nullptr;
ChromeOSVersionInfo& GetChromeOSVersionInfo() {
// ChromeOSVersionInfo only stores the parsed lsb-release values, not the full
// contents of the lsb-release file. Therefore, use a second instance for
// overrides in tests so we can cleanly restore the original lsb-release.
if (g_chromeos_version_info_for_test) {
return *g_chromeos_version_info_for_test;
}
static base::NoDestructor<ChromeOSVersionInfo> version_info;
return *version_info;
}
// static
std::string SysInfo::HardwareModelName() {
std::string board = GetLsbReleaseBoard();
if (board == "unknown") {
return "";
}
// GetLsbReleaseBoard() may be suffixed with a "-signed-" and other extra
// info. Strip it.
const size_t index = board.find("-signed-");
if (index != std::string::npos) {
board.resize(index);
}
return base::ToUpperASCII(board);
}
// static
void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
int32_t* minor_version,
int32_t* bugfix_version) {
return GetChromeOSVersionInfo().GetVersionNumbers(
major_version, minor_version, bugfix_version);
}
// static
std::string SysInfo::OperatingSystemVersion() {
int32_t major, minor, bugfix;
GetChromeOSVersionInfo().GetVersionNumbers(&major, &minor, &bugfix);
return base::StringPrintf("%d.%d.%d", major, minor, bugfix);
}
// static
std::string SysInfo::KernelVersion() {
struct utsname info;
if (uname(&info) < 0) {
NOTREACHED();
}
return std::string(info.release);
}
// static
bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) {
return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value);
}
// static
std::string SysInfo::GetLsbReleaseBoard() {
const char kMachineInfoBoard[] = "CHROMEOS_RELEASE_BOARD";
std::string board;
if (!GetLsbReleaseValue(kMachineInfoBoard, &board)) {
board = "unknown";
}
return board;
}
// static
Time SysInfo::GetLsbReleaseTime() {
return GetChromeOSVersionInfo().lsb_release_time();
}
// static
bool SysInfo::IsRunningOnChromeOS() {
return GetChromeOSVersionInfo().is_running_on_chromeos();
}
// static
void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
const Time& lsb_release_time) {
DCHECK(!g_chromeos_version_info_for_test) << "Nesting is not allowed";
g_chromeos_version_info_for_test =
new ChromeOSVersionInfo(ChromeOSVersionInfo::FOR_TEST);
g_chromeos_version_info_for_test->ParseLsbRelease(lsb_release);
g_chromeos_version_info_for_test->set_lsb_release_time(lsb_release_time);
}
// static
void SysInfo::ResetChromeOSVersionInfoForTest() {
DCHECK(g_chromeos_version_info_for_test);
delete g_chromeos_version_info_for_test;
g_chromeos_version_info_for_test = nullptr;
}
// static
void SysInfo::CrashIfChromeOSNonTestImage() {
if (!IsRunningOnChromeOS()) {
return;
}
// On the test images etc/lsb-release has a line:
// CHROMEOS_RELEASE_TRACK=testimage-channel.
const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK";
const char kTestImageRelease[] = "testimage-channel";
std::string track;
CHECK(SysInfo::GetLsbReleaseValue(kChromeOSReleaseTrack, &track));
// Crash if can't find test-image marker in the release track.
CHECK_NE(track.find(kTestImageRelease), std::string::npos);
}
SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() {
HardwareInfo info;
// Manufacturer of ChromeOS device is always Google so hardcode it.
info.manufacturer = "Google";
if (IsRunningOnChromeOS()) {
// Read the model name from cros-configfs.
constexpr char kModelNamePath[] = "/run/chromeos-config/v1/name";
constexpr size_t kMaxStringSize = 100u;
std::string data;
if (ReadFileToStringWithMaxSize(FilePath(kModelNamePath), &data,
kMaxStringSize)) {
TrimWhitespaceASCII(data, TrimPositions::TRIM_ALL, &info.model);
}
DCHECK(IsStringUTF8(info.model));
} else {
// Fake model name on chromeos linux-emulator (for both linux/ash).
info.model = "linux-emulator";
}
return info;
}
} // namespace base
|