blob: 92a3f0c4b1a5d2969d7ddcb4b4bbc6569e796ab0 [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
5#include "base/sys_info.h"
6
7#include "base/basictypes.h"
8#include "base/file_path.h"
9#include "base/file_util.h"
[email protected]8c32ac12011-04-05 11:31:4210#include "base/lazy_instance.h"
[email protected]e83326f2010-07-31 17:29:2511#include "base/string_number_conversions.h"
[email protected]eb72b272011-12-19 16:10:5512#include "base/string_piece.h"
[email protected]61b8ad72009-07-22 00:35:1813#include "base/string_tokenizer.h"
[email protected]34b99632011-01-01 01:01:0614#include "base/threading/thread_restrictions.h"
[email protected]61b8ad72009-07-22 00:35:1815
[email protected]8c32ac12011-04-05 11:31:4216#include <execinfo.h>
17
[email protected]61b8ad72009-07-22 00:35:1818namespace base {
19
[email protected]8c32ac12011-04-05 11:31:4220static const char* kLinuxStandardBaseVersionKeys[] = {
21 "CHROMEOS_RELEASE_VERSION",
22 "GOOGLE_RELEASE",
23 "DISTRIB_RELEASE",
24 NULL
25};
[email protected]61b8ad72009-07-22 00:35:1826
27const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
28
[email protected]8c32ac12011-04-05 11:31:4229struct ChromeOSVersionNumbers {
30 ChromeOSVersionNumbers()
31 : major_version(0),
32 minor_version(0),
33 bugfix_version(0),
34 parsed(false) {
35 }
36
37 int32 major_version;
38 int32 minor_version;
39 int32 bugfix_version;
40 bool parsed;
41};
42
43static base::LazyInstance<ChromeOSVersionNumbers>
[email protected]6de0fd1d2011-11-15 13:31:4944 g_chrome_os_version_numbers = LAZY_INSTANCE_INITIALIZER;
[email protected]8c32ac12011-04-05 11:31:4245
[email protected]61b8ad72009-07-22 00:35:1846// static
[email protected]f481221192011-04-07 22:15:3447void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
48 int32* minor_version,
49 int32* bugfix_version) {
[email protected]8c32ac12011-04-05 11:31:4250 if (!g_chrome_os_version_numbers.Get().parsed) {
51 // The other implementations of SysInfo don't block on the disk.
52 // See http://code.google.com/p/chromium/issues/detail?id=60394
53 // Perhaps the caller ought to cache this?
54 // Temporary allowing while we work the bug out.
55 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]ba74b0d22010-10-23 05:19:2056
[email protected]8c32ac12011-04-05 11:31:4257 FilePath path(kLinuxStandardBaseReleaseFile);
58 std::string contents;
59 if (file_util::ReadFileToString(path, &contents)) {
60 g_chrome_os_version_numbers.Get().parsed = true;
61 ParseLsbRelease(contents,
62 &(g_chrome_os_version_numbers.Get().major_version),
63 &(g_chrome_os_version_numbers.Get().minor_version),
64 &(g_chrome_os_version_numbers.Get().bugfix_version));
65 }
[email protected]61b8ad72009-07-22 00:35:1866 }
[email protected]8c32ac12011-04-05 11:31:4267 *major_version = g_chrome_os_version_numbers.Get().major_version;
68 *minor_version = g_chrome_os_version_numbers.Get().minor_version;
69 *bugfix_version = g_chrome_os_version_numbers.Get().bugfix_version;
[email protected]61b8ad72009-07-22 00:35:1870}
71
72// static
73std::string SysInfo::GetLinuxStandardBaseVersionKey() {
[email protected]8c32ac12011-04-05 11:31:4274 return std::string(kLinuxStandardBaseVersionKeys[0]);
[email protected]61b8ad72009-07-22 00:35:1875}
76
77// static
78void SysInfo::ParseLsbRelease(const std::string& lsb_release,
[email protected]f481221192011-04-07 22:15:3479 int32* major_version,
80 int32* minor_version,
81 int32* bugfix_version) {
[email protected]8c32ac12011-04-05 11:31:4282 size_t version_key_index = std::string::npos;
83 for (int i = 0; kLinuxStandardBaseVersionKeys[i] != NULL; ++i) {
84 version_key_index = lsb_release.find(kLinuxStandardBaseVersionKeys[i]);
85 if (std::string::npos != version_key_index) {
86 break;
87 }
88 }
[email protected]61b8ad72009-07-22 00:35:1889 if (std::string::npos == version_key_index) {
90 return;
91 }
[email protected]8c32ac12011-04-05 11:31:4292
[email protected]61b8ad72009-07-22 00:35:1893 size_t start_index = lsb_release.find_first_of('=', version_key_index);
94 start_index++; // Move past '='.
95 size_t length = lsb_release.find_first_of('\n', start_index) - start_index;
96 std::string version = lsb_release.substr(start_index, length);
97 StringTokenizer tokenizer(version, ".");
[email protected]46c373a2011-10-03 06:22:1298 for (int i = 0; i < 3 && tokenizer.GetNext(); ++i) {
99 if (0 == i) {
[email protected]eb72b272011-12-19 16:10:55100 StringToInt(StringPiece(tokenizer.token_begin(),
101 tokenizer.token_end()),
[email protected]c2750c62010-10-22 16:05:47102 major_version);
[email protected]61b8ad72009-07-22 00:35:18103 *minor_version = *bugfix_version = 0;
[email protected]46c373a2011-10-03 06:22:12104 } else if (1 == i) {
[email protected]eb72b272011-12-19 16:10:55105 StringToInt(StringPiece(tokenizer.token_begin(),
106 tokenizer.token_end()),
[email protected]c2750c62010-10-22 16:05:47107 minor_version);
[email protected]46c373a2011-10-03 06:22:12108 } else { // 2 == i
[email protected]eb72b272011-12-19 16:10:55109 StringToInt(StringPiece(tokenizer.token_begin(),
110 tokenizer.token_end()),
[email protected]c2750c62010-10-22 16:05:47111 bugfix_version);
[email protected]61b8ad72009-07-22 00:35:18112 }
113 }
114}
115
[email protected]af25b472012-04-25 01:59:44116// static
117FilePath SysInfo::GetLsbReleaseFilePath() {
118 return FilePath(kLinuxStandardBaseReleaseFile);
119}
120
[email protected]61b8ad72009-07-22 00:35:18121} // namespace base