blob: 9abe4b50bc99eb4dd0009b2496b6acaa70dc7916 [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) {
Steven Morelandf5a5bb82021-12-16 00:04:29 +0000179 if (mMaxThreads == 0) {
180 ALOGW("Extra binder thread started, but 0 threads requested. Do not use "
181 "*startThreadPool when zero threads are requested.");
182 }
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 mThreadPoolStarted = true;
185 spawnPooledThread(true);
186 }
187}
188
Steven Moreland61096622020-08-31 23:36:39 +0000189bool ProcessState::becomeContextManager()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190{
Steven Moreland4411d712019-07-12 14:02:53 -0700191 AutoMutex _l(mLock);
Jeff Browne16986c2011-07-08 18:52:57 -0700192
Steven Moreland4411d712019-07-12 14:02:53 -0700193 flat_binder_object obj {
194 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
195 };
Steven Moreland3085a472018-12-26 13:59:23 -0800196
Steven Moreland4411d712019-07-12 14:02:53 -0700197 int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
Steven Moreland3085a472018-12-26 13:59:23 -0800198
Steven Moreland4411d712019-07-12 14:02:53 -0700199 // fallback to original method
200 if (result != 0) {
201 android_errorWriteLog(0x534e4554, "121035042");
Steven Moreland3085a472018-12-26 13:59:23 -0800202
Hungming Chen28b42522020-08-28 17:29:55 +0800203 int unused = 0;
204 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 }
Steven Moreland4411d712019-07-12 14:02:53 -0700206
207 if (result == -1) {
Steven Moreland4411d712019-07-12 14:02:53 -0700208 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
209 }
210
211 return result == 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
Colin Cross9d45ccc2017-06-20 17:48:33 -0700214// Get references to userspace objects held by the kernel binder driver
215// Writes up to count elements into buf, and returns the total number
216// of references the kernel has, which may be larger than count.
217// buf may be NULL if count is 0. The pointers returned by this method
218// should only be used for debugging and not dereferenced, they may
219// already be invalid.
220ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf)
221{
Colin Cross9d45ccc2017-06-20 17:48:33 -0700222 binder_node_debug_info info = {};
223
Yi Kongfdd8da92018-06-07 17:52:27 -0700224 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Cross9d45ccc2017-06-20 17:48:33 -0700225 size_t count = 0;
226
227 do {
228 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
229 if (result < 0) {
230 return -1;
231 }
232 if (info.ptr != 0) {
233 if (buf && buf < end)
234 *buf++ = info.ptr;
235 count++;
236 if (buf && buf < end)
237 *buf++ = info.cookie;
238 count++;
239 }
240 } while (info.ptr != 0);
241
242 return count;
243}
244
Jon Spivack902e6fc2019-10-18 21:22:37 -0700245// Queries the driver for the current strong reference count of the node
246// that the handle points to. Can only be used by the servicemanager.
247//
248// Returns -1 in case of failure, otherwise the strong reference count.
Steven Morelande8393882020-12-18 02:27:20 +0000249ssize_t ProcessState::getStrongRefCountForNode(const sp<BpBinder>& binder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000250 if (binder->isRpcBinder()) return -1;
251
Jon Spivack902e6fc2019-10-18 21:22:37 -0700252 binder_node_info_for_ref info;
253 memset(&info, 0, sizeof(binder_node_info_for_ref));
254
Steven Moreland99157622021-09-13 16:27:34 -0700255 info.handle = binder->getPrivateAccessor().binderHandle();
Jon Spivack902e6fc2019-10-18 21:22:37 -0700256
257 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info);
258
259 if (result != OK) {
260 static bool logged = false;
261 if (!logged) {
262 ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF.");
263 logged = true;
264 }
265 return -1;
266 }
267
268 return info.strong_count;
269}
270
Steven Moreland7732a092019-01-02 17:54:16 -0800271void ProcessState::setCallRestriction(CallRestriction restriction) {
Steven Moreland28723ae2019-04-01 18:52:30 -0700272 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr,
273 "Call restrictions must be set before the threadpool is started.");
Steven Moreland7732a092019-01-02 17:54:16 -0800274
275 mCallRestriction = restriction;
276}
277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
279{
280 const size_t N=mHandleToObject.size();
281 if (N <= (size_t)handle) {
282 handle_entry e;
Yi Kongfdd8da92018-06-07 17:52:27 -0700283 e.binder = nullptr;
284 e.refs = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kongfdd8da92018-06-07 17:52:27 -0700286 if (err < NO_ERROR) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 }
288 return &mHandleToObject.editItemAt(handle);
289}
290
291sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
292{
293 sp<IBinder> result;
294
295 AutoMutex _l(mLock);
296
297 handle_entry* e = lookupHandleLocked(handle);
298
Yi Kongfdd8da92018-06-07 17:52:27 -0700299 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 // We need to create a new BpBinder if there isn't currently one, OR we
Steven Morelande171d622019-07-17 16:06:01 -0700301 // are unable to acquire a weak reference on this current one. The
302 // attemptIncWeak() is safe because we know the BpBinder destructor will always
303 // call expungeHandle(), which acquires the same lock we are holding now.
304 // We need to do this because there is a race condition between someone
305 // releasing a reference on this BpBinder, and a new reference on its handle
306 // arriving from the driver.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700308 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700309 if (handle == 0) {
310 // Special case for context manager...
311 // The context manager is the only object for which we create
312 // a BpBinder proxy without already holding a reference.
313 // Perform a dummy transaction to ensure the context manager
314 // is registered before we create the first local reference
315 // to it (which will occur when creating the BpBinder).
316 // If a local reference is created for the BpBinder when the
317 // context manager is not present, the driver will fail to
318 // provide a reference to the context manager, but the
319 // driver API does not return status.
320 //
321 // Note that this is not race-free if the context manager
322 // dies while this code runs.
323 //
324 // TODO: add a driver API to wait for context manager, or
325 // stop special casing handle 0 for context manager and add
326 // a driver API to get a handle to the context manager with
327 // proper reference counting.
328
Steven Moreland9514b202020-09-21 18:03:27 +0000329 IPCThreadState* ipc = IPCThreadState::self();
330
331 CallRestriction originalCallRestriction = ipc->getCallRestriction();
332 ipc->setCallRestriction(CallRestriction::NONE);
333
Todd Poynora7b0f042013-06-18 17:25:37 -0700334 Parcel data;
Steven Moreland9514b202020-09-21 18:03:27 +0000335 status_t status = ipc->transact(
Yi Kongfdd8da92018-06-07 17:52:27 -0700336 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
Steven Moreland9514b202020-09-21 18:03:27 +0000337
338 ipc->setCallRestriction(originalCallRestriction);
339
Todd Poynora7b0f042013-06-18 17:25:37 -0700340 if (status == DEAD_OBJECT)
Yi Kongfdd8da92018-06-07 17:52:27 -0700341 return nullptr;
Todd Poynora7b0f042013-06-18 17:25:37 -0700342 }
343
Steven Moreland99157622021-09-13 16:27:34 -0700344 sp<BpBinder> b = BpBinder::PrivateAccessor::create(handle);
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000345 e->binder = b.get();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 if (b) e->refs = b->getWeakRefs();
347 result = b;
348 } else {
349 // This little bit of nastyness is to allow us to add a primary
350 // reference to the remote proxy when this team doesn't have one
351 // but another team is sending the handle to us.
352 result.force_set(b);
353 e->refs->decWeak(this);
354 }
355 }
356
357 return result;
358}
359
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
361{
362 AutoMutex _l(mLock);
Jooyung Han1b228f42020-01-30 13:41:12 +0900363
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 handle_entry* e = lookupHandleLocked(handle);
365
366 // This handle may have already been replaced with a new BpBinder
367 // (if someone failed the AttemptIncWeak() above); we don't want
368 // to overwrite it.
Yi Kongfdd8da92018-06-07 17:52:27 -0700369 if (e && e->binder == binder) e->binder = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370}
371
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800372String8 ProcessState::makeBinderThreadName() {
373 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700374 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800375 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700376 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800377 return name;
378}
379
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380void ProcessState::spawnPooledThread(bool isMain)
381{
382 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800383 String8 name = makeBinderThreadName();
384 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000385 sp<Thread> t = sp<PoolThread>::make(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800386 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 }
388}
389
Mathias Agopian1b80f792012-04-17 16:11:08 -0700390status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
Steven Moreland1fdb98b2020-03-06 21:42:12 +0000391 LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads,
392 "Binder threadpool cannot be shrunk after starting");
Mathias Agopian1b80f792012-04-17 16:11:08 -0700393 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700394 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
395 mMaxThreads = maxThreads;
396 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700397 result = -errno;
398 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
399 }
400 return result;
401}
402
Yifan Hong84bedeb2021-04-21 21:37:17 -0700403size_t ProcessState::getThreadPoolMaxThreadCount() const {
404 // may actually be one more than this, if join is called
405 if (mThreadPoolStarted) return mMaxThreads;
406 // must not be initialized or maybe has poll thread setup, we
407 // currently don't track this in libbinder
408 return 0;
409}
410
Hang Lub185ac02021-03-24 13:17:22 +0800411status_t ProcessState::enableOnewaySpamDetection(bool enable) {
412 uint32_t enableDetection = enable ? 1 : 0;
413 if (ioctl(mDriverFD, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enableDetection) == -1) {
Martijn Coenende0a39e2021-04-22 14:38:46 +0200414 ALOGI("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
Hang Lub185ac02021-03-24 13:17:22 +0800415 return -errno;
416 }
417 return NO_ERROR;
418}
419
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800420void ProcessState::giveThreadPoolName() {
421 androidSetThreadName( makeBinderThreadName().string() );
422}
423
Iliyan Malchev32062242017-04-10 14:06:11 -0700424String8 ProcessState::getDriverName() {
425 return mDriverName;
426}
427
Steven Moreland13a43c92021-08-30 13:21:48 -0700428static base::Result<int> open_driver(const char* driver) {
Martijn Coenen55d871c2017-03-21 15:56:40 -0700429 int fd = open(driver, O_RDWR | O_CLOEXEC);
Steven Moreland13a43c92021-08-30 13:21:48 -0700430 if (fd < 0) {
431 return base::ErrnoError() << "Opening '" << driver << "' failed";
432 }
433 int vers = 0;
434 status_t result = ioctl(fd, BINDER_VERSION, &vers);
435 if (result == -1) {
436 close(fd);
437 return base::ErrnoError() << "Binder ioctl to obtain version failed";
438 }
439 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
440 close(fd);
441 return base::Error() << "Binder driver protocol(" << vers
442 << ") does not match user space protocol("
443 << BINDER_CURRENT_PROTOCOL_VERSION
444 << ")! ioctl() return value: " << result;
445 }
446 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
447 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
448 if (result == -1) {
449 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
450 }
451 uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION;
452 result = ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable);
453 if (result == -1) {
454 ALOGV("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 }
456 return fd;
457}
458
Steven Moreland13a43c92021-08-30 13:21:48 -0700459ProcessState::ProcessState(const char* driver)
460 : mDriverName(String8(driver)),
461 mDriverFD(-1),
462 mVMStart(MAP_FAILED),
463 mThreadCountLock(PTHREAD_MUTEX_INITIALIZER),
464 mThreadCountDecrement(PTHREAD_COND_INITIALIZER),
465 mExecutingThreadsCount(0),
466 mWaitingForThreads(0),
467 mMaxThreads(DEFAULT_MAX_BINDER_THREADS),
468 mStarvationStartTimeMs(0),
Steven Morelandee9df902021-10-14 14:00:08 -0700469 mForked(false),
Steven Moreland13a43c92021-08-30 13:21:48 -0700470 mThreadPoolStarted(false),
471 mThreadPoolSeq(1),
472 mCallRestriction(CallRestriction::NONE) {
473 base::Result<int> opened = open_driver(driver);
474
475 if (opened.ok()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Steven Moreland13a43c92021-08-30 13:21:48 -0700477 mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE,
478 opened.value(), 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 if (mVMStart == MAP_FAILED) {
Steven Moreland13a43c92021-08-30 13:21:48 -0700480 close(opened.value());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 // *sigh*
Steven Moreland13a43c92021-08-30 13:21:48 -0700482 opened = base::Error()
483 << "Using " << driver << " failed: unable to mmap transaction memory.";
Iliyan Malchev32062242017-04-10 14:06:11 -0700484 mDriverName.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 }
Jeff Browne16986c2011-07-08 18:52:57 -0700487
Steven Moreland24bc0d12019-10-11 12:29:20 -0700488#ifdef __ANDROID__
Steven Moreland13a43c92021-08-30 13:21:48 -0700489 LOG_ALWAYS_FATAL_IF(!opened.ok(), "Binder driver '%s' could not be opened. Terminating: %s",
490 driver, opened.error().message().c_str());
Steven Moreland24bc0d12019-10-11 12:29:20 -0700491#endif
Steven Moreland13a43c92021-08-30 13:21:48 -0700492
493 if (opened.ok()) {
494 mDriverFD = opened.value();
495 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496}
497
498ProcessState::~ProcessState()
499{
zhongjieff405782016-03-09 15:05:04 +0800500 if (mDriverFD >= 0) {
501 if (mVMStart != MAP_FAILED) {
Steven Moreland072cc7e2019-07-12 21:01:54 +0000502 munmap(mVMStart, BINDER_VM_SIZE);
zhongjieff405782016-03-09 15:05:04 +0800503 }
504 close(mDriverFD);
505 }
506 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507}
Jooyung Han1b228f42020-01-30 13:41:12 +0900508
Steven Moreland61ff8492019-09-26 16:05:45 -0700509} // namespace android