The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 "ProcessState" |
| 18 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | #include <binder/ProcessState.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Steven Moreland | 281abad | 2022-02-24 22:06:40 +0000 | [diff] [blame] | 21 | #include <android-base/strings.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/BpBinder.h> |
Tomasz Wasilczyk | 1de48a2 | 2023-10-30 14:19:19 +0000 | [diff] [blame] | 23 | #include <binder/Functional.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 26 | #include <binder/Stability.h> |
Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 27 | #include <cutils/atomic.h> |
Andrei Homescu | 8028ff4 | 2022-03-14 22:11:54 +0000 | [diff] [blame] | 28 | #include <utils/AndroidThreads.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
| 30 | #include <utils/String8.h> |
Andrei Homescu | 8028ff4 | 2022-03-14 22:11:54 +0000 | [diff] [blame] | 31 | #include <utils/Thread.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 33 | #include "Static.h" |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 34 | #include "Utils.h" |
Steven Moreland | 6ba5a25 | 2021-05-04 22:49:00 +0000 | [diff] [blame] | 35 | #include "binder_module.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
Elie Kheirallah | c2f5a7e | 2022-05-27 22:43:40 +0000 | [diff] [blame] | 39 | #include <pthread.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
| 43 | #include <sys/mman.h> |
| 44 | #include <sys/stat.h> |
Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 45 | #include <sys/types.h> |
Elie Kheirallah | c2f5a7e | 2022-05-27 22:43:40 +0000 | [diff] [blame] | 46 | #include <unistd.h> |
| 47 | #include <mutex> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 49 | #define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2) |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 50 | #define DEFAULT_MAX_BINDER_THREADS 15 |
Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 51 | #define DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION 1 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
Martijn Coenen | 7bca77a | 2019-03-13 11:51:08 +0000 | [diff] [blame] | 53 | #ifdef __ANDROID_VNDK__ |
| 54 | const char* kDefaultDriver = "/dev/vndbinder"; |
| 55 | #else |
Steven Moreland | 2ae2f5e | 2018-07-06 13:02:53 -0700 | [diff] [blame] | 56 | const char* kDefaultDriver = "/dev/binder"; |
Martijn Coenen | 7bca77a | 2019-03-13 11:51:08 +0000 | [diff] [blame] | 57 | #endif |
Steven Moreland | 2ae2f5e | 2018-07-06 13:02:53 -0700 | [diff] [blame] | 58 | |
Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 59 | // ------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| 61 | namespace android { |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 62 | |
Tomasz Wasilczyk | 1de48a2 | 2023-10-30 14:19:19 +0000 | [diff] [blame] | 63 | using namespace android::binder::impl; |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 64 | using android::binder::unique_fd; |
Tomasz Wasilczyk | 1de48a2 | 2023-10-30 14:19:19 +0000 | [diff] [blame] | 65 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | class PoolThread : public Thread |
| 67 | { |
| 68 | public: |
Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 69 | explicit PoolThread(bool isMain) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | : mIsMain(isMain) |
| 71 | { |
| 72 | } |
Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 73 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | protected: |
| 75 | virtual bool threadLoop() |
| 76 | { |
| 77 | IPCThreadState::self()->joinThreadPool(mIsMain); |
| 78 | return false; |
| 79 | } |
Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 80 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | const bool mIsMain; |
| 82 | }; |
| 83 | |
| 84 | sp<ProcessState> ProcessState::self() |
| 85 | { |
Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 86 | return init(kDefaultDriver, false /*requireDefault*/); |
Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | sp<ProcessState> ProcessState::initWithDriver(const char* driver) |
| 90 | { |
Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 91 | return init(driver, true /*requireDefault*/); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 94 | sp<ProcessState> ProcessState::selfOrNull() |
| 95 | { |
Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 96 | return init(nullptr, false /*requireDefault*/); |
| 97 | } |
| 98 | |
Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 99 | [[clang::no_destroy]] static sp<ProcessState> gProcess; |
| 100 | [[clang::no_destroy]] static std::mutex gProcessMutex; |
| 101 | |
| 102 | static void verifyNotForked(bool forked) { |
Steven Moreland | bd98e0f | 2021-10-14 14:24:15 -0700 | [diff] [blame] | 103 | LOG_ALWAYS_FATAL_IF(forked, "libbinder ProcessState can not be used after fork"); |
Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Steven Moreland | 9a4278e | 2023-02-14 01:33:39 +0000 | [diff] [blame] | 106 | bool ProcessState::isVndservicemanagerEnabled() { |
| 107 | return access("/vendor/bin/vndservicemanager", R_OK) == 0; |
| 108 | } |
| 109 | |
Steven Moreland | 68275d7 | 2023-04-21 22:12:45 +0000 | [diff] [blame] | 110 | sp<ProcessState> ProcessState::init(const char* driver, bool requireDefault) { |
Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 111 | if (driver == nullptr) { |
| 112 | std::lock_guard<std::mutex> l(gProcessMutex); |
Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 113 | if (gProcess) { |
| 114 | verifyNotForked(gProcess->mForked); |
| 115 | } |
Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 116 | return gProcess; |
| 117 | } |
| 118 | |
| 119 | [[clang::no_destroy]] static std::once_flag gProcessOnce; |
| 120 | std::call_once(gProcessOnce, [&](){ |
| 121 | if (access(driver, R_OK) == -1) { |
| 122 | ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); |
| 123 | driver = "/dev/binder"; |
| 124 | } |
| 125 | |
Steven Moreland | 9a4278e | 2023-02-14 01:33:39 +0000 | [diff] [blame] | 126 | if (0 == strcmp(driver, "/dev/vndbinder") && !isVndservicemanagerEnabled()) { |
| 127 | ALOGE("vndservicemanager is not started on this device, you can save resources/threads " |
| 128 | "by not initializing ProcessState with /dev/vndbinder."); |
| 129 | } |
| 130 | |
Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 131 | // we must install these before instantiating the gProcess object, |
| 132 | // otherwise this would race with creating it, and there could be the |
| 133 | // possibility of an invalid gProcess object forked by another thread |
| 134 | // before these are installed |
| 135 | int ret = pthread_atfork(ProcessState::onFork, ProcessState::parentPostFork, |
| 136 | ProcessState::childPostFork); |
| 137 | LOG_ALWAYS_FATAL_IF(ret != 0, "pthread_atfork error %s", strerror(ret)); |
| 138 | |
Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 139 | std::lock_guard<std::mutex> l(gProcessMutex); |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 140 | gProcess = sp<ProcessState>::make(driver); |
Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 141 | }); |
| 142 | |
| 143 | if (requireDefault) { |
| 144 | // Detect if we are trying to initialize with a different driver, and |
| 145 | // consider that an error. ProcessState will only be initialized once above. |
| 146 | LOG_ALWAYS_FATAL_IF(gProcess->getDriverName() != driver, |
| 147 | "ProcessState was already initialized with %s," |
| 148 | " can't initialize with %s.", |
| 149 | gProcess->getDriverName().c_str(), driver); |
| 150 | } |
| 151 | |
Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 152 | verifyNotForked(gProcess->mForked); |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 153 | return gProcess; |
| 154 | } |
| 155 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 156 | sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | { |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 158 | sp<IBinder> context = getStrongProxyForHandle(0); |
| 159 | |
Steven Moreland | 4da8fb0 | 2020-12-29 22:51:32 +0000 | [diff] [blame] | 160 | if (context) { |
| 161 | // The root object is special since we get it directly from the driver, it is never |
| 162 | // written by Parcell::writeStrongBinder. |
| 163 | internal::Stability::markCompilationUnit(context.get()); |
| 164 | } else { |
| 165 | ALOGW("Not able to get context object on %s.", mDriverName.c_str()); |
Steven Moreland | 8d93a71 | 2020-02-19 15:16:15 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 168 | return context; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 171 | void ProcessState::onFork() { |
| 172 | // make sure another thread isn't currently retrieving ProcessState |
| 173 | gProcessMutex.lock(); |
| 174 | } |
| 175 | |
| 176 | void ProcessState::parentPostFork() { |
| 177 | gProcessMutex.unlock(); |
| 178 | } |
| 179 | |
| 180 | void ProcessState::childPostFork() { |
| 181 | // another thread might call fork before gProcess is instantiated, but after |
| 182 | // the thread handler is installed |
| 183 | if (gProcess) { |
| 184 | gProcess->mForked = true; |
Steven Moreland | df732ba | 2022-05-18 22:04:49 +0000 | [diff] [blame] | 185 | |
| 186 | // "O_CLOFORK" |
| 187 | close(gProcess->mDriverFD); |
| 188 | gProcess->mDriverFD = -1; |
Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 189 | } |
| 190 | gProcessMutex.unlock(); |
| 191 | } |
| 192 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | void ProcessState::startThreadPool() |
| 194 | { |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 195 | std::unique_lock<std::mutex> _l(mLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | if (!mThreadPoolStarted) { |
Steven Moreland | f5a5bb8 | 2021-12-16 00:04:29 +0000 | [diff] [blame] | 197 | if (mMaxThreads == 0) { |
Steven Moreland | 3e9debc | 2023-06-15 00:35:29 +0000 | [diff] [blame] | 198 | // see also getThreadPoolMaxTotalThreadCount |
Steven Moreland | f5a5bb8 | 2021-12-16 00:04:29 +0000 | [diff] [blame] | 199 | ALOGW("Extra binder thread started, but 0 threads requested. Do not use " |
| 200 | "*startThreadPool when zero threads are requested."); |
| 201 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | mThreadPoolStarted = true; |
| 203 | spawnPooledThread(true); |
| 204 | } |
| 205 | } |
| 206 | |
Steven Moreland | 6109662 | 2020-08-31 23:36:39 +0000 | [diff] [blame] | 207 | bool ProcessState::becomeContextManager() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | { |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 209 | std::unique_lock<std::mutex> _l(mLock); |
Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 210 | |
Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 211 | flat_binder_object obj { |
| 212 | .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX, |
| 213 | }; |
Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 214 | |
Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 215 | int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj); |
Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 216 | |
Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 217 | // fallback to original method |
| 218 | if (result != 0) { |
| 219 | android_errorWriteLog(0x534e4554, "121035042"); |
Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 220 | |
Hungming Chen | 28b4252 | 2020-08-28 17:29:55 +0800 | [diff] [blame] | 221 | int unused = 0; |
| 222 | result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 224 | |
| 225 | if (result == -1) { |
Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 226 | ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno)); |
| 227 | } |
| 228 | |
| 229 | return result == 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 232 | // Get references to userspace objects held by the kernel binder driver |
| 233 | // Writes up to count elements into buf, and returns the total number |
| 234 | // of references the kernel has, which may be larger than count. |
| 235 | // buf may be NULL if count is 0. The pointers returned by this method |
| 236 | // should only be used for debugging and not dereferenced, they may |
| 237 | // already be invalid. |
| 238 | ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) |
| 239 | { |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 240 | binder_node_debug_info info = {}; |
| 241 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 242 | uintptr_t* end = buf ? buf + buf_count : nullptr; |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 243 | size_t count = 0; |
| 244 | |
| 245 | do { |
| 246 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info); |
| 247 | if (result < 0) { |
| 248 | return -1; |
| 249 | } |
| 250 | if (info.ptr != 0) { |
| 251 | if (buf && buf < end) |
| 252 | *buf++ = info.ptr; |
| 253 | count++; |
| 254 | if (buf && buf < end) |
| 255 | *buf++ = info.cookie; |
| 256 | count++; |
| 257 | } |
| 258 | } while (info.ptr != 0); |
| 259 | |
| 260 | return count; |
| 261 | } |
| 262 | |
Jon Spivack | 902e6fc | 2019-10-18 21:22:37 -0700 | [diff] [blame] | 263 | // Queries the driver for the current strong reference count of the node |
| 264 | // that the handle points to. Can only be used by the servicemanager. |
| 265 | // |
| 266 | // Returns -1 in case of failure, otherwise the strong reference count. |
Steven Moreland | e839388 | 2020-12-18 02:27:20 +0000 | [diff] [blame] | 267 | ssize_t ProcessState::getStrongRefCountForNode(const sp<BpBinder>& binder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 268 | if (binder->isRpcBinder()) return -1; |
| 269 | |
Jon Spivack | 902e6fc | 2019-10-18 21:22:37 -0700 | [diff] [blame] | 270 | binder_node_info_for_ref info; |
| 271 | memset(&info, 0, sizeof(binder_node_info_for_ref)); |
| 272 | |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 273 | info.handle = binder->getPrivateAccessor().binderHandle(); |
Jon Spivack | 902e6fc | 2019-10-18 21:22:37 -0700 | [diff] [blame] | 274 | |
| 275 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info); |
| 276 | |
| 277 | if (result != OK) { |
| 278 | static bool logged = false; |
| 279 | if (!logged) { |
| 280 | ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF."); |
| 281 | logged = true; |
| 282 | } |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | return info.strong_count; |
| 287 | } |
| 288 | |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 289 | void ProcessState::setCallRestriction(CallRestriction restriction) { |
Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 290 | LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr, |
| 291 | "Call restrictions must be set before the threadpool is started."); |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 292 | |
| 293 | mCallRestriction = restriction; |
| 294 | } |
| 295 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle) |
| 297 | { |
| 298 | const size_t N=mHandleToObject.size(); |
| 299 | if (N <= (size_t)handle) { |
| 300 | handle_entry e; |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 301 | e.binder = nullptr; |
| 302 | e.refs = nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | status_t err = mHandleToObject.insertAt(e, N, handle+1-N); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 304 | if (err < NO_ERROR) return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | return &mHandleToObject.editItemAt(handle); |
| 307 | } |
| 308 | |
Steven Moreland | 5c75a5a | 2022-05-11 22:15:10 +0000 | [diff] [blame] | 309 | // see b/166779391: cannot change the VNDK interface, so access like this |
| 310 | extern sp<BBinder> the_context_object; |
| 311 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) |
| 313 | { |
| 314 | sp<IBinder> result; |
| 315 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 316 | std::unique_lock<std::mutex> _l(mLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | |
Steven Moreland | 5c75a5a | 2022-05-11 22:15:10 +0000 | [diff] [blame] | 318 | if (handle == 0 && the_context_object != nullptr) return the_context_object; |
| 319 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | handle_entry* e = lookupHandleLocked(handle); |
| 321 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 322 | if (e != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | // We need to create a new BpBinder if there isn't currently one, OR we |
Steven Moreland | e171d62 | 2019-07-17 16:06:01 -0700 | [diff] [blame] | 324 | // are unable to acquire a weak reference on this current one. The |
| 325 | // attemptIncWeak() is safe because we know the BpBinder destructor will always |
| 326 | // call expungeHandle(), which acquires the same lock we are holding now. |
| 327 | // We need to do this because there is a race condition between someone |
| 328 | // releasing a reference on this BpBinder, and a new reference on its handle |
| 329 | // arriving from the driver. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | IBinder* b = e->binder; |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 331 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 332 | if (handle == 0) { |
| 333 | // Special case for context manager... |
| 334 | // The context manager is the only object for which we create |
| 335 | // a BpBinder proxy without already holding a reference. |
| 336 | // Perform a dummy transaction to ensure the context manager |
| 337 | // is registered before we create the first local reference |
| 338 | // to it (which will occur when creating the BpBinder). |
| 339 | // If a local reference is created for the BpBinder when the |
| 340 | // context manager is not present, the driver will fail to |
| 341 | // provide a reference to the context manager, but the |
| 342 | // driver API does not return status. |
| 343 | // |
| 344 | // Note that this is not race-free if the context manager |
| 345 | // dies while this code runs. |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 346 | |
Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 347 | IPCThreadState* ipc = IPCThreadState::self(); |
| 348 | |
| 349 | CallRestriction originalCallRestriction = ipc->getCallRestriction(); |
| 350 | ipc->setCallRestriction(CallRestriction::NONE); |
| 351 | |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 352 | Parcel data; |
Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 353 | status_t status = ipc->transact( |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 354 | 0, IBinder::PING_TRANSACTION, data, nullptr, 0); |
Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 355 | |
| 356 | ipc->setCallRestriction(originalCallRestriction); |
| 357 | |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 358 | if (status == DEAD_OBJECT) |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 359 | return nullptr; |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 362 | sp<BpBinder> b = BpBinder::PrivateAccessor::create(handle); |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 363 | e->binder = b.get(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | if (b) e->refs = b->getWeakRefs(); |
| 365 | result = b; |
| 366 | } else { |
| 367 | // This little bit of nastyness is to allow us to add a primary |
| 368 | // reference to the remote proxy when this team doesn't have one |
| 369 | // but another team is sending the handle to us. |
| 370 | result.force_set(b); |
| 371 | e->refs->decWeak(this); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return result; |
| 376 | } |
| 377 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | void ProcessState::expungeHandle(int32_t handle, IBinder* binder) |
| 379 | { |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 380 | std::unique_lock<std::mutex> _l(mLock); |
Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 381 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | handle_entry* e = lookupHandleLocked(handle); |
| 383 | |
| 384 | // This handle may have already been replaced with a new BpBinder |
| 385 | // (if someone failed the AttemptIncWeak() above); we don't want |
| 386 | // to overwrite it. |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 387 | if (e && e->binder == binder) e->binder = nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 390 | String8 ProcessState::makeBinderThreadName() { |
| 391 | int32_t s = android_atomic_add(1, &mThreadPoolSeq); |
Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 392 | pid_t pid = getpid(); |
Steven Moreland | 281abad | 2022-02-24 22:06:40 +0000 | [diff] [blame] | 393 | |
| 394 | std::string_view driverName = mDriverName.c_str(); |
| 395 | android::base::ConsumePrefix(&driverName, "/dev/"); |
| 396 | |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 397 | String8 name; |
Steven Moreland | 281abad | 2022-02-24 22:06:40 +0000 | [diff] [blame] | 398 | name.appendFormat("%.*s:%d_%X", static_cast<int>(driverName.length()), driverName.data(), pid, |
| 399 | s); |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 400 | return name; |
| 401 | } |
| 402 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | void ProcessState::spawnPooledThread(bool isMain) |
| 404 | { |
| 405 | if (mThreadPoolStarted) { |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 406 | String8 name = makeBinderThreadName(); |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 407 | ALOGV("Spawning new pooled thread, name=%s\n", name.c_str()); |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 408 | sp<Thread> t = sp<PoolThread>::make(isMain); |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 409 | t->run(name.c_str()); |
Elie Kheirallah | 47431c1 | 2022-04-21 23:46:17 +0000 | [diff] [blame] | 410 | mKernelStartedThreads++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } |
Steven Moreland | 3e9debc | 2023-06-15 00:35:29 +0000 | [diff] [blame] | 412 | // TODO: if startThreadPool is called on another thread after the process |
| 413 | // starts up, the kernel might think that it already requested those |
| 414 | // binder threads, and additional won't be started. This is likely to |
| 415 | // cause deadlocks, and it will also cause getThreadPoolMaxTotalThreadCount |
| 416 | // to return too high of a value. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 419 | status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) { |
Steven Moreland | 1fdb98b | 2020-03-06 21:42:12 +0000 | [diff] [blame] | 420 | LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads, |
| 421 | "Binder threadpool cannot be shrunk after starting"); |
Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 422 | status_t result = NO_ERROR; |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 423 | if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) { |
| 424 | mMaxThreads = maxThreads; |
| 425 | } else { |
Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 426 | result = -errno; |
| 427 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result)); |
| 428 | } |
| 429 | return result; |
| 430 | } |
| 431 | |
Elie Kheirallah | 47431c1 | 2022-04-21 23:46:17 +0000 | [diff] [blame] | 432 | size_t ProcessState::getThreadPoolMaxTotalThreadCount() const { |
Elie Kheirallah | 47431c1 | 2022-04-21 23:46:17 +0000 | [diff] [blame] | 433 | if (mThreadPoolStarted) { |
Frederick Mayle | 263507f | 2024-05-30 14:54:27 -0700 | [diff] [blame] | 434 | size_t kernelStarted = mKernelStartedThreads; |
| 435 | size_t max = mMaxThreads; |
| 436 | size_t current = mCurrentThreads; |
| 437 | |
| 438 | LOG_ALWAYS_FATAL_IF(kernelStarted > max + 1, |
| 439 | "too many kernel-started threads: %zu > %zu + 1", kernelStarted, max); |
Steven Moreland | 3e9debc | 2023-06-15 00:35:29 +0000 | [diff] [blame] | 440 | |
| 441 | // calling startThreadPool starts a thread |
| 442 | size_t threads = 1; |
| 443 | |
| 444 | // the kernel is configured to start up to mMaxThreads more threads |
Frederick Mayle | 263507f | 2024-05-30 14:54:27 -0700 | [diff] [blame] | 445 | threads += max; |
Steven Moreland | 3e9debc | 2023-06-15 00:35:29 +0000 | [diff] [blame] | 446 | |
| 447 | // Users may call IPCThreadState::joinThreadPool directly. We don't |
| 448 | // currently have a way to count this directly (it could be added by |
| 449 | // adding a separate private joinKernelThread method in IPCThreadState). |
| 450 | // So, if we are in a race between the kernel thread variable being |
| 451 | // incremented in this file and mCurrentThreads being incremented |
| 452 | // in IPCThreadState, temporarily forget about the extra join threads. |
| 453 | // This is okay, because most callers of this method only care about |
| 454 | // having 0, 1, or more threads. |
Frederick Mayle | 263507f | 2024-05-30 14:54:27 -0700 | [diff] [blame] | 455 | if (current > kernelStarted) { |
| 456 | threads += current - kernelStarted; |
Steven Moreland | 3e9debc | 2023-06-15 00:35:29 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | return threads; |
Elie Kheirallah | 47431c1 | 2022-04-21 23:46:17 +0000 | [diff] [blame] | 460 | } |
Steven Moreland | 3e9debc | 2023-06-15 00:35:29 +0000 | [diff] [blame] | 461 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 462 | // must not be initialized or maybe has poll thread setup, we |
| 463 | // currently don't track this in libbinder |
Frederick Mayle | 263507f | 2024-05-30 14:54:27 -0700 | [diff] [blame] | 464 | size_t kernelStarted = mKernelStartedThreads; |
| 465 | LOG_ALWAYS_FATAL_IF(kernelStarted != 0, "Expecting 0 kernel started threads but have %zu", |
| 466 | kernelStarted); |
Elie Kheirallah | 47431c1 | 2022-04-21 23:46:17 +0000 | [diff] [blame] | 467 | return mCurrentThreads; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Devin Moore | 4354f71 | 2022-12-08 01:44:46 +0000 | [diff] [blame] | 470 | bool ProcessState::isThreadPoolStarted() const { |
| 471 | return mThreadPoolStarted; |
| 472 | } |
| 473 | |
Carlos Llamas | 4f88670 | 2022-03-07 22:07:03 -0800 | [diff] [blame] | 474 | #define DRIVER_FEATURES_PATH "/dev/binderfs/features/" |
| 475 | bool ProcessState::isDriverFeatureEnabled(const DriverFeature feature) { |
| 476 | static const char* const names[] = { |
| 477 | [static_cast<int>(DriverFeature::ONEWAY_SPAM_DETECTION)] = |
| 478 | DRIVER_FEATURES_PATH "oneway_spam_detection", |
Carlos Llamas | b235b12 | 2021-12-20 06:38:44 -0800 | [diff] [blame] | 479 | [static_cast<int>(DriverFeature::EXTENDED_ERROR)] = |
| 480 | DRIVER_FEATURES_PATH "extended_error", |
Carlos Llamas | 4f88670 | 2022-03-07 22:07:03 -0800 | [diff] [blame] | 481 | }; |
| 482 | int fd = open(names[static_cast<int>(feature)], O_RDONLY | O_CLOEXEC); |
| 483 | char on; |
| 484 | if (fd == -1) { |
| 485 | ALOGE_IF(errno != ENOENT, "%s: cannot open %s: %s", __func__, |
| 486 | names[static_cast<int>(feature)], strerror(errno)); |
| 487 | return false; |
| 488 | } |
| 489 | if (read(fd, &on, sizeof(on)) == -1) { |
| 490 | ALOGE("%s: error reading to %s: %s", __func__, |
| 491 | names[static_cast<int>(feature)], strerror(errno)); |
wangmingming1 | dec7b1e | 2024-02-06 10:17:07 +0800 | [diff] [blame] | 492 | close(fd); |
Carlos Llamas | 4f88670 | 2022-03-07 22:07:03 -0800 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | close(fd); |
| 496 | return on == '1'; |
| 497 | } |
| 498 | |
Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 499 | status_t ProcessState::enableOnewaySpamDetection(bool enable) { |
| 500 | uint32_t enableDetection = enable ? 1 : 0; |
| 501 | if (ioctl(mDriverFD, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enableDetection) == -1) { |
Martijn Coenen | de0a39e | 2021-04-22 14:38:46 +0200 | [diff] [blame] | 502 | ALOGI("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno)); |
Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 503 | return -errno; |
| 504 | } |
| 505 | return NO_ERROR; |
| 506 | } |
| 507 | |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 508 | void ProcessState::giveThreadPoolName() { |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 509 | androidSetThreadName(makeBinderThreadName().c_str()); |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 512 | String8 ProcessState::getDriverName() { |
| 513 | return mDriverName; |
| 514 | } |
| 515 | |
Steven Moreland | 2fd1feb | 2024-05-25 00:13:10 +0000 | [diff] [blame] | 516 | static unique_fd open_driver(const char* driver, String8* error) { |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 517 | auto fd = unique_fd(open(driver, O_RDWR | O_CLOEXEC)); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 518 | if (!fd.ok()) { |
Steven Moreland | 2fd1feb | 2024-05-25 00:13:10 +0000 | [diff] [blame] | 519 | error->appendFormat("%d (%s) Opening '%s' failed", errno, strerror(errno), driver); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 520 | return {}; |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 521 | } |
| 522 | int vers = 0; |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 523 | int result = ioctl(fd.get(), BINDER_VERSION, &vers); |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 524 | if (result == -1) { |
Steven Moreland | 2fd1feb | 2024-05-25 00:13:10 +0000 | [diff] [blame] | 525 | error->appendFormat("%d (%s) Binder ioctl to obtain version failed", errno, |
| 526 | strerror(errno)); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 527 | return {}; |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 528 | } |
| 529 | if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { |
Steven Moreland | 2fd1feb | 2024-05-25 00:13:10 +0000 | [diff] [blame] | 530 | error->appendFormat("Binder driver protocol(%d) does not match user space protocol(%d)! " |
| 531 | "ioctl() return value: %d", |
| 532 | vers, BINDER_CURRENT_PROTOCOL_VERSION, result); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 533 | return {}; |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 534 | } |
| 535 | size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 536 | result = ioctl(fd.get(), BINDER_SET_MAX_THREADS, &maxThreads); |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 537 | if (result == -1) { |
| 538 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); |
| 539 | } |
| 540 | uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION; |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 541 | result = ioctl(fd.get(), BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable); |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 542 | if (result == -1) { |
Carlos Llamas | 4f88670 | 2022-03-07 22:07:03 -0800 | [diff] [blame] | 543 | ALOGE_IF(ProcessState::isDriverFeatureEnabled( |
| 544 | ProcessState::DriverFeature::ONEWAY_SPAM_DETECTION), |
| 545 | "Binder ioctl to enable oneway spam detection failed: %s", strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | } |
| 547 | return fd; |
| 548 | } |
| 549 | |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 550 | ProcessState::ProcessState(const char* driver) |
| 551 | : mDriverName(String8(driver)), |
| 552 | mDriverFD(-1), |
| 553 | mVMStart(MAP_FAILED), |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 554 | mExecutingThreadsCount(0), |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 555 | mMaxThreads(DEFAULT_MAX_BINDER_THREADS), |
Elie Kheirallah | 47431c1 | 2022-04-21 23:46:17 +0000 | [diff] [blame] | 556 | mCurrentThreads(0), |
| 557 | mKernelStartedThreads(0), |
Tomasz Wasilczyk | 1d46f58 | 2024-05-21 15:06:29 -0700 | [diff] [blame^] | 558 | mStarvationStartTime(never()), |
Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 559 | mForked(false), |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 560 | mThreadPoolStarted(false), |
| 561 | mThreadPoolSeq(1), |
| 562 | mCallRestriction(CallRestriction::NONE) { |
Steven Moreland | 2fd1feb | 2024-05-25 00:13:10 +0000 | [diff] [blame] | 563 | String8 error; |
| 564 | unique_fd opened = open_driver(driver, &error); |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 565 | |
| 566 | if (opened.ok()) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | // mmap the binder, providing a chunk of virtual address space to receive transactions. |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 568 | mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 569 | opened.get(), 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | if (mVMStart == MAP_FAILED) { |
| 571 | // *sigh* |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 572 | ALOGE("Using %s failed: unable to mmap transaction memory.", driver); |
| 573 | opened.reset(); |
Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 574 | mDriverName.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |
Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 577 | |
Steven Moreland | 24bc0d1 | 2019-10-11 12:29:20 -0700 | [diff] [blame] | 578 | #ifdef __ANDROID__ |
Steven Moreland | 2fd1feb | 2024-05-25 00:13:10 +0000 | [diff] [blame] | 579 | LOG_ALWAYS_FATAL_IF(!opened.ok(), |
| 580 | "Binder driver '%s' could not be opened. Error: %s. Terminating.", |
| 581 | error.c_str(), driver); |
Steven Moreland | 24bc0d1 | 2019-10-11 12:29:20 -0700 | [diff] [blame] | 582 | #endif |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 583 | |
| 584 | if (opened.ok()) { |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 585 | mDriverFD = opened.release(); |
Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 586 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | ProcessState::~ProcessState() |
| 590 | { |
zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 591 | if (mDriverFD >= 0) { |
| 592 | if (mVMStart != MAP_FAILED) { |
Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 593 | munmap(mVMStart, BINDER_VM_SIZE); |
zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 594 | } |
| 595 | close(mDriverFD); |
| 596 | } |
| 597 | mDriverFD = -1; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | } |
Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 599 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 600 | } // namespace android |