diff options
author | Mark Salyzyn <salyzyn@google.com> | 2018-04-06 09:40:26 -0700 |
---|---|---|
committer | Mark Salyzyn <salyzyn@google.com> | 2018-04-09 07:19:33 -0700 |
commit | 2507a048abd7ccd0337384fb16c0fa0a4648c4f1 (patch) | |
tree | 95593d86624ad8515cc53b9ff0c3111f2245372b | |
parent | 0289f35b832824757f261775cc730f64a2bb7561 (diff) | |
download | system_core-2507a048abd7ccd0337384fb16c0fa0a4648c4f1.tar.gz system_core-2507a048abd7ccd0337384fb16c0fa0a4648c4f1.tar.bz2 system_core-2507a048abd7ccd0337384fb16c0fa0a4648c4f1.zip |
base: kernel logging get pre-opened /dev/kmsg from environment
If the environment has an instance of /dev/kmsg pre-opened, pick up.
This happens when an init script has 'file w /dev/kmsg' in the
daemon's recipe.
libcutils android_get_control_file operations are open-coded to
prevent a dependency on libcutils itself. Also check that /dev/kmsg
file descriptor is valid and open'd for write only.
Test: check to make sure init() kernel logging still works
Bug: 77541952
Bug: 77661605
Change-Id: I007553acd594ef0815d23f32f8aa2867518570ba
-rw-r--r-- | base/logging.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/base/logging.cpp b/base/logging.cpp index 30d7f8d57..a33da2211 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -54,6 +54,7 @@ #endif #include <android-base/macros.h> +#include <android-base/parseint.h> #include <android-base/strings.h> #include <android-base/threads.h> @@ -82,6 +83,23 @@ const char* getprogname() { return progname; } #endif + +#if defined(__linux__) +int OpenKmsg() { +#if defined(__ANDROID__) + // pick up 'file w /dev/kmsg' environment from daemon's init rc file + const auto val = getenv("ANDROID_FILE__dev_kmsg"); + if (val != nullptr) { + int fd; + if (android::base::ParseInt(val, &fd, 0)) { + auto flags = fcntl(fd, F_GETFL); + if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd; + } + } +#endif + return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); +} +#endif } // namespace namespace android { @@ -150,7 +168,7 @@ void KernelLogger(android::base::LogId, android::base::LogSeverity severity, static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1, "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity"); - static int klog_fd = TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); + static int klog_fd = OpenKmsg(); if (klog_fd == -1) return; int level = kLogSeverityToKernelLogLevel[severity]; |