blob: 82f6faf7a1bd774bb2185cfd9cf9d63b0af23d8d [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>
Steven Morelandc709dd82019-08-05 20:30:14 -070024#include <binder/Stability.h>
Steven Moreland2716e112018-02-23 14:57:20 -080025#include <cutils/atomic.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <utils/Log.h>
27#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <utils/threads.h>
29
Mathias Agopian208059f2009-05-18 15:08:03 -070030#include <private/binder/binder_module.h>
Steven Morelanda4853cd2019-07-12 15:44:37 -070031#include "Static.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include <errno.h>
34#include <fcntl.h>
Steven Moreland4fdb12f2020-07-21 02:21:48 +000035#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39#include <sys/ioctl.h>
40#include <sys/mman.h>
41#include <sys/stat.h>
Philip Cuadraa082fa82016-04-08 10:29:14 -070042#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Steven Moreland072cc7e2019-07-12 21:01:54 +000044#define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2)
Wale Ogunwale376b8222015-04-13 16:16:10 -070045#define DEFAULT_MAX_BINDER_THREADS 15
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
Martijn Coenen7bca77a2019-03-13 11:51:08 +000047#ifdef __ANDROID_VNDK__
48const char* kDefaultDriver = "/dev/vndbinder";
49#else
Steven Moreland2ae2f5e2018-07-06 13:02:53 -070050const char* kDefaultDriver = "/dev/binder";
Martijn Coenen7bca77a2019-03-13 11:51:08 +000051#endif
Steven Moreland2ae2f5e2018-07-06 13:02:53 -070052
Philip Cuadraa082fa82016-04-08 10:29:14 -070053// -------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55namespace android {
Wale Ogunwale376b8222015-04-13 16:16:10 -070056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057class PoolThread : public Thread
58{
59public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070060 explicit PoolThread(bool isMain)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 : mIsMain(isMain)
62 {
63 }
Jooyung Han1b228f42020-01-30 13:41:12 +090064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065protected:
66 virtual bool threadLoop()
67 {
68 IPCThreadState::self()->joinThreadPool(mIsMain);
69 return false;
70 }
Jooyung Han1b228f42020-01-30 13:41:12 +090071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 const bool mIsMain;
73};
74
75sp<ProcessState> ProcessState::self()
76{
Steven Moreland4fdb12f2020-07-21 02:21:48 +000077 return init(kDefaultDriver, false /*requireDefault*/);
Martijn Coenen55d871c2017-03-21 15:56:40 -070078}
79
80sp<ProcessState> ProcessState::initWithDriver(const char* driver)
81{
Steven Moreland4fdb12f2020-07-21 02:21:48 +000082 return init(driver, true /*requireDefault*/);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083}
84
Steven Moreland072cc7e2019-07-12 21:01:54 +000085sp<ProcessState> ProcessState::selfOrNull()
86{
Steven Moreland4fdb12f2020-07-21 02:21:48 +000087 return init(nullptr, false /*requireDefault*/);
88}
89
90sp<ProcessState> ProcessState::init(const char *driver, bool requireDefault)
91{
92 [[clang::no_destroy]] static sp<ProcessState> gProcess;
93 [[clang::no_destroy]] static std::mutex gProcessMutex;
94
95 if (driver == nullptr) {
96 std::lock_guard<std::mutex> l(gProcessMutex);
97 return gProcess;
98 }
99
100 [[clang::no_destroy]] static std::once_flag gProcessOnce;
101 std::call_once(gProcessOnce, [&](){
102 if (access(driver, R_OK) == -1) {
103 ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver);
104 driver = "/dev/binder";
105 }
106
107 std::lock_guard<std::mutex> l(gProcessMutex);
108 gProcess = new ProcessState(driver);
109 });
110
111 if (requireDefault) {
112 // Detect if we are trying to initialize with a different driver, and
113 // consider that an error. ProcessState will only be initialized once above.
114 LOG_ALWAYS_FATAL_IF(gProcess->getDriverName() != driver,
115 "ProcessState was already initialized with %s,"
116 " can't initialize with %s.",
117 gProcess->getDriverName().c_str(), driver);
118 }
119
Colin Cross9d45ccc2017-06-20 17:48:33 -0700120 return gProcess;
121}
122
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800123sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124{
Steven Morelandc709dd82019-08-05 20:30:14 -0700125 sp<IBinder> context = getStrongProxyForHandle(0);
126
Steven Moreland4da8fb02020-12-29 22:51:32 +0000127 if (context) {
128 // The root object is special since we get it directly from the driver, it is never
129 // written by Parcell::writeStrongBinder.
130 internal::Stability::markCompilationUnit(context.get());
131 } else {
132 ALOGW("Not able to get context object on %s.", mDriverName.c_str());
Steven Moreland8d93a712020-02-19 15:16:15 -0800133 }
134
Steven Morelandc709dd82019-08-05 20:30:14 -0700135 return context;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136}
137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138void ProcessState::startThreadPool()
139{
140 AutoMutex _l(mLock);
141 if (!mThreadPoolStarted) {
142 mThreadPoolStarted = true;
143 spawnPooledThread(true);
144 }
145}
146
Steven Moreland61096622020-08-31 23:36:39 +0000147bool ProcessState::becomeContextManager()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148{
Steven Moreland4411d712019-07-12 14:02:53 -0700149 AutoMutex _l(mLock);
Jeff Browne16986c2011-07-08 18:52:57 -0700150
Steven Moreland4411d712019-07-12 14:02:53 -0700151 flat_binder_object obj {
152 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
153 };
Steven Moreland3085a472018-12-26 13:59:23 -0800154
Steven Moreland4411d712019-07-12 14:02:53 -0700155 int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
Steven Moreland3085a472018-12-26 13:59:23 -0800156
Steven Moreland4411d712019-07-12 14:02:53 -0700157 // fallback to original method
158 if (result != 0) {
159 android_errorWriteLog(0x534e4554, "121035042");
Steven Moreland3085a472018-12-26 13:59:23 -0800160
Hungming Chen28b42522020-08-28 17:29:55 +0800161 int unused = 0;
162 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 }
Steven Moreland4411d712019-07-12 14:02:53 -0700164
165 if (result == -1) {
Steven Moreland4411d712019-07-12 14:02:53 -0700166 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
167 }
168
169 return result == 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170}
171
Colin Cross9d45ccc2017-06-20 17:48:33 -0700172// Get references to userspace objects held by the kernel binder driver
173// Writes up to count elements into buf, and returns the total number
174// of references the kernel has, which may be larger than count.
175// buf may be NULL if count is 0. The pointers returned by this method
176// should only be used for debugging and not dereferenced, they may
177// already be invalid.
178ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf)
179{
Colin Cross9d45ccc2017-06-20 17:48:33 -0700180 binder_node_debug_info info = {};
181
Yi Kongfdd8da92018-06-07 17:52:27 -0700182 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Cross9d45ccc2017-06-20 17:48:33 -0700183 size_t count = 0;
184
185 do {
186 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
187 if (result < 0) {
188 return -1;
189 }
190 if (info.ptr != 0) {
191 if (buf && buf < end)
192 *buf++ = info.ptr;
193 count++;
194 if (buf && buf < end)
195 *buf++ = info.cookie;
196 count++;
197 }
198 } while (info.ptr != 0);
199
200 return count;
201}
202
Jon Spivack902e6fc2019-10-18 21:22:37 -0700203// Queries the driver for the current strong reference count of the node
204// that the handle points to. Can only be used by the servicemanager.
205//
206// Returns -1 in case of failure, otherwise the strong reference count.
Steven Morelande8393882020-12-18 02:27:20 +0000207ssize_t ProcessState::getStrongRefCountForNode(const sp<BpBinder>& binder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000208 if (binder->isRpcBinder()) return -1;
209
Jon Spivack902e6fc2019-10-18 21:22:37 -0700210 binder_node_info_for_ref info;
211 memset(&info, 0, sizeof(binder_node_info_for_ref));
212
Steven Moreland5553ac42020-11-11 02:14:45 +0000213 info.handle = binder->getPrivateAccessorForId().binderHandle();
Jon Spivack902e6fc2019-10-18 21:22:37 -0700214
215 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info);
216
217 if (result != OK) {
218 static bool logged = false;
219 if (!logged) {
220 ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF.");
221 logged = true;
222 }
223 return -1;
224 }
225
226 return info.strong_count;
227}
228
Steven Moreland7732a092019-01-02 17:54:16 -0800229void ProcessState::setCallRestriction(CallRestriction restriction) {
Steven Moreland28723ae2019-04-01 18:52:30 -0700230 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr,
231 "Call restrictions must be set before the threadpool is started.");
Steven Moreland7732a092019-01-02 17:54:16 -0800232
233 mCallRestriction = restriction;
234}
235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
237{
238 const size_t N=mHandleToObject.size();
239 if (N <= (size_t)handle) {
240 handle_entry e;
Yi Kongfdd8da92018-06-07 17:52:27 -0700241 e.binder = nullptr;
242 e.refs = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kongfdd8da92018-06-07 17:52:27 -0700244 if (err < NO_ERROR) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 }
246 return &mHandleToObject.editItemAt(handle);
247}
248
249sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
250{
251 sp<IBinder> result;
252
253 AutoMutex _l(mLock);
254
255 handle_entry* e = lookupHandleLocked(handle);
256
Yi Kongfdd8da92018-06-07 17:52:27 -0700257 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 // We need to create a new BpBinder if there isn't currently one, OR we
Steven Morelande171d622019-07-17 16:06:01 -0700259 // are unable to acquire a weak reference on this current one. The
260 // attemptIncWeak() is safe because we know the BpBinder destructor will always
261 // call expungeHandle(), which acquires the same lock we are holding now.
262 // We need to do this because there is a race condition between someone
263 // releasing a reference on this BpBinder, and a new reference on its handle
264 // arriving from the driver.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700266 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700267 if (handle == 0) {
268 // Special case for context manager...
269 // The context manager is the only object for which we create
270 // a BpBinder proxy without already holding a reference.
271 // Perform a dummy transaction to ensure the context manager
272 // is registered before we create the first local reference
273 // to it (which will occur when creating the BpBinder).
274 // If a local reference is created for the BpBinder when the
275 // context manager is not present, the driver will fail to
276 // provide a reference to the context manager, but the
277 // driver API does not return status.
278 //
279 // Note that this is not race-free if the context manager
280 // dies while this code runs.
281 //
282 // TODO: add a driver API to wait for context manager, or
283 // stop special casing handle 0 for context manager and add
284 // a driver API to get a handle to the context manager with
285 // proper reference counting.
286
Steven Moreland9514b202020-09-21 18:03:27 +0000287 IPCThreadState* ipc = IPCThreadState::self();
288
289 CallRestriction originalCallRestriction = ipc->getCallRestriction();
290 ipc->setCallRestriction(CallRestriction::NONE);
291
Todd Poynora7b0f042013-06-18 17:25:37 -0700292 Parcel data;
Steven Moreland9514b202020-09-21 18:03:27 +0000293 status_t status = ipc->transact(
Yi Kongfdd8da92018-06-07 17:52:27 -0700294 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
Steven Moreland9514b202020-09-21 18:03:27 +0000295
296 ipc->setCallRestriction(originalCallRestriction);
297
Todd Poynora7b0f042013-06-18 17:25:37 -0700298 if (status == DEAD_OBJECT)
Yi Kongfdd8da92018-06-07 17:52:27 -0700299 return nullptr;
Todd Poynora7b0f042013-06-18 17:25:37 -0700300 }
301
Michael Wachenschwanz2d349902017-08-15 00:57:14 -0700302 b = BpBinder::create(handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 e->binder = b;
304 if (b) e->refs = b->getWeakRefs();
305 result = b;
306 } else {
307 // This little bit of nastyness is to allow us to add a primary
308 // reference to the remote proxy when this team doesn't have one
309 // but another team is sending the handle to us.
310 result.force_set(b);
311 e->refs->decWeak(this);
312 }
313 }
314
315 return result;
316}
317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
319{
320 AutoMutex _l(mLock);
Jooyung Han1b228f42020-01-30 13:41:12 +0900321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 handle_entry* e = lookupHandleLocked(handle);
323
324 // This handle may have already been replaced with a new BpBinder
325 // (if someone failed the AttemptIncWeak() above); we don't want
326 // to overwrite it.
Yi Kongfdd8da92018-06-07 17:52:27 -0700327 if (e && e->binder == binder) e->binder = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328}
329
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800330String8 ProcessState::makeBinderThreadName() {
331 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700332 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800333 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700334 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800335 return name;
336}
337
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338void ProcessState::spawnPooledThread(bool isMain)
339{
340 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800341 String8 name = makeBinderThreadName();
342 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800344 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 }
346}
347
Mathias Agopian1b80f792012-04-17 16:11:08 -0700348status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
Steven Moreland1fdb98b2020-03-06 21:42:12 +0000349 LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads,
350 "Binder threadpool cannot be shrunk after starting");
Mathias Agopian1b80f792012-04-17 16:11:08 -0700351 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700352 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
353 mMaxThreads = maxThreads;
354 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700355 result = -errno;
356 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
357 }
358 return result;
359}
360
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800361void ProcessState::giveThreadPoolName() {
362 androidSetThreadName( makeBinderThreadName().string() );
363}
364
Iliyan Malchev32062242017-04-10 14:06:11 -0700365String8 ProcessState::getDriverName() {
366 return mDriverName;
367}
368
Martijn Coenen55d871c2017-03-21 15:56:40 -0700369static int open_driver(const char *driver)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370{
Martijn Coenen55d871c2017-03-21 15:56:40 -0700371 int fd = open(driver, O_RDWR | O_CLOEXEC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 if (fd >= 0) {
Jin Wei78181df2012-10-18 17:00:48 +0800373 int vers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000376 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 close(fd);
378 fd = -1;
379 }
380 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartman67ac00f2017-01-03 16:54:59 -0800381 ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d",
382 vers, BINDER_CURRENT_PROTOCOL_VERSION, result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 close(fd);
384 fd = -1;
385 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700386 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
388 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000389 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 } else {
Martijn Coenen55d871c2017-03-21 15:56:40 -0700392 ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 }
394 return fd;
395}
396
Steven Moreland072cc7e2019-07-12 21:01:54 +0000397ProcessState::ProcessState(const char *driver)
Iliyan Malchev32062242017-04-10 14:06:11 -0700398 : mDriverName(String8(driver))
399 , mDriverFD(open_driver(driver))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 , mVMStart(MAP_FAILED)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700401 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
402 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
403 , mExecutingThreadsCount(0)
Steven Morelandc648a762021-01-16 02:39:45 +0000404 , mWaitingForThreads(0)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700405 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Cross96e83222016-04-15 14:29:55 -0700406 , mStarvationStartTimeMs(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407 , mThreadPoolStarted(false)
408 , mThreadPoolSeq(1)
Steven Moreland7732a092019-01-02 17:54:16 -0800409 , mCallRestriction(CallRestriction::NONE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410{
Steven Morelandb6d0e762019-11-15 00:51:42 -0800411
Steven Morelande09b0822020-08-26 18:06:13 +0000412// TODO(b/166468760): enforce in build system
Jooyung Han1b228f42020-01-30 13:41:12 +0900413#if defined(__ANDROID_APEX__)
Steven Morelandb6d0e762019-11-15 00:51:42 -0800414 LOG_ALWAYS_FATAL("Cannot use libbinder in APEX (only system.img libbinder) since it is not stable.");
415#endif
416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 if (mDriverFD >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Steven Moreland072cc7e2019-07-12 21:01:54 +0000419 mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420 if (mVMStart == MAP_FAILED) {
421 // *sigh*
Steven Moreland376a5592017-12-19 15:42:16 -0800422 ALOGE("Using %s failed: unable to mmap transaction memory.\n", mDriverName.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 close(mDriverFD);
424 mDriverFD = -1;
Iliyan Malchev32062242017-04-10 14:06:11 -0700425 mDriverName.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 }
Jeff Browne16986c2011-07-08 18:52:57 -0700428
Steven Moreland24bc0d12019-10-11 12:29:20 -0700429#ifdef __ANDROID__
Steven Moreland8c78e2e2019-07-10 16:13:12 -0700430 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver '%s' could not be opened. Terminating.", driver);
Steven Moreland24bc0d12019-10-11 12:29:20 -0700431#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432}
433
434ProcessState::~ProcessState()
435{
zhongjieff405782016-03-09 15:05:04 +0800436 if (mDriverFD >= 0) {
437 if (mVMStart != MAP_FAILED) {
Steven Moreland072cc7e2019-07-12 21:01:54 +0000438 munmap(mVMStart, BINDER_VM_SIZE);
zhongjieff405782016-03-09 15:05:04 +0800439 }
440 close(mDriverFD);
441 }
442 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443}
Jooyung Han1b228f42020-01-30 13:41:12 +0900444
Steven Moreland61ff8492019-09-26 16:05:45 -0700445} // namespace android