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> |
| 24 | #include <cutils/atomic.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | #include <utils/String8.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <utils/String8.h> |
| 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> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <unistd.h> |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/stat.h> |
Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 41 | #include <sys/types.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 43 | #define DEFAULT_BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2) |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 44 | #define DEFAULT_MAX_BINDER_THREADS 15 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
Martijn Coenen | 7bca77a | 2019-03-13 11:51:08 +0000 | [diff] [blame] | 46 | #ifdef __ANDROID_VNDK__ |
| 47 | const char* kDefaultDriver = "/dev/vndbinder"; |
| 48 | #else |
Steven Moreland | 2ae2f5e | 2018-07-06 13:02:53 -0700 | [diff] [blame] | 49 | const char* kDefaultDriver = "/dev/binder"; |
Martijn Coenen | 7bca77a | 2019-03-13 11:51:08 +0000 | [diff] [blame] | 50 | #endif |
Steven Moreland | 2ae2f5e | 2018-07-06 13:02:53 -0700 | [diff] [blame] | 51 | |
Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 52 | // ------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | namespace android { |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 55 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | class PoolThread : public Thread |
| 57 | { |
| 58 | public: |
Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 59 | explicit PoolThread(bool isMain) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | : mIsMain(isMain) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | virtual bool threadLoop() |
| 66 | { |
| 67 | IPCThreadState::self()->joinThreadPool(mIsMain); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | const bool mIsMain; |
| 72 | }; |
| 73 | |
| 74 | sp<ProcessState> ProcessState::self() |
| 75 | { |
Mathias Agopian | e8db871 | 2012-04-16 19:30:56 -0700 | [diff] [blame] | 76 | Mutex::Autolock _l(gProcessMutex); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 77 | if (gProcess != nullptr) { |
Mathias Agopian | e8db871 | 2012-04-16 19:30:56 -0700 | [diff] [blame] | 78 | return gProcess; |
| 79 | } |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 80 | gProcess = new ProcessState(kDefaultDriver, DEFAULT_BINDER_VM_SIZE); |
| 81 | return gProcess; |
| 82 | } |
| 83 | |
| 84 | sp<ProcessState> ProcessState::selfOrNull() |
| 85 | { |
| 86 | Mutex::Autolock _l(gProcessMutex); |
Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 87 | return gProcess; |
| 88 | } |
| 89 | |
| 90 | sp<ProcessState> ProcessState::initWithDriver(const char* driver) |
| 91 | { |
| 92 | Mutex::Autolock _l(gProcessMutex); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 93 | if (gProcess != nullptr) { |
Iliyan Malchev | ed1ffe0 | 2017-04-14 00:34:57 -0700 | [diff] [blame] | 94 | // Allow for initWithDriver to be called repeatedly with the same |
| 95 | // driver. |
| 96 | if (!strcmp(gProcess->getDriverName().c_str(), driver)) { |
| 97 | return gProcess; |
| 98 | } |
Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 99 | LOG_ALWAYS_FATAL("ProcessState was already initialized."); |
| 100 | } |
Steven Moreland | 376a559 | 2017-12-19 15:42:16 -0800 | [diff] [blame] | 101 | |
| 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 | |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 107 | gProcess = new ProcessState(driver, DEFAULT_BINDER_VM_SIZE); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | return gProcess; |
| 109 | } |
| 110 | |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 111 | sp<ProcessState> ProcessState::initWithMmapSize(size_t mmap_size) { |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 112 | Mutex::Autolock _l(gProcessMutex); |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 113 | if (gProcess != nullptr) { |
| 114 | LOG_ALWAYS_FATAL_IF(mmap_size != gProcess->getMmapSize(), |
| 115 | "ProcessState already initialized with a different mmap size."); |
| 116 | return gProcess; |
| 117 | } |
| 118 | |
| 119 | gProcess = new ProcessState(kDefaultDriver, mmap_size); |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 120 | return gProcess; |
| 121 | } |
| 122 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | void ProcessState::setContextObject(const sp<IBinder>& object) |
| 124 | { |
| 125 | setContextObject(object, String16("default")); |
| 126 | } |
| 127 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 128 | sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | { |
Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 130 | return getStrongProxyForHandle(0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name) |
| 134 | { |
| 135 | AutoMutex _l(mLock); |
| 136 | mContexts.add(name, object); |
| 137 | } |
| 138 | |
| 139 | sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller) |
| 140 | { |
| 141 | mLock.lock(); |
| 142 | sp<IBinder> object( |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 143 | mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : nullptr); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | mLock.unlock(); |
| 145 | |
| 146 | //printf("Getting context object %s for %p\n", String8(name).string(), caller.get()); |
| 147 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 148 | if (object != nullptr) return object; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
| 150 | // Don't attempt to retrieve contexts if we manage them |
| 151 | if (mManagesContexts) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 152 | ALOGE("getContextObject(%s) failed, but we manage the contexts!\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | String8(name).string()); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 154 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | IPCThreadState* ipc = IPCThreadState::self(); |
| 158 | { |
| 159 | Parcel data, reply; |
| 160 | // no interface token on this magic transaction |
| 161 | data.writeString16(name); |
| 162 | data.writeStrongBinder(caller); |
| 163 | status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0); |
| 164 | if (result == NO_ERROR) { |
| 165 | object = reply.readStrongBinder(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | ipc->flushCommands(); |
| 170 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 171 | if (object != nullptr) setContextObject(object, name); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | return object; |
| 173 | } |
| 174 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | void ProcessState::startThreadPool() |
| 176 | { |
| 177 | AutoMutex _l(mLock); |
| 178 | if (!mThreadPoolStarted) { |
| 179 | mThreadPoolStarted = true; |
| 180 | spawnPooledThread(true); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | bool ProcessState::isContextManager(void) const |
| 185 | { |
| 186 | return mManagesContexts; |
| 187 | } |
| 188 | |
| 189 | bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData) |
| 190 | { |
| 191 | if (!mManagesContexts) { |
| 192 | AutoMutex _l(mLock); |
| 193 | mBinderContextCheckFunc = checkFunc; |
| 194 | mBinderContextUserData = userData; |
Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 195 | |
Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 196 | flat_binder_object obj { |
| 197 | .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX, |
| 198 | }; |
| 199 | |
| 200 | status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj); |
| 201 | |
| 202 | // fallback to original method |
| 203 | if (result != 0) { |
| 204 | android_errorWriteLog(0x534e4554, "121035042"); |
| 205 | |
| 206 | int dummy = 0; |
| 207 | result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy); |
| 208 | } |
| 209 | |
Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 210 | if (result == 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | mManagesContexts = true; |
Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 212 | } else if (result == -1) { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 213 | mBinderContextCheckFunc = nullptr; |
| 214 | mBinderContextUserData = nullptr; |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 215 | ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | return mManagesContexts; |
| 219 | } |
| 220 | |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 221 | // Get references to userspace objects held by the kernel binder driver |
| 222 | // Writes up to count elements into buf, and returns the total number |
| 223 | // of references the kernel has, which may be larger than count. |
| 224 | // buf may be NULL if count is 0. The pointers returned by this method |
| 225 | // should only be used for debugging and not dereferenced, they may |
| 226 | // already be invalid. |
| 227 | ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) |
| 228 | { |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 229 | binder_node_debug_info info = {}; |
| 230 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 231 | uintptr_t* end = buf ? buf + buf_count : nullptr; |
Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 232 | size_t count = 0; |
| 233 | |
| 234 | do { |
| 235 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info); |
| 236 | if (result < 0) { |
| 237 | return -1; |
| 238 | } |
| 239 | if (info.ptr != 0) { |
| 240 | if (buf && buf < end) |
| 241 | *buf++ = info.ptr; |
| 242 | count++; |
| 243 | if (buf && buf < end) |
| 244 | *buf++ = info.cookie; |
| 245 | count++; |
| 246 | } |
| 247 | } while (info.ptr != 0); |
| 248 | |
| 249 | return count; |
| 250 | } |
| 251 | |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 252 | size_t ProcessState::getMmapSize() { |
| 253 | return mMmapSize; |
| 254 | } |
| 255 | |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 256 | void ProcessState::setCallRestriction(CallRestriction restriction) { |
| 257 | LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull(), "Call restrictions must be set before the threadpool is started."); |
| 258 | |
| 259 | mCallRestriction = restriction; |
| 260 | } |
| 261 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle) |
| 263 | { |
| 264 | const size_t N=mHandleToObject.size(); |
| 265 | if (N <= (size_t)handle) { |
| 266 | handle_entry e; |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 267 | e.binder = nullptr; |
| 268 | e.refs = nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | status_t err = mHandleToObject.insertAt(e, N, handle+1-N); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 270 | if (err < NO_ERROR) return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | } |
| 272 | return &mHandleToObject.editItemAt(handle); |
| 273 | } |
| 274 | |
| 275 | sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) |
| 276 | { |
| 277 | sp<IBinder> result; |
| 278 | |
| 279 | AutoMutex _l(mLock); |
| 280 | |
| 281 | handle_entry* e = lookupHandleLocked(handle); |
| 282 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 283 | if (e != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | // We need to create a new BpBinder if there isn't currently one, OR we |
| 285 | // are unable to acquire a weak reference on this current one. See comment |
| 286 | // in getWeakProxyForHandle() for more info about this. |
| 287 | IBinder* b = e->binder; |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 288 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 289 | if (handle == 0) { |
| 290 | // Special case for context manager... |
| 291 | // The context manager is the only object for which we create |
| 292 | // a BpBinder proxy without already holding a reference. |
| 293 | // Perform a dummy transaction to ensure the context manager |
| 294 | // is registered before we create the first local reference |
| 295 | // to it (which will occur when creating the BpBinder). |
| 296 | // If a local reference is created for the BpBinder when the |
| 297 | // context manager is not present, the driver will fail to |
| 298 | // provide a reference to the context manager, but the |
| 299 | // driver API does not return status. |
| 300 | // |
| 301 | // Note that this is not race-free if the context manager |
| 302 | // dies while this code runs. |
| 303 | // |
| 304 | // TODO: add a driver API to wait for context manager, or |
| 305 | // stop special casing handle 0 for context manager and add |
| 306 | // a driver API to get a handle to the context manager with |
| 307 | // proper reference counting. |
| 308 | |
| 309 | Parcel data; |
| 310 | status_t status = IPCThreadState::self()->transact( |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 311 | 0, IBinder::PING_TRANSACTION, data, nullptr, 0); |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 312 | if (status == DEAD_OBJECT) |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 313 | return nullptr; |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Michael Wachenschwanz | 2d34990 | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 316 | b = BpBinder::create(handle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | e->binder = b; |
| 318 | if (b) e->refs = b->getWeakRefs(); |
| 319 | result = b; |
| 320 | } else { |
| 321 | // This little bit of nastyness is to allow us to add a primary |
| 322 | // reference to the remote proxy when this team doesn't have one |
| 323 | // but another team is sending the handle to us. |
| 324 | result.force_set(b); |
| 325 | e->refs->decWeak(this); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return result; |
| 330 | } |
| 331 | |
| 332 | wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle) |
| 333 | { |
| 334 | wp<IBinder> result; |
| 335 | |
| 336 | AutoMutex _l(mLock); |
| 337 | |
| 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 |
| 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. |
| 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)) { |
Michael Wachenschwanz | 2d34990 | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 350 | b = BpBinder::create(handle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | result = b; |
| 352 | e->binder = b; |
| 353 | if (b) e->refs = b->getWeakRefs(); |
| 354 | } else { |
| 355 | result = b; |
| 356 | e->refs->decWeak(this); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return result; |
| 361 | } |
| 362 | |
| 363 | void ProcessState::expungeHandle(int32_t handle, IBinder* binder) |
| 364 | { |
| 365 | AutoMutex _l(mLock); |
| 366 | |
| 367 | handle_entry* e = lookupHandleLocked(handle); |
| 368 | |
| 369 | // This handle may have already been replaced with a new BpBinder |
| 370 | // (if someone failed the AttemptIncWeak() above); we don't want |
| 371 | // to overwrite it. |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 372 | if (e && e->binder == binder) e->binder = nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 375 | String8 ProcessState::makeBinderThreadName() { |
| 376 | int32_t s = android_atomic_add(1, &mThreadPoolSeq); |
Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 377 | pid_t pid = getpid(); |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 378 | String8 name; |
Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 379 | name.appendFormat("Binder:%d_%X", pid, s); |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 380 | return name; |
| 381 | } |
| 382 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | void ProcessState::spawnPooledThread(bool isMain) |
| 384 | { |
| 385 | if (mThreadPoolStarted) { |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 386 | String8 name = makeBinderThreadName(); |
| 387 | 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] | 388 | sp<Thread> t = new PoolThread(isMain); |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 389 | t->run(name.string()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 393 | status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) { |
| 394 | status_t result = NO_ERROR; |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 395 | if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) { |
| 396 | mMaxThreads = maxThreads; |
| 397 | } else { |
Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 398 | result = -errno; |
| 399 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result)); |
| 400 | } |
| 401 | return result; |
| 402 | } |
| 403 | |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 404 | void ProcessState::giveThreadPoolName() { |
| 405 | androidSetThreadName( makeBinderThreadName().string() ); |
| 406 | } |
| 407 | |
Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 408 | String8 ProcessState::getDriverName() { |
| 409 | return mDriverName; |
| 410 | } |
| 411 | |
Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 412 | static int open_driver(const char *driver) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | { |
Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 414 | int fd = open(driver, O_RDWR | O_CLOEXEC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | if (fd >= 0) { |
Jin Wei | 78181df | 2012-10-18 17:00:48 +0800 | [diff] [blame] | 416 | int vers = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | status_t result = ioctl(fd, BINDER_VERSION, &vers); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | if (result == -1) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 419 | 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] | 420 | close(fd); |
| 421 | fd = -1; |
| 422 | } |
| 423 | if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { |
Greg Hartman | 67ac00f | 2017-01-03 16:54:59 -0800 | [diff] [blame] | 424 | ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d", |
| 425 | vers, BINDER_CURRENT_PROTOCOL_VERSION, result); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | close(fd); |
| 427 | fd = -1; |
| 428 | } |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 429 | size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); |
| 431 | if (result == -1) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 432 | 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] | 433 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } else { |
Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 435 | ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | } |
| 437 | return fd; |
| 438 | } |
| 439 | |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 440 | ProcessState::ProcessState(const char *driver, size_t mmap_size) |
Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 441 | : mDriverName(String8(driver)) |
| 442 | , mDriverFD(open_driver(driver)) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | , mVMStart(MAP_FAILED) |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 444 | , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER) |
| 445 | , mThreadCountDecrement(PTHREAD_COND_INITIALIZER) |
| 446 | , mExecutingThreadsCount(0) |
| 447 | , mMaxThreads(DEFAULT_MAX_BINDER_THREADS) |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 448 | , mStarvationStartTimeMs(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | , mManagesContexts(false) |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 450 | , mBinderContextCheckFunc(nullptr) |
| 451 | , mBinderContextUserData(nullptr) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | , mThreadPoolStarted(false) |
| 453 | , mThreadPoolSeq(1) |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 454 | , mMmapSize(mmap_size) |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 455 | , mCallRestriction(CallRestriction::NONE) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | { |
| 457 | if (mDriverFD >= 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | // mmap the binder, providing a chunk of virtual address space to receive transactions. |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 459 | mVMStart = mmap(nullptr, mMmapSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | if (mVMStart == MAP_FAILED) { |
| 461 | // *sigh* |
Steven Moreland | 376a559 | 2017-12-19 15:42:16 -0800 | [diff] [blame] | 462 | 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] | 463 | close(mDriverFD); |
| 464 | mDriverFD = -1; |
Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 465 | mDriverName.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | } |
Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 468 | |
Steven Moreland | 8c78e2e | 2019-07-10 16:13:12 -0700 | [diff] [blame] | 469 | LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver '%s' could not be opened. Terminating.", driver); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | ProcessState::~ProcessState() |
| 473 | { |
zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 474 | if (mDriverFD >= 0) { |
| 475 | if (mVMStart != MAP_FAILED) { |
Martijn Coenen | d26d3de | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 476 | munmap(mVMStart, mMmapSize); |
zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 477 | } |
| 478 | close(mDriverFD); |
| 479 | } |
| 480 | mDriverFD = -1; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | }; // namespace android |