blob: 156766a7f2761096d236238a56bf59300e9d6259 [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>
41
Rebecca Schultz Zavinc0c10922009-10-30 18:39:55 -070042#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2))
Wale Ogunwale376b8222015-04-13 16:16:10 -070043#define DEFAULT_MAX_BINDER_THREADS 15
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46// ---------------------------------------------------------------------------
47
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);
279 String8 name;
280 name.appendFormat("Binder_%X", s);
281 return name;
282}
283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284void ProcessState::spawnPooledThread(bool isMain)
285{
286 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800287 String8 name = makeBinderThreadName();
288 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800290 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 }
292}
293
Mathias Agopian1b80f792012-04-17 16:11:08 -0700294status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
295 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700296 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
297 mMaxThreads = maxThreads;
298 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700299 result = -errno;
300 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
301 }
302 return result;
303}
304
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800305void ProcessState::giveThreadPoolName() {
306 androidSetThreadName( makeBinderThreadName().string() );
307}
308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309static int open_driver()
310{
Nick Kralevich0fe7ce32015-12-23 18:58:05 -0800311 int fd = open("/dev/binder", O_RDWR | O_CLOEXEC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 if (fd >= 0) {
Jin Wei78181df2012-10-18 17:00:48 +0800313 int vers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000316 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 close(fd);
318 fd = -1;
319 }
320 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000321 ALOGE("Binder driver protocol does not match user space protocol!");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 close(fd);
323 fd = -1;
324 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700325 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
327 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000328 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 } else {
Steve Block32397c12012-01-05 23:22:43 +0000331 ALOGW("Opening '/dev/binder' failed: %s\n", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 }
333 return fd;
334}
335
336ProcessState::ProcessState()
337 : mDriverFD(open_driver())
338 , mVMStart(MAP_FAILED)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700339 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
340 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
341 , mExecutingThreadsCount(0)
342 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 , mManagesContexts(false)
344 , mBinderContextCheckFunc(NULL)
345 , mBinderContextUserData(NULL)
346 , mThreadPoolStarted(false)
347 , mThreadPoolSeq(1)
348{
349 if (mDriverFD >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 // mmap the binder, providing a chunk of virtual address space to receive transactions.
351 mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
352 if (mVMStart == MAP_FAILED) {
353 // *sigh*
Steve Blocke6f43dd2012-01-06 19:20:56 +0000354 ALOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355 close(mDriverFD);
356 mDriverFD = -1;
357 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 }
Jeff Browne16986c2011-07-08 18:52:57 -0700359
360 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361}
362
363ProcessState::~ProcessState()
364{
zhongjieff405782016-03-09 15:05:04 +0800365 if (mDriverFD >= 0) {
366 if (mVMStart != MAP_FAILED) {
367 munmap(mVMStart, BINDER_VM_SIZE);
368 }
369 close(mDriverFD);
370 }
371 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372}
373
374}; // namespace android