blob: 4f21cda4a1d2557bd5a00a4762ce821a74668f14 [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
Steven Moreland13a43c92021-08-30 13:21:48 -070021#include <android-base/result.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/BpBinder.h>
23#include <binder/IPCThreadState.h>
Steven Moreland2716e112018-02-23 14:57:20 -080024#include <binder/IServiceManager.h>
Steven Morelandc709dd82019-08-05 20:30:14 -070025#include <binder/Stability.h>
Steven Moreland2716e112018-02-23 14:57:20 -080026#include <cutils/atomic.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/Log.h>
28#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/threads.h>
30
Steven Morelanda4853cd2019-07-12 15:44:37 -070031#include "Static.h"
Steven Moreland6ba5a252021-05-04 22:49:00 +000032#include "binder_module.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include <errno.h>
35#include <fcntl.h>
Steven Moreland4fdb12f2020-07-21 02:21:48 +000036#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <sys/ioctl.h>
41#include <sys/mman.h>
42#include <sys/stat.h>
Philip Cuadraa082fa82016-04-08 10:29:14 -070043#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Steven Moreland072cc7e2019-07-12 21:01:54 +000045#define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2)
Wale Ogunwale376b8222015-04-13 16:16:10 -070046#define DEFAULT_MAX_BINDER_THREADS 15
Hang Lub185ac02021-03-24 13:17:22 +080047#define DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION 1
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Martijn Coenen7bca77a2019-03-13 11:51:08 +000049#ifdef __ANDROID_VNDK__
50const char* kDefaultDriver = "/dev/vndbinder";
51#else
Steven Moreland2ae2f5e2018-07-06 13:02:53 -070052const char* kDefaultDriver = "/dev/binder";
Martijn Coenen7bca77a2019-03-13 11:51:08 +000053#endif
Steven Moreland2ae2f5e2018-07-06 13:02:53 -070054
Philip Cuadraa082fa82016-04-08 10:29:14 -070055// -------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
57namespace android {
Wale Ogunwale376b8222015-04-13 16:16:10 -070058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059class PoolThread : public Thread
60{
61public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070062 explicit PoolThread(bool isMain)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 : mIsMain(isMain)
64 {
65 }
Jooyung Han1b228f42020-01-30 13:41:12 +090066
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067protected:
68 virtual bool threadLoop()
69 {
70 IPCThreadState::self()->joinThreadPool(mIsMain);
71 return false;
72 }
Jooyung Han1b228f42020-01-30 13:41:12 +090073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 const bool mIsMain;
75};
76
77sp<ProcessState> ProcessState::self()
78{
Steven Moreland4fdb12f2020-07-21 02:21:48 +000079 return init(kDefaultDriver, false /*requireDefault*/);
Martijn Coenen55d871c2017-03-21 15:56:40 -070080}
81
82sp<ProcessState> ProcessState::initWithDriver(const char* driver)
83{
Steven Moreland4fdb12f2020-07-21 02:21:48 +000084 return init(driver, true /*requireDefault*/);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
Steven Moreland072cc7e2019-07-12 21:01:54 +000087sp<ProcessState> ProcessState::selfOrNull()
88{
Steven Moreland4fdb12f2020-07-21 02:21:48 +000089 return init(nullptr, false /*requireDefault*/);
90}
91
Steven Morelandee9df902021-10-14 14:00:08 -070092[[clang::no_destroy]] static sp<ProcessState> gProcess;
93[[clang::no_destroy]] static std::mutex gProcessMutex;
94
95static void verifyNotForked(bool forked) {
Steven Morelandbd98e0f2021-10-14 14:24:15 -070096 LOG_ALWAYS_FATAL_IF(forked, "libbinder ProcessState can not be used after fork");
Steven Morelandee9df902021-10-14 14:00:08 -070097}
98
Steven Moreland4fdb12f2020-07-21 02:21:48 +000099sp<ProcessState> ProcessState::init(const char *driver, bool requireDefault)
100{
Steven Moreland4fdb12f2020-07-21 02:21:48 +0000101
102 if (driver == nullptr) {
103 std::lock_guard<std::mutex> l(gProcessMutex);
Steven Morelandee9df902021-10-14 14:00:08 -0700104 if (gProcess) {
105 verifyNotForked(gProcess->mForked);
106 }
Steven Moreland4fdb12f2020-07-21 02:21:48 +0000107 return gProcess;
108 }
109
110 [[clang::no_destroy]] static std::once_flag gProcessOnce;
111 std::call_once(gProcessOnce, [&](){
112 if (access(driver, R_OK) == -1) {
113 ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver);
114 driver = "/dev/binder";
115 }
116
Steven Morelandee9df902021-10-14 14:00:08 -0700117 // we must install these before instantiating the gProcess object,
118 // otherwise this would race with creating it, and there could be the
119 // possibility of an invalid gProcess object forked by another thread
120 // before these are installed
121 int ret = pthread_atfork(ProcessState::onFork, ProcessState::parentPostFork,
122 ProcessState::childPostFork);
123 LOG_ALWAYS_FATAL_IF(ret != 0, "pthread_atfork error %s", strerror(ret));
124
Steven Moreland4fdb12f2020-07-21 02:21:48 +0000125 std::lock_guard<std::mutex> l(gProcessMutex);
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000126 gProcess = sp<ProcessState>::make(driver);
Steven Moreland4fdb12f2020-07-21 02:21:48 +0000127 });
128
129 if (requireDefault) {
130 // Detect if we are trying to initialize with a different driver, and
131 // consider that an error. ProcessState will only be initialized once above.
132 LOG_ALWAYS_FATAL_IF(gProcess->getDriverName() != driver,
133 "ProcessState was already initialized with %s,"
134 " can't initialize with %s.",
135 gProcess->getDriverName().c_str(), driver);
136 }
137
Steven Morelandee9df902021-10-14 14:00:08 -0700138 verifyNotForked(gProcess->mForked);
Colin Cross9d45ccc2017-06-20 17:48:33 -0700139 return gProcess;
140}
141
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800142sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143{
Steven Morelandc709dd82019-08-05 20:30:14 -0700144 sp<IBinder> context = getStrongProxyForHandle(0);
145
Steven Moreland4da8fb02020-12-29 22:51:32 +0000146 if (context) {
147 // The root object is special since we get it directly from the driver, it is never
148 // written by Parcell::writeStrongBinder.
149 internal::Stability::markCompilationUnit(context.get());
150 } else {
151 ALOGW("Not able to get context object on %s.", mDriverName.c_str());
Steven Moreland8d93a712020-02-19 15:16:15 -0800152 }
153
Steven Morelandc709dd82019-08-05 20:30:14 -0700154 return context;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155}
156
Steven Morelandee9df902021-10-14 14:00:08 -0700157void ProcessState::onFork() {
158 // make sure another thread isn't currently retrieving ProcessState
159 gProcessMutex.lock();
160}
161
162void ProcessState::parentPostFork() {
163 gProcessMutex.unlock();
164}
165
166void ProcessState::childPostFork() {
167 // another thread might call fork before gProcess is instantiated, but after
168 // the thread handler is installed
169 if (gProcess) {
170 gProcess->mForked = true;
171 }
172 gProcessMutex.unlock();
173}
174
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175void ProcessState::startThreadPool()
176{
177 AutoMutex _l(mLock);
178 if (!mThreadPoolStarted) {
179 mThreadPoolStarted = true;
180 spawnPooledThread(true);
181 }
182}
183
Steven Moreland61096622020-08-31 23:36:39 +0000184bool ProcessState::becomeContextManager()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185{
Steven Moreland4411d712019-07-12 14:02:53 -0700186 AutoMutex _l(mLock);
Jeff Browne16986c2011-07-08 18:52:57 -0700187
Steven Moreland4411d712019-07-12 14:02:53 -0700188 flat_binder_object obj {
189 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
190 };
Steven Moreland3085a472018-12-26 13:59:23 -0800191
Steven Moreland4411d712019-07-12 14:02:53 -0700192 int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
Steven Moreland3085a472018-12-26 13:59:23 -0800193
Steven Moreland4411d712019-07-12 14:02:53 -0700194 // fallback to original method
195 if (result != 0) {
196 android_errorWriteLog(0x534e4554, "121035042");
Steven Moreland3085a472018-12-26 13:59:23 -0800197
Hungming Chen28b42522020-08-28 17:29:55 +0800198 int unused = 0;
199 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 }
Steven Moreland4411d712019-07-12 14:02:53 -0700201
202 if (result == -1) {
Steven Moreland4411d712019-07-12 14:02:53 -0700203 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
204 }
205
206 return result == 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
Colin Cross9d45ccc2017-06-20 17:48:33 -0700209// Get references to userspace objects held by the kernel binder driver
210// Writes up to count elements into buf, and returns the total number
211// of references the kernel has, which may be larger than count.
212// buf may be NULL if count is 0. The pointers returned by this method
213// should only be used for debugging and not dereferenced, they may
214// already be invalid.
215ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf)
216{
Colin Cross9d45ccc2017-06-20 17:48:33 -0700217 binder_node_debug_info info = {};
218
Yi Kongfdd8da92018-06-07 17:52:27 -0700219 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Cross9d45ccc2017-06-20 17:48:33 -0700220 size_t count = 0;
221
222 do {
223 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
224 if (result < 0) {
225 return -1;
226 }
227 if (info.ptr != 0) {
228 if (buf && buf < end)
229 *buf++ = info.ptr;
230 count++;
231 if (buf && buf < end)
232 *buf++ = info.cookie;
233 count++;
234 }
235 } while (info.ptr != 0);
236
237 return count;
238}
239
Jon Spivack902e6fc2019-10-18 21:22:37 -0700240// Queries the driver for the current strong reference count of the node
241// that the handle points to. Can only be used by the servicemanager.
242//
243// Returns -1 in case of failure, otherwise the strong reference count.
Steven Morelande8393882020-12-18 02:27:20 +0000244ssize_t ProcessState::getStrongRefCountForNode(const sp<BpBinder>& binder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000245 if (binder->isRpcBinder()) return -1;
246
Jon Spivack902e6fc2019-10-18 21:22:37 -0700247 binder_node_info_for_ref info;
248 memset(&info, 0, sizeof(binder_node_info_for_ref));
249
Steven Moreland99157622021-09-13 16:27:34 -0700250 info.handle = binder->getPrivateAccessor().binderHandle();
Jon Spivack902e6fc2019-10-18 21:22:37 -0700251
252 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info);
253
254 if (result != OK) {
255 static bool logged = false;
256 if (!logged) {
257 ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF.");
258 logged = true;
259 }
260 return -1;
261 }
262
263 return info.strong_count;
264}
265
Steven Moreland7732a092019-01-02 17:54:16 -0800266void ProcessState::setCallRestriction(CallRestriction restriction) {
Steven Moreland28723ae2019-04-01 18:52:30 -0700267 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr,
268 "Call restrictions must be set before the threadpool is started.");
Steven Moreland7732a092019-01-02 17:54:16 -0800269
270 mCallRestriction = restriction;
271}
272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
274{
275 const size_t N=mHandleToObject.size();
276 if (N <= (size_t)handle) {
277 handle_entry e;
Yi Kongfdd8da92018-06-07 17:52:27 -0700278 e.binder = nullptr;
279 e.refs = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kongfdd8da92018-06-07 17:52:27 -0700281 if (err < NO_ERROR) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 }
283 return &mHandleToObject.editItemAt(handle);
284}
285
286sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
287{
288 sp<IBinder> result;
289
290 AutoMutex _l(mLock);
291
292 handle_entry* e = lookupHandleLocked(handle);
293
Yi Kongfdd8da92018-06-07 17:52:27 -0700294 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 // We need to create a new BpBinder if there isn't currently one, OR we
Steven Morelande171d622019-07-17 16:06:01 -0700296 // are unable to acquire a weak reference on this current one. The
297 // attemptIncWeak() is safe because we know the BpBinder destructor will always
298 // call expungeHandle(), which acquires the same lock we are holding now.
299 // We need to do this because there is a race condition between someone
300 // releasing a reference on this BpBinder, and a new reference on its handle
301 // arriving from the driver.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700303 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700304 if (handle == 0) {
305 // Special case for context manager...
306 // The context manager is the only object for which we create
307 // a BpBinder proxy without already holding a reference.
308 // Perform a dummy transaction to ensure the context manager
309 // is registered before we create the first local reference
310 // to it (which will occur when creating the BpBinder).
311 // If a local reference is created for the BpBinder when the
312 // context manager is not present, the driver will fail to
313 // provide a reference to the context manager, but the
314 // driver API does not return status.
315 //
316 // Note that this is not race-free if the context manager
317 // dies while this code runs.
318 //
319 // TODO: add a driver API to wait for context manager, or
320 // stop special casing handle 0 for context manager and add
321 // a driver API to get a handle to the context manager with
322 // proper reference counting.
323
Steven Moreland9514b202020-09-21 18:03:27 +0000324 IPCThreadState* ipc = IPCThreadState::self();
325
326 CallRestriction originalCallRestriction = ipc->getCallRestriction();
327 ipc->setCallRestriction(CallRestriction::NONE);
328
Todd Poynora7b0f042013-06-18 17:25:37 -0700329 Parcel data;
Steven Moreland9514b202020-09-21 18:03:27 +0000330 status_t status = ipc->transact(
Yi Kongfdd8da92018-06-07 17:52:27 -0700331 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
Steven Moreland9514b202020-09-21 18:03:27 +0000332
333 ipc->setCallRestriction(originalCallRestriction);
334
Todd Poynora7b0f042013-06-18 17:25:37 -0700335 if (status == DEAD_OBJECT)
Yi Kongfdd8da92018-06-07 17:52:27 -0700336 return nullptr;
Todd Poynora7b0f042013-06-18 17:25:37 -0700337 }
338
Steven Moreland99157622021-09-13 16:27:34 -0700339 sp<BpBinder> b = BpBinder::PrivateAccessor::create(handle);
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000340 e->binder = b.get();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 if (b) e->refs = b->getWeakRefs();
342 result = b;
343 } else {
344 // This little bit of nastyness is to allow us to add a primary
345 // reference to the remote proxy when this team doesn't have one
346 // but another team is sending the handle to us.
347 result.force_set(b);
348 e->refs->decWeak(this);
349 }
350 }
351
352 return result;
353}
354
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
356{
357 AutoMutex _l(mLock);
Jooyung Han1b228f42020-01-30 13:41:12 +0900358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 handle_entry* e = lookupHandleLocked(handle);
360
361 // This handle may have already been replaced with a new BpBinder
362 // (if someone failed the AttemptIncWeak() above); we don't want
363 // to overwrite it.
Yi Kongfdd8da92018-06-07 17:52:27 -0700364 if (e && e->binder == binder) e->binder = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365}
366
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800367String8 ProcessState::makeBinderThreadName() {
368 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700369 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800370 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700371 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800372 return name;
373}
374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375void ProcessState::spawnPooledThread(bool isMain)
376{
377 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800378 String8 name = makeBinderThreadName();
379 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000380 sp<Thread> t = sp<PoolThread>::make(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800381 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 }
383}
384
Mathias Agopian1b80f792012-04-17 16:11:08 -0700385status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
Steven Moreland1fdb98b2020-03-06 21:42:12 +0000386 LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads,
387 "Binder threadpool cannot be shrunk after starting");
Mathias Agopian1b80f792012-04-17 16:11:08 -0700388 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700389 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
390 mMaxThreads = maxThreads;
391 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700392 result = -errno;
393 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
394 }
395 return result;
396}
397
Yifan Hong84bedeb2021-04-21 21:37:17 -0700398size_t ProcessState::getThreadPoolMaxThreadCount() const {
399 // may actually be one more than this, if join is called
400 if (mThreadPoolStarted) return mMaxThreads;
401 // must not be initialized or maybe has poll thread setup, we
402 // currently don't track this in libbinder
403 return 0;
404}
405
Hang Lub185ac02021-03-24 13:17:22 +0800406status_t ProcessState::enableOnewaySpamDetection(bool enable) {
407 uint32_t enableDetection = enable ? 1 : 0;
408 if (ioctl(mDriverFD, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enableDetection) == -1) {
Martijn Coenende0a39e2021-04-22 14:38:46 +0200409 ALOGI("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
Hang Lub185ac02021-03-24 13:17:22 +0800410 return -errno;
411 }
412 return NO_ERROR;
413}
414
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800415void ProcessState::giveThreadPoolName() {
416 androidSetThreadName( makeBinderThreadName().string() );
417}
418
Iliyan Malchev32062242017-04-10 14:06:11 -0700419String8 ProcessState::getDriverName() {
420 return mDriverName;
421}
422
Steven Moreland13a43c92021-08-30 13:21:48 -0700423static base::Result<int> open_driver(const char* driver) {
Martijn Coenen55d871c2017-03-21 15:56:40 -0700424 int fd = open(driver, O_RDWR | O_CLOEXEC);
Steven Moreland13a43c92021-08-30 13:21:48 -0700425 if (fd < 0) {
426 return base::ErrnoError() << "Opening '" << driver << "' failed";
427 }
428 int vers = 0;
429 status_t result = ioctl(fd, BINDER_VERSION, &vers);
430 if (result == -1) {
431 close(fd);
432 return base::ErrnoError() << "Binder ioctl to obtain version failed";
433 }
434 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
435 close(fd);
436 return base::Error() << "Binder driver protocol(" << vers
437 << ") does not match user space protocol("
438 << BINDER_CURRENT_PROTOCOL_VERSION
439 << ")! ioctl() return value: " << result;
440 }
441 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
442 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
443 if (result == -1) {
444 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
445 }
446 uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION;
447 result = ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable);
448 if (result == -1) {
449 ALOGV("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450 }
451 return fd;
452}
453
Steven Moreland13a43c92021-08-30 13:21:48 -0700454ProcessState::ProcessState(const char* driver)
455 : mDriverName(String8(driver)),
456 mDriverFD(-1),
457 mVMStart(MAP_FAILED),
458 mThreadCountLock(PTHREAD_MUTEX_INITIALIZER),
459 mThreadCountDecrement(PTHREAD_COND_INITIALIZER),
460 mExecutingThreadsCount(0),
461 mWaitingForThreads(0),
462 mMaxThreads(DEFAULT_MAX_BINDER_THREADS),
463 mStarvationStartTimeMs(0),
Steven Morelandee9df902021-10-14 14:00:08 -0700464 mForked(false),
Steven Moreland13a43c92021-08-30 13:21:48 -0700465 mThreadPoolStarted(false),
466 mThreadPoolSeq(1),
467 mCallRestriction(CallRestriction::NONE) {
468 base::Result<int> opened = open_driver(driver);
469
470 if (opened.ok()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Steven Moreland13a43c92021-08-30 13:21:48 -0700472 mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE,
473 opened.value(), 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474 if (mVMStart == MAP_FAILED) {
Steven Moreland13a43c92021-08-30 13:21:48 -0700475 close(opened.value());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 // *sigh*
Steven Moreland13a43c92021-08-30 13:21:48 -0700477 opened = base::Error()
478 << "Using " << driver << " failed: unable to mmap transaction memory.";
Iliyan Malchev32062242017-04-10 14:06:11 -0700479 mDriverName.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 }
Jeff Browne16986c2011-07-08 18:52:57 -0700482
Steven Moreland24bc0d12019-10-11 12:29:20 -0700483#ifdef __ANDROID__
Steven Moreland13a43c92021-08-30 13:21:48 -0700484 LOG_ALWAYS_FATAL_IF(!opened.ok(), "Binder driver '%s' could not be opened. Terminating: %s",
485 driver, opened.error().message().c_str());
Steven Moreland24bc0d12019-10-11 12:29:20 -0700486#endif
Steven Moreland13a43c92021-08-30 13:21:48 -0700487
488 if (opened.ok()) {
489 mDriverFD = opened.value();
490 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491}
492
493ProcessState::~ProcessState()
494{
zhongjieff405782016-03-09 15:05:04 +0800495 if (mDriverFD >= 0) {
496 if (mVMStart != MAP_FAILED) {
Steven Moreland072cc7e2019-07-12 21:01:54 +0000497 munmap(mVMStart, BINDER_VM_SIZE);
zhongjieff405782016-03-09 15:05:04 +0800498 }
499 close(mDriverFD);
500 }
501 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502}
Jooyung Han1b228f42020-01-30 13:41:12 +0900503
Steven Moreland61ff8492019-09-26 16:05:45 -0700504} // namespace android