init: clean up logging initialization
Clean up a few mistakes in logging initialization 1) Only init needs to clear stdout/stderr/stdin, so remove this from ueventd, watchdogd, and subcontext init 2) Only init should reboot due to FATAL errors. This was true even before this change due to getpid() checks, but there's no reason to not just use the DefaultAborter for other processes. 3) It's probably a mistake for FATAL logs in init to try to gracefully shutdown the system, so simply call RebootSystem() here. 4) Lastly, remove log.cpp since it's not actually shared code anymore Test: build Change-Id: Ic8c323393dc7ee98ed6bb9691361b51d0d915267
This commit is contained in:
parent
74be24d696
commit
74069d1734
|
@ -108,7 +108,6 @@ cc_library_static {
|
|||
"init.cpp",
|
||||
"init_first_stage.cpp",
|
||||
"keychords.cpp",
|
||||
"log.cpp",
|
||||
"parser.cpp",
|
||||
"persistent_properties.cpp",
|
||||
"persistent_properties.proto",
|
||||
|
|
|
@ -51,7 +51,6 @@
|
|||
#include "import_parser.h"
|
||||
#include "init_first_stage.h"
|
||||
#include "keychords.h"
|
||||
#include "log.h"
|
||||
#include "property_service.h"
|
||||
#include "reboot.h"
|
||||
#include "security.h"
|
||||
|
@ -582,6 +581,34 @@ void HandleKeychord(const std::vector<int>& keycodes) {
|
|||
}
|
||||
}
|
||||
|
||||
static void InitAborter(const char* abort_message) {
|
||||
// When init forks, it continues to use this aborter for LOG(FATAL), but we want children to
|
||||
// simply abort instead of trying to reboot the system.
|
||||
if (getpid() != 1) {
|
||||
android::base::DefaultAborter(abort_message);
|
||||
return;
|
||||
}
|
||||
|
||||
RebootSystem(ANDROID_RB_RESTART2, "bootloader");
|
||||
}
|
||||
|
||||
static 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, &android::base::KernelLogger, InitAborter);
|
||||
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);
|
||||
|
||||
android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (!strcmp(basename(argv[0]), "ueventd")) {
|
||||
return ueventd_main(argc, argv);
|
||||
|
@ -592,7 +619,7 @@ int main(int argc, char** argv) {
|
|||
}
|
||||
|
||||
if (argc > 1 && !strcmp(argv[1], "subcontext")) {
|
||||
InitKernelLogging(argv);
|
||||
android::base::InitLogging(argv, &android::base::KernelLogger);
|
||||
const BuiltinFunctionMap function_map;
|
||||
return SubcontextMain(argc, argv, &function_map);
|
||||
}
|
||||
|
|
89
init/log.cpp
89
init/log.cpp
|
@ -1,89 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2015 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 "log.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <linux/audit.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <cutils/android_reboot.h>
|
||||
#include <selinux/selinux.h>
|
||||
|
||||
#include "reboot.h"
|
||||
|
||||
namespace android {
|
||||
namespace init {
|
||||
|
||||
static void InitAborter(const char* abort_message) {
|
||||
// When init forks, it continues to use this aborter for LOG(FATAL), but we want children to
|
||||
// simply abort instead of trying to reboot the system.
|
||||
if (getpid() != 1) {
|
||||
android::base::DefaultAborter(abort_message);
|
||||
return;
|
||||
}
|
||||
|
||||
// DoReboot() does a lot to try to shutdown the system cleanly. If something happens to call
|
||||
// LOG(FATAL) in the shutdown path, we want to catch this and immediately use the syscall to
|
||||
// reboot instead of recursing here.
|
||||
static bool has_aborted = false;
|
||||
if (!has_aborted) {
|
||||
has_aborted = true;
|
||||
// Do not queue "shutdown" trigger since we want to shutdown immediately and it's not likely
|
||||
// that we can even run the ActionQueue at this point.
|
||||
DoReboot(ANDROID_RB_RESTART2, "reboot", "bootloader", false);
|
||||
} else {
|
||||
RebootSystem(ANDROID_RB_RESTART2, "bootloader");
|
||||
}
|
||||
}
|
||||
|
||||
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, &android::base::KernelLogger, InitAborter);
|
||||
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);
|
||||
|
||||
android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
|
||||
}
|
||||
|
||||
int selinux_klog_callback(int type, const char *fmt, ...) {
|
||||
android::base::LogSeverity severity = android::base::ERROR;
|
||||
if (type == SELINUX_WARNING) {
|
||||
severity = android::base::WARNING;
|
||||
} else if (type == SELINUX_INFO) {
|
||||
severity = android::base::INFO;
|
||||
}
|
||||
char buf[1024];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace init
|
||||
} // namespace android
|
32
init/log.h
32
init/log.h
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2010 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.
|
||||
*/
|
||||
|
||||
#ifndef _INIT_LOG_H_
|
||||
#define _INIT_LOG_H_
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
namespace android {
|
||||
namespace init {
|
||||
|
||||
void InitKernelLogging(char* argv[]);
|
||||
|
||||
int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3);
|
||||
|
||||
} // namespace init
|
||||
} // namespace android
|
||||
|
||||
#endif
|
|
@ -59,7 +59,6 @@
|
|||
#include <android-base/unique_fd.h>
|
||||
#include <selinux/android.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
using android::base::ParseInt;
|
||||
|
@ -448,10 +447,26 @@ void SelinuxRestoreContext() {
|
|||
selinux_android_restorecon("/sbin/sload.f2fs", 0);
|
||||
}
|
||||
|
||||
int SelinuxKlogCallback(int type, const char* fmt, ...) {
|
||||
android::base::LogSeverity severity = android::base::ERROR;
|
||||
if (type == SELINUX_WARNING) {
|
||||
severity = android::base::WARNING;
|
||||
} else if (type == SELINUX_INFO) {
|
||||
severity = android::base::INFO;
|
||||
}
|
||||
char buf[1024];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// This function sets up SELinux logging to be written to kmsg, to match init's logging.
|
||||
void SelinuxSetupKernelLogging() {
|
||||
selinux_callback cb;
|
||||
cb.func_log = selinux_klog_callback;
|
||||
cb.func_log = SelinuxKlogCallback;
|
||||
selinux_set_callback(SELINUX_CB_LOG, cb);
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,6 @@
|
|||
|
||||
#include "devices.h"
|
||||
#include "firmware_handler.h"
|
||||
#include "log.h"
|
||||
#include "selinux.h"
|
||||
#include "uevent_listener.h"
|
||||
#include "ueventd_parser.h"
|
||||
|
@ -223,7 +222,7 @@ int ueventd_main(int argc, char** argv) {
|
|||
*/
|
||||
umask(000);
|
||||
|
||||
InitKernelLogging(argv);
|
||||
android::base::InitLogging(argv, &android::base::KernelLogger);
|
||||
|
||||
LOG(INFO) << "ueventd started!";
|
||||
|
||||
|
|
|
@ -23,8 +23,6 @@
|
|||
|
||||
#include <android-base/logging.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#ifdef _INIT_INIT_H
|
||||
#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
|
||||
#endif
|
||||
|
@ -35,7 +33,7 @@ namespace android {
|
|||
namespace init {
|
||||
|
||||
int watchdogd_main(int argc, char **argv) {
|
||||
InitKernelLogging(argv);
|
||||
android::base::InitLogging(argv, &android::base::KernelLogger);
|
||||
|
||||
int interval = 10;
|
||||
if (argc >= 2) interval = atoi(argv[1]);
|
||||
|
|
Loading…
Reference in New Issue