blob: 5399e5288c7dc1c3da87c5b31dbc4308fc061f4c [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
19#include <cutils/process_name.h>
20
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/ProcessState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022
23#include <utils/Atomic.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070024#include <binder/BpBinder.h>
25#include <binder/IPCThreadState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <utils/Log.h>
27#include <utils/String8.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/String8.h>
30#include <utils/threads.h>
31
Mathias Agopian208059f2009-05-18 15:08:03 -070032#include <private/binder/binder_module.h>
33#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
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>
43
Rebecca Schultz Zavinc0c10922009-10-30 18:39:55 -070044#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
47// ---------------------------------------------------------------------------
48
49namespace android {
50
51// Global variables
52int mArgC;
53const char* const* mArgV;
54int mArgLen;
55
56class PoolThread : public Thread
57{
58public:
59 PoolThread(bool isMain)
60 : mIsMain(isMain)
61 {
62 }
63
64protected:
65 virtual bool threadLoop()
66 {
67 IPCThreadState::self()->joinThreadPool(mIsMain);
68 return false;
69 }
70
71 const bool mIsMain;
72};
73
74sp<ProcessState> ProcessState::self()
75{
Mathias Agopiane8db8712012-04-16 19:30:56 -070076 Mutex::Autolock _l(gProcessMutex);
77 if (gProcess != NULL) {
78 return gProcess;
79 }
80 gProcess = new ProcessState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 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
89sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& caller)
90{
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
170ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
171{
172 const size_t N=mHandleToObject.size();
173 if (N <= (size_t)handle) {
174 handle_entry e;
175 e.binder = NULL;
176 e.refs = NULL;
177 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
178 if (err < NO_ERROR) return NULL;
179 }
180 return &mHandleToObject.editItemAt(handle);
181}
182
183sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
184{
185 sp<IBinder> result;
186
187 AutoMutex _l(mLock);
188
189 handle_entry* e = lookupHandleLocked(handle);
190
191 if (e != NULL) {
192 // We need to create a new BpBinder if there isn't currently one, OR we
193 // are unable to acquire a weak reference on this current one. See comment
194 // in getWeakProxyForHandle() for more info about this.
195 IBinder* b = e->binder;
196 if (b == NULL || !e->refs->attemptIncWeak(this)) {
197 b = new BpBinder(handle);
198 e->binder = b;
199 if (b) e->refs = b->getWeakRefs();
200 result = b;
201 } else {
202 // This little bit of nastyness is to allow us to add a primary
203 // reference to the remote proxy when this team doesn't have one
204 // but another team is sending the handle to us.
205 result.force_set(b);
206 e->refs->decWeak(this);
207 }
208 }
209
210 return result;
211}
212
213wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
214{
215 wp<IBinder> result;
216
217 AutoMutex _l(mLock);
218
219 handle_entry* e = lookupHandleLocked(handle);
220
221 if (e != NULL) {
222 // We need to create a new BpBinder if there isn't currently one, OR we
223 // are unable to acquire a weak reference on this current one. The
224 // attemptIncWeak() is safe because we know the BpBinder destructor will always
225 // call expungeHandle(), which acquires the same lock we are holding now.
226 // We need to do this because there is a race condition between someone
227 // releasing a reference on this BpBinder, and a new reference on its handle
228 // arriving from the driver.
229 IBinder* b = e->binder;
230 if (b == NULL || !e->refs->attemptIncWeak(this)) {
231 b = new BpBinder(handle);
232 result = b;
233 e->binder = b;
234 if (b) e->refs = b->getWeakRefs();
235 } else {
236 result = b;
237 e->refs->decWeak(this);
238 }
239 }
240
241 return result;
242}
243
244void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
245{
246 AutoMutex _l(mLock);
247
248 handle_entry* e = lookupHandleLocked(handle);
249
250 // This handle may have already been replaced with a new BpBinder
251 // (if someone failed the AttemptIncWeak() above); we don't want
252 // to overwrite it.
253 if (e && e->binder == binder) e->binder = NULL;
254}
255
256void ProcessState::setArgs(int argc, const char* const argv[])
257{
258 mArgC = argc;
259 mArgV = (const char **)argv;
260
261 mArgLen = 0;
262 for (int i=0; i<argc; i++) {
263 mArgLen += strlen(argv[i]) + 1;
264 }
265 mArgLen--;
266}
267
268int ProcessState::getArgC() const
269{
270 return mArgC;
271}
272
273const char* const* ProcessState::getArgV() const
274{
275 return mArgV;
276}
277
278void ProcessState::setArgV0(const char* txt)
279{
280 if (mArgV != NULL) {
281 strncpy((char*)mArgV[0], txt, mArgLen);
282 set_process_name(txt);
283 }
284}
285
286void ProcessState::spawnPooledThread(bool isMain)
287{
288 if (mThreadPoolStarted) {
289 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Glenn Kasten102d5392012-02-28 12:30:08 -0800290 char buf[16];
291 snprintf(buf, sizeof(buf), "Binder_%X", s);
Steve Block6807e592011-10-20 11:56:00 +0100292 ALOGV("Spawning new pooled thread, name=%s\n", buf);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 sp<Thread> t = new PoolThread(isMain);
294 t->run(buf);
295 }
296}
297
298static int open_driver()
299{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 int fd = open("/dev/binder", O_RDWR);
301 if (fd >= 0) {
302 fcntl(fd, F_SETFD, FD_CLOEXEC);
303 int vers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 status_t result = ioctl(fd, BINDER_VERSION, &vers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000306 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 close(fd);
308 fd = -1;
309 }
310 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000311 ALOGE("Binder driver protocol does not match user space protocol!");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 close(fd);
313 fd = -1;
314 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 size_t maxThreads = 15;
316 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
317 if (result == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000318 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 } else {
Steve Block32397c12012-01-05 23:22:43 +0000321 ALOGW("Opening '/dev/binder' failed: %s\n", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 }
323 return fd;
324}
325
326ProcessState::ProcessState()
327 : mDriverFD(open_driver())
328 , mVMStart(MAP_FAILED)
329 , mManagesContexts(false)
330 , mBinderContextCheckFunc(NULL)
331 , mBinderContextUserData(NULL)
332 , mThreadPoolStarted(false)
333 , mThreadPoolSeq(1)
334{
335 if (mDriverFD >= 0) {
336 // XXX Ideally, there should be a specific define for whether we
337 // have mmap (or whether we could possibly have the kernel module
338 // availabla).
339#if !defined(HAVE_WIN32_IPC)
340 // mmap the binder, providing a chunk of virtual address space to receive transactions.
341 mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
342 if (mVMStart == MAP_FAILED) {
343 // *sigh*
Steve Blocke6f43dd2012-01-06 19:20:56 +0000344 ALOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 close(mDriverFD);
346 mDriverFD = -1;
347 }
348#else
349 mDriverFD = -1;
350#endif
351 }
Jeff Browne16986c2011-07-08 18:52:57 -0700352
353 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354}
355
356ProcessState::~ProcessState()
357{
358}
359
360}; // namespace android