Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 17 | #include "log.h" |
| 18 | |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 20 | #include <linux/audit.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 21 | #include <string.h> |
Tom Cherry | 1ca8324 | 2017-08-25 15:10:48 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 23 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 25 | #include <cutils/android_reboot.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 26 | #include <selinux/selinux.h> |
| 27 | |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 28 | #include "reboot.h" |
| 29 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace init { |
| 32 | |
Tom Cherry | 1ca8324 | 2017-08-25 15:10:48 -0700 | [diff] [blame] | 33 | static 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 | |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 55 | void InitKernelLogging(char* argv[]) { |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 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; |
Tom Cherry | 1ca8324 | 2017-08-25 15:10:48 -0700 | [diff] [blame] | 60 | android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter); |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 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); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 68 | |
Tom Cherry | 1ca8324 | 2017-08-25 15:10:48 -0700 | [diff] [blame] | 69 | android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | int selinux_klog_callback(int type, const char *fmt, ...) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 73 | android::base::LogSeverity severity = android::base::ERROR; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 74 | if (type == SELINUX_WARNING) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 75 | severity = android::base::WARNING; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 76 | } else if (type == SELINUX_INFO) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 77 | severity = android::base::INFO; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 78 | } |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 79 | char buf[1024]; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 80 | va_list ap; |
| 81 | va_start(ap, fmt); |
Elliott Hughes | d8f9356 | 2017-04-14 19:47:05 +0000 | [diff] [blame] | 82 | vsnprintf(buf, sizeof(buf), fmt, ap); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 83 | va_end(ap); |
Elliott Hughes | d8f9356 | 2017-04-14 19:47:05 +0000 | [diff] [blame] | 84 | android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 85 | return 0; |
| 86 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 87 | |
| 88 | } // namespace init |
| 89 | } // namespace android |