1da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes/*
2da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * Copyright (C) 2015 The Android Open Source Project
3da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes *
4da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * you may not use this file except in compliance with the License.
6da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * You may obtain a copy of the License at
7da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes *
8da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes *
10da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * See the License for the specific language governing permissions and
14da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes * limitations under the License.
15da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes */
16da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes
17e5ce30fed81d1918a259be092dcd8bfffc3c2649Elliott Hughes#include "log.h"
18e5ce30fed81d1918a259be092dcd8bfffc3c2649Elliott Hughes
19171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes#include <fcntl.h>
203f5eaae526413a29de899270714469c76dc91ec8Tom Cherry#include <linux/audit.h>
21da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes#include <string.h>
22da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes
233f5eaae526413a29de899270714469c76dc91ec8Tom Cherry#include <android-base/logging.h>
24da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes#include <selinux/selinux.h>
25da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes
2681f5d3ebef2c3789737bf718fc2a2cdd7b9e8b33Tom Cherrynamespace android {
2781f5d3ebef2c3789737bf718fc2a2cdd7b9e8b33Tom Cherrynamespace init {
2881f5d3ebef2c3789737bf718fc2a2cdd7b9e8b33Tom Cherry
29f86b5a6b90619e02d1d034ef7b0adc3b439f4abbElliott Hughesvoid InitKernelLogging(char* argv[]) {
30171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes    // Make stdin/stdout/stderr all point to /dev/null.
31171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes    int fd = open("/sys/fs/selinux/null", O_RDWR);
32171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes    if (fd == -1) {
33171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes        int saved_errno = errno;
347bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughes        android::base::InitLogging(argv, &android::base::KernelLogger);
35171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes        errno = saved_errno;
36171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes        PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
37171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes    }
38171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes    dup2(fd, 0);
39171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes    dup2(fd, 1);
40171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes    dup2(fd, 2);
41171a829c39d9298432505fe943ad7128aeefe2b2Elliott Hughes    if (fd > 2) close(fd);
42f86b5a6b90619e02d1d034ef7b0adc3b439f4abbElliott Hughes
437bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughes    android::base::InitLogging(argv, &android::base::KernelLogger);
44da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes}
45da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes
46da40c00137f75543a69972f1be506e2d14a41845Elliott Hughesint selinux_klog_callback(int type, const char *fmt, ...) {
47f86b5a6b90619e02d1d034ef7b0adc3b439f4abbElliott Hughes    android::base::LogSeverity severity = android::base::ERROR;
48da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes    if (type == SELINUX_WARNING) {
49f86b5a6b90619e02d1d034ef7b0adc3b439f4abbElliott Hughes        severity = android::base::WARNING;
50da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes    } else if (type == SELINUX_INFO) {
51f86b5a6b90619e02d1d034ef7b0adc3b439f4abbElliott Hughes        severity = android::base::INFO;
52da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes    }
53f86b5a6b90619e02d1d034ef7b0adc3b439f4abbElliott Hughes    char buf[1024];
54da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes    va_list ap;
55da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes    va_start(ap, fmt);
56d8f9356bec6725dfbfb8b02d553c720c91f3f142Elliott Hughes    vsnprintf(buf, sizeof(buf), fmt, ap);
57da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes    va_end(ap);
58d8f9356bec6725dfbfb8b02d553c720c91f3f142Elliott Hughes    android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
59da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes    return 0;
60da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes}
6181f5d3ebef2c3789737bf718fc2a2cdd7b9e8b33Tom Cherry
6281f5d3ebef2c3789737bf718fc2a2cdd7b9e8b33Tom Cherry}  // namespace init
6381f5d3ebef2c3789737bf718fc2a2cdd7b9e8b33Tom Cherry}  // namespace android
64