Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "android-base/properties.h" |
| 18 | |
Elliott Hughes | 083c2fe | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 19 | #if defined(__BIONIC__) |
| 20 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 21 | #include <sys/system_properties.h> |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 22 | #include <sys/_system_properties.h> |
Elliott Hughes | 083c2fe | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 23 | #endif |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | #include <chrono> |
yusukes | 57eb8d9 | 2018-02-13 18:28:50 -0800 | [diff] [blame] | 27 | #include <limits> |
Elliott Hughes | 083c2fe | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 28 | #include <map> |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 29 | #include <string> |
| 30 | |
Daniel Colascione | 57fceb9 | 2019-11-14 00:48:36 -0800 | [diff] [blame] | 31 | #include <android-base/parsebool.h> |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 32 | #include <android-base/parseint.h> |
Josh Gao | eb63a7e | 2020-02-26 14:57:20 -0800 | [diff] [blame] | 33 | #include <android-base/strings.h> |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | c928585 | 2020-05-20 08:03:50 -0700 | [diff] [blame] | 35 | #if !defined(__BIONIC__) |
| 36 | |
| 37 | #define PROP_VALUE_MAX 92 |
| 38 | |
| 39 | static std::map<std::string, std::string>& g_properties = *new std::map<std::string, std::string>; |
| 40 | |
| 41 | int __system_property_set(const char* key, const char* value) { |
| 42 | if (key == nullptr || *key == '\0') return -1; |
| 43 | if (value == nullptr) value = ""; |
| 44 | |
| 45 | bool read_only = !strncmp(key, "ro.", 3); |
| 46 | if (read_only) { |
| 47 | const auto [it, success] = g_properties.insert({key, value}); |
| 48 | return success ? 0 : -1; |
| 49 | } |
| 50 | |
| 51 | if (strlen(value) >= 92) return -1; |
| 52 | g_properties[key] = value; |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | int __system_property_get(const char* key, char* value) { |
| 57 | auto it = g_properties.find(key); |
| 58 | if (it == g_properties.end()) { |
| 59 | *value = '\0'; |
| 60 | return 0; |
| 61 | } |
| 62 | snprintf(value, PROP_VALUE_MAX, "%s", it->second.c_str()); |
| 63 | return strlen(value); |
| 64 | } |
| 65 | |
| 66 | #endif |
| 67 | |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 68 | namespace android { |
| 69 | namespace base { |
| 70 | |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 71 | bool GetBoolProperty(const std::string& key, bool default_value) { |
Daniel Colascione | 57fceb9 | 2019-11-14 00:48:36 -0800 | [diff] [blame] | 72 | switch (ParseBool(GetProperty(key, ""))) { |
| 73 | case ParseBoolResult::kError: |
| 74 | return default_value; |
| 75 | case ParseBoolResult::kFalse: |
| 76 | return false; |
| 77 | case ParseBoolResult::kTrue: |
| 78 | return true; |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 79 | } |
Daniel Colascione | 57fceb9 | 2019-11-14 00:48:36 -0800 | [diff] [blame] | 80 | __builtin_unreachable(); |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | template <typename T> |
| 84 | T GetIntProperty(const std::string& key, T default_value, T min, T max) { |
| 85 | T result; |
| 86 | std::string value = GetProperty(key, ""); |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 87 | if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result; |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 88 | return default_value; |
| 89 | } |
| 90 | |
| 91 | template <typename T> |
| 92 | T GetUintProperty(const std::string& key, T default_value, T max) { |
| 93 | T result; |
| 94 | std::string value = GetProperty(key, ""); |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 95 | if (!value.empty() && android::base::ParseUint(value, &result, max)) return result; |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 96 | return default_value; |
| 97 | } |
| 98 | |
| 99 | template int8_t GetIntProperty(const std::string&, int8_t, int8_t, int8_t); |
| 100 | template int16_t GetIntProperty(const std::string&, int16_t, int16_t, int16_t); |
| 101 | template int32_t GetIntProperty(const std::string&, int32_t, int32_t, int32_t); |
| 102 | template int64_t GetIntProperty(const std::string&, int64_t, int64_t, int64_t); |
| 103 | |
| 104 | template uint8_t GetUintProperty(const std::string&, uint8_t, uint8_t); |
| 105 | template uint16_t GetUintProperty(const std::string&, uint16_t, uint16_t); |
| 106 | template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t); |
| 107 | template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t); |
| 108 | |
Elliott Hughes | 083c2fe | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 109 | std::string GetProperty(const std::string& key, const std::string& default_value) { |
| 110 | std::string property_value; |
| 111 | #if defined(__BIONIC__) |
| 112 | const prop_info* pi = __system_property_find(key.c_str()); |
| 113 | if (pi == nullptr) return default_value; |
| 114 | |
| 115 | __system_property_read_callback(pi, |
| 116 | [](void* cookie, const char*, const char* value, unsigned) { |
| 117 | auto property_value = reinterpret_cast<std::string*>(cookie); |
| 118 | *property_value = value; |
| 119 | }, |
| 120 | &property_value); |
| 121 | #else |
Elliott Hughes | c928585 | 2020-05-20 08:03:50 -0700 | [diff] [blame] | 122 | // TODO: implement host __system_property_find()/__system_property_read_callback()? |
Elliott Hughes | 083c2fe | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 123 | auto it = g_properties.find(key); |
| 124 | if (it == g_properties.end()) return default_value; |
| 125 | property_value = it->second; |
| 126 | #endif |
| 127 | // If the property exists but is empty, also return the default value. |
| 128 | // Since we can't remove system properties, "empty" is traditionally |
| 129 | // the same as "missing" (this was true for cutils' property_get). |
| 130 | return property_value.empty() ? default_value : property_value; |
| 131 | } |
| 132 | |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 133 | bool SetProperty(const std::string& key, const std::string& value) { |
| 134 | return (__system_property_set(key.c_str(), value.c_str()) == 0); |
| 135 | } |
| 136 | |
Elliott Hughes | 083c2fe | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 137 | #if defined(__BIONIC__) |
| 138 | |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 139 | struct WaitForPropertyData { |
| 140 | bool done; |
| 141 | const std::string* expected_value; |
| 142 | unsigned last_read_serial; |
| 143 | }; |
| 144 | |
| 145 | static void WaitForPropertyCallback(void* data_ptr, const char*, const char* value, unsigned serial) { |
| 146 | WaitForPropertyData* data = reinterpret_cast<WaitForPropertyData*>(data_ptr); |
| 147 | if (*data->expected_value == value) { |
| 148 | data->done = true; |
| 149 | } else { |
| 150 | data->last_read_serial = serial; |
| 151 | } |
| 152 | } |
| 153 | |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 154 | // TODO: chrono_utils? |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 155 | static void DurationToTimeSpec(timespec& ts, const std::chrono::milliseconds d) { |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 156 | auto s = std::chrono::duration_cast<std::chrono::seconds>(d); |
| 157 | auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d - s); |
yusukes | 57eb8d9 | 2018-02-13 18:28:50 -0800 | [diff] [blame] | 158 | ts.tv_sec = std::min<std::chrono::seconds::rep>(s.count(), std::numeric_limits<time_t>::max()); |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 159 | ts.tv_nsec = ns.count(); |
| 160 | } |
| 161 | |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 162 | using AbsTime = std::chrono::time_point<std::chrono::steady_clock>; |
| 163 | |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 164 | static void UpdateTimeSpec(timespec& ts, std::chrono::milliseconds relative_timeout, |
| 165 | const AbsTime& start_time) { |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 166 | auto now = std::chrono::steady_clock::now(); |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 167 | auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time); |
| 168 | if (time_elapsed >= relative_timeout) { |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 169 | ts = { 0, 0 }; |
| 170 | } else { |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 171 | auto remaining_timeout = relative_timeout - time_elapsed; |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 172 | DurationToTimeSpec(ts, remaining_timeout); |
| 173 | } |
| 174 | } |
| 175 | |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 176 | // Waits for the system property `key` to be created. |
| 177 | // Times out after `relative_timeout`. |
| 178 | // Sets absolute_timeout which represents absolute time for the timeout. |
| 179 | // Returns nullptr on timeout. |
| 180 | static const prop_info* WaitForPropertyCreation(const std::string& key, |
| 181 | const std::chrono::milliseconds& relative_timeout, |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 182 | const AbsTime& start_time) { |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 183 | // Find the property's prop_info*. |
| 184 | const prop_info* pi; |
| 185 | unsigned global_serial = 0; |
| 186 | while ((pi = __system_property_find(key.c_str())) == nullptr) { |
| 187 | // The property doesn't even exist yet. |
| 188 | // Wait for a global change and then look again. |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 189 | timespec ts; |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 190 | UpdateTimeSpec(ts, relative_timeout, start_time); |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 191 | if (!__system_property_wait(nullptr, global_serial, &global_serial, &ts)) return nullptr; |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 192 | } |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 193 | return pi; |
| 194 | } |
| 195 | |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 196 | bool WaitForProperty(const std::string& key, const std::string& expected_value, |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 197 | std::chrono::milliseconds relative_timeout) { |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 198 | auto start_time = std::chrono::steady_clock::now(); |
| 199 | const prop_info* pi = WaitForPropertyCreation(key, relative_timeout, start_time); |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 200 | if (pi == nullptr) return false; |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 201 | |
| 202 | WaitForPropertyData data; |
| 203 | data.expected_value = &expected_value; |
| 204 | data.done = false; |
| 205 | while (true) { |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 206 | timespec ts; |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 207 | // Check whether the property has the value we're looking for? |
| 208 | __system_property_read_callback(pi, WaitForPropertyCallback, &data); |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 209 | if (data.done) return true; |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 210 | |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 211 | // It didn't, so wait for the property to change before checking again. |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 212 | UpdateTimeSpec(ts, relative_timeout, start_time); |
Elliott Hughes | 81bcb21 | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 213 | uint32_t unused; |
| 214 | if (!__system_property_wait(pi, data.last_read_serial, &unused, &ts)) return false; |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 218 | bool WaitForPropertyCreation(const std::string& key, |
| 219 | std::chrono::milliseconds relative_timeout) { |
Tom Cherry | d0516ca | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 220 | auto start_time = std::chrono::steady_clock::now(); |
| 221 | return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr); |
Keun-young Park | 16f7051 | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Josh Gao | eb63a7e | 2020-02-26 14:57:20 -0800 | [diff] [blame] | 224 | CachedProperty::CachedProperty(const char* property_name) |
| 225 | : property_name_(property_name), |
| 226 | prop_info_(nullptr), |
| 227 | cached_area_serial_(0), |
| 228 | cached_property_serial_(0), |
| 229 | is_read_only_(android::base::StartsWith(property_name, "ro.")), |
| 230 | read_only_property_(nullptr) { |
| 231 | static_assert(sizeof(cached_value_) == PROP_VALUE_MAX); |
| 232 | } |
| 233 | |
| 234 | const char* CachedProperty::Get(bool* changed) { |
| 235 | std::optional<uint32_t> initial_property_serial_ = cached_property_serial_; |
| 236 | |
| 237 | // Do we have a `struct prop_info` yet? |
| 238 | if (prop_info_ == nullptr) { |
| 239 | // `__system_property_find` is expensive, so only retry if a property |
| 240 | // has been created since last time we checked. |
| 241 | uint32_t property_area_serial = __system_property_area_serial(); |
| 242 | if (property_area_serial != cached_area_serial_) { |
| 243 | prop_info_ = __system_property_find(property_name_.c_str()); |
| 244 | cached_area_serial_ = property_area_serial; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (prop_info_ != nullptr) { |
| 249 | // Only bother re-reading the property if it's actually changed since last time. |
| 250 | uint32_t property_serial = __system_property_serial(prop_info_); |
| 251 | if (property_serial != cached_property_serial_) { |
| 252 | __system_property_read_callback( |
| 253 | prop_info_, |
| 254 | [](void* data, const char*, const char* value, uint32_t serial) { |
| 255 | CachedProperty* instance = reinterpret_cast<CachedProperty*>(data); |
| 256 | instance->cached_property_serial_ = serial; |
| 257 | // Read only properties can be larger than PROP_VALUE_MAX, but also never change value |
| 258 | // or location, thus we return the pointer from the shared memory directly. |
| 259 | if (instance->is_read_only_) { |
| 260 | instance->read_only_property_ = value; |
| 261 | } else { |
| 262 | strlcpy(instance->cached_value_, value, PROP_VALUE_MAX); |
| 263 | } |
| 264 | }, |
| 265 | this); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (changed) { |
| 270 | *changed = cached_property_serial_ != initial_property_serial_; |
| 271 | } |
| 272 | |
| 273 | if (is_read_only_) { |
| 274 | return read_only_property_; |
| 275 | } else { |
| 276 | return cached_value_; |
| 277 | } |
| 278 | } |
| 279 | |
Elliott Hughes | 083c2fe | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 280 | #endif |
| 281 | |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 282 | } // namespace base |
| 283 | } // namespace android |