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