xref: /aosp_15_r20/external/conscrypt/common/src/jni/main/cpp/conscrypt/compatibility_close_monitor.cc (revision cd0cc2e34ba52cdf454361820a14d744e4bd531d)
1 /*
2  * Copyright (C) 2017 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 #include <conscrypt/compatibility_close_monitor.h>
18 
19 #ifndef _WIN32
20 #include <dlfcn.h>
21 #endif
22 
23 namespace conscrypt {
24 
25 CompatibilityCloseMonitor::acm_create_func CompatibilityCloseMonitor::asyncCloseMonitorCreate =
26         nullptr;
27 CompatibilityCloseMonitor::acm_destroy_func CompatibilityCloseMonitor::asyncCloseMonitorDestroy =
28         nullptr;
29 
30 #ifdef CONSCRYPT_UNBUNDLED
31 CompatibilityCloseMonitor::acm_ctor_func CompatibilityCloseMonitor::asyncCloseMonitorConstructor =
32         nullptr;
33 CompatibilityCloseMonitor::acm_dtor_func CompatibilityCloseMonitor::asyncCloseMonitorDestructor =
34         nullptr;
35 #endif  // CONSCRYPT_UNBUNDLED
36 
init()37 void CompatibilityCloseMonitor::init() {
38 #ifndef _WIN32
39     void *lib = dlopen("libandroidio.so", RTLD_NOW);
40     if (lib != nullptr) {
41         asyncCloseMonitorCreate = (acm_create_func) dlsym(lib, "async_close_monitor_create");
42         asyncCloseMonitorDestroy = (acm_destroy_func) dlsym(lib, "async_close_monitor_destroy");
43         return;
44     }
45 #ifdef CONSCRYPT_UNBUNDLED
46     // Only attempt to initialise the legacy C++ API if the C API symbols were not found.
47     lib = dlopen("libjavacore.so", RTLD_NOW);
48     if (lib != nullptr) {
49         if (asyncCloseMonitorCreate == nullptr) {
50             asyncCloseMonitorConstructor =
51                     (acm_ctor_func)dlsym(lib, "_ZN24AsynchronousCloseMonitorC1Ei");
52             asyncCloseMonitorDestructor =
53                     (acm_dtor_func)dlsym(lib, "_ZN24AsynchronousCloseMonitorD1Ev");
54         }
55     }
56 #endif  // CONSCRYPT_UNBUNDLED
57 #endif  // _WIN32
58 }
59 }  // namespace conscrypt
60