| 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 |  | 
| Rebecca Schultz Zavin | c0c1092 | 2009-10-30 18:39:55 -0700 | [diff] [blame] | 43 | #define BINDER_VM_SIZE ((1*1024*1024) - (4096 *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 | } | 
|  | 74 | gProcess = new ProcessState; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | return gProcess; | 
|  | 76 | } | 
|  | 77 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | void ProcessState::setContextObject(const sp<IBinder>& object) | 
|  | 79 | { | 
|  | 80 | setContextObject(object, String16("default")); | 
|  | 81 | } | 
|  | 82 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 83 | sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | { | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 85 | return getStrongProxyForHandle(0); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name) | 
|  | 89 | { | 
|  | 90 | AutoMutex _l(mLock); | 
|  | 91 | mContexts.add(name, object); | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller) | 
|  | 95 | { | 
|  | 96 | mLock.lock(); | 
|  | 97 | sp<IBinder> object( | 
|  | 98 | mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : NULL); | 
|  | 99 | mLock.unlock(); | 
|  | 100 |  | 
|  | 101 | //printf("Getting context object %s for %p\n", String8(name).string(), caller.get()); | 
|  | 102 |  | 
|  | 103 | if (object != NULL) return object; | 
|  | 104 |  | 
|  | 105 | // Don't attempt to retrieve contexts if we manage them | 
|  | 106 | if (mManagesContexts) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 107 | 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] | 108 | String8(name).string()); | 
|  | 109 | return NULL; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 113 | { | 
|  | 114 | Parcel data, reply; | 
|  | 115 | // no interface token on this magic transaction | 
|  | 116 | data.writeString16(name); | 
|  | 117 | data.writeStrongBinder(caller); | 
|  | 118 | status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0); | 
|  | 119 | if (result == NO_ERROR) { | 
|  | 120 | object = reply.readStrongBinder(); | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | ipc->flushCommands(); | 
|  | 125 |  | 
|  | 126 | if (object != NULL) setContextObject(object, name); | 
|  | 127 | return object; | 
|  | 128 | } | 
|  | 129 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | void ProcessState::startThreadPool() | 
|  | 131 | { | 
|  | 132 | AutoMutex _l(mLock); | 
|  | 133 | if (!mThreadPoolStarted) { | 
|  | 134 | mThreadPoolStarted = true; | 
|  | 135 | spawnPooledThread(true); | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | bool ProcessState::isContextManager(void) const | 
|  | 140 | { | 
|  | 141 | return mManagesContexts; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData) | 
|  | 145 | { | 
|  | 146 | if (!mManagesContexts) { | 
|  | 147 | AutoMutex _l(mLock); | 
|  | 148 | mBinderContextCheckFunc = checkFunc; | 
|  | 149 | mBinderContextUserData = userData; | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 150 |  | 
|  | 151 | int dummy = 0; | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 152 | status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy); | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 153 | if (result == 0) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | mManagesContexts = true; | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 155 | } else if (result == -1) { | 
|  | 156 | mBinderContextCheckFunc = NULL; | 
|  | 157 | mBinderContextUserData = NULL; | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 158 | 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] | 159 | } | 
|  | 160 | } | 
|  | 161 | return mManagesContexts; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle) | 
|  | 165 | { | 
|  | 166 | const size_t N=mHandleToObject.size(); | 
|  | 167 | if (N <= (size_t)handle) { | 
|  | 168 | handle_entry e; | 
|  | 169 | e.binder = NULL; | 
|  | 170 | e.refs = NULL; | 
|  | 171 | status_t err = mHandleToObject.insertAt(e, N, handle+1-N); | 
|  | 172 | if (err < NO_ERROR) return NULL; | 
|  | 173 | } | 
|  | 174 | return &mHandleToObject.editItemAt(handle); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) | 
|  | 178 | { | 
|  | 179 | sp<IBinder> result; | 
|  | 180 |  | 
|  | 181 | AutoMutex _l(mLock); | 
|  | 182 |  | 
|  | 183 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 184 |  | 
|  | 185 | if (e != NULL) { | 
|  | 186 | // We need to create a new BpBinder if there isn't currently one, OR we | 
|  | 187 | // are unable to acquire a weak reference on this current one.  See comment | 
|  | 188 | // in getWeakProxyForHandle() for more info about this. | 
|  | 189 | IBinder* b = e->binder; | 
|  | 190 | if (b == NULL || !e->refs->attemptIncWeak(this)) { | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 191 | if (handle == 0) { | 
|  | 192 | // Special case for context manager... | 
|  | 193 | // The context manager is the only object for which we create | 
|  | 194 | // a BpBinder proxy without already holding a reference. | 
|  | 195 | // Perform a dummy transaction to ensure the context manager | 
|  | 196 | // is registered before we create the first local reference | 
|  | 197 | // to it (which will occur when creating the BpBinder). | 
|  | 198 | // If a local reference is created for the BpBinder when the | 
|  | 199 | // context manager is not present, the driver will fail to | 
|  | 200 | // provide a reference to the context manager, but the | 
|  | 201 | // driver API does not return status. | 
|  | 202 | // | 
|  | 203 | // Note that this is not race-free if the context manager | 
|  | 204 | // dies while this code runs. | 
|  | 205 | // | 
|  | 206 | // TODO: add a driver API to wait for context manager, or | 
|  | 207 | // stop special casing handle 0 for context manager and add | 
|  | 208 | // a driver API to get a handle to the context manager with | 
|  | 209 | // proper reference counting. | 
|  | 210 |  | 
|  | 211 | Parcel data; | 
|  | 212 | status_t status = IPCThreadState::self()->transact( | 
|  | 213 | 0, IBinder::PING_TRANSACTION, data, NULL, 0); | 
|  | 214 | if (status == DEAD_OBJECT) | 
|  | 215 | return NULL; | 
|  | 216 | } | 
|  | 217 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | b = new BpBinder(handle); | 
|  | 219 | e->binder = b; | 
|  | 220 | if (b) e->refs = b->getWeakRefs(); | 
|  | 221 | result = b; | 
|  | 222 | } else { | 
|  | 223 | // This little bit of nastyness is to allow us to add a primary | 
|  | 224 | // reference to the remote proxy when this team doesn't have one | 
|  | 225 | // but another team is sending the handle to us. | 
|  | 226 | result.force_set(b); | 
|  | 227 | e->refs->decWeak(this); | 
|  | 228 | } | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | return result; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle) | 
|  | 235 | { | 
|  | 236 | wp<IBinder> result; | 
|  | 237 |  | 
|  | 238 | AutoMutex _l(mLock); | 
|  | 239 |  | 
|  | 240 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 241 |  | 
|  | 242 | if (e != NULL) { | 
|  | 243 | // We need to create a new BpBinder if there isn't currently one, OR we | 
|  | 244 | // are unable to acquire a weak reference on this current one.  The | 
|  | 245 | // attemptIncWeak() is safe because we know the BpBinder destructor will always | 
|  | 246 | // call expungeHandle(), which acquires the same lock we are holding now. | 
|  | 247 | // We need to do this because there is a race condition between someone | 
|  | 248 | // releasing a reference on this BpBinder, and a new reference on its handle | 
|  | 249 | // arriving from the driver. | 
|  | 250 | IBinder* b = e->binder; | 
|  | 251 | if (b == NULL || !e->refs->attemptIncWeak(this)) { | 
|  | 252 | b = new BpBinder(handle); | 
|  | 253 | result = b; | 
|  | 254 | e->binder = b; | 
|  | 255 | if (b) e->refs = b->getWeakRefs(); | 
|  | 256 | } else { | 
|  | 257 | result = b; | 
|  | 258 | e->refs->decWeak(this); | 
|  | 259 | } | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | return result; | 
|  | 263 | } | 
|  | 264 |  | 
|  | 265 | void ProcessState::expungeHandle(int32_t handle, IBinder* binder) | 
|  | 266 | { | 
|  | 267 | AutoMutex _l(mLock); | 
|  | 268 |  | 
|  | 269 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 270 |  | 
|  | 271 | // This handle may have already been replaced with a new BpBinder | 
|  | 272 | // (if someone failed the AttemptIncWeak() above); we don't want | 
|  | 273 | // to overwrite it. | 
|  | 274 | if (e && e->binder == binder) e->binder = NULL; | 
|  | 275 | } | 
|  | 276 |  | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 277 | String8 ProcessState::makeBinderThreadName() { | 
|  | 278 | int32_t s = android_atomic_add(1, &mThreadPoolSeq); | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 279 | pid_t pid = getpid(); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 280 | String8 name; | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 281 | name.appendFormat("Binder:%d_%X", pid, s); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 282 | return name; | 
|  | 283 | } | 
|  | 284 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | void ProcessState::spawnPooledThread(bool isMain) | 
|  | 286 | { | 
|  | 287 | if (mThreadPoolStarted) { | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 288 | String8 name = makeBinderThreadName(); | 
|  | 289 | 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] | 290 | sp<Thread> t = new PoolThread(isMain); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 291 | t->run(name.string()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | } | 
|  | 293 | } | 
|  | 294 |  | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 295 | status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) { | 
|  | 296 | status_t result = NO_ERROR; | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 297 | if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) { | 
|  | 298 | mMaxThreads = maxThreads; | 
|  | 299 | } else { | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 300 | result = -errno; | 
|  | 301 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result)); | 
|  | 302 | } | 
|  | 303 | return result; | 
|  | 304 | } | 
|  | 305 |  | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 306 | void ProcessState::giveThreadPoolName() { | 
|  | 307 | androidSetThreadName( makeBinderThreadName().string() ); | 
|  | 308 | } | 
|  | 309 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | static int open_driver() | 
|  | 311 | { | 
| Nick Kralevich | 0fe7ce3 | 2015-12-23 18:58:05 -0800 | [diff] [blame] | 312 | int fd = open("/dev/binder", O_RDWR | O_CLOEXEC); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | if (fd >= 0) { | 
| Jin Wei | 78181df | 2012-10-18 17:00:48 +0800 | [diff] [blame] | 314 | int vers = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | status_t result = ioctl(fd, BINDER_VERSION, &vers); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | if (result == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 317 | 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] | 318 | close(fd); | 
|  | 319 | fd = -1; | 
|  | 320 | } | 
|  | 321 | if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 322 | ALOGE("Binder driver protocol does not match user space protocol!"); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | close(fd); | 
|  | 324 | fd = -1; | 
|  | 325 | } | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 326 | size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); | 
|  | 328 | if (result == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 329 | 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] | 330 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } else { | 
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 332 | ALOGW("Opening '/dev/binder' failed: %s\n", strerror(errno)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } | 
|  | 334 | return fd; | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | ProcessState::ProcessState() | 
|  | 338 | : mDriverFD(open_driver()) | 
|  | 339 | , mVMStart(MAP_FAILED) | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 340 | , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER) | 
|  | 341 | , mThreadCountDecrement(PTHREAD_COND_INITIALIZER) | 
|  | 342 | , mExecutingThreadsCount(0) | 
|  | 343 | , mMaxThreads(DEFAULT_MAX_BINDER_THREADS) | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 344 | , mStarvationStartTimeMs(0) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | , mManagesContexts(false) | 
|  | 346 | , mBinderContextCheckFunc(NULL) | 
|  | 347 | , mBinderContextUserData(NULL) | 
|  | 348 | , mThreadPoolStarted(false) | 
|  | 349 | , mThreadPoolSeq(1) | 
|  | 350 | { | 
|  | 351 | if (mDriverFD >= 0) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | // mmap the binder, providing a chunk of virtual address space to receive transactions. | 
|  | 353 | mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0); | 
|  | 354 | if (mVMStart == MAP_FAILED) { | 
|  | 355 | // *sigh* | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 356 | 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] | 357 | close(mDriverFD); | 
|  | 358 | mDriverFD = -1; | 
|  | 359 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | } | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 361 |  | 
|  | 362 | 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] | 363 | } | 
|  | 364 |  | 
|  | 365 | ProcessState::~ProcessState() | 
|  | 366 | { | 
| zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 367 | if (mDriverFD >= 0) { | 
|  | 368 | if (mVMStart != MAP_FAILED) { | 
|  | 369 | munmap(mVMStart, BINDER_VM_SIZE); | 
|  | 370 | } | 
|  | 371 | close(mDriverFD); | 
|  | 372 | } | 
|  | 373 | mDriverFD = -1; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | } | 
|  | 375 |  | 
|  | 376 | }; // namespace android |