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