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