| 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> | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 24 | #include <binder/Stability.h> | 
| Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 25 | #include <cutils/atomic.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> | 
|  | 27 | #include <utils/String8.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> | 
| Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 31 | #include "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 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [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 | } | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +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 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [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 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 105 | sp<ProcessState> ProcessState::selfOrNull() | 
|  | 106 | { | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 107 | Mutex::Autolock _l(gProcessMutex); | 
|  | 108 | return gProcess; | 
|  | 109 | } | 
|  | 110 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 111 | sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | { | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 113 | sp<IBinder> context = getStrongProxyForHandle(0); | 
|  | 114 |  | 
|  | 115 | // The root object is special since we get it directly from the driver, it is never | 
|  | 116 | // written by Parcell::writeStrongBinder. | 
|  | 117 | internal::Stability::tryMarkCompilationUnit(context.get()); | 
|  | 118 |  | 
|  | 119 | return context; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | void ProcessState::startThreadPool() | 
|  | 123 | { | 
|  | 124 | AutoMutex _l(mLock); | 
|  | 125 | if (!mThreadPoolStarted) { | 
|  | 126 | mThreadPoolStarted = true; | 
|  | 127 | spawnPooledThread(true); | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData) | 
|  | 132 | { | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 133 | AutoMutex _l(mLock); | 
|  | 134 | mBinderContextCheckFunc = checkFunc; | 
|  | 135 | mBinderContextUserData = userData; | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 136 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 137 | flat_binder_object obj { | 
|  | 138 | .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX, | 
|  | 139 | }; | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 140 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 141 | int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 142 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 143 | // fallback to original method | 
|  | 144 | if (result != 0) { | 
|  | 145 | android_errorWriteLog(0x534e4554, "121035042"); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 146 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 147 | int dummy = 0; | 
|  | 148 | result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 150 |  | 
|  | 151 | if (result == -1) { | 
|  | 152 | mBinderContextCheckFunc = nullptr; | 
|  | 153 | mBinderContextUserData = nullptr; | 
|  | 154 | ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno)); | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | return result == 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 160 | // Get references to userspace objects held by the kernel binder driver | 
|  | 161 | // Writes up to count elements into buf, and returns the total number | 
|  | 162 | // of references the kernel has, which may be larger than count. | 
|  | 163 | // buf may be NULL if count is 0.  The pointers returned by this method | 
|  | 164 | // should only be used for debugging and not dereferenced, they may | 
|  | 165 | // already be invalid. | 
|  | 166 | ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) | 
|  | 167 | { | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 168 | binder_node_debug_info info = {}; | 
|  | 169 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 170 | uintptr_t* end = buf ? buf + buf_count : nullptr; | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 171 | size_t count = 0; | 
|  | 172 |  | 
|  | 173 | do { | 
|  | 174 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info); | 
|  | 175 | if (result < 0) { | 
|  | 176 | return -1; | 
|  | 177 | } | 
|  | 178 | if (info.ptr != 0) { | 
|  | 179 | if (buf && buf < end) | 
|  | 180 | *buf++ = info.ptr; | 
|  | 181 | count++; | 
|  | 182 | if (buf && buf < end) | 
|  | 183 | *buf++ = info.cookie; | 
|  | 184 | count++; | 
|  | 185 | } | 
|  | 186 | } while (info.ptr != 0); | 
|  | 187 |  | 
|  | 188 | return count; | 
|  | 189 | } | 
|  | 190 |  | 
| Jon Spivack | 902e6fc | 2019-10-18 21:22:37 -0700 | [diff] [blame] | 191 | // Queries the driver for the current strong reference count of the node | 
|  | 192 | // that the handle points to. Can only be used by the servicemanager. | 
|  | 193 | // | 
|  | 194 | // Returns -1 in case of failure, otherwise the strong reference count. | 
|  | 195 | ssize_t ProcessState::getStrongRefCountForNodeByHandle(int32_t handle) { | 
|  | 196 | binder_node_info_for_ref info; | 
|  | 197 | memset(&info, 0, sizeof(binder_node_info_for_ref)); | 
|  | 198 |  | 
|  | 199 | info.handle = handle; | 
|  | 200 |  | 
|  | 201 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info); | 
|  | 202 |  | 
|  | 203 | if (result != OK) { | 
|  | 204 | static bool logged = false; | 
|  | 205 | if (!logged) { | 
|  | 206 | ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF."); | 
|  | 207 | logged = true; | 
|  | 208 | } | 
|  | 209 | return -1; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | return info.strong_count; | 
|  | 213 | } | 
|  | 214 |  | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 215 | void ProcessState::setCallRestriction(CallRestriction restriction) { | 
| Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 216 | LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr, | 
|  | 217 | "Call restrictions must be set before the threadpool is started."); | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 218 |  | 
|  | 219 | mCallRestriction = restriction; | 
|  | 220 | } | 
|  | 221 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle) | 
|  | 223 | { | 
|  | 224 | const size_t N=mHandleToObject.size(); | 
|  | 225 | if (N <= (size_t)handle) { | 
|  | 226 | handle_entry e; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 227 | e.binder = nullptr; | 
|  | 228 | e.refs = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | status_t err = mHandleToObject.insertAt(e, N, handle+1-N); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 230 | if (err < NO_ERROR) return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | } | 
|  | 232 | return &mHandleToObject.editItemAt(handle); | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) | 
|  | 236 | { | 
|  | 237 | sp<IBinder> result; | 
|  | 238 |  | 
|  | 239 | AutoMutex _l(mLock); | 
|  | 240 |  | 
|  | 241 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 242 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 243 | if (e != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | // 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] | 245 | // are unable to acquire a weak reference on this current one.  The | 
|  | 246 | // attemptIncWeak() is safe because we know the BpBinder destructor will always | 
|  | 247 | // call expungeHandle(), which acquires the same lock we are holding now. | 
|  | 248 | // We need to do this because there is a race condition between someone | 
|  | 249 | // releasing a reference on this BpBinder, and a new reference on its handle | 
|  | 250 | // arriving from the driver. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | IBinder* b = e->binder; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 252 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 253 | if (handle == 0) { | 
|  | 254 | // Special case for context manager... | 
|  | 255 | // The context manager is the only object for which we create | 
|  | 256 | // a BpBinder proxy without already holding a reference. | 
|  | 257 | // Perform a dummy transaction to ensure the context manager | 
|  | 258 | // is registered before we create the first local reference | 
|  | 259 | // to it (which will occur when creating the BpBinder). | 
|  | 260 | // If a local reference is created for the BpBinder when the | 
|  | 261 | // context manager is not present, the driver will fail to | 
|  | 262 | // provide a reference to the context manager, but the | 
|  | 263 | // driver API does not return status. | 
|  | 264 | // | 
|  | 265 | // Note that this is not race-free if the context manager | 
|  | 266 | // dies while this code runs. | 
|  | 267 | // | 
|  | 268 | // TODO: add a driver API to wait for context manager, or | 
|  | 269 | // stop special casing handle 0 for context manager and add | 
|  | 270 | // a driver API to get a handle to the context manager with | 
|  | 271 | // proper reference counting. | 
|  | 272 |  | 
|  | 273 | Parcel data; | 
|  | 274 | status_t status = IPCThreadState::self()->transact( | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 275 | 0, IBinder::PING_TRANSACTION, data, nullptr, 0); | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 276 | if (status == DEAD_OBJECT) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 277 | return nullptr; | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 278 | } | 
|  | 279 |  | 
| Michael Wachenschwanz | 2d34990 | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 280 | b = BpBinder::create(handle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | e->binder = b; | 
|  | 282 | if (b) e->refs = b->getWeakRefs(); | 
|  | 283 | result = b; | 
|  | 284 | } else { | 
|  | 285 | // This little bit of nastyness is to allow us to add a primary | 
|  | 286 | // reference to the remote proxy when this team doesn't have one | 
|  | 287 | // but another team is sending the handle to us. | 
|  | 288 | result.force_set(b); | 
|  | 289 | e->refs->decWeak(this); | 
|  | 290 | } | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | return result; | 
|  | 294 | } | 
|  | 295 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | void ProcessState::expungeHandle(int32_t handle, IBinder* binder) | 
|  | 297 | { | 
|  | 298 | AutoMutex _l(mLock); | 
|  | 299 |  | 
|  | 300 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 301 |  | 
|  | 302 | // This handle may have already been replaced with a new BpBinder | 
|  | 303 | // (if someone failed the AttemptIncWeak() above); we don't want | 
|  | 304 | // to overwrite it. | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 305 | if (e && e->binder == binder) e->binder = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | } | 
|  | 307 |  | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 308 | String8 ProcessState::makeBinderThreadName() { | 
|  | 309 | int32_t s = android_atomic_add(1, &mThreadPoolSeq); | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 310 | pid_t pid = getpid(); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 311 | String8 name; | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 312 | name.appendFormat("Binder:%d_%X", pid, s); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 313 | return name; | 
|  | 314 | } | 
|  | 315 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | void ProcessState::spawnPooledThread(bool isMain) | 
|  | 317 | { | 
|  | 318 | if (mThreadPoolStarted) { | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 319 | String8 name = makeBinderThreadName(); | 
|  | 320 | 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] | 321 | sp<Thread> t = new PoolThread(isMain); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 322 | t->run(name.string()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 326 | status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) { | 
|  | 327 | status_t result = NO_ERROR; | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 328 | if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) { | 
|  | 329 | mMaxThreads = maxThreads; | 
|  | 330 | } else { | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 331 | result = -errno; | 
|  | 332 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result)); | 
|  | 333 | } | 
|  | 334 | return result; | 
|  | 335 | } | 
|  | 336 |  | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 337 | void ProcessState::giveThreadPoolName() { | 
|  | 338 | androidSetThreadName( makeBinderThreadName().string() ); | 
|  | 339 | } | 
|  | 340 |  | 
| Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 341 | String8 ProcessState::getDriverName() { | 
|  | 342 | return mDriverName; | 
|  | 343 | } | 
|  | 344 |  | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 345 | static int open_driver(const char *driver) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | { | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 347 | int fd = open(driver, O_RDWR | O_CLOEXEC); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | if (fd >= 0) { | 
| Jin Wei | 78181df | 2012-10-18 17:00:48 +0800 | [diff] [blame] | 349 | int vers = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | status_t result = ioctl(fd, BINDER_VERSION, &vers); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | if (result == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 352 | 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] | 353 | close(fd); | 
|  | 354 | fd = -1; | 
|  | 355 | } | 
|  | 356 | if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { | 
| Greg Hartman | 67ac00f | 2017-01-03 16:54:59 -0800 | [diff] [blame] | 357 | ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d", | 
|  | 358 | vers, BINDER_CURRENT_PROTOCOL_VERSION, result); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | close(fd); | 
|  | 360 | fd = -1; | 
|  | 361 | } | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 362 | size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); | 
|  | 364 | if (result == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 365 | 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] | 366 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | } else { | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 368 | ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | } | 
|  | 370 | return fd; | 
|  | 371 | } | 
|  | 372 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 373 | ProcessState::ProcessState(const char *driver) | 
| Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 374 | : mDriverName(String8(driver)) | 
|  | 375 | , mDriverFD(open_driver(driver)) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | , mVMStart(MAP_FAILED) | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 377 | , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER) | 
|  | 378 | , mThreadCountDecrement(PTHREAD_COND_INITIALIZER) | 
|  | 379 | , mExecutingThreadsCount(0) | 
|  | 380 | , mMaxThreads(DEFAULT_MAX_BINDER_THREADS) | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 381 | , mStarvationStartTimeMs(0) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 382 | , mBinderContextCheckFunc(nullptr) | 
|  | 383 | , mBinderContextUserData(nullptr) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | , mThreadPoolStarted(false) | 
|  | 385 | , mThreadPoolSeq(1) | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 386 | , mCallRestriction(CallRestriction::NONE) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | { | 
| Steven Moreland | b6d0e76 | 2019-11-15 00:51:42 -0800 | [diff] [blame] | 388 |  | 
|  | 389 | // TODO(b/139016109): enforce in build system | 
|  | 390 | #if defined(__ANDROID_APEX__) && !defined(__ANDROID_APEX_COM_ANDROID_VNDK_CURRENT__) | 
|  | 391 | LOG_ALWAYS_FATAL("Cannot use libbinder in APEX (only system.img libbinder) since it is not stable."); | 
|  | 392 | #endif | 
|  | 393 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | if (mDriverFD >= 0) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | // 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] | 396 | 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] | 397 | if (mVMStart == MAP_FAILED) { | 
|  | 398 | // *sigh* | 
| Steven Moreland | 376a559 | 2017-12-19 15:42:16 -0800 | [diff] [blame] | 399 | 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] | 400 | close(mDriverFD); | 
|  | 401 | mDriverFD = -1; | 
| Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 402 | mDriverName.clear(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 405 |  | 
| Steven Moreland | 24bc0d1 | 2019-10-11 12:29:20 -0700 | [diff] [blame] | 406 | #ifdef __ANDROID__ | 
| Steven Moreland | 8c78e2e | 2019-07-10 16:13:12 -0700 | [diff] [blame] | 407 | LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver '%s' could not be opened.  Terminating.", driver); | 
| Steven Moreland | 24bc0d1 | 2019-10-11 12:29:20 -0700 | [diff] [blame] | 408 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | } | 
|  | 410 |  | 
|  | 411 | ProcessState::~ProcessState() | 
|  | 412 | { | 
| zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 413 | if (mDriverFD >= 0) { | 
|  | 414 | if (mVMStart != MAP_FAILED) { | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 415 | munmap(mVMStart, BINDER_VM_SIZE); | 
| zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 416 | } | 
|  | 417 | close(mDriverFD); | 
|  | 418 | } | 
|  | 419 | mDriverFD = -1; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | } | 
|  | 421 |  | 
| Steven Moreland | 6511af5 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 422 | } // namespace android |