| 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> | 
|  | 33 |  | 
|  | 34 | namespace android { | 
|  | 35 | namespace base { | 
|  | 36 |  | 
| Elliott Hughes | 1e88c8c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 37 | bool GetBoolProperty(const std::string& key, bool default_value) { | 
| Daniel Colascione | 9e3cbb6 | 2019-11-14 00:48:36 -0800 | [diff] [blame] | 38 | switch (ParseBool(GetProperty(key, ""))) { | 
|  | 39 | case ParseBoolResult::kError: | 
|  | 40 | return default_value; | 
|  | 41 | case ParseBoolResult::kFalse: | 
|  | 42 | return false; | 
|  | 43 | case ParseBoolResult::kTrue: | 
|  | 44 | return true; | 
| Elliott Hughes | 1e88c8c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 45 | } | 
| Daniel Colascione | 9e3cbb6 | 2019-11-14 00:48:36 -0800 | [diff] [blame] | 46 | __builtin_unreachable(); | 
| Elliott Hughes | 1e88c8c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
|  | 49 | template <typename T> | 
|  | 50 | T GetIntProperty(const std::string& key, T default_value, T min, T max) { | 
|  | 51 | T result; | 
|  | 52 | std::string value = GetProperty(key, ""); | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 53 | if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result; | 
| Elliott Hughes | 1e88c8c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 54 | return default_value; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | template <typename T> | 
|  | 58 | T GetUintProperty(const std::string& key, T default_value, T max) { | 
|  | 59 | T result; | 
|  | 60 | std::string value = GetProperty(key, ""); | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 61 | if (!value.empty() && android::base::ParseUint(value, &result, max)) return result; | 
| Elliott Hughes | 1e88c8c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 62 | return default_value; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | template int8_t GetIntProperty(const std::string&, int8_t, int8_t, int8_t); | 
|  | 66 | template int16_t GetIntProperty(const std::string&, int16_t, int16_t, int16_t); | 
|  | 67 | template int32_t GetIntProperty(const std::string&, int32_t, int32_t, int32_t); | 
|  | 68 | template int64_t GetIntProperty(const std::string&, int64_t, int64_t, int64_t); | 
|  | 69 |  | 
|  | 70 | template uint8_t GetUintProperty(const std::string&, uint8_t, uint8_t); | 
|  | 71 | template uint16_t GetUintProperty(const std::string&, uint16_t, uint16_t); | 
|  | 72 | template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t); | 
|  | 73 | template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t); | 
|  | 74 |  | 
| Elliott Hughes | dc80312 | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 75 | #if !defined(__BIONIC__) | 
|  | 76 | static std::map<std::string, std::string>& g_properties = *new std::map<std::string, std::string>; | 
|  | 77 | static int __system_property_set(const char* key, const char* value) { | 
|  | 78 | g_properties[key] = value; | 
|  | 79 | return 0; | 
|  | 80 | } | 
|  | 81 | #endif | 
|  | 82 |  | 
|  | 83 | std::string GetProperty(const std::string& key, const std::string& default_value) { | 
|  | 84 | std::string property_value; | 
|  | 85 | #if defined(__BIONIC__) | 
|  | 86 | const prop_info* pi = __system_property_find(key.c_str()); | 
|  | 87 | if (pi == nullptr) return default_value; | 
|  | 88 |  | 
|  | 89 | __system_property_read_callback(pi, | 
|  | 90 | [](void* cookie, const char*, const char* value, unsigned) { | 
|  | 91 | auto property_value = reinterpret_cast<std::string*>(cookie); | 
|  | 92 | *property_value = value; | 
|  | 93 | }, | 
|  | 94 | &property_value); | 
|  | 95 | #else | 
|  | 96 | auto it = g_properties.find(key); | 
|  | 97 | if (it == g_properties.end()) return default_value; | 
|  | 98 | property_value = it->second; | 
|  | 99 | #endif | 
|  | 100 | // If the property exists but is empty, also return the default value. | 
|  | 101 | // Since we can't remove system properties, "empty" is traditionally | 
|  | 102 | // the same as "missing" (this was true for cutils' property_get). | 
|  | 103 | return property_value.empty() ? default_value : property_value; | 
|  | 104 | } | 
|  | 105 |  | 
| Elliott Hughes | 1e88c8c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 106 | bool SetProperty(const std::string& key, const std::string& value) { | 
|  | 107 | return (__system_property_set(key.c_str(), value.c_str()) == 0); | 
|  | 108 | } | 
|  | 109 |  | 
| Elliott Hughes | dc80312 | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 110 | #if defined(__BIONIC__) | 
|  | 111 |  | 
| Elliott Hughes | b30769a | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 112 | struct WaitForPropertyData { | 
|  | 113 | bool done; | 
|  | 114 | const std::string* expected_value; | 
|  | 115 | unsigned last_read_serial; | 
|  | 116 | }; | 
|  | 117 |  | 
|  | 118 | static void WaitForPropertyCallback(void* data_ptr, const char*, const char* value, unsigned serial) { | 
|  | 119 | WaitForPropertyData* data = reinterpret_cast<WaitForPropertyData*>(data_ptr); | 
|  | 120 | if (*data->expected_value == value) { | 
|  | 121 | data->done = true; | 
|  | 122 | } else { | 
|  | 123 | data->last_read_serial = serial; | 
|  | 124 | } | 
|  | 125 | } | 
|  | 126 |  | 
| Elliott Hughes | 03edc9f | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 127 | // TODO: chrono_utils? | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 128 | static void DurationToTimeSpec(timespec& ts, const std::chrono::milliseconds d) { | 
| Elliott Hughes | 03edc9f | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 129 | auto s = std::chrono::duration_cast<std::chrono::seconds>(d); | 
|  | 130 | auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d - s); | 
| yusukes | d3b9404 | 2018-02-13 18:28:50 -0800 | [diff] [blame] | 131 | 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] | 132 | ts.tv_nsec = ns.count(); | 
|  | 133 | } | 
|  | 134 |  | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 135 | // TODO: boot_clock? | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 136 | using AbsTime = std::chrono::time_point<std::chrono::steady_clock>; | 
|  | 137 |  | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 138 | static void UpdateTimeSpec(timespec& ts, std::chrono::milliseconds relative_timeout, | 
|  | 139 | const AbsTime& start_time) { | 
| Elliott Hughes | 03edc9f | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 140 | auto now = std::chrono::steady_clock::now(); | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 141 | auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time); | 
|  | 142 | if (time_elapsed >= relative_timeout) { | 
| Elliott Hughes | 03edc9f | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 143 | ts = { 0, 0 }; | 
|  | 144 | } else { | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 145 | auto remaining_timeout = relative_timeout - time_elapsed; | 
| Elliott Hughes | 03edc9f | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 146 | DurationToTimeSpec(ts, remaining_timeout); | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 150 | // Waits for the system property `key` to be created. | 
|  | 151 | // Times out after `relative_timeout`. | 
|  | 152 | // Sets absolute_timeout which represents absolute time for the timeout. | 
|  | 153 | // Returns nullptr on timeout. | 
|  | 154 | static const prop_info* WaitForPropertyCreation(const std::string& key, | 
|  | 155 | const std::chrono::milliseconds& relative_timeout, | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 156 | const AbsTime& start_time) { | 
| Elliott Hughes | b30769a | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 157 | // Find the property's prop_info*. | 
|  | 158 | const prop_info* pi; | 
|  | 159 | unsigned global_serial = 0; | 
|  | 160 | while ((pi = __system_property_find(key.c_str())) == nullptr) { | 
|  | 161 | // The property doesn't even exist yet. | 
|  | 162 | // Wait for a global change and then look again. | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 163 | timespec ts; | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 164 | UpdateTimeSpec(ts, relative_timeout, start_time); | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 165 | if (!__system_property_wait(nullptr, global_serial, &global_serial, &ts)) return nullptr; | 
| Elliott Hughes | b30769a | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 166 | } | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 167 | return pi; | 
|  | 168 | } | 
|  | 169 |  | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 170 | bool WaitForProperty(const std::string& key, const std::string& expected_value, | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 171 | std::chrono::milliseconds relative_timeout) { | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 172 | auto start_time = std::chrono::steady_clock::now(); | 
|  | 173 | const prop_info* pi = WaitForPropertyCreation(key, relative_timeout, start_time); | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 174 | if (pi == nullptr) return false; | 
| Elliott Hughes | b30769a | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 175 |  | 
|  | 176 | WaitForPropertyData data; | 
|  | 177 | data.expected_value = &expected_value; | 
|  | 178 | data.done = false; | 
|  | 179 | while (true) { | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 180 | timespec ts; | 
| Elliott Hughes | b30769a | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 181 | // Check whether the property has the value we're looking for? | 
|  | 182 | __system_property_read_callback(pi, WaitForPropertyCallback, &data); | 
| Elliott Hughes | 03edc9f | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 183 | if (data.done) return true; | 
| Elliott Hughes | b30769a | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 184 |  | 
| Elliott Hughes | 03edc9f | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 185 | // 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] | 186 | UpdateTimeSpec(ts, relative_timeout, start_time); | 
| Elliott Hughes | 03edc9f | 2017-02-16 17:14:10 -0800 | [diff] [blame] | 187 | uint32_t unused; | 
|  | 188 | 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] | 189 | } | 
|  | 190 | } | 
|  | 191 |  | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 192 | bool WaitForPropertyCreation(const std::string& key, | 
|  | 193 | std::chrono::milliseconds relative_timeout) { | 
| Tom Cherry | 3d57294 | 2017-03-24 16:52:29 -0700 | [diff] [blame] | 194 | auto start_time = std::chrono::steady_clock::now(); | 
|  | 195 | return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr); | 
| Keun-young Park | e2d986d | 2017-02-27 13:23:50 -0800 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
| Elliott Hughes | dc80312 | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 198 | #endif | 
|  | 199 |  | 
| Elliott Hughes | 1e88c8c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 200 | }  // namespace base | 
|  | 201 | }  // namespace android |