summaryrefslogtreecommitdiffstats
path: root/init
diff options
context:
space:
mode:
authorTom Cherry2018-07-20 17:26:25 -0500
committerTom Cherry2018-07-20 17:30:14 -0500
commit74069d17347eb64ec932831f8383448fd16e6e0f (patch)
treeb147aa8faa8d579ba9f479ac8896b0c248ebcc0f /init
parent74be24d696f2d67293de6b9bb6e715c1728abed7 (diff)
downloadplatform-system-core-74069d17347eb64ec932831f8383448fd16e6e0f.tar.gz
platform-system-core-74069d17347eb64ec932831f8383448fd16e6e0f.tar.xz
platform-system-core-74069d17347eb64ec932831f8383448fd16e6e0f.zip
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
Diffstat (limited to 'init')
-rw-r--r--init/Android.bp1
-rw-r--r--init/init.cpp31
-rw-r--r--init/log.cpp89
-rw-r--r--init/log.h32
-rw-r--r--init/selinux.cpp19
-rw-r--r--init/ueventd.cpp3
-rw-r--r--init/watchdogd.cpp4
7 files changed, 48 insertions, 131 deletions
diff --git a/init/Android.bp b/init/Android.bp
index 5bbb7a09c..859aeb663 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -108,7 +108,6 @@ cc_library_static {
108 "init.cpp", 108 "init.cpp",
109 "init_first_stage.cpp", 109 "init_first_stage.cpp",
110 "keychords.cpp", 110 "keychords.cpp",
111 "log.cpp",
112 "parser.cpp", 111 "parser.cpp",
113 "persistent_properties.cpp", 112 "persistent_properties.cpp",
114 "persistent_properties.proto", 113 "persistent_properties.proto",
diff --git a/init/init.cpp b/init/init.cpp
index 686cd6ed9..12c3d6c4b 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -51,7 +51,6 @@
51#include "import_parser.h" 51#include "import_parser.h"
52#include "init_first_stage.h" 52#include "init_first_stage.h"
53#include "keychords.h" 53#include "keychords.h"
54#include "log.h"
55#include "property_service.h" 54#include "property_service.h"
56#include "reboot.h" 55#include "reboot.h"
57#include "security.h" 56#include "security.h"
@@ -582,6 +581,34 @@ void HandleKeychord(const std::vector<int>& keycodes) {
582 } 581 }
583} 582}
584 583
584static void InitAborter(const char* abort_message) {
585 // When init forks, it continues to use this aborter for LOG(FATAL), but we want children to
586 // simply abort instead of trying to reboot the system.
587 if (getpid() != 1) {
588 android::base::DefaultAborter(abort_message);
589 return;
590 }
591
592 RebootSystem(ANDROID_RB_RESTART2, "bootloader");
593}
594
595static void InitKernelLogging(char* argv[]) {
596 // Make stdin/stdout/stderr all point to /dev/null.
597 int fd = open("/sys/fs/selinux/null", O_RDWR);
598 if (fd == -1) {
599 int saved_errno = errno;
600 android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
601 errno = saved_errno;
602 PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
603 }
604 dup2(fd, 0);
605 dup2(fd, 1);
606 dup2(fd, 2);
607 if (fd > 2) close(fd);
608
609 android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
610}
611
585int main(int argc, char** argv) { 612int main(int argc, char** argv) {
586 if (!strcmp(basename(argv[0]), "ueventd")) { 613 if (!strcmp(basename(argv[0]), "ueventd")) {
587 return ueventd_main(argc, argv); 614 return ueventd_main(argc, argv);
@@ -592,7 +619,7 @@ int main(int argc, char** argv) {
592 } 619 }
593 620
594 if (argc > 1 && !strcmp(argv[1], "subcontext")) { 621 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
595 InitKernelLogging(argv); 622 android::base::InitLogging(argv, &android::base::KernelLogger);
596 const BuiltinFunctionMap function_map; 623 const BuiltinFunctionMap function_map;
597 return SubcontextMain(argc, argv, &function_map); 624 return SubcontextMain(argc, argv, &function_map);
598 } 625 }
diff --git a/init/log.cpp b/init/log.cpp
deleted file mode 100644
index 6198fc25f..000000000
--- a/init/log.cpp
+++ /dev/null
@@ -1,89 +0,0 @@
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "log.h"
18
19#include <fcntl.h>
20#include <linux/audit.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <android-base/logging.h>
25#include <cutils/android_reboot.h>
26#include <selinux/selinux.h>
27
28#include "reboot.h"
29
30namespace android {
31namespace init {
32
33static void InitAborter(const char* abort_message) {
34 // When init forks, it continues to use this aborter for LOG(FATAL), but we want children to
35 // simply abort instead of trying to reboot the system.
36 if (getpid() != 1) {
37 android::base::DefaultAborter(abort_message);
38 return;
39 }
40
41 // DoReboot() does a lot to try to shutdown the system cleanly. If something happens to call
42 // LOG(FATAL) in the shutdown path, we want to catch this and immediately use the syscall to
43 // reboot instead of recursing here.
44 static bool has_aborted = false;
45 if (!has_aborted) {
46 has_aborted = true;
47 // Do not queue "shutdown" trigger since we want to shutdown immediately and it's not likely
48 // that we can even run the ActionQueue at this point.
49 DoReboot(ANDROID_RB_RESTART2, "reboot", "bootloader", false);
50 } else {
51 RebootSystem(ANDROID_RB_RESTART2, "bootloader");
52 }
53}
54
55void InitKernelLogging(char* argv[]) {
56 // Make stdin/stdout/stderr all point to /dev/null.
57 int fd = open("/sys/fs/selinux/null", O_RDWR);
58 if (fd == -1) {
59 int saved_errno = errno;
60 android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
61 errno = saved_errno;
62 PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
63 }
64 dup2(fd, 0);
65 dup2(fd, 1);
66 dup2(fd, 2);
67 if (fd > 2) close(fd);
68
69 android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
70}
71
72int selinux_klog_callback(int type, const char *fmt, ...) {
73 android::base::LogSeverity severity = android::base::ERROR;
74 if (type == SELINUX_WARNING) {
75 severity = android::base::WARNING;
76 } else if (type == SELINUX_INFO) {
77 severity = android::base::INFO;
78 }
79 char buf[1024];
80 va_list ap;
81 va_start(ap, fmt);
82 vsnprintf(buf, sizeof(buf), fmt, ap);
83 va_end(ap);
84 android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
85 return 0;
86}
87
88} // namespace init
89} // namespace android
diff --git a/init/log.h b/init/log.h
deleted file mode 100644
index 5a4eba6b5..000000000
--- a/init/log.h
+++ /dev/null
@@ -1,32 +0,0 @@
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _INIT_LOG_H_
18#define _INIT_LOG_H_
19
20#include <sys/cdefs.h>
21
22namespace android {
23namespace init {
24
25void InitKernelLogging(char* argv[]);
26
27int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3);
28
29} // namespace init
30} // namespace android
31
32#endif
diff --git a/init/selinux.cpp b/init/selinux.cpp
index 0ba5c4ae3..94f206ede 100644
--- a/init/selinux.cpp
+++ b/init/selinux.cpp
@@ -59,7 +59,6 @@
59#include <android-base/unique_fd.h> 59#include <android-base/unique_fd.h>
60#include <selinux/android.h> 60#include <selinux/android.h>
61 61
62#include "log.h"
63#include "util.h" 62#include "util.h"
64 63
65using android::base::ParseInt; 64using android::base::ParseInt;
@@ -448,10 +447,26 @@ void SelinuxRestoreContext() {
448 selinux_android_restorecon("/sbin/sload.f2fs", 0); 447 selinux_android_restorecon("/sbin/sload.f2fs", 0);
449} 448}
450 449
450int SelinuxKlogCallback(int type, const char* fmt, ...) {
451 android::base::LogSeverity severity = android::base::ERROR;
452 if (type == SELINUX_WARNING) {
453 severity = android::base::WARNING;
454 } else if (type == SELINUX_INFO) {
455 severity = android::base::INFO;
456 }
457 char buf[1024];
458 va_list ap;
459 va_start(ap, fmt);
460 vsnprintf(buf, sizeof(buf), fmt, ap);
461 va_end(ap);
462 android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
463 return 0;
464}
465
451// This function sets up SELinux logging to be written to kmsg, to match init's logging. 466// This function sets up SELinux logging to be written to kmsg, to match init's logging.
452void SelinuxSetupKernelLogging() { 467void SelinuxSetupKernelLogging() {
453 selinux_callback cb; 468 selinux_callback cb;
454 cb.func_log = selinux_klog_callback; 469 cb.func_log = SelinuxKlogCallback;
455 selinux_set_callback(SELINUX_CB_LOG, cb); 470 selinux_set_callback(SELINUX_CB_LOG, cb);
456} 471}
457 472
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index b42a4c62a..cd45a3fb7 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -36,7 +36,6 @@
36 36
37#include "devices.h" 37#include "devices.h"
38#include "firmware_handler.h" 38#include "firmware_handler.h"
39#include "log.h"
40#include "selinux.h" 39#include "selinux.h"
41#include "uevent_listener.h" 40#include "uevent_listener.h"
42#include "ueventd_parser.h" 41#include "ueventd_parser.h"
@@ -223,7 +222,7 @@ int ueventd_main(int argc, char** argv) {
223 */ 222 */
224 umask(000); 223 umask(000);
225 224
226 InitKernelLogging(argv); 225 android::base::InitLogging(argv, &android::base::KernelLogger);
227 226
228 LOG(INFO) << "ueventd started!"; 227 LOG(INFO) << "ueventd started!";
229 228
diff --git a/init/watchdogd.cpp b/init/watchdogd.cpp
index e0164b419..e03a2c31d 100644
--- a/init/watchdogd.cpp
+++ b/init/watchdogd.cpp
@@ -23,8 +23,6 @@
23 23
24#include <android-base/logging.h> 24#include <android-base/logging.h>
25 25
26#include "log.h"
27
28#ifdef _INIT_INIT_H 26#ifdef _INIT_INIT_H
29#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals" 27#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
30#endif 28#endif
@@ -35,7 +33,7 @@ namespace android {
35namespace init { 33namespace init {
36 34
37int watchdogd_main(int argc, char **argv) { 35int watchdogd_main(int argc, char **argv) {
38 InitKernelLogging(argv); 36 android::base::InitLogging(argv, &android::base::KernelLogger);
39 37
40 int interval = 10; 38 int interval = 10;
41 if (argc >= 2) interval = atoi(argv[1]); 39 if (argc >= 2) interval = atoi(argv[1]);