Merge "Correct comment in boot image header"
diff --git a/base/Android.bp b/base/Android.bp
index 71bf154..3d80d97 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -48,6 +48,7 @@
"file.cpp",
"logging.cpp",
"parsenetaddress.cpp",
+ "properties.cpp",
"quick_exit.cpp",
"stringprintf.cpp",
"strings.cpp",
@@ -58,9 +59,6 @@
shared_libs: ["liblog"],
target: {
android: {
- srcs: [
- "properties.cpp",
- ],
sanitize: {
misc_undefined: ["integer"],
},
@@ -130,6 +128,7 @@
"parsedouble_test.cpp",
"parseint_test.cpp",
"parsenetaddress_test.cpp",
+ "properties_test.cpp",
"quick_exit_test.cpp",
"scopeguard_test.cpp",
"stringprintf_test.cpp",
@@ -139,7 +138,6 @@
],
target: {
android: {
- srcs: ["properties_test.cpp"],
sanitize: {
misc_undefined: ["integer"],
},
diff --git a/base/include/android-base/properties.h b/base/include/android-base/properties.h
index 041586c..3a05143 100644
--- a/base/include/android-base/properties.h
+++ b/base/include/android-base/properties.h
@@ -19,10 +19,6 @@
#include <sys/cdefs.h>
-#if !defined(__BIONIC__)
-#error Only bionic supports system properties.
-#endif
-
#include <chrono>
#include <limits>
#include <string>
@@ -62,14 +58,18 @@
// Waits for the system property `key` to have the value `expected_value`.
// Times out after `relative_timeout`.
// Returns true on success, false on timeout.
+#if defined(__BIONIC__)
bool WaitForProperty(const std::string& key, const std::string& expected_value,
std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max());
+#endif
// Waits for the system property `key` to be created.
// Times out after `relative_timeout`.
// Returns true on success, false on timeout.
+#if defined(__BIONIC__)
bool WaitForPropertyCreation(const std::string& key, std::chrono::milliseconds relative_timeout =
std::chrono::milliseconds::max());
+#endif
} // namespace base
} // namespace android
diff --git a/base/properties.cpp b/base/properties.cpp
index 6cf43f9..d5a5918 100644
--- a/base/properties.cpp
+++ b/base/properties.cpp
@@ -14,16 +14,18 @@
* limitations under the License.
*/
-#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
-
#include "android-base/properties.h"
+#if defined(__BIONIC__)
+#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/system_properties.h>
#include <sys/_system_properties.h>
+#endif
#include <algorithm>
#include <chrono>
#include <limits>
+#include <map>
#include <string>
#include <android-base/parseint.h>
@@ -31,24 +33,6 @@
namespace android {
namespace base {
-std::string GetProperty(const std::string& key, const std::string& default_value) {
- const prop_info* pi = __system_property_find(key.c_str());
- if (pi == nullptr) return default_value;
-
- std::string property_value;
- __system_property_read_callback(pi,
- [](void* cookie, const char*, const char* value, unsigned) {
- auto property_value = reinterpret_cast<std::string*>(cookie);
- *property_value = value;
- },
- &property_value);
-
- // If the property exists but is empty, also return the default value.
- // Since we can't remove system properties, "empty" is traditionally
- // the same as "missing" (this was true for cutils' property_get).
- return property_value.empty() ? default_value : property_value;
-}
-
bool GetBoolProperty(const std::string& key, bool default_value) {
std::string value = GetProperty(key, "");
if (value == "1" || value == "y" || value == "yes" || value == "on" || value == "true") {
@@ -85,10 +69,43 @@
template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t);
template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t);
+#if !defined(__BIONIC__)
+static std::map<std::string, std::string>& g_properties = *new std::map<std::string, std::string>;
+static int __system_property_set(const char* key, const char* value) {
+ g_properties[key] = value;
+ return 0;
+}
+#endif
+
+std::string GetProperty(const std::string& key, const std::string& default_value) {
+ std::string property_value;
+#if defined(__BIONIC__)
+ const prop_info* pi = __system_property_find(key.c_str());
+ if (pi == nullptr) return default_value;
+
+ __system_property_read_callback(pi,
+ [](void* cookie, const char*, const char* value, unsigned) {
+ auto property_value = reinterpret_cast<std::string*>(cookie);
+ *property_value = value;
+ },
+ &property_value);
+#else
+ auto it = g_properties.find(key);
+ if (it == g_properties.end()) return default_value;
+ property_value = it->second;
+#endif
+ // If the property exists but is empty, also return the default value.
+ // Since we can't remove system properties, "empty" is traditionally
+ // the same as "missing" (this was true for cutils' property_get).
+ return property_value.empty() ? default_value : property_value;
+}
+
bool SetProperty(const std::string& key, const std::string& value) {
return (__system_property_set(key.c_str(), value.c_str()) == 0);
}
+#if defined(__BIONIC__)
+
struct WaitForPropertyData {
bool done;
const std::string* expected_value;
@@ -175,5 +192,7 @@
return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr);
}
+#endif
+
} // namespace base
} // namespace android
diff --git a/base/properties_test.cpp b/base/properties_test.cpp
index de5f3dc..e7d4880 100644
--- a/base/properties_test.cpp
+++ b/base/properties_test.cpp
@@ -23,7 +23,9 @@
#include <string>
#include <thread>
-using namespace std::chrono_literals;
+#if !defined(_WIN32)
+using namespace std::literals;
+#endif
TEST(properties, smoke) {
android::base::SetProperty("debug.libbase.property_test", "hello");
@@ -126,6 +128,7 @@
TEST(properties, GetUintProperty_uint64_t) { CheckGetUintProperty<uint64_t>(); }
TEST(properties, WaitForProperty) {
+#if defined(__BIONIC__)
std::atomic<bool> flag{false};
std::thread thread([&]() {
std::this_thread::sleep_for(100ms);
@@ -138,9 +141,13 @@
flag = true;
ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", 1s));
thread.join();
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForProperty_timeout) {
+#if defined(__BIONIC__)
auto t0 = std::chrono::steady_clock::now();
ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_timeout_test", "a",
200ms));
@@ -149,9 +156,13 @@
ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms);
// Upper bounds on timing are inherently flaky, but let's try...
ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms);
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForProperty_MaxTimeout) {
+#if defined(__BIONIC__)
std::atomic<bool> flag{false};
std::thread thread([&]() {
android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
@@ -165,9 +176,13 @@
// Test that this does not immediately return false due to overflow issues with the timeout.
ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b"));
thread.join();
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForProperty_NegativeTimeout) {
+#if defined(__BIONIC__)
std::atomic<bool> flag{false};
std::thread thread([&]() {
android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
@@ -181,9 +196,13 @@
// Assert that this immediately returns with a negative timeout
ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", -100ms));
thread.join();
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForPropertyCreation) {
+#if defined(__BIONIC__)
std::thread thread([&]() {
std::this_thread::sleep_for(100ms);
android::base::SetProperty("debug.libbase.WaitForPropertyCreation_test", "a");
@@ -192,9 +211,13 @@
ASSERT_TRUE(android::base::WaitForPropertyCreation(
"debug.libbase.WaitForPropertyCreation_test", 1s));
thread.join();
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForPropertyCreation_timeout) {
+#if defined(__BIONIC__)
auto t0 = std::chrono::steady_clock::now();
ASSERT_FALSE(android::base::WaitForPropertyCreation(
"debug.libbase.WaitForPropertyCreation_timeout_test", 200ms));
@@ -203,4 +226,7 @@
ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms);
// Upper bounds on timing are inherently flaky, but let's try...
ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms);
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
diff --git a/init/Android.bp b/init/Android.bp
index a3083c1..517847c 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -170,6 +170,7 @@
srcs: [
"devices_test.cpp",
"init_test.cpp",
+ "keychords_test.cpp",
"persistent_properties_test.cpp",
"property_service_test.cpp",
"property_type_test.cpp",
diff --git a/init/action.cpp b/init/action.cpp
index f782b51..11335ca 100644
--- a/init/action.cpp
+++ b/init/action.cpp
@@ -18,16 +18,11 @@
#include <android-base/chrono_utils.h>
#include <android-base/logging.h>
+#include <android-base/properties.h>
#include <android-base/strings.h>
#include "util.h"
-#if defined(__ANDROID__)
-#include <android-base/properties.h>
-#else
-#include "host_init_stubs.h"
-#endif
-
using android::base::Join;
namespace android {
diff --git a/init/action_parser.cpp b/init/action_parser.cpp
index a2c9671..8a4b518 100644
--- a/init/action_parser.cpp
+++ b/init/action_parser.cpp
@@ -16,16 +16,11 @@
#include "action_parser.h"
+#include <android-base/properties.h>
#include <android-base/strings.h>
#include "stable_properties.h"
-#if defined(__ANDROID__)
-#include <android-base/properties.h>
-#else
-#include "host_init_stubs.h"
-#endif
-
using android::base::GetBoolProperty;
using android::base::StartsWith;
diff --git a/init/host_init_stubs.cpp b/init/host_init_stubs.cpp
index 4451ac8..c40a254 100644
--- a/init/host_init_stubs.cpp
+++ b/init/host_init_stubs.cpp
@@ -22,20 +22,6 @@
}
namespace android {
-namespace base {
-
-std::string GetProperty(const std::string&, const std::string& default_value) {
- return default_value;
-}
-
-bool GetBoolProperty(const std::string&, bool default_value) {
- return default_value;
-}
-
-} // namespace base
-} // namespace android
-
-namespace android {
namespace init {
// init.h
diff --git a/init/host_init_stubs.h b/init/host_init_stubs.h
index bb241af..f0e1f07 100644
--- a/init/host_init_stubs.h
+++ b/init/host_init_stubs.h
@@ -29,16 +29,6 @@
// unistd.h
int setgroups(size_t __size, const gid_t* __list);
-// android-base/properties.h
-namespace android {
-namespace base {
-
-std::string GetProperty(const std::string& key, const std::string& default_value);
-bool GetBoolProperty(const std::string& key, bool default_value);
-
-} // namespace base
-} // namespace android
-
namespace android {
namespace init {
diff --git a/init/init.cpp b/init/init.cpp
index 5652c5e..82648d9 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -553,22 +553,30 @@
}
}
-void HandleKeychord(int id) {
+void HandleKeychord(const std::vector<int>& keycodes) {
// Only handle keychords if adb is enabled.
std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
- if (adb_enabled == "running") {
- Service* svc = ServiceList::GetInstance().FindService(id, &Service::keychord_id);
- if (svc) {
- LOG(INFO) << "Starting service '" << svc->name() << "' from keychord " << id;
+ if (adb_enabled != "running") {
+ LOG(WARNING) << "Not starting service for keychord " << android::base::Join(keycodes, ' ')
+ << " because ADB is disabled";
+ return;
+ }
+
+ auto found = false;
+ for (const auto& service : ServiceList::GetInstance()) {
+ auto svc = service.get();
+ if (svc->keycodes() == keycodes) {
+ found = true;
+ LOG(INFO) << "Starting service '" << svc->name() << "' from keychord "
+ << android::base::Join(keycodes, ' ');
if (auto result = svc->Start(); !result) {
- LOG(ERROR) << "Could not start service '" << svc->name() << "' from keychord " << id
- << ": " << result.error();
+ LOG(ERROR) << "Could not start service '" << svc->name() << "' from keychord "
+ << android::base::Join(keycodes, ' ') << ": " << result.error();
}
- } else {
- LOG(ERROR) << "Service for keychord " << id << " not found";
}
- } else {
- LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled";
+ }
+ if (!found) {
+ LOG(ERROR) << "Service for keychord " << android::base::Join(keycodes, ' ') << " not found";
}
}
@@ -753,7 +761,7 @@
am.QueueBuiltinAction(
[&epoll, &keychords](const BuiltinArguments& args) -> Result<Success> {
for (const auto& svc : ServiceList::GetInstance()) {
- svc->set_keychord_id(keychords.GetId(svc->keycodes()));
+ keychords.Register(svc->keycodes());
}
keychords.Start(&epoll, HandleKeychord);
return Success();
diff --git a/init/keychords.cpp b/init/keychords.cpp
index 9aa8b2a..1af06dd 100644
--- a/init/keychords.cpp
+++ b/init/keychords.cpp
@@ -37,7 +37,7 @@
namespace android {
namespace init {
-Keychords::Keychords() : epoll_(nullptr), count_(0), inotify_fd_(-1) {}
+Keychords::Keychords() : epoll_(nullptr), inotify_fd_(-1) {}
Keychords::~Keychords() noexcept {
if (inotify_fd_ >= 0) {
@@ -108,23 +108,22 @@
}
}
-Keychords::Entry::Entry(const std::vector<int>& keycodes, int id)
- : keycodes(keycodes), id(id), notified(false) {}
+Keychords::Entry::Entry() : notified(false) {}
void Keychords::LambdaCheck() {
- for (auto& e : entries_) {
+ for (auto& [keycodes, entry] : entries_) {
auto found = true;
- for (auto& code : e.keycodes) {
+ for (auto& code : keycodes) {
if (!current_.GetBit(code)) {
- e.notified = false;
+ entry.notified = false;
found = false;
break;
}
}
if (!found) continue;
- if (e.notified) continue;
- e.notified = true;
- handler_(e.id);
+ if (entry.notified) continue;
+ entry.notified = true;
+ handler_(keycodes);
}
}
@@ -158,8 +157,8 @@
#endif
Keychords::Mask mask;
- for (auto& e : entries_) {
- for (auto& code : e.keycodes) {
+ for (auto& [keycodes, entry] : entries_) {
+ for (auto& code : keycodes) {
mask.resize(code);
mask.SetBit(code);
}
@@ -271,17 +270,15 @@
}
}
-int Keychords::GetId(const std::vector<int>& keycodes) {
- if (keycodes.empty()) return 0;
- ++count_;
- entries_.emplace_back(Entry(keycodes, count_));
- return count_;
+void Keychords::Register(const std::vector<int>& keycodes) {
+ if (keycodes.empty()) return;
+ entries_.try_emplace(keycodes, Entry());
}
-void Keychords::Start(Epoll* epoll, std::function<void(int)> handler) {
+void Keychords::Start(Epoll* epoll, std::function<void(const std::vector<int>&)> handler) {
epoll_ = epoll;
handler_ = handler;
- if (count_) GeteventOpenDevice();
+ if (entries_.size()) GeteventOpenDevice();
}
} // namespace init
diff --git a/init/keychords.h b/init/keychords.h
index 74a9195..00ed205 100644
--- a/init/keychords.h
+++ b/init/keychords.h
@@ -36,8 +36,8 @@
Keychords& operator=(Keychords&&) = delete;
~Keychords() noexcept;
- int GetId(const std::vector<int>& keycodes);
- void Start(Epoll* epoll, std::function<void(int)> handler);
+ void Register(const std::vector<int>& keycodes);
+ void Start(Epoll* epoll, std::function<void(const std::vector<int>&)> handler);
private:
// Bit management
@@ -65,10 +65,8 @@
};
struct Entry {
- Entry(const std::vector<int>& keycodes, int id);
+ Entry();
- const std::vector<int> keycodes;
- const int id;
bool notified;
};
@@ -84,12 +82,11 @@
void GeteventCloseDevice(const std::string& device);
Epoll* epoll_;
- std::function<void(int)> handler_;
+ std::function<void(const std::vector<int>&)> handler_;
std::map<std::string, int> registration_;
- int count_;
- std::vector<Entry> entries_;
+ std::map<const std::vector<int>, Entry> entries_;
Mask current_;
diff --git a/init/keychords_test.cpp b/init/keychords_test.cpp
new file mode 100644
index 0000000..c8c47a8
--- /dev/null
+++ b/init/keychords_test.cpp
@@ -0,0 +1,348 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "keychords.h"
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <linux/input.h>
+#include <linux/uinput.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+#include <chrono>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <gtest/gtest.h>
+
+#include "epoll.h"
+
+using namespace std::chrono_literals;
+
+namespace android {
+namespace init {
+
+namespace {
+
+// This class is used to inject keys.
+class EventHandler {
+ public:
+ EventHandler();
+ EventHandler(const EventHandler&) = delete;
+ EventHandler(EventHandler&&);
+ EventHandler& operator=(const EventHandler&) = delete;
+ EventHandler& operator=(EventHandler&&);
+ ~EventHandler() noexcept;
+
+ bool init();
+
+ bool send(struct input_event& e);
+ bool send(uint16_t type, uint16_t code, uint16_t value);
+ bool send(uint16_t code, bool value);
+
+ private:
+ int fd_;
+};
+
+EventHandler::EventHandler() : fd_(-1) {}
+
+EventHandler::EventHandler(EventHandler&& rval) : fd_(rval.fd_) {
+ rval.fd_ = -1;
+}
+
+EventHandler& EventHandler::operator=(EventHandler&& rval) {
+ fd_ = rval.fd_;
+ rval.fd_ = -1;
+ return *this;
+}
+
+EventHandler::~EventHandler() {
+ if (fd_ == -1) return;
+ ::ioctl(fd_, UI_DEV_DESTROY);
+ ::close(fd_);
+}
+
+bool EventHandler::init() {
+ if (fd_ != -1) return true;
+ auto fd = TEMP_FAILURE_RETRY(::open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC));
+ if (fd == -1) return false;
+ if (::ioctl(fd, UI_SET_EVBIT, EV_KEY) == -1) {
+ ::close(fd);
+ return false;
+ }
+
+ static const struct uinput_user_dev u = {
+ .name = "com.google.android.init.test",
+ .id.bustype = BUS_VIRTUAL,
+ .id.vendor = 0x1AE0, // Google
+ .id.product = 0x494E, // IN
+ .id.version = 1,
+ };
+ if (TEMP_FAILURE_RETRY(::write(fd, &u, sizeof(u))) != sizeof(u)) {
+ ::close(fd);
+ return false;
+ }
+
+ // all keys
+ for (uint16_t i = 0; i < KEY_MAX; ++i) {
+ if (::ioctl(fd, UI_SET_KEYBIT, i) == -1) {
+ ::close(fd);
+ return false;
+ }
+ }
+ if (::ioctl(fd, UI_DEV_CREATE) == -1) {
+ ::close(fd);
+ return false;
+ }
+ fd_ = fd;
+ return true;
+}
+
+bool EventHandler::send(struct input_event& e) {
+ gettimeofday(&e.time, nullptr);
+ return TEMP_FAILURE_RETRY(::write(fd_, &e, sizeof(e))) == sizeof(e);
+}
+
+bool EventHandler::send(uint16_t type, uint16_t code, uint16_t value) {
+ struct input_event e = {.type = type, .code = code, .value = value};
+ return send(e);
+}
+
+bool EventHandler::send(uint16_t code, bool value) {
+ return (code < KEY_MAX) && init() && send(EV_KEY, code, value) && send(EV_SYN, SYN_REPORT, 0);
+}
+
+std::string InitFds(const char* prefix, pid_t pid = getpid()) {
+ std::string ret;
+
+ std::string init_fds("/proc/");
+ init_fds += std::to_string(pid) + "/fd";
+ std::unique_ptr<DIR, decltype(&closedir)> fds(opendir(init_fds.c_str()), closedir);
+ if (!fds) return ret;
+
+ dirent* entry;
+ while ((entry = readdir(fds.get()))) {
+ if (entry->d_name[0] == '.') continue;
+ std::string devname = init_fds + '/' + entry->d_name;
+ char buf[256];
+ auto retval = readlink(devname.c_str(), buf, sizeof(buf) - 1);
+ if ((retval < 0) || (size_t(retval) >= (sizeof(buf) - 1))) continue;
+ buf[retval] = '\0';
+ if (!android::base::StartsWith(buf, prefix)) continue;
+ if (ret.size() != 0) ret += ",";
+ ret += buf;
+ }
+ return ret;
+}
+
+std::string InitInputFds() {
+ return InitFds("/dev/input/");
+}
+
+std::string InitInotifyFds() {
+ return InitFds("anon_inode:inotify");
+}
+
+// NB: caller (this series of tests, or conversely the service parser in init)
+// is responsible for validation, sorting and uniqueness of the chords, so no
+// fuzzing is advised.
+
+const std::vector<int> escape_chord = {KEY_ESC};
+const std::vector<int> triple1_chord = {KEY_BACKSPACE, KEY_VOLUMEDOWN, KEY_VOLUMEUP};
+const std::vector<int> triple2_chord = {KEY_VOLUMEDOWN, KEY_VOLUMEUP, KEY_BACK};
+
+const std::vector<const std::vector<int>> empty_chords;
+const std::vector<const std::vector<int>> chords = {
+ escape_chord,
+ triple1_chord,
+ triple2_chord,
+};
+
+class TestFrame {
+ public:
+ TestFrame(const std::vector<const std::vector<int>>& chords, EventHandler* ev = nullptr);
+
+ void RelaxForMs(std::chrono::milliseconds wait = 1ms);
+
+ void SetChord(int key, bool value = true);
+ void SetChords(const std::vector<int>& chord, bool value = true);
+ void ClrChord(int key);
+ void ClrChords(const std::vector<int>& chord);
+
+ bool IsOnlyChord(const std::vector<int>& chord) const;
+ bool IsNoChord() const;
+ bool IsChord(const std::vector<int>& chord) const;
+ void WaitForChord(const std::vector<int>& chord);
+
+ std::string Format() const;
+
+ private:
+ static std::string Format(const std::vector<const std::vector<int>>& chords);
+
+ Epoll epoll_;
+ Keychords keychords_;
+ std::vector<const std::vector<int>> keycodes_;
+ EventHandler* ev_;
+};
+
+TestFrame::TestFrame(const std::vector<const std::vector<int>>& chords, EventHandler* ev)
+ : ev_(ev) {
+ if (!epoll_.Open()) return;
+ for (const auto& keycodes : chords) keychords_.Register(keycodes);
+ keychords_.Start(&epoll_, [this](const std::vector<int>& keycodes) {
+ this->keycodes_.emplace_back(keycodes);
+ });
+}
+
+void TestFrame::RelaxForMs(std::chrono::milliseconds wait) {
+ epoll_.Wait(wait);
+}
+
+void TestFrame::SetChord(int key, bool value) {
+ ASSERT_TRUE(!!ev_);
+ RelaxForMs();
+ EXPECT_TRUE(ev_->send(key, value));
+}
+
+void TestFrame::SetChords(const std::vector<int>& chord, bool value) {
+ ASSERT_TRUE(!!ev_);
+ for (auto& key : chord) SetChord(key, value);
+ RelaxForMs();
+}
+
+void TestFrame::ClrChord(int key) {
+ ASSERT_TRUE(!!ev_);
+ SetChord(key, false);
+}
+
+void TestFrame::ClrChords(const std::vector<int>& chord) {
+ ASSERT_TRUE(!!ev_);
+ SetChords(chord, false);
+}
+
+bool TestFrame::IsOnlyChord(const std::vector<int>& chord) const {
+ auto ret = false;
+ for (const auto& keycode : keycodes_) {
+ if (keycode != chord) return false;
+ ret = true;
+ }
+ return ret;
+}
+
+bool TestFrame::IsNoChord() const {
+ return keycodes_.empty();
+}
+
+bool TestFrame::IsChord(const std::vector<int>& chord) const {
+ for (const auto& keycode : keycodes_) {
+ if (keycode == chord) return true;
+ }
+ return false;
+}
+
+void TestFrame::WaitForChord(const std::vector<int>& chord) {
+ for (int retry = 1000; retry && !IsChord(chord); --retry) RelaxForMs();
+}
+
+std::string TestFrame::Format(const std::vector<const std::vector<int>>& chords) {
+ std::string ret("{");
+ if (!chords.empty()) {
+ ret += android::base::Join(chords.front(), ' ');
+ for (auto it = std::next(chords.begin()); it != chords.end(); ++it) {
+ ret += ',';
+ ret += android::base::Join(*it, ' ');
+ }
+ }
+ return ret + '}';
+}
+
+std::string TestFrame::Format() const {
+ return Format(keycodes_);
+}
+
+} // namespace
+
+TEST(keychords, not_instantiated) {
+ TestFrame test_frame(empty_chords);
+ EXPECT_TRUE(InitInotifyFds().size() == 0);
+}
+
+TEST(keychords, instantiated) {
+ // Test if a valid set of chords results in proper instantiation of the
+ // underlying mechanisms for /dev/input/ attachment.
+ TestFrame test_frame(chords);
+ EXPECT_TRUE(InitInotifyFds().size() != 0);
+}
+
+TEST(keychords, init_inotify) {
+ std::string before(InitInputFds());
+
+ TestFrame test_frame(chords);
+
+ EventHandler ev;
+ EXPECT_TRUE(ev.init());
+
+ for (int retry = 1000; retry && before == InitInputFds(); --retry) test_frame.RelaxForMs();
+ std::string after(InitInputFds());
+ EXPECT_NE(before, after);
+}
+
+TEST(keychords, key) {
+ EventHandler ev;
+ EXPECT_TRUE(ev.init());
+ TestFrame test_frame(chords, &ev);
+
+ test_frame.SetChords(escape_chord);
+ test_frame.WaitForChord(escape_chord);
+ test_frame.ClrChords(escape_chord);
+ EXPECT_TRUE(test_frame.IsOnlyChord(escape_chord))
+ << "expected only " << android::base::Join(escape_chord, ' ') << " got "
+ << test_frame.Format();
+}
+
+TEST(keychords, keys_in_series) {
+ EventHandler ev;
+ EXPECT_TRUE(ev.init());
+ TestFrame test_frame(chords, &ev);
+
+ for (auto& key : triple1_chord) {
+ test_frame.SetChord(key);
+ test_frame.ClrChord(key);
+ }
+ test_frame.WaitForChord(triple1_chord);
+ EXPECT_TRUE(test_frame.IsNoChord()) << "expected nothing got " << test_frame.Format();
+}
+
+TEST(keychords, keys_in_parallel) {
+ EventHandler ev;
+ EXPECT_TRUE(ev.init());
+ TestFrame test_frame(chords, &ev);
+
+ test_frame.SetChords(triple2_chord);
+ test_frame.WaitForChord(triple2_chord);
+ test_frame.ClrChords(triple2_chord);
+ EXPECT_TRUE(test_frame.IsOnlyChord(triple2_chord))
+ << "expected only " << android::base::Join(triple2_chord, ' ') << " got "
+ << test_frame.Format();
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/service.cpp b/init/service.cpp
index 5778a93..565cae7 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -33,6 +33,7 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
+#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
@@ -47,8 +48,6 @@
#if defined(__ANDROID__)
#include <sys/system_properties.h>
-#include <android-base/properties.h>
-
#include "init.h"
#include "property_service.h"
#else
@@ -229,7 +228,6 @@
seclabel_(seclabel),
onrestart_(false, subcontext_for_restart_commands, "<Service '" + name + "' onrestart>", 0,
"onrestart", {}),
- keychord_id_(0),
ioprio_class_(IoSchedClass_NONE),
ioprio_pri_(0),
priority_(0),
@@ -549,7 +547,7 @@
for (auto& key : keycodes_) {
if (key == code) return Error() << "duplicate keycode: " << args[i];
}
- keycodes_.emplace_back(code);
+ keycodes_.insert(std::upper_bound(keycodes_.begin(), keycodes_.end(), code), code);
} else {
return Error() << "invalid keycode: " << args[i];
}
diff --git a/init/service.h b/init/service.h
index cbfd52f..ea79a07 100644
--- a/init/service.h
+++ b/init/service.h
@@ -108,8 +108,6 @@
const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
const std::string& seclabel() const { return seclabel_; }
const std::vector<int>& keycodes() const { return keycodes_; }
- int keychord_id() const { return keychord_id_; }
- void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
IoSchedClass ioprio_class() const { return ioprio_class_; }
int ioprio_pri() const { return ioprio_pri_; }
const std::set<std::string>& interfaces() const { return interfaces_; }
@@ -199,9 +197,8 @@
std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name
- // keycodes for triggering this service via /dev/keychord
+ // keycodes for triggering this service via /dev/input/input*
std::vector<int> keycodes_;
- int keychord_id_;
IoSchedClass ioprio_class_;
int ioprio_pri_;
diff --git a/init/service_test.cpp b/init/service_test.cpp
index b43c2e9..194aa2b 100644
--- a/init/service_test.cpp
+++ b/init/service_test.cpp
@@ -46,7 +46,6 @@
EXPECT_EQ(0U, service_in_old_memory->uid());
EXPECT_EQ(0U, service_in_old_memory->gid());
EXPECT_EQ(0U, service_in_old_memory->namespace_flags());
- EXPECT_EQ(0, service_in_old_memory->keychord_id());
EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory->ioprio_class());
EXPECT_EQ(0, service_in_old_memory->ioprio_pri());
EXPECT_EQ(0, service_in_old_memory->priority());
@@ -66,7 +65,6 @@
EXPECT_EQ(0U, service_in_old_memory2->uid());
EXPECT_EQ(0U, service_in_old_memory2->gid());
EXPECT_EQ(0U, service_in_old_memory2->namespace_flags());
- EXPECT_EQ(0, service_in_old_memory2->keychord_id());
EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory2->ioprio_class());
EXPECT_EQ(0, service_in_old_memory2->ioprio_pri());
EXPECT_EQ(0, service_in_old_memory2->priority());
diff --git a/init/util.cpp b/init/util.cpp
index 4455b2e..5f2b87d 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -33,6 +33,7 @@
#include <android-base/file.h>
#include <android-base/logging.h>
+#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
@@ -43,8 +44,6 @@
#include "reboot.h"
#if defined(__ANDROID__)
-#include <android-base/properties.h>
-
#include "selinux.h"
#else
#include "host_init_stubs.h"
diff --git a/libmemunreachable/README.md b/libmemunreachable/README.md
index 6d9141a..9cc0c9b 100644
--- a/libmemunreachable/README.md
+++ b/libmemunreachable/README.md
@@ -44,7 +44,7 @@
### C++ interface ###
-####`bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit = 100)`####
+#### `bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit = 100)` ####
Updates an `UnreachableMemoryInfo` object with information on leaks, including details on up to `limit` leaks. Returns true if leak detection succeeded.
#### `std::string GetUnreachableMemoryString(bool log_contents = false, size_t limit = 100)` ####
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
index 58295fa..1cebb5d 100644
--- a/libprocessgroup/processgroup.cpp
+++ b/libprocessgroup/processgroup.cpp
@@ -39,18 +39,14 @@
#include <android-base/file.h>
#include <android-base/logging.h>
-#ifdef __ANDROID__
#include <android-base/properties.h>
-#endif
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <private/android_filesystem_config.h>
#include <processgroup/processgroup.h>
-#ifdef __ANDROID__
using android::base::GetBoolProperty;
-#endif
using android::base::StartsWith;
using android::base::StringPrintf;
using android::base::WriteStringToFile;
@@ -68,15 +64,10 @@
static const std::string& GetCgroupRootPath() {
static std::string cgroup_root_path;
std::call_once(init_path_flag, [&]() {
-#ifdef __ANDROID__
// low-ram devices use per-app memcg by default, unlike high-end ones
bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
bool per_app_memcg =
GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
-#else
- // host does not support Android properties
- bool per_app_memcg = false;
-#endif
if (per_app_memcg) {
// Check if mem cgroup is mounted, only then check for
// write-access to avoid SELinux denials