blob: d32f2dae88d3104b43e798ae76f44b4b3e78e65c [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
17#include <stdlib.h>
18#include <string.h>
19#include <sys/uio.h>
20
21#include <selinux/selinux.h>
22
23#include "log.h"
24
25static void init_klog_vwrite(int level, const char* fmt, va_list ap) {
26 static const char* tag = basename(getprogname());
27
28 char prefix[64];
29 snprintf(prefix, sizeof(prefix), "<%d>%s: ", level, tag);
30
31 char msg[512];
32 vsnprintf(msg, sizeof(msg), fmt, ap);
33
34 iovec iov[2];
35 iov[0].iov_base = prefix;
36 iov[0].iov_len = strlen(prefix);
37 iov[1].iov_base = msg;
38 iov[1].iov_len = strlen(msg);
39
40 klog_writev(level, iov, 2);
41}
42
43void init_klog_write(int level, const char* fmt, ...) {
44 va_list ap;
45 va_start(ap, fmt);
46 init_klog_vwrite(level, fmt, ap);
47 va_end(ap);
48}
49
50int selinux_klog_callback(int type, const char *fmt, ...) {
51 int level = KLOG_ERROR_LEVEL;
52 if (type == SELINUX_WARNING) {
53 level = KLOG_WARNING_LEVEL;
54 } else if (type == SELINUX_INFO) {
55 level = KLOG_INFO_LEVEL;
56 }
57 va_list ap;
58 va_start(ap, fmt);
59 init_klog_vwrite(level, fmt, ap);
60 va_end(ap);
61 return 0;
62}