blob: a07b3a043bf8859cabf2444bf4abfac0318a171f [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/ProcessState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/BpBinder.h>
22#include <binder/IPCThreadState.h>
Steven Moreland2716e112018-02-23 14:57:20 -080023#include <binder/IServiceManager.h>
24#include <cutils/atomic.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Log.h>
26#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/threads.h>
28
Mathias Agopian208059f2009-05-18 15:08:03 -070029#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070030#include "Static.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
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 Cuadraa082fa82016-04-08 10:29:14 -070040#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Steven Moreland072cc7e2019-07-12 21:01:54 +000042#define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2)
Wale Ogunwale376b8222015-04-13 16:16:10 -070043#define DEFAULT_MAX_BINDER_THREADS 15
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Martijn Coenen7bca77a2019-03-13 11:51:08 +000045#ifdef __ANDROID_VNDK__
46const char* kDefaultDriver = "/dev/vndbinder";
47#else
Steven Moreland2ae2f5e2018-07-06 13:02:53 -070048const char* kDefaultDriver = "/dev/binder";
Martijn Coenen7bca77a2019-03-13 11:51:08 +000049#endif
Steven Moreland2ae2f5e2018-07-06 13:02:53 -070050
Philip Cuadraa082fa82016-04-08 10:29:14 -070051// -------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53namespace android {
Wale Ogunwale376b8222015-04-13 16:16:10 -070054
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055class PoolThread : public Thread
56{
57public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070058 explicit PoolThread(bool isMain)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 : mIsMain(isMain)
60 {
61 }
62
63protected:
64 virtual bool threadLoop()
65 {
66 IPCThreadState::self()->joinThreadPool(mIsMain);
67 return false;
68 }
69
70 const bool mIsMain;
71};
72
73sp<ProcessState> ProcessState::self()
74{
Mathias Agopiane8db8712012-04-16 19:30:56 -070075 Mutex::Autolock _l(gProcessMutex);
Yi Kongfdd8da92018-06-07 17:52:27 -070076 if (gProcess != nullptr) {
Mathias Agopiane8db8712012-04-16 19:30:56 -070077 return gProcess;
78 }
Steven Moreland072cc7e2019-07-12 21:01:54 +000079 gProcess = new ProcessState(kDefaultDriver);
Martijn Coenen55d871c2017-03-21 15:56:40 -070080 return gProcess;
81}
82
83sp<ProcessState> ProcessState::initWithDriver(const char* driver)
84{
85 Mutex::Autolock _l(gProcessMutex);
Yi Kongfdd8da92018-06-07 17:52:27 -070086 if (gProcess != nullptr) {
Iliyan Malcheved1ffe02017-04-14 00:34:57 -070087 // 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 Coenen55d871c2017-03-21 15:56:40 -070092 LOG_ALWAYS_FATAL("ProcessState was already initialized.");
93 }
Steven Moreland376a5592017-12-19 15:42:16 -080094
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 Moreland072cc7e2019-07-12 21:01:54 +0000100 gProcess = new ProcessState(driver);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 return gProcess;
102}
103
Steven Moreland072cc7e2019-07-12 21:01:54 +0000104sp<ProcessState> ProcessState::selfOrNull()
105{
Colin Cross9d45ccc2017-06-20 17:48:33 -0700106 Mutex::Autolock _l(gProcessMutex);
107 return gProcess;
108}
109
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800110sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111{
Jeff Browne16986c2011-07-08 18:52:57 -0700112 return getStrongProxyForHandle(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113}
114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115void ProcessState::startThreadPool()
116{
117 AutoMutex _l(mLock);
118 if (!mThreadPoolStarted) {
119 mThreadPoolStarted = true;
120 spawnPooledThread(true);
121 }
122}
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
125{
Steven Moreland4411d712019-07-12 14:02:53 -0700126 AutoMutex _l(mLock);
127 mBinderContextCheckFunc = checkFunc;
128 mBinderContextUserData = userData;
Jeff Browne16986c2011-07-08 18:52:57 -0700129
Steven Moreland4411d712019-07-12 14:02:53 -0700130 flat_binder_object obj {
131 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
132 };
Steven Moreland3085a472018-12-26 13:59:23 -0800133
Steven Moreland4411d712019-07-12 14:02:53 -0700134 int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
Steven Moreland3085a472018-12-26 13:59:23 -0800135
Steven Moreland4411d712019-07-12 14:02:53 -0700136 // fallback to original method
137 if (result != 0) {
138 android_errorWriteLog(0x534e4554, "121035042");
Steven Moreland3085a472018-12-26 13:59:23 -0800139
Steven Moreland4411d712019-07-12 14:02:53 -0700140 int dummy = 0;
141 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 }
Steven Moreland4411d712019-07-12 14:02:53 -0700143
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 Projectedbf3b62009-03-03 19:31:44 -0800151}
152
Colin Cross9d45ccc2017-06-20 17:48:33 -0700153// 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.
159ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf)
160{
Colin Cross9d45ccc2017-06-20 17:48:33 -0700161 binder_node_debug_info info = {};
162
Yi Kongfdd8da92018-06-07 17:52:27 -0700163 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Cross9d45ccc2017-06-20 17:48:33 -0700164 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 Moreland7732a092019-01-02 17:54:16 -0800184void 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 Projectedbf3b62009-03-03 19:31:44 -0800190ProcessState::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 Kongfdd8da92018-06-07 17:52:27 -0700195 e.binder = nullptr;
196 e.refs = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kongfdd8da92018-06-07 17:52:27 -0700198 if (err < NO_ERROR) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 }
200 return &mHandleToObject.editItemAt(handle);
201}
202
203sp<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 Kongfdd8da92018-06-07 17:52:27 -0700211 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 // We need to create a new BpBinder if there isn't currently one, OR we
213 // are unable to acquire a weak reference on this current one. See comment
214 // in getWeakProxyForHandle() for more info about this.
215 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700216 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700217 if (handle == 0) {
218 // Special case for context manager...
219 // The context manager is the only object for which we create
220 // a BpBinder proxy without already holding a reference.
221 // Perform a dummy transaction to ensure the context manager
222 // is registered before we create the first local reference
223 // to it (which will occur when creating the BpBinder).
224 // If a local reference is created for the BpBinder when the
225 // context manager is not present, the driver will fail to
226 // provide a reference to the context manager, but the
227 // driver API does not return status.
228 //
229 // Note that this is not race-free if the context manager
230 // dies while this code runs.
231 //
232 // TODO: add a driver API to wait for context manager, or
233 // stop special casing handle 0 for context manager and add
234 // a driver API to get a handle to the context manager with
235 // proper reference counting.
236
237 Parcel data;
238 status_t status = IPCThreadState::self()->transact(
Yi Kongfdd8da92018-06-07 17:52:27 -0700239 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
Todd Poynora7b0f042013-06-18 17:25:37 -0700240 if (status == DEAD_OBJECT)
Yi Kongfdd8da92018-06-07 17:52:27 -0700241 return nullptr;
Todd Poynora7b0f042013-06-18 17:25:37 -0700242 }
243
Michael Wachenschwanz2d349902017-08-15 00:57:14 -0700244 b = BpBinder::create(handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 e->binder = b;
246 if (b) e->refs = b->getWeakRefs();
247 result = b;
248 } else {
249 // This little bit of nastyness is to allow us to add a primary
250 // reference to the remote proxy when this team doesn't have one
251 // but another team is sending the handle to us.
252 result.force_set(b);
253 e->refs->decWeak(this);
254 }
255 }
256
257 return result;
258}
259
260wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
261{
262 wp<IBinder> result;
263
264 AutoMutex _l(mLock);
265
266 handle_entry* e = lookupHandleLocked(handle);
267
Yi Kongfdd8da92018-06-07 17:52:27 -0700268 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 // We need to create a new BpBinder if there isn't currently one, OR we
270 // are unable to acquire a weak reference on this current one. The
271 // attemptIncWeak() is safe because we know the BpBinder destructor will always
272 // call expungeHandle(), which acquires the same lock we are holding now.
273 // We need to do this because there is a race condition between someone
274 // releasing a reference on this BpBinder, and a new reference on its handle
275 // arriving from the driver.
276 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700277 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Michael Wachenschwanz2d349902017-08-15 00:57:14 -0700278 b = BpBinder::create(handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 result = b;
280 e->binder = b;
281 if (b) e->refs = b->getWeakRefs();
282 } else {
283 result = b;
284 e->refs->decWeak(this);
285 }
286 }
287
288 return result;
289}
290
291void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
292{
293 AutoMutex _l(mLock);
294
295 handle_entry* e = lookupHandleLocked(handle);
296
297 // This handle may have already been replaced with a new BpBinder
298 // (if someone failed the AttemptIncWeak() above); we don't want
299 // to overwrite it.
Yi Kongfdd8da92018-06-07 17:52:27 -0700300 if (e && e->binder == binder) e->binder = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301}
302
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800303String8 ProcessState::makeBinderThreadName() {
304 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700305 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800306 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700307 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800308 return name;
309}
310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311void ProcessState::spawnPooledThread(bool isMain)
312{
313 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800314 String8 name = makeBinderThreadName();
315 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800317 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
319}
320
Mathias Agopian1b80f792012-04-17 16:11:08 -0700321status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
322 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700323 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
324 mMaxThreads = maxThreads;
325 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700326 result = -errno;
327 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
328 }
329 return result;
330}
331
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800332void ProcessState::giveThreadPoolName() {
333 androidSetThreadName( makeBinderThreadName().string() );
334}
335
Iliyan Malchev32062242017-04-10 14:06:11 -0700336String8 ProcessState::getDriverName() {
337 return mDriverName;
338}
339
Martijn Coenen55d871c2017-03-21 15:56:40 -0700340static int open_driver(const char *driver)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341{
Martijn Coenen55d871c2017-03-21 15:56:40 -0700342 int fd = open(driver, O_RDWR | O_CLOEXEC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 if (fd >= 0) {
Jin Wei78181df2012-10-18 17:00:48 +0800344 int vers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000347 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 close(fd);
349 fd = -1;
350 }
351 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartman67ac00f2017-01-03 16:54:59 -0800352 ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d",
353 vers, BINDER_CURRENT_PROTOCOL_VERSION, result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 close(fd);
355 fd = -1;
356 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700357 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
359 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000360 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 } else {
Martijn Coenen55d871c2017-03-21 15:56:40 -0700363 ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 }
365 return fd;
366}
367
Steven Moreland072cc7e2019-07-12 21:01:54 +0000368ProcessState::ProcessState(const char *driver)
Iliyan Malchev32062242017-04-10 14:06:11 -0700369 : mDriverName(String8(driver))
370 , mDriverFD(open_driver(driver))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 , mVMStart(MAP_FAILED)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700372 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
373 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
374 , mExecutingThreadsCount(0)
375 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Cross96e83222016-04-15 14:29:55 -0700376 , mStarvationStartTimeMs(0)
Yi Kongfdd8da92018-06-07 17:52:27 -0700377 , mBinderContextCheckFunc(nullptr)
378 , mBinderContextUserData(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 , mThreadPoolStarted(false)
380 , mThreadPoolSeq(1)
Steven Moreland7732a092019-01-02 17:54:16 -0800381 , mCallRestriction(CallRestriction::NONE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382{
383 if (mDriverFD >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Steven Moreland072cc7e2019-07-12 21:01:54 +0000385 mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 if (mVMStart == MAP_FAILED) {
387 // *sigh*
Steven Moreland376a5592017-12-19 15:42:16 -0800388 ALOGE("Using %s failed: unable to mmap transaction memory.\n", mDriverName.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 close(mDriverFD);
390 mDriverFD = -1;
Iliyan Malchev32062242017-04-10 14:06:11 -0700391 mDriverName.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 }
Jeff Browne16986c2011-07-08 18:52:57 -0700394
Steven Moreland8c78e2e2019-07-10 16:13:12 -0700395 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver '%s' could not be opened. Terminating.", driver);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396}
397
398ProcessState::~ProcessState()
399{
zhongjieff405782016-03-09 15:05:04 +0800400 if (mDriverFD >= 0) {
401 if (mVMStart != MAP_FAILED) {
Steven Moreland072cc7e2019-07-12 21:01:54 +0000402 munmap(mVMStart, BINDER_VM_SIZE);
zhongjieff405782016-03-09 15:05:04 +0800403 }
404 close(mDriverFD);
405 }
406 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407}
408
409}; // namespace android