| 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 | |
| 5 | #include "base/system/sys_info.h" |
| 6 | |
| 7 | #include <dlfcn.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | #include <sys/system_properties.h> |
| 11 | |
| 12 | #include "base/android/jni_android.h" |
| 13 | #include "base/android/sys_utils.h" |
| 14 | #include "base/lazy_instance.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "base/strings/string_number_conversions.h" |
| 17 | #include "base/strings/string_piece.h" |
| 18 | #include "base/strings/string_util.h" |
| 19 | #include "base/strings/stringprintf.h" |
| 20 | #include "base/system/sys_info_internal.h" |
| 21 | |
| 22 | #if (__ANDROID_API__ >= 21 /* 5.0 - Lollipop */) |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | typedef int(SystemPropertyGetFunction)(const char*, char*); |
| 27 | |
| 28 | SystemPropertyGetFunction* DynamicallyLoadRealSystemPropertyGet() { |
| 29 | // libc.so should already be open, get a handle to it. |
| 30 | void* handle = dlopen("libc.so", RTLD_NOLOAD); |
| 31 | if (!handle) { |
| 32 | LOG(FATAL) << "Cannot dlopen libc.so: " << dlerror(); |
| 33 | } |
| 34 | SystemPropertyGetFunction* real_system_property_get = |
| 35 | reinterpret_cast<SystemPropertyGetFunction*>( |
| 36 | dlsym(handle, "__system_property_get")); |
| 37 | if (!real_system_property_get) { |
| 38 | LOG(FATAL) << "Cannot resolve __system_property_get(): " << dlerror(); |
| 39 | } |
| 40 | return real_system_property_get; |
| 41 | } |
| 42 | |
| 43 | static base::LazyInstance<base::internal::LazySysInfoValue< |
| 44 | SystemPropertyGetFunction*, |
| 45 | DynamicallyLoadRealSystemPropertyGet>>::Leaky |
| 46 | g_lazy_real_system_property_get = LAZY_INSTANCE_INITIALIZER; |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | // Android 'L' removes __system_property_get from the NDK, however it is still |
| 51 | // a hidden symbol in libc. Until we remove all calls of __system_property_get |
| 52 | // from Chrome we work around this by defining a weak stub here, which uses |
| 53 | // dlsym to but ensures that Chrome uses the real system |
| 54 | // implementatation when loaded. http://crbug.com/392191. |
| 55 | BASE_EXPORT int __system_property_get(const char* name, char* value) { |
| 56 | return g_lazy_real_system_property_get.Get().value()(name, value); |
| 57 | } |
| 58 | |
| 59 | #endif |
| 60 | |
| 61 | namespace { |
| 62 | |
| 63 | // Default version of Android to fall back to when actual version numbers |
| 64 | // cannot be acquired. Use the latest Android release with a higher bug fix |
| 65 | // version to avoid unnecessarily comparison errors with the latest release. |
| 66 | // This should be manually kept up to date on each Android release. |
| 67 | const int kDefaultAndroidMajorVersion = 10; |
| 68 | const int kDefaultAndroidMinorVersion = 0; |
| 69 | const int kDefaultAndroidBugfixVersion = 99; |
| 70 | |
| 71 | // Get and parse out the OS version numbers from the system properties. |
| 72 | // Note if parse fails, the "default" version is returned as fallback. |
| 73 | void GetOsVersionStringAndNumbers(std::string* version_string, |
| 74 | int32_t* major_version, |
| 75 | int32_t* minor_version, |
| 76 | int32_t* bugfix_version) { |
| 77 | // Read the version number string out from the properties. |
| 78 | char os_version_str[PROP_VALUE_MAX]; |
| 79 | __system_property_get("ro.build.version.release", os_version_str); |
| 80 | |
| 81 | if (os_version_str[0]) { |
| 82 | // Try to parse out the version numbers from the string. |
| 83 | int num_read = sscanf(os_version_str, "%d.%d.%d", major_version, |
| 84 | minor_version, bugfix_version); |
| 85 | |
| 86 | if (num_read > 0) { |
| 87 | // If we don't have a full set of version numbers, make the extras 0. |
| 88 | if (num_read < 2) |
| 89 | *minor_version = 0; |
| 90 | if (num_read < 3) |
| 91 | *bugfix_version = 0; |
| 92 | *version_string = std::string(os_version_str); |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // For some reason, we couldn't parse the version number string. |
| 98 | *major_version = kDefaultAndroidMajorVersion; |
| 99 | *minor_version = kDefaultAndroidMinorVersion; |
| 100 | *bugfix_version = kDefaultAndroidBugfixVersion; |
| 101 | *version_string = ::base::StringPrintf("%d.%d.%d", *major_version, |
| 102 | *minor_version, *bugfix_version); |
| 103 | } |
| 104 | |
| 105 | // Parses a system property (specified with unit 'k','m' or 'g'). |
| 106 | // Returns a value in bytes. |
| 107 | // Returns -1 if the string could not be parsed. |
| 108 | int64_t ParseSystemPropertyBytes(const base::StringPiece& str) { |
| 109 | const int64_t KB = 1024; |
| 110 | const int64_t MB = 1024 * KB; |
| 111 | const int64_t GB = 1024 * MB; |
| 112 | if (str.size() == 0u) |
| 113 | return -1; |
| 114 | int64_t unit_multiplier = 1; |
| 115 | size_t length = str.size(); |
| 116 | if (str[length - 1] == 'k') { |
| 117 | unit_multiplier = KB; |
| 118 | length--; |
| 119 | } else if (str[length - 1] == 'm') { |
| 120 | unit_multiplier = MB; |
| 121 | length--; |
| 122 | } else if (str[length - 1] == 'g') { |
| 123 | unit_multiplier = GB; |
| 124 | length--; |
| 125 | } |
| 126 | int64_t result = 0; |
| 127 | bool parsed = base::StringToInt64(str.substr(0, length), &result); |
| 128 | bool negative = result <= 0; |
| 129 | bool overflow = |
| 130 | result >= std::numeric_limits<int64_t>::max() / unit_multiplier; |
| 131 | if (!parsed || negative || overflow) |
| 132 | return -1; |
| 133 | return result * unit_multiplier; |
| 134 | } |
| 135 | |
| 136 | int GetDalvikHeapSizeMB() { |
| 137 | char heap_size_str[PROP_VALUE_MAX]; |
| 138 | __system_property_get("dalvik.vm.heapsize", heap_size_str); |
| 139 | // dalvik.vm.heapsize property is writable by a root user. |
| 140 | // Clamp it to reasonable range as a sanity check, |
| 141 | // a typical android device will never have less than 48MB. |
| 142 | const int64_t MB = 1024 * 1024; |
| 143 | int64_t result = ParseSystemPropertyBytes(heap_size_str); |
| 144 | if (result == -1) { |
| 145 | // We should consider not exposing these values if they are not reliable. |
| 146 | LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str; |
| 147 | result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3; |
| 148 | } |
| 149 | result = |
| 150 | std::min<int64_t>(std::max<int64_t>(32 * MB, result), 1024 * MB) / MB; |
| 151 | return static_cast<int>(result); |
| 152 | } |
| 153 | |
| 154 | int GetDalvikHeapGrowthLimitMB() { |
| 155 | char heap_size_str[PROP_VALUE_MAX]; |
| 156 | __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str); |
| 157 | // dalvik.vm.heapgrowthlimit property is writable by a root user. |
| 158 | // Clamp it to reasonable range as a sanity check, |
| 159 | // a typical android device will never have less than 24MB. |
| 160 | const int64_t MB = 1024 * 1024; |
| 161 | int64_t result = ParseSystemPropertyBytes(heap_size_str); |
| 162 | if (result == -1) { |
| 163 | // We should consider not exposing these values if they are not reliable. |
| 164 | LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str; |
| 165 | result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6; |
| 166 | } |
| 167 | result = std::min<int64_t>(std::max<int64_t>(16 * MB, result), 512 * MB) / MB; |
| 168 | return static_cast<int>(result); |
| 169 | } |
| 170 | |
| 171 | std::string HardwareManufacturerName() { |
| 172 | char device_model_str[PROP_VALUE_MAX]; |
| 173 | __system_property_get("ro.product.manufacturer", device_model_str); |
| 174 | return std::string(device_model_str); |
| 175 | } |
| 176 | |
| 177 | } // anonymous namespace |
| 178 | |
| 179 | namespace base { |
| 180 | |
| 181 | std::string SysInfo::HardwareModelName() { |
| 182 | char device_model_str[PROP_VALUE_MAX]; |
| 183 | __system_property_get("ro.product.model", device_model_str); |
| 184 | return std::string(device_model_str); |
| 185 | } |
| 186 | |
| 187 | std::string SysInfo::OperatingSystemName() { |
| 188 | return "Android"; |
| 189 | } |
| 190 | |
| 191 | std::string SysInfo::OperatingSystemVersion() { |
| 192 | std::string version_string; |
| 193 | int32_t major, minor, bugfix; |
| 194 | GetOsVersionStringAndNumbers(&version_string, &major, &minor, &bugfix); |
| 195 | return version_string; |
| 196 | } |
| 197 | |
| 198 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 199 | int32_t* minor_version, |
| 200 | int32_t* bugfix_version) { |
| 201 | std::string version_string; |
| 202 | GetOsVersionStringAndNumbers(&version_string, major_version, minor_version, |
| 203 | bugfix_version); |
| 204 | } |
| 205 | |
| 206 | std::string SysInfo::GetAndroidBuildCodename() { |
| 207 | char os_version_codename_str[PROP_VALUE_MAX]; |
| 208 | __system_property_get("ro.build.version.codename", os_version_codename_str); |
| 209 | return std::string(os_version_codename_str); |
| 210 | } |
| 211 | |
| 212 | std::string SysInfo::GetAndroidBuildID() { |
| 213 | char os_build_id_str[PROP_VALUE_MAX]; |
| 214 | __system_property_get("ro.build.id", os_build_id_str); |
| 215 | return std::string(os_build_id_str); |
| 216 | } |
| 217 | |
| 218 | std::string SysInfo::GetAndroidHardwareEGL() { |
| 219 | char os_hardware_egl_str[PROP_VALUE_MAX]; |
| 220 | __system_property_get("ro.hardware.egl", os_hardware_egl_str); |
| 221 | return std::string(os_hardware_egl_str); |
| 222 | } |
| 223 | |
| 224 | int SysInfo::DalvikHeapSizeMB() { |
| 225 | static int heap_size = GetDalvikHeapSizeMB(); |
| 226 | return heap_size; |
| 227 | } |
| 228 | |
| 229 | int SysInfo::DalvikHeapGrowthLimitMB() { |
| 230 | static int heap_growth_limit = GetDalvikHeapGrowthLimitMB(); |
| 231 | return heap_growth_limit; |
| 232 | } |
| 233 | |
| 234 | static base::LazyInstance<base::internal::LazySysInfoValue< |
| 235 | bool, |
| 236 | android::SysUtils::IsLowEndDeviceFromJni>>::Leaky g_lazy_low_end_device = |
| 237 | LAZY_INSTANCE_INITIALIZER; |
| 238 | |
| 239 | bool SysInfo::IsLowEndDeviceImpl() { |
| 240 | // This code might be used in some environments |
| 241 | // which might not have a Java environment. |
| 242 | // Note that we need to call the Java version here. |
| 243 | // There exists a complete native implementation in |
| 244 | // sys_info.cc but calling that here would mean that |
| 245 | // the Java code and the native code would call different |
| 246 | // implementations which could give different results. |
| 247 | // Also the Java code cannot depend on the native code |
| 248 | // since it might not be loaded yet. |
| 249 | if (!base::android::IsVMInitialized()) |
| 250 | return false; |
| 251 | return g_lazy_low_end_device.Get().value(); |
| 252 | } |
| 253 | |
| 254 | // static |
| 255 | SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() { |
| 256 | HardwareInfo info; |
| 257 | info.manufacturer = HardwareManufacturerName(); |
| 258 | info.model = HardwareModelName(); |
| 259 | DCHECK(IsStringUTF8(info.manufacturer)); |
| 260 | DCHECK(IsStringUTF8(info.model)); |
| 261 | return info; |
| 262 | } |
| 263 | |
| 264 | } // namespace base |
| 265 | |