blob: 8a8b7ccc3460abdc8c5110c8f4577442766b471d [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>
Colin Crossdd6dafb2017-06-22 12:29:13 -070024#include <hwbinder/binder_kernel.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070025#include <utils/Log.h>
26#include <utils/String8.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070027#include <utils/threads.h>
28
29#include <private/binder/binder_module.h>
Martijn Coenene01f4f22016-05-12 12:33:28 +020030#include <hwbinder/Static.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070031
32#include <errno.h>
33#include <fcntl.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <sys/ioctl.h>
38#include <sys/mman.h>
39#include <sys/stat.h>
Philip Cuadra788e0052016-04-08 10:29:14 -070040#include <sys/types.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070041
Martijn Coenen32507d32018-03-22 17:12:57 +010042#define DEFAULT_BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2)
Martijn Coenenab00b6a2016-11-22 15:03:13 +010043#define DEFAULT_MAX_BINDER_THREADS 0
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{
Mathias Agopianb9a7d6a2012-04-16 19:30:56 -070070 Mutex::Autolock _l(gProcessMutex);
Yi Kong55d41072018-07-23 14:55:39 -070071 if (gProcess != nullptr) {
Mathias Agopianb9a7d6a2012-04-16 19:30:56 -070072 return gProcess;
73 }
Martijn Coenen32507d32018-03-22 17:12:57 +010074 gProcess = new ProcessState(DEFAULT_BINDER_VM_SIZE);
Mathias Agopian7922fa22009-05-18 15:08:03 -070075 return gProcess;
76}
77
Colin Crossdd6dafb2017-06-22 12:29:13 -070078sp<ProcessState> ProcessState::selfOrNull() {
79 Mutex::Autolock _l(gProcessMutex);
80 return gProcess;
81}
82
Martijn Coenen32507d32018-03-22 17:12:57 +010083sp<ProcessState> ProcessState::initWithMmapSize(size_t mmap_size) {
84 Mutex::Autolock _l(gProcessMutex);
Yi Kong55d41072018-07-23 14:55:39 -070085 if (gProcess != nullptr) {
Martijn Coenen32507d32018-03-22 17:12:57 +010086 LOG_ALWAYS_FATAL_IF(mmap_size != gProcess->getMmapSize(),
87 "ProcessState already initialized with a different mmap size.");
88 return gProcess;
89 }
90
91 gProcess = new ProcessState(mmap_size);
92 return gProcess;
93}
94
Mathias Agopian7922fa22009-05-18 15:08:03 -070095void ProcessState::setContextObject(const sp<IBinder>& object)
96{
97 setContextObject(object, String16("default"));
98}
99
Colin Crossf0487982014-02-05 17:42:44 -0800100sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700101{
Jeff Brownc0f143d2011-07-08 18:52:57 -0700102 return getStrongProxyForHandle(0);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700103}
104
105void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name)
106{
107 AutoMutex _l(mLock);
108 mContexts.add(name, object);
109}
110
111sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller)
112{
113 mLock.lock();
114 sp<IBinder> object(
Yi Kong55d41072018-07-23 14:55:39 -0700115 mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : nullptr);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700116 mLock.unlock();
Yifan Hong1e118d22017-01-12 14:42:28 -0800117
Mathias Agopian7922fa22009-05-18 15:08:03 -0700118 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
Yifan Hong1e118d22017-01-12 14:42:28 -0800119
Yi Kong55d41072018-07-23 14:55:39 -0700120 if (object != nullptr) return object;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700121
122 // Don't attempt to retrieve contexts if we manage them
123 if (mManagesContexts) {
Steve Blockeafc6212012-01-06 19:20:56 +0000124 ALOGE("getContextObject(%s) failed, but we manage the contexts!\n",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700125 String8(name).string());
Yi Kong55d41072018-07-23 14:55:39 -0700126 return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700127 }
Yifan Hong1e118d22017-01-12 14:42:28 -0800128
Mathias Agopian7922fa22009-05-18 15:08:03 -0700129 IPCThreadState* ipc = IPCThreadState::self();
130 {
131 Parcel data, reply;
132 // no interface token on this magic transaction
133 data.writeString16(name);
134 data.writeStrongBinder(caller);
135 status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0);
136 if (result == NO_ERROR) {
137 object = reply.readStrongBinder();
138 }
139 }
Yifan Hong1e118d22017-01-12 14:42:28 -0800140
Mathias Agopian7922fa22009-05-18 15:08:03 -0700141 ipc->flushCommands();
Yifan Hong1e118d22017-01-12 14:42:28 -0800142
Yi Kong55d41072018-07-23 14:55:39 -0700143 if (object != nullptr) setContextObject(object, name);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700144 return object;
145}
146
Mathias Agopian7922fa22009-05-18 15:08:03 -0700147void ProcessState::startThreadPool()
148{
149 AutoMutex _l(mLock);
150 if (!mThreadPoolStarted) {
151 mThreadPoolStarted = true;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100152 if (mSpawnThreadOnStart) {
153 spawnPooledThread(true);
154 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700155 }
156}
157
158bool ProcessState::isContextManager(void) const
159{
160 return mManagesContexts;
161}
162
163bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
164{
165 if (!mManagesContexts) {
166 AutoMutex _l(mLock);
167 mBinderContextCheckFunc = checkFunc;
168 mBinderContextUserData = userData;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700169
Steven Morelandc149dca2019-01-09 18:01:02 -0800170 flat_binder_object obj {
171 .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
172 };
173
174 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
175
176 // fallback to original method
177 if (result != 0) {
178 android_errorWriteLog(0x534e4554, "121035042");
179
180 int dummy = 0;
181 result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
182 }
183
Jeff Brownc0f143d2011-07-08 18:52:57 -0700184 if (result == 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700185 mManagesContexts = true;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700186 } else if (result == -1) {
Yi Kong55d41072018-07-23 14:55:39 -0700187 mBinderContextCheckFunc = nullptr;
188 mBinderContextUserData = nullptr;
Steve Blockeafc6212012-01-06 19:20:56 +0000189 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700190 }
191 }
192 return mManagesContexts;
193}
194
Colin Crossdd6dafb2017-06-22 12:29:13 -0700195// Get references to userspace objects held by the kernel binder driver
196// Writes up to count elements into buf, and returns the total number
197// of references the kernel has, which may be larger than count.
198// buf may be NULL if count is 0. The pointers returned by this method
199// should only be used for debugging and not dereferenced, they may
200// already be invalid.
201ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) {
202 binder_node_debug_info info = {};
203
Yi Kong55d41072018-07-23 14:55:39 -0700204 uintptr_t* end = buf ? buf + buf_count : nullptr;
Colin Crossdd6dafb2017-06-22 12:29:13 -0700205 size_t count = 0;
206
207 do {
208 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
209 if (result < 0) {
210 return -1;
211 }
212 if (info.ptr != 0) {
213 if (buf && buf < end) *buf++ = info.ptr;
214 count++;
215 if (buf && buf < end) *buf++ = info.cookie;
216 count++;
217 }
218 } while (info.ptr != 0);
219
220 return count;
221}
222
Martijn Coenen320e1d32018-09-07 10:41:33 +0200223// Queries the driver for the current strong reference count of the node
224// that the handle points to. Can only be used by the servicemanager.
225//
226// Returns -1 in case of failure, otherwise the strong reference count.
227ssize_t ProcessState::getStrongRefCountForNodeByHandle(int32_t handle) {
228 binder_node_info_for_ref info;
229 memset(&info, 0, sizeof(binder_node_info_for_ref));
230
231 info.handle = handle;
232
233 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info);
234
235 if (result != OK) {
236 return -1;
237 }
238
239 return info.strong_count;
240}
241
Martijn Coenen32507d32018-03-22 17:12:57 +0100242size_t ProcessState::getMmapSize() {
243 return mMmapSize;
244}
245
Steven Moreland14603002019-01-02 17:54:16 -0800246void ProcessState::setCallRestriction(CallRestriction restriction) {
247 LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull(), "Call restrictions must be set before the threadpool is started.");
248
249 mCallRestriction = restriction;
250}
251
Mathias Agopian7922fa22009-05-18 15:08:03 -0700252ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
253{
254 const size_t N=mHandleToObject.size();
255 if (N <= (size_t)handle) {
256 handle_entry e;
Yi Kong55d41072018-07-23 14:55:39 -0700257 e.binder = nullptr;
258 e.refs = nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700259 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
Yi Kong55d41072018-07-23 14:55:39 -0700260 if (err < NO_ERROR) return nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700261 }
262 return &mHandleToObject.editItemAt(handle);
263}
264
265sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
266{
267 sp<IBinder> result;
268
269 AutoMutex _l(mLock);
270
271 handle_entry* e = lookupHandleLocked(handle);
272
Yi Kong55d41072018-07-23 14:55:39 -0700273 if (e != nullptr) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800274 // We need to create a new BpHwBinder if there isn't currently one, OR we
Mathias Agopian7922fa22009-05-18 15:08:03 -0700275 // are unable to acquire a weak reference on this current one. See comment
276 // in getWeakProxyForHandle() for more info about this.
277 IBinder* b = e->binder;
Yi Kong55d41072018-07-23 14:55:39 -0700278 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800279 b = new BpHwBinder(handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700280 e->binder = b;
281 if (b) e->refs = b->getWeakRefs();
282 result = b;
283 } else {
284 // This little bit of nastyness is to allow us to add a primary
285 // reference to the remote proxy when this team doesn't have one
286 // but another team is sending the handle to us.
287 result.force_set(b);
288 e->refs->decWeak(this);
289 }
290 }
291
292 return result;
293}
294
295wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
296{
297 wp<IBinder> result;
298
299 AutoMutex _l(mLock);
300
301 handle_entry* e = lookupHandleLocked(handle);
302
Yi Kong55d41072018-07-23 14:55:39 -0700303 if (e != nullptr) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800304 // We need to create a new BpHwBinder if there isn't currently one, OR we
Mathias Agopian7922fa22009-05-18 15:08:03 -0700305 // are unable to acquire a weak reference on this current one. The
Yifan Hong1e118d22017-01-12 14:42:28 -0800306 // attemptIncWeak() is safe because we know the BpHwBinder destructor will always
Mathias Agopian7922fa22009-05-18 15:08:03 -0700307 // call expungeHandle(), which acquires the same lock we are holding now.
308 // We need to do this because there is a race condition between someone
Yifan Hong1e118d22017-01-12 14:42:28 -0800309 // releasing a reference on this BpHwBinder, and a new reference on its handle
Mathias Agopian7922fa22009-05-18 15:08:03 -0700310 // arriving from the driver.
311 IBinder* b = e->binder;
Yi Kong55d41072018-07-23 14:55:39 -0700312 if (b == nullptr || !e->refs->attemptIncWeak(this)) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800313 b = new BpHwBinder(handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700314 result = b;
315 e->binder = b;
316 if (b) e->refs = b->getWeakRefs();
317 } else {
318 result = b;
319 e->refs->decWeak(this);
320 }
321 }
322
323 return result;
324}
325
326void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
327{
328 AutoMutex _l(mLock);
Yifan Hong1e118d22017-01-12 14:42:28 -0800329
Mathias Agopian7922fa22009-05-18 15:08:03 -0700330 handle_entry* e = lookupHandleLocked(handle);
331
Yifan Hong1e118d22017-01-12 14:42:28 -0800332 // This handle may have already been replaced with a new BpHwBinder
Mathias Agopian7922fa22009-05-18 15:08:03 -0700333 // (if someone failed the AttemptIncWeak() above); we don't want
334 // to overwrite it.
Yi Kong55d41072018-07-23 14:55:39 -0700335 if (e && e->binder == binder) e->binder = nullptr;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700336}
337
Mathias Agopiand191ed72013-03-07 15:34:28 -0800338String8 ProcessState::makeBinderThreadName() {
339 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadra788e0052016-04-08 10:29:14 -0700340 pid_t pid = getpid();
Mathias Agopiand191ed72013-03-07 15:34:28 -0800341 String8 name;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100342 name.appendFormat("HwBinder:%d_%X", pid, s);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800343 return name;
344}
345
Mathias Agopian7922fa22009-05-18 15:08:03 -0700346void ProcessState::spawnPooledThread(bool isMain)
347{
348 if (mThreadPoolStarted) {
Mathias Agopiand191ed72013-03-07 15:34:28 -0800349 String8 name = makeBinderThreadName();
350 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700351 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800352 t->run(name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700353 }
354}
355
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100356status_t ProcessState::setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool) {
Steven Moreland2aa37072018-10-25 16:32:17 -0700357 // if the caller joins the pool, then there will be one thread which is impossible.
358 LOG_ALWAYS_FATAL_IF(maxThreads == 0 && callerJoinsPool,
359 "Binder threadpool must have a minimum of one thread if caller joins pool.");
360
361 size_t threadsToAllocate = maxThreads;
362
363 // If the caller is going to join the pool it will contribute one thread to the threadpool.
364 // This is part of the API's contract.
365 if (callerJoinsPool) threadsToAllocate--;
366
367 // If we can, spawn one thread from userspace when the threadpool is started. This ensures
368 // that there is always a thread available to start more threads as soon as the threadpool
369 // is started.
370 bool spawnThreadOnStart = threadsToAllocate > 0;
371 if (spawnThreadOnStart) threadsToAllocate--;
372
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100373 // the BINDER_SET_MAX_THREADS ioctl really tells the kernel how many threads
374 // it's allowed to spawn, *in addition* to any threads we may have already
Steven Moreland2aa37072018-10-25 16:32:17 -0700375 // spawned locally.
376 size_t kernelMaxThreads = threadsToAllocate;
377
378 AutoMutex _l(mLock);
379 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &kernelMaxThreads) == -1) {
380 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
381 return -errno;
Mathias Agopian8297f502012-04-17 16:11:08 -0700382 }
Steven Moreland2aa37072018-10-25 16:32:17 -0700383
384 mMaxThreads = maxThreads;
385 mSpawnThreadOnStart = spawnThreadOnStart;
386
387 return NO_ERROR;
Mathias Agopian8297f502012-04-17 16:11:08 -0700388}
389
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200390size_t ProcessState::getMaxThreads() {
391 return mMaxThreads;
392}
393
Mathias Agopiand191ed72013-03-07 15:34:28 -0800394void ProcessState::giveThreadPoolName() {
395 androidSetThreadName( makeBinderThreadName().string() );
396}
397
Mathias Agopian7922fa22009-05-18 15:08:03 -0700398static int open_driver()
399{
Martijn Coenena660cbc2016-05-12 11:29:23 +0200400 int fd = open("/dev/hwbinder", O_RDWR | O_CLOEXEC);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700401 if (fd >= 0) {
Jin Wei023b4682012-10-18 17:00:48 +0800402 int vers = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700403 status_t result = ioctl(fd, BINDER_VERSION, &vers);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700404 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000405 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700406 close(fd);
407 fd = -1;
408 }
409 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartmand5217742017-01-03 16:56:05 -0800410 ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)!", vers, BINDER_CURRENT_PROTOCOL_VERSION);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700411 close(fd);
412 fd = -1;
413 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700414 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700415 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
416 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000417 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700418 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700419 } else {
Iliyan Malcheve3256a62016-09-19 16:00:32 -0700420 ALOGW("Opening '/dev/hwbinder' failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700421 }
422 return fd;
423}
424
Martijn Coenen32507d32018-03-22 17:12:57 +0100425ProcessState::ProcessState(size_t mmap_size)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700426 : mDriverFD(open_driver())
427 , mVMStart(MAP_FAILED)
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700428 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
429 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
430 , mExecutingThreadsCount(0)
431 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Crossb1dc6542016-04-15 14:29:55 -0700432 , mStarvationStartTimeMs(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700433 , mManagesContexts(false)
Yi Kong55d41072018-07-23 14:55:39 -0700434 , mBinderContextCheckFunc(nullptr)
435 , mBinderContextUserData(nullptr)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700436 , mThreadPoolStarted(false)
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100437 , mSpawnThreadOnStart(true)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700438 , mThreadPoolSeq(1)
Martijn Coenen32507d32018-03-22 17:12:57 +0100439 , mMmapSize(mmap_size)
Steven Moreland14603002019-01-02 17:54:16 -0800440 , mCallRestriction(CallRestriction::NONE)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700441{
442 if (mDriverFD >= 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700443 // mmap the binder, providing a chunk of virtual address space to receive transactions.
Yi Kong55d41072018-07-23 14:55:39 -0700444 mVMStart = mmap(nullptr, mMmapSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700445 if (mVMStart == MAP_FAILED) {
446 // *sigh*
Jiyong Park1268c282019-01-28 16:22:49 +0900447 ALOGE("Mmapping /dev/hwbinder failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700448 close(mDriverFD);
449 mDriverFD = -1;
450 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700451 }
Iliyan Malchevb7e22332016-09-26 00:08:55 -0700452 else {
453 ALOGE("Binder driver could not be opened. Terminating.");
454 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700455}
456
457ProcessState::~ProcessState()
458{
zhongjie8e8a0252016-03-09 15:05:04 +0800459 if (mDriverFD >= 0) {
460 if (mVMStart != MAP_FAILED) {
Martijn Coenen32507d32018-03-22 17:12:57 +0100461 munmap(mVMStart, mMmapSize);
zhongjie8e8a0252016-03-09 15:05:04 +0800462 }
463 close(mDriverFD);
464 }
465 mDriverFD = -1;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700466}
Yifan Hong1e118d22017-01-12 14:42:28 -0800467
Martijn Coenenf75a23d2016-08-01 11:55:17 +0200468}; // namespace hardware
Mathias Agopian7922fa22009-05-18 15:08:03 -0700469}; // namespace android