blob: ee75ffc86fa01b3310253fe8750b5c8978162003 [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 Hughesda40c002015-03-27 23:20:44 -070019#include <stdlib.h>
20#include <string.h>
21#include <sys/uio.h>
22
23#include <selinux/selinux.h>
24
Elliott Hughesf86b5a62016-06-24 15:12:21 -070025static const int kLogSeverityToKLogLevel[] = {
26 KLOG_NOTICE_LEVEL, KLOG_DEBUG_LEVEL, KLOG_INFO_LEVEL,
27 KLOG_WARNING_LEVEL, KLOG_ERROR_LEVEL, KLOG_ERROR_LEVEL,
28};
29static_assert(arraysize(kLogSeverityToKLogLevel) == android::base::FATAL + 1,
30 "Mismatch in size of kLogSeverityToKLogLevel and values in LogSeverity");
Elliott Hughesda40c002015-03-27 23:20:44 -070031
Elliott Hughesf86b5a62016-06-24 15:12:21 -070032static 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 Kralevich5db8d6a2016-01-16 16:19:17 -080035 if (level > klog_get_level()) return;
36
Elliott Hughese5ce30f2015-05-06 19:19:24 -070037 // 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 Hughesf86b5a62016-06-24 15:12:21 -070041 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 Hughese5ce30f2015-05-06 19:19:24 -070045 }
Elliott Hughesda40c002015-03-27 23:20:44 -070046
Elliott Hughese5ce30f2015-05-06 19:19:24 -070047 iovec iov[1];
48 iov[0].iov_base = buf;
Elliott Hughesf86b5a62016-06-24 15:12:21 -070049 iov[0].iov_len = size;
Elliott Hughese5ce30f2015-05-06 19:19:24 -070050 klog_writev(level, iov, 1);
Elliott Hughesda40c002015-03-27 23:20:44 -070051}
52
Elliott Hughesf86b5a62016-06-24 15:12:21 -070053void InitKernelLogging(char* argv[]) {
54 android::base::InitLogging(argv, &KernelLogger);
55
56 klog_init();
57 klog_set_level(KLOG_NOTICE_LEVEL);
Elliott Hughesda40c002015-03-27 23:20:44 -070058}
59
60int selinux_klog_callback(int type, const char *fmt, ...) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070061 android::base::LogSeverity severity = android::base::ERROR;
Elliott Hughesda40c002015-03-27 23:20:44 -070062 if (type == SELINUX_WARNING) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070063 severity = android::base::WARNING;
Elliott Hughesda40c002015-03-27 23:20:44 -070064 } else if (type == SELINUX_INFO) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070065 severity = android::base::INFO;
Elliott Hughesda40c002015-03-27 23:20:44 -070066 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -070067 char buf[1024];
Elliott Hughesda40c002015-03-27 23:20:44 -070068 va_list ap;
69 va_start(ap, fmt);
Elliott Hughesf86b5a62016-06-24 15:12:21 -070070 vsnprintf(buf, sizeof(buf), fmt, ap);
Elliott Hughesda40c002015-03-27 23:20:44 -070071 va_end(ap);
Elliott Hughesf86b5a62016-06-24 15:12:21 -070072 KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
Elliott Hughesda40c002015-03-27 23:20:44 -070073 return 0;
74}