1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/properties.h"
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #if defined(__BIONIC__)
20*8f0ba417SAndroid Build Coastguard Worker #include <sys/system_properties.h>
21*8f0ba417SAndroid Build Coastguard Worker #endif
22*8f0ba417SAndroid Build Coastguard Worker
23*8f0ba417SAndroid Build Coastguard Worker #include <algorithm>
24*8f0ba417SAndroid Build Coastguard Worker #include <chrono>
25*8f0ba417SAndroid Build Coastguard Worker #include <limits>
26*8f0ba417SAndroid Build Coastguard Worker #include <set>
27*8f0ba417SAndroid Build Coastguard Worker #include <string>
28*8f0ba417SAndroid Build Coastguard Worker
29*8f0ba417SAndroid Build Coastguard Worker #include <android-base/parsebool.h>
30*8f0ba417SAndroid Build Coastguard Worker #include <android-base/parseint.h>
31*8f0ba417SAndroid Build Coastguard Worker #include <android-base/strings.h>
32*8f0ba417SAndroid Build Coastguard Worker #include <android-base/thread_annotations.h>
33*8f0ba417SAndroid Build Coastguard Worker
34*8f0ba417SAndroid Build Coastguard Worker #if !defined(__BIONIC__)
35*8f0ba417SAndroid Build Coastguard Worker
36*8f0ba417SAndroid Build Coastguard Worker // Here lies a rudimentary implementation of system properties for non-Bionic
37*8f0ba417SAndroid Build Coastguard Worker // platforms. We are using weak symbols here because we want to allow
38*8f0ba417SAndroid Build Coastguard Worker // downstream users of libbase to override with their own implementation.
39*8f0ba417SAndroid Build Coastguard Worker // For example, on Ravenwood (host-side testing for platform development)
40*8f0ba417SAndroid Build Coastguard Worker // we'd love to be able to fully control system properties exposed to tests,
41*8f0ba417SAndroid Build Coastguard Worker // so we reimplement the entire system properties API there.
42*8f0ba417SAndroid Build Coastguard Worker
43*8f0ba417SAndroid Build Coastguard Worker #if defined(__linux__)
44*8f0ba417SAndroid Build Coastguard Worker // Weak symbols are not supported on Windows, and to prevent unnecessary
45*8f0ba417SAndroid Build Coastguard Worker // complications, we strictly limit the use of weak symbols to Linux.
46*8f0ba417SAndroid Build Coastguard Worker #define SYSPROP_WEAK __attribute__((weak))
47*8f0ba417SAndroid Build Coastguard Worker #else
48*8f0ba417SAndroid Build Coastguard Worker #define SYSPROP_WEAK
49*8f0ba417SAndroid Build Coastguard Worker #endif
50*8f0ba417SAndroid Build Coastguard Worker
51*8f0ba417SAndroid Build Coastguard Worker #define PROP_VALUE_MAX 92
52*8f0ba417SAndroid Build Coastguard Worker
53*8f0ba417SAndroid Build Coastguard Worker struct prop_info {
54*8f0ba417SAndroid Build Coastguard Worker std::string key;
55*8f0ba417SAndroid Build Coastguard Worker mutable std::string value;
56*8f0ba417SAndroid Build Coastguard Worker mutable uint32_t serial;
57*8f0ba417SAndroid Build Coastguard Worker
prop_infoprop_info58*8f0ba417SAndroid Build Coastguard Worker prop_info(const char* key, const char* value) : key(key), value(value), serial(0) {}
59*8f0ba417SAndroid Build Coastguard Worker };
60*8f0ba417SAndroid Build Coastguard Worker
61*8f0ba417SAndroid Build Coastguard Worker struct prop_info_cmp {
62*8f0ba417SAndroid Build Coastguard Worker using is_transparent = void;
operator ()prop_info_cmp63*8f0ba417SAndroid Build Coastguard Worker bool operator()(const prop_info& lhs, const prop_info& rhs) { return lhs.key < rhs.key; }
operator ()prop_info_cmp64*8f0ba417SAndroid Build Coastguard Worker bool operator()(std::string_view lhs, const prop_info& rhs) { return lhs < rhs.key; }
operator ()prop_info_cmp65*8f0ba417SAndroid Build Coastguard Worker bool operator()(const prop_info& lhs, std::string_view rhs) { return lhs.key < rhs; }
66*8f0ba417SAndroid Build Coastguard Worker };
67*8f0ba417SAndroid Build Coastguard Worker
68*8f0ba417SAndroid Build Coastguard Worker static auto& g_properties_lock = *new std::mutex;
69*8f0ba417SAndroid Build Coastguard Worker static auto& g_properties GUARDED_BY(g_properties_lock) = *new std::set<prop_info, prop_info_cmp>;
70*8f0ba417SAndroid Build Coastguard Worker
__system_property_set(const char * key,const char * value)71*8f0ba417SAndroid Build Coastguard Worker SYSPROP_WEAK int __system_property_set(const char* key, const char* value) {
72*8f0ba417SAndroid Build Coastguard Worker if (key == nullptr || *key == '\0') return -1;
73*8f0ba417SAndroid Build Coastguard Worker if (value == nullptr) value = "";
74*8f0ba417SAndroid Build Coastguard Worker bool read_only = !strncmp(key, "ro.", 3);
75*8f0ba417SAndroid Build Coastguard Worker if (!read_only && strlen(value) >= PROP_VALUE_MAX) return -1;
76*8f0ba417SAndroid Build Coastguard Worker
77*8f0ba417SAndroid Build Coastguard Worker std::lock_guard lock(g_properties_lock);
78*8f0ba417SAndroid Build Coastguard Worker auto [it, success] = g_properties.emplace(key, value);
79*8f0ba417SAndroid Build Coastguard Worker if (read_only) return success ? 0 : -1;
80*8f0ba417SAndroid Build Coastguard Worker if (!success) {
81*8f0ba417SAndroid Build Coastguard Worker it->value = value;
82*8f0ba417SAndroid Build Coastguard Worker ++it->serial;
83*8f0ba417SAndroid Build Coastguard Worker }
84*8f0ba417SAndroid Build Coastguard Worker return 0;
85*8f0ba417SAndroid Build Coastguard Worker }
86*8f0ba417SAndroid Build Coastguard Worker
__system_property_get(const char * key,char * value)87*8f0ba417SAndroid Build Coastguard Worker SYSPROP_WEAK int __system_property_get(const char* key, char* value) {
88*8f0ba417SAndroid Build Coastguard Worker std::lock_guard lock(g_properties_lock);
89*8f0ba417SAndroid Build Coastguard Worker auto it = g_properties.find(key);
90*8f0ba417SAndroid Build Coastguard Worker if (it == g_properties.end()) {
91*8f0ba417SAndroid Build Coastguard Worker *value = '\0';
92*8f0ba417SAndroid Build Coastguard Worker return 0;
93*8f0ba417SAndroid Build Coastguard Worker }
94*8f0ba417SAndroid Build Coastguard Worker snprintf(value, PROP_VALUE_MAX, "%s", it->value.c_str());
95*8f0ba417SAndroid Build Coastguard Worker return strlen(value);
96*8f0ba417SAndroid Build Coastguard Worker }
97*8f0ba417SAndroid Build Coastguard Worker
__system_property_find(const char * key)98*8f0ba417SAndroid Build Coastguard Worker SYSPROP_WEAK const prop_info* __system_property_find(const char* key) {
99*8f0ba417SAndroid Build Coastguard Worker std::lock_guard lock(g_properties_lock);
100*8f0ba417SAndroid Build Coastguard Worker auto it = g_properties.find(key);
101*8f0ba417SAndroid Build Coastguard Worker if (it == g_properties.end()) {
102*8f0ba417SAndroid Build Coastguard Worker return nullptr;
103*8f0ba417SAndroid Build Coastguard Worker } else {
104*8f0ba417SAndroid Build Coastguard Worker return &*it;
105*8f0ba417SAndroid Build Coastguard Worker }
106*8f0ba417SAndroid Build Coastguard Worker }
107*8f0ba417SAndroid Build Coastguard Worker
__system_property_read_callback(const prop_info * pi,void (* callback)(void *,const char *,const char *,uint32_t),void * cookie)108*8f0ba417SAndroid Build Coastguard Worker SYSPROP_WEAK void __system_property_read_callback(const prop_info* pi,
109*8f0ba417SAndroid Build Coastguard Worker void (*callback)(void*, const char*, const char*,
110*8f0ba417SAndroid Build Coastguard Worker uint32_t),
111*8f0ba417SAndroid Build Coastguard Worker void* cookie) {
112*8f0ba417SAndroid Build Coastguard Worker std::lock_guard lock(g_properties_lock);
113*8f0ba417SAndroid Build Coastguard Worker callback(cookie, pi->key.c_str(), pi->value.c_str(), pi->serial);
114*8f0ba417SAndroid Build Coastguard Worker }
115*8f0ba417SAndroid Build Coastguard Worker
116*8f0ba417SAndroid Build Coastguard Worker #endif // __BIONIC__
117*8f0ba417SAndroid Build Coastguard Worker
118*8f0ba417SAndroid Build Coastguard Worker namespace android {
119*8f0ba417SAndroid Build Coastguard Worker namespace base {
120*8f0ba417SAndroid Build Coastguard Worker
GetBoolProperty(const std::string & key,bool default_value)121*8f0ba417SAndroid Build Coastguard Worker bool GetBoolProperty(const std::string& key, bool default_value) {
122*8f0ba417SAndroid Build Coastguard Worker switch (ParseBool(GetProperty(key, ""))) {
123*8f0ba417SAndroid Build Coastguard Worker case ParseBoolResult::kError:
124*8f0ba417SAndroid Build Coastguard Worker return default_value;
125*8f0ba417SAndroid Build Coastguard Worker case ParseBoolResult::kFalse:
126*8f0ba417SAndroid Build Coastguard Worker return false;
127*8f0ba417SAndroid Build Coastguard Worker case ParseBoolResult::kTrue:
128*8f0ba417SAndroid Build Coastguard Worker return true;
129*8f0ba417SAndroid Build Coastguard Worker }
130*8f0ba417SAndroid Build Coastguard Worker __builtin_unreachable();
131*8f0ba417SAndroid Build Coastguard Worker }
132*8f0ba417SAndroid Build Coastguard Worker
133*8f0ba417SAndroid Build Coastguard Worker template <typename T>
GetIntProperty(const std::string & key,T default_value,T min,T max)134*8f0ba417SAndroid Build Coastguard Worker T GetIntProperty(const std::string& key, T default_value, T min, T max) {
135*8f0ba417SAndroid Build Coastguard Worker T result;
136*8f0ba417SAndroid Build Coastguard Worker std::string value = GetProperty(key, "");
137*8f0ba417SAndroid Build Coastguard Worker if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result;
138*8f0ba417SAndroid Build Coastguard Worker return default_value;
139*8f0ba417SAndroid Build Coastguard Worker }
140*8f0ba417SAndroid Build Coastguard Worker
141*8f0ba417SAndroid Build Coastguard Worker template <typename T>
GetUintProperty(const std::string & key,T default_value,T max)142*8f0ba417SAndroid Build Coastguard Worker T GetUintProperty(const std::string& key, T default_value, T max) {
143*8f0ba417SAndroid Build Coastguard Worker T result;
144*8f0ba417SAndroid Build Coastguard Worker std::string value = GetProperty(key, "");
145*8f0ba417SAndroid Build Coastguard Worker if (!value.empty() && android::base::ParseUint(value, &result, max)) return result;
146*8f0ba417SAndroid Build Coastguard Worker return default_value;
147*8f0ba417SAndroid Build Coastguard Worker }
148*8f0ba417SAndroid Build Coastguard Worker
149*8f0ba417SAndroid Build Coastguard Worker template int8_t GetIntProperty(const std::string&, int8_t, int8_t, int8_t);
150*8f0ba417SAndroid Build Coastguard Worker template int16_t GetIntProperty(const std::string&, int16_t, int16_t, int16_t);
151*8f0ba417SAndroid Build Coastguard Worker template int32_t GetIntProperty(const std::string&, int32_t, int32_t, int32_t);
152*8f0ba417SAndroid Build Coastguard Worker template int64_t GetIntProperty(const std::string&, int64_t, int64_t, int64_t);
153*8f0ba417SAndroid Build Coastguard Worker
154*8f0ba417SAndroid Build Coastguard Worker template uint8_t GetUintProperty(const std::string&, uint8_t, uint8_t);
155*8f0ba417SAndroid Build Coastguard Worker template uint16_t GetUintProperty(const std::string&, uint16_t, uint16_t);
156*8f0ba417SAndroid Build Coastguard Worker template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t);
157*8f0ba417SAndroid Build Coastguard Worker template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t);
158*8f0ba417SAndroid Build Coastguard Worker
GetProperty(const std::string & key,const std::string & default_value)159*8f0ba417SAndroid Build Coastguard Worker std::string GetProperty(const std::string& key, const std::string& default_value) {
160*8f0ba417SAndroid Build Coastguard Worker std::string property_value;
161*8f0ba417SAndroid Build Coastguard Worker const prop_info* pi = __system_property_find(key.c_str());
162*8f0ba417SAndroid Build Coastguard Worker if (pi == nullptr) return default_value;
163*8f0ba417SAndroid Build Coastguard Worker
164*8f0ba417SAndroid Build Coastguard Worker __system_property_read_callback(
165*8f0ba417SAndroid Build Coastguard Worker pi,
166*8f0ba417SAndroid Build Coastguard Worker [](void* cookie, const char*, const char* value, unsigned) {
167*8f0ba417SAndroid Build Coastguard Worker auto property_value = reinterpret_cast<std::string*>(cookie);
168*8f0ba417SAndroid Build Coastguard Worker *property_value = value;
169*8f0ba417SAndroid Build Coastguard Worker },
170*8f0ba417SAndroid Build Coastguard Worker &property_value);
171*8f0ba417SAndroid Build Coastguard Worker // If the property exists but is empty, also return the default value.
172*8f0ba417SAndroid Build Coastguard Worker // Since we can't remove system properties, "empty" is traditionally
173*8f0ba417SAndroid Build Coastguard Worker // the same as "missing" (this was true for cutils' property_get).
174*8f0ba417SAndroid Build Coastguard Worker return property_value.empty() ? default_value : property_value;
175*8f0ba417SAndroid Build Coastguard Worker }
176*8f0ba417SAndroid Build Coastguard Worker
SetProperty(const std::string & key,const std::string & value)177*8f0ba417SAndroid Build Coastguard Worker bool SetProperty(const std::string& key, const std::string& value) {
178*8f0ba417SAndroid Build Coastguard Worker return (__system_property_set(key.c_str(), value.c_str()) == 0);
179*8f0ba417SAndroid Build Coastguard Worker }
180*8f0ba417SAndroid Build Coastguard Worker
181*8f0ba417SAndroid Build Coastguard Worker #if defined(__BIONIC__)
182*8f0ba417SAndroid Build Coastguard Worker
183*8f0ba417SAndroid Build Coastguard Worker struct WaitForPropertyData {
184*8f0ba417SAndroid Build Coastguard Worker bool done;
185*8f0ba417SAndroid Build Coastguard Worker const std::string* expected_value;
186*8f0ba417SAndroid Build Coastguard Worker unsigned last_read_serial;
187*8f0ba417SAndroid Build Coastguard Worker };
188*8f0ba417SAndroid Build Coastguard Worker
WaitForPropertyCallback(void * data_ptr,const char *,const char * value,unsigned serial)189*8f0ba417SAndroid Build Coastguard Worker static void WaitForPropertyCallback(void* data_ptr, const char*, const char* value, unsigned serial) {
190*8f0ba417SAndroid Build Coastguard Worker WaitForPropertyData* data = reinterpret_cast<WaitForPropertyData*>(data_ptr);
191*8f0ba417SAndroid Build Coastguard Worker if (*data->expected_value == value) {
192*8f0ba417SAndroid Build Coastguard Worker data->done = true;
193*8f0ba417SAndroid Build Coastguard Worker } else {
194*8f0ba417SAndroid Build Coastguard Worker data->last_read_serial = serial;
195*8f0ba417SAndroid Build Coastguard Worker }
196*8f0ba417SAndroid Build Coastguard Worker }
197*8f0ba417SAndroid Build Coastguard Worker
198*8f0ba417SAndroid Build Coastguard Worker // TODO: chrono_utils?
DurationToTimeSpec(timespec & ts,const std::chrono::milliseconds d)199*8f0ba417SAndroid Build Coastguard Worker static void DurationToTimeSpec(timespec& ts, const std::chrono::milliseconds d) {
200*8f0ba417SAndroid Build Coastguard Worker auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
201*8f0ba417SAndroid Build Coastguard Worker auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d - s);
202*8f0ba417SAndroid Build Coastguard Worker ts.tv_sec = std::min<std::chrono::seconds::rep>(s.count(), std::numeric_limits<time_t>::max());
203*8f0ba417SAndroid Build Coastguard Worker ts.tv_nsec = ns.count();
204*8f0ba417SAndroid Build Coastguard Worker }
205*8f0ba417SAndroid Build Coastguard Worker
206*8f0ba417SAndroid Build Coastguard Worker using AbsTime = std::chrono::time_point<std::chrono::steady_clock>;
207*8f0ba417SAndroid Build Coastguard Worker
UpdateTimeSpec(timespec & ts,std::chrono::milliseconds relative_timeout,const AbsTime & start_time)208*8f0ba417SAndroid Build Coastguard Worker static void UpdateTimeSpec(timespec& ts, std::chrono::milliseconds relative_timeout,
209*8f0ba417SAndroid Build Coastguard Worker const AbsTime& start_time) {
210*8f0ba417SAndroid Build Coastguard Worker auto now = std::chrono::steady_clock::now();
211*8f0ba417SAndroid Build Coastguard Worker auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
212*8f0ba417SAndroid Build Coastguard Worker if (time_elapsed >= relative_timeout) {
213*8f0ba417SAndroid Build Coastguard Worker ts = { 0, 0 };
214*8f0ba417SAndroid Build Coastguard Worker } else {
215*8f0ba417SAndroid Build Coastguard Worker auto remaining_timeout = relative_timeout - time_elapsed;
216*8f0ba417SAndroid Build Coastguard Worker DurationToTimeSpec(ts, remaining_timeout);
217*8f0ba417SAndroid Build Coastguard Worker }
218*8f0ba417SAndroid Build Coastguard Worker }
219*8f0ba417SAndroid Build Coastguard Worker
220*8f0ba417SAndroid Build Coastguard Worker // Waits for the system property `key` to be created.
221*8f0ba417SAndroid Build Coastguard Worker // Times out after `relative_timeout`.
222*8f0ba417SAndroid Build Coastguard Worker // Sets absolute_timeout which represents absolute time for the timeout.
223*8f0ba417SAndroid Build Coastguard Worker // Returns nullptr on timeout.
WaitForPropertyCreation(const std::string & key,const std::chrono::milliseconds & relative_timeout,const AbsTime & start_time)224*8f0ba417SAndroid Build Coastguard Worker static const prop_info* WaitForPropertyCreation(const std::string& key,
225*8f0ba417SAndroid Build Coastguard Worker const std::chrono::milliseconds& relative_timeout,
226*8f0ba417SAndroid Build Coastguard Worker const AbsTime& start_time) {
227*8f0ba417SAndroid Build Coastguard Worker // Find the property's prop_info*.
228*8f0ba417SAndroid Build Coastguard Worker const prop_info* pi;
229*8f0ba417SAndroid Build Coastguard Worker unsigned global_serial = 0;
230*8f0ba417SAndroid Build Coastguard Worker while ((pi = __system_property_find(key.c_str())) == nullptr) {
231*8f0ba417SAndroid Build Coastguard Worker // The property doesn't even exist yet.
232*8f0ba417SAndroid Build Coastguard Worker // Wait for a global change and then look again.
233*8f0ba417SAndroid Build Coastguard Worker timespec ts;
234*8f0ba417SAndroid Build Coastguard Worker UpdateTimeSpec(ts, relative_timeout, start_time);
235*8f0ba417SAndroid Build Coastguard Worker if (!__system_property_wait(nullptr, global_serial, &global_serial, &ts)) return nullptr;
236*8f0ba417SAndroid Build Coastguard Worker }
237*8f0ba417SAndroid Build Coastguard Worker return pi;
238*8f0ba417SAndroid Build Coastguard Worker }
239*8f0ba417SAndroid Build Coastguard Worker
WaitForProperty(const std::string & key,const std::string & expected_value,std::chrono::milliseconds relative_timeout)240*8f0ba417SAndroid Build Coastguard Worker bool WaitForProperty(const std::string& key, const std::string& expected_value,
241*8f0ba417SAndroid Build Coastguard Worker std::chrono::milliseconds relative_timeout) {
242*8f0ba417SAndroid Build Coastguard Worker auto start_time = std::chrono::steady_clock::now();
243*8f0ba417SAndroid Build Coastguard Worker const prop_info* pi = WaitForPropertyCreation(key, relative_timeout, start_time);
244*8f0ba417SAndroid Build Coastguard Worker if (pi == nullptr) return false;
245*8f0ba417SAndroid Build Coastguard Worker
246*8f0ba417SAndroid Build Coastguard Worker WaitForPropertyData data;
247*8f0ba417SAndroid Build Coastguard Worker data.expected_value = &expected_value;
248*8f0ba417SAndroid Build Coastguard Worker data.done = false;
249*8f0ba417SAndroid Build Coastguard Worker while (true) {
250*8f0ba417SAndroid Build Coastguard Worker timespec ts;
251*8f0ba417SAndroid Build Coastguard Worker // Check whether the property has the value we're looking for?
252*8f0ba417SAndroid Build Coastguard Worker __system_property_read_callback(pi, WaitForPropertyCallback, &data);
253*8f0ba417SAndroid Build Coastguard Worker if (data.done) return true;
254*8f0ba417SAndroid Build Coastguard Worker
255*8f0ba417SAndroid Build Coastguard Worker // It didn't, so wait for the property to change before checking again.
256*8f0ba417SAndroid Build Coastguard Worker UpdateTimeSpec(ts, relative_timeout, start_time);
257*8f0ba417SAndroid Build Coastguard Worker uint32_t unused;
258*8f0ba417SAndroid Build Coastguard Worker if (!__system_property_wait(pi, data.last_read_serial, &unused, &ts)) return false;
259*8f0ba417SAndroid Build Coastguard Worker }
260*8f0ba417SAndroid Build Coastguard Worker }
261*8f0ba417SAndroid Build Coastguard Worker
WaitForPropertyCreation(const std::string & key,std::chrono::milliseconds relative_timeout)262*8f0ba417SAndroid Build Coastguard Worker bool WaitForPropertyCreation(const std::string& key,
263*8f0ba417SAndroid Build Coastguard Worker std::chrono::milliseconds relative_timeout) {
264*8f0ba417SAndroid Build Coastguard Worker auto start_time = std::chrono::steady_clock::now();
265*8f0ba417SAndroid Build Coastguard Worker return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr);
266*8f0ba417SAndroid Build Coastguard Worker }
267*8f0ba417SAndroid Build Coastguard Worker
CachedProperty(std::string property_name)268*8f0ba417SAndroid Build Coastguard Worker CachedProperty::CachedProperty(std::string property_name)
269*8f0ba417SAndroid Build Coastguard Worker : property_name_(std::move(property_name)),
270*8f0ba417SAndroid Build Coastguard Worker prop_info_(nullptr),
271*8f0ba417SAndroid Build Coastguard Worker cached_area_serial_(0),
272*8f0ba417SAndroid Build Coastguard Worker cached_property_serial_(0),
273*8f0ba417SAndroid Build Coastguard Worker is_read_only_(android::base::StartsWith(property_name, "ro.")),
274*8f0ba417SAndroid Build Coastguard Worker read_only_property_(nullptr) {
275*8f0ba417SAndroid Build Coastguard Worker static_assert(sizeof(cached_value_) == PROP_VALUE_MAX);
276*8f0ba417SAndroid Build Coastguard Worker }
277*8f0ba417SAndroid Build Coastguard Worker
CachedProperty(const char * property_name)278*8f0ba417SAndroid Build Coastguard Worker CachedProperty::CachedProperty(const char* property_name)
279*8f0ba417SAndroid Build Coastguard Worker : CachedProperty(std::string(property_name)) {}
280*8f0ba417SAndroid Build Coastguard Worker
Get(bool * changed)281*8f0ba417SAndroid Build Coastguard Worker const char* CachedProperty::Get(bool* changed) {
282*8f0ba417SAndroid Build Coastguard Worker std::optional<uint32_t> initial_property_serial = cached_property_serial_;
283*8f0ba417SAndroid Build Coastguard Worker
284*8f0ba417SAndroid Build Coastguard Worker // Do we have a `struct prop_info` yet?
285*8f0ba417SAndroid Build Coastguard Worker if (prop_info_ == nullptr) {
286*8f0ba417SAndroid Build Coastguard Worker // `__system_property_find` is expensive, so only retry if a property
287*8f0ba417SAndroid Build Coastguard Worker // has been created since last time we checked.
288*8f0ba417SAndroid Build Coastguard Worker uint32_t property_area_serial = __system_property_area_serial();
289*8f0ba417SAndroid Build Coastguard Worker if (property_area_serial != cached_area_serial_) {
290*8f0ba417SAndroid Build Coastguard Worker prop_info_ = __system_property_find(property_name_.c_str());
291*8f0ba417SAndroid Build Coastguard Worker cached_area_serial_ = property_area_serial;
292*8f0ba417SAndroid Build Coastguard Worker }
293*8f0ba417SAndroid Build Coastguard Worker }
294*8f0ba417SAndroid Build Coastguard Worker
295*8f0ba417SAndroid Build Coastguard Worker if (prop_info_ != nullptr) {
296*8f0ba417SAndroid Build Coastguard Worker // Only bother re-reading the property if it's actually changed since last time.
297*8f0ba417SAndroid Build Coastguard Worker uint32_t property_serial = __system_property_serial(prop_info_);
298*8f0ba417SAndroid Build Coastguard Worker if (property_serial != cached_property_serial_) {
299*8f0ba417SAndroid Build Coastguard Worker __system_property_read_callback(
300*8f0ba417SAndroid Build Coastguard Worker prop_info_,
301*8f0ba417SAndroid Build Coastguard Worker [](void* data, const char*, const char* value, uint32_t serial) {
302*8f0ba417SAndroid Build Coastguard Worker CachedProperty* instance = reinterpret_cast<CachedProperty*>(data);
303*8f0ba417SAndroid Build Coastguard Worker instance->cached_property_serial_ = serial;
304*8f0ba417SAndroid Build Coastguard Worker // Read only properties can be larger than PROP_VALUE_MAX, but also never change value
305*8f0ba417SAndroid Build Coastguard Worker // or location, thus we return the pointer from the shared memory directly.
306*8f0ba417SAndroid Build Coastguard Worker if (instance->is_read_only_) {
307*8f0ba417SAndroid Build Coastguard Worker instance->read_only_property_ = value;
308*8f0ba417SAndroid Build Coastguard Worker } else {
309*8f0ba417SAndroid Build Coastguard Worker strlcpy(instance->cached_value_, value, PROP_VALUE_MAX);
310*8f0ba417SAndroid Build Coastguard Worker }
311*8f0ba417SAndroid Build Coastguard Worker },
312*8f0ba417SAndroid Build Coastguard Worker this);
313*8f0ba417SAndroid Build Coastguard Worker }
314*8f0ba417SAndroid Build Coastguard Worker }
315*8f0ba417SAndroid Build Coastguard Worker
316*8f0ba417SAndroid Build Coastguard Worker if (changed) {
317*8f0ba417SAndroid Build Coastguard Worker *changed = cached_property_serial_ != initial_property_serial;
318*8f0ba417SAndroid Build Coastguard Worker }
319*8f0ba417SAndroid Build Coastguard Worker
320*8f0ba417SAndroid Build Coastguard Worker if (is_read_only_) {
321*8f0ba417SAndroid Build Coastguard Worker return read_only_property_;
322*8f0ba417SAndroid Build Coastguard Worker } else {
323*8f0ba417SAndroid Build Coastguard Worker return cached_value_;
324*8f0ba417SAndroid Build Coastguard Worker }
325*8f0ba417SAndroid Build Coastguard Worker }
326*8f0ba417SAndroid Build Coastguard Worker
WaitForChange(std::chrono::milliseconds relative_timeout)327*8f0ba417SAndroid Build Coastguard Worker const char* CachedProperty::WaitForChange(std::chrono::milliseconds relative_timeout) {
328*8f0ba417SAndroid Build Coastguard Worker if (!prop_info_) {
329*8f0ba417SAndroid Build Coastguard Worker auto start_time = std::chrono::steady_clock::now();
330*8f0ba417SAndroid Build Coastguard Worker prop_info_ = WaitForPropertyCreation(property_name_, relative_timeout, start_time);
331*8f0ba417SAndroid Build Coastguard Worker if (!prop_info_) {
332*8f0ba417SAndroid Build Coastguard Worker return nullptr;
333*8f0ba417SAndroid Build Coastguard Worker }
334*8f0ba417SAndroid Build Coastguard Worker } else {
335*8f0ba417SAndroid Build Coastguard Worker timespec ts;
336*8f0ba417SAndroid Build Coastguard Worker DurationToTimeSpec(ts, relative_timeout);
337*8f0ba417SAndroid Build Coastguard Worker
338*8f0ba417SAndroid Build Coastguard Worker uint32_t old_serial = cached_property_serial_.value_or(0);
339*8f0ba417SAndroid Build Coastguard Worker uint32_t new_serial;
340*8f0ba417SAndroid Build Coastguard Worker if (!__system_property_wait(prop_info_, old_serial, &new_serial, &ts)) return nullptr;
341*8f0ba417SAndroid Build Coastguard Worker }
342*8f0ba417SAndroid Build Coastguard Worker
343*8f0ba417SAndroid Build Coastguard Worker return Get(nullptr);
344*8f0ba417SAndroid Build Coastguard Worker }
345*8f0ba417SAndroid Build Coastguard Worker
CachedBoolProperty(std::string property_name)346*8f0ba417SAndroid Build Coastguard Worker CachedBoolProperty::CachedBoolProperty(std::string property_name)
347*8f0ba417SAndroid Build Coastguard Worker : cached_parsed_property_(std::move(property_name),
348*8f0ba417SAndroid Build Coastguard Worker [](const char* value) -> std::optional<bool> {
349*8f0ba417SAndroid Build Coastguard Worker switch (ParseBool(value)) {
350*8f0ba417SAndroid Build Coastguard Worker case ParseBoolResult::kError:
351*8f0ba417SAndroid Build Coastguard Worker return std::nullopt;
352*8f0ba417SAndroid Build Coastguard Worker case ParseBoolResult::kFalse:
353*8f0ba417SAndroid Build Coastguard Worker return false;
354*8f0ba417SAndroid Build Coastguard Worker case ParseBoolResult::kTrue:
355*8f0ba417SAndroid Build Coastguard Worker return true;
356*8f0ba417SAndroid Build Coastguard Worker }
357*8f0ba417SAndroid Build Coastguard Worker }) {}
358*8f0ba417SAndroid Build Coastguard Worker
GetOptional()359*8f0ba417SAndroid Build Coastguard Worker std::optional<bool> CachedBoolProperty::GetOptional() {
360*8f0ba417SAndroid Build Coastguard Worker return cached_parsed_property_.Get();
361*8f0ba417SAndroid Build Coastguard Worker }
362*8f0ba417SAndroid Build Coastguard Worker
Get(bool default_value)363*8f0ba417SAndroid Build Coastguard Worker bool CachedBoolProperty::Get(bool default_value) {
364*8f0ba417SAndroid Build Coastguard Worker return GetOptional().value_or(default_value);
365*8f0ba417SAndroid Build Coastguard Worker }
366*8f0ba417SAndroid Build Coastguard Worker
367*8f0ba417SAndroid Build Coastguard Worker #endif
368*8f0ba417SAndroid Build Coastguard Worker
369*8f0ba417SAndroid Build Coastguard Worker } // namespace base
370*8f0ba417SAndroid Build Coastguard Worker } // namespace android
371