| 6 |
| 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | #include <sys/utsname.h> |
| 10 |
| 11 | #include "base/environment.h" |
| 12 | #include "base/files/file.h" |
| 13 | #include "base/files/file_path.h" |
| 14 | #include "base/files/file_util.h" |
| 15 | #include "base/lazy_instance.h" |
| 16 | #include "base/stl_util.h" |
| 17 | #include "base/strings/string_number_conversions.h" |
| 18 | #include "base/strings/string_piece.h" |
| 19 | #include "base/strings/string_split.h" |
| 20 | #include "base/strings/string_tokenizer.h" |
| 21 | #include "base/strings/string_util.h" |
| 22 | #include "base/strings/stringprintf.h" |
| 23 | #include "base/threading/thread_restrictions.h" |
| 24 |
| 25 | namespace base { |
| 26 |
| 27 | namespace { |
| 28 |
| 29 | const char* const kLinuxStandardBaseVersionKeys[] = { |
| 30 | "CHROMEOS_RELEASE_VERSION", "GOOGLE_RELEASE", "DISTRIB_RELEASE", |
| 31 | }; |
| 32 |
| 33 | const char kChromeOsReleaseNameKey[] = "CHROMEOS_RELEASE_NAME"; |
| 34 |
| 35 | const char* const kChromeOsReleaseNames[] = { |
| 36 | "Chrome OS", "Chromium OS", |
| 37 | }; |
| 38 |
| 39 | const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; |
| 40 |
| 41 | const char kLsbReleaseKey[] = "LSB_RELEASE"; |
| 42 | const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch |
| 43 |
| 44 | const char kLsbReleaseSourceKey[] = "lsb-release"; |
| 45 | const char kLsbReleaseSourceEnv[] = "env"; |
| 46 | const char kLsbReleaseSourceFile[] = "file"; |
| 47 |
| 48 | class ChromeOSVersionInfo { |
| 49 | public: |
| 50 | ChromeOSVersionInfo() { Parse(); } |
| 51 |
| 52 | void Parse() { |
| 53 | lsb_release_map_.clear(); |
| 54 | major_version_ = 0; |
| 55 | minor_version_ = 0; |
| 56 | bugfix_version_ = 0; |
| 57 | is_running_on_chromeos_ = false; |
| 58 |
| 59 | std::string lsb_release, lsb_release_time_str; |
| 60 | std::unique_ptr<Environment> env(Environment::Create()); |
| 61 | bool parsed_from_env = |
| 62 | env->GetVar(kLsbReleaseKey, &lsb_release) && |
| 63 | env->GetVar(kLsbReleaseTimeKey, &lsb_release_time_str); |
| 64 | if (parsed_from_env) { |
| 65 | double us = 0; |
| 66 | if (StringToDouble(lsb_release_time_str, &us)) |
| 67 | lsb_release_time_ = Time::FromDoubleT(us); |
| 68 | } else { |
| 69 | // If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not |
| 70 | // set, fall back to a blocking read of the lsb_release file. This should |
| 71 | // only happen in non Chrome OS environments. |
| 72 | ThreadRestrictions::ScopedAllowIO allow_io; |
| 73 | FilePath path(kLinuxStandardBaseReleaseFile); |
| 74 | ReadFileToString(path, &lsb_release); |
| 75 | File::Info fileinfo; |
| 76 | if (GetFileInfo(path, &fileinfo)) |
| 77 | lsb_release_time_ = fileinfo.creation_time; |
| 78 | } |
| 79 | ParseLsbRelease(lsb_release); |
| 80 | // For debugging: |
| 81 | lsb_release_map_[kLsbReleaseSourceKey] = |
| 82 | parsed_from_env ? kLsbReleaseSourceEnv : kLsbReleaseSourceFile; |
| 83 | } |
| 84 |
| 85 | bool GetLsbReleaseValue(const std::string& key, std::string* value) { |
| 86 | LsbReleaseMap::const_iterator iter = lsb_release_map_.find(key); |
| 87 | if (iter == lsb_release_map_.end()) |
| 88 | return false; |
| 89 | *value = iter->second; |
| 90 | return true; |
| 91 | } |
| 92 |
| 93 | void GetVersionNumbers(int32_t* major_version, |
| 94 | int32_t* minor_version, |
| 95 | int32_t* bugfix_version) { |
| 96 | *major_version = major_version_; |
| 97 | *minor_version = minor_version_; |
| 98 | *bugfix_version = bugfix_version_; |
| 99 | } |
| 100 |
| 101 | const Time& lsb_release_time() const { return lsb_release_time_; } |
| 102 | bool is_running_on_chromeos() const { return is_running_on_chromeos_; } |
| 103 |
| 104 | private: |
| 105 | void ParseLsbRelease(const std::string& lsb_release) { |
| 106 | // Parse and cache lsb_release key pairs. There should only be a handful |
| 107 | // of entries so the overhead for this will be small, and it can be |
| 108 | // useful for debugging. |
| 109 | base::StringPairs pairs; |
| 110 | SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs); |
| 111 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 112 | std::string key, value; |
| 113 | TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key); |
| 114 | TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value); |
| 115 | if (key.empty()) |
| 116 | continue; |
| 117 | lsb_release_map_[key] = value; |
| 118 | } |
| 119 | // Parse the version from the first matching recognized version key. |
| 120 | std::string version; |
| 121 | for (size_t i = 0; i < base::size(kLinuxStandardBaseVersionKeys); ++i) { |
| 122 | std::string key = kLinuxStandardBaseVersionKeys[i]; |
| 123 | if (GetLsbReleaseValue(key, &version) && !version.empty()) |
| 124 | break; |
| 125 | } |
| 126 | StringTokenizer tokenizer(version, "."); |
| 127 | if (tokenizer.GetNext()) { |
| 128 | StringToInt(tokenizer.token_piece(), &major_version_); |
| 129 | } |
| 130 | if (tokenizer.GetNext()) { |
| 131 | StringToInt(tokenizer.token_piece(), &minor_version_); |
| 132 | } |
| 133 | if (tokenizer.GetNext()) { |
| 134 | StringToInt(tokenizer.token_piece(), &bugfix_version_); |
| 135 | } |
| 136 |
| 137 | // Check release name for Chrome OS. |
| 138 | std::string release_name; |
| 139 | if (GetLsbReleaseValue(kChromeOsReleaseNameKey, &release_name)) { |
| 140 | for (size_t i = 0; i < base::size(kChromeOsReleaseNames); ++i) { |
| 141 | if (release_name == kChromeOsReleaseNames[i]) { |
| 142 | is_running_on_chromeos_ = true; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 |
| 149 | using LsbReleaseMap = std::map<std::string, std::string>; |
| 150 | Time lsb_release_time_; |
| 151 | LsbReleaseMap lsb_release_map_; |
| 152 | int32_t major_version_; |
| 153 | int32_t minor_version_; |
| 154 | int32_t bugfix_version_; |
| 155 | bool is_running_on_chromeos_; |
| 156 | }; |
| 157 |
| 158 | static LazyInstance<ChromeOSVersionInfo>::Leaky g_chrome_os_version_info = |
| 159 | LAZY_INSTANCE_INITIALIZER; |
| 160 |
| 161 | ChromeOSVersionInfo& GetChromeOSVersionInfo() { |
| 162 | return g_chrome_os_version_info.Get(); |
| 163 | } |
| 164 |
| 165 | } // namespace |
| 166 |
| 167 | // static |
| 168 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 169 | int32_t* minor_version, |
| 170 | int32_t* bugfix_version) { |
| 171 | return GetChromeOSVersionInfo().GetVersionNumbers( |
| 172 | major_version, minor_version, bugfix_version); |
| 173 | } |
| 174 |
| 175 | // static |
| 176 | std::string SysInfo::OperatingSystemVersion() { |
| 177 | int32_t major, minor, bugfix; |
| 178 | GetChromeOSVersionInfo().GetVersionNumbers(&major, &minor, &bugfix); |
| 179 | return base::StringPrintf("%d.%d.%d", major, minor, bugfix); |
| 180 | } |
| 181 |
| 182 | // static |
| 183 | std::string SysInfo::KernelVersion() { |
| 184 | struct utsname info; |
| 185 | if (uname(&info) < 0) { |
| 186 | NOTREACHED(); |
| 187 | return std::string(); |
| 188 | } |
| 189 | return std::string(info.release); |
| 190 | } |
| 191 |
| 192 | // static |
| 193 | bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) { |
| 194 | return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value); |
| 195 | } |
| 196 |
| 197 | // static |
| 198 | std::string SysInfo::GetLsbReleaseBoard() { |
| 199 | const char kMachineInfoBoard[] = "CHROMEOS_RELEASE_BOARD"; |
| 200 | std::string board; |
| 201 | if (!GetLsbReleaseValue(kMachineInfoBoard, &board)) |
| 202 | board = "unknown"; |
| 203 | return board; |
| 204 | } |
| 205 |
| 206 | // static |
| 207 | Time SysInfo::GetLsbReleaseTime() { |
| 208 | return GetChromeOSVersionInfo().lsb_release_time(); |
| 209 | } |
| 210 |
| 211 | // static |
| 212 | bool SysInfo::IsRunningOnChromeOS() { |
| 213 | return GetChromeOSVersionInfo().is_running_on_chromeos(); |
| 214 | } |
| 215 |
| 216 | // static |
| 217 | void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release, |
| 218 | const Time& lsb_release_time) { |
| 219 | std::unique_ptr<Environment> env(Environment::Create()); |
| 220 | env->SetVar(kLsbReleaseKey, lsb_release); |
| 221 | env->SetVar(kLsbReleaseTimeKey, NumberToString(lsb_release_time.ToDoubleT())); |
| 222 | g_chrome_os_version_info.Get().Parse(); |
| 223 | } |
| 224 |
| 225 | } // namespace base |
| 226 |