blob: 8b7f903f1aedac60944b3afff80d11f08e503605 [file] [log] [blame]
Orion Hodson4ec55ea2020-12-08 09:52:06 +00001// 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 Alif20af332022-06-21 16:14:01 +000018#include <android-modules-utils/sdk_level.h>
Orion Hodson4ec55ea2020-12-08 09:52:06 +000019
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
28namespace android {
29namespace netjniutils {
30
31namespace {
32
Orion Hodson4ec55ea2020-12-08 09:52:06 +000033
34int 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
53int 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 Hodson059eb442021-05-12 10:01:13 +010057 typedef int (*ndkGetFd_t)(JNIEnv*, jobject);
58 static const ndkGetFd_t ndkGetFd = []() -> ndkGetFd_t {
Orion Hodson4ec55ea2020-12-08 09:52:06 +000059 void* handle = dlopen("libnativehelper.so", RTLD_NOLOAD | RTLD_NODELETE);
Orion Hodson059eb442021-05-12 10:01:13 +010060 auto ndkGetFd = reinterpret_cast<ndkGetFd_t>(dlsym(handle, "AFileDescriptor_getFd"));
61 if (ndkGetFd == nullptr) {
Orion Hodson4ec55ea2020-12-08 09:52:06 +000062 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG,
Orion Hodson059eb442021-05-12 10:01:13 +010063 "Failed to dlsym(AFileDescriptor_getFd): %s", dlerror());
Orion Hodson4ec55ea2020-12-08 09:52:06 +000064 dlclose(handle);
65 }
Orion Hodson059eb442021-05-12 10:01:13 +010066 return ndkGetFd;
Orion Hodson4ec55ea2020-12-08 09:52:06 +000067 }();
68
Orion Hodson059eb442021-05-12 10:01:13 +010069 return javaFd != nullptr ? ndkGetFd(env, javaFd) : -1;
Orion Hodson4ec55ea2020-12-08 09:52:06 +000070}
71
72} // namespace
73
74int GetNativeFileDescriptor(JNIEnv* env, jobject javaFd) {
Hassan Alif20af332022-06-21 16:14:01 +000075 static const bool preferNdkFileDescriptorApi = []() -> bool
76 { return android::modules::sdklevel::IsAtLeastS(); }();
Orion Hodson4ec55ea2020-12-08 09:52:06 +000077 if (preferNdkFileDescriptorApi) {
78 return GetNativeFileDescriptorWithNdk(env, javaFd);
79 } else {
80 return GetNativeFileDescriptorWithoutNdk(env, javaFd);
81 }
82}
83
84} // namespace netjniutils
85} // namespace android