summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/Android.bp6
-rw-r--r--base/include/android-base/properties.h8
-rw-r--r--base/properties.cpp59
-rw-r--r--base/properties_test.cpp28
-rw-r--r--init/action.cpp7
-rw-r--r--init/action_parser.cpp5
-rw-r--r--init/host_init_stubs.cpp14
-rw-r--r--init/host_init_stubs.h10
-rw-r--r--init/service.cpp3
-rw-r--r--init/util.cpp3
-rw-r--r--libprocessgroup/processgroup.cpp9
11 files changed, 77 insertions, 75 deletions
diff --git a/base/Android.bp b/base/Android.bp
index 71bf1540c..3d80d9719 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -48,6 +48,7 @@ cc_defaults {
48 "file.cpp", 48 "file.cpp",
49 "logging.cpp", 49 "logging.cpp",
50 "parsenetaddress.cpp", 50 "parsenetaddress.cpp",
51 "properties.cpp",
51 "quick_exit.cpp", 52 "quick_exit.cpp",
52 "stringprintf.cpp", 53 "stringprintf.cpp",
53 "strings.cpp", 54 "strings.cpp",
@@ -58,9 +59,6 @@ cc_defaults {
58 shared_libs: ["liblog"], 59 shared_libs: ["liblog"],
59 target: { 60 target: {
60 android: { 61 android: {
61 srcs: [
62 "properties.cpp",
63 ],
64 sanitize: { 62 sanitize: {
65 misc_undefined: ["integer"], 63 misc_undefined: ["integer"],
66 }, 64 },
@@ -130,6 +128,7 @@ cc_test {
130 "parsedouble_test.cpp", 128 "parsedouble_test.cpp",
131 "parseint_test.cpp", 129 "parseint_test.cpp",
132 "parsenetaddress_test.cpp", 130 "parsenetaddress_test.cpp",
131 "properties_test.cpp",
133 "quick_exit_test.cpp", 132 "quick_exit_test.cpp",
134 "scopeguard_test.cpp", 133 "scopeguard_test.cpp",
135 "stringprintf_test.cpp", 134 "stringprintf_test.cpp",
@@ -139,7 +138,6 @@ cc_test {
139 ], 138 ],
140 target: { 139 target: {
141 android: { 140 android: {
142 srcs: ["properties_test.cpp"],
143 sanitize: { 141 sanitize: {
144 misc_undefined: ["integer"], 142 misc_undefined: ["integer"],
145 }, 143 },
diff --git a/base/include/android-base/properties.h b/base/include/android-base/properties.h
index 041586c2c..3a051435c 100644
--- a/base/include/android-base/properties.h
+++ b/base/include/android-base/properties.h
@@ -19,10 +19,6 @@
19 19
20#include <sys/cdefs.h> 20#include <sys/cdefs.h>
21 21
22#if !defined(__BIONIC__)
23#error Only bionic supports system properties.
24#endif
25
26#include <chrono> 22#include <chrono>
27#include <limits> 23#include <limits>
28#include <string> 24#include <string>
@@ -62,14 +58,18 @@ bool SetProperty(const std::string& key, const std::string& value);
62// Waits for the system property `key` to have the value `expected_value`. 58// Waits for the system property `key` to have the value `expected_value`.
63// Times out after `relative_timeout`. 59// Times out after `relative_timeout`.
64// Returns true on success, false on timeout. 60// Returns true on success, false on timeout.
61#if defined(__BIONIC__)
65bool WaitForProperty(const std::string& key, const std::string& expected_value, 62bool WaitForProperty(const std::string& key, const std::string& expected_value,
66 std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max()); 63 std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max());
64#endif
67 65
68// Waits for the system property `key` to be created. 66// Waits for the system property `key` to be created.
69// Times out after `relative_timeout`. 67// Times out after `relative_timeout`.
70// Returns true on success, false on timeout. 68// Returns true on success, false on timeout.
69#if defined(__BIONIC__)
71bool WaitForPropertyCreation(const std::string& key, std::chrono::milliseconds relative_timeout = 70bool WaitForPropertyCreation(const std::string& key, std::chrono::milliseconds relative_timeout =
72 std::chrono::milliseconds::max()); 71 std::chrono::milliseconds::max());
72#endif
73 73
74} // namespace base 74} // namespace base
75} // namespace android 75} // namespace android
diff --git a/base/properties.cpp b/base/properties.cpp
index 6cf43f9e2..d5a5918ce 100644
--- a/base/properties.cpp
+++ b/base/properties.cpp
@@ -14,16 +14,18 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
18
19#include "android-base/properties.h" 17#include "android-base/properties.h"
20 18
19#if defined(__BIONIC__)
20#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
21#include <sys/system_properties.h> 21#include <sys/system_properties.h>
22#include <sys/_system_properties.h> 22#include <sys/_system_properties.h>
23#endif
23 24
24#include <algorithm> 25#include <algorithm>
25#include <chrono> 26#include <chrono>
26#include <limits> 27#include <limits>
28#include <map>
27#include <string> 29#include <string>
28 30
29#include <android-base/parseint.h> 31#include <android-base/parseint.h>
@@ -31,24 +33,6 @@
31namespace android { 33namespace android {
32namespace base { 34namespace base {
33 35
34std::string GetProperty(const std::string& key, const std::string& default_value) {
35 const prop_info* pi = __system_property_find(key.c_str());
36 if (pi == nullptr) return default_value;
37
38 std::string property_value;
39 __system_property_read_callback(pi,
40 [](void* cookie, const char*, const char* value, unsigned) {
41 auto property_value = reinterpret_cast<std::string*>(cookie);
42 *property_value = value;
43 },
44 &property_value);
45
46 // If the property exists but is empty, also return the default value.
47 // Since we can't remove system properties, "empty" is traditionally
48 // the same as "missing" (this was true for cutils' property_get).
49 return property_value.empty() ? default_value : property_value;
50}
51
52bool GetBoolProperty(const std::string& key, bool default_value) { 36bool GetBoolProperty(const std::string& key, bool default_value) {
53 std::string value = GetProperty(key, ""); 37 std::string value = GetProperty(key, "");
54 if (value == "1" || value == "y" || value == "yes" || value == "on" || value == "true") { 38 if (value == "1" || value == "y" || value == "yes" || value == "on" || value == "true") {
@@ -85,10 +69,43 @@ template uint16_t GetUintProperty(const std::string&, uint16_t, uint16_t);
85template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t); 69template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t);
86template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t); 70template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t);
87 71
72#if !defined(__BIONIC__)
73static std::map<std::string, std::string>& g_properties = *new std::map<std::string, std::string>;
74static int __system_property_set(const char* key, const char* value) {
75 g_properties[key] = value;
76 return 0;
77}
78#endif
79
80std::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
88bool SetProperty(const std::string& key, const std::string& value) { 103bool SetProperty(const std::string& key, const std::string& value) {
89 return (__system_property_set(key.c_str(), value.c_str()) == 0); 104 return (__system_property_set(key.c_str(), value.c_str()) == 0);
90} 105}
91 106
107#if defined(__BIONIC__)
108
92struct WaitForPropertyData { 109struct WaitForPropertyData {
93 bool done; 110 bool done;
94 const std::string* expected_value; 111 const std::string* expected_value;
@@ -175,5 +192,7 @@ bool WaitForPropertyCreation(const std::string& key,
175 return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr); 192 return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr);
176} 193}
177 194
195#endif
196
178} // namespace base 197} // namespace base
179} // namespace android 198} // namespace android
diff --git a/base/properties_test.cpp b/base/properties_test.cpp
index de5f3dcfa..e7d4880c2 100644
--- a/base/properties_test.cpp
+++ b/base/properties_test.cpp
@@ -23,7 +23,9 @@
23#include <string> 23#include <string>
24#include <thread> 24#include <thread>
25 25
26using namespace std::chrono_literals; 26#if !defined(_WIN32)
27using namespace std::literals;
28#endif
27 29
28TEST(properties, smoke) { 30TEST(properties, smoke) {
29 android::base::SetProperty("debug.libbase.property_test", "hello"); 31 android::base::SetProperty("debug.libbase.property_test", "hello");
@@ -126,6 +128,7 @@ TEST(properties, GetUintProperty_uint32_t) { CheckGetUintProperty<uint32_t>(); }
126TEST(properties, GetUintProperty_uint64_t) { CheckGetUintProperty<uint64_t>(); } 128TEST(properties, GetUintProperty_uint64_t) { CheckGetUintProperty<uint64_t>(); }
127 129
128TEST(properties, WaitForProperty) { 130TEST(properties, WaitForProperty) {
131#if defined(__BIONIC__)
129 std::atomic<bool> flag{false}; 132 std::atomic<bool> flag{false};
130 std::thread thread([&]() { 133 std::thread thread([&]() {
131 std::this_thread::sleep_for(100ms); 134 std::this_thread::sleep_for(100ms);
@@ -138,9 +141,13 @@ TEST(properties, WaitForProperty) {
138 flag = true; 141 flag = true;
139 ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", 1s)); 142 ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", 1s));
140 thread.join(); 143 thread.join();
144#else
145 GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
146#endif
141} 147}
142 148
143TEST(properties, WaitForProperty_timeout) { 149TEST(properties, WaitForProperty_timeout) {
150#if defined(__BIONIC__)
144 auto t0 = std::chrono::steady_clock::now(); 151 auto t0 = std::chrono::steady_clock::now();
145 ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_timeout_test", "a", 152 ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_timeout_test", "a",
146 200ms)); 153 200ms));
@@ -149,9 +156,13 @@ TEST(properties, WaitForProperty_timeout) {
149 ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms); 156 ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms);
150 // Upper bounds on timing are inherently flaky, but let's try... 157 // Upper bounds on timing are inherently flaky, but let's try...
151 ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms); 158 ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms);
159#else
160 GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
161#endif
152} 162}
153 163
154TEST(properties, WaitForProperty_MaxTimeout) { 164TEST(properties, WaitForProperty_MaxTimeout) {
165#if defined(__BIONIC__)
155 std::atomic<bool> flag{false}; 166 std::atomic<bool> flag{false};
156 std::thread thread([&]() { 167 std::thread thread([&]() {
157 android::base::SetProperty("debug.libbase.WaitForProperty_test", "a"); 168 android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
@@ -165,9 +176,13 @@ TEST(properties, WaitForProperty_MaxTimeout) {
165 // Test that this does not immediately return false due to overflow issues with the timeout. 176 // Test that this does not immediately return false due to overflow issues with the timeout.
166 ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b")); 177 ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b"));
167 thread.join(); 178 thread.join();
179#else
180 GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
181#endif
168} 182}
169 183
170TEST(properties, WaitForProperty_NegativeTimeout) { 184TEST(properties, WaitForProperty_NegativeTimeout) {
185#if defined(__BIONIC__)
171 std::atomic<bool> flag{false}; 186 std::atomic<bool> flag{false};
172 std::thread thread([&]() { 187 std::thread thread([&]() {
173 android::base::SetProperty("debug.libbase.WaitForProperty_test", "a"); 188 android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
@@ -181,9 +196,13 @@ TEST(properties, WaitForProperty_NegativeTimeout) {
181 // Assert that this immediately returns with a negative timeout 196 // Assert that this immediately returns with a negative timeout
182 ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", -100ms)); 197 ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", -100ms));
183 thread.join(); 198 thread.join();
199#else
200 GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
201#endif
184} 202}
185 203
186TEST(properties, WaitForPropertyCreation) { 204TEST(properties, WaitForPropertyCreation) {
205#if defined(__BIONIC__)
187 std::thread thread([&]() { 206 std::thread thread([&]() {
188 std::this_thread::sleep_for(100ms); 207 std::this_thread::sleep_for(100ms);
189 android::base::SetProperty("debug.libbase.WaitForPropertyCreation_test", "a"); 208 android::base::SetProperty("debug.libbase.WaitForPropertyCreation_test", "a");
@@ -192,9 +211,13 @@ TEST(properties, WaitForPropertyCreation) {
192 ASSERT_TRUE(android::base::WaitForPropertyCreation( 211 ASSERT_TRUE(android::base::WaitForPropertyCreation(
193 "debug.libbase.WaitForPropertyCreation_test", 1s)); 212 "debug.libbase.WaitForPropertyCreation_test", 1s));
194 thread.join(); 213 thread.join();
214#else
215 GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
216#endif
195} 217}
196 218
197TEST(properties, WaitForPropertyCreation_timeout) { 219TEST(properties, WaitForPropertyCreation_timeout) {
220#if defined(__BIONIC__)
198 auto t0 = std::chrono::steady_clock::now(); 221 auto t0 = std::chrono::steady_clock::now();
199 ASSERT_FALSE(android::base::WaitForPropertyCreation( 222 ASSERT_FALSE(android::base::WaitForPropertyCreation(
200 "debug.libbase.WaitForPropertyCreation_timeout_test", 200ms)); 223 "debug.libbase.WaitForPropertyCreation_timeout_test", 200ms));
@@ -203,4 +226,7 @@ TEST(properties, WaitForPropertyCreation_timeout) {
203 ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms); 226 ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms);
204 // Upper bounds on timing are inherently flaky, but let's try... 227 // Upper bounds on timing are inherently flaky, but let's try...
205 ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms); 228 ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms);
229#else
230 GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
231#endif
206} 232}
diff --git a/init/action.cpp b/init/action.cpp
index f782b51c1..11335ca14 100644
--- a/init/action.cpp
+++ b/init/action.cpp
@@ -18,16 +18,11 @@
18 18
19#include <android-base/chrono_utils.h> 19#include <android-base/chrono_utils.h>
20#include <android-base/logging.h> 20#include <android-base/logging.h>
21#include <android-base/properties.h>
21#include <android-base/strings.h> 22#include <android-base/strings.h>
22 23
23#include "util.h" 24#include "util.h"
24 25
25#if defined(__ANDROID__)
26#include <android-base/properties.h>
27#else
28#include "host_init_stubs.h"
29#endif
30
31using android::base::Join; 26using android::base::Join;
32 27
33namespace android { 28namespace android {
diff --git a/init/action_parser.cpp b/init/action_parser.cpp
index a2c96718b..f9b96eb5e 100644
--- a/init/action_parser.cpp
+++ b/init/action_parser.cpp
@@ -16,13 +16,12 @@
16 16
17#include "action_parser.h" 17#include "action_parser.h"
18 18
19#include <android-base/properties.h>
19#include <android-base/strings.h> 20#include <android-base/strings.h>
20 21
21#include "stable_properties.h" 22#include "stable_properties.h"
22 23
23#if defined(__ANDROID__) 24#if !defined(__ANDROID__)
24#include <android-base/properties.h>
25#else
26#include "host_init_stubs.h" 25#include "host_init_stubs.h"
27#endif 26#endif
28 27
diff --git a/init/host_init_stubs.cpp b/init/host_init_stubs.cpp
index 4451ac8b9..c40a25435 100644
--- a/init/host_init_stubs.cpp
+++ b/init/host_init_stubs.cpp
@@ -22,20 +22,6 @@ int setgroups(size_t __size, const gid_t* __list) {
22} 22}
23 23
24namespace android { 24namespace android {
25namespace base {
26
27std::string GetProperty(const std::string&, const std::string& default_value) {
28 return default_value;
29}
30
31bool GetBoolProperty(const std::string&, bool default_value) {
32 return default_value;
33}
34
35} // namespace base
36} // namespace android
37
38namespace android {
39namespace init { 25namespace init {
40 26
41// init.h 27// init.h
diff --git a/init/host_init_stubs.h b/init/host_init_stubs.h
index bb241af9b..f0e1f0729 100644
--- a/init/host_init_stubs.h
+++ b/init/host_init_stubs.h
@@ -29,16 +29,6 @@
29// unistd.h 29// unistd.h
30int setgroups(size_t __size, const gid_t* __list); 30int setgroups(size_t __size, const gid_t* __list);
31 31
32// android-base/properties.h
33namespace android {
34namespace base {
35
36std::string GetProperty(const std::string& key, const std::string& default_value);
37bool GetBoolProperty(const std::string& key, bool default_value);
38
39} // namespace base
40} // namespace android
41
42namespace android { 32namespace android {
43namespace init { 33namespace init {
44 34
diff --git a/init/service.cpp b/init/service.cpp
index 5778a93b0..a6987358f 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -33,6 +33,7 @@
33#include <android-base/file.h> 33#include <android-base/file.h>
34#include <android-base/logging.h> 34#include <android-base/logging.h>
35#include <android-base/parseint.h> 35#include <android-base/parseint.h>
36#include <android-base/properties.h>
36#include <android-base/stringprintf.h> 37#include <android-base/stringprintf.h>
37#include <android-base/strings.h> 38#include <android-base/strings.h>
38#include <android-base/unique_fd.h> 39#include <android-base/unique_fd.h>
@@ -47,8 +48,6 @@
47#if defined(__ANDROID__) 48#if defined(__ANDROID__)
48#include <sys/system_properties.h> 49#include <sys/system_properties.h>
49 50
50#include <android-base/properties.h>
51
52#include "init.h" 51#include "init.h"
53#include "property_service.h" 52#include "property_service.h"
54#else 53#else
diff --git a/init/util.cpp b/init/util.cpp
index 4455b2eb1..5f2b87d71 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -33,6 +33,7 @@
33 33
34#include <android-base/file.h> 34#include <android-base/file.h>
35#include <android-base/logging.h> 35#include <android-base/logging.h>
36#include <android-base/properties.h>
36#include <android-base/stringprintf.h> 37#include <android-base/stringprintf.h>
37#include <android-base/strings.h> 38#include <android-base/strings.h>
38#include <android-base/unique_fd.h> 39#include <android-base/unique_fd.h>
@@ -43,8 +44,6 @@
43#include "reboot.h" 44#include "reboot.h"
44 45
45#if defined(__ANDROID__) 46#if defined(__ANDROID__)
46#include <android-base/properties.h>
47
48#include "selinux.h" 47#include "selinux.h"
49#else 48#else
50#include "host_init_stubs.h" 49#include "host_init_stubs.h"
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
index 58295fadb..1cebb5daa 100644
--- a/libprocessgroup/processgroup.cpp
+++ b/libprocessgroup/processgroup.cpp
@@ -39,18 +39,14 @@
39 39
40#include <android-base/file.h> 40#include <android-base/file.h>
41#include <android-base/logging.h> 41#include <android-base/logging.h>
42#ifdef __ANDROID__
43#include <android-base/properties.h> 42#include <android-base/properties.h>
44#endif
45#include <android-base/stringprintf.h> 43#include <android-base/stringprintf.h>
46#include <android-base/strings.h> 44#include <android-base/strings.h>
47#include <private/android_filesystem_config.h> 45#include <private/android_filesystem_config.h>
48 46
49#include <processgroup/processgroup.h> 47#include <processgroup/processgroup.h>
50 48
51#ifdef __ANDROID__
52using android::base::GetBoolProperty; 49using android::base::GetBoolProperty;
53#endif
54using android::base::StartsWith; 50using android::base::StartsWith;
55using android::base::StringPrintf; 51using android::base::StringPrintf;
56using android::base::WriteStringToFile; 52using android::base::WriteStringToFile;
@@ -68,15 +64,10 @@ std::once_flag init_path_flag;
68static const std::string& GetCgroupRootPath() { 64static const std::string& GetCgroupRootPath() {
69 static std::string cgroup_root_path; 65 static std::string cgroup_root_path;
70 std::call_once(init_path_flag, [&]() { 66 std::call_once(init_path_flag, [&]() {
71#ifdef __ANDROID__
72 // low-ram devices use per-app memcg by default, unlike high-end ones 67 // low-ram devices use per-app memcg by default, unlike high-end ones
73 bool low_ram_device = GetBoolProperty("ro.config.low_ram", false); 68 bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
74 bool per_app_memcg = 69 bool per_app_memcg =
75 GetBoolProperty("ro.config.per_app_memcg", low_ram_device); 70 GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
76#else
77 // host does not support Android properties
78 bool per_app_memcg = false;
79#endif
80 if (per_app_memcg) { 71 if (per_app_memcg) {
81 // Check if mem cgroup is mounted, only then check for 72 // Check if mem cgroup is mounted, only then check for
82 // write-access to avoid SELinux denials 73 // write-access to avoid SELinux denials