1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #define LOG_TAG "netjniutils"
16
17 #include "netjniutils/netjniutils.h"
18 #include <android-modules-utils/sdk_level.h>
19
20 #include <dlfcn.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <sys/system_properties.h>
24
25 #include <android/api-level.h>
26 #include <android/log.h>
27
28 namespace android {
29 namespace netjniutils {
30
31 namespace {
32
33
GetNativeFileDescriptorWithoutNdk(JNIEnv * env,jobject javaFd)34 int GetNativeFileDescriptorWithoutNdk(JNIEnv* env, jobject javaFd) {
35 // Prior to Android S, we need to find the descriptor field in the FileDescriptor class. The
36 // symbol name has been stable in libcore, but is a private implementation detail.
37 // Older libnativehelper_compat_c++ versions had a jniGetFdFromFileDescriptor method, but this
38 // was removed in S to replace it with the NDK API in libnativehelper.
39 // The code is copied here instead. This code can be removed once R is not supported anymore.
40 static const jfieldID descriptorFieldID = [env]() -> jfieldID {
41 jclass cls = env->FindClass("java/io/FileDescriptor");
42 jfieldID fieldID = env->GetFieldID(cls, "descriptor", "I");
43 env->DeleteLocalRef(cls);
44 if (fieldID == nullptr) {
45 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "Failed to get descriptor field.");
46 }
47 return fieldID;
48 }();
49
50 return javaFd != nullptr ? env->GetIntField(javaFd, descriptorFieldID) : -1;
51 }
52
GetNativeFileDescriptorWithNdk(JNIEnv * env,jobject javaFd)53 int GetNativeFileDescriptorWithNdk(JNIEnv* env, jobject javaFd) {
54 // Since Android S, there is an NDK API to get a file descriptor present in libnativehelper.so.
55 // libnativehelper is loaded into all processes by the zygote since the zygote uses it
56 // to load the Android Runtime and is also a public library (because of the NDK API).
57 typedef int (*ndkGetFd_t)(JNIEnv*, jobject);
58 static const ndkGetFd_t ndkGetFd = []() -> ndkGetFd_t {
59 void* handle = dlopen("libnativehelper.so", RTLD_NOLOAD | RTLD_NODELETE);
60 auto ndkGetFd = reinterpret_cast<ndkGetFd_t>(dlsym(handle, "AFileDescriptor_getFd"));
61 if (ndkGetFd == nullptr) {
62 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG,
63 "Failed to dlsym(AFileDescriptor_getFd): %s", dlerror());
64 dlclose(handle);
65 }
66 return ndkGetFd;
67 }();
68
69 return javaFd != nullptr ? ndkGetFd(env, javaFd) : -1;
70 }
71
72 } // namespace
73
GetNativeFileDescriptor(JNIEnv * env,jobject javaFd)74 int GetNativeFileDescriptor(JNIEnv* env, jobject javaFd) {
75 static const bool preferNdkFileDescriptorApi = []() -> bool
76 { return android::modules::sdklevel::IsAtLeastS(); }();
77 if (preferNdkFileDescriptorApi) {
78 return GetNativeFileDescriptorWithNdk(env, javaFd);
79 } else {
80 return GetNativeFileDescriptorWithoutNdk(env, javaFd);
81 }
82 }
83
84 } // namespace netjniutils
85 } // namespace android
86