Merge "init: Don't look for logical partitions via uevents."
diff --git a/init/Android.bp b/init/Android.bp
index 625fb94..5bbb7a0 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -180,6 +180,7 @@
"service_test.cpp",
"subcontext_test.cpp",
"tokenizer_test.cpp",
+ "ueventd_parser_test.cpp",
"ueventd_test.cpp",
"util_test.cpp",
],
diff --git a/init/devices.h b/init/devices.h
index 9224fcd..0be660f 100644
--- a/init/devices.h
+++ b/init/devices.h
@@ -35,6 +35,8 @@
class Permissions {
public:
+ friend void TestPermissions(const Permissions& expected, const Permissions& test);
+
Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid);
bool Match(const std::string& path) const;
@@ -57,6 +59,8 @@
class SysfsPermissions : public Permissions {
public:
+ friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test);
+
SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
gid_t gid)
: Permissions(name, perm, uid, gid), attribute_(attribute) {}
@@ -71,16 +75,24 @@
class Subsystem {
public:
friend class SubsystemParser;
+ friend void TestSubsystems(const Subsystem& expected, const Subsystem& test);
+
+ enum DevnameSource {
+ DEVNAME_UEVENT_DEVNAME,
+ DEVNAME_UEVENT_DEVPATH,
+ };
Subsystem() {}
- Subsystem(std::string name) : name_(std::move(name)) {}
+ Subsystem(const std::string& name) : name_(name) {}
+ Subsystem(const std::string& name, DevnameSource source, const std::string& dir_name)
+ : name_(name), devname_source_(source), dir_name_(dir_name) {}
// Returns the full path for a uevent of a device that is a member of this subsystem,
// according to the rules parsed from ueventd.rc
std::string ParseDevPath(const Uevent& uevent) const {
- std::string devname = devname_source_ == DevnameSource::DEVNAME_UEVENT_DEVNAME
- ? uevent.device_name
- : android::base::Basename(uevent.path);
+ std::string devname = devname_source_ == DEVNAME_UEVENT_DEVNAME
+ ? uevent.device_name
+ : android::base::Basename(uevent.path);
return dir_name_ + "/" + devname;
}
@@ -88,14 +100,9 @@
bool operator==(const std::string& string_name) const { return name_ == string_name; }
private:
- enum class DevnameSource {
- DEVNAME_UEVENT_DEVNAME,
- DEVNAME_UEVENT_DEVPATH,
- };
-
std::string name_;
+ DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME;
std::string dir_name_ = "/dev";
- DevnameSource devname_source_;
};
class DeviceHandler {
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index 6809445..b42a4c6 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -240,7 +240,8 @@
auto hardware = android::base::GetProperty("ro.hardware", "");
auto ueventd_configuration =
- ParseConfig({"/ueventd.rc", "/vendor/ueventd.rc", "/odm/ueventd.rc", hardware});
+ ParseConfig({"/ueventd.rc", "/vendor/ueventd.rc", "/odm/ueventd.rc",
+ "/ueventd." + hardware + ".rc"});
device_handler = DeviceHandler{std::move(ueventd_configuration.dev_permissions),
std::move(ueventd_configuration.sysfs_permissions),
diff --git a/init/ueventd_parser.cpp b/init/ueventd_parser.cpp
index c114ec7..54b0d16 100644
--- a/init/ueventd_parser.cpp
+++ b/init/ueventd_parser.cpp
@@ -117,11 +117,11 @@
Result<Success> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
if (args[1] == "uevent_devname") {
- subsystem_.devname_source_ = Subsystem::DevnameSource::DEVNAME_UEVENT_DEVNAME;
+ subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
return Success();
}
if (args[1] == "uevent_devpath") {
- subsystem_.devname_source_ = Subsystem::DevnameSource::DEVNAME_UEVENT_DEVPATH;
+ subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
return Success();
}
diff --git a/init/ueventd_parser_test.cpp b/init/ueventd_parser_test.cpp
new file mode 100644
index 0000000..31208b9
--- /dev/null
+++ b/init/ueventd_parser_test.cpp
@@ -0,0 +1,224 @@
+/*
+ * 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 "ueventd_parser.h"
+
+#include <android-base/test_utils.h>
+#include <gtest/gtest.h>
+#include <private/android_filesystem_config.h>
+
+namespace android {
+namespace init {
+
+void TestSubsystems(const Subsystem& expected, const Subsystem& test) {
+ EXPECT_EQ(expected.name_, test.name_);
+ EXPECT_EQ(expected.devname_source_, test.devname_source_) << expected.name_;
+ EXPECT_EQ(expected.dir_name_, test.dir_name_) << expected.name_;
+}
+
+void TestPermissions(const Permissions& expected, const Permissions& test) {
+ EXPECT_EQ(expected.name_, test.name_);
+ EXPECT_EQ(expected.perm_, test.perm_) << expected.name_;
+ EXPECT_EQ(expected.uid_, test.uid_) << expected.name_;
+ EXPECT_EQ(expected.gid_, test.gid_) << expected.name_;
+ EXPECT_EQ(expected.prefix_, test.prefix_) << expected.name_;
+ EXPECT_EQ(expected.wildcard_, test.wildcard_) << expected.name_;
+}
+
+void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test) {
+ TestPermissions(expected, test);
+ EXPECT_EQ(expected.attribute_, test.attribute_);
+}
+
+template <typename T, typename F>
+void TestVector(const T& expected, const T& test, F function) {
+ ASSERT_EQ(expected.size(), test.size());
+ auto expected_it = expected.begin();
+ auto test_it = test.begin();
+
+ for (; expected_it != expected.end(); ++expected_it, ++test_it) {
+ function(*expected_it, *test_it);
+ }
+}
+
+void TestUeventdFile(const std::string& content, const UeventdConfiguration& expected) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::base::WriteStringToFd(content, tf.fd));
+
+ auto result = ParseConfig({tf.path});
+
+ TestVector(expected.subsystems, result.subsystems, TestSubsystems);
+ TestVector(expected.sysfs_permissions, result.sysfs_permissions, TestSysfsPermissions);
+ TestVector(expected.dev_permissions, result.dev_permissions, TestPermissions);
+ EXPECT_EQ(expected.firmware_directories, result.firmware_directories);
+}
+
+TEST(ueventd_parser, EmptyFile) {
+ TestUeventdFile("", {});
+}
+
+TEST(ueventd_parser, Subsystems) {
+ auto ueventd_file = R"(
+subsystem test_devname
+ devname uevent_devname
+
+subsystem test_devpath_no_dirname
+ devname uevent_devpath
+
+subsystem test_devname2
+ devname uevent_devname
+
+subsystem test_devpath_dirname
+ devname uevent_devpath
+ dirname /dev/graphics
+)";
+
+ auto subsystems = std::vector<Subsystem>{
+ {"test_devname", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
+ {"test_devpath_no_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev"},
+ {"test_devname2", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
+ {"test_devpath_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev/graphics"}};
+
+ TestUeventdFile(ueventd_file, {subsystems, {}, {}, {}});
+}
+
+TEST(ueventd_parser, Permissions) {
+ auto ueventd_file = R"(
+/dev/rtc0 0640 system system
+/dev/graphics/* 0660 root graphics
+/dev/*/test 0660 root system
+
+/sys/devices/platform/trusty.* trusty_version 0440 root log
+/sys/devices/virtual/input/input enable 0660 root input
+/sys/devices/virtual/*/input poll_delay 0660 root input
+)";
+
+ auto permissions = std::vector<Permissions>{
+ {"/dev/rtc0", 0640, AID_SYSTEM, AID_SYSTEM},
+ {"/dev/graphics/*", 0660, AID_ROOT, AID_GRAPHICS},
+ {"/dev/*/test", 0660, AID_ROOT, AID_SYSTEM},
+ };
+
+ auto sysfs_permissions = std::vector<SysfsPermissions>{
+ {"/sys/devices/platform/trusty.*", "trusty_version", 0440, AID_ROOT, AID_LOG},
+ {"/sys/devices/virtual/input/input", "enable", 0660, AID_ROOT, AID_INPUT},
+ {"/sys/devices/virtual/*/input", "poll_delay", 0660, AID_ROOT, AID_INPUT},
+ };
+
+ TestUeventdFile(ueventd_file, {{}, sysfs_permissions, permissions, {}});
+}
+
+TEST(ueventd_parser, FirmwareDirectories) {
+ auto ueventd_file = R"(
+firmware_directories /first/ /second /third
+firmware_directories /more
+)";
+
+ auto firmware_directories = std::vector<std::string>{
+ "/first/",
+ "/second",
+ "/third",
+ "/more",
+ };
+
+ TestUeventdFile(ueventd_file, {{}, {}, {}, firmware_directories});
+}
+
+TEST(ueventd_parser, AllTogether) {
+ auto ueventd_file = R"(
+
+/dev/rtc0 0640 system system
+firmware_directories /first/ /second /third
+/sys/devices/platform/trusty.* trusty_version 0440 root log
+
+subsystem test_devname
+ devname uevent_devname
+
+/dev/graphics/* 0660 root graphics
+
+subsystem test_devpath_no_dirname
+ devname uevent_devpath
+
+/sys/devices/virtual/input/input enable 0660 root input
+
+## this is a comment
+
+subsystem test_devname2
+## another comment
+ devname uevent_devname
+
+subsystem test_devpath_dirname
+ devname uevent_devpath
+ dirname /dev/graphics
+
+/dev/*/test 0660 root system
+/sys/devices/virtual/*/input poll_delay 0660 root input
+firmware_directories /more
+
+#ending comment
+)";
+
+ auto subsystems = std::vector<Subsystem>{
+ {"test_devname", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
+ {"test_devpath_no_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev"},
+ {"test_devname2", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
+ {"test_devpath_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev/graphics"}};
+
+ auto permissions = std::vector<Permissions>{
+ {"/dev/rtc0", 0640, AID_SYSTEM, AID_SYSTEM},
+ {"/dev/graphics/*", 0660, AID_ROOT, AID_GRAPHICS},
+ {"/dev/*/test", 0660, AID_ROOT, AID_SYSTEM},
+ };
+
+ auto sysfs_permissions = std::vector<SysfsPermissions>{
+ {"/sys/devices/platform/trusty.*", "trusty_version", 0440, AID_ROOT, AID_LOG},
+ {"/sys/devices/virtual/input/input", "enable", 0660, AID_ROOT, AID_INPUT},
+ {"/sys/devices/virtual/*/input", "poll_delay", 0660, AID_ROOT, AID_INPUT},
+ };
+
+ auto firmware_directories = std::vector<std::string>{
+ "/first/",
+ "/second",
+ "/third",
+ "/more",
+ };
+
+ TestUeventdFile(ueventd_file,
+ {subsystems, sysfs_permissions, permissions, firmware_directories});
+}
+
+// All of these lines are ill-formed, so test that there is 0 output.
+TEST(ueventd_parser, ParseErrors) {
+ auto ueventd_file = R"(
+
+/dev/rtc0 badmode baduidbad system
+/dev/rtc0 0640 baduidbad system
+/dev/rtc0 0640 system baduidbad
+firmware_directories #no directory listed
+/sys/devices/platform/trusty.* trusty_version badmode root log
+/sys/devices/platform/trusty.* trusty_version 0440 baduidbad log
+/sys/devices/platform/trusty.* trusty_version 0440 root baduidbad
+
+subsystem #no name
+
+)";
+
+ TestUeventdFile(ueventd_file, {});
+}
+
+} // namespace init
+} // namespace android
diff --git a/libcutils/ashmem-dev.cpp b/libcutils/ashmem-dev.cpp
index 15ace0e..0cc4fc0 100644
--- a/libcutils/ashmem-dev.cpp
+++ b/libcutils/ashmem-dev.cpp
@@ -90,7 +90,7 @@
dev_t rdev;
struct stat st;
- if (TEMP_FAILURE_RETRY(fstat(fd, &st)) < 0) {
+ if (fstat(fd, &st) < 0) {
return -1;
}
@@ -135,6 +135,12 @@
return -1;
}
+static int __ashmem_check_failure(int fd, int result)
+{
+ if (result == -1 && errno == ENOTTY) __ashmem_is_ashmem(fd, 1);
+ return result;
+}
+
int ashmem_valid(int fd)
{
return __ashmem_is_ashmem(fd, 0) >= 0;
@@ -182,12 +188,7 @@
int ashmem_set_prot_region(int fd, int prot)
{
- int ret = __ashmem_is_ashmem(fd, 1);
- if (ret < 0) {
- return ret;
- }
-
- return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
+ return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)));
}
int ashmem_pin_region(int fd, size_t offset, size_t len)
@@ -195,12 +196,7 @@
// TODO: should LP64 reject too-large offset/len?
ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
- int ret = __ashmem_is_ashmem(fd, 1);
- if (ret < 0) {
- return ret;
- }
-
- return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
+ return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin)));
}
int ashmem_unpin_region(int fd, size_t offset, size_t len)
@@ -208,20 +204,10 @@
// TODO: should LP64 reject too-large offset/len?
ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
- int ret = __ashmem_is_ashmem(fd, 1);
- if (ret < 0) {
- return ret;
- }
-
- return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
+ return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin)));
}
int ashmem_get_size_region(int fd)
{
- int ret = __ashmem_is_ashmem(fd, 1);
- if (ret < 0) {
- return ret;
- }
-
- return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
+ return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)));
}
diff --git a/liblog/include/log/log_main.h b/liblog/include/log/log_main.h
index 21fc7cc..f1ff31a 100644
--- a/liblog/include/log/log_main.h
+++ b/liblog/include/log/log_main.h
@@ -40,6 +40,17 @@
#endif
#endif
+/*
+ * Use __VA_ARGS__ if running a static analyzer,
+ * to avoid warnings of unused variables in __VA_ARGS__.
+ */
+
+#ifdef __clang_analyzer__
+#define __FAKE_USE_VA_ARGS(...) ((void)(__VA_ARGS__))
+#else
+#define __FAKE_USE_VA_ARGS(...) ((void)(0))
+#endif
+
/* --------------------------------------------------------------------- */
/*
@@ -112,7 +123,7 @@
#define LOG_ALWAYS_FATAL_IF(cond, ...) \
((__predict_false(cond)) \
? ((void)android_printAssert(#cond, LOG_TAG, ##__VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
#ifndef LOG_ALWAYS_FATAL
@@ -128,10 +139,10 @@
#if LOG_NDEBUG
#ifndef LOG_FATAL_IF
-#define LOG_FATAL_IF(cond, ...) ((void)0)
+#define LOG_FATAL_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
#endif
#ifndef LOG_FATAL
-#define LOG_FATAL(...) ((void)0)
+#define LOG_FATAL(...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
#endif
#else
@@ -175,11 +186,12 @@
#ifndef ALOGV
#define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#if LOG_NDEBUG
-#define ALOGV(...) \
- do { \
- if (false) { \
- __ALOGV(__VA_ARGS__); \
- } \
+#define ALOGV(...) \
+ do { \
+ __FAKE_USE_VA_ARGS(__VA_ARGS__); \
+ if (false) { \
+ __ALOGV(__VA_ARGS__); \
+ } \
} while (false)
#else
#define ALOGV(...) __ALOGV(__VA_ARGS__)
@@ -188,11 +200,11 @@
#ifndef ALOGV_IF
#if LOG_NDEBUG
-#define ALOGV_IF(cond, ...) ((void)0)
+#define ALOGV_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
#else
#define ALOGV_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
#endif
@@ -206,7 +218,7 @@
#ifndef ALOGD_IF
#define ALOGD_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
/*
@@ -219,7 +231,7 @@
#ifndef ALOGI_IF
#define ALOGI_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
/*
@@ -232,7 +244,7 @@
#ifndef ALOGW_IF
#define ALOGW_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
/*
@@ -245,7 +257,7 @@
#ifndef ALOGE_IF
#define ALOGE_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
/* --------------------------------------------------------------------- */