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 
26 namespace android {
27 
28 constexpr char kSecurityLogEventClass[] = "android/app/admin/SecurityLog$SecurityEvent";
29 template class EventLogHelper<log_id_t::LOG_ID_SECURITY, kSecurityLogEventClass>;
30 using SLog = EventLogHelper<log_id_t::LOG_ID_SECURITY, kSecurityLogEventClass>;
31 
android_app_admin_SecurityLog_isLoggingEnabled(JNIEnv * env,jobject)32 static jboolean android_app_admin_SecurityLog_isLoggingEnabled(JNIEnv* env,
33                                                     jobject /* clazz */) {
34     return (bool)__android_log_security();
35 }
36 
android_app_admin_SecurityLog_readEvents(JNIEnv * env,jobject,jobject out)37 static 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_NONBLOCK, 0, out);
45 }
46 
android_app_admin_SecurityLog_readEventsSince(JNIEnv * env,jobject,jlong timestamp,jobject out)47 static 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_NONBLOCK, timestamp, out);
56 }
57 
android_app_admin_SecurityLog_readPreviousEvents(JNIEnv * env,jobject,jobject out)58 static 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_NONBLOCK | ANDROID_LOG_PSTORE, 0, out);
66 }
67 
android_app_admin_SecurityLog_readEventsOnWrapping(JNIEnv * env,jobject,jlong timestamp,jobject out)68 static 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_NONBLOCK | ANDROID_LOG_WRAP, timestamp, out);
76 }
77 
78 /*
79  * JNI registration.
80  */
81 static const JNINativeMethod gRegisterMethods[] = {
82     /* name, signature, funcPtr */
83     { "isLoggingEnabled",
84       "()Z",
85       (void*) android_app_admin_SecurityLog_isLoggingEnabled
86     },
87     { "writeEvent",
88       "(I[Ljava/lang/Object;)I",
89       (void*) SLog::writeEventArray
90     },
91     { "readEvents",
92       "(Ljava/util/Collection;)V",
93       (void*) android_app_admin_SecurityLog_readEvents
94     },
95     { "readEventsSince",
96       "(JLjava/util/Collection;)V",
97       (void*) android_app_admin_SecurityLog_readEventsSince
98     },
99     { "readPreviousEvents",
100       "(Ljava/util/Collection;)V",
101       (void*) android_app_admin_SecurityLog_readPreviousEvents
102     },
103     { "readEventsOnWrapping",
104       "(JLjava/util/Collection;)V",
105       (void*) android_app_admin_SecurityLog_readEventsOnWrapping
106     },
107 };
108 
register_android_app_admin_SecurityLog(JNIEnv * env)109 int register_android_app_admin_SecurityLog(JNIEnv* env) {
110     SLog::Init(env);
111 
112     return RegisterMethodsOrDie(
113             env,
114             "android/app/admin/SecurityLog",
115             gRegisterMethods, NELEM(gRegisterMethods));
116 }
117 
118 }; // namespace android
119