blob: ca8e96fc47e121b546c4071282bd1aad2bcfcc8a [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
Elliott Hughesb30769a2017-02-10 19:02:51 -080017#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
18
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070019#include "android-base/properties.h"
20
21#include <sys/system_properties.h>
Elliott Hughesb30769a2017-02-10 19:02:51 -080022#include <sys/_system_properties.h>
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070023
Elliott Hughes03edc9f2017-02-16 17:14:10 -080024#include <algorithm>
25#include <chrono>
yusukesd3b94042018-02-13 18:28:50 -080026#include <limits>
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070027#include <string>
28
29#include <android-base/parseint.h>
30
Elliott Hughes03edc9f2017-02-16 17:14:10 -080031using namespace std::chrono_literals;
32
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070033namespace android {
34namespace base {
35
36std::string GetProperty(const std::string& key, const std::string& default_value) {
37 const prop_info* pi = __system_property_find(key.c_str());
38 if (pi == nullptr) return default_value;
39
Tom Cherry31121ca2017-10-10 13:30:57 -070040 std::string property_value;
41 __system_property_read_callback(pi,
42 [](void* cookie, const char*, const char* value, unsigned) {
43 auto property_value = reinterpret_cast<std::string*>(cookie);
44 *property_value = value;
45 },
46 &property_value);
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070047
48 // If the property exists but is empty, also return the default value.
49 // Since we can't remove system properties, "empty" is traditionally
50 // the same as "missing" (this was true for cutils' property_get).
Tom Cherry31121ca2017-10-10 13:30:57 -070051 return property_value.empty() ? default_value : property_value;
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070052}
53
54bool GetBoolProperty(const std::string& key, bool default_value) {
55 std::string value = GetProperty(key, "");
56 if (value == "1" || value == "y" || value == "yes" || value == "on" || value == "true") {
57 return true;
58 } else if (value == "0" || value == "n" || value == "no" || value == "off" || value == "false") {
59 return false;
60 }
61 return default_value;
62}
63
64template <typename T>
65T GetIntProperty(const std::string& key, T default_value, T min, T max) {
66 T result;
67 std::string value = GetProperty(key, "");
Elliott Hughesda46b392016-10-11 17:09:00 -070068 if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result;
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070069 return default_value;
70}
71
72template <typename T>
73T GetUintProperty(const std::string& key, T default_value, T max) {
74 T result;
75 std::string value = GetProperty(key, "");
Elliott Hughesda46b392016-10-11 17:09:00 -070076 if (!value.empty() && android::base::ParseUint(value, &result, max)) return result;
Elliott Hughes1e88c8c2016-09-21 16:53:15 -070077 return default_value;
78}
79
80template int8_t GetIntProperty(const std::string&, int8_t, int8_t, int8_t);
81template int16_t GetIntProperty(const std::string&, int16_t, int16_t, int16_t);
82template int32_t GetIntProperty(const std::string&, int32_t, int32_t, int32_t);
83template int64_t GetIntProperty(const std::string&, int64_t, int64_t, int64_t);
84
85template uint8_t GetUintProperty(const std::string&, uint8_t, uint8_t);
86template uint16_t GetUintProperty(const std::string&, uint16_t, uint16_t);
87template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t);
88template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t);
89
90bool SetProperty(const std::string& key, const std::string& value) {
91 return (__system_property_set(key.c_str(), value.c_str()) == 0);
92}
93
Elliott Hughesb30769a2017-02-10 19:02:51 -080094struct WaitForPropertyData {
95 bool done;
96 const std::string* expected_value;
97 unsigned last_read_serial;
98};
99
100static void WaitForPropertyCallback(void* data_ptr, const char*, const char* value, unsigned serial) {
101 WaitForPropertyData* data = reinterpret_cast<WaitForPropertyData*>(data_ptr);
102 if (*data->expected_value == value) {
103 data->done = true;
104 } else {
105 data->last_read_serial = serial;
106 }
107}
108
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800109// TODO: chrono_utils?
Tom Cherry3d572942017-03-24 16:52:29 -0700110static void DurationToTimeSpec(timespec& ts, const std::chrono::milliseconds d) {
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800111 auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
112 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d - s);
yusukesd3b94042018-02-13 18:28:50 -0800113 ts.tv_sec = std::min<std::chrono::seconds::rep>(s.count(), std::numeric_limits<time_t>::max());
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800114 ts.tv_nsec = ns.count();
115}
116
Tom Cherry3d572942017-03-24 16:52:29 -0700117// TODO: boot_clock?
Keun-young Parke2d986d2017-02-27 13:23:50 -0800118using AbsTime = std::chrono::time_point<std::chrono::steady_clock>;
119
Tom Cherry3d572942017-03-24 16:52:29 -0700120static void UpdateTimeSpec(timespec& ts, std::chrono::milliseconds relative_timeout,
121 const AbsTime& start_time) {
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800122 auto now = std::chrono::steady_clock::now();
Tom Cherry3d572942017-03-24 16:52:29 -0700123 auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
124 if (time_elapsed >= relative_timeout) {
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800125 ts = { 0, 0 };
126 } else {
Tom Cherry3d572942017-03-24 16:52:29 -0700127 auto remaining_timeout = relative_timeout - time_elapsed;
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800128 DurationToTimeSpec(ts, remaining_timeout);
129 }
130}
131
Keun-young Parke2d986d2017-02-27 13:23:50 -0800132// Waits for the system property `key` to be created.
133// Times out after `relative_timeout`.
134// Sets absolute_timeout which represents absolute time for the timeout.
135// Returns nullptr on timeout.
136static const prop_info* WaitForPropertyCreation(const std::string& key,
137 const std::chrono::milliseconds& relative_timeout,
Tom Cherry3d572942017-03-24 16:52:29 -0700138 const AbsTime& start_time) {
Elliott Hughesb30769a2017-02-10 19:02:51 -0800139 // Find the property's prop_info*.
140 const prop_info* pi;
141 unsigned global_serial = 0;
142 while ((pi = __system_property_find(key.c_str())) == nullptr) {
143 // The property doesn't even exist yet.
144 // Wait for a global change and then look again.
Keun-young Parke2d986d2017-02-27 13:23:50 -0800145 timespec ts;
Tom Cherry3d572942017-03-24 16:52:29 -0700146 UpdateTimeSpec(ts, relative_timeout, start_time);
Keun-young Parke2d986d2017-02-27 13:23:50 -0800147 if (!__system_property_wait(nullptr, global_serial, &global_serial, &ts)) return nullptr;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800148 }
Keun-young Parke2d986d2017-02-27 13:23:50 -0800149 return pi;
150}
151
Tom Cherry3d572942017-03-24 16:52:29 -0700152bool WaitForProperty(const std::string& key, const std::string& expected_value,
Keun-young Parke2d986d2017-02-27 13:23:50 -0800153 std::chrono::milliseconds relative_timeout) {
Tom Cherry3d572942017-03-24 16:52:29 -0700154 auto start_time = std::chrono::steady_clock::now();
155 const prop_info* pi = WaitForPropertyCreation(key, relative_timeout, start_time);
Keun-young Parke2d986d2017-02-27 13:23:50 -0800156 if (pi == nullptr) return false;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800157
158 WaitForPropertyData data;
159 data.expected_value = &expected_value;
160 data.done = false;
161 while (true) {
Keun-young Parke2d986d2017-02-27 13:23:50 -0800162 timespec ts;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800163 // Check whether the property has the value we're looking for?
164 __system_property_read_callback(pi, WaitForPropertyCallback, &data);
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800165 if (data.done) return true;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800166
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800167 // It didn't, so wait for the property to change before checking again.
Tom Cherry3d572942017-03-24 16:52:29 -0700168 UpdateTimeSpec(ts, relative_timeout, start_time);
Elliott Hughes03edc9f2017-02-16 17:14:10 -0800169 uint32_t unused;
170 if (!__system_property_wait(pi, data.last_read_serial, &unused, &ts)) return false;
Elliott Hughesb30769a2017-02-10 19:02:51 -0800171 }
172}
173
Keun-young Parke2d986d2017-02-27 13:23:50 -0800174bool WaitForPropertyCreation(const std::string& key,
175 std::chrono::milliseconds relative_timeout) {
Tom Cherry3d572942017-03-24 16:52:29 -0700176 auto start_time = std::chrono::steady_clock::now();
177 return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr);
Keun-young Parke2d986d2017-02-27 13:23:50 -0800178}
179
Elliott Hughes1e88c8c2016-09-21 16:53:15 -0700180} // namespace base
181} // namespace android