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