blob: 3934ca02f28b094ded219d06e5a1bc4f7d4f58a1 [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>
Elliott Hughesda40c002015-03-27 23:20:44 -070020#include <stdlib.h>
21#include <string.h>
Elliott Hughes171a8292016-06-29 16:16:41 -070022#include <sys/stat.h>
23#include <sys/types.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070024#include <sys/uio.h>
25
26#include <selinux/selinux.h>
27
Elliott Hughesf86b5a62016-06-24 15:12:21 -070028static const int kLogSeverityToKLogLevel[] = {
29 KLOG_NOTICE_LEVEL, KLOG_DEBUG_LEVEL, KLOG_INFO_LEVEL,
30 KLOG_WARNING_LEVEL, KLOG_ERROR_LEVEL, KLOG_ERROR_LEVEL,
31};
32static_assert(arraysize(kLogSeverityToKLogLevel) == android::base::FATAL + 1,
33 "Mismatch in size of kLogSeverityToKLogLevel and values in LogSeverity");
Elliott Hughesda40c002015-03-27 23:20:44 -070034
Elliott Hughesf86b5a62016-06-24 15:12:21 -070035static 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 Kralevich5db8d6a2016-01-16 16:19:17 -080038 if (level > klog_get_level()) return;
39
Elliott Hughese5ce30f2015-05-06 19:19:24 -070040 // 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 Hughesf86b5a62016-06-24 15:12:21 -070044 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 Hughese5ce30f2015-05-06 19:19:24 -070048 }
Elliott Hughesda40c002015-03-27 23:20:44 -070049
Elliott Hughese5ce30f2015-05-06 19:19:24 -070050 iovec iov[1];
51 iov[0].iov_base = buf;
Elliott Hughesf86b5a62016-06-24 15:12:21 -070052 iov[0].iov_len = size;
Elliott Hughese5ce30f2015-05-06 19:19:24 -070053 klog_writev(level, iov, 1);
Elliott Hughesda40c002015-03-27 23:20:44 -070054}
55
Elliott Hughesf86b5a62016-06-24 15:12:21 -070056void InitKernelLogging(char* argv[]) {
Elliott Hughes171a8292016-06-29 16:16:41 -070057 // 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 Hughesf86b5a62016-06-24 15:12:21 -070069
Elliott Hughes171a8292016-06-29 16:16:41 -070070 android::base::InitLogging(argv, &KernelLogger);
Elliott Hughesf86b5a62016-06-24 15:12:21 -070071 klog_set_level(KLOG_NOTICE_LEVEL);
Elliott Hughesda40c002015-03-27 23:20:44 -070072}
73
74int selinux_klog_callback(int type, const char *fmt, ...) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070075 android::base::LogSeverity severity = android::base::ERROR;
Elliott Hughesda40c002015-03-27 23:20:44 -070076 if (type == SELINUX_WARNING) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070077 severity = android::base::WARNING;
Elliott Hughesda40c002015-03-27 23:20:44 -070078 } else if (type == SELINUX_INFO) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070079 severity = android::base::INFO;
Elliott Hughesda40c002015-03-27 23:20:44 -070080 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -070081 char buf[1024];
Elliott Hughesda40c002015-03-27 23:20:44 -070082 va_list ap;
83 va_start(ap, fmt);
Elliott Hughesf86b5a62016-06-24 15:12:21 -070084 vsnprintf(buf, sizeof(buf), fmt, ap);
Elliott Hughesda40c002015-03-27 23:20:44 -070085 va_end(ap);
Elliott Hughesf86b5a62016-06-24 15:12:21 -070086 KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
Elliott Hughesda40c002015-03-27 23:20:44 -070087 return 0;
88}