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 /*
18  * DO NOT INCLUDE THIS HEADER IN NEW CODE. IT IS FOR LEGACY COMPATIBILITY ONLY.
19  */
20 
21 #pragma once
22 
23 #include <jni.h>
24 
25 __BEGIN_DECLS
26 
27 /*
28  * Returns the UNIX file descriptor as an integer from a
29  * java.io.FileDescriptor instance or -1 if the java.io.FileDescriptor
30  * is NULL.
31  *
32  * Exported by libnativehelper_compat_libc++.so.
33  *
34  * Note:
35  *
36  * This method exists solely for NetworkStack and Tethering until a better
37  * solution can be found (b/158749603).
38  *
39  * In Android S, libnativehelper_compat_libc++.so had methods depending on
40  * private Java API surfaces removed and those methods are only available in
41  * libnativehelper.so. From Android S, libnativehelper.so is a public
42  * library that is distributed in ART module and so we can ensure the
43  * private API it depends on is compatible with the current Java core
44  * libraries that are also part of the ART module.
45  *
46  * jniGetFDFromFileDescriptor() could not be removed from
47  * libnativehelper_compat_libc++.so because NetworkStack and Tethering need
48  * to be compatible with Android versions Q and R. On these releases,
49  * libnativehelper.so is not available to these modules, ie NetworkStack
50  * ships as an APK, so effectively an app.
51  */
52 int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
53 
54 /*
55  * Returns the UNIX file descriptor as an integer from a
56  * java.io.FileDescriptor instance or -1 if the java.io.FileDescriptor
57  * is NULL.
58  *
59  * Exported by libnativehelper_compat_libc++.so
60  *
61  * Note:
62  *
63  * This method exists primarily for testing purposes. It is the
64  * implementation used by jniGetFDFromFileDescriptor() exported by
65  * libnativehelper_compat_libc++.so.
66  *
67  * This method exists to make the tested surface area explicit in the
68  * CtsLibnativehelperTestCases and ensure the compatibility version is
69  * tested. The symbol jniGetFDFromFileDescriptor() is exported by both
70  * libnativehelper_compat_libc++.so and libnativehelper.so and the test
71  * harness depends on libnativehelper_compat_libc++.so, but most of the
72  * tests cover libnativehelper.so.
73  */
74 int jniGetFDFromFileDescriptor_QR(C_JNIEnv* env, jobject fileDescriptor);
75 
76 __END_DECLS
77 
78 /*
79  * For C++ code, we provide inlines that map to the C functions.  g++ always
80  * inlines these, even on non-optimized builds.
81  */
82 #if defined(__cplusplus)
83 
jniGetFDFromFileDescriptor(JNIEnv * env,jobject fileDescriptor)84 inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
85     return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
86 }
87 
jniGetFDFromFileDescriptor_QR(JNIEnv * env,jobject fileDescriptor)88 inline int jniGetFDFromFileDescriptor_QR(JNIEnv* env, jobject fileDescriptor) {
89     return jniGetFDFromFileDescriptor_QR(&env->functions, fileDescriptor);
90 }
91 
92 #endif  // defined(__cplusplus)
93