blob: 33ec65f10e2c1c9dc21735b5b52d97f46f31fe36 [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");
Yi Kongfdd8da92018-06-07 17:52:27 -0700292 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800293 }
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));
Yi Kongfdd8da92018-06-07 17:52:27 -0700302 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 }
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 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700317 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800318}
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;
Yi Kongfdd8da92018-06-07 17:52:27 -0700329 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 }
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
Martijn Coenen2b631742017-05-05 11:16:59 -0700341bool IPCThreadState::backgroundSchedulingDisabled()
342{
343 return gDisableBackgroundScheduling;
344}
345
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346sp<ProcessState> IPCThreadState::process()
347{
348 return mProcess;
349}
350
351status_t IPCThreadState::clearLastError()
352{
353 const status_t err = mLastError;
354 mLastError = NO_ERROR;
355 return err;
356}
357
Dan Stoza9c634fd2014-11-26 12:23:23 -0800358pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359{
360 return mCallingPid;
361}
362
Dan Stoza9c634fd2014-11-26 12:23:23 -0800363uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364{
365 return mCallingUid;
366}
367
368int64_t IPCThreadState::clearCallingIdentity()
369{
370 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
371 clearCaller();
372 return token;
373}
374
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700375void IPCThreadState::setStrictModePolicy(int32_t policy)
376{
377 mStrictModePolicy = policy;
378}
379
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700380int32_t IPCThreadState::getStrictModePolicy() const
381{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700382 return mStrictModePolicy;
383}
384
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700385void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
386{
387 mLastTransactionBinderFlags = flags;
388}
389
390int32_t IPCThreadState::getLastTransactionBinderFlags() const
391{
392 return mLastTransactionBinderFlags;
393}
394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395void IPCThreadState::restoreCallingIdentity(int64_t token)
396{
397 mCallingUid = (int)(token>>32);
398 mCallingPid = (int)token;
399}
400
401void IPCThreadState::clearCaller()
402{
Marco Nelissend43b1942009-07-17 07:59:17 -0700403 mCallingPid = getpid();
404 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405}
406
407void IPCThreadState::flushCommands()
408{
409 if (mProcess->mDriverFD <= 0)
410 return;
411 talkWithDriver(false);
412}
413
Wale Ogunwale376b8222015-04-13 16:16:10 -0700414void IPCThreadState::blockUntilThreadAvailable()
415{
416 pthread_mutex_lock(&mProcess->mThreadCountLock);
417 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700418 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
419 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
420 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700421 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
422 }
423 pthread_mutex_unlock(&mProcess->mThreadCountLock);
424}
425
Todd Poynor8d96cab2013-06-25 19:12:18 -0700426status_t IPCThreadState::getAndExecuteCommand()
427{
428 status_t result;
429 int32_t cmd;
430
431 result = talkWithDriver();
432 if (result >= NO_ERROR) {
433 size_t IN = mIn.dataAvail();
434 if (IN < sizeof(int32_t)) return result;
435 cmd = mIn.readInt32();
436 IF_LOG_COMMANDS() {
437 alog << "Processing top-level Command: "
438 << getReturnString(cmd) << endl;
439 }
440
Wale Ogunwale376b8222015-04-13 16:16:10 -0700441 pthread_mutex_lock(&mProcess->mThreadCountLock);
442 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700443 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
444 mProcess->mStarvationStartTimeMs == 0) {
445 mProcess->mStarvationStartTimeMs = uptimeMillis();
446 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700447 pthread_mutex_unlock(&mProcess->mThreadCountLock);
448
Todd Poynor8d96cab2013-06-25 19:12:18 -0700449 result = executeCommand(cmd);
450
Wale Ogunwale376b8222015-04-13 16:16:10 -0700451 pthread_mutex_lock(&mProcess->mThreadCountLock);
452 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700453 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
454 mProcess->mStarvationStartTimeMs != 0) {
455 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
456 if (starvationTimeMs > 100) {
457 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
458 mProcess->mMaxThreads, starvationTimeMs);
459 }
460 mProcess->mStarvationStartTimeMs = 0;
461 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700462 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
463 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700464 }
465
466 return result;
467}
468
469// When we've cleared the incoming command queue, process any pending derefs
470void IPCThreadState::processPendingDerefs()
471{
472 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200473 /*
474 * The decWeak()/decStrong() calls may cause a destructor to run,
475 * which in turn could have initiated an outgoing transaction,
476 * which in turn could cause us to add to the pending refs
477 * vectors; so instead of simply iterating, loop until they're empty.
478 *
479 * We do this in an outer loop, because calling decStrong()
480 * may result in something being added to mPendingWeakDerefs,
481 * which could be delayed until the next incoming command
482 * from the driver if we don't process it now.
483 */
484 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
485 while (mPendingWeakDerefs.size() > 0) {
486 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
487 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700488 refs->decWeak(mProcess.get());
489 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700490
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200491 if (mPendingStrongDerefs.size() > 0) {
492 // We don't use while() here because we don't want to re-order
493 // strong and weak decs at all; if this decStrong() causes both a
494 // decWeak() and a decStrong() to be queued, we want to process
495 // the decWeak() first.
496 BBinder* obj = mPendingStrongDerefs[0];
497 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700498 obj->decStrong(mProcess.get());
499 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700500 }
501 }
502}
503
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504void IPCThreadState::joinThreadPool(bool isMain)
505{
506 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
507
508 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800509
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 status_t result;
511 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700512 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700514 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800515
Todd Poynor8d96cab2013-06-25 19:12:18 -0700516 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
517 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700518 mProcess->mDriverFD, result);
519 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800521
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 // Let this thread exit the thread pool if it is no longer
523 // needed and it is not the main process thread.
524 if(result == TIMED_OUT && !isMain) {
525 break;
526 }
527 } while (result != -ECONNREFUSED && result != -EBADF);
528
Wei Wangc7341432016-10-19 10:23:59 -0700529 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
530 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800531
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 mOut.writeInt32(BC_EXIT_LOOPER);
533 talkWithDriver(false);
534}
535
Todd Poynor8d96cab2013-06-25 19:12:18 -0700536int IPCThreadState::setupPolling(int* fd)
537{
538 if (mProcess->mDriverFD <= 0) {
539 return -EBADF;
540 }
541
542 mOut.writeInt32(BC_ENTER_LOOPER);
543 *fd = mProcess->mDriverFD;
544 return 0;
545}
546
547status_t IPCThreadState::handlePolledCommands()
548{
549 status_t result;
550
551 do {
552 result = getAndExecuteCommand();
553 } while (mIn.dataPosition() < mIn.dataSize());
554
555 processPendingDerefs();
556 flushCommands();
557 return result;
558}
559
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800560void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561{
Steve Blocka19954a2012-01-04 20:05:49 +0000562 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 flushCommands();
564 int fd = mProcess->mDriverFD;
565 mProcess->mDriverFD = -1;
566 close(fd);
567 //kill(getpid(), SIGKILL);
568}
569
570status_t IPCThreadState::transact(int32_t handle,
571 uint32_t code, const Parcel& data,
572 Parcel* reply, uint32_t flags)
573{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800574 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575
576 flags |= TF_ACCEPT_FDS;
577
578 IF_LOG_TRANSACTIONS() {
579 TextOutput::Bundle _b(alog);
580 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
581 << handle << " / code " << TypeCode(code) << ": "
582 << indent << data << dedent << endl;
583 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800584
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800585 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
586 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700587 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800588
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 if (err != NO_ERROR) {
590 if (reply) reply->setError(err);
591 return (mLastError = err);
592 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800593
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700595 #if 0
596 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000597 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700598 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000599 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700600 }
601 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 if (reply) {
603 err = waitForResponse(reply);
604 } else {
605 Parcel fakeReply;
606 err = waitForResponse(&fakeReply);
607 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700608 #if 0
609 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000610 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700611 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000612 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700613 }
614 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800615
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 IF_LOG_TRANSACTIONS() {
617 TextOutput::Bundle _b(alog);
618 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
619 << handle << ": ";
620 if (reply) alog << indent << *reply << dedent << endl;
621 else alog << "(none requested)" << endl;
622 }
623 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700624 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800626
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 return err;
628}
629
630void IPCThreadState::incStrongHandle(int32_t handle)
631{
632 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
633 mOut.writeInt32(BC_ACQUIRE);
634 mOut.writeInt32(handle);
635}
636
637void IPCThreadState::decStrongHandle(int32_t handle)
638{
639 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
640 mOut.writeInt32(BC_RELEASE);
641 mOut.writeInt32(handle);
642}
643
644void IPCThreadState::incWeakHandle(int32_t handle)
645{
646 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
647 mOut.writeInt32(BC_INCREFS);
648 mOut.writeInt32(handle);
649}
650
651void IPCThreadState::decWeakHandle(int32_t handle)
652{
653 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
654 mOut.writeInt32(BC_DECREFS);
655 mOut.writeInt32(handle);
656}
657
658status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
659{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800660#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700661 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
663 mOut.writeInt32(0); // xxx was thread priority
664 mOut.writeInt32(handle);
665 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800668
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800670 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
672#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800673
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800675#else
676 (void)handle;
677 ALOGE("%s(%d): Not supported\n", __func__, handle);
678 return INVALID_OPERATION;
679#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680}
681
682void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
683{
684#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800685 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700687 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688}
689
690status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
691{
692 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
693 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000694 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 return NO_ERROR;
696}
697
698status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
699{
700 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
701 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000702 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 return NO_ERROR;
704}
705
706IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700707 : mProcess(ProcessState::self()),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700708 mStrictModePolicy(0),
709 mLastTransactionBinderFlags(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710{
711 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800712 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 mIn.setDataCapacity(256);
714 mOut.setDataCapacity(256);
715}
716
717IPCThreadState::~IPCThreadState()
718{
719}
720
Martijn Coenenea0090a2017-11-02 18:54:40 +0000721status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
722{
723 status_t err;
724 status_t statusBuffer;
725 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
726 if (err < NO_ERROR) return err;
727
Yi Kongfdd8da92018-06-07 17:52:27 -0700728 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000729}
730
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
732{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100733 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 int32_t err;
735
736 while (1) {
737 if ((err=talkWithDriver()) < NO_ERROR) break;
738 err = mIn.errorCheck();
739 if (err < NO_ERROR) break;
740 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800741
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100742 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 IF_LOG_COMMANDS() {
745 alog << "Processing waitForResponse Command: "
746 << getReturnString(cmd) << endl;
747 }
748
749 switch (cmd) {
750 case BR_TRANSACTION_COMPLETE:
751 if (!reply && !acquireResult) goto finish;
752 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800753
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 case BR_DEAD_REPLY:
755 err = DEAD_OBJECT;
756 goto finish;
757
758 case BR_FAILED_REPLY:
759 err = FAILED_TRANSACTION;
760 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800761
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 case BR_ACQUIRE_RESULT:
763 {
Steve Block67263472012-01-09 18:35:44 +0000764 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 const int32_t result = mIn.readInt32();
766 if (!acquireResult) continue;
767 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
768 }
769 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800770
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771 case BR_REPLY:
772 {
773 binder_transaction_data tr;
774 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000775 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 if (err != NO_ERROR) goto finish;
777
778 if (reply) {
779 if ((tr.flags & TF_STATUS_CODE) == 0) {
780 reply->ipcSetDataReference(
781 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
782 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800783 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
784 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 freeBuffer, this);
786 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800787 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700788 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
790 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800791 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
792 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 }
794 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700795 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
797 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800798 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
799 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800 continue;
801 }
802 }
803 goto finish;
804
805 default:
806 err = executeCommand(cmd);
807 if (err != NO_ERROR) goto finish;
808 break;
809 }
810 }
811
812finish:
813 if (err != NO_ERROR) {
814 if (acquireResult) *acquireResult = err;
815 if (reply) reply->setError(err);
816 mLastError = err;
817 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 return err;
820}
821
822status_t IPCThreadState::talkWithDriver(bool doReceive)
823{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100824 if (mProcess->mDriverFD <= 0) {
825 return -EBADF;
826 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800827
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800829
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 // Is the read buffer empty?
831 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833 // We don't want to write anything if we are still reading
834 // from data left in the input buffer and the caller
835 // has requested to read the next data.
836 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800837
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800839 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840
841 // This is what we'll read.
842 if (doReceive && needRead) {
843 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800844 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 } else {
846 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800847 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700849
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 IF_LOG_COMMANDS() {
851 TextOutput::Bundle _b(alog);
852 if (outAvail != 0) {
853 alog << "Sending commands to driver: " << indent;
854 const void* cmds = (const void*)bwr.write_buffer;
855 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
856 alog << HexDump(cmds, bwr.write_size) << endl;
857 while (cmds < end) cmds = printCommand(alog, cmds);
858 alog << dedent;
859 }
860 alog << "Size of receive buffer: " << bwr.read_size
861 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
862 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800863
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 // Return immediately if there is nothing to do.
865 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700866
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 bwr.write_consumed = 0;
868 bwr.read_consumed = 0;
869 status_t err;
870 do {
871 IF_LOG_COMMANDS() {
872 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
873 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700874#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
876 err = NO_ERROR;
877 else
878 err = -errno;
879#else
880 err = INVALID_OPERATION;
881#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100882 if (mProcess->mDriverFD <= 0) {
883 err = -EBADF;
884 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 IF_LOG_COMMANDS() {
886 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
887 }
888 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700889
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800891 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700893 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 }
895
896 if (err >= NO_ERROR) {
897 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800898 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 mOut.remove(0, bwr.write_consumed);
900 else
901 mOut.setDataSize(0);
902 }
903 if (bwr.read_consumed > 0) {
904 mIn.setDataSize(bwr.read_consumed);
905 mIn.setDataPosition(0);
906 }
907 IF_LOG_COMMANDS() {
908 TextOutput::Bundle _b(alog);
909 alog << "Remaining data size: " << mOut.dataSize() << endl;
910 alog << "Received commands from driver: " << indent;
911 const void* cmds = mIn.data();
912 const void* end = mIn.data() + mIn.dataSize();
913 alog << HexDump(cmds, mIn.dataSize()) << endl;
914 while (cmds < end) cmds = printReturnCommand(alog, cmds);
915 alog << dedent;
916 }
917 return NO_ERROR;
918 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800919
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 return err;
921}
922
923status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
924 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
925{
926 binder_transaction_data tr;
927
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800928 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 tr.target.handle = handle;
930 tr.code = code;
931 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +0400932 tr.cookie = 0;
933 tr.sender_pid = 0;
934 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800935
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 const status_t err = data.errorCheck();
937 if (err == NO_ERROR) {
938 tr.data_size = data.ipcDataSize();
939 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800940 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 tr.data.ptr.offsets = data.ipcObjects();
942 } else if (statusBuffer) {
943 tr.flags |= TF_STATUS_CODE;
944 *statusBuffer = err;
945 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -0800946 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800948 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 } else {
950 return (mLastError = err);
951 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800952
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 mOut.writeInt32(cmd);
954 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -0800955
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956 return NO_ERROR;
957}
958
959sp<BBinder> the_context_object;
960
961void setTheContextObject(sp<BBinder> obj)
962{
963 the_context_object = obj;
964}
965
966status_t IPCThreadState::executeCommand(int32_t cmd)
967{
968 BBinder* obj;
969 RefBase::weakref_type* refs;
970 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800971
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100972 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 case BR_ERROR:
974 result = mIn.readInt32();
975 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800976
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 case BR_OK:
978 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800979
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +0000981 refs = (RefBase::weakref_type*)mIn.readPointer();
982 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +0000983 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
985 refs, obj, refs->refBase());
986 obj->incStrong(mProcess.get());
987 IF_LOG_REMOTEREFS() {
988 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
989 obj->printRefs();
990 }
991 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000992 mOut.writePointer((uintptr_t)refs);
993 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800995
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +0000997 refs = (RefBase::weakref_type*)mIn.readPointer();
998 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +0000999 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1001 refs, obj, refs->refBase());
1002 IF_LOG_REMOTEREFS() {
1003 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1004 obj->printRefs();
1005 }
1006 mPendingStrongDerefs.push(obj);
1007 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001008
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001010 refs = (RefBase::weakref_type*)mIn.readPointer();
1011 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 refs->incWeak(mProcess.get());
1013 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001014 mOut.writePointer((uintptr_t)refs);
1015 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001017
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001019 refs = (RefBase::weakref_type*)mIn.readPointer();
1020 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 // NOTE: This assertion is not valid, because the object may no
1022 // longer exist (thus the (BBinder*)cast above resulting in a different
1023 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001024 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1026 // refs, obj, refs->refBase());
1027 mPendingWeakDerefs.push(refs);
1028 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001029
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001031 refs = (RefBase::weakref_type*)mIn.readPointer();
1032 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001033
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 {
1035 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001036 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1038 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 mOut.writeInt32(BC_ACQUIRE_RESULT);
1041 mOut.writeInt32((int32_t)success);
1042 }
1043 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 case BR_TRANSACTION:
1046 {
1047 binder_transaction_data tr;
1048 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001049 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 "Not enough command data for brTRANSACTION");
1051 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001052
Martijn Coenenea0090a2017-11-02 18:54:40 +00001053 Parcel buffer;
1054 buffer.ipcSetDataReference(
1055 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1056 tr.data_size,
1057 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1058 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1059
1060 const pid_t origPid = mCallingPid;
1061 const uid_t origUid = mCallingUid;
1062 const int32_t origStrictModePolicy = mStrictModePolicy;
1063 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1064
1065 mCallingPid = tr.sender_pid;
1066 mCallingUid = tr.sender_euid;
1067 mLastTransactionBinderFlags = tr.flags;
1068
1069 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
1070
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001072 status_t error;
1073 IF_LOG_TRANSACTIONS() {
1074 TextOutput::Bundle _b(alog);
1075 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1076 << " / obj " << tr.target.ptr << " / code "
1077 << TypeCode(tr.code) << ": " << indent << buffer
1078 << dedent << endl
1079 << "Data addr = "
1080 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1081 << ", offsets addr="
1082 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1083 }
1084 if (tr.target.ptr) {
1085 // We only have a weak reference on the target object, so we must first try to
1086 // safely acquire a strong reference before doing anything else with it.
1087 if (reinterpret_cast<RefBase::weakref_type*>(
1088 tr.target.ptr)->attemptIncStrong(this)) {
1089 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1090 &reply, tr.flags);
1091 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001092 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001093 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001094 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001095
Martijn Coenenea0090a2017-11-02 18:54:40 +00001096 } else {
1097 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001099
Martijn Coenenea0090a2017-11-02 18:54:40 +00001100 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
1101 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001102
Martijn Coenenea0090a2017-11-02 18:54:40 +00001103 if ((tr.flags & TF_ONE_WAY) == 0) {
1104 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1105 if (error < NO_ERROR) reply.setError(error);
1106 sendReply(reply, 0);
1107 } else {
1108 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1109 }
1110
1111 mCallingPid = origPid;
1112 mCallingUid = origUid;
1113 mStrictModePolicy = origStrictModePolicy;
1114 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate440fd872010-03-18 17:55:03 -07001115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 IF_LOG_TRANSACTIONS() {
1117 TextOutput::Bundle _b(alog);
1118 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1119 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1120 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 }
1123 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 case BR_DEAD_BINDER:
1126 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001127 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 proxy->sendObituary();
1129 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001130 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1134 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001135 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 proxy->getWeakRefs()->decWeak(proxy);
1137 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 case BR_FINISHED:
1140 result = TIMED_OUT;
1141 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 case BR_NOOP:
1144 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 case BR_SPAWN_LOOPER:
1147 mProcess->spawnPooledThread(false);
1148 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 default:
liangweikanga43ee152016-10-25 16:37:54 +08001151 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 result = UNKNOWN_ERROR;
1153 break;
1154 }
1155
1156 if (result != NO_ERROR) {
1157 mLastError = result;
1158 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 return result;
1161}
1162
1163void IPCThreadState::threadDestructor(void *st)
1164{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001165 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1166 if (self) {
1167 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001168#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001169 if (self->mProcess->mDriverFD > 0) {
1170 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1171 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001173 delete self;
1174 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175}
1176
1177
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001178void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1179 size_t /*dataSize*/,
1180 const binder_size_t* /*objects*/,
1181 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182{
Steve Blocka19954a2012-01-04 20:05:49 +00001183 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 IF_LOG_COMMANDS() {
1185 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1186 }
Steve Block67263472012-01-09 18:35:44 +00001187 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001188 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 IPCThreadState* state = self();
1190 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001191 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192}
1193
1194}; // namespace android