blob: 874fa06c2be5b33fe37518606cae0e858814d701 [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
17#define LOG_TAG "ProcessState"
18
19#include <cutils/process_name.h>
20
Martijn Coenen4080edc2016-05-04 14:17:02 +020021#include <hwbinder/ProcessState.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070022
23#include <utils/Atomic.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020024#include <hwbinder/BpBinder.h>
25#include <hwbinder/IPCThreadState.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070026#include <utils/Log.h>
27#include <utils/String8.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020028#include <hwbinder/IServiceManager.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070029#include <utils/String8.h>
30#include <utils/threads.h>
31
32#include <private/binder/binder_module.h>
Martijn Coenene01f4f22016-05-12 12:33:28 +020033#include <hwbinder/Static.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070034
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <sys/ioctl.h>
41#include <sys/mman.h>
42#include <sys/stat.h>
Philip Cuadra788e0052016-04-08 10:29:14 -070043#include <sys/types.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070044
Rebecca Schultz Zavinba3e5e22009-10-30 18:39:55 -070045#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2))
Wale Ogunwale2e604f02015-04-13 16:16:10 -070046#define DEFAULT_MAX_BINDER_THREADS 15
Mathias Agopian7922fa22009-05-18 15:08:03 -070047
Philip Cuadra788e0052016-04-08 10:29:14 -070048// -------------------------------------------------------------------------
Mathias Agopian7922fa22009-05-18 15:08:03 -070049
50namespace android {
Martijn Coenene01f4f22016-05-12 12:33:28 +020051namespace hidl {
Wale Ogunwale2e604f02015-04-13 16:16:10 -070052
Mathias Agopian7922fa22009-05-18 15:08:03 -070053class PoolThread : public Thread
54{
55public:
Chih-Hung Hsieh1f555e92016-04-25 15:41:05 -070056 explicit PoolThread(bool isMain)
Mathias Agopian7922fa22009-05-18 15:08:03 -070057 : mIsMain(isMain)
58 {
59 }
60
61protected:
62 virtual bool threadLoop()
63 {
64 IPCThreadState::self()->joinThreadPool(mIsMain);
65 return false;
66 }
67
68 const bool mIsMain;
69};
70
71sp<ProcessState> ProcessState::self()
72{
Mathias Agopianb9a7d6a2012-04-16 19:30:56 -070073 Mutex::Autolock _l(gProcessMutex);
74 if (gProcess != NULL) {
75 return gProcess;
76 }
77 gProcess = new ProcessState;
Mathias Agopian7922fa22009-05-18 15:08:03 -070078 return gProcess;
79}
80
Mathias Agopian7922fa22009-05-18 15:08:03 -070081void ProcessState::setContextObject(const sp<IBinder>& object)
82{
83 setContextObject(object, String16("default"));
84}
85
Colin Crossf0487982014-02-05 17:42:44 -080086sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -070087{
Jeff Brownc0f143d2011-07-08 18:52:57 -070088 return getStrongProxyForHandle(0);
Mathias Agopian7922fa22009-05-18 15:08:03 -070089}
90
91void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name)
92{
93 AutoMutex _l(mLock);
94 mContexts.add(name, object);
95}
96
97sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller)
98{
99 mLock.lock();
100 sp<IBinder> object(
101 mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : NULL);
102 mLock.unlock();
103
104 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
105
106 if (object != NULL) return object;
107
108 // Don't attempt to retrieve contexts if we manage them
109 if (mManagesContexts) {
Steve Blockeafc6212012-01-06 19:20:56 +0000110 ALOGE("getContextObject(%s) failed, but we manage the contexts!\n",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700111 String8(name).string());
112 return NULL;
113 }
114
115 IPCThreadState* ipc = IPCThreadState::self();
116 {
117 Parcel data, reply;
118 // no interface token on this magic transaction
119 data.writeString16(name);
120 data.writeStrongBinder(caller);
121 status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0);
122 if (result == NO_ERROR) {
123 object = reply.readStrongBinder();
124 }
125 }
126
127 ipc->flushCommands();
128
129 if (object != NULL) setContextObject(object, name);
130 return object;
131}
132
Mathias Agopian7922fa22009-05-18 15:08:03 -0700133void ProcessState::startThreadPool()
134{
135 AutoMutex _l(mLock);
136 if (!mThreadPoolStarted) {
137 mThreadPoolStarted = true;
138 spawnPooledThread(true);
139 }
140}
141
142bool ProcessState::isContextManager(void) const
143{
144 return mManagesContexts;
145}
146
147bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
148{
149 if (!mManagesContexts) {
150 AutoMutex _l(mLock);
151 mBinderContextCheckFunc = checkFunc;
152 mBinderContextUserData = userData;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700153
154 int dummy = 0;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700155 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
Jeff Brownc0f143d2011-07-08 18:52:57 -0700156 if (result == 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700157 mManagesContexts = true;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700158 } else if (result == -1) {
159 mBinderContextCheckFunc = NULL;
160 mBinderContextUserData = NULL;
Steve Blockeafc6212012-01-06 19:20:56 +0000161 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700162 }
163 }
164 return mManagesContexts;
165}
166
167ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
168{
169 const size_t N=mHandleToObject.size();
170 if (N <= (size_t)handle) {
171 handle_entry e;
172 e.binder = NULL;
173 e.refs = NULL;
174 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
175 if (err < NO_ERROR) return NULL;
176 }
177 return &mHandleToObject.editItemAt(handle);
178}
179
180sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
181{
182 sp<IBinder> result;
183
184 AutoMutex _l(mLock);
185
186 handle_entry* e = lookupHandleLocked(handle);
187
188 if (e != NULL) {
189 // We need to create a new BpBinder if there isn't currently one, OR we
190 // are unable to acquire a weak reference on this current one. See comment
191 // in getWeakProxyForHandle() for more info about this.
192 IBinder* b = e->binder;
193 if (b == NULL || !e->refs->attemptIncWeak(this)) {
Todd Poynore6df0c12013-06-18 17:25:37 -0700194 if (handle == 0) {
195 // Special case for context manager...
196 // The context manager is the only object for which we create
197 // a BpBinder proxy without already holding a reference.
198 // Perform a dummy transaction to ensure the context manager
199 // is registered before we create the first local reference
200 // to it (which will occur when creating the BpBinder).
201 // If a local reference is created for the BpBinder when the
202 // context manager is not present, the driver will fail to
203 // provide a reference to the context manager, but the
204 // driver API does not return status.
205 //
206 // Note that this is not race-free if the context manager
207 // dies while this code runs.
208 //
209 // TODO: add a driver API to wait for context manager, or
210 // stop special casing handle 0 for context manager and add
211 // a driver API to get a handle to the context manager with
212 // proper reference counting.
213
214 Parcel data;
215 status_t status = IPCThreadState::self()->transact(
216 0, IBinder::PING_TRANSACTION, data, NULL, 0);
217 if (status == DEAD_OBJECT)
218 return NULL;
219 }
220
Mathias Agopian7922fa22009-05-18 15:08:03 -0700221 b = new BpBinder(handle);
222 e->binder = b;
223 if (b) e->refs = b->getWeakRefs();
224 result = b;
225 } else {
226 // This little bit of nastyness is to allow us to add a primary
227 // reference to the remote proxy when this team doesn't have one
228 // but another team is sending the handle to us.
229 result.force_set(b);
230 e->refs->decWeak(this);
231 }
232 }
233
234 return result;
235}
236
237wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
238{
239 wp<IBinder> result;
240
241 AutoMutex _l(mLock);
242
243 handle_entry* e = lookupHandleLocked(handle);
244
245 if (e != NULL) {
246 // We need to create a new BpBinder if there isn't currently one, OR we
247 // are unable to acquire a weak reference on this current one. The
248 // attemptIncWeak() is safe because we know the BpBinder destructor will always
249 // call expungeHandle(), which acquires the same lock we are holding now.
250 // We need to do this because there is a race condition between someone
251 // releasing a reference on this BpBinder, and a new reference on its handle
252 // arriving from the driver.
253 IBinder* b = e->binder;
254 if (b == NULL || !e->refs->attemptIncWeak(this)) {
255 b = new BpBinder(handle);
256 result = b;
257 e->binder = b;
258 if (b) e->refs = b->getWeakRefs();
259 } else {
260 result = b;
261 e->refs->decWeak(this);
262 }
263 }
264
265 return result;
266}
267
268void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
269{
270 AutoMutex _l(mLock);
271
272 handle_entry* e = lookupHandleLocked(handle);
273
274 // This handle may have already been replaced with a new BpBinder
275 // (if someone failed the AttemptIncWeak() above); we don't want
276 // to overwrite it.
277 if (e && e->binder == binder) e->binder = NULL;
278}
279
Mathias Agopiand191ed72013-03-07 15:34:28 -0800280String8 ProcessState::makeBinderThreadName() {
281 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadra788e0052016-04-08 10:29:14 -0700282 pid_t pid = getpid();
Mathias Agopiand191ed72013-03-07 15:34:28 -0800283 String8 name;
Philip Cuadra788e0052016-04-08 10:29:14 -0700284 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800285 return name;
286}
287
Mathias Agopian7922fa22009-05-18 15:08:03 -0700288void ProcessState::spawnPooledThread(bool isMain)
289{
290 if (mThreadPoolStarted) {
Mathias Agopiand191ed72013-03-07 15:34:28 -0800291 String8 name = makeBinderThreadName();
292 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700293 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800294 t->run(name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700295 }
296}
297
Mathias Agopian8297f502012-04-17 16:11:08 -0700298status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
299 status_t result = NO_ERROR;
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700300 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
301 mMaxThreads = maxThreads;
302 } else {
Mathias Agopian8297f502012-04-17 16:11:08 -0700303 result = -errno;
304 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
305 }
306 return result;
307}
308
Mathias Agopiand191ed72013-03-07 15:34:28 -0800309void ProcessState::giveThreadPoolName() {
310 androidSetThreadName( makeBinderThreadName().string() );
311}
312
Mathias Agopian7922fa22009-05-18 15:08:03 -0700313static int open_driver()
314{
Nick Kralevichd4d2c2f2015-12-23 18:58:05 -0800315 int fd = open("/dev/binder", O_RDWR | O_CLOEXEC);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700316 if (fd >= 0) {
Jin Wei023b4682012-10-18 17:00:48 +0800317 int vers = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700318 status_t result = ioctl(fd, BINDER_VERSION, &vers);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700319 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000320 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700321 close(fd);
322 fd = -1;
323 }
324 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Steve Blockeafc6212012-01-06 19:20:56 +0000325 ALOGE("Binder driver protocol does not match user space protocol!");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700326 close(fd);
327 fd = -1;
328 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700329 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700330 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
331 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000332 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700333 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700334 } else {
Steve Blockd8e19162012-01-05 23:22:43 +0000335 ALOGW("Opening '/dev/binder' failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700336 }
337 return fd;
338}
339
340ProcessState::ProcessState()
341 : mDriverFD(open_driver())
342 , mVMStart(MAP_FAILED)
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700343 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
344 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
345 , mExecutingThreadsCount(0)
346 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Crossb1dc6542016-04-15 14:29:55 -0700347 , mStarvationStartTimeMs(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700348 , mManagesContexts(false)
349 , mBinderContextCheckFunc(NULL)
350 , mBinderContextUserData(NULL)
351 , mThreadPoolStarted(false)
352 , mThreadPoolSeq(1)
353{
354 if (mDriverFD >= 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700355 // mmap the binder, providing a chunk of virtual address space to receive transactions.
356 mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
357 if (mVMStart == MAP_FAILED) {
358 // *sigh*
Steve Blockeafc6212012-01-06 19:20:56 +0000359 ALOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700360 close(mDriverFD);
361 mDriverFD = -1;
362 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700363 }
Jeff Brownc0f143d2011-07-08 18:52:57 -0700364
365 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700366}
367
368ProcessState::~ProcessState()
369{
zhongjie8e8a0252016-03-09 15:05:04 +0800370 if (mDriverFD >= 0) {
371 if (mVMStart != MAP_FAILED) {
372 munmap(mVMStart, BINDER_VM_SIZE);
373 }
374 close(mDriverFD);
375 }
376 mDriverFD = -1;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700377}
378
Martijn Coenene01f4f22016-05-12 12:33:28 +0200379}; // namespace hidl
Mathias Agopian7922fa22009-05-18 15:08:03 -0700380}; // namespace android