blob: d0cd8f2f22d24a88ea26b32b5123f3673861fb13 [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()) {
468 size_t numPending = mPendingWeakDerefs.size();
469 if (numPending > 0) {
470 for (size_t i = 0; i < numPending; i++) {
471 RefBase::weakref_type* refs = mPendingWeakDerefs[i];
472 refs->decWeak(mProcess.get());
473 }
474 mPendingWeakDerefs.clear();
475 }
476
477 numPending = mPendingStrongDerefs.size();
478 if (numPending > 0) {
479 for (size_t i = 0; i < numPending; i++) {
480 BBinder* obj = mPendingStrongDerefs[i];
481 obj->decStrong(mProcess.get());
482 }
483 mPendingStrongDerefs.clear();
484 }
485 }
486}
487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488void IPCThreadState::joinThreadPool(bool isMain)
489{
490 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
491
492 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 status_t result;
495 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700496 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700498 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800499
Todd Poynor8d96cab2013-06-25 19:12:18 -0700500 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
501 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700502 mProcess->mDriverFD, result);
503 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800505
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506 // Let this thread exit the thread pool if it is no longer
507 // needed and it is not the main process thread.
508 if(result == TIMED_OUT && !isMain) {
509 break;
510 }
511 } while (result != -ECONNREFUSED && result != -EBADF);
512
Wei Wangc7341432016-10-19 10:23:59 -0700513 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
514 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800515
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 mOut.writeInt32(BC_EXIT_LOOPER);
517 talkWithDriver(false);
518}
519
Todd Poynor8d96cab2013-06-25 19:12:18 -0700520int IPCThreadState::setupPolling(int* fd)
521{
522 if (mProcess->mDriverFD <= 0) {
523 return -EBADF;
524 }
525
526 mOut.writeInt32(BC_ENTER_LOOPER);
527 *fd = mProcess->mDriverFD;
528 return 0;
529}
530
531status_t IPCThreadState::handlePolledCommands()
532{
533 status_t result;
534
535 do {
536 result = getAndExecuteCommand();
537 } while (mIn.dataPosition() < mIn.dataSize());
538
539 processPendingDerefs();
540 flushCommands();
541 return result;
542}
543
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800544void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545{
Steve Blocka19954a2012-01-04 20:05:49 +0000546 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 flushCommands();
548 int fd = mProcess->mDriverFD;
549 mProcess->mDriverFD = -1;
550 close(fd);
551 //kill(getpid(), SIGKILL);
552}
553
554status_t IPCThreadState::transact(int32_t handle,
555 uint32_t code, const Parcel& data,
556 Parcel* reply, uint32_t flags)
557{
558 status_t err = data.errorCheck();
559
560 flags |= TF_ACCEPT_FDS;
561
562 IF_LOG_TRANSACTIONS() {
563 TextOutput::Bundle _b(alog);
564 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
565 << handle << " / code " << TypeCode(code) << ": "
566 << indent << data << dedent << endl;
567 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800568
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 if (err == NO_ERROR) {
570 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
571 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
572 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);
573 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800574
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 if (err != NO_ERROR) {
576 if (reply) reply->setError(err);
577 return (mLastError = err);
578 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700581 #if 0
582 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000583 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700584 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000585 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700586 }
587 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 if (reply) {
589 err = waitForResponse(reply);
590 } else {
591 Parcel fakeReply;
592 err = waitForResponse(&fakeReply);
593 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700594 #if 0
595 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000596 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700597 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000598 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700599 }
600 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800601
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 IF_LOG_TRANSACTIONS() {
603 TextOutput::Bundle _b(alog);
604 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
605 << handle << ": ";
606 if (reply) alog << indent << *reply << dedent << endl;
607 else alog << "(none requested)" << endl;
608 }
609 } else {
610 err = waitForResponse(NULL, NULL);
611 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800612
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 return err;
614}
615
616void IPCThreadState::incStrongHandle(int32_t handle)
617{
618 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
619 mOut.writeInt32(BC_ACQUIRE);
620 mOut.writeInt32(handle);
621}
622
623void IPCThreadState::decStrongHandle(int32_t handle)
624{
625 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
626 mOut.writeInt32(BC_RELEASE);
627 mOut.writeInt32(handle);
628}
629
630void IPCThreadState::incWeakHandle(int32_t handle)
631{
632 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
633 mOut.writeInt32(BC_INCREFS);
634 mOut.writeInt32(handle);
635}
636
637void IPCThreadState::decWeakHandle(int32_t handle)
638{
639 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
640 mOut.writeInt32(BC_DECREFS);
641 mOut.writeInt32(handle);
642}
643
644status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
645{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800646#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700647 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
649 mOut.writeInt32(0); // xxx was thread priority
650 mOut.writeInt32(handle);
651 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800652
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800654
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800656 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
658#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800659
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800661#else
662 (void)handle;
663 ALOGE("%s(%d): Not supported\n", __func__, handle);
664 return INVALID_OPERATION;
665#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666}
667
668void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
669{
670#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800671 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672#endif
673 self()->mProcess->expungeHandle(handle, binder);
674}
675
676status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
677{
678 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
679 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000680 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 return NO_ERROR;
682}
683
684status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
685{
686 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
687 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000688 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 return NO_ERROR;
690}
691
692IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700693 : mProcess(ProcessState::self()),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700694 mStrictModePolicy(0),
695 mLastTransactionBinderFlags(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696{
697 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800698 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 mIn.setDataCapacity(256);
700 mOut.setDataCapacity(256);
701}
702
703IPCThreadState::~IPCThreadState()
704{
705}
706
707status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
708{
709 status_t err;
710 status_t statusBuffer;
711 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
712 if (err < NO_ERROR) return err;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 return waitForResponse(NULL, NULL);
715}
716
717status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
718{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100719 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 int32_t err;
721
722 while (1) {
723 if ((err=talkWithDriver()) < NO_ERROR) break;
724 err = mIn.errorCheck();
725 if (err < NO_ERROR) break;
726 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800727
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100728 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800729
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 IF_LOG_COMMANDS() {
731 alog << "Processing waitForResponse Command: "
732 << getReturnString(cmd) << endl;
733 }
734
735 switch (cmd) {
736 case BR_TRANSACTION_COMPLETE:
737 if (!reply && !acquireResult) goto finish;
738 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800739
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 case BR_DEAD_REPLY:
741 err = DEAD_OBJECT;
742 goto finish;
743
744 case BR_FAILED_REPLY:
745 err = FAILED_TRANSACTION;
746 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800747
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748 case BR_ACQUIRE_RESULT:
749 {
Steve Block67263472012-01-09 18:35:44 +0000750 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 const int32_t result = mIn.readInt32();
752 if (!acquireResult) continue;
753 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
754 }
755 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800756
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 case BR_REPLY:
758 {
759 binder_transaction_data tr;
760 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000761 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 if (err != NO_ERROR) goto finish;
763
764 if (reply) {
765 if ((tr.flags & TF_STATUS_CODE) == 0) {
766 reply->ipcSetDataReference(
767 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
768 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800769 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
770 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771 freeBuffer, this);
772 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800773 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 freeBuffer(NULL,
775 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
776 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800777 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
778 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 }
780 } else {
781 freeBuffer(NULL,
782 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
783 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800784 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
785 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 continue;
787 }
788 }
789 goto finish;
790
791 default:
792 err = executeCommand(cmd);
793 if (err != NO_ERROR) goto finish;
794 break;
795 }
796 }
797
798finish:
799 if (err != NO_ERROR) {
800 if (acquireResult) *acquireResult = err;
801 if (reply) reply->setError(err);
802 mLastError = err;
803 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800804
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 return err;
806}
807
808status_t IPCThreadState::talkWithDriver(bool doReceive)
809{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100810 if (mProcess->mDriverFD <= 0) {
811 return -EBADF;
812 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800813
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 // Is the read buffer empty?
817 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 // We don't want to write anything if we are still reading
820 // from data left in the input buffer and the caller
821 // has requested to read the next data.
822 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800823
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800825 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826
827 // This is what we'll read.
828 if (doReceive && needRead) {
829 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800830 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 } else {
832 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800833 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700835
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836 IF_LOG_COMMANDS() {
837 TextOutput::Bundle _b(alog);
838 if (outAvail != 0) {
839 alog << "Sending commands to driver: " << indent;
840 const void* cmds = (const void*)bwr.write_buffer;
841 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
842 alog << HexDump(cmds, bwr.write_size) << endl;
843 while (cmds < end) cmds = printCommand(alog, cmds);
844 alog << dedent;
845 }
846 alog << "Size of receive buffer: " << bwr.read_size
847 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
848 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800849
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 // Return immediately if there is nothing to do.
851 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700852
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853 bwr.write_consumed = 0;
854 bwr.read_consumed = 0;
855 status_t err;
856 do {
857 IF_LOG_COMMANDS() {
858 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
859 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700860#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
862 err = NO_ERROR;
863 else
864 err = -errno;
865#else
866 err = INVALID_OPERATION;
867#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100868 if (mProcess->mDriverFD <= 0) {
869 err = -EBADF;
870 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871 IF_LOG_COMMANDS() {
872 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
873 }
874 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700875
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800877 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700879 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 }
881
882 if (err >= NO_ERROR) {
883 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800884 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 mOut.remove(0, bwr.write_consumed);
886 else
887 mOut.setDataSize(0);
888 }
889 if (bwr.read_consumed > 0) {
890 mIn.setDataSize(bwr.read_consumed);
891 mIn.setDataPosition(0);
892 }
893 IF_LOG_COMMANDS() {
894 TextOutput::Bundle _b(alog);
895 alog << "Remaining data size: " << mOut.dataSize() << endl;
896 alog << "Received commands from driver: " << indent;
897 const void* cmds = mIn.data();
898 const void* end = mIn.data() + mIn.dataSize();
899 alog << HexDump(cmds, mIn.dataSize()) << endl;
900 while (cmds < end) cmds = printReturnCommand(alog, cmds);
901 alog << dedent;
902 }
903 return NO_ERROR;
904 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800905
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 return err;
907}
908
909status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
910 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
911{
912 binder_transaction_data tr;
913
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800914 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 tr.target.handle = handle;
916 tr.code = code;
917 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +0400918 tr.cookie = 0;
919 tr.sender_pid = 0;
920 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800921
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 const status_t err = data.errorCheck();
923 if (err == NO_ERROR) {
924 tr.data_size = data.ipcDataSize();
925 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800926 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 tr.data.ptr.offsets = data.ipcObjects();
928 } else if (statusBuffer) {
929 tr.flags |= TF_STATUS_CODE;
930 *statusBuffer = err;
931 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -0800932 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800934 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 } else {
936 return (mLastError = err);
937 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800938
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 mOut.writeInt32(cmd);
940 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -0800941
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 return NO_ERROR;
943}
944
945sp<BBinder> the_context_object;
946
947void setTheContextObject(sp<BBinder> obj)
948{
949 the_context_object = obj;
950}
951
952status_t IPCThreadState::executeCommand(int32_t cmd)
953{
954 BBinder* obj;
955 RefBase::weakref_type* refs;
956 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800957
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100958 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 case BR_ERROR:
960 result = mIn.readInt32();
961 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800962
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963 case BR_OK:
964 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800965
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +0000967 refs = (RefBase::weakref_type*)mIn.readPointer();
968 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +0000969 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
971 refs, obj, refs->refBase());
972 obj->incStrong(mProcess.get());
973 IF_LOG_REMOTEREFS() {
974 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
975 obj->printRefs();
976 }
977 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000978 mOut.writePointer((uintptr_t)refs);
979 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800981
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +0000983 refs = (RefBase::weakref_type*)mIn.readPointer();
984 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +0000985 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
987 refs, obj, refs->refBase());
988 IF_LOG_REMOTEREFS() {
989 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
990 obj->printRefs();
991 }
992 mPendingStrongDerefs.push(obj);
993 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800994
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +0000996 refs = (RefBase::weakref_type*)mIn.readPointer();
997 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 refs->incWeak(mProcess.get());
999 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001000 mOut.writePointer((uintptr_t)refs);
1001 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001003
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001005 refs = (RefBase::weakref_type*)mIn.readPointer();
1006 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 // NOTE: This assertion is not valid, because the object may no
1008 // longer exist (thus the (BBinder*)cast above resulting in a different
1009 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001010 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1012 // refs, obj, refs->refBase());
1013 mPendingWeakDerefs.push(refs);
1014 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001015
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001017 refs = (RefBase::weakref_type*)mIn.readPointer();
1018 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001019
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 {
1021 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001022 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1024 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001025
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 mOut.writeInt32(BC_ACQUIRE_RESULT);
1027 mOut.writeInt32((int32_t)success);
1028 }
1029 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001030
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 case BR_TRANSACTION:
1032 {
1033 binder_transaction_data tr;
1034 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001035 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 "Not enough command data for brTRANSACTION");
1037 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 Parcel buffer;
1040 buffer.ipcSetDataReference(
1041 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1042 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001043 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1044 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 const pid_t origPid = mCallingPid;
1047 const uid_t origUid = mCallingUid;
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001048 const int32_t origStrictModePolicy = mStrictModePolicy;
1049 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 mCallingPid = tr.sender_pid;
1052 mCallingUid = tr.sender_euid;
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001053 mLastTransactionBinderFlags = tr.flags;
1054
Steve Blocka19954a2012-01-04 20:05:49 +00001055 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 Parcel reply;
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001058 status_t error;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 IF_LOG_TRANSACTIONS() {
1060 TextOutput::Bundle _b(alog);
1061 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1062 << " / obj " << tr.target.ptr << " / code "
1063 << TypeCode(tr.code) << ": " << indent << buffer
1064 << dedent << endl
1065 << "Data addr = "
1066 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1067 << ", offsets addr="
1068 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1069 }
1070 if (tr.target.ptr) {
Dianne Hackbornc1114612016-03-21 10:36:54 -07001071 // We only have a weak reference on the target object, so we must first try to
1072 // safely acquire a strong reference before doing anything else with it.
1073 if (reinterpret_cast<RefBase::weakref_type*>(
1074 tr.target.ptr)->attemptIncStrong(this)) {
1075 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1076 &reply, tr.flags);
1077 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
1078 } else {
1079 error = UNKNOWN_TRANSACTION;
1080 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 } else {
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001083 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001085
Steve Blocka19954a2012-01-04 20:05:49 +00001086 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 if ((tr.flags & TF_ONE_WAY) == 0) {
1090 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001091 if (error < NO_ERROR) reply.setError(error);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 sendReply(reply, 0);
1093 } else {
1094 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1095 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 mCallingPid = origPid;
1098 mCallingUid = origUid;
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001099 mStrictModePolicy = origStrictModePolicy;
1100 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate440fd872010-03-18 17:55:03 -07001101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 IF_LOG_TRANSACTIONS() {
1103 TextOutput::Bundle _b(alog);
1104 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1105 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1106 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 }
1109 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 case BR_DEAD_BINDER:
1112 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001113 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 proxy->sendObituary();
1115 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001116 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1120 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001121 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 proxy->getWeakRefs()->decWeak(proxy);
1123 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 case BR_FINISHED:
1126 result = TIMED_OUT;
1127 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 case BR_NOOP:
1130 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 case BR_SPAWN_LOOPER:
1133 mProcess->spawnPooledThread(false);
1134 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 default:
liangweikanga43ee152016-10-25 16:37:54 +08001137 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 result = UNKNOWN_ERROR;
1139 break;
1140 }
1141
1142 if (result != NO_ERROR) {
1143 mLastError = result;
1144 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 return result;
1147}
1148
1149void IPCThreadState::threadDestructor(void *st)
1150{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001151 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1152 if (self) {
1153 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001154#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001155 if (self->mProcess->mDriverFD > 0) {
1156 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001159 delete self;
1160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161}
1162
1163
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001164void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1165 size_t /*dataSize*/,
1166 const binder_size_t* /*objects*/,
1167 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168{
Steve Blocka19954a2012-01-04 20:05:49 +00001169 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 IF_LOG_COMMANDS() {
1171 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1172 }
Steve Block67263472012-01-09 18:35:44 +00001173 ALOG_ASSERT(data != NULL, "Called with NULL data");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174 if (parcel != NULL) parcel->closeFileDescriptors();
1175 IPCThreadState* state = self();
1176 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001177 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178}
1179
1180}; // namespace android