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