blob: 14a801cec81423b90d6b200c863cc2e05c27e3c5 [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
Glenn Kastena26e1cf2012-03-16 07:15:23 -070026#include <cutils/sched_policy.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/Log.h>
Colin Cross96e83222016-04-15 14:29:55 -070028#include <utils/SystemClock.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/threads.h>
30
Mathias Agopian208059f2009-05-18 15:08:03 -070031#include <private/binder/binder_module.h>
32#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070035#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <pthread.h>
37#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080038#include <signal.h>
39#include <stdio.h>
40#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080042#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#if LOG_NDEBUG
45
46#define IF_LOG_TRANSACTIONS() if (false)
47#define IF_LOG_COMMANDS() if (false)
48#define LOG_REMOTEREFS(...)
49#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#define LOG_THREADPOOL(...)
52#define LOG_ONEWAY(...)
53
54#else
55
Steve Block9f760152011-10-12 17:27:03 +010056#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
57#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
58#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
59#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
60#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
61#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
63#endif
64
65// ---------------------------------------------------------------------------
66
67namespace android {
68
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070069// Static const and functions will be optimized out if not used,
70// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071static const char *kReturnStrings[] = {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070072 "BR_ERROR",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 "BR_OK",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 "BR_TRANSACTION",
75 "BR_REPLY",
76 "BR_ACQUIRE_RESULT",
77 "BR_DEAD_REPLY",
78 "BR_TRANSACTION_COMPLETE",
79 "BR_INCREFS",
80 "BR_ACQUIRE",
81 "BR_RELEASE",
82 "BR_DECREFS",
83 "BR_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 "BR_NOOP",
85 "BR_SPAWN_LOOPER",
86 "BR_FINISHED",
87 "BR_DEAD_BINDER",
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070088 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Steven Morelandf0212002018-12-26 13:59:23 -080089 "BR_FAILED_REPLY",
90 "BR_TRANSACTION_SEC_CTX",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091};
92
93static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 "BC_TRANSACTION",
95 "BC_REPLY",
96 "BC_ACQUIRE_RESULT",
97 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 "BC_INCREFS",
99 "BC_ACQUIRE",
100 "BC_RELEASE",
101 "BC_DECREFS",
102 "BC_INCREFS_DONE",
103 "BC_ACQUIRE_DONE",
104 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 "BC_REGISTER_LOOPER",
106 "BC_ENTER_LOOPER",
107 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 "BC_REQUEST_DEATH_NOTIFICATION",
109 "BC_CLEAR_DEATH_NOTIFICATION",
110 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111};
112
Olivier Gaillard91a04802018-11-14 17:32:41 +0000113static const int64_t kWorkSourcePropagatedBitIndex = 32;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100114
songjinshi73a7dde2016-10-18 21:05:56 +0800115static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
songjinshi73a7dde2016-10-18 21:05:56 +0800117 size_t idx = cmd & 0xff;
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 Morelandf0212002018-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 Morelandf0212002018-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
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000395int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100396{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000397 int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
398 mPropagateWorkSource = true;
399 return token;
400}
401
402int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
403{
404 const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
405 int64_t token = propagatedBit | mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100406 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000407 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100408}
409
Olivier Gaillard91a04802018-11-14 17:32:41 +0000410void IPCThreadState::clearPropagateWorkSource()
411{
412 mPropagateWorkSource = false;
413}
414
415bool IPCThreadState::shouldPropagateWorkSource() const
416{
417 return mPropagateWorkSource;
418}
419
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000420uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100421{
422 return mWorkSource;
423}
424
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000425int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100426{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000427 return setCallingWorkSourceUid(kUnsetWorkSource);
428}
429
430void IPCThreadState::restoreCallingWorkSource(int64_t token)
431{
432 uid_t uid = (int)token;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000433 setCallingWorkSourceUidWithoutPropagation(uid);
434 mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100435}
436
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700437void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
438{
439 mLastTransactionBinderFlags = flags;
440}
441
442int32_t IPCThreadState::getLastTransactionBinderFlags() const
443{
444 return mLastTransactionBinderFlags;
445}
446
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447void IPCThreadState::restoreCallingIdentity(int64_t token)
448{
449 mCallingUid = (int)(token>>32);
Steven Morelandf0212002018-12-26 13:59:23 -0800450 mCallingSid = nullptr; // not enough data to restore
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 mCallingPid = (int)token;
452}
453
454void IPCThreadState::clearCaller()
455{
Marco Nelissend43b1942009-07-17 07:59:17 -0700456 mCallingPid = getpid();
Steven Morelandf0212002018-12-26 13:59:23 -0800457 mCallingSid = nullptr; // expensive to lookup
Marco Nelissend43b1942009-07-17 07:59:17 -0700458 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459}
460
461void IPCThreadState::flushCommands()
462{
463 if (mProcess->mDriverFD <= 0)
464 return;
465 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700466 // The flush could have caused post-write refcount decrements to have
467 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
468 // being queued in mOut. So flush again, if we need to.
469 if (mOut.dataSize() > 0) {
470 talkWithDriver(false);
471 }
472 if (mOut.dataSize() > 0) {
473 ALOGW("mOut.dataSize() > 0 after flushCommands()");
474 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475}
476
Wale Ogunwale376b8222015-04-13 16:16:10 -0700477void IPCThreadState::blockUntilThreadAvailable()
478{
479 pthread_mutex_lock(&mProcess->mThreadCountLock);
480 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700481 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
482 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
483 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700484 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
485 }
486 pthread_mutex_unlock(&mProcess->mThreadCountLock);
487}
488
Todd Poynor8d96cab2013-06-25 19:12:18 -0700489status_t IPCThreadState::getAndExecuteCommand()
490{
491 status_t result;
492 int32_t cmd;
493
494 result = talkWithDriver();
495 if (result >= NO_ERROR) {
496 size_t IN = mIn.dataAvail();
497 if (IN < sizeof(int32_t)) return result;
498 cmd = mIn.readInt32();
499 IF_LOG_COMMANDS() {
500 alog << "Processing top-level Command: "
501 << getReturnString(cmd) << endl;
502 }
503
Wale Ogunwale376b8222015-04-13 16:16:10 -0700504 pthread_mutex_lock(&mProcess->mThreadCountLock);
505 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700506 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
507 mProcess->mStarvationStartTimeMs == 0) {
508 mProcess->mStarvationStartTimeMs = uptimeMillis();
509 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700510 pthread_mutex_unlock(&mProcess->mThreadCountLock);
511
Todd Poynor8d96cab2013-06-25 19:12:18 -0700512 result = executeCommand(cmd);
513
Wale Ogunwale376b8222015-04-13 16:16:10 -0700514 pthread_mutex_lock(&mProcess->mThreadCountLock);
515 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700516 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
517 mProcess->mStarvationStartTimeMs != 0) {
518 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
519 if (starvationTimeMs > 100) {
520 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
521 mProcess->mMaxThreads, starvationTimeMs);
522 }
523 mProcess->mStarvationStartTimeMs = 0;
524 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700525 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
526 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700527 }
528
529 return result;
530}
531
532// When we've cleared the incoming command queue, process any pending derefs
533void IPCThreadState::processPendingDerefs()
534{
535 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200536 /*
537 * The decWeak()/decStrong() calls may cause a destructor to run,
538 * which in turn could have initiated an outgoing transaction,
539 * which in turn could cause us to add to the pending refs
540 * vectors; so instead of simply iterating, loop until they're empty.
541 *
542 * We do this in an outer loop, because calling decStrong()
543 * may result in something being added to mPendingWeakDerefs,
544 * which could be delayed until the next incoming command
545 * from the driver if we don't process it now.
546 */
547 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
548 while (mPendingWeakDerefs.size() > 0) {
549 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
550 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700551 refs->decWeak(mProcess.get());
552 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700553
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200554 if (mPendingStrongDerefs.size() > 0) {
555 // We don't use while() here because we don't want to re-order
556 // strong and weak decs at all; if this decStrong() causes both a
557 // decWeak() and a decStrong() to be queued, we want to process
558 // the decWeak() first.
559 BBinder* obj = mPendingStrongDerefs[0];
560 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700561 obj->decStrong(mProcess.get());
562 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700563 }
564 }
565}
566
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700567void IPCThreadState::processPostWriteDerefs()
568{
569 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
570 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
571 refs->decWeak(mProcess.get());
572 }
573 mPostWriteWeakDerefs.clear();
574
575 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
576 RefBase* obj = mPostWriteStrongDerefs[i];
577 obj->decStrong(mProcess.get());
578 }
579 mPostWriteStrongDerefs.clear();
580}
581
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582void IPCThreadState::joinThreadPool(bool isMain)
583{
584 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
585
586 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800587
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 status_t result;
589 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700590 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700592 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800593
Todd Poynor8d96cab2013-06-25 19:12:18 -0700594 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
595 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700596 mProcess->mDriverFD, result);
597 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800599
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 // Let this thread exit the thread pool if it is no longer
601 // needed and it is not the main process thread.
602 if(result == TIMED_OUT && !isMain) {
603 break;
604 }
605 } while (result != -ECONNREFUSED && result != -EBADF);
606
Wei Wangc7341432016-10-19 10:23:59 -0700607 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
608 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800609
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 mOut.writeInt32(BC_EXIT_LOOPER);
611 talkWithDriver(false);
612}
613
Todd Poynor8d96cab2013-06-25 19:12:18 -0700614int IPCThreadState::setupPolling(int* fd)
615{
616 if (mProcess->mDriverFD <= 0) {
617 return -EBADF;
618 }
619
620 mOut.writeInt32(BC_ENTER_LOOPER);
621 *fd = mProcess->mDriverFD;
622 return 0;
623}
624
625status_t IPCThreadState::handlePolledCommands()
626{
627 status_t result;
628
629 do {
630 result = getAndExecuteCommand();
631 } while (mIn.dataPosition() < mIn.dataSize());
632
633 processPendingDerefs();
634 flushCommands();
635 return result;
636}
637
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800638void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639{
Steve Blocka19954a2012-01-04 20:05:49 +0000640 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 flushCommands();
642 int fd = mProcess->mDriverFD;
643 mProcess->mDriverFD = -1;
644 close(fd);
645 //kill(getpid(), SIGKILL);
646}
647
648status_t IPCThreadState::transact(int32_t handle,
649 uint32_t code, const Parcel& data,
650 Parcel* reply, uint32_t flags)
651{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800652 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653
654 flags |= TF_ACCEPT_FDS;
655
656 IF_LOG_TRANSACTIONS() {
657 TextOutput::Bundle _b(alog);
658 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
659 << handle << " / code " << TypeCode(code) << ": "
660 << indent << data << dedent << endl;
661 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800662
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800663 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
664 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700665 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 if (err != NO_ERROR) {
668 if (reply) reply->setError(err);
669 return (mLastError = err);
670 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800671
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700673 #if 0
674 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000675 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700676 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000677 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700678 }
679 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 if (reply) {
681 err = waitForResponse(reply);
682 } else {
683 Parcel fakeReply;
684 err = waitForResponse(&fakeReply);
685 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700686 #if 0
687 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000688 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700689 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000690 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700691 }
692 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800693
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 IF_LOG_TRANSACTIONS() {
695 TextOutput::Bundle _b(alog);
696 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
697 << handle << ": ";
698 if (reply) alog << indent << *reply << dedent << endl;
699 else alog << "(none requested)" << endl;
700 }
701 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700702 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 return err;
706}
707
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700708void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709{
710 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
711 mOut.writeInt32(BC_ACQUIRE);
712 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700713 // Create a temp reference until the driver has handled this command.
714 proxy->incStrong(mProcess.get());
715 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716}
717
718void IPCThreadState::decStrongHandle(int32_t handle)
719{
720 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
721 mOut.writeInt32(BC_RELEASE);
722 mOut.writeInt32(handle);
723}
724
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700725void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726{
727 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
728 mOut.writeInt32(BC_INCREFS);
729 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700730 // Create a temp reference until the driver has handled this command.
731 proxy->getWeakRefs()->incWeak(mProcess.get());
732 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733}
734
735void IPCThreadState::decWeakHandle(int32_t handle)
736{
737 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
738 mOut.writeInt32(BC_DECREFS);
739 mOut.writeInt32(handle);
740}
741
742status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
743{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800744#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700745 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
747 mOut.writeInt32(0); // xxx was thread priority
748 mOut.writeInt32(handle);
749 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800750
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800752
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800754 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
756#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800757
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800759#else
760 (void)handle;
761 ALOGE("%s(%d): Not supported\n", __func__, handle);
762 return INVALID_OPERATION;
763#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764}
765
766void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
767{
768#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800769 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700771 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772}
773
774status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
775{
776 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
777 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000778 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 return NO_ERROR;
780}
781
782status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
783{
784 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
785 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000786 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 return NO_ERROR;
788}
789
790IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700791 : mProcess(ProcessState::self()),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100792 mWorkSource(kUnsetWorkSource),
Olivier Gaillard91a04802018-11-14 17:32:41 +0000793 mPropagateWorkSource(false),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700794 mStrictModePolicy(0),
795 mLastTransactionBinderFlags(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796{
797 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800798 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 mIn.setDataCapacity(256);
800 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000801 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802}
803
804IPCThreadState::~IPCThreadState()
805{
806}
807
Martijn Coenenea0090a2017-11-02 18:54:40 +0000808status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
809{
810 status_t err;
811 status_t statusBuffer;
812 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
813 if (err < NO_ERROR) return err;
814
Yi Kongfdd8da92018-06-07 17:52:27 -0700815 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000816}
817
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
819{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100820 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 int32_t err;
822
823 while (1) {
824 if ((err=talkWithDriver()) < NO_ERROR) break;
825 err = mIn.errorCheck();
826 if (err < NO_ERROR) break;
827 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800828
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100829 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 IF_LOG_COMMANDS() {
832 alog << "Processing waitForResponse Command: "
833 << getReturnString(cmd) << endl;
834 }
835
836 switch (cmd) {
837 case BR_TRANSACTION_COMPLETE:
838 if (!reply && !acquireResult) goto finish;
839 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800840
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841 case BR_DEAD_REPLY:
842 err = DEAD_OBJECT;
843 goto finish;
844
845 case BR_FAILED_REPLY:
846 err = FAILED_TRANSACTION;
847 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800848
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 case BR_ACQUIRE_RESULT:
850 {
Steve Block67263472012-01-09 18:35:44 +0000851 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 const int32_t result = mIn.readInt32();
853 if (!acquireResult) continue;
854 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
855 }
856 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800857
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858 case BR_REPLY:
859 {
860 binder_transaction_data tr;
861 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000862 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 if (err != NO_ERROR) goto finish;
864
865 if (reply) {
866 if ((tr.flags & TF_STATUS_CODE) == 0) {
867 reply->ipcSetDataReference(
868 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
869 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800870 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
871 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 freeBuffer, this);
873 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800874 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700875 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
877 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800878 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
879 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 }
881 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700882 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
884 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800885 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
886 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 continue;
888 }
889 }
890 goto finish;
891
892 default:
893 err = executeCommand(cmd);
894 if (err != NO_ERROR) goto finish;
895 break;
896 }
897 }
898
899finish:
900 if (err != NO_ERROR) {
901 if (acquireResult) *acquireResult = err;
902 if (reply) reply->setError(err);
903 mLastError = err;
904 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800905
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 return err;
907}
908
909status_t IPCThreadState::talkWithDriver(bool doReceive)
910{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100911 if (mProcess->mDriverFD <= 0) {
912 return -EBADF;
913 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800914
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800916
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 // Is the read buffer empty?
918 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800919
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 // We don't want to write anything if we are still reading
921 // from data left in the input buffer and the caller
922 // has requested to read the next data.
923 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800924
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800926 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927
928 // This is what we'll read.
929 if (doReceive && needRead) {
930 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800931 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 } else {
933 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800934 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700936
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 IF_LOG_COMMANDS() {
938 TextOutput::Bundle _b(alog);
939 if (outAvail != 0) {
940 alog << "Sending commands to driver: " << indent;
941 const void* cmds = (const void*)bwr.write_buffer;
942 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
943 alog << HexDump(cmds, bwr.write_size) << endl;
944 while (cmds < end) cmds = printCommand(alog, cmds);
945 alog << dedent;
946 }
947 alog << "Size of receive buffer: " << bwr.read_size
948 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
949 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800950
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 // Return immediately if there is nothing to do.
952 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700953
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954 bwr.write_consumed = 0;
955 bwr.read_consumed = 0;
956 status_t err;
957 do {
958 IF_LOG_COMMANDS() {
959 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
960 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700961#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
963 err = NO_ERROR;
964 else
965 err = -errno;
966#else
967 err = INVALID_OPERATION;
968#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100969 if (mProcess->mDriverFD <= 0) {
970 err = -EBADF;
971 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 IF_LOG_COMMANDS() {
973 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
974 }
975 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700976
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800978 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700980 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 }
982
983 if (err >= NO_ERROR) {
984 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800985 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700987 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700989 processPostWriteDerefs();
990 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 }
992 if (bwr.read_consumed > 0) {
993 mIn.setDataSize(bwr.read_consumed);
994 mIn.setDataPosition(0);
995 }
996 IF_LOG_COMMANDS() {
997 TextOutput::Bundle _b(alog);
998 alog << "Remaining data size: " << mOut.dataSize() << endl;
999 alog << "Received commands from driver: " << indent;
1000 const void* cmds = mIn.data();
1001 const void* end = mIn.data() + mIn.dataSize();
1002 alog << HexDump(cmds, mIn.dataSize()) << endl;
1003 while (cmds < end) cmds = printReturnCommand(alog, cmds);
1004 alog << dedent;
1005 }
1006 return NO_ERROR;
1007 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001008
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 return err;
1010}
1011
1012status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1013 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1014{
1015 binder_transaction_data tr;
1016
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001017 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 tr.target.handle = handle;
1019 tr.code = code;
1020 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001021 tr.cookie = 0;
1022 tr.sender_pid = 0;
1023 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001024
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025 const status_t err = data.errorCheck();
1026 if (err == NO_ERROR) {
1027 tr.data_size = data.ipcDataSize();
1028 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001029 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030 tr.data.ptr.offsets = data.ipcObjects();
1031 } else if (statusBuffer) {
1032 tr.flags |= TF_STATUS_CODE;
1033 *statusBuffer = err;
1034 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -08001035 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001037 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 } else {
1039 return (mLastError = err);
1040 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 mOut.writeInt32(cmd);
1043 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 return NO_ERROR;
1046}
1047
1048sp<BBinder> the_context_object;
1049
1050void setTheContextObject(sp<BBinder> obj)
1051{
1052 the_context_object = obj;
1053}
1054
1055status_t IPCThreadState::executeCommand(int32_t cmd)
1056{
1057 BBinder* obj;
1058 RefBase::weakref_type* refs;
1059 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001060
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001061 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 case BR_ERROR:
1063 result = mIn.readInt32();
1064 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 case BR_OK:
1067 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001070 refs = (RefBase::weakref_type*)mIn.readPointer();
1071 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001072 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1074 refs, obj, refs->refBase());
1075 obj->incStrong(mProcess.get());
1076 IF_LOG_REMOTEREFS() {
1077 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1078 obj->printRefs();
1079 }
1080 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001081 mOut.writePointer((uintptr_t)refs);
1082 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001086 refs = (RefBase::weakref_type*)mIn.readPointer();
1087 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001088 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1090 refs, obj, refs->refBase());
1091 IF_LOG_REMOTEREFS() {
1092 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1093 obj->printRefs();
1094 }
1095 mPendingStrongDerefs.push(obj);
1096 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001099 refs = (RefBase::weakref_type*)mIn.readPointer();
1100 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 refs->incWeak(mProcess.get());
1102 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001103 mOut.writePointer((uintptr_t)refs);
1104 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001108 refs = (RefBase::weakref_type*)mIn.readPointer();
1109 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 // NOTE: This assertion is not valid, because the object may no
1111 // longer exist (thus the (BBinder*)cast above resulting in a different
1112 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001113 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1115 // refs, obj, refs->refBase());
1116 mPendingWeakDerefs.push(refs);
1117 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001120 refs = (RefBase::weakref_type*)mIn.readPointer();
1121 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 {
1124 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001125 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1127 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 mOut.writeInt32(BC_ACQUIRE_RESULT);
1130 mOut.writeInt32((int32_t)success);
1131 }
1132 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001133
Steven Morelandf0212002018-12-26 13:59:23 -08001134 case BR_TRANSACTION_SEC_CTX:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 case BR_TRANSACTION:
1136 {
Steven Morelandf0212002018-12-26 13:59:23 -08001137 binder_transaction_data_secctx tr_secctx;
1138 binder_transaction_data& tr = tr_secctx.transaction_data;
1139
1140 if (cmd == (int) BR_TRANSACTION_SEC_CTX) {
1141 result = mIn.read(&tr_secctx, sizeof(tr_secctx));
1142 } else {
1143 result = mIn.read(&tr, sizeof(tr));
1144 tr_secctx.secctx = 0;
1145 }
1146
Steve Block67263472012-01-09 18:35:44 +00001147 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 "Not enough command data for brTRANSACTION");
1149 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001150
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001151 //Record the fact that we're in a binder call.
1152 mIPCThreadStateBase->pushCurrentState(
1153 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001154 Parcel buffer;
1155 buffer.ipcSetDataReference(
1156 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1157 tr.data_size,
1158 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1159 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1160
1161 const pid_t origPid = mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001162 const char* origSid = mCallingSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001163 const uid_t origUid = mCallingUid;
1164 const int32_t origStrictModePolicy = mStrictModePolicy;
1165 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001166 const int32_t origWorkSource = mWorkSource;
1167 const bool origPropagateWorkSet = mPropagateWorkSource;
1168 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1169 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1170 // here to never propagate it.
1171 clearCallingWorkSource();
1172 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001173
1174 mCallingPid = tr.sender_pid;
Steven Morelandf0212002018-12-26 13:59:23 -08001175 mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001176 mCallingUid = tr.sender_euid;
1177 mLastTransactionBinderFlags = tr.flags;
1178
Steven Morelandf0212002018-12-26 13:59:23 -08001179 // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid,
1180 // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001183 status_t error;
1184 IF_LOG_TRANSACTIONS() {
1185 TextOutput::Bundle _b(alog);
1186 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1187 << " / obj " << tr.target.ptr << " / code "
1188 << TypeCode(tr.code) << ": " << indent << buffer
1189 << dedent << endl
1190 << "Data addr = "
1191 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1192 << ", offsets addr="
1193 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1194 }
1195 if (tr.target.ptr) {
1196 // We only have a weak reference on the target object, so we must first try to
1197 // safely acquire a strong reference before doing anything else with it.
1198 if (reinterpret_cast<RefBase::weakref_type*>(
1199 tr.target.ptr)->attemptIncStrong(this)) {
1200 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1201 &reply, tr.flags);
1202 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001203 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001204 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001205 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001206
Martijn Coenenea0090a2017-11-02 18:54:40 +00001207 } else {
1208 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001210
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001211 mIPCThreadStateBase->popCurrentState();
Steven Morelandf0212002018-12-26 13:59:23 -08001212 //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n",
1213 // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001214
Martijn Coenenea0090a2017-11-02 18:54:40 +00001215 if ((tr.flags & TF_ONE_WAY) == 0) {
1216 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1217 if (error < NO_ERROR) reply.setError(error);
1218 sendReply(reply, 0);
1219 } else {
1220 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1221 }
1222
1223 mCallingPid = origPid;
Steven Morelandf0212002018-12-26 13:59:23 -08001224 mCallingSid = origSid;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001225 mCallingUid = origUid;
1226 mStrictModePolicy = origStrictModePolicy;
1227 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001228 mWorkSource = origWorkSource;
1229 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 IF_LOG_TRANSACTIONS() {
1232 TextOutput::Bundle _b(alog);
1233 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1234 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1235 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001236
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 }
1238 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 case BR_DEAD_BINDER:
1241 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001242 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 proxy->sendObituary();
1244 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001245 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001247
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1249 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001250 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 proxy->getWeakRefs()->decWeak(proxy);
1252 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 case BR_FINISHED:
1255 result = TIMED_OUT;
1256 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 case BR_NOOP:
1259 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 case BR_SPAWN_LOOPER:
1262 mProcess->spawnPooledThread(false);
1263 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 default:
liangweikanga43ee152016-10-25 16:37:54 +08001266 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 result = UNKNOWN_ERROR;
1268 break;
1269 }
1270
1271 if (result != NO_ERROR) {
1272 mLastError = result;
1273 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 return result;
1276}
1277
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001278bool IPCThreadState::isServingCall() const {
1279 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1280}
1281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282void IPCThreadState::threadDestructor(void *st)
1283{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001284 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1285 if (self) {
1286 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001287#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001288 if (self->mProcess->mDriverFD > 0) {
1289 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1290 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001292 delete self;
1293 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294}
1295
1296
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001297void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1298 size_t /*dataSize*/,
1299 const binder_size_t* /*objects*/,
1300 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301{
Steve Blocka19954a2012-01-04 20:05:49 +00001302 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 IF_LOG_COMMANDS() {
1304 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1305 }
Steve Block67263472012-01-09 18:35:44 +00001306 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001307 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 IPCThreadState* state = self();
1309 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001310 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311}
1312
1313}; // namespace android