Move init's kernel logging into libbase.
This will let other stuff (such as adbd and recovery) use it too.
Bug: http://b/28467098
Change-Id: Idab51f438ed3edd6fe65a56c4b396eaff5f3833e
diff --git a/init/action.cpp b/init/action.cpp
index f3e362e..ed88f6d 100644
--- a/init/action.cpp
+++ b/init/action.cpp
@@ -118,7 +118,8 @@
Timer t;
int result = command.InvokeFunc();
- if (klog_get_level() >= KLOG_DEBUG_LEVEL) {
+ // TODO: this should probably be changed to "if (failed || took a long time)"...
+ if (android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
std::string trigger_name = BuildTriggersString();
std::string cmd_str = command.BuildCommandString();
std::string source = command.BuildSourceString();
diff --git a/init/builtins.cpp b/init/builtins.cpp
index d64c3d2..3d220c5 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -482,11 +482,7 @@
* not return.
*/
static int do_mount_all(const std::vector<std::string>& args) {
- pid_t pid;
int ret = -1;
- int child_ret = -1;
- int status;
- struct fstab *fstab;
const char* fstabfile = args[1].c_str();
/*
@@ -495,9 +491,10 @@
* process if anything goes wrong (crash or memory leak), and wait for
* the child to finish in the parent.
*/
- pid = fork();
+ pid_t pid = fork();
if (pid > 0) {
/* Parent. Wait for the child to return */
+ int status;
int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
if (wp_ret == -1) {
// Unexpected error code. We will continue anyway.
@@ -511,9 +508,13 @@
}
} else if (pid == 0) {
/* child, call fs_mgr_mount_all() */
- klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
- fstab = fs_mgr_read_fstab(fstabfile);
- child_ret = fs_mgr_mount_all(fstab);
+
+ // So we can always see what fs_mgr_mount_all() does.
+ // Only needed if someone explicitly changes the default log level in their init.rc.
+ android::base::ScopedLogSeverity info(android::base::INFO);
+
+ struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
+ int child_ret = fs_mgr_mount_all(fstab);
fs_mgr_free_fstab(fstab);
if (child_ret == -1) {
PLOG(ERROR) << "fs_mgr_mount_all returned an error";
@@ -866,12 +867,23 @@
}
static int do_loglevel(const std::vector<std::string>& args) {
+ // TODO: support names instead/as well?
int log_level = std::stoi(args[1]);
- if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
- LOG(ERROR) << "loglevel: invalid log level " << log_level;
- return -EINVAL;
+ android::base::LogSeverity severity;
+ switch (log_level) {
+ case 7: severity = android::base::DEBUG; break;
+ case 6: severity = android::base::INFO; break;
+ case 5:
+ case 4: severity = android::base::WARNING; break;
+ case 3: severity = android::base::ERROR; break;
+ case 2:
+ case 1:
+ case 0: severity = android::base::FATAL; break;
+ default:
+ LOG(ERROR) << "loglevel: invalid log level " << log_level;
+ return -EINVAL;
}
- klog_set_level(log_level);
+ android::base::SetMinimumLogSeverity(severity);
return 0;
}
diff --git a/init/log.cpp b/init/log.cpp
index 379141a..8618340 100644
--- a/init/log.cpp
+++ b/init/log.cpp
@@ -17,52 +17,16 @@
#include "log.h"
#include <fcntl.h>
-#include <stdlib.h>
#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/uio.h>
#include <selinux/selinux.h>
-static const int kLogSeverityToKLogLevel[] = {
- [android::base::VERBOSE] = KLOG_DEBUG_LEVEL,
- [android::base::DEBUG] = KLOG_DEBUG_LEVEL,
- [android::base::INFO] = KLOG_INFO_LEVEL,
- [android::base::WARNING] = KLOG_WARNING_LEVEL,
- [android::base::ERROR] = KLOG_ERROR_LEVEL,
- [android::base::FATAL] = KLOG_ERROR_LEVEL,
-};
-static_assert(arraysize(kLogSeverityToKLogLevel) == android::base::FATAL + 1,
- "Mismatch in size of kLogSeverityToKLogLevel and values in LogSeverity");
-
-static void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
- const char* tag, const char*, unsigned int, const char* msg) {
- int level = kLogSeverityToKLogLevel[severity];
- if (level > klog_get_level()) return;
-
- // The kernel's printk buffer is only 1024 bytes.
- // TODO: should we automatically break up long lines into multiple lines?
- // Or we could log but with something like "..." at the end?
- char buf[1024];
- size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg);
- if (size > sizeof(buf)) {
- size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n",
- level, tag, size);
- }
-
- iovec iov[1];
- iov[0].iov_base = buf;
- iov[0].iov_len = size;
- klog_writev(level, iov, 1);
-}
-
void InitKernelLogging(char* argv[]) {
// Make stdin/stdout/stderr all point to /dev/null.
int fd = open("/sys/fs/selinux/null", O_RDWR);
if (fd == -1) {
int saved_errno = errno;
- android::base::InitLogging(argv, &KernelLogger);
+ android::base::InitLogging(argv, &android::base::KernelLogger);
errno = saved_errno;
PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
}
@@ -71,8 +35,7 @@
dup2(fd, 2);
if (fd > 2) close(fd);
- android::base::InitLogging(argv, &KernelLogger);
- klog_set_level(KLOG_INFO_LEVEL);
+ android::base::InitLogging(argv, &android::base::KernelLogger);
}
int selinux_klog_callback(int type, const char *fmt, ...) {
@@ -87,6 +50,6 @@
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
- KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
+ android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
return 0;
}
diff --git a/init/log.h b/init/log.h
index cf552a1..8fa6d74 100644
--- a/init/log.h
+++ b/init/log.h
@@ -19,8 +19,6 @@
#include <android-base/logging.h>
-#include <cutils/klog.h>
-
void InitKernelLogging(char* argv[]);
int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3);
diff --git a/init/readme.txt b/init/readme.txt
index 7260775..4c04f17 100644
--- a/init/readme.txt
+++ b/init/readme.txt
@@ -514,6 +514,3 @@
Alternatively, use the emulator:
emulator -partition-size 1024 -verbose -show-kernel -no-window
-
-You might want to change the klog_set_level call so you see all the kernel
-logging in dmesg (or the emulator output).