1 /*
2  * Copyright (C) 2010 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 #define LOG_TAG "AsynchronousCloseMonitor"
18 
19 #include <log/log.h>
20 
21 #include <errno.h>
22 #include <signal.h>
23 #include <string.h>
24 
25 #include <mutex>
26 
27 #include "AsynchronousCloseMonitor.h"
28 
29 namespace {
30 
31 class AsynchronousCloseMonitorImpl {
32 public:
33     explicit AsynchronousCloseMonitorImpl(int fd);
34     ~AsynchronousCloseMonitorImpl();
35     bool wasSignaled() const;
36 
37     static void init();
38 
39     static void signalBlockedThreads(int fd);
40 
41 private:
42     AsynchronousCloseMonitorImpl(const AsynchronousCloseMonitorImpl&) = delete;
43     AsynchronousCloseMonitorImpl& operator=(const AsynchronousCloseMonitorImpl&) = delete;
44 
45     AsynchronousCloseMonitorImpl* mPrev;
46     AsynchronousCloseMonitorImpl* mNext;
47     pthread_t mThread;
48     int mFd;
49     bool mSignaled;
50 };
51 
52 /**
53  * We use an intrusive doubly-linked list to keep track of blocked threads.
54  * This gives us O(1) insertion and removal, and means we don't need to do any allocation.
55  * (The objects themselves are stack-allocated.)
56  * Waking potentially-blocked threads when a file descriptor is closed is O(n) in the total number
57  * of blocked threads (not the number of threads actually blocked on the file descriptor in
58  * question). For now at least, this seems like a good compromise for Android.
59  */
60 static std::mutex blockedThreadListMutex;
61 static AsynchronousCloseMonitorImpl* blockedThreadList = NULL;
62 
63 /**
64  * The specific signal chosen here is arbitrary, but bionic needs to know so that SIGRTMIN
65  * starts at a higher value.
66  */
67 #if defined(__Fuchsia__)
68 static const int BLOCKED_THREAD_SIGNAL = SIGRTMIN + 2;
69 #elif !defined (__BIONIC__)
70 static const int BLOCKED_THREAD_SIGNAL = SIGRTMAX - 2;
71 #else
72 static const int BLOCKED_THREAD_SIGNAL = __SIGRTMIN + 2;
73 #endif
74 
blockedThreadSignalHandler(int)75 static void blockedThreadSignalHandler(int /*signal*/) {
76     // Do nothing. We only sent this signal for its side-effect of interrupting syscalls.
77 }
78 
init()79 void AsynchronousCloseMonitorImpl::init() {
80     // Ensure that the signal we send interrupts system calls but doesn't kill threads.
81     // Using sigaction(2) lets us ensure that the SA_RESTART flag is not set.
82     // (The whole reason we're sending this signal is to unblock system calls!)
83     struct sigaction sa;
84     memset(&sa, 0, sizeof(sa));
85     sa.sa_handler = blockedThreadSignalHandler;
86     sa.sa_flags = 0;
87     int rc = sigaction(BLOCKED_THREAD_SIGNAL, &sa, NULL);
88     if (rc == -1) {
89         ALOGE("setting blocked thread signal handler failed: %s", strerror(errno));
90     }
91 }
92 
signalBlockedThreads(int fd)93 void AsynchronousCloseMonitorImpl::signalBlockedThreads(int fd) {
94     std::lock_guard<std::mutex> lock(blockedThreadListMutex);
95     for (AsynchronousCloseMonitorImpl* it = blockedThreadList; it != NULL; it = it->mNext) {
96         if (it->mFd == fd) {
97             it->mSignaled = true;
98             pthread_kill(it->mThread, BLOCKED_THREAD_SIGNAL);
99             // Keep going, because there may be more than one thread...
100         }
101     }
102 }
103 
wasSignaled() const104 bool AsynchronousCloseMonitorImpl::wasSignaled() const {
105     return mSignaled;
106 }
107 
AsynchronousCloseMonitorImpl(int fd)108 AsynchronousCloseMonitorImpl::AsynchronousCloseMonitorImpl(int fd) {
109     std::lock_guard<std::mutex> lock(blockedThreadListMutex);
110     // Who are we, and what are we waiting for?
111     mThread = pthread_self();
112     mFd = fd;
113     mSignaled = false;
114     // Insert ourselves at the head of the intrusive doubly-linked list...
115     mPrev = NULL;
116     mNext = blockedThreadList;
117     if (mNext != NULL) {
118         mNext->mPrev = this;
119     }
120     blockedThreadList = this;
121 }
122 
~AsynchronousCloseMonitorImpl()123 AsynchronousCloseMonitorImpl::~AsynchronousCloseMonitorImpl() {
124     std::lock_guard<std::mutex> lock(blockedThreadListMutex);
125     // Unlink ourselves from the intrusive doubly-linked list...
126     if (mNext != NULL) {
127         mNext->mPrev = mPrev;
128     }
129     if (mPrev == NULL) {
130         blockedThreadList = mNext;
131     } else {
132         mPrev->mNext = mNext;
133     }
134 }
135 
136 }  // namespace
137 
138 //
139 // C ABI and API boundary
140 //
141 
142 extern "C" {
async_close_monitor_static_init()143 void async_close_monitor_static_init() {
144   AsynchronousCloseMonitorImpl::init();
145 }
146 
async_close_monitor_signal_blocked_threads(int fd)147 void async_close_monitor_signal_blocked_threads(int fd) {
148   AsynchronousCloseMonitorImpl::signalBlockedThreads(fd);
149 }
150 
async_close_monitor_create(int fd)151 void* async_close_monitor_create(int fd) {
152   return new AsynchronousCloseMonitorImpl(fd);
153 }
154 
async_close_monitor_destroy(void * instance)155 void async_close_monitor_destroy(void* instance) {
156   auto monitor = reinterpret_cast<AsynchronousCloseMonitorImpl*>(instance);
157   delete monitor;
158 }
159 
async_close_monitor_was_signalled(const void * instance)160 int async_close_monitor_was_signalled(const void* instance) {
161   auto monitor = reinterpret_cast<const AsynchronousCloseMonitorImpl*>(instance);
162   return monitor->wasSignaled() ? 1 : 0;
163 }
164 }
165