Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 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" |
Hassan Ali | f20af33 | 2022-06-21 16:14:01 +0000 | [diff] [blame] | 18 | #include <android-modules-utils/sdk_level.h> |
Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 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 | |
Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 33 | |
| 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 | |
| 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). |
Orion Hodson | 059eb44 | 2021-05-12 10:01:13 +0100 | [diff] [blame] | 57 | typedef int (*ndkGetFd_t)(JNIEnv*, jobject); |
| 58 | static const ndkGetFd_t ndkGetFd = []() -> ndkGetFd_t { |
Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 59 | void* handle = dlopen("libnativehelper.so", RTLD_NOLOAD | RTLD_NODELETE); |
Orion Hodson | 059eb44 | 2021-05-12 10:01:13 +0100 | [diff] [blame] | 60 | auto ndkGetFd = reinterpret_cast<ndkGetFd_t>(dlsym(handle, "AFileDescriptor_getFd")); |
| 61 | if (ndkGetFd == nullptr) { |
Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 62 | __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, |
Orion Hodson | 059eb44 | 2021-05-12 10:01:13 +0100 | [diff] [blame] | 63 | "Failed to dlsym(AFileDescriptor_getFd): %s", dlerror()); |
Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 64 | dlclose(handle); |
| 65 | } |
Orion Hodson | 059eb44 | 2021-05-12 10:01:13 +0100 | [diff] [blame] | 66 | return ndkGetFd; |
Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 67 | }(); |
| 68 | |
Orion Hodson | 059eb44 | 2021-05-12 10:01:13 +0100 | [diff] [blame] | 69 | return javaFd != nullptr ? ndkGetFd(env, javaFd) : -1; |
Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } // namespace |
| 73 | |
| 74 | int GetNativeFileDescriptor(JNIEnv* env, jobject javaFd) { |
Hassan Ali | f20af33 | 2022-06-21 16:14:01 +0000 | [diff] [blame] | 75 | static const bool preferNdkFileDescriptorApi = []() -> bool |
| 76 | { return android::modules::sdklevel::IsAtLeastS(); }(); |
Orion Hodson | 4ec55ea | 2020-12-08 09:52:06 +0000 | [diff] [blame] | 77 | if (preferNdkFileDescriptorApi) { |
| 78 | return GetNativeFileDescriptorWithNdk(env, javaFd); |
| 79 | } else { |
| 80 | return GetNativeFileDescriptorWithoutNdk(env, javaFd); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | } // namespace netjniutils |
| 85 | } // namespace android |