blob: 86e26c894126b556c7a8487c2888127e0b0ea761 [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
Mathias Agopian7922fa22009-05-18 15:08:03 -070043
Philip Cuadra788e0052016-04-08 10:29:14 -070044// -------------------------------------------------------------------------
Mathias Agopian7922fa22009-05-18 15:08:03 -070045
46namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020047namespace hardware {
Wale Ogunwale2e604f02015-04-13 16:16:10 -070048
Mathias Agopian7922fa22009-05-18 15:08:03 -070049class PoolThread : public Thread
50{
51public:
Chih-Hung Hsieh1f555e92016-04-25 15:41:05 -070052 explicit PoolThread(bool isMain)
Mathias Agopian7922fa22009-05-18 15:08:03 -070053 : mIsMain(isMain)
54 {
55 }
Yifan Hong1e118d22017-01-12 14:42:28 -080056
Mathias Agopian7922fa22009-05-18 15:08:03 -070057protected:
58 virtual bool threadLoop()
59 {
60 IPCThreadState::self()->joinThreadPool(mIsMain);
61 return false;
62 }
Yifan Hong1e118d22017-01-12 14:42:28 -080063
Mathias Agopian7922fa22009-05-18 15:08:03 -070064 const bool mIsMain;
65};
66
67sp<ProcessState> ProcessState::self()
68{
Mathias Agopianb9a7d6a2012-04-16 19:30:56 -070069 Mutex::Autolock _l(gProcessMutex);
Yi Kong55d41072018-07-23 14:55:39 -070070 if (gProcess != nullptr) {
Mathias Agopianb9a7d6a2012-04-16 19:30:56 -070071 return gProcess;
72 }
Martijn Coenen32507d32018-03-22 17:12:57 +010073 gProcess = new ProcessState(DEFAULT_BINDER_VM_SIZE);
Mathias Agopian7922fa22009-05-18 15:08:03 -070074 return gProcess;
75}
76
Colin Crossdd6dafb2017-06-22 12:29:13 -070077sp<ProcessState> ProcessState::selfOrNull() {
78 Mutex::Autolock _l(gProcessMutex);
79 return gProcess;
80}
81
Martijn Coenen32507d32018-03-22 17:12:57 +010082sp<ProcessState> ProcessState::initWithMmapSize(size_t mmap_size) {
83 Mutex::Autolock _l(gProcessMutex);
Yi Kong55d41072018-07-23 14:55:39 -070084 if (gProcess != nullptr) {
Martijn Coenen32507d32018-03-22 17:12:57 +010085 LOG_ALWAYS_FATAL_IF(mmap_size != gProcess->getMmapSize(),
86 "ProcessState already initialized with a different mmap size.");
87 return gProcess;
88 }
89
90 gProcess = new ProcessState(mmap_size);
91 return gProcess;
92}
93
Mathias Agopian7922fa22009-05-18 15:08:03 -070094void ProcessState::setContextObject(const sp<IBinder>& object)
95{
96 setContextObject(object, String16("default"));
97}
98
Colin Crossf0487982014-02-05 17:42:44 -080099sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700100{
Jeff Brownc0f143d2011-07-08 18:52:57 -0700101 return getStrongProxyForHandle(0);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700102}
103
104void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name)
105{
106 AutoMutex _l(mLock);
107 mContexts.add(name, object);
108}
109
110sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller)
111{
112 mLock.lock();
113 sp<IBinder> object(
Yi Kong55d41072018-07-23 14:55:39 -0700114 mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : nullptr);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700115 mLock.unlock();
Yifan Hong1e118d22017-01-12 14:42:28 -0800116
Mathias Agopian7922fa22009-05-18 15:08:03 -0700117 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
Yifan Hong1e118d22017-01-12 14:42:28 -0800118
Yi Kong55d41072018-07-23 14:55:39 -0700119 if (object != nullptr) return object;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700120
121 // Don't attempt to retrieve contexts if we manage them
122 if (mManagesContexts) {
Steve Blockeafc6212012-01-06 19:20:56 +0000123 ALOGE("getContextObject(%s) failed, but we manage the contexts!\n",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700124 String8(name).string());
Yi Kong55d41072018-07-23 14:55:39 -0700125 return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700126 }
Yifan Hong1e118d22017-01-12 14:42:28 -0800127
Mathias Agopian7922fa22009-05-18 15:08:03 -0700128 IPCThreadState* ipc = IPCThreadState::self();
129 {
130 Parcel data, reply;
131 // no interface token on this magic transaction
132 data.writeString16(name);
133 data.writeStrongBinder(caller);
134 status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0);
135 if (result == NO_ERROR) {
136 object = reply.readStrongBinder();
137 }
138 }
Yifan Hong1e118d22017-01-12 14:42:28 -0800139
Mathias Agopian7922fa22009-05-18 15:08:03 -0700140 ipc->flushCommands();
Yifan Hong1e118d22017-01-12 14:42:28 -0800141
Yi Kong55d41072018-07-23 14:55:39 -0700142 if (object != nullptr) setContextObject(object, name);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700143 return object;
144}
145
Mathias Agopian7922fa22009-05-18 15:08:03 -0700146void ProcessState::startThreadPool()
147{
148 AutoMutex _l(mLock);
149 if (!mThreadPoolStarted) {
150 mThreadPoolStarted = true;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100151 if (mSpawnThreadOnStart) {
152 spawnPooledThread(true);
153 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700154 }
155}
156
157bool ProcessState::isContextManager(void) const
158{
159 return mManagesContexts;
160}
161
162bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
163{
164 if (!mManagesContexts) {
165 AutoMutex _l(mLock);
166 mBinderContextCheckFunc = checkFunc;
167 mBinderContextUserData = userData;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700168
Steven Morelandc149dca2019-01-09 18:01:02 -0800169 flat_binder_object obj {
170 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
171 };
172
173 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
174
175 // fallback to original method
176 if (result != 0) {
177 android_errorWriteLog(0x534e4554, "121035042");
178
179 int dummy = 0;
180 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
181 }
182
Jeff Brownc0f143d2011-07-08 18:52:57 -0700183 if (result == 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700184 mManagesContexts = true;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700185 } else if (result == -1) {
Yi Kong55d41072018-07-23 14:55:39 -0700186 mBinderContextCheckFunc = nullptr;
187 mBinderContextUserData = nullptr;
Steve Blockeafc6212012-01-06 19:20:56 +0000188 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700189 }
190 }
191 return mManagesContexts;
192}
193
Colin Crossdd6dafb2017-06-22 12:29:13 -0700194// Get references to userspace objects held by the kernel binder driver
195// Writes up to count elements into buf, and returns the total number
196// of references the kernel has, which may be larger than count.
197// buf may be NULL if count is 0. The pointers returned by this method
198// should only be used for debugging and not dereferenced, they may
199// already be invalid.
200ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) {
201 binder_node_debug_info info = {};
202
Yi Kong55d41072018-07-23 14:55:39 -0700203 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Crossdd6dafb2017-06-22 12:29:13 -0700204 size_t count = 0;
205
206 do {
207 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
208 if (result < 0) {
209 return -1;
210 }
211 if (info.ptr != 0) {
212 if (buf && buf < end) *buf++ = info.ptr;
213 count++;
214 if (buf && buf < end) *buf++ = info.cookie;
215 count++;
216 }
217 } while (info.ptr != 0);
218
219 return count;
220}
221
Martijn Coenen320e1d32018-09-07 10:41:33 +0200222// Queries the driver for the current strong reference count of the node
223// that the handle points to. Can only be used by the servicemanager.
224//
225// Returns -1 in case of failure, otherwise the strong reference count.
226ssize_t ProcessState::getStrongRefCountForNodeByHandle(int32_t handle) {
227 binder_node_info_for_ref info;
228 memset(&info, 0, sizeof(binder_node_info_for_ref));
229
230 info.handle = handle;
231
232 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info);
233
234 if (result != OK) {
Daniel Normanc0af11a2019-06-14 11:22:03 -0700235 static bool logged = false;
236 if (!logged) {
237 ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF.");
238 logged = true;
239 }
Martijn Coenen320e1d32018-09-07 10:41:33 +0200240 return -1;
241 }
242
243 return info.strong_count;
244}
245
Martijn Coenen32507d32018-03-22 17:12:57 +0100246size_t ProcessState::getMmapSize() {
247 return mMmapSize;
248}
249
Steven Moreland14603002019-01-02 17:54:16 -0800250void ProcessState::setCallRestriction(CallRestriction restriction) {
Steven Morelandd9bdb652019-09-17 15:42:45 -0700251 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr,
252 "Call restrictions must be set before the threadpool is started.");
Steven Moreland14603002019-01-02 17:54:16 -0800253
254 mCallRestriction = restriction;
255}
256
Mathias Agopian7922fa22009-05-18 15:08:03 -0700257ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
258{
259 const size_t N=mHandleToObject.size();
260 if (N <= (size_t)handle) {
261 handle_entry e;
Yi Kong55d41072018-07-23 14:55:39 -0700262 e.binder = nullptr;
263 e.refs = nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700264 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kong55d41072018-07-23 14:55:39 -0700265 if (err < NO_ERROR) return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700266 }
267 return &mHandleToObject.editItemAt(handle);
268}
269
270sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
271{
272 sp<IBinder> result;
273
274 AutoMutex _l(mLock);
275
276 handle_entry* e = lookupHandleLocked(handle);
277
Yi Kong55d41072018-07-23 14:55:39 -0700278 if (e != nullptr) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800279 // We need to create a new BpHwBinder if there isn't currently one, OR we
Mathias Agopian7922fa22009-05-18 15:08:03 -0700280 // are unable to acquire a weak reference on this current one. See comment
281 // in getWeakProxyForHandle() for more info about this.
282 IBinder* b = e->binder;
Yi Kong55d41072018-07-23 14:55:39 -0700283 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800284 b = new BpHwBinder(handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700285 e->binder = b;
286 if (b) e->refs = b->getWeakRefs();
287 result = b;
288 } else {
289 // This little bit of nastyness is to allow us to add a primary
290 // reference to the remote proxy when this team doesn't have one
291 // but another team is sending the handle to us.
292 result.force_set(b);
293 e->refs->decWeak(this);
294 }
295 }
296
297 return result;
298}
299
300wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
301{
302 wp<IBinder> result;
303
304 AutoMutex _l(mLock);
305
306 handle_entry* e = lookupHandleLocked(handle);
307
Yi Kong55d41072018-07-23 14:55:39 -0700308 if (e != nullptr) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800309 // We need to create a new BpHwBinder if there isn't currently one, OR we
Mathias Agopian7922fa22009-05-18 15:08:03 -0700310 // are unable to acquire a weak reference on this current one. The
Yifan Hong1e118d22017-01-12 14:42:28 -0800311 // attemptIncWeak() is safe because we know the BpHwBinder destructor will always
Mathias Agopian7922fa22009-05-18 15:08:03 -0700312 // call expungeHandle(), which acquires the same lock we are holding now.
313 // We need to do this because there is a race condition between someone
Yifan Hong1e118d22017-01-12 14:42:28 -0800314 // releasing a reference on this BpHwBinder, and a new reference on its handle
Mathias Agopian7922fa22009-05-18 15:08:03 -0700315 // arriving from the driver.
316 IBinder* b = e->binder;
Yi Kong55d41072018-07-23 14:55:39 -0700317 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800318 b = new BpHwBinder(handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700319 result = b;
320 e->binder = b;
321 if (b) e->refs = b->getWeakRefs();
322 } else {
323 result = b;
324 e->refs->decWeak(this);
325 }
326 }
327
328 return result;
329}
330
331void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
332{
333 AutoMutex _l(mLock);
Yifan Hong1e118d22017-01-12 14:42:28 -0800334
Mathias Agopian7922fa22009-05-18 15:08:03 -0700335 handle_entry* e = lookupHandleLocked(handle);
336
Yifan Hong1e118d22017-01-12 14:42:28 -0800337 // This handle may have already been replaced with a new BpHwBinder
Mathias Agopian7922fa22009-05-18 15:08:03 -0700338 // (if someone failed the AttemptIncWeak() above); we don't want
339 // to overwrite it.
Yi Kong55d41072018-07-23 14:55:39 -0700340 if (e && e->binder == binder) e->binder = nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700341}
342
Mathias Agopiand191ed72013-03-07 15:34:28 -0800343String8 ProcessState::makeBinderThreadName() {
344 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadra788e0052016-04-08 10:29:14 -0700345 pid_t pid = getpid();
Mathias Agopiand191ed72013-03-07 15:34:28 -0800346 String8 name;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100347 name.appendFormat("HwBinder:%d_%X", pid, s);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800348 return name;
349}
350
Mathias Agopian7922fa22009-05-18 15:08:03 -0700351void ProcessState::spawnPooledThread(bool isMain)
352{
353 if (mThreadPoolStarted) {
Mathias Agopiand191ed72013-03-07 15:34:28 -0800354 String8 name = makeBinderThreadName();
355 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700356 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800357 t->run(name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700358 }
359}
360
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100361status_t ProcessState::setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool) {
Steven Moreland2aa37072018-10-25 16:32:17 -0700362 // if the caller joins the pool, then there will be one thread which is impossible.
363 LOG_ALWAYS_FATAL_IF(maxThreads == 0 && callerJoinsPool,
364 "Binder threadpool must have a minimum of one thread if caller joins pool.");
365
366 size_t threadsToAllocate = maxThreads;
367
368 // If the caller is going to join the pool it will contribute one thread to the threadpool.
369 // This is part of the API's contract.
370 if (callerJoinsPool) threadsToAllocate--;
371
372 // If we can, spawn one thread from userspace when the threadpool is started. This ensures
373 // that there is always a thread available to start more threads as soon as the threadpool
374 // is started.
375 bool spawnThreadOnStart = threadsToAllocate > 0;
376 if (spawnThreadOnStart) threadsToAllocate--;
377
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100378 // the BINDER_SET_MAX_THREADS ioctl really tells the kernel how many threads
379 // it's allowed to spawn, *in addition* to any threads we may have already
Steven Moreland2aa37072018-10-25 16:32:17 -0700380 // spawned locally.
381 size_t kernelMaxThreads = threadsToAllocate;
382
383 AutoMutex _l(mLock);
384 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &kernelMaxThreads) == -1) {
385 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
386 return -errno;
Mathias Agopian8297f502012-04-17 16:11:08 -0700387 }
Steven Moreland2aa37072018-10-25 16:32:17 -0700388
389 mMaxThreads = maxThreads;
390 mSpawnThreadOnStart = spawnThreadOnStart;
391
392 return NO_ERROR;
Mathias Agopian8297f502012-04-17 16:11:08 -0700393}
394
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200395size_t ProcessState::getMaxThreads() {
396 return mMaxThreads;
397}
398
Mathias Agopiand191ed72013-03-07 15:34:28 -0800399void ProcessState::giveThreadPoolName() {
400 androidSetThreadName( makeBinderThreadName().string() );
401}
402
Mathias Agopian7922fa22009-05-18 15:08:03 -0700403static int open_driver()
404{
Martijn Coenena660cbc2016-05-12 11:29:23 +0200405 int fd = open("/dev/hwbinder", O_RDWR | O_CLOEXEC);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700406 if (fd >= 0) {
Jin Wei023b4682012-10-18 17:00:48 +0800407 int vers = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700408 status_t result = ioctl(fd, BINDER_VERSION, &vers);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700409 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000410 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700411 close(fd);
412 fd = -1;
413 }
414 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartmand5217742017-01-03 16:56:05 -0800415 ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)!", vers, BINDER_CURRENT_PROTOCOL_VERSION);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700416 close(fd);
417 fd = -1;
418 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700419 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700420 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
421 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000422 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700423 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700424 } else {
Iliyan Malcheve3256a62016-09-19 16:00:32 -0700425 ALOGW("Opening '/dev/hwbinder' failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700426 }
427 return fd;
428}
429
Martijn Coenen32507d32018-03-22 17:12:57 +0100430ProcessState::ProcessState(size_t mmap_size)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700431 : mDriverFD(open_driver())
432 , mVMStart(MAP_FAILED)
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700433 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
434 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
435 , mExecutingThreadsCount(0)
436 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Crossb1dc6542016-04-15 14:29:55 -0700437 , mStarvationStartTimeMs(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700438 , mManagesContexts(false)
Yi Kong55d41072018-07-23 14:55:39 -0700439 , mBinderContextCheckFunc(nullptr)
440 , mBinderContextUserData(nullptr)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700441 , mThreadPoolStarted(false)
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100442 , mSpawnThreadOnStart(true)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700443 , mThreadPoolSeq(1)
Martijn Coenen32507d32018-03-22 17:12:57 +0100444 , mMmapSize(mmap_size)
Steven Moreland14603002019-01-02 17:54:16 -0800445 , mCallRestriction(CallRestriction::NONE)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700446{
447 if (mDriverFD >= 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700448 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Yi Kong55d41072018-07-23 14:55:39 -0700449 mVMStart = mmap(nullptr, mMmapSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700450 if (mVMStart == MAP_FAILED) {
451 // *sigh*
Jiyong Park1268c282019-01-28 16:22:49 +0900452 ALOGE("Mmapping /dev/hwbinder failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700453 close(mDriverFD);
454 mDriverFD = -1;
455 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700456 }
Iliyan Malchevb7e22332016-09-26 00:08:55 -0700457 else {
458 ALOGE("Binder driver could not be opened. Terminating.");
459 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700460}
461
462ProcessState::~ProcessState()
463{
zhongjie8e8a0252016-03-09 15:05:04 +0800464 if (mDriverFD >= 0) {
465 if (mVMStart != MAP_FAILED) {
Martijn Coenen32507d32018-03-22 17:12:57 +0100466 munmap(mVMStart, mMmapSize);
zhongjie8e8a0252016-03-09 15:05:04 +0800467 }
468 close(mDriverFD);
469 }
470 mDriverFD = -1;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700471}
Yifan Hong1e118d22017-01-12 14:42:28 -0800472
Martijn Coenenf75a23d2016-08-01 11:55:17 +0200473}; // namespace hardware
Mathias Agopian7922fa22009-05-18 15:08:03 -0700474}; // namespace android