blob: 391bc1f86d6ce462029165849e2b0c06ce0260df [file] [log] [blame]
Elliott Hughesda40c002015-03-27 23:20:44 -07001/*
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
Elliott Hughese5ce30f2015-05-06 19:19:24 -070017#include "log.h"
18
Elliott Hughes171a8292016-06-29 16:16:41 -070019#include <fcntl.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070020#include <linux/audit.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070021#include <string.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070022
Tom Cherry3f5eaae52017-04-06 16:30:22 -070023#include <android-base/logging.h>
Tom Cherryd8db7ab2017-08-17 17:28:30 -070024#include <cutils/android_reboot.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070025#include <selinux/selinux.h>
26
Tom Cherryd8db7ab2017-08-17 17:28:30 -070027#include "reboot.h"
28
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Tom Cherryd8db7ab2017-08-17 17:28:30 -070032static void RebootAborter(const char* abort_message) {
33 // DoReboot() does a lot to try to shutdown the system cleanly. If something happens to call
34 // LOG(FATAL) in the shutdown path, we want to catch this and immediately use the syscall to
35 // reboot instead of recursing here.
36 static bool has_aborted = false;
37 if (!has_aborted) {
38 has_aborted = true;
39 // Do not queue "shutdown" trigger since we want to shutdown immediately and it's not likely
40 // that we can even run the ActionQueue at this point.
41 DoReboot(ANDROID_RB_RESTART2, "reboot", "bootloader", false);
42 } else {
43 RebootSystem(ANDROID_RB_RESTART2, "bootloader");
44 }
45}
46
Elliott Hughesf86b5a62016-06-24 15:12:21 -070047void InitKernelLogging(char* argv[]) {
Elliott Hughes171a8292016-06-29 16:16:41 -070048 // Make stdin/stdout/stderr all point to /dev/null.
49 int fd = open("/sys/fs/selinux/null", O_RDWR);
50 if (fd == -1) {
51 int saved_errno = errno;
Tom Cherryd8db7ab2017-08-17 17:28:30 -070052 android::base::InitLogging(argv, &android::base::KernelLogger, RebootAborter);
Elliott Hughes171a8292016-06-29 16:16:41 -070053 errno = saved_errno;
54 PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
55 }
56 dup2(fd, 0);
57 dup2(fd, 1);
58 dup2(fd, 2);
59 if (fd > 2) close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -070060
Tom Cherryd8db7ab2017-08-17 17:28:30 -070061 android::base::InitLogging(argv, &android::base::KernelLogger, RebootAborter);
Elliott Hughesda40c002015-03-27 23:20:44 -070062}
63
64int selinux_klog_callback(int type, const char *fmt, ...) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070065 android::base::LogSeverity severity = android::base::ERROR;
Elliott Hughesda40c002015-03-27 23:20:44 -070066 if (type == SELINUX_WARNING) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070067 severity = android::base::WARNING;
Elliott Hughesda40c002015-03-27 23:20:44 -070068 } else if (type == SELINUX_INFO) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070069 severity = android::base::INFO;
Elliott Hughesda40c002015-03-27 23:20:44 -070070 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -070071 char buf[1024];
Elliott Hughesda40c002015-03-27 23:20:44 -070072 va_list ap;
73 va_start(ap, fmt);
Elliott Hughesd8f93562017-04-14 19:47:05 +000074 vsnprintf(buf, sizeof(buf), fmt, ap);
Elliott Hughesda40c002015-03-27 23:20:44 -070075 va_end(ap);
Elliott Hughesd8f93562017-04-14 19:47:05 +000076 android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
Elliott Hughesda40c002015-03-27 23:20:44 -070077 return 0;
78}
Tom Cherry81f5d3e2017-06-22 12:53:17 -070079
80} // namespace init
81} // namespace android