blob: 8ed72b087c29b5f95798bd070f3ade62b57462b2 [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"
18
19#include <dlfcn.h>
20#include <stdbool.h>
21#include <string.h>
22#include <sys/system_properties.h>
23
24#include <android/api-level.h>
25#include <android/log.h>
26
27namespace android {
28namespace netjniutils {
29
30namespace {
31
32bool IsAtLeastS() {
33 // TODO(b/158749603#comment19): move to android::modules::sdklevel::IsAtLeastS().
34 int api_level = android_get_device_api_level();
35
36 // Guarded check for branches that do not have __ANDROID_API_S__.
37#ifdef __ANDROID_API_S__
38 if (api_level >= __ANDROID_API_S__) {
39 return true;
40 }
41#endif
42
43 if (api_level < __ANDROID_API_R__) {
44 return false;
45 }
46
47 // Device looks like R or above, check codename as it could be (S or above).
48 static constexpr const char* kCodenameProperty = "ro.build.version.codename";
49 char codename[PROP_VALUE_MAX] = { 0 };
50 return (__system_property_get(kCodenameProperty, codename) > 0 &&
51 codename[0] >= 'S' && codename[1] == '\0');
52}
53
54int GetNativeFileDescriptorWithoutNdk(JNIEnv* env, jobject javaFd) {
55 // Prior to Android S, we need to find the descriptor field in the FileDescriptor class. The
56 // symbol name has been stable in libcore, but is a private implementation detail.
57 // Older libnativehelper_compat_c++ versions had a jniGetFdFromFileDescriptor method, but this
58 // was removed in S to replace it with the NDK API in libnativehelper.
59 // The code is copied here instead. This code can be removed once R is not supported anymore.
60 static const jfieldID descriptorFieldID = [env]() -> jfieldID {
61 jclass cls = env->FindClass("java/io/FileDescriptor");
62 jfieldID fieldID = env->GetFieldID(cls, "descriptor", "I");
63 env->DeleteLocalRef(cls);
64 if (fieldID == nullptr) {
65 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "Failed to get descriptor field.");
66 }
67 return fieldID;
68 }();
69
70 return javaFd != nullptr ? env->GetIntField(javaFd, descriptorFieldID) : -1;
71}
72
73int GetNativeFileDescriptorWithNdk(JNIEnv* env, jobject javaFd) {
74 // Since Android S, there is an NDK API to get a file descriptor present in libnativehelper.so.
75 // libnativehelper is loaded into all processes by the zygote since the zygote uses it
76 // to load the Android Runtime and is also a public library (because of the NDK API).
77 typedef int (*ndkGetFD_t)(JNIEnv*, jobject);
78 static const ndkGetFD_t ndkGetFD = []() -> ndkGetFD_t {
79 void* handle = dlopen("libnativehelper.so", RTLD_NOLOAD | RTLD_NODELETE);
80 auto ndkGetFD = reinterpret_cast<ndkGetFD_t>(dlsym(handle, "AFileDescriptor_getFD"));
81 if (ndkGetFD == nullptr) {
82 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG,
83 "Failed to dlsym(AFileDescriptor_getFD): %s", dlerror());
84 dlclose(handle);
85 }
86 return ndkGetFD;
87 }();
88
89 return javaFd != nullptr ? ndkGetFD(env, javaFd) : -1;
90}
91
92} // namespace
93
94int GetNativeFileDescriptor(JNIEnv* env, jobject javaFd) {
95 static const bool preferNdkFileDescriptorApi = []() -> bool { return IsAtLeastS(); }();
96 if (preferNdkFileDescriptorApi) {
97 return GetNativeFileDescriptorWithNdk(env, javaFd);
98 } else {
99 return GetNativeFileDescriptorWithoutNdk(env, javaFd);
100 }
101}
102
103} // namespace netjniutils
104} // namespace android