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