Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Make klog_fd thread-safe and make klog_init a no-op.
Browse files Browse the repository at this point in the history
I'll come back and remove klog_init when I've removed other calls to it.

Change-Id: Iad7fd26d853b4ddc54e9abd44516b6f138cbbfcb
Test: booted N9, looked at "adb shell dmesg" output.
  • Loading branch information
enh-google committed Jun 29, 2016
1 parent b0d062a commit 171a829
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 49 deletions.
2 changes: 0 additions & 2 deletions fs_mgr/fs_mgr_main.c
Expand Up @@ -85,7 +85,6 @@ int main(int argc, char *argv[])
char *fstab_file=NULL;
struct fstab *fstab=NULL;

klog_init();
klog_set_level(6);

parse_options(argc, argv, &a_flag, &u_flag, &n_flag, &n_name, &n_blk_dev);
Expand All @@ -111,4 +110,3 @@ int main(int argc, char *argv[])
/* Should not get here */
exit(1);
}

1 change: 0 additions & 1 deletion include/cutils/klog.h
Expand Up @@ -26,7 +26,6 @@ __BEGIN_DECLS
void klog_init(void);
int klog_get_level(void);
void klog_set_level(int level);
/* TODO: void klog_close(void); - and make klog_fd users thread safe. */

void klog_write(int level, const char *fmt, ...)
__attribute__ ((format(printf, 2, 3)));
Expand Down
1 change: 0 additions & 1 deletion init/init.cpp
Expand Up @@ -477,7 +477,6 @@ int main(int argc, char** argv) {
// kmsg and null, otherwise we won't be able to remount / read-only
// later on. Now that tmpfs is mounted on /dev, we can actually talk
// to the outside world.
open_devnull_stdio();
InitKernelLogging(argv);

LOG(INFO) << "init " << (is_first_stage ? "first stage" : "second stage") << " started!";
Expand Down
18 changes: 16 additions & 2 deletions init/log.cpp
Expand Up @@ -16,8 +16,11 @@

#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>
Expand Down Expand Up @@ -51,9 +54,20 @@ static void KernelLogger(android::base::LogId, android::base::LogSeverity severi
}

void InitKernelLogging(char* argv[]) {
android::base::InitLogging(argv, &KernelLogger);
// 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);
errno = saved_errno;
PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
}
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2) close(fd);

klog_init();
android::base::InitLogging(argv, &KernelLogger);
klog_set_level(KLOG_NOTICE_LEVEL);
}

Expand Down
4 changes: 2 additions & 2 deletions init/readme.txt
Expand Up @@ -515,5 +515,5 @@ Alternatively, use the emulator:

emulator -partition-size 1024 -verbose -show-kernel -no-window

You might want to call klog_set_level(6) after the klog_init() call
so you see the kernel logging in dmesg (or the emulator output).
You might want to change the klog_set_level call so you see all the kernel
logging in dmesg (or the emulator output).
1 change: 0 additions & 1 deletion init/ueventd.cpp
Expand Up @@ -52,7 +52,6 @@ int ueventd_main(int argc, char **argv)
*/
signal(SIGCHLD, SIG_IGN);

open_devnull_stdio();
InitKernelLogging(argv);

LOG(INFO) << "ueventd started!";
Expand Down
24 changes: 0 additions & 24 deletions init/util.cpp
Expand Up @@ -316,30 +316,6 @@ int wait_for_file(const char *filename, int timeout)
return ret;
}

void open_devnull_stdio(void)
{
int fd = open("/sys/fs/selinux/null", O_RDWR);
if (fd == -1) {
/* Fail silently.
* stdout/stderr isn't available, and because
* klog_init() is called after open_devnull_stdio(), we can't
* log to dmesg. Reordering klog_init() to be called before
* open_devnull_stdio() isn't an option either, as then klog_fd
* will be assigned 0 or 1, which will end up getting clobbered
* by the code below. There's nowhere good to log.
*/

exit(1);
}

dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2) {
close(fd);
}
}

void import_kernel_cmdline(bool in_qemu,
std::function<void(const std::string&, const std::string&, bool)> fn) {
std::string cmdline;
Expand Down
1 change: 0 additions & 1 deletion init/util.h
Expand Up @@ -54,7 +54,6 @@ void sanitize(char *p);
void make_link_init(const char *oldpath, const char *newpath);
void remove_link(const char *oldpath, const char *newpath);
int wait_for_file(const char *filename, int timeout);
void open_devnull_stdio(void);
void import_kernel_cmdline(bool in_qemu,
std::function<void(const std::string&, const std::string&, bool)>);
int make_dir(const char *path, mode_t mode);
Expand Down
1 change: 0 additions & 1 deletion init/watchdogd.cpp
Expand Up @@ -28,7 +28,6 @@
#define DEV_NAME "/dev/watchdog"

int watchdogd_main(int argc, char **argv) {
open_devnull_stdio();
InitKernelLogging(argv);

int interval = 10;
Expand Down
2 changes: 1 addition & 1 deletion libcutils/Android.mk
Expand Up @@ -95,7 +95,7 @@ LOCAL_SRC_FILES := $(libcutils_common_sources) \
android_reboot.c \
ashmem-dev.c \
debugger.c \
klog.c \
klog.cpp \
partition_utils.c \
properties.c \
qtaguid.c \
Expand Down
27 changes: 14 additions & 13 deletions libcutils/klog.c → libcutils/klog.cpp
Expand Up @@ -26,7 +26,6 @@

#include <cutils/klog.h>

static int klog_fd = -1;
static int klog_level = KLOG_DEFAULT_LEVEL;

int klog_get_level(void) {
Expand All @@ -38,31 +37,33 @@ void klog_set_level(int level) {
}

void klog_init(void) {
if (klog_fd >= 0) return; /* Already initialized */

klog_fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC);
if (klog_fd >= 0) {
return;
}
}

static const char* name = "/dev/__kmsg__";
if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
klog_fd = open(name, O_WRONLY | O_CLOEXEC);
unlink(name);
static int __open_klog(void) {
int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC);
if (fd == -1) {
static const char* name = "/dev/__kmsg__";
if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
fd = open(name, O_WRONLY | O_CLOEXEC);
unlink(name);
}
}
return fd;
}

#define LOG_BUF_MAX 512

void klog_writev(int level, const struct iovec* iov, int iov_count) {
if (level > klog_level) return;
if (klog_fd < 0) klog_init();
if (klog_fd < 0) return;

static int klog_fd = __open_klog();
if (klog_fd == -1) return;
TEMP_FAILURE_RETRY(writev(klog_fd, iov, iov_count));
}

void klog_write(int level, const char* fmt, ...) {
if (level > klog_level) return;

char buf[LOG_BUF_MAX];
va_list ap;
va_start(ap, fmt);
Expand Down

0 comments on commit 171a829

Please sign in to comment.