1 /*
2  * Copyright (C) 2020 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 "include_platform/nativehelper/JNIPlatformHelp.h"
18 
19 #include <dlfcn.h>
20 #include "include_jni/jni.h"
21 #include <stdbool.h>
22 #include <stddef.h>
23 
24 #define LOG_TAG "JNIHelpCompat"
25 #include "ALog-priv.h"
26 
27 /*
28  * This is compatibility code so the NetworkStack.apk and Tethering mainline module continues to
29  * work on Q and R. This code should not be used anywhere else. When this module is no longer
30  * required to run on Q and R, this compat code should be removed. Ideally we find a solution
31  * in NetworkStack and Tethering instead of here (b/158749603).
32  */
jniGetFDFromFileDescriptor_QR(JNIEnv * env,jobject fileDescriptor)33 int jniGetFDFromFileDescriptor_QR(JNIEnv* env, jobject fileDescriptor) {
34     if (fileDescriptor == NULL) {
35         return -1;
36     }
37 
38     static jfieldID g_descriptorFieldID = NULL;
39     if (g_descriptorFieldID == NULL) {
40         jclass fileDescriptorClass = (*env)->FindClass(env, "java/io/FileDescriptor");
41         g_descriptorFieldID = (*env)->GetFieldID(env, fileDescriptorClass, "descriptor", "I");
42         (*env)->DeleteLocalRef(env, fileDescriptorClass);
43     }
44 
45     return (*env)->GetIntField(env, fileDescriptor, g_descriptorFieldID);
46 }
47 
jniGetFDFromFileDescriptor(JNIEnv * env,jobject fileDescriptor)48 jint jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
49     return jniGetFDFromFileDescriptor_QR(env, fileDescriptor);
50 }
51