blob: f1c6664fabd4edb8a72dd3498eeb53ae939ceac2 [file] [log] [blame]
[email protected]8c32ac12011-04-05 11:31:421// Copyright (c) 2011 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]61b8ad72009-07-22 00:35:1812#include "base/string_tokenizer.h"
[email protected]34b99632011-01-01 01:01:0613#include "base/threading/thread_restrictions.h"
[email protected]61b8ad72009-07-22 00:35:1814
[email protected]8c32ac12011-04-05 11:31:4215#include <execinfo.h>
16
[email protected]61b8ad72009-07-22 00:35:1817namespace base {
18
[email protected]8c32ac12011-04-05 11:31:4219static const char* kLinuxStandardBaseVersionKeys[] = {
20 "CHROMEOS_RELEASE_VERSION",
21 "GOOGLE_RELEASE",
22 "DISTRIB_RELEASE",
23 NULL
24};
[email protected]61b8ad72009-07-22 00:35:1825
26const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
27
[email protected]8c32ac12011-04-05 11:31:4228struct ChromeOSVersionNumbers {
29 ChromeOSVersionNumbers()
30 : major_version(0),
31 minor_version(0),
32 bugfix_version(0),
33 parsed(false) {
34 }
35
36 int32 major_version;
37 int32 minor_version;
38 int32 bugfix_version;
39 bool parsed;
40};
41
42static base::LazyInstance<ChromeOSVersionNumbers>
[email protected]6de0fd1d2011-11-15 13:31:4943 g_chrome_os_version_numbers = LAZY_INSTANCE_INITIALIZER;
[email protected]8c32ac12011-04-05 11:31:4244
[email protected]61b8ad72009-07-22 00:35:1845// static
[email protected]f481221192011-04-07 22:15:3446void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
47 int32* minor_version,
48 int32* bugfix_version) {
[email protected]8c32ac12011-04-05 11:31:4249 if (!g_chrome_os_version_numbers.Get().parsed) {
50 // The other implementations of SysInfo don't block on the disk.
51 // See http://code.google.com/p/chromium/issues/detail?id=60394
52 // Perhaps the caller ought to cache this?
53 // Temporary allowing while we work the bug out.
54 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]ba74b0d22010-10-23 05:19:2055
[email protected]8c32ac12011-04-05 11:31:4256 FilePath path(kLinuxStandardBaseReleaseFile);
57 std::string contents;
58 if (file_util::ReadFileToString(path, &contents)) {
59 g_chrome_os_version_numbers.Get().parsed = true;
60 ParseLsbRelease(contents,
61 &(g_chrome_os_version_numbers.Get().major_version),
62 &(g_chrome_os_version_numbers.Get().minor_version),
63 &(g_chrome_os_version_numbers.Get().bugfix_version));
64 }
[email protected]61b8ad72009-07-22 00:35:1865 }
[email protected]8c32ac12011-04-05 11:31:4266 *major_version = g_chrome_os_version_numbers.Get().major_version;
67 *minor_version = g_chrome_os_version_numbers.Get().minor_version;
68 *bugfix_version = g_chrome_os_version_numbers.Get().bugfix_version;
[email protected]61b8ad72009-07-22 00:35:1869}
70
71// static
72std::string SysInfo::GetLinuxStandardBaseVersionKey() {
[email protected]8c32ac12011-04-05 11:31:4273 return std::string(kLinuxStandardBaseVersionKeys[0]);
[email protected]61b8ad72009-07-22 00:35:1874}
75
76// static
77void SysInfo::ParseLsbRelease(const std::string& lsb_release,
[email protected]f481221192011-04-07 22:15:3478 int32* major_version,
79 int32* minor_version,
80 int32* bugfix_version) {
[email protected]8c32ac12011-04-05 11:31:4281 size_t version_key_index = std::string::npos;
82 for (int i = 0; kLinuxStandardBaseVersionKeys[i] != NULL; ++i) {
83 version_key_index = lsb_release.find(kLinuxStandardBaseVersionKeys[i]);
84 if (std::string::npos != version_key_index) {
85 break;
86 }
87 }
[email protected]61b8ad72009-07-22 00:35:1888 if (std::string::npos == version_key_index) {
89 return;
90 }
[email protected]8c32ac12011-04-05 11:31:4291
[email protected]61b8ad72009-07-22 00:35:1892 size_t start_index = lsb_release.find_first_of('=', version_key_index);
93 start_index++; // Move past '='.
94 size_t length = lsb_release.find_first_of('\n', start_index) - start_index;
95 std::string version = lsb_release.substr(start_index, length);
96 StringTokenizer tokenizer(version, ".");
[email protected]46c373a2011-10-03 06:22:1297 for (int i = 0; i < 3 && tokenizer.GetNext(); ++i) {
98 if (0 == i) {
[email protected]c2750c62010-10-22 16:05:4799 StringToInt(tokenizer.token_begin(),
100 tokenizer.token_end(),
101 major_version);
[email protected]61b8ad72009-07-22 00:35:18102 *minor_version = *bugfix_version = 0;
[email protected]46c373a2011-10-03 06:22:12103 } else if (1 == i) {
[email protected]c2750c62010-10-22 16:05:47104 StringToInt(tokenizer.token_begin(),
105 tokenizer.token_end(),
106 minor_version);
[email protected]46c373a2011-10-03 06:22:12107 } else { // 2 == i
[email protected]c2750c62010-10-22 16:05:47108 StringToInt(tokenizer.token_begin(),
109 tokenizer.token_end(),
110 bugfix_version);
[email protected]61b8ad72009-07-22 00:35:18111 }
112 }
113}
114
115} // namespace base