Home | History | Annotate | Download | only in conscrypt
      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 #ifndef CONSCRYPT_COMPATIBILITY_CLOSE_MONITOR_H_
     18 #define CONSCRYPT_COMPATIBILITY_CLOSE_MONITOR_H_
     19 
     20 #include <conscrypt/macros.h>
     21 
     22 #ifndef CONSCRYPT_UNBUNDLED
     23 
     24 /* If we're compiled unbundled from Android system image, we use the
     25  * CompatibilityCloseMonitor
     26  */
     27 #include <nativehelper/AsynchronousCloseMonitor.h>
     28 
     29 namespace conscrypt {
     30 
     31 /**
     32  * When bundled with Android, this just wraps around AsynchronousCloseMonitor.
     33  */
     34 class CompatibilityCloseMonitor {
     35  private:
     36     AsynchronousCloseMonitor monitor;
     37 
     38  public:
     39     explicit CompatibilityCloseMonitor(int fd) : monitor(fd) {}
     40 
     41     ~CompatibilityCloseMonitor() {}
     42 
     43     static void init() {}
     44 };
     45 
     46 }  // namespace conscrypt
     47 
     48 #elif !defined(CONSCRYPT_OPENJDK)  // && CONSCRYPT_UNBUNDLED
     49 
     50 namespace conscrypt {
     51 
     52 /*
     53  * This is a big hack; don't learn from this. Basically what happened is we do
     54  * not have an API way to insert ourselves into the AsynchronousCloseMonitor
     55  * that's compiled into the native libraries for libcore when we're unbundled.
     56  * So we try to look up the symbol from the main library to find it.
     57  */
     58 class CompatibilityCloseMonitor {
     59  public:
     60     explicit CompatibilityCloseMonitor(int fd) {
     61         if (asyncCloseMonitorConstructor != nullptr) {
     62             asyncCloseMonitorConstructor(objBuffer, fd);
     63         }
     64     }
     65 
     66     ~CompatibilityCloseMonitor() {
     67         if (asyncCloseMonitorDestructor != nullptr) {
     68             asyncCloseMonitorDestructor(objBuffer);
     69         }
     70     }
     71 
     72     static void init();
     73 
     74  private:
     75     typedef void (*acm_ctor_func)(void*, int);
     76     typedef void (*acm_dtor_func)(void*);
     77 
     78     static acm_ctor_func asyncCloseMonitorConstructor;
     79     static acm_dtor_func asyncCloseMonitorDestructor;
     80 
     81     char objBuffer[256];
     82 #if 0
     83     static_assert(sizeof(objBuffer) > 2*sizeof(AsynchronousCloseMonitor),
     84                   "CompatibilityCloseMonitor must be larger than the actual object");
     85 #endif
     86 };
     87 
     88 }  // namespace conscrypt
     89 
     90 #else  // CONSCRYPT_UNBUNDLED && CONSCRYPT_OPENJDK
     91 
     92 namespace conscrypt {
     93 
     94 /**
     95  * For OpenJDK, do nothing.
     96  */
     97 class CompatibilityCloseMonitor {
     98  public:
     99     explicit CompatibilityCloseMonitor(int) {}
    100 
    101     ~CompatibilityCloseMonitor() {}
    102 
    103     static void init() {}
    104 };
    105 
    106 }  // namespace conscrypt
    107 
    108 #endif  // CONSCRYPT_UNBUNDLED && CONSCRYPT_OPENJDK
    109 
    110 #endif  // CONSCRYPT_COMPATIBILITY_CLOSE_MONITOR_H_
    111