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