blob: 53bfbd64d92cd378ec54cca7e9d0a3260f80bed5 [file] [log] [blame]
khorimoto@chromium.orgc7e4e002012-04-25 10:59:44 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
evan@chromium.org54b72102009-07-22 09:35:18 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/sys_info.h"
6
avia6a6a682015-12-27 07:15:14 +09007#include <stddef.h>
8#include <stdint.h>
9
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090010#include "base/environment.h"
rvargas@chromium.orgb005b382014-01-07 19:06:58 +090011#include "base/files/file.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090012#include "base/files/file_path.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +090013#include "base/files/file_util.h"
dgozman@chromium.org4fadf422011-04-05 20:31:42 +090014#include "base/lazy_instance.h"
avia6a6a682015-12-27 07:15:14 +090015#include "base/macros.h"
brettw@chromium.orgabcde5c2013-02-07 11:57:22 +090016#include "base/strings/string_number_conversions.h"
tfarina@chromium.orgb6d49112013-03-30 23:29:00 +090017#include "base/strings/string_piece.h"
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090018#include "base/strings/string_split.h"
brettw@chromium.org2cbc2872013-02-02 09:21:39 +090019#include "base/strings/string_tokenizer.h"
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090020#include "base/strings/string_util.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090021#include "base/threading/thread_restrictions.h"
evan@chromium.org54b72102009-07-22 09:35:18 +090022
23namespace base {
24
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090025namespace {
26
thestigdd7aaa02014-10-21 12:11:21 +090027const char* const kLinuxStandardBaseVersionKeys[] = {
dgozman@chromium.org4fadf422011-04-05 20:31:42 +090028 "CHROMEOS_RELEASE_VERSION",
29 "GOOGLE_RELEASE",
30 "DISTRIB_RELEASE",
dgozman@chromium.org4fadf422011-04-05 20:31:42 +090031};
stevenjb@chromium.org861313b2013-09-28 04:28:24 +090032
33const char kChromeOsReleaseNameKey[] = "CHROMEOS_RELEASE_NAME";
34
35const char* const kChromeOsReleaseNames[] = {
36 "Chrome OS",
37 "Chromium OS",
38};
evan@chromium.org54b72102009-07-22 09:35:18 +090039
40const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
41
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090042const char kLsbReleaseKey[] = "LSB_RELEASE";
43const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch
44
45const char kLsbReleaseSourceKey[] = "lsb-release";
46const char kLsbReleaseSourceEnv[] = "env";
47const char kLsbReleaseSourceFile[] = "file";
48
49class ChromeOSVersionInfo {
50 public:
51 ChromeOSVersionInfo() {
52 Parse();
dgozman@chromium.org4fadf422011-04-05 20:31:42 +090053 }
54
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090055 void Parse() {
56 lsb_release_map_.clear();
57 major_version_ = 0;
58 minor_version_ = 0;
59 bugfix_version_ = 0;
stevenjb@chromium.org861313b2013-09-28 04:28:24 +090060 is_running_on_chromeos_ = false;
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090061
62 std::string lsb_release, lsb_release_time_str;
dchengcc8e4d82016-04-05 06:25:51 +090063 std::unique_ptr<Environment> env(Environment::Create());
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090064 bool parsed_from_env =
65 env->GetVar(kLsbReleaseKey, &lsb_release) &&
66 env->GetVar(kLsbReleaseTimeKey, &lsb_release_time_str);
67 if (parsed_from_env) {
68 double us = 0;
69 if (StringToDouble(lsb_release_time_str, &us))
brettw@chromium.orga9154032013-12-05 05:56:49 +090070 lsb_release_time_ = Time::FromDoubleT(us);
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090071 } else {
72 // If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not
73 // set, fall back to a blocking read of the lsb_release file. This should
74 // only happen in non Chrome OS environments.
75 ThreadRestrictions::ScopedAllowIO allow_io;
76 FilePath path(kLinuxStandardBaseReleaseFile);
77 ReadFileToString(path, &lsb_release);
rvargas@chromium.orgb005b382014-01-07 19:06:58 +090078 File::Info fileinfo;
brettw@chromium.orga9154032013-12-05 05:56:49 +090079 if (GetFileInfo(path, &fileinfo))
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090080 lsb_release_time_ = fileinfo.creation_time;
81 }
82 ParseLsbRelease(lsb_release);
83 // For debugging:
84 lsb_release_map_[kLsbReleaseSourceKey] =
85 parsed_from_env ? kLsbReleaseSourceEnv : kLsbReleaseSourceFile;
86 }
87
88 bool GetLsbReleaseValue(const std::string& key, std::string* value) {
89 SysInfo::LsbReleaseMap::const_iterator iter = lsb_release_map_.find(key);
90 if (iter == lsb_release_map_.end())
91 return false;
92 *value = iter->second;
93 return true;
94 }
95
avi95a2f372015-12-09 09:44:49 +090096 void GetVersionNumbers(int32_t* major_version,
97 int32_t* minor_version,
98 int32_t* bugfix_version) {
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090099 *major_version = major_version_;
100 *minor_version = minor_version_;
101 *bugfix_version = bugfix_version_;
102 }
103
brettw@chromium.orga9154032013-12-05 05:56:49 +0900104 const Time& lsb_release_time() const { return lsb_release_time_; }
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900105 const SysInfo::LsbReleaseMap& lsb_release_map() const {
106 return lsb_release_map_;
107 }
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900108 bool is_running_on_chromeos() const { return is_running_on_chromeos_; }
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900109
110 private:
111 void ParseLsbRelease(const std::string& lsb_release) {
112 // Parse and cache lsb_release key pairs. There should only be a handful
113 // of entries so the overhead for this will be small, and it can be
114 // useful for debugging.
patrikacklandd5168d92015-04-28 06:29:45 +0900115 base::StringPairs pairs;
brettw@chromium.orga9154032013-12-05 05:56:49 +0900116 SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900117 for (size_t i = 0; i < pairs.size(); ++i) {
118 std::string key, value;
119 TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
120 TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value);
121 if (key.empty())
122 continue;
123 lsb_release_map_[key] = value;
124 }
125 // Parse the version from the first matching recognized version key.
126 std::string version;
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900127 for (size_t i = 0; i < arraysize(kLinuxStandardBaseVersionKeys); ++i) {
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900128 std::string key = kLinuxStandardBaseVersionKeys[i];
129 if (GetLsbReleaseValue(key, &version) && !version.empty())
130 break;
131 }
132 StringTokenizer tokenizer(version, ".");
133 if (tokenizer.GetNext()) {
David Benjaminc4447332018-05-08 11:13:51 +0900134 StringToInt(tokenizer.token_piece(), &major_version_);
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900135 }
136 if (tokenizer.GetNext()) {
David Benjaminc4447332018-05-08 11:13:51 +0900137 StringToInt(tokenizer.token_piece(), &minor_version_);
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900138 }
139 if (tokenizer.GetNext()) {
David Benjaminc4447332018-05-08 11:13:51 +0900140 StringToInt(tokenizer.token_piece(), &bugfix_version_);
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900141 }
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900142
143 // Check release name for Chrome OS.
144 std::string release_name;
145 if (GetLsbReleaseValue(kChromeOsReleaseNameKey, &release_name)) {
146 for (size_t i = 0; i < arraysize(kChromeOsReleaseNames); ++i) {
147 if (release_name == kChromeOsReleaseNames[i]) {
148 is_running_on_chromeos_ = true;
149 break;
150 }
151 }
152 }
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900153 }
154
brettw@chromium.orga9154032013-12-05 05:56:49 +0900155 Time lsb_release_time_;
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900156 SysInfo::LsbReleaseMap lsb_release_map_;
avi95a2f372015-12-09 09:44:49 +0900157 int32_t major_version_;
158 int32_t minor_version_;
159 int32_t bugfix_version_;
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900160 bool is_running_on_chromeos_;
dgozman@chromium.org4fadf422011-04-05 20:31:42 +0900161};
162
jamescook01a73262016-07-20 07:11:10 +0900163static LazyInstance<ChromeOSVersionInfo>::Leaky
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900164 g_chrome_os_version_info = LAZY_INSTANCE_INITIALIZER;
165
166ChromeOSVersionInfo& GetChromeOSVersionInfo() {
167 return g_chrome_os_version_info.Get();
168}
169
170} // namespace
dgozman@chromium.org4fadf422011-04-05 20:31:42 +0900171
evan@chromium.org54b72102009-07-22 09:35:18 +0900172// static
avi95a2f372015-12-09 09:44:49 +0900173void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
174 int32_t* minor_version,
175 int32_t* bugfix_version) {
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900176 return GetChromeOSVersionInfo().GetVersionNumbers(
177 major_version, minor_version, bugfix_version);
evan@chromium.org54b72102009-07-22 09:35:18 +0900178}
179
180// static
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900181const SysInfo::LsbReleaseMap& SysInfo::GetLsbReleaseMap() {
182 return GetChromeOSVersionInfo().lsb_release_map();
evan@chromium.org54b72102009-07-22 09:35:18 +0900183}
184
185// static
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900186bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) {
187 return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value);
evan@chromium.org54b72102009-07-22 09:35:18 +0900188}
189
khorimoto@chromium.orgc7e4e002012-04-25 10:59:44 +0900190// static
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900191std::string SysInfo::GetLsbReleaseBoard() {
192 const char kMachineInfoBoard[] = "CHROMEOS_RELEASE_BOARD";
193 std::string board;
194 if (!GetLsbReleaseValue(kMachineInfoBoard, &board))
195 board = "unknown";
196 return board;
197}
198
199// static
brettw@chromium.orga9154032013-12-05 05:56:49 +0900200Time SysInfo::GetLsbReleaseTime() {
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900201 return GetChromeOSVersionInfo().lsb_release_time();
202}
203
204// static
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900205bool SysInfo::IsRunningOnChromeOS() {
206 return GetChromeOSVersionInfo().is_running_on_chromeos();
207}
208
209// static
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900210void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
211 const Time& lsb_release_time) {
dchengcc8e4d82016-04-05 06:25:51 +0900212 std::unique_ptr<Environment> env(Environment::Create());
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900213 env->SetVar(kLsbReleaseKey, lsb_release);
Brett Wilsonbd836f12017-12-01 10:25:11 +0900214 env->SetVar(kLsbReleaseTimeKey, NumberToString(lsb_release_time.ToDoubleT()));
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900215 g_chrome_os_version_info.Get().Parse();
khorimoto@chromium.orgc7e4e002012-04-25 10:59:44 +0900216}
217
evan@chromium.org54b72102009-07-22 09:35:18 +0900218} // namespace base