| 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 |  | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/BpBinder.h> | 
|  | 22 | #include <binder/IPCThreadState.h> | 
| Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 24 | #include <binder/Stability.h> | 
| Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 25 | #include <cutils/atomic.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> | 
|  | 27 | #include <utils/String8.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <utils/threads.h> | 
|  | 29 |  | 
| Mathias Agopian | 208059f | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 30 | #include <private/binder/binder_module.h> | 
| Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 31 | #include "Static.h" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 |  | 
|  | 33 | #include <errno.h> | 
|  | 34 | #include <fcntl.h> | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 35 | #include <mutex> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include <stdio.h> | 
|  | 37 | #include <stdlib.h> | 
|  | 38 | #include <unistd.h> | 
|  | 39 | #include <sys/ioctl.h> | 
|  | 40 | #include <sys/mman.h> | 
|  | 41 | #include <sys/stat.h> | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 42 | #include <sys/types.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 44 | #define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2) | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 45 | #define DEFAULT_MAX_BINDER_THREADS 15 | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 |  | 
| Martijn Coenen | 7bca77a | 2019-03-13 11:51:08 +0000 | [diff] [blame] | 47 | #ifdef __ANDROID_VNDK__ | 
|  | 48 | const char* kDefaultDriver = "/dev/vndbinder"; | 
|  | 49 | #else | 
| Steven Moreland | 2ae2f5e | 2018-07-06 13:02:53 -0700 | [diff] [blame] | 50 | const char* kDefaultDriver = "/dev/binder"; | 
| Martijn Coenen | 7bca77a | 2019-03-13 11:51:08 +0000 | [diff] [blame] | 51 | #endif | 
| Steven Moreland | 2ae2f5e | 2018-07-06 13:02:53 -0700 | [diff] [blame] | 52 |  | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 53 | // ------------------------------------------------------------------------- | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 |  | 
|  | 55 | namespace android { | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 56 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | class PoolThread : public Thread | 
|  | 58 | { | 
|  | 59 | public: | 
| Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 60 | explicit PoolThread(bool isMain) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | : mIsMain(isMain) | 
|  | 62 | { | 
|  | 63 | } | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 64 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | protected: | 
|  | 66 | virtual bool threadLoop() | 
|  | 67 | { | 
|  | 68 | IPCThreadState::self()->joinThreadPool(mIsMain); | 
|  | 69 | return false; | 
|  | 70 | } | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 71 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | const bool mIsMain; | 
|  | 73 | }; | 
|  | 74 |  | 
|  | 75 | sp<ProcessState> ProcessState::self() | 
|  | 76 | { | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 77 | return init(kDefaultDriver, false /*requireDefault*/); | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
|  | 80 | sp<ProcessState> ProcessState::initWithDriver(const char* driver) | 
|  | 81 | { | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 82 | return init(driver, true /*requireDefault*/); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 85 | sp<ProcessState> ProcessState::selfOrNull() | 
|  | 86 | { | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 87 | return init(nullptr, false /*requireDefault*/); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | sp<ProcessState> ProcessState::init(const char *driver, bool requireDefault) | 
|  | 91 | { | 
|  | 92 | [[clang::no_destroy]] static sp<ProcessState> gProcess; | 
|  | 93 | [[clang::no_destroy]] static std::mutex gProcessMutex; | 
|  | 94 |  | 
|  | 95 | if (driver == nullptr) { | 
|  | 96 | std::lock_guard<std::mutex> l(gProcessMutex); | 
|  | 97 | return gProcess; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | [[clang::no_destroy]] static std::once_flag gProcessOnce; | 
|  | 101 | std::call_once(gProcessOnce, [&](){ | 
|  | 102 | if (access(driver, R_OK) == -1) { | 
|  | 103 | ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); | 
|  | 104 | driver = "/dev/binder"; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | std::lock_guard<std::mutex> l(gProcessMutex); | 
|  | 108 | gProcess = new ProcessState(driver); | 
|  | 109 | }); | 
|  | 110 |  | 
|  | 111 | if (requireDefault) { | 
|  | 112 | // Detect if we are trying to initialize with a different driver, and | 
|  | 113 | // consider that an error. ProcessState will only be initialized once above. | 
|  | 114 | LOG_ALWAYS_FATAL_IF(gProcess->getDriverName() != driver, | 
|  | 115 | "ProcessState was already initialized with %s," | 
|  | 116 | " can't initialize with %s.", | 
|  | 117 | gProcess->getDriverName().c_str(), driver); | 
|  | 118 | } | 
|  | 119 |  | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 120 | return gProcess; | 
|  | 121 | } | 
|  | 122 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 123 | sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | { | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 125 | sp<IBinder> context = getStrongProxyForHandle(0); | 
|  | 126 |  | 
| Steven Moreland | 8d93a71 | 2020-02-19 15:16:15 -0800 | [diff] [blame] | 127 | if (context == nullptr) { | 
|  | 128 | ALOGW("Not able to get context object on %s.", mDriverName.c_str()); | 
|  | 129 | } | 
|  | 130 |  | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 131 | // The root object is special since we get it directly from the driver, it is never | 
|  | 132 | // written by Parcell::writeStrongBinder. | 
|  | 133 | internal::Stability::tryMarkCompilationUnit(context.get()); | 
|  | 134 |  | 
|  | 135 | return context; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | void ProcessState::startThreadPool() | 
|  | 139 | { | 
|  | 140 | AutoMutex _l(mLock); | 
|  | 141 | if (!mThreadPoolStarted) { | 
|  | 142 | mThreadPoolStarted = true; | 
|  | 143 | spawnPooledThread(true); | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 |  | 
| Steven Moreland | 6109662 | 2020-08-31 23:36:39 +0000 | [diff] [blame] | 147 | bool ProcessState::becomeContextManager() | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | { | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 149 | AutoMutex _l(mLock); | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 150 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 151 | flat_binder_object obj { | 
|  | 152 | .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX, | 
|  | 153 | }; | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 154 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 155 | int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 156 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 157 | // fallback to original method | 
|  | 158 | if (result != 0) { | 
|  | 159 | android_errorWriteLog(0x534e4554, "121035042"); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 160 |  | 
| Hungming Chen | 28b4252 | 2020-08-28 17:29:55 +0800 | [diff] [blame] | 161 | int unused = 0; | 
|  | 162 | result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 164 |  | 
|  | 165 | if (result == -1) { | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 166 | ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno)); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | return result == 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 172 | // Get references to userspace objects held by the kernel binder driver | 
|  | 173 | // Writes up to count elements into buf, and returns the total number | 
|  | 174 | // of references the kernel has, which may be larger than count. | 
|  | 175 | // buf may be NULL if count is 0.  The pointers returned by this method | 
|  | 176 | // should only be used for debugging and not dereferenced, they may | 
|  | 177 | // already be invalid. | 
|  | 178 | ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) | 
|  | 179 | { | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 180 | binder_node_debug_info info = {}; | 
|  | 181 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 182 | uintptr_t* end = buf ? buf + buf_count : nullptr; | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 183 | size_t count = 0; | 
|  | 184 |  | 
|  | 185 | do { | 
|  | 186 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info); | 
|  | 187 | if (result < 0) { | 
|  | 188 | return -1; | 
|  | 189 | } | 
|  | 190 | if (info.ptr != 0) { | 
|  | 191 | if (buf && buf < end) | 
|  | 192 | *buf++ = info.ptr; | 
|  | 193 | count++; | 
|  | 194 | if (buf && buf < end) | 
|  | 195 | *buf++ = info.cookie; | 
|  | 196 | count++; | 
|  | 197 | } | 
|  | 198 | } while (info.ptr != 0); | 
|  | 199 |  | 
|  | 200 | return count; | 
|  | 201 | } | 
|  | 202 |  | 
| Jon Spivack | 902e6fc | 2019-10-18 21:22:37 -0700 | [diff] [blame] | 203 | // Queries the driver for the current strong reference count of the node | 
|  | 204 | // that the handle points to. Can only be used by the servicemanager. | 
|  | 205 | // | 
|  | 206 | // Returns -1 in case of failure, otherwise the strong reference count. | 
|  | 207 | ssize_t ProcessState::getStrongRefCountForNodeByHandle(int32_t handle) { | 
|  | 208 | binder_node_info_for_ref info; | 
|  | 209 | memset(&info, 0, sizeof(binder_node_info_for_ref)); | 
|  | 210 |  | 
|  | 211 | info.handle = handle; | 
|  | 212 |  | 
|  | 213 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info); | 
|  | 214 |  | 
|  | 215 | if (result != OK) { | 
|  | 216 | static bool logged = false; | 
|  | 217 | if (!logged) { | 
|  | 218 | ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF."); | 
|  | 219 | logged = true; | 
|  | 220 | } | 
|  | 221 | return -1; | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | return info.strong_count; | 
|  | 225 | } | 
|  | 226 |  | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 227 | void ProcessState::setCallRestriction(CallRestriction restriction) { | 
| Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 228 | LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr, | 
|  | 229 | "Call restrictions must be set before the threadpool is started."); | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 230 |  | 
|  | 231 | mCallRestriction = restriction; | 
|  | 232 | } | 
|  | 233 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle) | 
|  | 235 | { | 
|  | 236 | const size_t N=mHandleToObject.size(); | 
|  | 237 | if (N <= (size_t)handle) { | 
|  | 238 | handle_entry e; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 239 | e.binder = nullptr; | 
|  | 240 | e.refs = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | status_t err = mHandleToObject.insertAt(e, N, handle+1-N); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 242 | if (err < NO_ERROR) return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } | 
|  | 244 | return &mHandleToObject.editItemAt(handle); | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) | 
|  | 248 | { | 
|  | 249 | sp<IBinder> result; | 
|  | 250 |  | 
|  | 251 | AutoMutex _l(mLock); | 
|  | 252 |  | 
|  | 253 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 254 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 255 | if (e != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | // 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] | 257 | // are unable to acquire a weak reference on this current one.  The | 
|  | 258 | // attemptIncWeak() is safe because we know the BpBinder destructor will always | 
|  | 259 | // call expungeHandle(), which acquires the same lock we are holding now. | 
|  | 260 | // We need to do this because there is a race condition between someone | 
|  | 261 | // releasing a reference on this BpBinder, and a new reference on its handle | 
|  | 262 | // arriving from the driver. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | IBinder* b = e->binder; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 264 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 265 | if (handle == 0) { | 
|  | 266 | // Special case for context manager... | 
|  | 267 | // The context manager is the only object for which we create | 
|  | 268 | // a BpBinder proxy without already holding a reference. | 
|  | 269 | // Perform a dummy transaction to ensure the context manager | 
|  | 270 | // is registered before we create the first local reference | 
|  | 271 | // to it (which will occur when creating the BpBinder). | 
|  | 272 | // If a local reference is created for the BpBinder when the | 
|  | 273 | // context manager is not present, the driver will fail to | 
|  | 274 | // provide a reference to the context manager, but the | 
|  | 275 | // driver API does not return status. | 
|  | 276 | // | 
|  | 277 | // Note that this is not race-free if the context manager | 
|  | 278 | // dies while this code runs. | 
|  | 279 | // | 
|  | 280 | // TODO: add a driver API to wait for context manager, or | 
|  | 281 | // stop special casing handle 0 for context manager and add | 
|  | 282 | // a driver API to get a handle to the context manager with | 
|  | 283 | // proper reference counting. | 
|  | 284 |  | 
|  | 285 | Parcel data; | 
| Martijn Coenen | 41a4c36 | 2020-09-21 08:13:30 +0000 | [diff] [blame] | 286 | status_t status = IPCThreadState::self()->transact( | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 287 | 0, IBinder::PING_TRANSACTION, data, nullptr, 0); | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 288 | if (status == DEAD_OBJECT) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 289 | return nullptr; | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 290 | } | 
|  | 291 |  | 
| Michael Wachenschwanz | 2d34990 | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 292 | b = BpBinder::create(handle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | e->binder = b; | 
|  | 294 | if (b) e->refs = b->getWeakRefs(); | 
|  | 295 | result = b; | 
|  | 296 | } else { | 
|  | 297 | // This little bit of nastyness is to allow us to add a primary | 
|  | 298 | // reference to the remote proxy when this team doesn't have one | 
|  | 299 | // but another team is sending the handle to us. | 
|  | 300 | result.force_set(b); | 
|  | 301 | e->refs->decWeak(this); | 
|  | 302 | } | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | return result; | 
|  | 306 | } | 
|  | 307 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | void ProcessState::expungeHandle(int32_t handle, IBinder* binder) | 
|  | 309 | { | 
|  | 310 | AutoMutex _l(mLock); | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 311 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 313 |  | 
|  | 314 | // This handle may have already been replaced with a new BpBinder | 
|  | 315 | // (if someone failed the AttemptIncWeak() above); we don't want | 
|  | 316 | // to overwrite it. | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 317 | if (e && e->binder == binder) e->binder = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } | 
|  | 319 |  | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 320 | String8 ProcessState::makeBinderThreadName() { | 
|  | 321 | int32_t s = android_atomic_add(1, &mThreadPoolSeq); | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 322 | pid_t pid = getpid(); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 323 | String8 name; | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 324 | name.appendFormat("Binder:%d_%X", pid, s); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 325 | return name; | 
|  | 326 | } | 
|  | 327 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | void ProcessState::spawnPooledThread(bool isMain) | 
|  | 329 | { | 
|  | 330 | if (mThreadPoolStarted) { | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 331 | String8 name = makeBinderThreadName(); | 
|  | 332 | ALOGV("Spawning new pooled thread, name=%s\n", name.string()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | sp<Thread> t = new PoolThread(isMain); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 334 | t->run(name.string()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | } | 
|  | 336 | } | 
|  | 337 |  | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 338 | status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) { | 
| Steven Moreland | 1fdb98b | 2020-03-06 21:42:12 +0000 | [diff] [blame] | 339 | LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads, | 
|  | 340 | "Binder threadpool cannot be shrunk after starting"); | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 341 | status_t result = NO_ERROR; | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 342 | if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) { | 
|  | 343 | mMaxThreads = maxThreads; | 
|  | 344 | } else { | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 345 | result = -errno; | 
|  | 346 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result)); | 
|  | 347 | } | 
|  | 348 | return result; | 
|  | 349 | } | 
|  | 350 |  | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 351 | void ProcessState::giveThreadPoolName() { | 
|  | 352 | androidSetThreadName( makeBinderThreadName().string() ); | 
|  | 353 | } | 
|  | 354 |  | 
| Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 355 | String8 ProcessState::getDriverName() { | 
|  | 356 | return mDriverName; | 
|  | 357 | } | 
|  | 358 |  | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 359 | static int open_driver(const char *driver) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | { | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 361 | int fd = open(driver, O_RDWR | O_CLOEXEC); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | if (fd >= 0) { | 
| Jin Wei | 78181df | 2012-10-18 17:00:48 +0800 | [diff] [blame] | 363 | int vers = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | status_t result = ioctl(fd, BINDER_VERSION, &vers); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | if (result == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 366 | ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | close(fd); | 
|  | 368 | fd = -1; | 
|  | 369 | } | 
|  | 370 | if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { | 
| Greg Hartman | 67ac00f | 2017-01-03 16:54:59 -0800 | [diff] [blame] | 371 | ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d", | 
|  | 372 | vers, BINDER_CURRENT_PROTOCOL_VERSION, result); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | close(fd); | 
|  | 374 | fd = -1; | 
|  | 375 | } | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 376 | size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); | 
|  | 378 | if (result == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 379 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | } else { | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 382 | ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } | 
|  | 384 | return fd; | 
|  | 385 | } | 
|  | 386 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 387 | ProcessState::ProcessState(const char *driver) | 
| Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 388 | : mDriverName(String8(driver)) | 
|  | 389 | , mDriverFD(open_driver(driver)) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | , mVMStart(MAP_FAILED) | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 391 | , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER) | 
|  | 392 | , mThreadCountDecrement(PTHREAD_COND_INITIALIZER) | 
|  | 393 | , mExecutingThreadsCount(0) | 
|  | 394 | , mMaxThreads(DEFAULT_MAX_BINDER_THREADS) | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 395 | , mStarvationStartTimeMs(0) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | , mThreadPoolStarted(false) | 
|  | 397 | , mThreadPoolSeq(1) | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 398 | , mCallRestriction(CallRestriction::NONE) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | { | 
| Steven Moreland | b6d0e76 | 2019-11-15 00:51:42 -0800 | [diff] [blame] | 400 |  | 
| Steven Moreland | e09b082 | 2020-08-26 18:06:13 +0000 | [diff] [blame] | 401 | // TODO(b/166468760): enforce in build system | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 402 | #if defined(__ANDROID_APEX__) | 
| Steven Moreland | b6d0e76 | 2019-11-15 00:51:42 -0800 | [diff] [blame] | 403 | LOG_ALWAYS_FATAL("Cannot use libbinder in APEX (only system.img libbinder) since it is not stable."); | 
|  | 404 | #endif | 
|  | 405 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | if (mDriverFD >= 0) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | // mmap the binder, providing a chunk of virtual address space to receive transactions. | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 408 | mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | if (mVMStart == MAP_FAILED) { | 
|  | 410 | // *sigh* | 
| Steven Moreland | 376a559 | 2017-12-19 15:42:16 -0800 | [diff] [blame] | 411 | ALOGE("Using %s failed: unable to mmap transaction memory.\n", mDriverName.c_str()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | close(mDriverFD); | 
|  | 413 | mDriverFD = -1; | 
| Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 414 | mDriverName.clear(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | } | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 417 |  | 
| Steven Moreland | 24bc0d1 | 2019-10-11 12:29:20 -0700 | [diff] [blame] | 418 | #ifdef __ANDROID__ | 
| Steven Moreland | 8c78e2e | 2019-07-10 16:13:12 -0700 | [diff] [blame] | 419 | LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver '%s' could not be opened.  Terminating.", driver); | 
| Steven Moreland | 24bc0d1 | 2019-10-11 12:29:20 -0700 | [diff] [blame] | 420 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | } | 
|  | 422 |  | 
|  | 423 | ProcessState::~ProcessState() | 
|  | 424 | { | 
| zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 425 | if (mDriverFD >= 0) { | 
|  | 426 | if (mVMStart != MAP_FAILED) { | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 427 | munmap(mVMStart, BINDER_VM_SIZE); | 
| zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 428 | } | 
|  | 429 | close(mDriverFD); | 
|  | 430 | } | 
|  | 431 | mDriverFD = -1; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | } | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 433 |  | 
| Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 434 | } // namespace android |