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