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 #pragma once
18 
19 #include <sys/cdefs.h>
20 
21 #include <chrono>
22 #include <limits>
23 #include <optional>
24 #include <string>
25 
26 struct prop_info;
27 
28 namespace android {
29 namespace base {
30 
31 // Returns the current value of the system property `key`,
32 // or `default_value` if the property is empty or doesn't exist.
33 std::string GetProperty(const std::string& key, const std::string& default_value);
34 
35 // Returns true if the system property `key` has the value "1", "y", "yes", "on", or "true",
36 // false for "0", "n", "no", "off", or "false", or `default_value` otherwise.
37 bool GetBoolProperty(const std::string& key, bool default_value);
38 
39 // Returns the signed integer corresponding to the system property `key`.
40 // If the property is empty, doesn't exist, doesn't have an integer value, or is outside
41 // the optional bounds, returns `default_value`.
42 template <typename T> T GetIntProperty(const std::string& key,
43                                        T default_value,
44                                        T min = std::numeric_limits<T>::min(),
45                                        T max = std::numeric_limits<T>::max());
46 
47 // Returns the unsigned integer corresponding to the system property `key`.
48 // If the property is empty, doesn't exist, doesn't have an integer value, or is outside
49 // the optional bound, returns `default_value`.
50 template <typename T> T GetUintProperty(const std::string& key,
51                                         T default_value,
52                                         T max = std::numeric_limits<T>::max());
53 
54 // Sets the system property `key` to `value`.
55 bool SetProperty(const std::string& key, const std::string& value);
56 
57 // Waits for the system property `key` to have the value `expected_value`.
58 // Times out after `relative_timeout`.
59 // Returns true on success, false on timeout.
60 #if defined(__BIONIC__)
61 bool WaitForProperty(const std::string& key, const std::string& expected_value,
62                      std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max());
63 #endif
64 
65 // Waits for the system property `key` to be created.
66 // Times out after `relative_timeout`.
67 // Returns true on success, false on timeout.
68 #if defined(__BIONIC__)
69 bool WaitForPropertyCreation(const std::string& key, std::chrono::milliseconds relative_timeout =
70                                                          std::chrono::milliseconds::max());
71 #endif
72 
73 #if defined(__BIONIC__) && __cplusplus >= 201703L
74 // Cached system property lookup. For code that needs to read the same property multiple times,
75 // this class helps optimize those lookups.
76 class CachedProperty {
77  public:
78   explicit CachedProperty(const char* property_name);
79 
80   // Returns the current value of the underlying system property as cheaply as possible.
81   // The returned pointer is valid until the next call to Get. Because most callers are going
82   // to want to parse the string returned here and cached that as well, this function performs
83   // no locking, and is completely thread unsafe. It is the caller's responsibility to provide a
84   // lock for thread-safety.
85   //
86   // Note: *changed can be set to true even if the contents of the property remain the same.
87   const char* Get(bool* changed = nullptr);
88 
89  private:
90   std::string property_name_;
91   const prop_info* prop_info_;
92   std::optional<uint32_t> cached_area_serial_;
93   std::optional<uint32_t> cached_property_serial_;
94   char cached_value_[92];
95   bool is_read_only_;
96   const char* read_only_property_;
97 };
98 #endif
99 
100 } // namespace base
101 } // namespace android
102 
103 #if !defined(__BIONIC__)
104 /** Implementation detail. */
105 extern "C" int __system_property_set(const char*, const char*);
106 /** Implementation detail. */
107 extern "C" int __system_property_get(const char*, char*);
108 #endif
109