Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: chromeos/system/statistics_provider.cc

Issue 2218703006: Clean up handling of invalid HWID when running ChromeOS in VMs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add comment Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/system/statistics_provider.h" 5 #include "chromeos/system/statistics_provider.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
13 #include "base/json/json_file_value_serializer.h" 13 #include "base/json/json_file_value_serializer.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/singleton.h" 17 #include "base/memory/singleton.h"
18 #include "base/path_service.h" 18 #include "base/path_service.h"
19 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_util.h"
20 #include "base/synchronization/cancellation_flag.h" 21 #include "base/synchronization/cancellation_flag.h"
21 #include "base/synchronization/waitable_event.h" 22 #include "base/synchronization/waitable_event.h"
22 #include "base/sys_info.h" 23 #include "base/sys_info.h"
23 #include "base/task_runner.h" 24 #include "base/task_runner.h"
24 #include "base/threading/thread_restrictions.h" 25 #include "base/threading/thread_restrictions.h"
25 #include "base/time/time.h" 26 #include "base/time/time.h"
26 #include "base/values.h" 27 #include "base/values.h"
27 #include "chromeos/app_mode/kiosk_oem_manifest_parser.h" 28 #include "chromeos/app_mode/kiosk_oem_manifest_parser.h"
28 #include "chromeos/chromeos_constants.h" 29 #include "chromeos/chromeos_constants.h"
29 #include "chromeos/chromeos_paths.h" 30 #include "chromeos/chromeos_paths.h"
30 #include "chromeos/chromeos_switches.h" 31 #include "chromeos/chromeos_switches.h"
31 #include "chromeos/system/name_value_pairs_parser.h" 32 #include "chromeos/system/name_value_pairs_parser.h"
32 33
33 namespace chromeos { 34 namespace chromeos {
34 namespace system { 35 namespace system {
35 36
36 namespace { 37 namespace {
37 38
38 // Path to the tool used to get system info, and delimiters for the output 39 // Path to the tool used to get system info, and delimiters for the output
39 // format of the tool. 40 // format of the tool.
40 const char* kCrosSystemTool[] = { "/usr/bin/crossystem" }; 41 const char* kCrosSystemTool[] = { "/usr/bin/crossystem" };
41 const char kCrosSystemEq[] = "="; 42 const char kCrosSystemEq[] = "=";
42 const char kCrosSystemDelim[] = "\n"; 43 const char kCrosSystemDelim[] = "\n";
43 const char kCrosSystemCommentDelim[] = "#"; 44 const char kCrosSystemCommentDelim[] = "#";
44 const char kCrosSystemUnknownValue[] = "(error)"; 45 const char kCrosSystemValueError[] = "(error)";
45 46
46 const char kHardwareClassCrosSystemKey[] = "hwid"; 47 const char kHardwareClassCrosSystemKey[] = "hwid";
47 const char kUnknownHardwareClass[] = "unknown"; 48 const char kHardwareClassValueUnknown[] = "unknown";
48 49
49 // File to get system vendor information from. 50 const char kIsVmCrosSystemKey[] = "inside_vm";
51
52 // File to get system vendor information from. (x86-only)
50 const char kSystemVendorFile[] = "/sys/class/dmi/id/sys_vendor"; 53 const char kSystemVendorFile[] = "/sys/class/dmi/id/sys_vendor";
54 const char kSystemVendorValueUnknown[] = "unknown";
51 55
52 // Key/value delimiters of machine hardware info file. machine-info is generated 56 // Key/value delimiters of machine hardware info file. machine-info is generated
53 // only for OOBE and enterprise enrollment and may not be present. See 57 // only for OOBE and enterprise enrollment and may not be present. See
54 // login-manager/init/machine-info.conf. 58 // login-manager/init/machine-info.conf.
55 const char kMachineHardwareInfoEq[] = "="; 59 const char kMachineHardwareInfoEq[] = "=";
56 const char kMachineHardwareInfoDelim[] = " \n"; 60 const char kMachineHardwareInfoDelim[] = " \n";
57 61
58 // File to get ECHO coupon info from, and key/value delimiters of 62 // File to get ECHO coupon info from, and key/value delimiters of
59 // the file. 63 // the file.
60 const char kEchoCouponFile[] = "/var/cache/echo/vpd_echo.txt"; 64 const char kEchoCouponFile[] = "/var/cache/echo/vpd_echo.txt";
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 const char kCheckEnrollmentKey[] = "check_enrollment"; 155 const char kCheckEnrollmentKey[] = "check_enrollment";
152 const char kCustomizationIdKey[] = "customization_id"; 156 const char kCustomizationIdKey[] = "customization_id";
153 const char kDevSwitchBootKey[] = "devsw_boot"; 157 const char kDevSwitchBootKey[] = "devsw_boot";
154 const char kDevSwitchBootValueDev[] = "1"; 158 const char kDevSwitchBootValueDev[] = "1";
155 const char kDevSwitchBootValueVerified[] = "0"; 159 const char kDevSwitchBootValueVerified[] = "0";
156 const char kFirmwareTypeKey[] = "mainfw_type"; 160 const char kFirmwareTypeKey[] = "mainfw_type";
157 const char kFirmwareTypeValueDeveloper[] = "developer"; 161 const char kFirmwareTypeValueDeveloper[] = "developer";
158 const char kFirmwareTypeValueNonchrome[] = "nonchrome"; 162 const char kFirmwareTypeValueNonchrome[] = "nonchrome";
159 const char kFirmwareTypeValueNormal[] = "normal"; 163 const char kFirmwareTypeValueNormal[] = "normal";
160 const char kHardwareClassKey[] = "hardware_class"; 164 const char kHardwareClassKey[] = "hardware_class";
165 const char kIsCrosVmKey[] = "inside_cros_vm";
Daniel Erat 2016/08/10 16:32:28 this code is only built for chrome os, so includin
norvez 2016/08/11 17:10:34 Done.
166 const char kIsCrosVmValueTrue[] = "1";
167 const char kIsCrosVmValueFalse[] = "0";
161 const char kSystemVendorKey[] = "system_vendor"; 168 const char kSystemVendorKey[] = "system_vendor";
162 const char kOffersCouponCodeKey[] = "ubind_attribute"; 169 const char kOffersCouponCodeKey[] = "ubind_attribute";
163 const char kOffersGroupCodeKey[] = "gbind_attribute"; 170 const char kOffersGroupCodeKey[] = "gbind_attribute";
164 const char kRlzBrandCodeKey[] = "rlz_brand_code"; 171 const char kRlzBrandCodeKey[] = "rlz_brand_code";
165 const char kWriteProtectSwitchBootKey[] = "wpsw_boot"; 172 const char kWriteProtectSwitchBootKey[] = "wpsw_boot";
166 const char kWriteProtectSwitchBootValueOff[] = "0"; 173 const char kWriteProtectSwitchBootValueOff[] = "0";
167 const char kWriteProtectSwitchBootValueOn[] = "1"; 174 const char kWriteProtectSwitchBootValueOn[] = "1";
168 const char kRegionKey[] = "region"; 175 const char kRegionKey[] = "region";
169 const char kInitialLocaleKey[] = "initial_locale"; 176 const char kInitialLocaleKey[] = "initial_locale";
170 const char kInitialTimezoneKey[] = "initial_timezone"; 177 const char kInitialTimezoneKey[] = "initial_timezone";
(...skipping 14 matching lines...) Expand all
185 public: 192 public:
186 // StatisticsProvider implementation: 193 // StatisticsProvider implementation:
187 void StartLoadingMachineStatistics( 194 void StartLoadingMachineStatistics(
188 const scoped_refptr<base::TaskRunner>& file_task_runner, 195 const scoped_refptr<base::TaskRunner>& file_task_runner,
189 bool load_oem_manifest) override; 196 bool load_oem_manifest) override;
190 bool GetMachineStatistic(const std::string& name, 197 bool GetMachineStatistic(const std::string& name,
191 std::string* result) override; 198 std::string* result) override;
192 bool GetMachineFlag(const std::string& name, bool* result) override; 199 bool GetMachineFlag(const std::string& name, bool* result) override;
193 void Shutdown() override; 200 void Shutdown() override;
194 201
202 // Returns true when Chrome OS is running in a VM. NOTE: if crossystem is not
203 // installed it will return false even if Chrome OS is running in a VM.
204 bool IsRunningOnVm() override;
205
195 static StatisticsProviderImpl* GetInstance(); 206 static StatisticsProviderImpl* GetInstance();
196 207
197 protected: 208 protected:
198 typedef std::map<std::string, bool> MachineFlags; 209 typedef std::map<std::string, bool> MachineFlags;
199 typedef bool (*RegionDataExtractor)(const base::DictionaryValue*, 210 typedef bool (*RegionDataExtractor)(const base::DictionaryValue*,
200 std::string*); 211 std::string*);
201 friend struct base::DefaultSingletonTraits<StatisticsProviderImpl>; 212 friend struct base::DefaultSingletonTraits<StatisticsProviderImpl>;
202 213
203 StatisticsProviderImpl(); 214 StatisticsProviderImpl();
204 ~StatisticsProviderImpl() override; 215 ~StatisticsProviderImpl() override;
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 } 379 }
369 if (result != nullptr) 380 if (result != nullptr)
370 *result = iter->second; 381 *result = iter->second;
371 return true; 382 return true;
372 } 383 }
373 384
374 void StatisticsProviderImpl::Shutdown() { 385 void StatisticsProviderImpl::Shutdown() {
375 cancellation_flag_.Set(); // Cancel any pending loads 386 cancellation_flag_.Set(); // Cancel any pending loads
376 } 387 }
377 388
389 bool StatisticsProviderImpl::IsRunningOnVm() {
390 if (!base::SysInfo::IsRunningOnChromeOS())
391 return false;
392 std::string is_vm;
393 return GetMachineStatistic(kIsCrosVmKey, &is_vm) &&
394 is_vm == kIsCrosVmValueTrue;
395 }
396
378 StatisticsProviderImpl::StatisticsProviderImpl() 397 StatisticsProviderImpl::StatisticsProviderImpl()
379 : load_statistics_started_(false), 398 : load_statistics_started_(false),
380 on_statistics_loaded_(base::WaitableEvent::ResetPolicy::MANUAL, 399 on_statistics_loaded_(base::WaitableEvent::ResetPolicy::MANUAL,
381 base::WaitableEvent::InitialState::NOT_SIGNALED), 400 base::WaitableEvent::InitialState::NOT_SIGNALED),
382 oem_manifest_loaded_(false) { 401 oem_manifest_loaded_(false) {
383 regional_data_extractors_[kInitialLocaleKey] = 402 regional_data_extractors_[kInitialLocaleKey] =
384 &GetInitialLocaleFromRegionalData; 403 &GetInitialLocaleFromRegionalData;
385 regional_data_extractors_[kKeyboardLayoutKey] = 404 regional_data_extractors_[kKeyboardLayoutKey] =
386 &GetKeyboardLayoutFromRegionalData; 405 &GetKeyboardLayoutFromRegionalData;
387 regional_data_extractors_[kInitialTimezoneKey] = 406 regional_data_extractors_[kInitialTimezoneKey] =
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 int bytes_written = base::WriteFile(machine_info_path, 459 int bytes_written = base::WriteFile(machine_info_path,
441 stub_contents.c_str(), 460 stub_contents.c_str(),
442 stub_contents.size()); 461 stub_contents.size());
443 // static_cast<int> is fine because stub_contents is small. 462 // static_cast<int> is fine because stub_contents is small.
444 if (bytes_written < static_cast<int>(stub_contents.size())) { 463 if (bytes_written < static_cast<int>(stub_contents.size())) {
445 LOG(ERROR) << "Error writing machine info stub: " 464 LOG(ERROR) << "Error writing machine info stub: "
446 << machine_info_path.value(); 465 << machine_info_path.value();
447 } 466 }
448 } 467 }
449 468
450 if (base::SysInfo::IsRunningOnChromeOS()) {
451 std::string system_vendor;
452 base::ReadFileToString(base::FilePath(kSystemVendorFile), &system_vendor);
453 machine_info_[kSystemVendorKey] = system_vendor;
454 }
455
456 parser.GetNameValuePairsFromFile(machine_info_path, 469 parser.GetNameValuePairsFromFile(machine_info_path,
457 kMachineHardwareInfoEq, 470 kMachineHardwareInfoEq,
458 kMachineHardwareInfoDelim); 471 kMachineHardwareInfoDelim);
459 parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile), 472 parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile),
460 kEchoCouponEq, 473 kEchoCouponEq,
461 kEchoCouponDelim); 474 kEchoCouponDelim);
462 parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile), 475 parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile),
463 kVpdEq, 476 kVpdEq,
464 kVpdDelim); 477 kVpdDelim);
465 478
466 // Ensure that the hardware class key is present with the expected 479 // Ensure that the hardware class key is present with the expected
467 // key name, and if it couldn't be retrieved, that the value is "unknown". 480 // key name, and if it couldn't be retrieved, that the value is "unknown".
468 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; 481 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey];
469 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) 482 if (hardware_class.empty() || hardware_class == kCrosSystemValueError) {
470 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; 483 machine_info_[kHardwareClassKey] = kHardwareClassValueUnknown;
471 else 484 } else {
472 machine_info_[kHardwareClassKey] = hardware_class; 485 machine_info_[kHardwareClassKey] = hardware_class;
486 }
487
488 if (base::SysInfo::IsRunningOnChromeOS()) {
489 // By default, assume that this is *not* a VM. If crossystem is not present,
490 // report that we are not in a VM.
491 machine_info_[kIsCrosVmKey] = kIsCrosVmValueFalse;
492 const auto is_vm_iter = machine_info_.find(kIsVmCrosSystemKey);
493 if (is_vm_iter != machine_info_.end() &&
494 is_vm_iter->second == kIsCrosVmValueTrue) {
495 machine_info_[kIsCrosVmKey] = kIsCrosVmValueTrue;
496 }
497
498 // Ensure that the system vendor key is present, in particular it is
499 // potentially useful for VM environments from different vendors.
500 // On non-x86 architectures the value will be "unknown".
501 machine_info_[kSystemVendorKey] = kSystemVendorValueUnknown;
502 std::string system_vendor;
503 if (base::ReadFileToString(base::FilePath(kSystemVendorFile),
504 &system_vendor)) {
505 base::TrimWhitespaceASCII(system_vendor, base::TRIM_ALL, &system_vendor);
506 if (!system_vendor.empty())
507 machine_info_[kSystemVendorKey] = system_vendor;
achuithb 2016/08/10 18:24:58 Do we actually care about this?
norvez 2016/08/11 17:10:34 Removed system_vendor altogether
508 }
509 }
473 510
474 if (load_oem_manifest) { 511 if (load_oem_manifest) {
475 // If kAppOemManifestFile switch is specified, load OEM Manifest file. 512 // If kAppOemManifestFile switch is specified, load OEM Manifest file.
476 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 513 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
477 if (command_line->HasSwitch(switches::kAppOemManifestFile)) { 514 if (command_line->HasSwitch(switches::kAppOemManifestFile)) {
478 LoadOemManifestFromFile( 515 LoadOemManifestFromFile(
479 command_line->GetSwitchValuePath(switches::kAppOemManifestFile)); 516 command_line->GetSwitchValuePath(switches::kAppOemManifestFile));
480 } else if (base::SysInfo::IsRunningOnChromeOS()) { 517 } else if (base::SysInfo::IsRunningOnChromeOS()) {
481 LoadOemManifestFromFile(base::FilePath(kOemManifestFilePath)); 518 LoadOemManifestFromFile(base::FilePath(kOemManifestFilePath));
482 } 519 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 return StatisticsProviderImpl::GetInstance(); 608 return StatisticsProviderImpl::GetInstance();
572 } 609 }
573 610
574 // static 611 // static
575 void StatisticsProvider::SetTestProvider(StatisticsProvider* test_provider) { 612 void StatisticsProvider::SetTestProvider(StatisticsProvider* test_provider) {
576 g_test_statistics_provider = test_provider; 613 g_test_statistics_provider = test_provider;
577 } 614 }
578 615
579 } // namespace system 616 } // namespace system
580 } // namespace chromeos 617 } // namespace chromeos
OLDNEW
« chromeos/system/statistics_provider.h ('K') | « chromeos/system/statistics_provider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698