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