blob: 4b70e2e00466b4edf1a4e05c6319672653ebbc01 [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>
Jayant Chowdharydac6dc82018-10-01 22:52:44 +000020#include <binderthreadstate/IPCThreadStateBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/Binder.h>
23#include <binder/BpBinder.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070024#include <binder/TextOutput.h>
25
Steven Moreland7732a092019-01-02 17:54:16 -080026#include <android-base/macros.h>
Glenn Kastena26e1cf2012-03-16 07:15:23 -070027#include <cutils/sched_policy.h>
Steven Moreland7732a092019-01-02 17:54:16 -080028#include <utils/CallStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/Log.h>
Colin Cross96e83222016-04-15 14:29:55 -070030#include <utils/SystemClock.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/threads.h>
32
Mathias Agopian208059f2009-05-18 15:08:03 -070033#include <private/binder/binder_module.h>
34#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070037#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <pthread.h>
39#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080040#include <signal.h>
41#include <stdio.h>
42#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080044#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46#if LOG_NDEBUG
47
48#define IF_LOG_TRANSACTIONS() if (false)
49#define IF_LOG_COMMANDS() if (false)
50#define LOG_REMOTEREFS(...)
51#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080052
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#define LOG_THREADPOOL(...)
54#define LOG_ONEWAY(...)
55
56#else
57
Steve Block9f760152011-10-12 17:27:03 +010058#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
59#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
60#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
61#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
62#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
63#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
65#endif
66
67// ---------------------------------------------------------------------------
68
69namespace android {
70
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070071// Static const and functions will be optimized out if not used,
72// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073static const char *kReturnStrings[] = {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070074 "BR_ERROR",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 "BR_OK",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 "BR_TRANSACTION",
77 "BR_REPLY",
78 "BR_ACQUIRE_RESULT",
79 "BR_DEAD_REPLY",
80 "BR_TRANSACTION_COMPLETE",
81 "BR_INCREFS",
82 "BR_ACQUIRE",
83 "BR_RELEASE",
84 "BR_DECREFS",
85 "BR_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 "BR_NOOP",
87 "BR_SPAWN_LOOPER",
88 "BR_FINISHED",
89 "BR_DEAD_BINDER",
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070090 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Steven Moreland3085a472018-12-26 13:59:23 -080091 "BR_FAILED_REPLY",
92 "BR_TRANSACTION_SEC_CTX",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093};
94
95static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 "BC_TRANSACTION",
97 "BC_REPLY",
98 "BC_ACQUIRE_RESULT",
99 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 "BC_INCREFS",
101 "BC_ACQUIRE",
102 "BC_RELEASE",
103 "BC_DECREFS",
104 "BC_INCREFS_DONE",
105 "BC_ACQUIRE_DONE",
106 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 "BC_REGISTER_LOOPER",
108 "BC_ENTER_LOOPER",
109 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 "BC_REQUEST_DEATH_NOTIFICATION",
111 "BC_CLEAR_DEATH_NOTIFICATION",
112 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113};
114
songjinshi73a7dde2016-10-18 21:05:56 +0800115static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
songjinshi8e486c62019-04-04 11:22:52 +0800117 size_t idx = cmd & _IOC_NRMASK;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
119 return kReturnStrings[idx];
120 else
121 return "unknown";
122}
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124static const void* printBinderTransactionData(TextOutput& out, const void* data)
125{
126 const binder_transaction_data* btd =
127 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700128 if (btd->target.handle < 1024) {
129 /* want to print descriptors in decimal; guess based on value */
130 out << "target.desc=" << btd->target.handle;
131 } else {
132 out << "target.ptr=" << btd->target.ptr;
133 }
134 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700135 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
137 << " bytes)" << endl
138 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700139 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 return btd+1;
141}
142
143static const void* printReturnCommand(TextOutput& out, const void* _cmd)
144{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700145 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100147 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700148 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100149 if (code == BR_ERROR) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700150 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700152 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 out << "Unknown reply: " << code << endl;
154 return cmd;
155 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700156 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800157
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 switch (code) {
159 case BR_TRANSACTION:
160 case BR_REPLY: {
161 out << ": " << indent;
162 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
163 out << dedent;
164 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 case BR_ACQUIRE_RESULT: {
167 const int32_t res = *cmd++;
168 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
169 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800170
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 case BR_INCREFS:
172 case BR_ACQUIRE:
173 case BR_RELEASE:
174 case BR_DECREFS: {
175 const int32_t b = *cmd++;
176 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700177 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 case BR_ATTEMPT_ACQUIRE: {
181 const int32_t p = *cmd++;
182 const int32_t b = *cmd++;
183 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700184 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 << "), pri=" << p;
186 } break;
187
188 case BR_DEAD_BINDER:
189 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
190 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700191 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700193
194 default:
195 // no details to show for: BR_OK, BR_DEAD_REPLY,
196 // BR_TRANSACTION_COMPLETE, BR_FINISHED
197 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 out << endl;
201 return cmd;
202}
203
204static const void* printCommand(TextOutput& out, const void* _cmd)
205{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700206 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100208 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700209 size_t cmdIndex = code & 0xff;
210
211 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 out << "Unknown command: " << code << endl;
213 return cmd;
214 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700215 out << kCommandStrings[cmdIndex];
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 switch (code) {
218 case BC_TRANSACTION:
219 case BC_REPLY: {
220 out << ": " << indent;
221 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
222 out << dedent;
223 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 case BC_ACQUIRE_RESULT: {
226 const int32_t res = *cmd++;
227 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
228 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 case BC_FREE_BUFFER: {
231 const int32_t buf = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700232 out << ": buffer=" << (void*)(long)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 case BC_INCREFS:
236 case BC_ACQUIRE:
237 case BC_RELEASE:
238 case BC_DECREFS: {
239 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700240 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 case BC_INCREFS_DONE:
244 case BC_ACQUIRE_DONE: {
245 const int32_t b = *cmd++;
246 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700247 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 case BC_ATTEMPT_ACQUIRE: {
251 const int32_t p = *cmd++;
252 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700253 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 case BC_REQUEST_DEATH_NOTIFICATION:
257 case BC_CLEAR_DEATH_NOTIFICATION: {
258 const int32_t h = *cmd++;
259 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700260 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 } break;
262
263 case BC_DEAD_BINDER_DONE: {
264 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700265 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700267
268 default:
269 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
270 // BC_EXIT_LOOPER
271 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 out << endl;
275 return cmd;
276}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277
278static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
279static bool gHaveTLS = false;
280static pthread_key_t gTLS = 0;
281static bool gShutdown = false;
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800282static bool gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283
284IPCThreadState* IPCThreadState::self()
285{
286 if (gHaveTLS) {
287restart:
288 const pthread_key_t k = gTLS;
289 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
290 if (st) return st;
291 return new IPCThreadState;
292 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800293
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800294 if (gShutdown) {
295 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700296 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800297 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800298
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 pthread_mutex_lock(&gTLSMutex);
300 if (!gHaveTLS) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800301 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
302 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800304 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
305 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700306 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 }
308 gHaveTLS = true;
309 }
310 pthread_mutex_unlock(&gTLSMutex);
311 goto restart;
312}
313
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800314IPCThreadState* IPCThreadState::selfOrNull()
315{
316 if (gHaveTLS) {
317 const pthread_key_t k = gTLS;
318 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
319 return st;
320 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700321 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800322}
323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324void IPCThreadState::shutdown()
325{
326 gShutdown = true;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 if (gHaveTLS) {
329 // XXX Need to wait for all thread pool threads to exit!
330 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
331 if (st) {
332 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700333 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 }
zhongjieff405782016-03-09 15:05:04 +0800335 pthread_key_delete(gTLS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 gHaveTLS = false;
337 }
338}
339
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800340void IPCThreadState::disableBackgroundScheduling(bool disable)
341{
342 gDisableBackgroundScheduling = disable;
343}
344
Martijn Coenen2b631742017-05-05 11:16:59 -0700345bool IPCThreadState::backgroundSchedulingDisabled()
346{
347 return gDisableBackgroundScheduling;
348}
349
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350sp<ProcessState> IPCThreadState::process()
351{
352 return mProcess;
353}
354
355status_t IPCThreadState::clearLastError()
356{
357 const status_t err = mLastError;
358 mLastError = NO_ERROR;
359 return err;
360}
361
Dan Stoza9c634fd2014-11-26 12:23:23 -0800362pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363{
364 return mCallingPid;
365}
366
Steven Moreland3085a472018-12-26 13:59:23 -0800367const char* IPCThreadState::getCallingSid() const
368{
369 return mCallingSid;
370}
371
Dan Stoza9c634fd2014-11-26 12:23:23 -0800372uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373{
374 return mCallingUid;
375}
376
377int64_t IPCThreadState::clearCallingIdentity()
378{
Steven Moreland3085a472018-12-26 13:59:23 -0800379 // ignore mCallingSid for legacy reasons
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
381 clearCaller();
382 return token;
383}
384
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700385void IPCThreadState::setStrictModePolicy(int32_t policy)
386{
387 mStrictModePolicy = policy;
388}
389
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700390int32_t IPCThreadState::getStrictModePolicy() const
391{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700392 return mStrictModePolicy;
393}
394
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700395void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
396{
397 mLastTransactionBinderFlags = flags;
398}
399
400int32_t IPCThreadState::getLastTransactionBinderFlags() const
401{
402 return mLastTransactionBinderFlags;
403}
404
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405void IPCThreadState::restoreCallingIdentity(int64_t token)
406{
407 mCallingUid = (int)(token>>32);
Steven Moreland3085a472018-12-26 13:59:23 -0800408 mCallingSid = nullptr; // not enough data to restore
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 mCallingPid = (int)token;
410}
411
412void IPCThreadState::clearCaller()
413{
Marco Nelissend43b1942009-07-17 07:59:17 -0700414 mCallingPid = getpid();
Steven Moreland3085a472018-12-26 13:59:23 -0800415 mCallingSid = nullptr; // expensive to lookup
Marco Nelissend43b1942009-07-17 07:59:17 -0700416 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417}
418
419void IPCThreadState::flushCommands()
420{
421 if (mProcess->mDriverFD <= 0)
422 return;
423 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700424 // The flush could have caused post-write refcount decrements to have
425 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
426 // being queued in mOut. So flush again, if we need to.
427 if (mOut.dataSize() > 0) {
428 talkWithDriver(false);
429 }
430 if (mOut.dataSize() > 0) {
431 ALOGW("mOut.dataSize() > 0 after flushCommands()");
432 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433}
434
Wale Ogunwale376b8222015-04-13 16:16:10 -0700435void IPCThreadState::blockUntilThreadAvailable()
436{
437 pthread_mutex_lock(&mProcess->mThreadCountLock);
438 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700439 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
440 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
441 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700442 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
443 }
444 pthread_mutex_unlock(&mProcess->mThreadCountLock);
445}
446
Todd Poynor8d96cab2013-06-25 19:12:18 -0700447status_t IPCThreadState::getAndExecuteCommand()
448{
449 status_t result;
450 int32_t cmd;
451
452 result = talkWithDriver();
453 if (result >= NO_ERROR) {
454 size_t IN = mIn.dataAvail();
455 if (IN < sizeof(int32_t)) return result;
456 cmd = mIn.readInt32();
457 IF_LOG_COMMANDS() {
458 alog << "Processing top-level Command: "
459 << getReturnString(cmd) << endl;
460 }
461
Wale Ogunwale376b8222015-04-13 16:16:10 -0700462 pthread_mutex_lock(&mProcess->mThreadCountLock);
463 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700464 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
465 mProcess->mStarvationStartTimeMs == 0) {
466 mProcess->mStarvationStartTimeMs = uptimeMillis();
467 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700468 pthread_mutex_unlock(&mProcess->mThreadCountLock);
469
Todd Poynor8d96cab2013-06-25 19:12:18 -0700470 result = executeCommand(cmd);
471
Wale Ogunwale376b8222015-04-13 16:16:10 -0700472 pthread_mutex_lock(&mProcess->mThreadCountLock);
473 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700474 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
475 mProcess->mStarvationStartTimeMs != 0) {
476 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
477 if (starvationTimeMs > 100) {
478 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
479 mProcess->mMaxThreads, starvationTimeMs);
480 }
481 mProcess->mStarvationStartTimeMs = 0;
482 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700483 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
484 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700485 }
486
487 return result;
488}
489
490// When we've cleared the incoming command queue, process any pending derefs
491void IPCThreadState::processPendingDerefs()
492{
493 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200494 /*
495 * The decWeak()/decStrong() calls may cause a destructor to run,
496 * which in turn could have initiated an outgoing transaction,
497 * which in turn could cause us to add to the pending refs
498 * vectors; so instead of simply iterating, loop until they're empty.
499 *
500 * We do this in an outer loop, because calling decStrong()
501 * may result in something being added to mPendingWeakDerefs,
502 * which could be delayed until the next incoming command
503 * from the driver if we don't process it now.
504 */
505 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
506 while (mPendingWeakDerefs.size() > 0) {
507 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
508 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700509 refs->decWeak(mProcess.get());
510 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700511
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200512 if (mPendingStrongDerefs.size() > 0) {
513 // We don't use while() here because we don't want to re-order
514 // strong and weak decs at all; if this decStrong() causes both a
515 // decWeak() and a decStrong() to be queued, we want to process
516 // the decWeak() first.
517 BBinder* obj = mPendingStrongDerefs[0];
518 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700519 obj->decStrong(mProcess.get());
520 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700521 }
522 }
523}
524
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700525void IPCThreadState::processPostWriteDerefs()
526{
527 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
528 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
529 refs->decWeak(mProcess.get());
530 }
531 mPostWriteWeakDerefs.clear();
532
533 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
534 RefBase* obj = mPostWriteStrongDerefs[i];
535 obj->decStrong(mProcess.get());
536 }
537 mPostWriteStrongDerefs.clear();
538}
539
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540void IPCThreadState::joinThreadPool(bool isMain)
541{
542 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
543
544 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800545
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 status_t result;
547 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700548 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700550 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800551
Todd Poynor8d96cab2013-06-25 19:12:18 -0700552 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
553 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700554 mProcess->mDriverFD, result);
555 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 // Let this thread exit the thread pool if it is no longer
559 // needed and it is not the main process thread.
560 if(result == TIMED_OUT && !isMain) {
561 break;
562 }
563 } while (result != -ECONNREFUSED && result != -EBADF);
564
Wei Wangc7341432016-10-19 10:23:59 -0700565 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
566 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800567
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 mOut.writeInt32(BC_EXIT_LOOPER);
569 talkWithDriver(false);
570}
571
Todd Poynor8d96cab2013-06-25 19:12:18 -0700572int IPCThreadState::setupPolling(int* fd)
573{
574 if (mProcess->mDriverFD <= 0) {
575 return -EBADF;
576 }
577
578 mOut.writeInt32(BC_ENTER_LOOPER);
579 *fd = mProcess->mDriverFD;
580 return 0;
581}
582
583status_t IPCThreadState::handlePolledCommands()
584{
585 status_t result;
586
587 do {
588 result = getAndExecuteCommand();
589 } while (mIn.dataPosition() < mIn.dataSize());
590
591 processPendingDerefs();
592 flushCommands();
593 return result;
594}
595
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800596void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597{
Steve Blocka19954a2012-01-04 20:05:49 +0000598 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 flushCommands();
600 int fd = mProcess->mDriverFD;
601 mProcess->mDriverFD = -1;
602 close(fd);
603 //kill(getpid(), SIGKILL);
604}
605
606status_t IPCThreadState::transact(int32_t handle,
607 uint32_t code, const Parcel& data,
608 Parcel* reply, uint32_t flags)
609{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800610 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611
612 flags |= TF_ACCEPT_FDS;
613
614 IF_LOG_TRANSACTIONS() {
615 TextOutput::Bundle _b(alog);
616 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
617 << handle << " / code " << TypeCode(code) << ": "
618 << indent << data << dedent << endl;
619 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800620
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800621 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
622 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700623 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800624
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 if (err != NO_ERROR) {
626 if (reply) reply->setError(err);
627 return (mLastError = err);
628 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800629
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 if ((flags & TF_ONE_WAY) == 0) {
Steven Moreland7732a092019-01-02 17:54:16 -0800631 if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) {
632 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
633 ALOGE("Process making non-oneway call but is restricted.");
634 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
635 ANDROID_LOG_ERROR);
636 } else /* FATAL_IF_NOT_ONEWAY */ {
637 LOG_ALWAYS_FATAL("Process may not make oneway calls.");
638 }
639 }
640
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700641 #if 0
642 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000643 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700644 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000645 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700646 }
647 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 if (reply) {
649 err = waitForResponse(reply);
650 } else {
651 Parcel fakeReply;
652 err = waitForResponse(&fakeReply);
653 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700654 #if 0
655 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000656 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700657 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000658 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700659 }
660 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800661
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 IF_LOG_TRANSACTIONS() {
663 TextOutput::Bundle _b(alog);
664 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
665 << handle << ": ";
666 if (reply) alog << indent << *reply << dedent << endl;
667 else alog << "(none requested)" << endl;
668 }
669 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700670 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800672
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 return err;
674}
675
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700676void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677{
678 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
679 mOut.writeInt32(BC_ACQUIRE);
680 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700681 // Create a temp reference until the driver has handled this command.
682 proxy->incStrong(mProcess.get());
683 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684}
685
686void IPCThreadState::decStrongHandle(int32_t handle)
687{
688 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
689 mOut.writeInt32(BC_RELEASE);
690 mOut.writeInt32(handle);
691}
692
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700693void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694{
695 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
696 mOut.writeInt32(BC_INCREFS);
697 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700698 // Create a temp reference until the driver has handled this command.
699 proxy->getWeakRefs()->incWeak(mProcess.get());
700 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701}
702
703void IPCThreadState::decWeakHandle(int32_t handle)
704{
705 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
706 mOut.writeInt32(BC_DECREFS);
707 mOut.writeInt32(handle);
708}
709
710status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
711{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800712#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700713 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
715 mOut.writeInt32(0); // xxx was thread priority
716 mOut.writeInt32(handle);
717 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800718
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800722 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
724#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800725
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800727#else
728 (void)handle;
729 ALOGE("%s(%d): Not supported\n", __func__, handle);
730 return INVALID_OPERATION;
731#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732}
733
734void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
735{
736#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800737 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700739 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740}
741
742status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
743{
744 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
745 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000746 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 return NO_ERROR;
748}
749
750status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
751{
752 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
753 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000754 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 return NO_ERROR;
756}
757
758IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700759 : mProcess(ProcessState::self()),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700760 mStrictModePolicy(0),
Steven Moreland7732a092019-01-02 17:54:16 -0800761 mLastTransactionBinderFlags(0),
762 mCallRestriction(mProcess->mCallRestriction)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763{
764 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800765 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 mIn.setDataCapacity(256);
767 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000768 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769}
770
771IPCThreadState::~IPCThreadState()
772{
773}
774
Martijn Coenenea0090a2017-11-02 18:54:40 +0000775status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
776{
777 status_t err;
778 status_t statusBuffer;
779 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
780 if (err < NO_ERROR) return err;
781
Yi Kongfdd8da92018-06-07 17:52:27 -0700782 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000783}
784
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
786{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100787 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 int32_t err;
789
790 while (1) {
791 if ((err=talkWithDriver()) < NO_ERROR) break;
792 err = mIn.errorCheck();
793 if (err < NO_ERROR) break;
794 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800795
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100796 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800797
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 IF_LOG_COMMANDS() {
799 alog << "Processing waitForResponse Command: "
800 << getReturnString(cmd) << endl;
801 }
802
803 switch (cmd) {
804 case BR_TRANSACTION_COMPLETE:
805 if (!reply && !acquireResult) goto finish;
806 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800807
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 case BR_DEAD_REPLY:
809 err = DEAD_OBJECT;
810 goto finish;
811
812 case BR_FAILED_REPLY:
813 err = FAILED_TRANSACTION;
814 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 case BR_ACQUIRE_RESULT:
817 {
Steve Block67263472012-01-09 18:35:44 +0000818 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 const int32_t result = mIn.readInt32();
820 if (!acquireResult) continue;
821 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
822 }
823 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800824
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 case BR_REPLY:
826 {
827 binder_transaction_data tr;
828 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000829 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 if (err != NO_ERROR) goto finish;
831
832 if (reply) {
833 if ((tr.flags & TF_STATUS_CODE) == 0) {
834 reply->ipcSetDataReference(
835 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
836 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800837 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
838 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 freeBuffer, this);
840 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800841 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700842 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
844 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800845 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
846 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 }
848 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700849 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
851 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800852 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
853 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854 continue;
855 }
856 }
857 goto finish;
858
859 default:
860 err = executeCommand(cmd);
861 if (err != NO_ERROR) goto finish;
862 break;
863 }
864 }
865
866finish:
867 if (err != NO_ERROR) {
868 if (acquireResult) *acquireResult = err;
869 if (reply) reply->setError(err);
870 mLastError = err;
871 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800872
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 return err;
874}
875
876status_t IPCThreadState::talkWithDriver(bool doReceive)
877{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100878 if (mProcess->mDriverFD <= 0) {
879 return -EBADF;
880 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800881
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800883
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 // Is the read buffer empty?
885 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800886
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 // We don't want to write anything if we are still reading
888 // from data left in the input buffer and the caller
889 // has requested to read the next data.
890 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800891
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800893 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894
895 // This is what we'll read.
896 if (doReceive && needRead) {
897 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800898 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 } else {
900 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800901 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700903
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 IF_LOG_COMMANDS() {
905 TextOutput::Bundle _b(alog);
906 if (outAvail != 0) {
907 alog << "Sending commands to driver: " << indent;
908 const void* cmds = (const void*)bwr.write_buffer;
909 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
910 alog << HexDump(cmds, bwr.write_size) << endl;
911 while (cmds < end) cmds = printCommand(alog, cmds);
912 alog << dedent;
913 }
914 alog << "Size of receive buffer: " << bwr.read_size
915 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
916 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800917
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 // Return immediately if there is nothing to do.
919 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700920
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 bwr.write_consumed = 0;
922 bwr.read_consumed = 0;
923 status_t err;
924 do {
925 IF_LOG_COMMANDS() {
926 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
927 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700928#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
930 err = NO_ERROR;
931 else
932 err = -errno;
933#else
934 err = INVALID_OPERATION;
935#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100936 if (mProcess->mDriverFD <= 0) {
937 err = -EBADF;
938 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 IF_LOG_COMMANDS() {
940 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
941 }
942 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700943
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800945 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700947 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 }
949
950 if (err >= NO_ERROR) {
951 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800952 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700954 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700956 processPostWriteDerefs();
957 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958 }
959 if (bwr.read_consumed > 0) {
960 mIn.setDataSize(bwr.read_consumed);
961 mIn.setDataPosition(0);
962 }
963 IF_LOG_COMMANDS() {
964 TextOutput::Bundle _b(alog);
965 alog << "Remaining data size: " << mOut.dataSize() << endl;
966 alog << "Received commands from driver: " << indent;
967 const void* cmds = mIn.data();
968 const void* end = mIn.data() + mIn.dataSize();
969 alog << HexDump(cmds, mIn.dataSize()) << endl;
970 while (cmds < end) cmds = printReturnCommand(alog, cmds);
971 alog << dedent;
972 }
973 return NO_ERROR;
974 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800975
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 return err;
977}
978
979status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
980 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
981{
982 binder_transaction_data tr;
983
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800984 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 tr.target.handle = handle;
986 tr.code = code;
987 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +0400988 tr.cookie = 0;
989 tr.sender_pid = 0;
990 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800991
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 const status_t err = data.errorCheck();
993 if (err == NO_ERROR) {
994 tr.data_size = data.ipcDataSize();
995 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800996 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800997 tr.data.ptr.offsets = data.ipcObjects();
998 } else if (statusBuffer) {
999 tr.flags |= TF_STATUS_CODE;
1000 *statusBuffer = err;
1001 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -08001002 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001004 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 } else {
1006 return (mLastError = err);
1007 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001008
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 mOut.writeInt32(cmd);
1010 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001011
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 return NO_ERROR;
1013}
1014
1015sp<BBinder> the_context_object;
1016
1017void setTheContextObject(sp<BBinder> obj)
1018{
1019 the_context_object = obj;
1020}
1021
1022status_t IPCThreadState::executeCommand(int32_t cmd)
1023{
1024 BBinder* obj;
1025 RefBase::weakref_type* refs;
1026 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001027
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001028 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 case BR_ERROR:
1030 result = mIn.readInt32();
1031 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 case BR_OK:
1034 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001035
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001037 refs = (RefBase::weakref_type*)mIn.readPointer();
1038 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001039 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1041 refs, obj, refs->refBase());
1042 obj->incStrong(mProcess.get());
1043 IF_LOG_REMOTEREFS() {
1044 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1045 obj->printRefs();
1046 }
1047 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001048 mOut.writePointer((uintptr_t)refs);
1049 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001051
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001053 refs = (RefBase::weakref_type*)mIn.readPointer();
1054 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001055 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1057 refs, obj, refs->refBase());
1058 IF_LOG_REMOTEREFS() {
1059 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1060 obj->printRefs();
1061 }
1062 mPendingStrongDerefs.push(obj);
1063 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001066 refs = (RefBase::weakref_type*)mIn.readPointer();
1067 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 refs->incWeak(mProcess.get());
1069 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001070 mOut.writePointer((uintptr_t)refs);
1071 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001075 refs = (RefBase::weakref_type*)mIn.readPointer();
1076 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 // NOTE: This assertion is not valid, because the object may no
1078 // longer exist (thus the (BBinder*)cast above resulting in a different
1079 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001080 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1082 // refs, obj, refs->refBase());
1083 mPendingWeakDerefs.push(refs);
1084 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001085
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001087 refs = (RefBase::weakref_type*)mIn.readPointer();
1088 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 {
1091 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001092 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1094 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096 mOut.writeInt32(BC_ACQUIRE_RESULT);
1097 mOut.writeInt32((int32_t)success);
1098 }
1099 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001100
Steven Moreland3085a472018-12-26 13:59:23 -08001101 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 case BR_TRANSACTION:
1103 {
Steven Moreland3085a472018-12-26 13:59:23 -08001104 binder_transaction_data_secctx tr_secctx;
1105 binder_transaction_data& tr = tr_secctx.transaction_data;
1106
1107 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1108 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1109 } else {
1110 result = mIn.read(&tr, sizeof(tr));
1111 tr_secctx.secctx = 0;
1112 }
1113
Steve Block67263472012-01-09 18:35:44 +00001114 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 "Not enough command data for brTRANSACTION");
1116 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001117
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001118 //Record the fact that we're in a binder call.
1119 mIPCThreadStateBase->pushCurrentState(
1120 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001121 Parcel buffer;
1122 buffer.ipcSetDataReference(
1123 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1124 tr.data_size,
1125 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1126 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1127
1128 const pid_t origPid = mCallingPid;
Steven Moreland3085a472018-12-26 13:59:23 -08001129 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001130 const uid_t origUid = mCallingUid;
1131 const int32_t origStrictModePolicy = mStrictModePolicy;
1132 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1133
1134 mCallingPid = tr.sender_pid;
Steven Moreland3085a472018-12-26 13:59:23 -08001135 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001136 mCallingUid = tr.sender_euid;
1137 mLastTransactionBinderFlags = tr.flags;
1138
Steven Moreland3085a472018-12-26 13:59:23 -08001139 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1140 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001143 status_t error;
1144 IF_LOG_TRANSACTIONS() {
1145 TextOutput::Bundle _b(alog);
1146 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1147 << " / obj " << tr.target.ptr << " / code "
1148 << TypeCode(tr.code) << ": " << indent << buffer
1149 << dedent << endl
1150 << "Data addr = "
1151 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1152 << ", offsets addr="
1153 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1154 }
1155 if (tr.target.ptr) {
1156 // We only have a weak reference on the target object, so we must first try to
1157 // safely acquire a strong reference before doing anything else with it.
1158 if (reinterpret_cast<RefBase::weakref_type*>(
1159 tr.target.ptr)->attemptIncStrong(this)) {
1160 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1161 &reply, tr.flags);
1162 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001163 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001164 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001165 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001166
Martijn Coenenea0090a2017-11-02 18:54:40 +00001167 } else {
1168 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001170
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001171 mIPCThreadStateBase->popCurrentState();
Steven Moreland3085a472018-12-26 13:59:23 -08001172 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1173 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001174
Martijn Coenenea0090a2017-11-02 18:54:40 +00001175 if ((tr.flags & TF_ONE_WAY) == 0) {
1176 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1177 if (error < NO_ERROR) reply.setError(error);
1178 sendReply(reply, 0);
1179 } else {
1180 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1181 }
1182
1183 mCallingPid = origPid;
Steven Moreland3085a472018-12-26 13:59:23 -08001184 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001185 mCallingUid = origUid;
1186 mStrictModePolicy = origStrictModePolicy;
1187 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate440fd872010-03-18 17:55:03 -07001188
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 IF_LOG_TRANSACTIONS() {
1190 TextOutput::Bundle _b(alog);
1191 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1192 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1193 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 }
1196 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 case BR_DEAD_BINDER:
1199 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001200 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 proxy->sendObituary();
1202 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001203 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1207 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001208 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 proxy->getWeakRefs()->decWeak(proxy);
1210 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 case BR_FINISHED:
1213 result = TIMED_OUT;
1214 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216 case BR_NOOP:
1217 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 case BR_SPAWN_LOOPER:
1220 mProcess->spawnPooledThread(false);
1221 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 default:
liangweikanga43ee152016-10-25 16:37:54 +08001224 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 result = UNKNOWN_ERROR;
1226 break;
1227 }
1228
1229 if (result != NO_ERROR) {
1230 mLastError = result;
1231 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 return result;
1234}
1235
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001236bool IPCThreadState::isServingCall() const {
1237 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1238}
1239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240void IPCThreadState::threadDestructor(void *st)
1241{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001242 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1243 if (self) {
1244 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001245#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001246 if (self->mProcess->mDriverFD > 0) {
1247 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1248 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001250 delete self;
1251 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252}
1253
1254
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001255void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1256 size_t /*dataSize*/,
1257 const binder_size_t* /*objects*/,
1258 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259{
Steve Blocka19954a2012-01-04 20:05:49 +00001260 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 IF_LOG_COMMANDS() {
1262 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1263 }
Steve Block67263472012-01-09 18:35:44 +00001264 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001265 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 IPCThreadState* state = self();
1267 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001268 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269}
1270
1271}; // namespace android