blob: 1830077e19834a76a67a76250c798ba0dde5baea [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>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070020#include <linux/audit.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070021#include <string.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070022
Tom Cherry3f5eaae52017-04-06 16:30:22 -070023#include <android-base/logging.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070024#include <selinux/selinux.h>
25
Tom Cherry81f5d3e2017-06-22 12:53:17 -070026namespace android {
27namespace init {
28
Elliott Hughesf86b5a62016-06-24 15:12:21 -070029void InitKernelLogging(char* argv[]) {
Elliott Hughes171a8292016-06-29 16:16:41 -070030 // 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;
Elliott Hughes7bc87a52016-08-04 16:09:39 -070034 android::base::InitLogging(argv, &android::base::KernelLogger);
Elliott Hughes171a8292016-06-29 16:16:41 -070035 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);
Elliott Hughesf86b5a62016-06-24 15:12:21 -070042
Elliott Hughes7bc87a52016-08-04 16:09:39 -070043 android::base::InitLogging(argv, &android::base::KernelLogger);
Elliott Hughesda40c002015-03-27 23:20:44 -070044}
45
46int selinux_klog_callback(int type, const char *fmt, ...) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070047 android::base::LogSeverity severity = android::base::ERROR;
Elliott Hughesda40c002015-03-27 23:20:44 -070048 if (type == SELINUX_WARNING) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070049 severity = android::base::WARNING;
Elliott Hughesda40c002015-03-27 23:20:44 -070050 } else if (type == SELINUX_INFO) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070051 severity = android::base::INFO;
Elliott Hughesda40c002015-03-27 23:20:44 -070052 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -070053 char buf[1024];
Elliott Hughesda40c002015-03-27 23:20:44 -070054 va_list ap;
55 va_start(ap, fmt);
Elliott Hughesd8f93562017-04-14 19:47:05 +000056 vsnprintf(buf, sizeof(buf), fmt, ap);
Elliott Hughesda40c002015-03-27 23:20:44 -070057 va_end(ap);
Elliott Hughesd8f93562017-04-14 19:47:05 +000058 android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
Elliott Hughesda40c002015-03-27 23:20:44 -070059 return 0;
60}
Tom Cherry81f5d3e2017-06-22 12:53:17 -070061
62} // namespace init
63} // namespace android