1/*
2 * Copyright (C) 2016 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/log_id.h>
18#include <private/android_logger.h>
19
20#include <nativehelper/JNIHelp.h>
21#include "jni.h"
22
23#include "core_jni_helpers.h"
24#include "eventlog_helper.h"
25
26namespace android {
27
28constexpr char kSecurityLogEventClass[] = "android/app/admin/SecurityLog$SecurityEvent";
29template class EventLogHelper<log_id_t::LOG_ID_SECURITY, kSecurityLogEventClass>;
30using SLog = EventLogHelper<log_id_t::LOG_ID_SECURITY, kSecurityLogEventClass>;
31
32static jboolean android_app_admin_SecurityLog_isLoggingEnabled(JNIEnv* env,
33                                                    jobject /* clazz */) {
34    return (bool)__android_log_security();
35}
36
37static void android_app_admin_SecurityLog_readEvents(JNIEnv* env, jobject /* clazz */,
38                                             jobject out) {
39
40    if (out == NULL) {
41        jniThrowNullPointerException(env, NULL);
42        return;
43    }
44    SLog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, out);
45}
46
47static void android_app_admin_SecurityLog_readEventsSince(JNIEnv* env, jobject /* clazz */,
48                                             jlong timestamp,
49                                             jobject out) {
50
51    if (out == NULL) {
52        jniThrowNullPointerException(env, NULL);
53        return;
54    }
55    SLog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, timestamp, out);
56}
57
58static void android_app_admin_SecurityLog_readPreviousEvents(JNIEnv* env, jobject /* clazz */,
59                                             jobject out) {
60
61    if (out == NULL) {
62        jniThrowNullPointerException(env, NULL);
63        return;
64    }
65    SLog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_PSTORE, 0, out);
66}
67
68static void android_app_admin_SecurityLog_readEventsOnWrapping(JNIEnv* env, jobject /* clazz */,
69                                             jlong timestamp,
70                                             jobject out) {
71    if (out == NULL) {
72        jniThrowNullPointerException(env, NULL);
73        return;
74    }
75    SLog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_WRAP, timestamp,
76            out);
77}
78
79/*
80 * JNI registration.
81 */
82static const JNINativeMethod gRegisterMethods[] = {
83    /* name, signature, funcPtr */
84    { "isLoggingEnabled",
85      "()Z",
86      (void*) android_app_admin_SecurityLog_isLoggingEnabled
87    },
88    { "writeEvent",
89      "(ILjava/lang/String;)I",
90      (void*) SLog::writeEventString
91    },
92    { "writeEvent",
93      "(I[Ljava/lang/Object;)I",
94      (void*) SLog::writeEventArray
95    },
96    { "readEvents",
97      "(Ljava/util/Collection;)V",
98      (void*) android_app_admin_SecurityLog_readEvents
99    },
100    { "readEventsSince",
101      "(JLjava/util/Collection;)V",
102      (void*) android_app_admin_SecurityLog_readEventsSince
103    },
104    { "readPreviousEvents",
105      "(Ljava/util/Collection;)V",
106      (void*) android_app_admin_SecurityLog_readPreviousEvents
107    },
108    { "readEventsOnWrapping",
109      "(JLjava/util/Collection;)V",
110      (void*) android_app_admin_SecurityLog_readEventsOnWrapping
111    },
112};
113
114int register_android_app_admin_SecurityLog(JNIEnv* env) {
115    SLog::Init(env);
116
117    return RegisterMethodsOrDie(
118            env,
119            "android/app/admin/SecurityLog",
120            gRegisterMethods, NELEM(gRegisterMethods));
121}
122
123}; // namespace android
124