blob: fe28533ba8bd3755878f66ad1324faf5b67c2c3a [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
21#include <utils/Atomic.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/BpBinder.h>
23#include <binder/IPCThreadState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/Log.h>
25#include <utils/String8.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070026#include <binder/IServiceManager.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
Rebecca Schultz Zavinc0c10922009-10-30 18:39:55 -070043#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *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
Philip Cuadraa082fa82016-04-08 10:29:14 -070046// -------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
48namespace android {
Wale Ogunwale376b8222015-04-13 16:16:10 -070049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050class PoolThread : public Thread
51{
52public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070053 explicit PoolThread(bool isMain)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 : mIsMain(isMain)
55 {
56 }
57
58protected:
59 virtual bool threadLoop()
60 {
61 IPCThreadState::self()->joinThreadPool(mIsMain);
62 return false;
63 }
64
65 const bool mIsMain;
66};
67
68sp<ProcessState> ProcessState::self()
69{
Mathias Agopiane8db8712012-04-16 19:30:56 -070070 Mutex::Autolock _l(gProcessMutex);
71 if (gProcess != NULL) {
72 return gProcess;
73 }
74 gProcess = new ProcessState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 return gProcess;
76}
77
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078void ProcessState::setContextObject(const sp<IBinder>& object)
79{
80 setContextObject(object, String16("default"));
81}
82
Colin Cross6f4f3ab2014-02-05 17:42:44 -080083sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084{
Jeff Browne16986c2011-07-08 18:52:57 -070085 return getStrongProxyForHandle(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
88void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name)
89{
90 AutoMutex _l(mLock);
91 mContexts.add(name, object);
92}
93
94sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller)
95{
96 mLock.lock();
97 sp<IBinder> object(
98 mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : NULL);
99 mLock.unlock();
100
101 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
102
103 if (object != NULL) return object;
104
105 // Don't attempt to retrieve contexts if we manage them
106 if (mManagesContexts) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000107 ALOGE("getContextObject(%s) failed, but we manage the contexts!\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 String8(name).string());
109 return NULL;
110 }
111
112 IPCThreadState* ipc = IPCThreadState::self();
113 {
114 Parcel data, reply;
115 // no interface token on this magic transaction
116 data.writeString16(name);
117 data.writeStrongBinder(caller);
118 status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0);
119 if (result == NO_ERROR) {
120 object = reply.readStrongBinder();
121 }
122 }
123
124 ipc->flushCommands();
125
126 if (object != NULL) setContextObject(object, name);
127 return object;
128}
129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130void ProcessState::startThreadPool()
131{
132 AutoMutex _l(mLock);
133 if (!mThreadPoolStarted) {
134 mThreadPoolStarted = true;
135 spawnPooledThread(true);
136 }
137}
138
139bool ProcessState::isContextManager(void) const
140{
141 return mManagesContexts;
142}
143
144bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
145{
146 if (!mManagesContexts) {
147 AutoMutex _l(mLock);
148 mBinderContextCheckFunc = checkFunc;
149 mBinderContextUserData = userData;
Jeff Browne16986c2011-07-08 18:52:57 -0700150
151 int dummy = 0;
Jeff Browne16986c2011-07-08 18:52:57 -0700152 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
Jeff Browne16986c2011-07-08 18:52:57 -0700153 if (result == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 mManagesContexts = true;
Jeff Browne16986c2011-07-08 18:52:57 -0700155 } else if (result == -1) {
156 mBinderContextCheckFunc = NULL;
157 mBinderContextUserData = NULL;
Steve Blocke6f43dd2012-01-06 19:20:56 +0000158 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 }
160 }
161 return mManagesContexts;
162}
163
164ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
165{
166 const size_t N=mHandleToObject.size();
167 if (N <= (size_t)handle) {
168 handle_entry e;
169 e.binder = NULL;
170 e.refs = NULL;
171 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
172 if (err < NO_ERROR) return NULL;
173 }
174 return &mHandleToObject.editItemAt(handle);
175}
176
177sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
178{
179 sp<IBinder> result;
180
181 AutoMutex _l(mLock);
182
183 handle_entry* e = lookupHandleLocked(handle);
184
185 if (e != NULL) {
186 // We need to create a new BpBinder if there isn't currently one, OR we
187 // are unable to acquire a weak reference on this current one. See comment
188 // in getWeakProxyForHandle() for more info about this.
189 IBinder* b = e->binder;
190 if (b == NULL || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700191 if (handle == 0) {
192 // Special case for context manager...
193 // The context manager is the only object for which we create
194 // a BpBinder proxy without already holding a reference.
195 // Perform a dummy transaction to ensure the context manager
196 // is registered before we create the first local reference
197 // to it (which will occur when creating the BpBinder).
198 // If a local reference is created for the BpBinder when the
199 // context manager is not present, the driver will fail to
200 // provide a reference to the context manager, but the
201 // driver API does not return status.
202 //
203 // Note that this is not race-free if the context manager
204 // dies while this code runs.
205 //
206 // TODO: add a driver API to wait for context manager, or
207 // stop special casing handle 0 for context manager and add
208 // a driver API to get a handle to the context manager with
209 // proper reference counting.
210
211 Parcel data;
212 status_t status = IPCThreadState::self()->transact(
213 0, IBinder::PING_TRANSACTION, data, NULL, 0);
214 if (status == DEAD_OBJECT)
215 return NULL;
216 }
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 b = new BpBinder(handle);
219 e->binder = b;
220 if (b) e->refs = b->getWeakRefs();
221 result = b;
222 } else {
223 // This little bit of nastyness is to allow us to add a primary
224 // reference to the remote proxy when this team doesn't have one
225 // but another team is sending the handle to us.
226 result.force_set(b);
227 e->refs->decWeak(this);
228 }
229 }
230
231 return result;
232}
233
234wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
235{
236 wp<IBinder> result;
237
238 AutoMutex _l(mLock);
239
240 handle_entry* e = lookupHandleLocked(handle);
241
242 if (e != NULL) {
243 // We need to create a new BpBinder if there isn't currently one, OR we
244 // are unable to acquire a weak reference on this current one. The
245 // attemptIncWeak() is safe because we know the BpBinder destructor will always
246 // call expungeHandle(), which acquires the same lock we are holding now.
247 // We need to do this because there is a race condition between someone
248 // releasing a reference on this BpBinder, and a new reference on its handle
249 // arriving from the driver.
250 IBinder* b = e->binder;
251 if (b == NULL || !e->refs->attemptIncWeak(this)) {
252 b = new BpBinder(handle);
253 result = b;
254 e->binder = b;
255 if (b) e->refs = b->getWeakRefs();
256 } else {
257 result = b;
258 e->refs->decWeak(this);
259 }
260 }
261
262 return result;
263}
264
265void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
266{
267 AutoMutex _l(mLock);
268
269 handle_entry* e = lookupHandleLocked(handle);
270
271 // This handle may have already been replaced with a new BpBinder
272 // (if someone failed the AttemptIncWeak() above); we don't want
273 // to overwrite it.
274 if (e && e->binder == binder) e->binder = NULL;
275}
276
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800277String8 ProcessState::makeBinderThreadName() {
278 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700279 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800280 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700281 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800282 return name;
283}
284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285void ProcessState::spawnPooledThread(bool isMain)
286{
287 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800288 String8 name = makeBinderThreadName();
289 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800291 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 }
293}
294
Mathias Agopian1b80f792012-04-17 16:11:08 -0700295status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
296 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700297 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
298 mMaxThreads = maxThreads;
299 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700300 result = -errno;
301 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
302 }
303 return result;
304}
305
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800306void ProcessState::giveThreadPoolName() {
307 androidSetThreadName( makeBinderThreadName().string() );
308}
309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310static int open_driver()
311{
Nick Kralevich0fe7ce32015-12-23 18:58:05 -0800312 int fd = open("/dev/binder", O_RDWR | O_CLOEXEC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 if (fd >= 0) {
Jin Wei78181df2012-10-18 17:00:48 +0800314 int vers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000317 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 close(fd);
319 fd = -1;
320 }
321 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Greg Hartman67ac00f2017-01-03 16:54:59 -0800322 ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! ioctl() return value: %d",
323 vers, BINDER_CURRENT_PROTOCOL_VERSION, result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 close(fd);
325 fd = -1;
326 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700327 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
329 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000330 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 } else {
Steve Block32397c12012-01-05 23:22:43 +0000333 ALOGW("Opening '/dev/binder' failed: %s\n", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 }
335 return fd;
336}
337
338ProcessState::ProcessState()
339 : mDriverFD(open_driver())
340 , mVMStart(MAP_FAILED)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700341 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
342 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
343 , mExecutingThreadsCount(0)
344 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Cross96e83222016-04-15 14:29:55 -0700345 , mStarvationStartTimeMs(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 , mManagesContexts(false)
347 , mBinderContextCheckFunc(NULL)
348 , mBinderContextUserData(NULL)
349 , mThreadPoolStarted(false)
350 , mThreadPoolSeq(1)
351{
352 if (mDriverFD >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 // mmap the binder, providing a chunk of virtual address space to receive transactions.
354 mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
355 if (mVMStart == MAP_FAILED) {
356 // *sigh*
Steve Blocke6f43dd2012-01-06 19:20:56 +0000357 ALOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 close(mDriverFD);
359 mDriverFD = -1;
360 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 }
Jeff Browne16986c2011-07-08 18:52:57 -0700362
363 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364}
365
366ProcessState::~ProcessState()
367{
zhongjieff405782016-03-09 15:05:04 +0800368 if (mDriverFD >= 0) {
369 if (mVMStart != MAP_FAILED) {
370 munmap(mVMStart, BINDER_VM_SIZE);
371 }
372 close(mDriverFD);
373 }
374 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375}
376
377}; // namespace android