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> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 24 | #include <sys/uio.h> |
| 25 | |
| 26 | #include <selinux/selinux.h> |
| 27 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 28 | static const int kLogSeverityToKLogLevel[] = { |
Elliott Hughes | 35f5d04 | 2016-07-26 09:32:33 -0700 | [diff] [blame] | 29 | [android::base::VERBOSE] = KLOG_DEBUG_LEVEL, |
| 30 | [android::base::DEBUG] = KLOG_DEBUG_LEVEL, |
| 31 | [android::base::INFO] = KLOG_INFO_LEVEL, |
| 32 | [android::base::WARNING] = KLOG_WARNING_LEVEL, |
| 33 | [android::base::ERROR] = KLOG_ERROR_LEVEL, |
| 34 | [android::base::FATAL] = KLOG_ERROR_LEVEL, |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 35 | }; |
| 36 | static_assert(arraysize(kLogSeverityToKLogLevel) == android::base::FATAL + 1, |
| 37 | "Mismatch in size of kLogSeverityToKLogLevel and values in LogSeverity"); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 39 | static void KernelLogger(android::base::LogId, android::base::LogSeverity severity, |
| 40 | const char* tag, const char*, unsigned int, const char* msg) { |
| 41 | int level = kLogSeverityToKLogLevel[severity]; |
Nick Kralevich | 5db8d6a | 2016-01-16 16:19:17 -0800 | [diff] [blame] | 42 | if (level > klog_get_level()) return; |
| 43 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 44 | // The kernel's printk buffer is only 1024 bytes. |
| 45 | // TODO: should we automatically break up long lines into multiple lines? |
| 46 | // Or we could log but with something like "..." at the end? |
| 47 | char buf[1024]; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 48 | size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg); |
| 49 | if (size > sizeof(buf)) { |
| 50 | size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n", |
| 51 | level, tag, size); |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 52 | } |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 53 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 54 | iovec iov[1]; |
| 55 | iov[0].iov_base = buf; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 56 | iov[0].iov_len = size; |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 57 | klog_writev(level, iov, 1); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 60 | void InitKernelLogging(char* argv[]) { |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 61 | // Make stdin/stdout/stderr all point to /dev/null. |
| 62 | int fd = open("/sys/fs/selinux/null", O_RDWR); |
| 63 | if (fd == -1) { |
| 64 | int saved_errno = errno; |
| 65 | android::base::InitLogging(argv, &KernelLogger); |
| 66 | errno = saved_errno; |
| 67 | PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null"; |
| 68 | } |
| 69 | dup2(fd, 0); |
| 70 | dup2(fd, 1); |
| 71 | dup2(fd, 2); |
| 72 | if (fd > 2) close(fd); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 74 | android::base::InitLogging(argv, &KernelLogger); |
Elliott Hughes | 35f5d04 | 2016-07-26 09:32:33 -0700 | [diff] [blame] | 75 | klog_set_level(KLOG_INFO_LEVEL); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int selinux_klog_callback(int type, const char *fmt, ...) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 79 | android::base::LogSeverity severity = android::base::ERROR; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 80 | if (type == SELINUX_WARNING) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 81 | severity = android::base::WARNING; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 82 | } else if (type == SELINUX_INFO) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 83 | severity = android::base::INFO; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 84 | } |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 85 | char buf[1024]; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 86 | va_list ap; |
| 87 | va_start(ap, fmt); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 88 | vsnprintf(buf, sizeof(buf), fmt, ap); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 89 | va_end(ap); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 90 | KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |