blob: 1e1bc3a50eb157f32b00be526d79a4d4c4c0b695 [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
Steven Morelande171d622019-07-17 16:06:01 -0700213 // 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 Projectedbf3b62009-03-03 19:31:44 -0800219 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700220 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700221 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 Kongfdd8da92018-06-07 17:52:27 -0700243 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
Todd Poynora7b0f042013-06-18 17:25:37 -0700244 if (status == DEAD_OBJECT)
Yi Kongfdd8da92018-06-07 17:52:27 -0700245 return nullptr;
Todd Poynora7b0f042013-06-18 17:25:37 -0700246 }
247
Michael Wachenschwanz2d349902017-08-15 00:57:14 -0700248 b = BpBinder::create(handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 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 Projectedbf3b62009-03-03 19:31:44 -0800264void 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 Kongfdd8da92018-06-07 17:52:27 -0700273 if (e && e->binder == binder) e->binder = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274}
275
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800276String8 ProcessState::makeBinderThreadName() {
277 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700278 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800279 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700280 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800281 return name;
282}
283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284void ProcessState::spawnPooledThread(bool isMain)
285{
286 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800287 String8 name = makeBinderThreadName();
288 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800290 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 }
292}
293
Mathias Agopian1b80f792012-04-17 16:11:08 -0700294status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
295 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700296 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
297 mMaxThreads = maxThreads;
298 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700299 result = -errno;
300 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
301 }
302 return result;
303}
304
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800305void ProcessState::giveThreadPoolName() {
306 androidSetThreadName( makeBinderThreadName().string() );
307}
308
Iliyan Malchev32062242017-04-10 14:06:11 -0700309String8 ProcessState::getDriverName() {
310 return mDriverName;
311}
312
Martijn Coenen55d871c2017-03-21 15:56:40 -0700313static int open_driver(const char *driver)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314{
Martijn Coenen55d871c2017-03-21 15:56:40 -0700315 int fd = open(driver, O_RDWR | O_CLOEXEC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 if (fd >= 0) {
Jin Wei78181df2012-10-18 17:00:48 +0800317 int vers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000320 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 close(fd);
322 fd = -1;
323 }
324 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartman67ac00f2017-01-03 16:54:59 -0800325 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 Projectedbf3b62009-03-03 19:31:44 -0800327 close(fd);
328 fd = -1;
329 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700330 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
332 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000333 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 } else {
Martijn Coenen55d871c2017-03-21 15:56:40 -0700336 ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 }
338 return fd;
339}
340
Steven Moreland072cc7e2019-07-12 21:01:54 +0000341ProcessState::ProcessState(const char *driver)
Iliyan Malchev32062242017-04-10 14:06:11 -0700342 : mDriverName(String8(driver))
343 , mDriverFD(open_driver(driver))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 , mVMStart(MAP_FAILED)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700345 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
346 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
347 , mExecutingThreadsCount(0)
348 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Cross96e83222016-04-15 14:29:55 -0700349 , mStarvationStartTimeMs(0)
Yi Kongfdd8da92018-06-07 17:52:27 -0700350 , mBinderContextCheckFunc(nullptr)
351 , mBinderContextUserData(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 , mThreadPoolStarted(false)
353 , mThreadPoolSeq(1)
Steven Moreland7732a092019-01-02 17:54:16 -0800354 , mCallRestriction(CallRestriction::NONE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355{
356 if (mDriverFD >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Steven Moreland072cc7e2019-07-12 21:01:54 +0000358 mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 if (mVMStart == MAP_FAILED) {
360 // *sigh*
Steven Moreland376a5592017-12-19 15:42:16 -0800361 ALOGE("Using %s failed: unable to mmap transaction memory.\n", mDriverName.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 close(mDriverFD);
363 mDriverFD = -1;
Iliyan Malchev32062242017-04-10 14:06:11 -0700364 mDriverName.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366 }
Jeff Browne16986c2011-07-08 18:52:57 -0700367
Steven Moreland8c78e2e2019-07-10 16:13:12 -0700368 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 -0800369}
370
371ProcessState::~ProcessState()
372{
zhongjieff405782016-03-09 15:05:04 +0800373 if (mDriverFD >= 0) {
374 if (mVMStart != MAP_FAILED) {
Steven Moreland072cc7e2019-07-12 21:01:54 +0000375 munmap(mVMStart, BINDER_VM_SIZE);
zhongjieff405782016-03-09 15:05:04 +0800376 }
377 close(mDriverFD);
378 }
379 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380}
381
382}; // namespace android