sys_info_chromeos.cc 9.4 KB

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