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