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
17 #include "log.h"
18
19 #include <fcntl.h>
20 #include <linux/audit.h>
21 #include <string.h>
22
23 #include <android-base/logging.h>
24 #include <selinux/selinux.h>
25
InitKernelLogging(char * argv[])26 void InitKernelLogging(char* argv[]) {
27 // Make stdin/stdout/stderr all point to /dev/null.
28 int fd = open("/sys/fs/selinux/null", O_RDWR);
29 if (fd == -1) {
30 int saved_errno = errno;
31 android::base::InitLogging(argv, &android::base::KernelLogger);
32 errno = saved_errno;
33 PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
34 }
35 dup2(fd, 0);
36 dup2(fd, 1);
37 dup2(fd, 2);
38 if (fd > 2) close(fd);
39
40 android::base::InitLogging(argv, &android::base::KernelLogger);
41 }
42
selinux_klog_callback(int type,const char * fmt,...)43 int selinux_klog_callback(int type, const char *fmt, ...) {
44 android::base::LogSeverity severity = android::base::ERROR;
45 if (type == SELINUX_WARNING) {
46 severity = android::base::WARNING;
47 } else if (type == SELINUX_INFO) {
48 severity = android::base::INFO;
49 }
50 char buf[1024];
51 va_list ap;
52 va_start(ap, fmt);
53 vsnprintf(buf, sizeof(buf), fmt, ap);
54 va_end(ap);
55 android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
56 return 0;
57 }
58