blob: 7fcd8710a33f3bdcb2fa99fb4d1f3d93cc5c225c [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
Ganesh Mahendran6ac7fb52017-03-03 09:41:14 +080043#define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 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
Colin Cross0fb888d2017-06-20 17:48:33 -070078sp<ProcessState> ProcessState::selfOrNull()
79{
80 Mutex::Autolock _l(gProcessMutex);
81 return gProcess;
82}
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084void ProcessState::setContextObject(const sp<IBinder>& object)
85{
86 setContextObject(object, String16("default"));
87}
88
Colin Cross6f4f3ab2014-02-05 17:42:44 -080089sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090{
Jeff Browne16986c2011-07-08 18:52:57 -070091 return getStrongProxyForHandle(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092}
93
94void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name)
95{
96 AutoMutex _l(mLock);
97 mContexts.add(name, object);
98}
99
100sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller)
101{
102 mLock.lock();
103 sp<IBinder> object(
104 mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : NULL);
105 mLock.unlock();
106
107 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
108
109 if (object != NULL) return object;
110
111 // Don't attempt to retrieve contexts if we manage them
112 if (mManagesContexts) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000113 ALOGE("getContextObject(%s) failed, but we manage the contexts!\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 String8(name).string());
115 return NULL;
116 }
117
118 IPCThreadState* ipc = IPCThreadState::self();
119 {
120 Parcel data, reply;
121 // no interface token on this magic transaction
122 data.writeString16(name);
123 data.writeStrongBinder(caller);
124 status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0);
125 if (result == NO_ERROR) {
126 object = reply.readStrongBinder();
127 }
128 }
129
130 ipc->flushCommands();
131
132 if (object != NULL) setContextObject(object, name);
133 return object;
134}
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136void ProcessState::startThreadPool()
137{
138 AutoMutex _l(mLock);
139 if (!mThreadPoolStarted) {
140 mThreadPoolStarted = true;
141 spawnPooledThread(true);
142 }
143}
144
145bool ProcessState::isContextManager(void) const
146{
147 return mManagesContexts;
148}
149
150bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
151{
152 if (!mManagesContexts) {
153 AutoMutex _l(mLock);
154 mBinderContextCheckFunc = checkFunc;
155 mBinderContextUserData = userData;
Jeff Browne16986c2011-07-08 18:52:57 -0700156
157 int dummy = 0;
Jeff Browne16986c2011-07-08 18:52:57 -0700158 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
Jeff Browne16986c2011-07-08 18:52:57 -0700159 if (result == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 mManagesContexts = true;
Jeff Browne16986c2011-07-08 18:52:57 -0700161 } else if (result == -1) {
162 mBinderContextCheckFunc = NULL;
163 mBinderContextUserData = NULL;
Steve Blocke6f43dd2012-01-06 19:20:56 +0000164 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 }
166 }
167 return mManagesContexts;
168}
169
Colin Cross0fb888d2017-06-20 17:48:33 -0700170// Get references to userspace objects held by the kernel binder driver
171// Writes up to count elements into buf, and returns the total number
172// of references the kernel has, which may be larger than count.
173// buf may be NULL if count is 0. The pointers returned by this method
174// should only be used for debugging and not dereferenced, they may
175// already be invalid.
176ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf)
177{
178 // TODO: remove these when they are defined by bionic's binder.h
179 struct binder_node_debug_info {
180 binder_uintptr_t ptr;
181 binder_uintptr_t cookie;
182 __u32 has_strong_ref;
183 __u32 has_weak_ref;
184 };
185#define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
186
187 binder_node_debug_info info = {};
188
189 uintptr_t* end = buf ? buf + buf_count : NULL;
190 size_t count = 0;
191
192 do {
193 status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
194 if (result < 0) {
195 return -1;
196 }
197 if (info.ptr != 0) {
198 if (buf && buf < end)
199 *buf++ = info.ptr;
200 count++;
201 if (buf && buf < end)
202 *buf++ = info.cookie;
203 count++;
204 }
205 } while (info.ptr != 0);
206
207 return count;
208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
211{
212 const size_t N=mHandleToObject.size();
213 if (N <= (size_t)handle) {
214 handle_entry e;
215 e.binder = NULL;
216 e.refs = NULL;
217 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
218 if (err < NO_ERROR) return NULL;
219 }
220 return &mHandleToObject.editItemAt(handle);
221}
222
223sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
224{
225 sp<IBinder> result;
226
227 AutoMutex _l(mLock);
228
229 handle_entry* e = lookupHandleLocked(handle);
230
231 if (e != NULL) {
232 // We need to create a new BpBinder if there isn't currently one, OR we
233 // are unable to acquire a weak reference on this current one. See comment
234 // in getWeakProxyForHandle() for more info about this.
235 IBinder* b = e->binder;
236 if (b == NULL || !e->refs->attemptIncWeak(this)) {
Todd Poynora7b0f042013-06-18 17:25:37 -0700237 if (handle == 0) {
238 // Special case for context manager...
239 // The context manager is the only object for which we create
240 // a BpBinder proxy without already holding a reference.
241 // Perform a dummy transaction to ensure the context manager
242 // is registered before we create the first local reference
243 // to it (which will occur when creating the BpBinder).
244 // If a local reference is created for the BpBinder when the
245 // context manager is not present, the driver will fail to
246 // provide a reference to the context manager, but the
247 // driver API does not return status.
248 //
249 // Note that this is not race-free if the context manager
250 // dies while this code runs.
251 //
252 // TODO: add a driver API to wait for context manager, or
253 // stop special casing handle 0 for context manager and add
254 // a driver API to get a handle to the context manager with
255 // proper reference counting.
256
257 Parcel data;
258 status_t status = IPCThreadState::self()->transact(
259 0, IBinder::PING_TRANSACTION, data, NULL, 0);
260 if (status == DEAD_OBJECT)
261 return NULL;
262 }
263
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 b = new BpBinder(handle);
265 e->binder = b;
266 if (b) e->refs = b->getWeakRefs();
267 result = b;
268 } else {
269 // This little bit of nastyness is to allow us to add a primary
270 // reference to the remote proxy when this team doesn't have one
271 // but another team is sending the handle to us.
272 result.force_set(b);
273 e->refs->decWeak(this);
274 }
275 }
276
277 return result;
278}
279
280wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
281{
282 wp<IBinder> result;
283
284 AutoMutex _l(mLock);
285
286 handle_entry* e = lookupHandleLocked(handle);
287
288 if (e != NULL) {
289 // We need to create a new BpBinder if there isn't currently one, OR we
290 // are unable to acquire a weak reference on this current one. The
291 // attemptIncWeak() is safe because we know the BpBinder destructor will always
292 // call expungeHandle(), which acquires the same lock we are holding now.
293 // We need to do this because there is a race condition between someone
294 // releasing a reference on this BpBinder, and a new reference on its handle
295 // arriving from the driver.
296 IBinder* b = e->binder;
297 if (b == NULL || !e->refs->attemptIncWeak(this)) {
298 b = new BpBinder(handle);
299 result = b;
300 e->binder = b;
301 if (b) e->refs = b->getWeakRefs();
302 } else {
303 result = b;
304 e->refs->decWeak(this);
305 }
306 }
307
308 return result;
309}
310
311void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
312{
313 AutoMutex _l(mLock);
314
315 handle_entry* e = lookupHandleLocked(handle);
316
317 // This handle may have already been replaced with a new BpBinder
318 // (if someone failed the AttemptIncWeak() above); we don't want
319 // to overwrite it.
320 if (e && e->binder == binder) e->binder = NULL;
321}
322
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800323String8 ProcessState::makeBinderThreadName() {
324 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadraa082fa82016-04-08 10:29:14 -0700325 pid_t pid = getpid();
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800326 String8 name;
Philip Cuadraa082fa82016-04-08 10:29:14 -0700327 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800328 return name;
329}
330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331void ProcessState::spawnPooledThread(bool isMain)
332{
333 if (mThreadPoolStarted) {
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800334 String8 name = makeBinderThreadName();
335 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800337 t->run(name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 }
339}
340
Mathias Agopian1b80f792012-04-17 16:11:08 -0700341status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
342 status_t result = NO_ERROR;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700343 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
344 mMaxThreads = maxThreads;
345 } else {
Mathias Agopian1b80f792012-04-17 16:11:08 -0700346 result = -errno;
347 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
348 }
349 return result;
350}
351
Mathias Agopiane3e43b32013-03-07 15:34:28 -0800352void ProcessState::giveThreadPoolName() {
353 androidSetThreadName( makeBinderThreadName().string() );
354}
355
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356static int open_driver()
357{
Nick Kralevich0fe7ce32015-12-23 18:58:05 -0800358 int fd = open("/dev/binder", O_RDWR | O_CLOEXEC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 if (fd >= 0) {
Jin Wei78181df2012-10-18 17:00:48 +0800360 int vers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000363 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 close(fd);
365 fd = -1;
366 }
367 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000368 ALOGE("Binder driver protocol does not match user space protocol!");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 close(fd);
370 fd = -1;
371 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700372 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
374 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000375 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 } else {
Steve Block32397c12012-01-05 23:22:43 +0000378 ALOGW("Opening '/dev/binder' failed: %s\n", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 }
380 return fd;
381}
382
383ProcessState::ProcessState()
384 : mDriverFD(open_driver())
385 , mVMStart(MAP_FAILED)
Wale Ogunwale376b8222015-04-13 16:16:10 -0700386 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
387 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
388 , mExecutingThreadsCount(0)
389 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Cross96e83222016-04-15 14:29:55 -0700390 , mStarvationStartTimeMs(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 , mManagesContexts(false)
392 , mBinderContextCheckFunc(NULL)
393 , mBinderContextUserData(NULL)
394 , mThreadPoolStarted(false)
395 , mThreadPoolSeq(1)
396{
397 if (mDriverFD >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 // mmap the binder, providing a chunk of virtual address space to receive transactions.
399 mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
400 if (mVMStart == MAP_FAILED) {
401 // *sigh*
Steve Blocke6f43dd2012-01-06 19:20:56 +0000402 ALOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 close(mDriverFD);
404 mDriverFD = -1;
405 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 }
Jeff Browne16986c2011-07-08 18:52:57 -0700407
408 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409}
410
411ProcessState::~ProcessState()
412{
zhongjieff405782016-03-09 15:05:04 +0800413 if (mDriverFD >= 0) {
414 if (mVMStart != MAP_FAILED) {
415 munmap(mVMStart, BINDER_VM_SIZE);
416 }
417 close(mDriverFD);
418 }
419 mDriverFD = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420}
421
422}; // namespace android