blob: c2284f8e587164617a08131c1e528c09352129c3 [file] [log] [blame]
Mathias Agopian7922fa22009-05-18 15:08:03 -07001/*
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
Steven Morelandc4dd2102017-02-23 13:57:21 -080017#define LOG_TAG "hw-ProcessState"
Mathias Agopian7922fa22009-05-18 15:08:03 -070018
Martijn Coenen4080edc2016-05-04 14:17:02 +020019#include <hwbinder/ProcessState.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070020
Steven Moreland5e677882018-02-23 14:59:21 -080021#include <cutils/atomic.h>
Yifan Hong1e118d22017-01-12 14:42:28 -080022#include <hwbinder/BpHwBinder.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020023#include <hwbinder/IPCThreadState.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070024#include <utils/Log.h>
25#include <utils/String8.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070026#include <utils/threads.h>
27
Steven Moreland48adadd2019-09-05 17:04:38 -070028#include "binder_kernel.h"
Martijn Coenene01f4f22016-05-12 12:33:28 +020029#include <hwbinder/Static.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070030
31#include <errno.h>
32#include <fcntl.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <sys/ioctl.h>
37#include <sys/mman.h>
38#include <sys/stat.h>
Philip Cuadra788e0052016-04-08 10:29:14 -070039#include <sys/types.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070040
Martijn Coenen32507d32018-03-22 17:12:57 +010041#define DEFAULT_BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2)
Martijn Coenenab00b6a2016-11-22 15:03:13 +010042#define DEFAULT_MAX_BINDER_THREADS 0
Hang Lud1388da2021-03-24 14:30:06 +080043#define DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION 1
Mathias Agopian7922fa22009-05-18 15:08:03 -070044
Philip Cuadra788e0052016-04-08 10:29:14 -070045// -------------------------------------------------------------------------
Mathias Agopian7922fa22009-05-18 15:08:03 -070046
47namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020048namespace hardware {
Wale Ogunwale2e604f02015-04-13 16:16:10 -070049
Mathias Agopian7922fa22009-05-18 15:08:03 -070050class PoolThread : public Thread
51{
52public:
Chih-Hung Hsieh1f555e92016-04-25 15:41:05 -070053 explicit PoolThread(bool isMain)
Mathias Agopian7922fa22009-05-18 15:08:03 -070054 : mIsMain(isMain)
55 {
56 }
Yifan Hong1e118d22017-01-12 14:42:28 -080057
Mathias Agopian7922fa22009-05-18 15:08:03 -070058protected:
59 virtual bool threadLoop()
60 {
61 IPCThreadState::self()->joinThreadPool(mIsMain);
62 return false;
63 }
Yifan Hong1e118d22017-01-12 14:42:28 -080064
Mathias Agopian7922fa22009-05-18 15:08:03 -070065 const bool mIsMain;
66};
67
68sp<ProcessState> ProcessState::self()
69{
Steven Morelandf050d282020-07-21 02:42:12 +000070 return init(DEFAULT_BINDER_VM_SIZE, false /*requireMmapSize*/);
Mathias Agopian7922fa22009-05-18 15:08:03 -070071}
72
Colin Crossdd6dafb2017-06-22 12:29:13 -070073sp<ProcessState> ProcessState::selfOrNull() {
Steven Morelandf050d282020-07-21 02:42:12 +000074 return init(0, false /*requireMmapSize*/);
Colin Crossdd6dafb2017-06-22 12:29:13 -070075}
76
Steven Morelandf050d282020-07-21 02:42:12 +000077sp<ProcessState> ProcessState::initWithMmapSize(size_t mmapSize) {
78 return init(mmapSize, true /*requireMmapSize*/);
79}
80
81sp<ProcessState> ProcessState::init(size_t mmapSize, bool requireMmapSize) {
82 [[clang::no_destroy]] static sp<ProcessState> gProcess;
83 [[clang::no_destroy]] static std::mutex gProcessMutex;
84
85 if (mmapSize == 0) {
86 std::lock_guard<std::mutex> l(gProcessMutex);
Martijn Coenen32507d32018-03-22 17:12:57 +010087 return gProcess;
88 }
89
Steven Morelandf050d282020-07-21 02:42:12 +000090 [[clang::no_destroy]] static std::once_flag gProcessOnce;
91 std::call_once(gProcessOnce, [&](){
92 std::lock_guard<std::mutex> l(gProcessMutex);
93 gProcess = new ProcessState(mmapSize);
94 });
95
96 if (requireMmapSize) {
97 LOG_ALWAYS_FATAL_IF(mmapSize != gProcess->getMmapSize(),
98 "ProcessState already initialized with a different mmap size.");
99 }
100
Martijn Coenen32507d32018-03-22 17:12:57 +0100101 return gProcess;
102}
103
Mathias Agopian7922fa22009-05-18 15:08:03 -0700104void ProcessState::startThreadPool()
105{
106 AutoMutex _l(mLock);
107 if (!mThreadPoolStarted) {
108 mThreadPoolStarted = true;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100109 if (mSpawnThreadOnStart) {
110 spawnPooledThread(true);
111 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700112 }
113}
114
Steven Morelande9b4f912020-11-05 00:25:37 +0000115sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700116{
Steven Morelande9b4f912020-11-05 00:25:37 +0000117 return getStrongProxyForHandle(0);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700118}
119
Steven Morelande9b4f912020-11-05 00:25:37 +0000120void ProcessState::becomeContextManager()
Mathias Agopian7922fa22009-05-18 15:08:03 -0700121{
Steven Morelande9b4f912020-11-05 00:25:37 +0000122 AutoMutex _l(mLock);
Jeff Brownc0f143d2011-07-08 18:52:57 -0700123
Steven Morelande9b4f912020-11-05 00:25:37 +0000124 flat_binder_object obj {
125 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
126 };
Steven Morelandc149dca2019-01-09 18:01:02 -0800127
Steven Morelande9b4f912020-11-05 00:25:37 +0000128 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
Steven Morelandc149dca2019-01-09 18:01:02 -0800129
Steven Morelande9b4f912020-11-05 00:25:37 +0000130 // fallback to original method
131 if (result != 0) {
132 android_errorWriteLog(0x534e4554, "121035042");
Steven Morelandc149dca2019-01-09 18:01:02 -0800133
Steven Morelande9b4f912020-11-05 00:25:37 +0000134 int unused = 0;
135 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700136 }
Steven Morelande9b4f912020-11-05 00:25:37 +0000137
138 if (result == -1) {
139 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
140 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700141}
142
Colin Crossdd6dafb2017-06-22 12:29:13 -0700143// Get references to userspace objects held by the kernel binder driver
144// Writes up to count elements into buf, and returns the total number
145// of references the kernel has, which may be larger than count.
146// buf may be NULL if count is 0. The pointers returned by this method
147// should only be used for debugging and not dereferenced, they may
148// already be invalid.
149ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) {
150 binder_node_debug_info info = {};
151
Yi Kong55d41072018-07-23 14:55:39 -0700152 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Crossdd6dafb2017-06-22 12:29:13 -0700153 size_t count = 0;
154
155 do {
156 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
157 if (result < 0) {
158 return -1;
159 }
160 if (info.ptr != 0) {
161 if (buf && buf < end) *buf++ = info.ptr;
162 count++;
163 if (buf && buf < end) *buf++ = info.cookie;
164 count++;
165 }
166 } while (info.ptr != 0);
167
168 return count;
169}
170
Martijn Coenen320e1d32018-09-07 10:41:33 +0200171// Queries the driver for the current strong reference count of the node
172// that the handle points to. Can only be used by the servicemanager.
173//
174// Returns -1 in case of failure, otherwise the strong reference count.
175ssize_t ProcessState::getStrongRefCountForNodeByHandle(int32_t handle) {
176 binder_node_info_for_ref info;
177 memset(&info, 0, sizeof(binder_node_info_for_ref));
178
179 info.handle = handle;
180
181 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info);
182
183 if (result != OK) {
Daniel Normanc0af11a2019-06-14 11:22:03 -0700184 static bool logged = false;
185 if (!logged) {
186 ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF.");
187 logged = true;
188 }
Martijn Coenen320e1d32018-09-07 10:41:33 +0200189 return -1;
190 }
191
192 return info.strong_count;
193}
194
Martijn Coenen32507d32018-03-22 17:12:57 +0100195size_t ProcessState::getMmapSize() {
196 return mMmapSize;
197}
198
Steven Moreland14603002019-01-02 17:54:16 -0800199void ProcessState::setCallRestriction(CallRestriction restriction) {
Steven Morelandd9bdb652019-09-17 15:42:45 -0700200 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr,
201 "Call restrictions must be set before the threadpool is started.");
Steven Moreland14603002019-01-02 17:54:16 -0800202
203 mCallRestriction = restriction;
204}
205
Mathias Agopian7922fa22009-05-18 15:08:03 -0700206ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
207{
208 const size_t N=mHandleToObject.size();
209 if (N <= (size_t)handle) {
210 handle_entry e;
Yi Kong55d41072018-07-23 14:55:39 -0700211 e.binder = nullptr;
212 e.refs = nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700213 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kong55d41072018-07-23 14:55:39 -0700214 if (err < NO_ERROR) return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700215 }
216 return &mHandleToObject.editItemAt(handle);
217}
218
219sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
220{
221 sp<IBinder> result;
222
223 AutoMutex _l(mLock);
224
225 handle_entry* e = lookupHandleLocked(handle);
226
Yi Kong55d41072018-07-23 14:55:39 -0700227 if (e != nullptr) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800228 // We need to create a new BpHwBinder if there isn't currently one, OR we
Mathias Agopian7922fa22009-05-18 15:08:03 -0700229 // are unable to acquire a weak reference on this current one. See comment
230 // in getWeakProxyForHandle() for more info about this.
231 IBinder* b = e->binder;
Yi Kong55d41072018-07-23 14:55:39 -0700232 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800233 b = new BpHwBinder(handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700234 e->binder = b;
235 if (b) e->refs = b->getWeakRefs();
236 result = b;
237 } else {
238 // This little bit of nastyness is to allow us to add a primary
239 // reference to the remote proxy when this team doesn't have one
240 // but another team is sending the handle to us.
241 result.force_set(b);
242 e->refs->decWeak(this);
243 }
244 }
245
246 return result;
247}
248
249wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
250{
251 wp<IBinder> result;
252
253 AutoMutex _l(mLock);
254
255 handle_entry* e = lookupHandleLocked(handle);
256
Yi Kong55d41072018-07-23 14:55:39 -0700257 if (e != nullptr) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800258 // We need to create a new BpHwBinder if there isn't currently one, OR we
Mathias Agopian7922fa22009-05-18 15:08:03 -0700259 // are unable to acquire a weak reference on this current one. The
Yifan Hong1e118d22017-01-12 14:42:28 -0800260 // attemptIncWeak() is safe because we know the BpHwBinder destructor will always
Mathias Agopian7922fa22009-05-18 15:08:03 -0700261 // 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
Yifan Hong1e118d22017-01-12 14:42:28 -0800263 // releasing a reference on this BpHwBinder, and a new reference on its handle
Mathias Agopian7922fa22009-05-18 15:08:03 -0700264 // arriving from the driver.
265 IBinder* b = e->binder;
Yi Kong55d41072018-07-23 14:55:39 -0700266 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800267 b = new BpHwBinder(handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700268 result = b;
269 e->binder = b;
270 if (b) e->refs = b->getWeakRefs();
271 } else {
272 result = b;
273 e->refs->decWeak(this);
274 }
275 }
276
277 return result;
278}
279
280void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
281{
282 AutoMutex _l(mLock);
Yifan Hong1e118d22017-01-12 14:42:28 -0800283
Mathias Agopian7922fa22009-05-18 15:08:03 -0700284 handle_entry* e = lookupHandleLocked(handle);
285
Yifan Hong1e118d22017-01-12 14:42:28 -0800286 // This handle may have already been replaced with a new BpHwBinder
Mathias Agopian7922fa22009-05-18 15:08:03 -0700287 // (if someone failed the AttemptIncWeak() above); we don't want
288 // to overwrite it.
Yi Kong55d41072018-07-23 14:55:39 -0700289 if (e && e->binder == binder) e->binder = nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700290}
291
Mathias Agopiand191ed72013-03-07 15:34:28 -0800292String8 ProcessState::makeBinderThreadName() {
293 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadra788e0052016-04-08 10:29:14 -0700294 pid_t pid = getpid();
Mathias Agopiand191ed72013-03-07 15:34:28 -0800295 String8 name;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100296 name.appendFormat("HwBinder:%d_%X", pid, s);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800297 return name;
298}
299
Mathias Agopian7922fa22009-05-18 15:08:03 -0700300void ProcessState::spawnPooledThread(bool isMain)
301{
302 if (mThreadPoolStarted) {
Mathias Agopiand191ed72013-03-07 15:34:28 -0800303 String8 name = makeBinderThreadName();
304 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700305 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800306 t->run(name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700307 }
308}
309
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100310status_t ProcessState::setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool) {
Steven Moreland8e047f72020-02-18 18:26:43 -0800311 LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads,
312 "Binder threadpool cannot be shrunk after starting");
313
Steven Moreland2aa37072018-10-25 16:32:17 -0700314 // if the caller joins the pool, then there will be one thread which is impossible.
315 LOG_ALWAYS_FATAL_IF(maxThreads == 0 && callerJoinsPool,
316 "Binder threadpool must have a minimum of one thread if caller joins pool.");
317
318 size_t threadsToAllocate = maxThreads;
319
320 // If the caller is going to join the pool it will contribute one thread to the threadpool.
321 // This is part of the API's contract.
322 if (callerJoinsPool) threadsToAllocate--;
323
324 // If we can, spawn one thread from userspace when the threadpool is started. This ensures
325 // that there is always a thread available to start more threads as soon as the threadpool
326 // is started.
327 bool spawnThreadOnStart = threadsToAllocate > 0;
328 if (spawnThreadOnStart) threadsToAllocate--;
329
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100330 // the BINDER_SET_MAX_THREADS ioctl really tells the kernel how many threads
331 // it's allowed to spawn, *in addition* to any threads we may have already
Steven Moreland2aa37072018-10-25 16:32:17 -0700332 // spawned locally.
333 size_t kernelMaxThreads = threadsToAllocate;
334
335 AutoMutex _l(mLock);
336 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &kernelMaxThreads) == -1) {
337 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
338 return -errno;
Mathias Agopian8297f502012-04-17 16:11:08 -0700339 }
Steven Moreland2aa37072018-10-25 16:32:17 -0700340
341 mMaxThreads = maxThreads;
342 mSpawnThreadOnStart = spawnThreadOnStart;
343
344 return NO_ERROR;
Mathias Agopian8297f502012-04-17 16:11:08 -0700345}
346
Hang Lud1388da2021-03-24 14:30:06 +0800347status_t ProcessState::enableOnewaySpamDetection(bool enable) {
348 uint32_t enableDetection = enable ? 1 : 0;
349 if (ioctl(mDriverFD, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enableDetection) == -1) {
Martijn Coenen4486bab2021-04-22 14:35:58 +0200350 ALOGI("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
Hang Lud1388da2021-03-24 14:30:06 +0800351 return -errno;
352 }
353 return NO_ERROR;
354}
355
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200356size_t ProcessState::getMaxThreads() {
357 return mMaxThreads;
358}
359
Mathias Agopiand191ed72013-03-07 15:34:28 -0800360void ProcessState::giveThreadPoolName() {
361 androidSetThreadName( makeBinderThreadName().string() );
362}
363
Mathias Agopian7922fa22009-05-18 15:08:03 -0700364static int open_driver()
365{
Martijn Coenena660cbc2016-05-12 11:29:23 +0200366 int fd = open("/dev/hwbinder", O_RDWR | O_CLOEXEC);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700367 if (fd >= 0) {
Jin Wei023b4682012-10-18 17:00:48 +0800368 int vers = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700369 status_t result = ioctl(fd, BINDER_VERSION, &vers);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700370 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000371 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700372 close(fd);
373 fd = -1;
374 }
375 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartmand5217742017-01-03 16:56:05 -0800376 ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)!", vers, BINDER_CURRENT_PROTOCOL_VERSION);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700377 close(fd);
378 fd = -1;
379 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700380 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700381 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
382 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000383 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700384 }
Hang Lud1388da2021-03-24 14:30:06 +0800385 uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION;
386 result = ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable);
387 if (result == -1) {
Steven Moreland0e4c9bc2021-08-12 16:27:40 -0700388 ALOGV("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
Hang Lud1388da2021-03-24 14:30:06 +0800389 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700390 } else {
Iliyan Malcheve3256a62016-09-19 16:00:32 -0700391 ALOGW("Opening '/dev/hwbinder' failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700392 }
393 return fd;
394}
395
Steven Morelandf050d282020-07-21 02:42:12 +0000396ProcessState::ProcessState(size_t mmapSize)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700397 : mDriverFD(open_driver())
398 , mVMStart(MAP_FAILED)
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700399 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700400 , mExecutingThreadsCount(0)
401 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Crossb1dc6542016-04-15 14:29:55 -0700402 , mStarvationStartTimeMs(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700403 , mThreadPoolStarted(false)
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100404 , mSpawnThreadOnStart(true)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700405 , mThreadPoolSeq(1)
Steven Morelandf050d282020-07-21 02:42:12 +0000406 , mMmapSize(mmapSize)
Steven Moreland14603002019-01-02 17:54:16 -0800407 , mCallRestriction(CallRestriction::NONE)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700408{
409 if (mDriverFD >= 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700410 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Yi Kong55d41072018-07-23 14:55:39 -0700411 mVMStart = mmap(nullptr, mMmapSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700412 if (mVMStart == MAP_FAILED) {
413 // *sigh*
Jiyong Park1268c282019-01-28 16:22:49 +0900414 ALOGE("Mmapping /dev/hwbinder failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700415 close(mDriverFD);
416 mDriverFD = -1;
417 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700418 }
Sergii Piatakovb2cded42019-11-08 17:59:31 +0200419
Steven Moreland065201c2019-12-11 11:16:08 -0800420#ifdef __ANDROID__
Sergii Piatakovb2cded42019-11-08 17:59:31 +0200421 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
Steven Moreland065201c2019-12-11 11:16:08 -0800422#endif
Mathias Agopian7922fa22009-05-18 15:08:03 -0700423}
424
425ProcessState::~ProcessState()
426{
zhongjie8e8a0252016-03-09 15:05:04 +0800427 if (mDriverFD >= 0) {
428 if (mVMStart != MAP_FAILED) {
Martijn Coenen32507d32018-03-22 17:12:57 +0100429 munmap(mVMStart, mMmapSize);
zhongjie8e8a0252016-03-09 15:05:04 +0800430 }
431 close(mDriverFD);
432 }
433 mDriverFD = -1;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700434}
Yifan Hong1e118d22017-01-12 14:42:28 -0800435
Steven Moreland7173a4c2019-09-26 15:55:02 -0700436} // namespace hardware
437} // namespace android