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