blob: 26b93ff7a647daa6f76cc0907e566133491c0ce9 [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>
30#include <private/binder/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
Martijn Coenend26d3de2018-03-22 17:12:57 +010042#define DEFAULT_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 }
Martijn Coenend26d3de2018-03-22 17:12:57 +010079 gProcess = new ProcessState(kDefaultDriver, DEFAULT_BINDER_VM_SIZE);
80 return gProcess;
81}
82
83sp<ProcessState> ProcessState::selfOrNull()
84{
85 Mutex::Autolock _l(gProcessMutex);
Martijn Coenen55d871c2017-03-21 15:56:40 -070086 return gProcess;
87}
88
89sp<ProcessState> ProcessState::initWithDriver(const char* driver)
90{
91 Mutex::Autolock _l(gProcessMutex);
Yi Kongfdd8da92018-06-07 17:52:27 -070092 if (gProcess != nullptr) {
Iliyan Malcheved1ffe02017-04-14 00:34:57 -070093 // Allow for initWithDriver to be called repeatedly with the same
94 // driver.
95 if (!strcmp(gProcess->getDriverName().c_str(), driver)) {
96 return gProcess;
97 }
Martijn Coenen55d871c2017-03-21 15:56:40 -070098 LOG_ALWAYS_FATAL("ProcessState was already initialized.");
99 }
Steven Moreland376a5592017-12-19 15:42:16 -0800100
101 if (access(driver, R_OK) == -1) {
102 ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver);
103 driver = "/dev/binder";
104 }
105
Martijn Coenend26d3de2018-03-22 17:12:57 +0100106 gProcess = new ProcessState(driver, DEFAULT_BINDER_VM_SIZE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 return gProcess;
108}
109
Martijn Coenend26d3de2018-03-22 17:12:57 +0100110sp<ProcessState> ProcessState::initWithMmapSize(size_t mmap_size) {
Colin Cross9d45ccc2017-06-20 17:48:33 -0700111 Mutex::Autolock _l(gProcessMutex);
Martijn Coenend26d3de2018-03-22 17:12:57 +0100112 if (gProcess != nullptr) {
113 LOG_ALWAYS_FATAL_IF(mmap_size != gProcess->getMmapSize(),
114 "ProcessState already initialized with a different mmap size.");
115 return gProcess;
116 }
117
118 gProcess = new ProcessState(kDefaultDriver, mmap_size);
Colin Cross9d45ccc2017-06-20 17:48:33 -0700119 return gProcess;
120}
121
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800122sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123{
Jeff Browne16986c2011-07-08 18:52:57 -0700124 return getStrongProxyForHandle(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125}
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127void ProcessState::startThreadPool()
128{
129 AutoMutex _l(mLock);
130 if (!mThreadPoolStarted) {
131 mThreadPoolStarted = true;
132 spawnPooledThread(true);
133 }
134}
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
137{
Steven Moreland4411d712019-07-12 14:02:53 -0700138 AutoMutex _l(mLock);
139 mBinderContextCheckFunc = checkFunc;
140 mBinderContextUserData = userData;
Jeff Browne16986c2011-07-08 18:52:57 -0700141
Steven Moreland4411d712019-07-12 14:02:53 -0700142 flat_binder_object obj {
143 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
144 };
Steven Moreland3085a472018-12-26 13:59:23 -0800145
Steven Moreland4411d712019-07-12 14:02:53 -0700146 int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
Steven Moreland3085a472018-12-26 13:59:23 -0800147
Steven Moreland4411d712019-07-12 14:02:53 -0700148 // fallback to original method
149 if (result != 0) {
150 android_errorWriteLog(0x534e4554, "121035042");
Steven Moreland3085a472018-12-26 13:59:23 -0800151
Steven Moreland4411d712019-07-12 14:02:53 -0700152 int dummy = 0;
153 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 }
Steven Moreland4411d712019-07-12 14:02:53 -0700155
156 if (result == -1) {
157 mBinderContextCheckFunc = nullptr;
158 mBinderContextUserData = nullptr;
159 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
160 }
161
162 return result == 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163}
164
Colin Cross9d45ccc2017-06-20 17:48:33 -0700165// Get references to userspace objects held by the kernel binder driver
166// Writes up to count elements into buf, and returns the total number
167// of references the kernel has, which may be larger than count.
168// buf may be NULL if count is 0. The pointers returned by this method
169// should only be used for debugging and not dereferenced, they may
170// already be invalid.
171ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf)
172{
Colin Cross9d45ccc2017-06-20 17:48:33 -0700173 binder_node_debug_info info = {};
174
Yi Kongfdd8da92018-06-07 17:52:27 -0700175 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Cross9d45ccc2017-06-20 17:48:33 -0700176 size_t count = 0;
177
178 do {
179 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
180 if (result < 0) {
181 return -1;
182 }
183 if (info.ptr != 0) {
184 if (buf && buf < end)
185 *buf++ = info.ptr;
186 count++;
187 if (buf && buf < end)
188 *buf++ = info.cookie;
189 count++;
190 }
191 } while (info.ptr != 0);
192
193 return count;
194}
195
Martijn Coenend26d3de2018-03-22 17:12:57 +0100196size_t ProcessState::getMmapSize() {
197 return mMmapSize;
198}
199
Steven Moreland7732a092019-01-02 17:54:16 -0800200void ProcessState::setCallRestriction(CallRestriction restriction) {
201 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull(), "Call restrictions must be set before the threadpool is started.");
202
203 mCallRestriction = restriction;
204}
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
207{
208 const size_t N=mHandleToObject.size();
209 if (N <= (size_t)handle) {
210 handle_entry e;
Yi Kongfdd8da92018-06-07 17:52:27 -0700211 e.binder = nullptr;
212 e.refs = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kongfdd8da92018-06-07 17:52:27 -0700214 if (err < NO_ERROR) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 }
216 return &mHandleToObject.editItemAt(handle);
217}
218
219sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
220{
221 sp<IBinder> result;
222
223 AutoMutex _l(mLock);
224
225 handle_entry* e = lookupHandleLocked(handle);
226
Yi Kongfdd8da92018-06-07 17:52:27 -0700227 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 // We need to create a new BpBinder if there isn't currently one, OR we
229 // are unable to acquire a weak reference on this current one. See comment
230 // in getWeakProxyForHandle() for more info about this.
231 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700232 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700233 if (handle == 0) {
234 // Special case for context manager...
235 // The context manager is the only object for which we create
236 // a BpBinder proxy without already holding a reference.
237 // Perform a dummy transaction to ensure the context manager
238 // is registered before we create the first local reference
239 // to it (which will occur when creating the BpBinder).
240 // If a local reference is created for the BpBinder when the
241 // context manager is not present, the driver will fail to
242 // provide a reference to the context manager, but the
243 // driver API does not return status.
244 //
245 // Note that this is not race-free if the context manager
246 // dies while this code runs.
247 //
248 // TODO: add a driver API to wait for context manager, or
249 // stop special casing handle 0 for context manager and add
250 // a driver API to get a handle to the context manager with
251 // proper reference counting.
252
253 Parcel data;
254 status_t status = IPCThreadState::self()->transact(
Yi Kongfdd8da92018-06-07 17:52:27 -0700255 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
Todd Poynora7b0f042013-06-18 17:25:37 -0700256 if (status == DEAD_OBJECT)
Yi Kongfdd8da92018-06-07 17:52:27 -0700257 return nullptr;
Todd Poynora7b0f042013-06-18 17:25:37 -0700258 }
259
Michael Wachenschwanz2d349902017-08-15 00:57:14 -0700260 b = BpBinder::create(handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 e->binder = b;
262 if (b) e->refs = b->getWeakRefs();
263 result = b;
264 } else {
265 // This little bit of nastyness is to allow us to add a primary
266 // reference to the remote proxy when this team doesn't have one
267 // but another team is sending the handle to us.
268 result.force_set(b);
269 e->refs->decWeak(this);
270 }
271 }
272
273 return result;
274}
275
276wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
277{
278 wp<IBinder> result;
279
280 AutoMutex _l(mLock);
281
282 handle_entry* e = lookupHandleLocked(handle);
283
Yi Kongfdd8da92018-06-07 17:52:27 -0700284 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285 // We need to create a new BpBinder if there isn't currently one, OR we
286 // are unable to acquire a weak reference on this current one. The
287 // attemptIncWeak() is safe because we know the BpBinder destructor will always
288 // call expungeHandle(), which acquires the same lock we are holding now.
289 // We need to do this because there is a race condition between someone
290 // releasing a reference on this BpBinder, and a new reference on its handle
291 // arriving from the driver.
292 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700293 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Michael Wachenschwanz2d349902017-08-15 00:57:14 -0700294 b = BpBinder::create(handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 result = b;
296 e->binder = b;
297 if (b) e->refs = b->getWeakRefs();
298 } else {
299 result = b;
300 e->refs->decWeak(this);
301 }
302 }
303
304 return result;
305}
306
307void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
308{
309 AutoMutex _l(mLock);
310
311 handle_entry* e = lookupHandleLocked(handle);
312
313 // This handle may have already been replaced with a new BpBinder
314 // (if someone failed the AttemptIncWeak() above); we don't want
315 // to overwrite it.
Yi Kongfdd8da92018-06-07 17:52:27 -0700316 if (e && e->binder == binder) e->binder = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317}
318
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800319String8 ProcessState::makeBinderThreadName() {
320 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700321 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800322 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700323 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800324 return name;
325}
326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327void ProcessState::spawnPooledThread(bool isMain)
328{
329 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800330 String8 name = makeBinderThreadName();
331 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800333 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 }
335}
336
Mathias Agopian1b80f792012-04-17 16:11:08 -0700337status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
338 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700339 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
340 mMaxThreads = maxThreads;
341 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700342 result = -errno;
343 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
344 }
345 return result;
346}
347
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800348void ProcessState::giveThreadPoolName() {
349 androidSetThreadName( makeBinderThreadName().string() );
350}
351
Iliyan Malchev32062242017-04-10 14:06:11 -0700352String8 ProcessState::getDriverName() {
353 return mDriverName;
354}
355
Martijn Coenen55d871c2017-03-21 15:56:40 -0700356static int open_driver(const char *driver)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357{
Martijn Coenen55d871c2017-03-21 15:56:40 -0700358 int fd = open(driver, O_RDWR | O_CLOEXEC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 if (fd >= 0) {
Jin Wei78181df2012-10-18 17:00:48 +0800360 int vers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000363 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 close(fd);
365 fd = -1;
366 }
367 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartman67ac00f2017-01-03 16:54:59 -0800368 ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d",
369 vers, BINDER_CURRENT_PROTOCOL_VERSION, result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370 close(fd);
371 fd = -1;
372 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700373 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
375 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000376 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 } else {
Martijn Coenen55d871c2017-03-21 15:56:40 -0700379 ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 }
381 return fd;
382}
383
Martijn Coenend26d3de2018-03-22 17:12:57 +0100384ProcessState::ProcessState(const char *driver, size_t mmap_size)
Iliyan Malchev32062242017-04-10 14:06:11 -0700385 : mDriverName(String8(driver))
386 , mDriverFD(open_driver(driver))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 , mVMStart(MAP_FAILED)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700388 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
389 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
390 , mExecutingThreadsCount(0)
391 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Cross96e83222016-04-15 14:29:55 -0700392 , mStarvationStartTimeMs(0)
Yi Kongfdd8da92018-06-07 17:52:27 -0700393 , mBinderContextCheckFunc(nullptr)
394 , mBinderContextUserData(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 , mThreadPoolStarted(false)
396 , mThreadPoolSeq(1)
Martijn Coenend26d3de2018-03-22 17:12:57 +0100397 , mMmapSize(mmap_size)
Steven Moreland7732a092019-01-02 17:54:16 -0800398 , mCallRestriction(CallRestriction::NONE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399{
400 if (mDriverFD >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Martijn Coenend26d3de2018-03-22 17:12:57 +0100402 mVMStart = mmap(nullptr, mMmapSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 if (mVMStart == MAP_FAILED) {
404 // *sigh*
Steven Moreland376a5592017-12-19 15:42:16 -0800405 ALOGE("Using %s failed: unable to mmap transaction memory.\n", mDriverName.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 close(mDriverFD);
407 mDriverFD = -1;
Iliyan Malchev32062242017-04-10 14:06:11 -0700408 mDriverName.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 }
Jeff Browne16986c2011-07-08 18:52:57 -0700411
Steven Moreland8c78e2e2019-07-10 16:13:12 -0700412 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 -0800413}
414
415ProcessState::~ProcessState()
416{
zhongjieff405782016-03-09 15:05:04 +0800417 if (mDriverFD >= 0) {
418 if (mVMStart != MAP_FAILED) {
Martijn Coenend26d3de2018-03-22 17:12:57 +0100419 munmap(mVMStart, mMmapSize);
zhongjieff405782016-03-09 15:05:04 +0800420 }
421 close(mDriverFD);
422 }
423 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424}
425
426}; // namespace android