blob: 1bc28f2542a53117be17ea206fa70cd480e375f5 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ProcessState"
18
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/ProcessState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/BpBinder.h>
22#include <binder/IPCThreadState.h>
Steven Moreland2716e112018-02-23 14:57:20 -080023#include <binder/IServiceManager.h>
24#include <cutils/atomic.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Log.h>
26#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/String8.h>
28#include <utils/threads.h>
29
Mathias Agopian208059f2009-05-18 15:08:03 -070030#include <private/binder/binder_module.h>
31#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include <errno.h>
34#include <fcntl.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/stat.h>
Philip Cuadraa082fa82016-04-08 10:29:14 -070041#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
Martijn Coenend26d3de2018-03-22 17:12:57 +010043#define DEFAULT_BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2)
Wale Ogunwale376b8222015-04-13 16:16:10 -070044#define DEFAULT_MAX_BINDER_THREADS 15
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Martijn Coenen7bca77a2019-03-13 11:51:08 +000046#ifdef __ANDROID_VNDK__
47const char* kDefaultDriver = "/dev/vndbinder";
48#else
Steven Moreland2ae2f5e2018-07-06 13:02:53 -070049const char* kDefaultDriver = "/dev/binder";
Martijn Coenen7bca77a2019-03-13 11:51:08 +000050#endif
Steven Moreland2ae2f5e2018-07-06 13:02:53 -070051
Philip Cuadraa082fa82016-04-08 10:29:14 -070052// -------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54namespace android {
Wale Ogunwale376b8222015-04-13 16:16:10 -070055
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056class PoolThread : public Thread
57{
58public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070059 explicit PoolThread(bool isMain)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 : mIsMain(isMain)
61 {
62 }
63
64protected:
65 virtual bool threadLoop()
66 {
67 IPCThreadState::self()->joinThreadPool(mIsMain);
68 return false;
69 }
70
71 const bool mIsMain;
72};
73
74sp<ProcessState> ProcessState::self()
75{
Mathias Agopiane8db8712012-04-16 19:30:56 -070076 Mutex::Autolock _l(gProcessMutex);
Yi Kongfdd8da92018-06-07 17:52:27 -070077 if (gProcess != nullptr) {
Mathias Agopiane8db8712012-04-16 19:30:56 -070078 return gProcess;
79 }
Martijn Coenend26d3de2018-03-22 17:12:57 +010080 gProcess = new ProcessState(kDefaultDriver, DEFAULT_BINDER_VM_SIZE);
81 return gProcess;
82}
83
84sp<ProcessState> ProcessState::selfOrNull()
85{
86 Mutex::Autolock _l(gProcessMutex);
Martijn Coenen55d871c2017-03-21 15:56:40 -070087 return gProcess;
88}
89
90sp<ProcessState> ProcessState::initWithDriver(const char* driver)
91{
92 Mutex::Autolock _l(gProcessMutex);
Yi Kongfdd8da92018-06-07 17:52:27 -070093 if (gProcess != nullptr) {
Iliyan Malcheved1ffe02017-04-14 00:34:57 -070094 // Allow for initWithDriver to be called repeatedly with the same
95 // driver.
96 if (!strcmp(gProcess->getDriverName().c_str(), driver)) {
97 return gProcess;
98 }
Martijn Coenen55d871c2017-03-21 15:56:40 -070099 LOG_ALWAYS_FATAL("ProcessState was already initialized.");
100 }
Steven Moreland376a5592017-12-19 15:42:16 -0800101
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
Martijn Coenend26d3de2018-03-22 17:12:57 +0100107 gProcess = new ProcessState(driver, DEFAULT_BINDER_VM_SIZE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 return gProcess;
109}
110
Martijn Coenend26d3de2018-03-22 17:12:57 +0100111sp<ProcessState> ProcessState::initWithMmapSize(size_t mmap_size) {
Colin Cross9d45ccc2017-06-20 17:48:33 -0700112 Mutex::Autolock _l(gProcessMutex);
Martijn Coenend26d3de2018-03-22 17:12:57 +0100113 if (gProcess != nullptr) {
114 LOG_ALWAYS_FATAL_IF(mmap_size != gProcess->getMmapSize(),
115 "ProcessState already initialized with a different mmap size.");
116 return gProcess;
117 }
118
119 gProcess = new ProcessState(kDefaultDriver, mmap_size);
Colin Cross9d45ccc2017-06-20 17:48:33 -0700120 return gProcess;
121}
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123void ProcessState::setContextObject(const sp<IBinder>& object)
124{
125 setContextObject(object, String16("default"));
126}
127
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800128sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129{
Jeff Browne16986c2011-07-08 18:52:57 -0700130 return getStrongProxyForHandle(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131}
132
133void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name)
134{
135 AutoMutex _l(mLock);
136 mContexts.add(name, object);
137}
138
139sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller)
140{
141 mLock.lock();
142 sp<IBinder> object(
Yi Kongfdd8da92018-06-07 17:52:27 -0700143 mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 mLock.unlock();
145
146 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
147
Yi Kongfdd8da92018-06-07 17:52:27 -0700148 if (object != nullptr) return object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149
150 // Don't attempt to retrieve contexts if we manage them
151 if (mManagesContexts) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000152 ALOGE("getContextObject(%s) failed, but we manage the contexts!\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 String8(name).string());
Yi Kongfdd8da92018-06-07 17:52:27 -0700154 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 }
156
157 IPCThreadState* ipc = IPCThreadState::self();
158 {
159 Parcel data, reply;
160 // no interface token on this magic transaction
161 data.writeString16(name);
162 data.writeStrongBinder(caller);
163 status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0);
164 if (result == NO_ERROR) {
165 object = reply.readStrongBinder();
166 }
167 }
168
169 ipc->flushCommands();
170
Yi Kongfdd8da92018-06-07 17:52:27 -0700171 if (object != nullptr) setContextObject(object, name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 return object;
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
184bool ProcessState::isContextManager(void) const
185{
186 return mManagesContexts;
187}
188
189bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
190{
191 if (!mManagesContexts) {
192 AutoMutex _l(mLock);
193 mBinderContextCheckFunc = checkFunc;
194 mBinderContextUserData = userData;
Jeff Browne16986c2011-07-08 18:52:57 -0700195
196 int dummy = 0;
Jeff Browne16986c2011-07-08 18:52:57 -0700197 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
Jeff Browne16986c2011-07-08 18:52:57 -0700198 if (result == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 mManagesContexts = true;
Jeff Browne16986c2011-07-08 18:52:57 -0700200 } else if (result == -1) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700201 mBinderContextCheckFunc = nullptr;
202 mBinderContextUserData = nullptr;
Steve Blocke6f43dd2012-01-06 19:20:56 +0000203 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 }
205 }
206 return mManagesContexts;
207}
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{
217 // TODO: remove these when they are defined by bionic's binder.h
218 struct binder_node_debug_info {
219 binder_uintptr_t ptr;
220 binder_uintptr_t cookie;
221 __u32 has_strong_ref;
222 __u32 has_weak_ref;
223 };
224#define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
225
226 binder_node_debug_info info = {};
227
Yi Kongfdd8da92018-06-07 17:52:27 -0700228 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Cross9d45ccc2017-06-20 17:48:33 -0700229 size_t count = 0;
230
231 do {
232 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
233 if (result < 0) {
234 return -1;
235 }
236 if (info.ptr != 0) {
237 if (buf && buf < end)
238 *buf++ = info.ptr;
239 count++;
240 if (buf && buf < end)
241 *buf++ = info.cookie;
242 count++;
243 }
244 } while (info.ptr != 0);
245
246 return count;
247}
248
Martijn Coenend26d3de2018-03-22 17:12:57 +0100249size_t ProcessState::getMmapSize() {
250 return mMmapSize;
251}
252
Steven Moreland7732a092019-01-02 17:54:16 -0800253void ProcessState::setCallRestriction(CallRestriction restriction) {
254 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull(), "Call restrictions must be set before the threadpool is started.");
255
256 mCallRestriction = restriction;
257}
258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
260{
261 const size_t N=mHandleToObject.size();
262 if (N <= (size_t)handle) {
263 handle_entry e;
Yi Kongfdd8da92018-06-07 17:52:27 -0700264 e.binder = nullptr;
265 e.refs = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kongfdd8da92018-06-07 17:52:27 -0700267 if (err < NO_ERROR) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 }
269 return &mHandleToObject.editItemAt(handle);
270}
271
272sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
273{
274 sp<IBinder> result;
275
276 AutoMutex _l(mLock);
277
278 handle_entry* e = lookupHandleLocked(handle);
279
Yi Kongfdd8da92018-06-07 17:52:27 -0700280 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281 // We need to create a new BpBinder if there isn't currently one, OR we
282 // are unable to acquire a weak reference on this current one. See comment
283 // in getWeakProxyForHandle() for more info about this.
284 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700285 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700286 if (handle == 0) {
287 // Special case for context manager...
288 // The context manager is the only object for which we create
289 // a BpBinder proxy without already holding a reference.
290 // Perform a dummy transaction to ensure the context manager
291 // is registered before we create the first local reference
292 // to it (which will occur when creating the BpBinder).
293 // If a local reference is created for the BpBinder when the
294 // context manager is not present, the driver will fail to
295 // provide a reference to the context manager, but the
296 // driver API does not return status.
297 //
298 // Note that this is not race-free if the context manager
299 // dies while this code runs.
300 //
301 // TODO: add a driver API to wait for context manager, or
302 // stop special casing handle 0 for context manager and add
303 // a driver API to get a handle to the context manager with
304 // proper reference counting.
305
306 Parcel data;
307 status_t status = IPCThreadState::self()->transact(
Yi Kongfdd8da92018-06-07 17:52:27 -0700308 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
Todd Poynora7b0f042013-06-18 17:25:37 -0700309 if (status == DEAD_OBJECT)
Yi Kongfdd8da92018-06-07 17:52:27 -0700310 return nullptr;
Todd Poynora7b0f042013-06-18 17:25:37 -0700311 }
312
Michael Wachenschwanz2d349902017-08-15 00:57:14 -0700313 b = BpBinder::create(handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 e->binder = b;
315 if (b) e->refs = b->getWeakRefs();
316 result = b;
317 } else {
318 // This little bit of nastyness is to allow us to add a primary
319 // reference to the remote proxy when this team doesn't have one
320 // but another team is sending the handle to us.
321 result.force_set(b);
322 e->refs->decWeak(this);
323 }
324 }
325
326 return result;
327}
328
329wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
330{
331 wp<IBinder> result;
332
333 AutoMutex _l(mLock);
334
335 handle_entry* e = lookupHandleLocked(handle);
336
Yi Kongfdd8da92018-06-07 17:52:27 -0700337 if (e != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 // We need to create a new BpBinder if there isn't currently one, OR we
339 // are unable to acquire a weak reference on this current one. The
340 // attemptIncWeak() is safe because we know the BpBinder destructor will always
341 // call expungeHandle(), which acquires the same lock we are holding now.
342 // We need to do this because there is a race condition between someone
343 // releasing a reference on this BpBinder, and a new reference on its handle
344 // arriving from the driver.
345 IBinder* b = e->binder;
Yi Kongfdd8da92018-06-07 17:52:27 -0700346 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Michael Wachenschwanz2d349902017-08-15 00:57:14 -0700347 b = BpBinder::create(handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 result = b;
349 e->binder = b;
350 if (b) e->refs = b->getWeakRefs();
351 } else {
352 result = b;
353 e->refs->decWeak(this);
354 }
355 }
356
357 return result;
358}
359
360void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
361{
362 AutoMutex _l(mLock);
363
364 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());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 sp<Thread> t = new PoolThread(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) {
391 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700392 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
393 mMaxThreads = maxThreads;
394 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700395 result = -errno;
396 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
397 }
398 return result;
399}
400
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800401void ProcessState::giveThreadPoolName() {
402 androidSetThreadName( makeBinderThreadName().string() );
403}
404
Iliyan Malchev32062242017-04-10 14:06:11 -0700405String8 ProcessState::getDriverName() {
406 return mDriverName;
407}
408
Martijn Coenen55d871c2017-03-21 15:56:40 -0700409static int open_driver(const char *driver)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410{
Martijn Coenen55d871c2017-03-21 15:56:40 -0700411 int fd = open(driver, O_RDWR | O_CLOEXEC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 if (fd >= 0) {
Jin Wei78181df2012-10-18 17:00:48 +0800413 int vers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000416 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 close(fd);
418 fd = -1;
419 }
420 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartman67ac00f2017-01-03 16:54:59 -0800421 ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d",
422 vers, BINDER_CURRENT_PROTOCOL_VERSION, result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 close(fd);
424 fd = -1;
425 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700426 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
428 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000429 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 } else {
Martijn Coenen55d871c2017-03-21 15:56:40 -0700432 ALOGW("Opening '%s' failed: %s\n", driver, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 }
434 return fd;
435}
436
Martijn Coenend26d3de2018-03-22 17:12:57 +0100437ProcessState::ProcessState(const char *driver, size_t mmap_size)
Iliyan Malchev32062242017-04-10 14:06:11 -0700438 : mDriverName(String8(driver))
439 , mDriverFD(open_driver(driver))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 , mVMStart(MAP_FAILED)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700441 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
442 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
443 , mExecutingThreadsCount(0)
444 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Cross96e83222016-04-15 14:29:55 -0700445 , mStarvationStartTimeMs(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446 , mManagesContexts(false)
Yi Kongfdd8da92018-06-07 17:52:27 -0700447 , mBinderContextCheckFunc(nullptr)
448 , mBinderContextUserData(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 , mThreadPoolStarted(false)
450 , mThreadPoolSeq(1)
Martijn Coenend26d3de2018-03-22 17:12:57 +0100451 , mMmapSize(mmap_size)
Steven Moreland7732a092019-01-02 17:54:16 -0800452 , mCallRestriction(CallRestriction::NONE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453{
454 if (mDriverFD >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Martijn Coenend26d3de2018-03-22 17:12:57 +0100456 mVMStart = mmap(nullptr, mMmapSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 if (mVMStart == MAP_FAILED) {
458 // *sigh*
Steven Moreland376a5592017-12-19 15:42:16 -0800459 ALOGE("Using %s failed: unable to mmap transaction memory.\n", mDriverName.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460 close(mDriverFD);
461 mDriverFD = -1;
Iliyan Malchev32062242017-04-10 14:06:11 -0700462 mDriverName.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464 }
Jeff Browne16986c2011-07-08 18:52:57 -0700465
466 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467}
468
469ProcessState::~ProcessState()
470{
zhongjieff405782016-03-09 15:05:04 +0800471 if (mDriverFD >= 0) {
472 if (mVMStart != MAP_FAILED) {
Martijn Coenend26d3de2018-03-22 17:12:57 +0100473 munmap(mVMStart, mMmapSize);
zhongjieff405782016-03-09 15:05:04 +0800474 }
475 close(mDriverFD);
476 }
477 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478}
479
480}; // namespace android