blob: 90c96a2c017d07e37ea5c33972ad4799e7702491 [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
Jason Parksdcd39582009-11-03 12:14:38 -080017#define LOG_TAG "IPCThreadState"
18
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IPCThreadState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/Binder.h>
22#include <binder/BpBinder.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070023#include <binder/TextOutput.h>
24
Glenn Kastena26e1cf2012-03-16 07:15:23 -070025#include <cutils/sched_policy.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <utils/Log.h>
Colin Cross96e83222016-04-15 14:29:55 -070027#include <utils/SystemClock.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070034#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <pthread.h>
36#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080037#include <signal.h>
38#include <stdio.h>
39#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080041#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#if LOG_NDEBUG
44
45#define IF_LOG_TRANSACTIONS() if (false)
46#define IF_LOG_COMMANDS() if (false)
47#define LOG_REMOTEREFS(...)
48#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#define LOG_THREADPOOL(...)
51#define LOG_ONEWAY(...)
52
53#else
54
Steve Block9f760152011-10-12 17:27:03 +010055#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
56#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
57#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
58#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
59#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
60#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
62#endif
63
64// ---------------------------------------------------------------------------
65
66namespace android {
67
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070068// Static const and functions will be optimized out if not used,
69// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070static const char *kReturnStrings[] = {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070071 "BR_ERROR",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 "BR_OK",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 "BR_TRANSACTION",
74 "BR_REPLY",
75 "BR_ACQUIRE_RESULT",
76 "BR_DEAD_REPLY",
77 "BR_TRANSACTION_COMPLETE",
78 "BR_INCREFS",
79 "BR_ACQUIRE",
80 "BR_RELEASE",
81 "BR_DECREFS",
82 "BR_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 "BR_NOOP",
84 "BR_SPAWN_LOOPER",
85 "BR_FINISHED",
86 "BR_DEAD_BINDER",
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070087 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
88 "BR_FAILED_REPLY"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089};
90
91static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 "BC_TRANSACTION",
93 "BC_REPLY",
94 "BC_ACQUIRE_RESULT",
95 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 "BC_INCREFS",
97 "BC_ACQUIRE",
98 "BC_RELEASE",
99 "BC_DECREFS",
100 "BC_INCREFS_DONE",
101 "BC_ACQUIRE_DONE",
102 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 "BC_REGISTER_LOOPER",
104 "BC_ENTER_LOOPER",
105 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 "BC_REQUEST_DEATH_NOTIFICATION",
107 "BC_CLEAR_DEATH_NOTIFICATION",
108 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109};
110
songjinshi73a7dde2016-10-18 21:05:56 +0800111static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112{
songjinshi73a7dde2016-10-18 21:05:56 +0800113 size_t idx = cmd & 0xff;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
115 return kReturnStrings[idx];
116 else
117 return "unknown";
118}
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120static const void* printBinderTransactionData(TextOutput& out, const void* data)
121{
122 const binder_transaction_data* btd =
123 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700124 if (btd->target.handle < 1024) {
125 /* want to print descriptors in decimal; guess based on value */
126 out << "target.desc=" << btd->target.handle;
127 } else {
128 out << "target.ptr=" << btd->target.ptr;
129 }
130 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700131 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
133 << " bytes)" << endl
134 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700135 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 return btd+1;
137}
138
139static const void* printReturnCommand(TextOutput& out, const void* _cmd)
140{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700141 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100143 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700144 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100145 if (code == BR_ERROR) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700146 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700148 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 out << "Unknown reply: " << code << endl;
150 return cmd;
151 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700152 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 switch (code) {
155 case BR_TRANSACTION:
156 case BR_REPLY: {
157 out << ": " << indent;
158 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
159 out << dedent;
160 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 case BR_ACQUIRE_RESULT: {
163 const int32_t res = *cmd++;
164 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
165 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 case BR_INCREFS:
168 case BR_ACQUIRE:
169 case BR_RELEASE:
170 case BR_DECREFS: {
171 const int32_t b = *cmd++;
172 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700173 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 case BR_ATTEMPT_ACQUIRE: {
177 const int32_t p = *cmd++;
178 const int32_t b = *cmd++;
179 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700180 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 << "), pri=" << p;
182 } break;
183
184 case BR_DEAD_BINDER:
185 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
186 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700187 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700189
190 default:
191 // no details to show for: BR_OK, BR_DEAD_REPLY,
192 // BR_TRANSACTION_COMPLETE, BR_FINISHED
193 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800195
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 out << endl;
197 return cmd;
198}
199
200static const void* printCommand(TextOutput& out, const void* _cmd)
201{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700202 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100204 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700205 size_t cmdIndex = code & 0xff;
206
207 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 out << "Unknown command: " << code << endl;
209 return cmd;
210 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700211 out << kCommandStrings[cmdIndex];
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 switch (code) {
214 case BC_TRANSACTION:
215 case BC_REPLY: {
216 out << ": " << indent;
217 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
218 out << dedent;
219 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 case BC_ACQUIRE_RESULT: {
222 const int32_t res = *cmd++;
223 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
224 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 case BC_FREE_BUFFER: {
227 const int32_t buf = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700228 out << ": buffer=" << (void*)(long)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 case BC_INCREFS:
232 case BC_ACQUIRE:
233 case BC_RELEASE:
234 case BC_DECREFS: {
235 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700236 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 case BC_INCREFS_DONE:
240 case BC_ACQUIRE_DONE: {
241 const int32_t b = *cmd++;
242 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700243 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800245
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 case BC_ATTEMPT_ACQUIRE: {
247 const int32_t p = *cmd++;
248 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700249 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 case BC_REQUEST_DEATH_NOTIFICATION:
253 case BC_CLEAR_DEATH_NOTIFICATION: {
254 const int32_t h = *cmd++;
255 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700256 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 } break;
258
259 case BC_DEAD_BINDER_DONE: {
260 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700261 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700263
264 default:
265 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
266 // BC_EXIT_LOOPER
267 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800269
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 out << endl;
271 return cmd;
272}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273
274static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
275static bool gHaveTLS = false;
276static pthread_key_t gTLS = 0;
277static bool gShutdown = false;
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800278static bool gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
280IPCThreadState* IPCThreadState::self()
281{
282 if (gHaveTLS) {
283restart:
284 const pthread_key_t k = gTLS;
285 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
286 if (st) return st;
287 return new IPCThreadState;
288 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800289
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800290 if (gShutdown) {
291 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
292 return NULL;
293 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800294
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 pthread_mutex_lock(&gTLSMutex);
296 if (!gHaveTLS) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800297 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
298 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800300 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
301 strerror(key_create_value));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 return NULL;
303 }
304 gHaveTLS = true;
305 }
306 pthread_mutex_unlock(&gTLSMutex);
307 goto restart;
308}
309
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800310IPCThreadState* IPCThreadState::selfOrNull()
311{
312 if (gHaveTLS) {
313 const pthread_key_t k = gTLS;
314 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
315 return st;
316 }
317 return NULL;
318}
319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320void IPCThreadState::shutdown()
321{
322 gShutdown = true;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 if (gHaveTLS) {
325 // XXX Need to wait for all thread pool threads to exit!
326 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
327 if (st) {
328 delete st;
329 pthread_setspecific(gTLS, NULL);
330 }
zhongjieff405782016-03-09 15:05:04 +0800331 pthread_key_delete(gTLS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 gHaveTLS = false;
333 }
334}
335
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800336void IPCThreadState::disableBackgroundScheduling(bool disable)
337{
338 gDisableBackgroundScheduling = disable;
339}
340
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341sp<ProcessState> IPCThreadState::process()
342{
343 return mProcess;
344}
345
346status_t IPCThreadState::clearLastError()
347{
348 const status_t err = mLastError;
349 mLastError = NO_ERROR;
350 return err;
351}
352
Dan Stoza9c634fd2014-11-26 12:23:23 -0800353pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354{
355 return mCallingPid;
356}
357
Dan Stoza9c634fd2014-11-26 12:23:23 -0800358uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359{
360 return mCallingUid;
361}
362
363int64_t IPCThreadState::clearCallingIdentity()
364{
365 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
366 clearCaller();
367 return token;
368}
369
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700370void IPCThreadState::setStrictModePolicy(int32_t policy)
371{
372 mStrictModePolicy = policy;
373}
374
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700375int32_t IPCThreadState::getStrictModePolicy() const
376{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700377 return mStrictModePolicy;
378}
379
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700380void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
381{
382 mLastTransactionBinderFlags = flags;
383}
384
385int32_t IPCThreadState::getLastTransactionBinderFlags() const
386{
387 return mLastTransactionBinderFlags;
388}
389
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390void IPCThreadState::restoreCallingIdentity(int64_t token)
391{
392 mCallingUid = (int)(token>>32);
393 mCallingPid = (int)token;
394}
395
396void IPCThreadState::clearCaller()
397{
Marco Nelissend43b1942009-07-17 07:59:17 -0700398 mCallingPid = getpid();
399 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400}
401
402void IPCThreadState::flushCommands()
403{
404 if (mProcess->mDriverFD <= 0)
405 return;
406 talkWithDriver(false);
407}
408
Wale Ogunwale376b8222015-04-13 16:16:10 -0700409void IPCThreadState::blockUntilThreadAvailable()
410{
411 pthread_mutex_lock(&mProcess->mThreadCountLock);
412 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700413 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
414 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
415 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700416 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
417 }
418 pthread_mutex_unlock(&mProcess->mThreadCountLock);
419}
420
Todd Poynor8d96cab2013-06-25 19:12:18 -0700421status_t IPCThreadState::getAndExecuteCommand()
422{
423 status_t result;
424 int32_t cmd;
425
426 result = talkWithDriver();
427 if (result >= NO_ERROR) {
428 size_t IN = mIn.dataAvail();
429 if (IN < sizeof(int32_t)) return result;
430 cmd = mIn.readInt32();
431 IF_LOG_COMMANDS() {
432 alog << "Processing top-level Command: "
433 << getReturnString(cmd) << endl;
434 }
435
Wale Ogunwale376b8222015-04-13 16:16:10 -0700436 pthread_mutex_lock(&mProcess->mThreadCountLock);
437 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700438 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
439 mProcess->mStarvationStartTimeMs == 0) {
440 mProcess->mStarvationStartTimeMs = uptimeMillis();
441 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700442 pthread_mutex_unlock(&mProcess->mThreadCountLock);
443
Todd Poynor8d96cab2013-06-25 19:12:18 -0700444 result = executeCommand(cmd);
445
Wale Ogunwale376b8222015-04-13 16:16:10 -0700446 pthread_mutex_lock(&mProcess->mThreadCountLock);
447 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700448 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
449 mProcess->mStarvationStartTimeMs != 0) {
450 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
451 if (starvationTimeMs > 100) {
452 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
453 mProcess->mMaxThreads, starvationTimeMs);
454 }
455 mProcess->mStarvationStartTimeMs = 0;
456 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700457 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
458 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700459 }
460
461 return result;
462}
463
464// When we've cleared the incoming command queue, process any pending derefs
465void IPCThreadState::processPendingDerefs()
466{
467 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200468 /*
469 * The decWeak()/decStrong() calls may cause a destructor to run,
470 * which in turn could have initiated an outgoing transaction,
471 * which in turn could cause us to add to the pending refs
472 * vectors; so instead of simply iterating, loop until they're empty.
473 *
474 * We do this in an outer loop, because calling decStrong()
475 * may result in something being added to mPendingWeakDerefs,
476 * which could be delayed until the next incoming command
477 * from the driver if we don't process it now.
478 */
479 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
480 while (mPendingWeakDerefs.size() > 0) {
481 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
482 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700483 refs->decWeak(mProcess.get());
484 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700485
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200486 if (mPendingStrongDerefs.size() > 0) {
487 // We don't use while() here because we don't want to re-order
488 // strong and weak decs at all; if this decStrong() causes both a
489 // decWeak() and a decStrong() to be queued, we want to process
490 // the decWeak() first.
491 BBinder* obj = mPendingStrongDerefs[0];
492 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700493 obj->decStrong(mProcess.get());
494 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700495 }
496 }
497}
498
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499void IPCThreadState::joinThreadPool(bool isMain)
500{
501 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
502
503 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 status_t result;
506 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700507 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700509 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800510
Todd Poynor8d96cab2013-06-25 19:12:18 -0700511 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
512 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700513 mProcess->mDriverFD, result);
514 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 // Let this thread exit the thread pool if it is no longer
518 // needed and it is not the main process thread.
519 if(result == TIMED_OUT && !isMain) {
520 break;
521 }
522 } while (result != -ECONNREFUSED && result != -EBADF);
523
Wei Wangc7341432016-10-19 10:23:59 -0700524 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
525 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800526
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 mOut.writeInt32(BC_EXIT_LOOPER);
528 talkWithDriver(false);
529}
530
Todd Poynor8d96cab2013-06-25 19:12:18 -0700531int IPCThreadState::setupPolling(int* fd)
532{
533 if (mProcess->mDriverFD <= 0) {
534 return -EBADF;
535 }
536
537 mOut.writeInt32(BC_ENTER_LOOPER);
538 *fd = mProcess->mDriverFD;
539 return 0;
540}
541
542status_t IPCThreadState::handlePolledCommands()
543{
544 status_t result;
545
546 do {
547 result = getAndExecuteCommand();
548 } while (mIn.dataPosition() < mIn.dataSize());
549
550 processPendingDerefs();
551 flushCommands();
552 return result;
553}
554
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800555void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556{
Steve Blocka19954a2012-01-04 20:05:49 +0000557 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 flushCommands();
559 int fd = mProcess->mDriverFD;
560 mProcess->mDriverFD = -1;
561 close(fd);
562 //kill(getpid(), SIGKILL);
563}
564
565status_t IPCThreadState::transact(int32_t handle,
566 uint32_t code, const Parcel& data,
567 Parcel* reply, uint32_t flags)
568{
569 status_t err = data.errorCheck();
570
571 flags |= TF_ACCEPT_FDS;
572
573 IF_LOG_TRANSACTIONS() {
574 TextOutput::Bundle _b(alog);
575 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
576 << handle << " / code " << TypeCode(code) << ": "
577 << indent << data << dedent << endl;
578 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 if (err == NO_ERROR) {
581 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
582 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
583 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);
584 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800585
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 if (err != NO_ERROR) {
587 if (reply) reply->setError(err);
588 return (mLastError = err);
589 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800590
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700592 #if 0
593 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000594 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700595 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000596 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700597 }
598 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 if (reply) {
600 err = waitForResponse(reply);
601 } else {
602 Parcel fakeReply;
603 err = waitForResponse(&fakeReply);
604 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700605 #if 0
606 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000607 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700608 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000609 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700610 }
611 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800612
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 IF_LOG_TRANSACTIONS() {
614 TextOutput::Bundle _b(alog);
615 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
616 << handle << ": ";
617 if (reply) alog << indent << *reply << dedent << endl;
618 else alog << "(none requested)" << endl;
619 }
620 } else {
621 err = waitForResponse(NULL, NULL);
622 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800623
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 return err;
625}
626
627void IPCThreadState::incStrongHandle(int32_t handle)
628{
629 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
630 mOut.writeInt32(BC_ACQUIRE);
631 mOut.writeInt32(handle);
632}
633
634void IPCThreadState::decStrongHandle(int32_t handle)
635{
636 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
637 mOut.writeInt32(BC_RELEASE);
638 mOut.writeInt32(handle);
639}
640
641void IPCThreadState::incWeakHandle(int32_t handle)
642{
643 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
644 mOut.writeInt32(BC_INCREFS);
645 mOut.writeInt32(handle);
646}
647
648void IPCThreadState::decWeakHandle(int32_t handle)
649{
650 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
651 mOut.writeInt32(BC_DECREFS);
652 mOut.writeInt32(handle);
653}
654
655status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
656{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800657#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700658 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
660 mOut.writeInt32(0); // xxx was thread priority
661 mOut.writeInt32(handle);
662 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800667 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
669#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800670
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800672#else
673 (void)handle;
674 ALOGE("%s(%d): Not supported\n", __func__, handle);
675 return INVALID_OPERATION;
676#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677}
678
679void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
680{
681#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800682 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683#endif
684 self()->mProcess->expungeHandle(handle, binder);
685}
686
687status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
688{
689 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
690 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000691 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 return NO_ERROR;
693}
694
695status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
696{
697 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
698 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000699 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 return NO_ERROR;
701}
702
703IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700704 : mProcess(ProcessState::self()),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700705 mStrictModePolicy(0),
706 mLastTransactionBinderFlags(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707{
708 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800709 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 mIn.setDataCapacity(256);
711 mOut.setDataCapacity(256);
712}
713
714IPCThreadState::~IPCThreadState()
715{
716}
717
718status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
719{
720 status_t err;
721 status_t statusBuffer;
722 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
723 if (err < NO_ERROR) return err;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 return waitForResponse(NULL, NULL);
726}
727
728status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
729{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100730 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 int32_t err;
732
733 while (1) {
734 if ((err=talkWithDriver()) < NO_ERROR) break;
735 err = mIn.errorCheck();
736 if (err < NO_ERROR) break;
737 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800738
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100739 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800740
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 IF_LOG_COMMANDS() {
742 alog << "Processing waitForResponse Command: "
743 << getReturnString(cmd) << endl;
744 }
745
746 switch (cmd) {
747 case BR_TRANSACTION_COMPLETE:
748 if (!reply && !acquireResult) goto finish;
749 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800750
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 case BR_DEAD_REPLY:
752 err = DEAD_OBJECT;
753 goto finish;
754
755 case BR_FAILED_REPLY:
756 err = FAILED_TRANSACTION;
757 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800758
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 case BR_ACQUIRE_RESULT:
760 {
Steve Block67263472012-01-09 18:35:44 +0000761 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 const int32_t result = mIn.readInt32();
763 if (!acquireResult) continue;
764 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
765 }
766 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800767
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768 case BR_REPLY:
769 {
770 binder_transaction_data tr;
771 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000772 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 if (err != NO_ERROR) goto finish;
774
775 if (reply) {
776 if ((tr.flags & TF_STATUS_CODE) == 0) {
777 reply->ipcSetDataReference(
778 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
779 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800780 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
781 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 freeBuffer, this);
783 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800784 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 freeBuffer(NULL,
786 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
787 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800788 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
789 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 }
791 } else {
792 freeBuffer(NULL,
793 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
794 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800795 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
796 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800797 continue;
798 }
799 }
800 goto finish;
801
802 default:
803 err = executeCommand(cmd);
804 if (err != NO_ERROR) goto finish;
805 break;
806 }
807 }
808
809finish:
810 if (err != NO_ERROR) {
811 if (acquireResult) *acquireResult = err;
812 if (reply) reply->setError(err);
813 mLastError = err;
814 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 return err;
817}
818
819status_t IPCThreadState::talkWithDriver(bool doReceive)
820{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100821 if (mProcess->mDriverFD <= 0) {
822 return -EBADF;
823 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800824
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800826
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827 // Is the read buffer empty?
828 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800829
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 // We don't want to write anything if we are still reading
831 // from data left in the input buffer and the caller
832 // has requested to read the next data.
833 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800836 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837
838 // This is what we'll read.
839 if (doReceive && needRead) {
840 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800841 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 } else {
843 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800844 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700846
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 IF_LOG_COMMANDS() {
848 TextOutput::Bundle _b(alog);
849 if (outAvail != 0) {
850 alog << "Sending commands to driver: " << indent;
851 const void* cmds = (const void*)bwr.write_buffer;
852 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
853 alog << HexDump(cmds, bwr.write_size) << endl;
854 while (cmds < end) cmds = printCommand(alog, cmds);
855 alog << dedent;
856 }
857 alog << "Size of receive buffer: " << bwr.read_size
858 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
859 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800860
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 // Return immediately if there is nothing to do.
862 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700863
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 bwr.write_consumed = 0;
865 bwr.read_consumed = 0;
866 status_t err;
867 do {
868 IF_LOG_COMMANDS() {
869 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
870 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700871#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
873 err = NO_ERROR;
874 else
875 err = -errno;
876#else
877 err = INVALID_OPERATION;
878#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100879 if (mProcess->mDriverFD <= 0) {
880 err = -EBADF;
881 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 IF_LOG_COMMANDS() {
883 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
884 }
885 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700886
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800888 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700890 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 }
892
893 if (err >= NO_ERROR) {
894 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800895 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 mOut.remove(0, bwr.write_consumed);
897 else
898 mOut.setDataSize(0);
899 }
900 if (bwr.read_consumed > 0) {
901 mIn.setDataSize(bwr.read_consumed);
902 mIn.setDataPosition(0);
903 }
904 IF_LOG_COMMANDS() {
905 TextOutput::Bundle _b(alog);
906 alog << "Remaining data size: " << mOut.dataSize() << endl;
907 alog << "Received commands from driver: " << indent;
908 const void* cmds = mIn.data();
909 const void* end = mIn.data() + mIn.dataSize();
910 alog << HexDump(cmds, mIn.dataSize()) << endl;
911 while (cmds < end) cmds = printReturnCommand(alog, cmds);
912 alog << dedent;
913 }
914 return NO_ERROR;
915 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800916
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 return err;
918}
919
920status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
921 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
922{
923 binder_transaction_data tr;
924
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800925 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 tr.target.handle = handle;
927 tr.code = code;
928 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +0400929 tr.cookie = 0;
930 tr.sender_pid = 0;
931 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800932
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 const status_t err = data.errorCheck();
934 if (err == NO_ERROR) {
935 tr.data_size = data.ipcDataSize();
936 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800937 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 tr.data.ptr.offsets = data.ipcObjects();
939 } else if (statusBuffer) {
940 tr.flags |= TF_STATUS_CODE;
941 *statusBuffer = err;
942 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -0800943 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800945 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 } else {
947 return (mLastError = err);
948 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800949
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 mOut.writeInt32(cmd);
951 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -0800952
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 return NO_ERROR;
954}
955
956sp<BBinder> the_context_object;
957
958void setTheContextObject(sp<BBinder> obj)
959{
960 the_context_object = obj;
961}
962
963status_t IPCThreadState::executeCommand(int32_t cmd)
964{
965 BBinder* obj;
966 RefBase::weakref_type* refs;
967 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800968
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100969 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 case BR_ERROR:
971 result = mIn.readInt32();
972 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800973
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974 case BR_OK:
975 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800976
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +0000978 refs = (RefBase::weakref_type*)mIn.readPointer();
979 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +0000980 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
982 refs, obj, refs->refBase());
983 obj->incStrong(mProcess.get());
984 IF_LOG_REMOTEREFS() {
985 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
986 obj->printRefs();
987 }
988 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000989 mOut.writePointer((uintptr_t)refs);
990 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800992
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +0000994 refs = (RefBase::weakref_type*)mIn.readPointer();
995 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +0000996 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800997 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
998 refs, obj, refs->refBase());
999 IF_LOG_REMOTEREFS() {
1000 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1001 obj->printRefs();
1002 }
1003 mPendingStrongDerefs.push(obj);
1004 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001005
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001007 refs = (RefBase::weakref_type*)mIn.readPointer();
1008 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 refs->incWeak(mProcess.get());
1010 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001011 mOut.writePointer((uintptr_t)refs);
1012 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001014
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001016 refs = (RefBase::weakref_type*)mIn.readPointer();
1017 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 // NOTE: This assertion is not valid, because the object may no
1019 // longer exist (thus the (BBinder*)cast above resulting in a different
1020 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001021 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1023 // refs, obj, refs->refBase());
1024 mPendingWeakDerefs.push(refs);
1025 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001026
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001028 refs = (RefBase::weakref_type*)mIn.readPointer();
1029 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001030
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 {
1032 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001033 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1035 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 mOut.writeInt32(BC_ACQUIRE_RESULT);
1038 mOut.writeInt32((int32_t)success);
1039 }
1040 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 case BR_TRANSACTION:
1043 {
1044 binder_transaction_data tr;
1045 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001046 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 "Not enough command data for brTRANSACTION");
1048 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 Parcel buffer;
1051 buffer.ipcSetDataReference(
1052 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1053 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001054 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1055 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 const pid_t origPid = mCallingPid;
1058 const uid_t origUid = mCallingUid;
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001059 const int32_t origStrictModePolicy = mStrictModePolicy;
1060 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 mCallingPid = tr.sender_pid;
1063 mCallingUid = tr.sender_euid;
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001064 mLastTransactionBinderFlags = tr.flags;
1065
Steve Blocka19954a2012-01-04 20:05:49 +00001066 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001067
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 Parcel reply;
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001069 status_t error;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 IF_LOG_TRANSACTIONS() {
1071 TextOutput::Bundle _b(alog);
1072 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1073 << " / obj " << tr.target.ptr << " / code "
1074 << TypeCode(tr.code) << ": " << indent << buffer
1075 << dedent << endl
1076 << "Data addr = "
1077 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1078 << ", offsets addr="
1079 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1080 }
1081 if (tr.target.ptr) {
Dianne Hackbornc1114612016-03-21 10:36:54 -07001082 // We only have a weak reference on the target object, so we must first try to
1083 // safely acquire a strong reference before doing anything else with it.
1084 if (reinterpret_cast<RefBase::weakref_type*>(
1085 tr.target.ptr)->attemptIncStrong(this)) {
1086 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1087 &reply, tr.flags);
1088 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
1089 } else {
1090 error = UNKNOWN_TRANSACTION;
1091 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 } else {
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001094 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001096
Steve Blocka19954a2012-01-04 20:05:49 +00001097 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001099
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 if ((tr.flags & TF_ONE_WAY) == 0) {
1101 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001102 if (error < NO_ERROR) reply.setError(error);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 sendReply(reply, 0);
1104 } else {
1105 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1106 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 mCallingPid = origPid;
1109 mCallingUid = origUid;
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001110 mStrictModePolicy = origStrictModePolicy;
1111 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate440fd872010-03-18 17:55:03 -07001112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 IF_LOG_TRANSACTIONS() {
1114 TextOutput::Bundle _b(alog);
1115 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1116 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1117 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 }
1120 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 case BR_DEAD_BINDER:
1123 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001124 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 proxy->sendObituary();
1126 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001127 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1131 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001132 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133 proxy->getWeakRefs()->decWeak(proxy);
1134 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 case BR_FINISHED:
1137 result = TIMED_OUT;
1138 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001139
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 case BR_NOOP:
1141 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 case BR_SPAWN_LOOPER:
1144 mProcess->spawnPooledThread(false);
1145 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 default:
liangweikanga43ee152016-10-25 16:37:54 +08001148 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 result = UNKNOWN_ERROR;
1150 break;
1151 }
1152
1153 if (result != NO_ERROR) {
1154 mLastError = result;
1155 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 return result;
1158}
1159
1160void IPCThreadState::threadDestructor(void *st)
1161{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001162 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1163 if (self) {
1164 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001165#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001166 if (self->mProcess->mDriverFD > 0) {
1167 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001170 delete self;
1171 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172}
1173
1174
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001175void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1176 size_t /*dataSize*/,
1177 const binder_size_t* /*objects*/,
1178 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179{
Steve Blocka19954a2012-01-04 20:05:49 +00001180 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 IF_LOG_COMMANDS() {
1182 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1183 }
Steve Block67263472012-01-09 18:35:44 +00001184 ALOG_ASSERT(data != NULL, "Called with NULL data");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 if (parcel != NULL) parcel->closeFileDescriptors();
1186 IPCThreadState* state = self();
1187 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001188 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189}
1190
1191}; // namespace android