blob: b6fd088d44bfa43ff0be9be1b3d11807b327073a [file] [log] [blame]
[email protected]af25b472012-04-25 01:59:441// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]61b8ad72009-07-22 00:35:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Sebastien Marchand75a7cdf2018-11-13 23:47:035#include "base/system/sys_info.h"
[email protected]61b8ad72009-07-22 00:35:186
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8#include <stdint.h>
Jonghyun Ahnc54f59f2018-07-19 17:41:509#include <sys/utsname.h>
avi9b6f42932015-12-26 22:15:1410
[email protected]a0a1c5a2013-09-26 21:51:2311#include "base/environment.h"
[email protected]54124ed02014-01-07 10:06:5812#include "base/files/file.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
[email protected]e3177dd52014-08-13 20:22:1414#include "base/files/file_util.h"
[email protected]8c32ac12011-04-05 11:31:4215#include "base/lazy_instance.h"
avi9b6f42932015-12-26 22:15:1416#include "base/macros.h"
[email protected]dfa049e2013-02-07 02:57:2217#include "base/strings/string_number_conversions.h"
[email protected]eb62f7262013-03-30 14:29:0018#include "base/strings/string_piece.h"
[email protected]a0a1c5a2013-09-26 21:51:2319#include "base/strings/string_split.h"
[email protected]f4ebe772013-02-02 00:21:3920#include "base/strings/string_tokenizer.h"
[email protected]a0a1c5a2013-09-26 21:51:2321#include "base/strings/string_util.h"
Jonghyun Ahnc54f59f2018-07-19 17:41:5022#include "base/strings/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0623#include "base/threading/thread_restrictions.h"
[email protected]61b8ad72009-07-22 00:35:1824
25namespace base {
26
[email protected]a0a1c5a2013-09-26 21:51:2327namespace {
28
thestig073d514d2014-10-21 03:11:2129const char* const kLinuxStandardBaseVersionKeys[] = {
Sebastien Marchand75a7cdf2018-11-13 23:47:0330 "CHROMEOS_RELEASE_VERSION", "GOOGLE_RELEASE", "DISTRIB_RELEASE",
[email protected]8c32ac12011-04-05 11:31:4231};
[email protected]49c4cf852013-09-27 19:28:2432
33const char kChromeOsReleaseNameKey[] = "CHROMEOS_RELEASE_NAME";
34
35const char* const kChromeOsReleaseNames[] = {
Sebastien Marchand75a7cdf2018-11-13 23:47:0336 "Chrome OS", "Chromium OS",
[email protected]49c4cf852013-09-27 19:28:2437};
[email protected]61b8ad72009-07-22 00:35:1838
39const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
40
[email protected]a0a1c5a2013-09-26 21:51:2341const char kLsbReleaseKey[] = "LSB_RELEASE";
42const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch
43
44const char kLsbReleaseSourceKey[] = "lsb-release";
45const char kLsbReleaseSourceEnv[] = "env";
46const char kLsbReleaseSourceFile[] = "file";
47
48class ChromeOSVersionInfo {
49 public:
Sebastien Marchand75a7cdf2018-11-13 23:47:0350 ChromeOSVersionInfo() { Parse(); }
[email protected]8c32ac12011-04-05 11:31:4251
[email protected]a0a1c5a2013-09-26 21:51:2352 void Parse() {
53 lsb_release_map_.clear();
54 major_version_ = 0;
55 minor_version_ = 0;
56 bugfix_version_ = 0;
[email protected]49c4cf852013-09-27 19:28:2457 is_running_on_chromeos_ = false;
[email protected]a0a1c5a2013-09-26 21:51:2358
59 std::string lsb_release, lsb_release_time_str;
dcheng093de9b2016-04-04 21:25:5160 std::unique_ptr<Environment> env(Environment::Create());
[email protected]a0a1c5a2013-09-26 21:51:2361 bool parsed_from_env =
62 env->GetVar(kLsbReleaseKey, &lsb_release) &&
63 env->GetVar(kLsbReleaseTimeKey, &lsb_release_time_str);
64 if (parsed_from_env) {
65 double us = 0;
66 if (StringToDouble(lsb_release_time_str, &us))
[email protected]9eae4e62013-12-04 20:56:4967 lsb_release_time_ = Time::FromDoubleT(us);
[email protected]a0a1c5a2013-09-26 21:51:2368 } else {
69 // If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not
70 // set, fall back to a blocking read of the lsb_release file. This should
71 // only happen in non Chrome OS environments.
72 ThreadRestrictions::ScopedAllowIO allow_io;
73 FilePath path(kLinuxStandardBaseReleaseFile);
74 ReadFileToString(path, &lsb_release);
[email protected]54124ed02014-01-07 10:06:5875 File::Info fileinfo;
[email protected]9eae4e62013-12-04 20:56:4976 if (GetFileInfo(path, &fileinfo))
[email protected]a0a1c5a2013-09-26 21:51:2377 lsb_release_time_ = fileinfo.creation_time;
78 }
79 ParseLsbRelease(lsb_release);
80 // For debugging:
81 lsb_release_map_[kLsbReleaseSourceKey] =
82 parsed_from_env ? kLsbReleaseSourceEnv : kLsbReleaseSourceFile;
83 }
84
85 bool GetLsbReleaseValue(const std::string& key, std::string* value) {
Kartik Hegdedcbbb2fd2018-09-26 20:13:2386 LsbReleaseMap::const_iterator iter = lsb_release_map_.find(key);
[email protected]a0a1c5a2013-09-26 21:51:2387 if (iter == lsb_release_map_.end())
88 return false;
89 *value = iter->second;
90 return true;
91 }
92
avidd4e614352015-12-09 00:44:4993 void GetVersionNumbers(int32_t* major_version,
94 int32_t* minor_version,
95 int32_t* bugfix_version) {
[email protected]a0a1c5a2013-09-26 21:51:2396 *major_version = major_version_;
97 *minor_version = minor_version_;
98 *bugfix_version = bugfix_version_;
99 }
100
[email protected]9eae4e62013-12-04 20:56:49101 const Time& lsb_release_time() const { return lsb_release_time_; }
[email protected]49c4cf852013-09-27 19:28:24102 bool is_running_on_chromeos() const { return is_running_on_chromeos_; }
[email protected]a0a1c5a2013-09-26 21:51:23103
104 private:
105 void ParseLsbRelease(const std::string& lsb_release) {
106 // Parse and cache lsb_release key pairs. There should only be a handful
107 // of entries so the overhead for this will be small, and it can be
108 // useful for debugging.
patrikackland484fbc42015-04-27 21:29:45109 base::StringPairs pairs;
[email protected]9eae4e62013-12-04 20:56:49110 SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
[email protected]a0a1c5a2013-09-26 21:51:23111 for (size_t i = 0; i < pairs.size(); ++i) {
112 std::string key, value;
113 TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
114 TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value);
115 if (key.empty())
116 continue;
117 lsb_release_map_[key] = value;
118 }
119 // Parse the version from the first matching recognized version key.
120 std::string version;
[email protected]49c4cf852013-09-27 19:28:24121 for (size_t i = 0; i < arraysize(kLinuxStandardBaseVersionKeys); ++i) {
[email protected]a0a1c5a2013-09-26 21:51:23122 std::string key = kLinuxStandardBaseVersionKeys[i];
123 if (GetLsbReleaseValue(key, &version) && !version.empty())
124 break;
125 }
126 StringTokenizer tokenizer(version, ".");
127 if (tokenizer.GetNext()) {
David Benjamin001b02f2018-05-08 02:13:51128 StringToInt(tokenizer.token_piece(), &major_version_);
[email protected]a0a1c5a2013-09-26 21:51:23129 }
130 if (tokenizer.GetNext()) {
David Benjamin001b02f2018-05-08 02:13:51131 StringToInt(tokenizer.token_piece(), &minor_version_);
[email protected]a0a1c5a2013-09-26 21:51:23132 }
133 if (tokenizer.GetNext()) {
David Benjamin001b02f2018-05-08 02:13:51134 StringToInt(tokenizer.token_piece(), &bugfix_version_);
[email protected]a0a1c5a2013-09-26 21:51:23135 }
[email protected]49c4cf852013-09-27 19:28:24136
137 // Check release name for Chrome OS.
138 std::string release_name;
139 if (GetLsbReleaseValue(kChromeOsReleaseNameKey, &release_name)) {
140 for (size_t i = 0; i < arraysize(kChromeOsReleaseNames); ++i) {
141 if (release_name == kChromeOsReleaseNames[i]) {
142 is_running_on_chromeos_ = true;
143 break;
144 }
145 }
146 }
[email protected]a0a1c5a2013-09-26 21:51:23147 }
148
Kartik Hegdedcbbb2fd2018-09-26 20:13:23149 using LsbReleaseMap = std::map<std::string, std::string>;
[email protected]9eae4e62013-12-04 20:56:49150 Time lsb_release_time_;
Kartik Hegdedcbbb2fd2018-09-26 20:13:23151 LsbReleaseMap lsb_release_map_;
avidd4e614352015-12-09 00:44:49152 int32_t major_version_;
153 int32_t minor_version_;
154 int32_t bugfix_version_;
[email protected]49c4cf852013-09-27 19:28:24155 bool is_running_on_chromeos_;
[email protected]8c32ac12011-04-05 11:31:42156};
157
Sebastien Marchand75a7cdf2018-11-13 23:47:03158static LazyInstance<ChromeOSVersionInfo>::Leaky g_chrome_os_version_info =
159 LAZY_INSTANCE_INITIALIZER;
[email protected]a0a1c5a2013-09-26 21:51:23160
161ChromeOSVersionInfo& GetChromeOSVersionInfo() {
162 return g_chrome_os_version_info.Get();
163}
164
165} // namespace
[email protected]8c32ac12011-04-05 11:31:42166
[email protected]61b8ad72009-07-22 00:35:18167// static
avidd4e614352015-12-09 00:44:49168void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
169 int32_t* minor_version,
170 int32_t* bugfix_version) {
[email protected]a0a1c5a2013-09-26 21:51:23171 return GetChromeOSVersionInfo().GetVersionNumbers(
172 major_version, minor_version, bugfix_version);
[email protected]61b8ad72009-07-22 00:35:18173}
174
175// static
Jonghyun Ahnc54f59f2018-07-19 17:41:50176std::string SysInfo::OperatingSystemVersion() {
177 int32_t major, minor, bugfix;
178 GetChromeOSVersionInfo().GetVersionNumbers(&major, &minor, &bugfix);
179 return base::StringPrintf("%d.%d.%d", major, minor, bugfix);
180}
181
182// static
183std::string SysInfo::KernelVersion() {
184 struct utsname info;
185 if (uname(&info) < 0) {
186 NOTREACHED();
187 return std::string();
188 }
189 return std::string(info.release);
190}
191
192// static
[email protected]a0a1c5a2013-09-26 21:51:23193bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) {
194 return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value);
[email protected]61b8ad72009-07-22 00:35:18195}
196
[email protected]af25b472012-04-25 01:59:44197// static
[email protected]a0a1c5a2013-09-26 21:51:23198std::string SysInfo::GetLsbReleaseBoard() {
199 const char kMachineInfoBoard[] = "CHROMEOS_RELEASE_BOARD";
200 std::string board;
201 if (!GetLsbReleaseValue(kMachineInfoBoard, &board))
202 board = "unknown";
203 return board;
204}
205
206// static
[email protected]9eae4e62013-12-04 20:56:49207Time SysInfo::GetLsbReleaseTime() {
[email protected]a0a1c5a2013-09-26 21:51:23208 return GetChromeOSVersionInfo().lsb_release_time();
209}
210
211// static
[email protected]49c4cf852013-09-27 19:28:24212bool SysInfo::IsRunningOnChromeOS() {
213 return GetChromeOSVersionInfo().is_running_on_chromeos();
214}
215
216// static
[email protected]a0a1c5a2013-09-26 21:51:23217void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
218 const Time& lsb_release_time) {
dcheng093de9b2016-04-04 21:25:51219 std::unique_ptr<Environment> env(Environment::Create());
[email protected]a0a1c5a2013-09-26 21:51:23220 env->SetVar(kLsbReleaseKey, lsb_release);
Brett Wilson5ed06e72017-12-01 01:25:11221 env->SetVar(kLsbReleaseTimeKey, NumberToString(lsb_release_time.ToDoubleT()));
[email protected]a0a1c5a2013-09-26 21:51:23222 g_chrome_os_version_info.Get().Parse();
[email protected]af25b472012-04-25 01:59:44223}
224
[email protected]61b8ad72009-07-22 00:35:18225} // namespace base