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