blob: 5c9ec7e9c59d850abfab36d5098ac4e6523e26a0 [file] [log] [blame]
Elliott Hughes1e88c8c2016-09-21 16:53:15 -07001/*
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 Hughesdc803122018-05-24 18:00:39 -070019#if defined(__BIONIC__)
20#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070021#include <sys/system_properties.h>
Elliott Hughesb30769a2017-02-10 19:02:51 -080022#include <sys/_system_properties.h>
Elliott Hughesdc803122018-05-24 18:00:39 -070023#endif
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070024
Elliott Hughes03edc9f2017-02-16 17:14:10 -080025#include <algorithm>
26#include <chrono>
yusukesd3b94042018-02-13 18:28:50 -080027#include <limits>
Elliott Hughesdc803122018-05-24 18:00:39 -070028#include <map>
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070029#include <string>
30
Daniel Colascione9e3cbb62019-11-14 00:48:36 -080031#include <android-base/parsebool.h>
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070032#include <android-base/parseint.h>
Josh Gao891e6da2020-02-26 14:57:20 -080033#include <android-base/strings.h>
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070034
35namespace android {
36namespace base {
37
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070038bool GetBoolProperty(const std::string& key, bool default_value) {
Daniel Colascione9e3cbb62019-11-14 00:48:36 -080039 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 Hughes1e88c8c2016-09-21 16:53:15 -070046 }
Daniel Colascione9e3cbb62019-11-14 00:48:36 -080047 __builtin_unreachable();
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070048}
49
50template <typename T>
51T GetIntProperty(const std::string& key, T default_value, T min, T max) {
52 T result;
53 std::string value = GetProperty(key, "");
Elliott Hughesda46b392016-10-11 17:09:00 -070054 if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result;
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070055 return default_value;
56}
57
58template <typename T>
59T GetUintProperty(const std::string& key, T default_value, T max) {
60 T result;
61 std::string value = GetProperty(key, "");
Elliott Hughesda46b392016-10-11 17:09:00 -070062 if (!value.empty() && android::base::ParseUint(value, &result, max)) return result;
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070063 return default_value;
64}
65
66template int8_t GetIntProperty(const std::string&, int8_t, int8_t, int8_t);
67template int16_t GetIntProperty(const std::string&, int16_t, int16_t, int16_t);
68template int32_t GetIntProperty(const std::string&, int32_t, int32_t, int32_t);
69template int64_t GetIntProperty(const std::string&, int64_t, int64_t, int64_t);
70
71template uint8_t GetUintProperty(const std::string&, uint8_t, uint8_t);
72template uint16_t GetUintProperty(const std::string&, uint16_t, uint16_t);
73template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t);
74template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t);
75
Elliott Hughesdc803122018-05-24 18:00:39 -070076#if !defined(__BIONIC__)
77static std::map<std::string, std::string>& g_properties = *new std::map<std::string, std::string>;
78static int __system_property_set(const char* key, const char* value) {
79 g_properties[key] = value;
80 return 0;
81}
82#endif
83
84std::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 Hughes1e88c8c2016-09-21 16:53:15 -0700107bool SetProperty(const std::string& key, const std::string& value) {
108 return (__system_property_set(key.c_str(), value.c_str()) == 0);
109}
110
Elliott Hughesdc803122018-05-24 18:00:39 -0700111#if defined(__BIONIC__)
112
Elliott Hughesb30769a2017-02-10 19:02:51 -0800113struct WaitForPropertyData {
114 bool done;
115 const std::string* expected_value;
116 unsigned last_read_serial;
117};
118
119static 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 Hughes03edc9f2017-02-16 17:14:10 -0800128// TODO: chrono_utils?
Tom Cherry3d572942017-03-24 16:52:29 -0700129static void DurationToTimeSpec(timespec& ts, const std::chrono::milliseconds d) {
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800130 auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
131 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d - s);
yusukesd3b94042018-02-13 18:28:50 -0800132 ts.tv_sec = std::min<std::chrono::seconds::rep>(s.count(), std::numeric_limits<time_t>::max());
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800133 ts.tv_nsec = ns.count();
134}
135
Keun-young Parke2d986d2017-02-27 13:23:50 -0800136using AbsTime = std::chrono::time_point<std::chrono::steady_clock>;
137
Tom Cherry3d572942017-03-24 16:52:29 -0700138static void UpdateTimeSpec(timespec& ts, std::chrono::milliseconds relative_timeout,
139 const AbsTime& start_time) {
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800140 auto now = std::chrono::steady_clock::now();
Tom Cherry3d572942017-03-24 16:52:29 -0700141 auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
142 if (time_elapsed >= relative_timeout) {
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800143 ts = { 0, 0 };
144 } else {
Tom Cherry3d572942017-03-24 16:52:29 -0700145 auto remaining_timeout = relative_timeout - time_elapsed;
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800146 DurationToTimeSpec(ts, remaining_timeout);
147 }
148}
149
Keun-young Parke2d986d2017-02-27 13:23:50 -0800150// 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.
154static const prop_info* WaitForPropertyCreation(const std::string& key,
155 const std::chrono::milliseconds& relative_timeout,
Tom Cherry3d572942017-03-24 16:52:29 -0700156 const AbsTime& start_time) {
Elliott Hughesb30769a2017-02-10 19:02:51 -0800157 // 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 Parke2d986d2017-02-27 13:23:50 -0800163 timespec ts;
Tom Cherry3d572942017-03-24 16:52:29 -0700164 UpdateTimeSpec(ts, relative_timeout, start_time);
Keun-young Parke2d986d2017-02-27 13:23:50 -0800165 if (!__system_property_wait(nullptr, global_serial, &global_serial, &ts)) return nullptr;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800166 }
Keun-young Parke2d986d2017-02-27 13:23:50 -0800167 return pi;
168}
169
Tom Cherry3d572942017-03-24 16:52:29 -0700170bool WaitForProperty(const std::string& key, const std::string& expected_value,
Keun-young Parke2d986d2017-02-27 13:23:50 -0800171 std::chrono::milliseconds relative_timeout) {
Tom Cherry3d572942017-03-24 16:52:29 -0700172 auto start_time = std::chrono::steady_clock::now();
173 const prop_info* pi = WaitForPropertyCreation(key, relative_timeout, start_time);
Keun-young Parke2d986d2017-02-27 13:23:50 -0800174 if (pi == nullptr) return false;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800175
176 WaitForPropertyData data;
177 data.expected_value = &expected_value;
178 data.done = false;
179 while (true) {
Keun-young Parke2d986d2017-02-27 13:23:50 -0800180 timespec ts;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800181 // Check whether the property has the value we're looking for?
182 __system_property_read_callback(pi, WaitForPropertyCallback, &data);
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800183 if (data.done) return true;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800184
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800185 // It didn't, so wait for the property to change before checking again.
Tom Cherry3d572942017-03-24 16:52:29 -0700186 UpdateTimeSpec(ts, relative_timeout, start_time);
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800187 uint32_t unused;
188 if (!__system_property_wait(pi, data.last_read_serial, &unused, &ts)) return false;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800189 }
190}
191
Keun-young Parke2d986d2017-02-27 13:23:50 -0800192bool WaitForPropertyCreation(const std::string& key,
193 std::chrono::milliseconds relative_timeout) {
Tom Cherry3d572942017-03-24 16:52:29 -0700194 auto start_time = std::chrono::steady_clock::now();
195 return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr);
Keun-young Parke2d986d2017-02-27 13:23:50 -0800196}
197
Josh Gao891e6da2020-02-26 14:57:20 -0800198CachedProperty::CachedProperty(const char* property_name)
199 : property_name_(property_name),
200 prop_info_(nullptr),
201 cached_area_serial_(0),
202 cached_property_serial_(0),
203 is_read_only_(android::base::StartsWith(property_name, "ro.")),
204 read_only_property_(nullptr) {
205 static_assert(sizeof(cached_value_) == PROP_VALUE_MAX);
206}
207
208const char* CachedProperty::Get(bool* changed) {
209 std::optional<uint32_t> initial_property_serial_ = cached_property_serial_;
210
211 // Do we have a `struct prop_info` yet?
212 if (prop_info_ == nullptr) {
213 // `__system_property_find` is expensive, so only retry if a property
214 // has been created since last time we checked.
215 uint32_t property_area_serial = __system_property_area_serial();
216 if (property_area_serial != cached_area_serial_) {
217 prop_info_ = __system_property_find(property_name_.c_str());
218 cached_area_serial_ = property_area_serial;
219 }
220 }
221
222 if (prop_info_ != nullptr) {
223 // Only bother re-reading the property if it's actually changed since last time.
224 uint32_t property_serial = __system_property_serial(prop_info_);
225 if (property_serial != cached_property_serial_) {
226 __system_property_read_callback(
227 prop_info_,
228 [](void* data, const char*, const char* value, uint32_t serial) {
229 CachedProperty* instance = reinterpret_cast<CachedProperty*>(data);
230 instance->cached_property_serial_ = serial;
231 // Read only properties can be larger than PROP_VALUE_MAX, but also never change value
232 // or location, thus we return the pointer from the shared memory directly.
233 if (instance->is_read_only_) {
234 instance->read_only_property_ = value;
235 } else {
236 strlcpy(instance->cached_value_, value, PROP_VALUE_MAX);
237 }
238 },
239 this);
240 }
241 }
242
243 if (changed) {
244 *changed = cached_property_serial_ != initial_property_serial_;
245 }
246
247 if (is_read_only_) {
248 return read_only_property_;
249 } else {
250 return cached_value_;
251 }
252}
253
Elliott Hughesdc803122018-05-24 18:00:39 -0700254#endif
255
Elliott Hughes1e88c8c2016-09-21 16:53:15 -0700256} // namespace base
257} // namespace android