blob: 7264ac40867c6e6ff84ffd2f4e1591d7cc9e78da [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{
76 if (gProcess != NULL) return gProcess;
77
78 AutoMutex _l(gProcessMutex);
79 if (gProcess == NULL) gProcess = new ProcessState;
80 return gProcess;
81}
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083void ProcessState::setContextObject(const sp<IBinder>& object)
84{
85 setContextObject(object, String16("default"));
86}
87
88sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& caller)
89{
Jeff Browne16986c2011-07-08 18:52:57 -070090 return getStrongProxyForHandle(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091}
92
93void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name)
94{
95 AutoMutex _l(mLock);
96 mContexts.add(name, object);
97}
98
99sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller)
100{
101 mLock.lock();
102 sp<IBinder> object(
103 mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : NULL);
104 mLock.unlock();
105
106 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
107
108 if (object != NULL) return object;
109
110 // Don't attempt to retrieve contexts if we manage them
111 if (mManagesContexts) {
112 LOGE("getContextObject(%s) failed, but we manage the contexts!\n",
113 String8(name).string());
114 return NULL;
115 }
116
117 IPCThreadState* ipc = IPCThreadState::self();
118 {
119 Parcel data, reply;
120 // no interface token on this magic transaction
121 data.writeString16(name);
122 data.writeStrongBinder(caller);
123 status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0);
124 if (result == NO_ERROR) {
125 object = reply.readStrongBinder();
126 }
127 }
128
129 ipc->flushCommands();
130
131 if (object != NULL) setContextObject(object, name);
132 return object;
133}
134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135void ProcessState::startThreadPool()
136{
137 AutoMutex _l(mLock);
138 if (!mThreadPoolStarted) {
139 mThreadPoolStarted = true;
140 spawnPooledThread(true);
141 }
142}
143
144bool ProcessState::isContextManager(void) const
145{
146 return mManagesContexts;
147}
148
149bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
150{
151 if (!mManagesContexts) {
152 AutoMutex _l(mLock);
153 mBinderContextCheckFunc = checkFunc;
154 mBinderContextUserData = userData;
Jeff Browne16986c2011-07-08 18:52:57 -0700155
156 int dummy = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157#if defined(HAVE_ANDROID_OS)
Jeff Browne16986c2011-07-08 18:52:57 -0700158 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159#else
Jeff Browne16986c2011-07-08 18:52:57 -0700160 status_t result = INVALID_OPERATION;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161#endif
Jeff Browne16986c2011-07-08 18:52:57 -0700162 if (result == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 mManagesContexts = true;
Jeff Browne16986c2011-07-08 18:52:57 -0700164 } else if (result == -1) {
165 mBinderContextCheckFunc = NULL;
166 mBinderContextUserData = NULL;
167 LOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 }
169 }
170 return mManagesContexts;
171}
172
173ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
174{
175 const size_t N=mHandleToObject.size();
176 if (N <= (size_t)handle) {
177 handle_entry e;
178 e.binder = NULL;
179 e.refs = NULL;
180 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
181 if (err < NO_ERROR) return NULL;
182 }
183 return &mHandleToObject.editItemAt(handle);
184}
185
186sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
187{
188 sp<IBinder> result;
189
190 AutoMutex _l(mLock);
191
192 handle_entry* e = lookupHandleLocked(handle);
193
194 if (e != NULL) {
195 // We need to create a new BpBinder if there isn't currently one, OR we
196 // are unable to acquire a weak reference on this current one. See comment
197 // in getWeakProxyForHandle() for more info about this.
198 IBinder* b = e->binder;
199 if (b == NULL || !e->refs->attemptIncWeak(this)) {
200 b = new BpBinder(handle);
201 e->binder = b;
202 if (b) e->refs = b->getWeakRefs();
203 result = b;
204 } else {
205 // This little bit of nastyness is to allow us to add a primary
206 // reference to the remote proxy when this team doesn't have one
207 // but another team is sending the handle to us.
208 result.force_set(b);
209 e->refs->decWeak(this);
210 }
211 }
212
213 return result;
214}
215
216wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
217{
218 wp<IBinder> result;
219
220 AutoMutex _l(mLock);
221
222 handle_entry* e = lookupHandleLocked(handle);
223
224 if (e != NULL) {
225 // We need to create a new BpBinder if there isn't currently one, OR we
226 // are unable to acquire a weak reference on this current one. The
227 // attemptIncWeak() is safe because we know the BpBinder destructor will always
228 // call expungeHandle(), which acquires the same lock we are holding now.
229 // We need to do this because there is a race condition between someone
230 // releasing a reference on this BpBinder, and a new reference on its handle
231 // arriving from the driver.
232 IBinder* b = e->binder;
233 if (b == NULL || !e->refs->attemptIncWeak(this)) {
234 b = new BpBinder(handle);
235 result = b;
236 e->binder = b;
237 if (b) e->refs = b->getWeakRefs();
238 } else {
239 result = b;
240 e->refs->decWeak(this);
241 }
242 }
243
244 return result;
245}
246
247void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
248{
249 AutoMutex _l(mLock);
250
251 handle_entry* e = lookupHandleLocked(handle);
252
253 // This handle may have already been replaced with a new BpBinder
254 // (if someone failed the AttemptIncWeak() above); we don't want
255 // to overwrite it.
256 if (e && e->binder == binder) e->binder = NULL;
257}
258
259void ProcessState::setArgs(int argc, const char* const argv[])
260{
261 mArgC = argc;
262 mArgV = (const char **)argv;
263
264 mArgLen = 0;
265 for (int i=0; i<argc; i++) {
266 mArgLen += strlen(argv[i]) + 1;
267 }
268 mArgLen--;
269}
270
271int ProcessState::getArgC() const
272{
273 return mArgC;
274}
275
276const char* const* ProcessState::getArgV() const
277{
278 return mArgV;
279}
280
281void ProcessState::setArgV0(const char* txt)
282{
283 if (mArgV != NULL) {
284 strncpy((char*)mArgV[0], txt, mArgLen);
285 set_process_name(txt);
286 }
287}
288
289void ProcessState::spawnPooledThread(bool isMain)
290{
291 if (mThreadPoolStarted) {
292 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
293 char buf[32];
294 sprintf(buf, "Binder Thread #%d", s);
295 LOGV("Spawning new pooled thread, name=%s\n", buf);
296 sp<Thread> t = new PoolThread(isMain);
297 t->run(buf);
298 }
299}
300
301static int open_driver()
302{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 int fd = open("/dev/binder", O_RDWR);
304 if (fd >= 0) {
305 fcntl(fd, F_SETFD, FD_CLOEXEC);
306 int vers;
307#if defined(HAVE_ANDROID_OS)
308 status_t result = ioctl(fd, BINDER_VERSION, &vers);
309#else
310 status_t result = -1;
311 errno = EPERM;
312#endif
313 if (result == -1) {
314 LOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
315 close(fd);
316 fd = -1;
317 }
318 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
319 LOGE("Binder driver protocol does not match user space protocol!");
320 close(fd);
321 fd = -1;
322 }
323#if defined(HAVE_ANDROID_OS)
324 size_t maxThreads = 15;
325 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
326 if (result == -1) {
327 LOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
328 }
329#endif
330
331 } else {
332 LOGW("Opening '/dev/binder' failed: %s\n", strerror(errno));
333 }
334 return fd;
335}
336
337ProcessState::ProcessState()
338 : mDriverFD(open_driver())
339 , mVMStart(MAP_FAILED)
340 , mManagesContexts(false)
341 , mBinderContextCheckFunc(NULL)
342 , mBinderContextUserData(NULL)
343 , mThreadPoolStarted(false)
344 , mThreadPoolSeq(1)
345{
346 if (mDriverFD >= 0) {
347 // XXX Ideally, there should be a specific define for whether we
348 // have mmap (or whether we could possibly have the kernel module
349 // availabla).
350#if !defined(HAVE_WIN32_IPC)
351 // mmap the binder, providing a chunk of virtual address space to receive transactions.
352 mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
353 if (mVMStart == MAP_FAILED) {
354 // *sigh*
355 LOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
356 close(mDriverFD);
357 mDriverFD = -1;
358 }
359#else
360 mDriverFD = -1;
361#endif
362 }
Jeff Browne16986c2011-07-08 18:52:57 -0700363
364 LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365}
366
367ProcessState::~ProcessState()
368{
369}
370
371}; // namespace android