blob: 15b8604101b2edb7e6c0612a1955bd0edc6ca88a [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 Morelandaf1fa6c2021-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
92sp<ProcessState> ProcessState::init(const char *driver, bool requireDefault)
93{
94 [[clang::no_destroy]] static sp<ProcessState> gProcess;
95 [[clang::no_destroy]] static std::mutex gProcessMutex;
96
97 if (driver == nullptr) {
98 std::lock_guard<std::mutex> l(gProcessMutex);
99 return gProcess;
100 }
101
102 [[clang::no_destroy]] static std::once_flag gProcessOnce;
103 std::call_once(gProcessOnce, [&](){
104 if (access(driver, R_OK) == -1) {
105 ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver);
106 driver = "/dev/binder";
107 }
108
109 std::lock_guard<std::mutex> l(gProcessMutex);
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000110 gProcess = sp<ProcessState>::make(driver);
Steven Moreland4fdb12f2020-07-21 02:21:48 +0000111 });
112
113 if (requireDefault) {
114 // Detect if we are trying to initialize with a different driver, and
115 // consider that an error. ProcessState will only be initialized once above.
116 LOG_ALWAYS_FATAL_IF(gProcess->getDriverName() != driver,
117 "ProcessState was already initialized with %s,"
118 " can't initialize with %s.",
119 gProcess->getDriverName().c_str(), driver);
120 }
121
Colin Cross9d45ccc2017-06-20 17:48:33 -0700122 return gProcess;
123}
124
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800125sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126{
Steven Morelandc709dd82019-08-05 20:30:14 -0700127 sp<IBinder> context = getStrongProxyForHandle(0);
128
Steven Moreland4da8fb02020-12-29 22:51:32 +0000129 if (context) {
130 // The root object is special since we get it directly from the driver, it is never
131 // written by Parcell::writeStrongBinder.
132 internal::Stability::markCompilationUnit(context.get());
133 } else {
134 ALOGW("Not able to get context object on %s.", mDriverName.c_str());
Steven Moreland8d93a712020-02-19 15:16:15 -0800135 }
136
Steven Morelandc709dd82019-08-05 20:30:14 -0700137 return context;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140void ProcessState::startThreadPool()
141{
142 AutoMutex _l(mLock);
143 if (!mThreadPoolStarted) {
144 mThreadPoolStarted = true;
145 spawnPooledThread(true);
146 }
147}
148
Steven Moreland61096622020-08-31 23:36:39 +0000149bool ProcessState::becomeContextManager()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150{
Steven Moreland4411d712019-07-12 14:02:53 -0700151 AutoMutex _l(mLock);
Jeff Browne16986c2011-07-08 18:52:57 -0700152
Steven Moreland4411d712019-07-12 14:02:53 -0700153 flat_binder_object obj {
154 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
155 };
Steven Moreland3085a472018-12-26 13:59:23 -0800156
Steven Moreland4411d712019-07-12 14:02:53 -0700157 int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
Steven Moreland3085a472018-12-26 13:59:23 -0800158
Steven Moreland4411d712019-07-12 14:02:53 -0700159 // fallback to original method
160 if (result != 0) {
161 android_errorWriteLog(0x534e4554, "121035042");
Steven Moreland3085a472018-12-26 13:59:23 -0800162
Hungming Chen28b42522020-08-28 17:29:55 +0800163 int unused = 0;
164 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 }
Steven Moreland4411d712019-07-12 14:02:53 -0700166
167 if (result == -1) {
Steven Moreland4411d712019-07-12 14:02:53 -0700168 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
169 }
170
171 return result == 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172}
173
Colin Cross9d45ccc2017-06-20 17:48:33 -0700174// Get references to userspace objects held by the kernel binder driver
175// Writes up to count elements into buf, and returns the total number
176// of references the kernel has, which may be larger than count.
177// buf may be NULL if count is 0. The pointers returned by this method
178// should only be used for debugging and not dereferenced, they may
179// already be invalid.
180ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf)
181{
Colin Cross9d45ccc2017-06-20 17:48:33 -0700182 binder_node_debug_info info = {};
183
Yi Kongfdd8da92018-06-07 17:52:27 -0700184 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Cross9d45ccc2017-06-20 17:48:33 -0700185 size_t count = 0;
186
187 do {
188 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
189 if (result < 0) {
190 return -1;
191 }
192 if (info.ptr != 0) {
193 if (buf && buf < end)
194 *buf++ = info.ptr;
195 count++;
196 if (buf && buf < end)
197 *buf++ = info.cookie;
198 count++;
199 }
200 } while (info.ptr != 0);
201
202 return count;
203}
204
Jon Spivack902e6fc2019-10-18 21:22:37 -0700205// Queries the driver for the current strong reference count of the node
206// that the handle points to. Can only be used by the servicemanager.
207//
208// Returns -1 in case of failure, otherwise the strong reference count.
Steven Morelande8393882020-12-18 02:27:20 +0000209ssize_t ProcessState::getStrongRefCountForNode(const sp<BpBinder>& binder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000210 if (binder->isRpcBinder()) return -1;
211
Jon Spivack902e6fc2019-10-18 21:22:37 -0700212 binder_node_info_for_ref info;
213 memset(&info, 0, sizeof(binder_node_info_for_ref));
214
Steven Moreland5553ac42020-11-11 02:14:45 +0000215 info.handle = binder->getPrivateAccessorForId().binderHandle();
Jon Spivack902e6fc2019-10-18 21:22:37 -0700216
217 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info);
218
219 if (result != OK) {
220 static bool logged = false;
221 if (!logged) {
222 ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF.");
223 logged = true;
224 }
225 return -1;
226 }
227
228 return info.strong_count;
229}
230
Steven Moreland7732a092019-01-02 17:54:16 -0800231void ProcessState::setCallRestriction(CallRestriction restriction) {
Steven Moreland28723ae2019-04-01 18:52:30 -0700232 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr,
233 "Call restrictions must be set before the threadpool is started.");
Steven Moreland7732a092019-01-02 17:54:16 -0800234
235 mCallRestriction = restriction;
236}
237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
239{
240 const size_t N=mHandleToObject.size();
241 if (N <= (size_t)handle) {
242 handle_entry e;
Yi Kongfdd8da92018-06-07 17:52:27 -0700243 e.binder = nullptr;
244 e.refs = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kongfdd8da92018-06-07 17:52:27 -0700246 if (err < NO_ERROR) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 }
248 return &mHandleToObject.editItemAt(handle);
249}
250
251sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
252{
253 sp<IBinder> result;
254
255 AutoMutex _l(mLock);
256
257 handle_entry* e = lookupHandleLocked(handle);
258
Yi Kongfdd8da92018-06-07 17:52:27 -0700259 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 // We need to create a new BpBinder if there isn't currently one, OR we
Steven Morelande171d622019-07-17 16:06:01 -0700261 // are unable to acquire a weak reference on this current one. The
262 // attemptIncWeak() is safe because we know the BpBinder destructor will always
263 // call expungeHandle(), which acquires the same lock we are holding now.
264 // We need to do this because there is a race condition between someone
265 // releasing a reference on this BpBinder, and a new reference on its handle
266 // arriving from the driver.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700268 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700269 if (handle == 0) {
270 // Special case for context manager...
271 // The context manager is the only object for which we create
272 // a BpBinder proxy without already holding a reference.
273 // Perform a dummy transaction to ensure the context manager
274 // is registered before we create the first local reference
275 // to it (which will occur when creating the BpBinder).
276 // If a local reference is created for the BpBinder when the
277 // context manager is not present, the driver will fail to
278 // provide a reference to the context manager, but the
279 // driver API does not return status.
280 //
281 // Note that this is not race-free if the context manager
282 // dies while this code runs.
283 //
284 // TODO: add a driver API to wait for context manager, or
285 // stop special casing handle 0 for context manager and add
286 // a driver API to get a handle to the context manager with
287 // proper reference counting.
288
Steven Moreland9514b202020-09-21 18:03:27 +0000289 IPCThreadState* ipc = IPCThreadState::self();
290
291 CallRestriction originalCallRestriction = ipc->getCallRestriction();
292 ipc->setCallRestriction(CallRestriction::NONE);
293
Todd Poynora7b0f042013-06-18 17:25:37 -0700294 Parcel data;
Steven Moreland9514b202020-09-21 18:03:27 +0000295 status_t status = ipc->transact(
Yi Kongfdd8da92018-06-07 17:52:27 -0700296 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
Steven Moreland9514b202020-09-21 18:03:27 +0000297
298 ipc->setCallRestriction(originalCallRestriction);
299
Todd Poynora7b0f042013-06-18 17:25:37 -0700300 if (status == DEAD_OBJECT)
Yi Kongfdd8da92018-06-07 17:52:27 -0700301 return nullptr;
Todd Poynora7b0f042013-06-18 17:25:37 -0700302 }
303
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000304 sp<BpBinder> b = BpBinder::create(handle);
305 e->binder = b.get();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 if (b) e->refs = b->getWeakRefs();
307 result = b;
308 } else {
309 // This little bit of nastyness is to allow us to add a primary
310 // reference to the remote proxy when this team doesn't have one
311 // but another team is sending the handle to us.
312 result.force_set(b);
313 e->refs->decWeak(this);
314 }
315 }
316
317 return result;
318}
319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
321{
322 AutoMutex _l(mLock);
Jooyung Han1b228f42020-01-30 13:41:12 +0900323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 handle_entry* e = lookupHandleLocked(handle);
325
326 // This handle may have already been replaced with a new BpBinder
327 // (if someone failed the AttemptIncWeak() above); we don't want
328 // to overwrite it.
Yi Kongfdd8da92018-06-07 17:52:27 -0700329 if (e && e->binder == binder) e->binder = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330}
331
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800332String8 ProcessState::makeBinderThreadName() {
333 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700334 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800335 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700336 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800337 return name;
338}
339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340void ProcessState::spawnPooledThread(bool isMain)
341{
342 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800343 String8 name = makeBinderThreadName();
344 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000345 sp<Thread> t = sp<PoolThread>::make(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800346 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347 }
348}
349
Mathias Agopian1b80f792012-04-17 16:11:08 -0700350status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
Steven Moreland1fdb98b2020-03-06 21:42:12 +0000351 LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads,
352 "Binder threadpool cannot be shrunk after starting");
Mathias Agopian1b80f792012-04-17 16:11:08 -0700353 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700354 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
355 mMaxThreads = maxThreads;
356 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700357 result = -errno;
358 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
359 }
360 return result;
361}
362
Yifan Hong84bedeb2021-04-21 21:37:17 -0700363size_t ProcessState::getThreadPoolMaxThreadCount() const {
364 // may actually be one more than this, if join is called
365 if (mThreadPoolStarted) return mMaxThreads;
366 // must not be initialized or maybe has poll thread setup, we
367 // currently don't track this in libbinder
368 return 0;
369}
370
Hang Lub185ac02021-03-24 13:17:22 +0800371status_t ProcessState::enableOnewaySpamDetection(bool enable) {
372 uint32_t enableDetection = enable ? 1 : 0;
373 if (ioctl(mDriverFD, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enableDetection) == -1) {
Martijn Coenende0a39e2021-04-22 14:38:46 +0200374 ALOGI("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
Hang Lub185ac02021-03-24 13:17:22 +0800375 return -errno;
376 }
377 return NO_ERROR;
378}
379
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800380void ProcessState::giveThreadPoolName() {
381 androidSetThreadName( makeBinderThreadName().string() );
382}
383
Iliyan Malchev32062242017-04-10 14:06:11 -0700384String8 ProcessState::getDriverName() {
385 return mDriverName;
386}
387
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700388static base::Result<int> open_driver(const char* driver) {
Martijn Coenen55d871c2017-03-21 15:56:40 -0700389 int fd = open(driver, O_RDWR | O_CLOEXEC);
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700390 if (fd < 0) {
391 return base::ErrnoError() << "Opening '" << driver << "' failed";
392 }
393 int vers = 0;
394 status_t result = ioctl(fd, BINDER_VERSION, &vers);
395 if (result == -1) {
396 close(fd);
397 return base::ErrnoError() << "Binder ioctl to obtain version failed";
398 }
399 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
400 close(fd);
401 return base::Error() << "Binder driver protocol(" << vers
402 << ") does not match user space protocol("
403 << BINDER_CURRENT_PROTOCOL_VERSION
404 << ")! ioctl() return value: " << result;
405 }
406 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
407 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
408 if (result == -1) {
409 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
410 }
411 uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION;
412 result = ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable);
413 if (result == -1) {
414 ALOGV("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 }
416 return fd;
417}
418
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700419ProcessState::ProcessState(const char* driver)
420 : mDriverName(String8(driver)),
421 mDriverFD(-1),
422 mVMStart(MAP_FAILED),
423 mThreadCountLock(PTHREAD_MUTEX_INITIALIZER),
424 mThreadCountDecrement(PTHREAD_COND_INITIALIZER),
425 mExecutingThreadsCount(0),
426 mWaitingForThreads(0),
427 mMaxThreads(DEFAULT_MAX_BINDER_THREADS),
428 mStarvationStartTimeMs(0),
429 mThreadPoolStarted(false),
430 mThreadPoolSeq(1),
431 mCallRestriction(CallRestriction::NONE) {
Steven Morelande09b0822020-08-26 18:06:13 +0000432// TODO(b/166468760): enforce in build system
Jooyung Han1b228f42020-01-30 13:41:12 +0900433#if defined(__ANDROID_APEX__)
Steven Morelandb6d0e762019-11-15 00:51:42 -0800434 LOG_ALWAYS_FATAL("Cannot use libbinder in APEX (only system.img libbinder) since it is not stable.");
435#endif
436
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700437 base::Result<int> opened = open_driver(driver);
438
439 if (opened.ok()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700441 mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE,
442 opened.value(), 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 if (mVMStart == MAP_FAILED) {
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700444 close(opened.value());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 // *sigh*
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700446 opened = base::Error()
447 << "Using " << driver << " failed: unable to mmap transaction memory.";
Iliyan Malchev32062242017-04-10 14:06:11 -0700448 mDriverName.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450 }
Jeff Browne16986c2011-07-08 18:52:57 -0700451
Steven Moreland24bc0d12019-10-11 12:29:20 -0700452#ifdef __ANDROID__
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700453 LOG_ALWAYS_FATAL_IF(!opened.ok(), "Binder driver '%s' could not be opened. Terminating: %s",
454 driver, opened.error().message().c_str());
Steven Moreland24bc0d12019-10-11 12:29:20 -0700455#endif
Steven Morelandaf1fa6c2021-08-30 13:21:48 -0700456
457 if (opened.ok()) {
458 mDriverFD = opened.value();
459 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460}
461
462ProcessState::~ProcessState()
463{
zhongjieff405782016-03-09 15:05:04 +0800464 if (mDriverFD >= 0) {
465 if (mVMStart != MAP_FAILED) {
Steven Moreland072cc7e2019-07-12 21:01:54 +0000466 munmap(mVMStart, BINDER_VM_SIZE);
zhongjieff405782016-03-09 15:05:04 +0800467 }
468 close(mDriverFD);
469 }
470 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471}
Jooyung Han1b228f42020-01-30 13:41:12 +0900472
Steven Moreland61ff8492019-09-26 16:05:45 -0700473} // namespace android