blob: 1d4e234558e90c41b1f4c95cc1af3387d3338a9f [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",
91 "BR_FAILED_REPLY"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092};
93
94static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 "BC_TRANSACTION",
96 "BC_REPLY",
97 "BC_ACQUIRE_RESULT",
98 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 "BC_INCREFS",
100 "BC_ACQUIRE",
101 "BC_RELEASE",
102 "BC_DECREFS",
103 "BC_INCREFS_DONE",
104 "BC_ACQUIRE_DONE",
105 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 "BC_REGISTER_LOOPER",
107 "BC_ENTER_LOOPER",
108 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 "BC_REQUEST_DEATH_NOTIFICATION",
110 "BC_CLEAR_DEATH_NOTIFICATION",
111 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112};
113
Olivier Gaillard91a04802018-11-14 17:32:41 +0000114static const int64_t kWorkSourcePropagatedBitIndex = 32;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100115
songjinshi73a7dde2016-10-18 21:05:56 +0800116static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117{
songjinshi73a7dde2016-10-18 21:05:56 +0800118 size_t idx = cmd & 0xff;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
120 return kReturnStrings[idx];
121 else
122 return "unknown";
123}
124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125static const void* printBinderTransactionData(TextOutput& out, const void* data)
126{
127 const binder_transaction_data* btd =
128 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700129 if (btd->target.handle < 1024) {
130 /* want to print descriptors in decimal; guess based on value */
131 out << "target.desc=" << btd->target.handle;
132 } else {
133 out << "target.ptr=" << btd->target.ptr;
134 }
135 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700136 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
138 << " bytes)" << endl
139 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700140 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 return btd+1;
142}
143
144static const void* printReturnCommand(TextOutput& out, const void* _cmd)
145{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700146 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100148 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700149 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100150 if (code == BR_ERROR) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700151 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700153 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 out << "Unknown reply: " << code << endl;
155 return cmd;
156 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700157 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 switch (code) {
160 case BR_TRANSACTION:
161 case BR_REPLY: {
162 out << ": " << indent;
163 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
164 out << dedent;
165 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 case BR_ACQUIRE_RESULT: {
168 const int32_t res = *cmd++;
169 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
170 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800171
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 case BR_INCREFS:
173 case BR_ACQUIRE:
174 case BR_RELEASE:
175 case BR_DECREFS: {
176 const int32_t b = *cmd++;
177 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700178 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 case BR_ATTEMPT_ACQUIRE: {
182 const int32_t p = *cmd++;
183 const int32_t b = *cmd++;
184 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700185 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 << "), pri=" << p;
187 } break;
188
189 case BR_DEAD_BINDER:
190 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
191 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700192 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700194
195 default:
196 // no details to show for: BR_OK, BR_DEAD_REPLY,
197 // BR_TRANSACTION_COMPLETE, BR_FINISHED
198 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 out << endl;
202 return cmd;
203}
204
205static const void* printCommand(TextOutput& out, const void* _cmd)
206{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700207 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100209 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700210 size_t cmdIndex = code & 0xff;
211
212 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 out << "Unknown command: " << code << endl;
214 return cmd;
215 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700216 out << kCommandStrings[cmdIndex];
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 switch (code) {
219 case BC_TRANSACTION:
220 case BC_REPLY: {
221 out << ": " << indent;
222 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
223 out << dedent;
224 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 case BC_ACQUIRE_RESULT: {
227 const int32_t res = *cmd++;
228 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
229 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 case BC_FREE_BUFFER: {
232 const int32_t buf = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700233 out << ": buffer=" << (void*)(long)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 case BC_INCREFS:
237 case BC_ACQUIRE:
238 case BC_RELEASE:
239 case BC_DECREFS: {
240 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700241 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 case BC_INCREFS_DONE:
245 case BC_ACQUIRE_DONE: {
246 const int32_t b = *cmd++;
247 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700248 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 case BC_ATTEMPT_ACQUIRE: {
252 const int32_t p = *cmd++;
253 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700254 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800256
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 case BC_REQUEST_DEATH_NOTIFICATION:
258 case BC_CLEAR_DEATH_NOTIFICATION: {
259 const int32_t h = *cmd++;
260 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700261 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 } break;
263
264 case BC_DEAD_BINDER_DONE: {
265 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700266 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700268
269 default:
270 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
271 // BC_EXIT_LOOPER
272 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 out << endl;
276 return cmd;
277}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278
279static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
280static bool gHaveTLS = false;
281static pthread_key_t gTLS = 0;
282static bool gShutdown = false;
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800283static bool gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284
285IPCThreadState* IPCThreadState::self()
286{
287 if (gHaveTLS) {
288restart:
289 const pthread_key_t k = gTLS;
290 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
291 if (st) return st;
292 return new IPCThreadState;
293 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800294
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800295 if (gShutdown) {
296 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700297 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800298 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800299
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 pthread_mutex_lock(&gTLSMutex);
301 if (!gHaveTLS) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800302 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
303 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800305 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
306 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700307 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 }
309 gHaveTLS = true;
310 }
311 pthread_mutex_unlock(&gTLSMutex);
312 goto restart;
313}
314
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800315IPCThreadState* IPCThreadState::selfOrNull()
316{
317 if (gHaveTLS) {
318 const pthread_key_t k = gTLS;
319 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
320 return st;
321 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700322 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800323}
324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325void IPCThreadState::shutdown()
326{
327 gShutdown = true;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800328
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 if (gHaveTLS) {
330 // XXX Need to wait for all thread pool threads to exit!
331 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
332 if (st) {
333 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700334 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 }
zhongjieff405782016-03-09 15:05:04 +0800336 pthread_key_delete(gTLS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 gHaveTLS = false;
338 }
339}
340
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800341void IPCThreadState::disableBackgroundScheduling(bool disable)
342{
343 gDisableBackgroundScheduling = disable;
344}
345
Martijn Coenen2b631742017-05-05 11:16:59 -0700346bool IPCThreadState::backgroundSchedulingDisabled()
347{
348 return gDisableBackgroundScheduling;
349}
350
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351sp<ProcessState> IPCThreadState::process()
352{
353 return mProcess;
354}
355
356status_t IPCThreadState::clearLastError()
357{
358 const status_t err = mLastError;
359 mLastError = NO_ERROR;
360 return err;
361}
362
Dan Stoza9c634fd2014-11-26 12:23:23 -0800363pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364{
365 return mCallingPid;
366}
367
Dan Stoza9c634fd2014-11-26 12:23:23 -0800368uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369{
370 return mCallingUid;
371}
372
373int64_t IPCThreadState::clearCallingIdentity()
374{
375 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
376 clearCaller();
377 return token;
378}
379
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700380void IPCThreadState::setStrictModePolicy(int32_t policy)
381{
382 mStrictModePolicy = policy;
383}
384
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700385int32_t IPCThreadState::getStrictModePolicy() const
386{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700387 return mStrictModePolicy;
388}
389
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000390int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100391{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000392 int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
393 mPropagateWorkSource = true;
394 return token;
395}
396
397int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
398{
399 const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
400 int64_t token = propagatedBit | mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100401 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000402 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100403}
404
Olivier Gaillard91a04802018-11-14 17:32:41 +0000405void IPCThreadState::clearPropagateWorkSource()
406{
407 mPropagateWorkSource = false;
408}
409
410bool IPCThreadState::shouldPropagateWorkSource() const
411{
412 return mPropagateWorkSource;
413}
414
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000415uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100416{
417 return mWorkSource;
418}
419
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000420int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100421{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000422 return setCallingWorkSourceUid(kUnsetWorkSource);
423}
424
425void IPCThreadState::restoreCallingWorkSource(int64_t token)
426{
427 uid_t uid = (int)token;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000428 setCallingWorkSourceUidWithoutPropagation(uid);
429 mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100430}
431
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700432void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
433{
434 mLastTransactionBinderFlags = flags;
435}
436
437int32_t IPCThreadState::getLastTransactionBinderFlags() const
438{
439 return mLastTransactionBinderFlags;
440}
441
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442void IPCThreadState::restoreCallingIdentity(int64_t token)
443{
444 mCallingUid = (int)(token>>32);
445 mCallingPid = (int)token;
446}
447
448void IPCThreadState::clearCaller()
449{
Marco Nelissend43b1942009-07-17 07:59:17 -0700450 mCallingPid = getpid();
451 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452}
453
454void IPCThreadState::flushCommands()
455{
456 if (mProcess->mDriverFD <= 0)
457 return;
458 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700459 // The flush could have caused post-write refcount decrements to have
460 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
461 // being queued in mOut. So flush again, if we need to.
462 if (mOut.dataSize() > 0) {
463 talkWithDriver(false);
464 }
465 if (mOut.dataSize() > 0) {
466 ALOGW("mOut.dataSize() > 0 after flushCommands()");
467 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468}
469
Wale Ogunwale376b8222015-04-13 16:16:10 -0700470void IPCThreadState::blockUntilThreadAvailable()
471{
472 pthread_mutex_lock(&mProcess->mThreadCountLock);
473 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700474 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
475 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
476 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700477 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
478 }
479 pthread_mutex_unlock(&mProcess->mThreadCountLock);
480}
481
Todd Poynor8d96cab2013-06-25 19:12:18 -0700482status_t IPCThreadState::getAndExecuteCommand()
483{
484 status_t result;
485 int32_t cmd;
486
487 result = talkWithDriver();
488 if (result >= NO_ERROR) {
489 size_t IN = mIn.dataAvail();
490 if (IN < sizeof(int32_t)) return result;
491 cmd = mIn.readInt32();
492 IF_LOG_COMMANDS() {
493 alog << "Processing top-level Command: "
494 << getReturnString(cmd) << endl;
495 }
496
Wale Ogunwale376b8222015-04-13 16:16:10 -0700497 pthread_mutex_lock(&mProcess->mThreadCountLock);
498 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700499 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
500 mProcess->mStarvationStartTimeMs == 0) {
501 mProcess->mStarvationStartTimeMs = uptimeMillis();
502 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700503 pthread_mutex_unlock(&mProcess->mThreadCountLock);
504
Todd Poynor8d96cab2013-06-25 19:12:18 -0700505 result = executeCommand(cmd);
506
Wale Ogunwale376b8222015-04-13 16:16:10 -0700507 pthread_mutex_lock(&mProcess->mThreadCountLock);
508 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700509 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
510 mProcess->mStarvationStartTimeMs != 0) {
511 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
512 if (starvationTimeMs > 100) {
513 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
514 mProcess->mMaxThreads, starvationTimeMs);
515 }
516 mProcess->mStarvationStartTimeMs = 0;
517 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700518 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
519 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700520 }
521
522 return result;
523}
524
525// When we've cleared the incoming command queue, process any pending derefs
526void IPCThreadState::processPendingDerefs()
527{
528 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200529 /*
530 * The decWeak()/decStrong() calls may cause a destructor to run,
531 * which in turn could have initiated an outgoing transaction,
532 * which in turn could cause us to add to the pending refs
533 * vectors; so instead of simply iterating, loop until they're empty.
534 *
535 * We do this in an outer loop, because calling decStrong()
536 * may result in something being added to mPendingWeakDerefs,
537 * which could be delayed until the next incoming command
538 * from the driver if we don't process it now.
539 */
540 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
541 while (mPendingWeakDerefs.size() > 0) {
542 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
543 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700544 refs->decWeak(mProcess.get());
545 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700546
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200547 if (mPendingStrongDerefs.size() > 0) {
548 // We don't use while() here because we don't want to re-order
549 // strong and weak decs at all; if this decStrong() causes both a
550 // decWeak() and a decStrong() to be queued, we want to process
551 // the decWeak() first.
552 BBinder* obj = mPendingStrongDerefs[0];
553 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700554 obj->decStrong(mProcess.get());
555 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700556 }
557 }
558}
559
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700560void IPCThreadState::processPostWriteDerefs()
561{
562 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
563 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
564 refs->decWeak(mProcess.get());
565 }
566 mPostWriteWeakDerefs.clear();
567
568 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
569 RefBase* obj = mPostWriteStrongDerefs[i];
570 obj->decStrong(mProcess.get());
571 }
572 mPostWriteStrongDerefs.clear();
573}
574
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575void IPCThreadState::joinThreadPool(bool isMain)
576{
577 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
578
579 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800580
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 status_t result;
582 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700583 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700585 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800586
Todd Poynor8d96cab2013-06-25 19:12:18 -0700587 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
588 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700589 mProcess->mDriverFD, result);
590 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800592
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 // Let this thread exit the thread pool if it is no longer
594 // needed and it is not the main process thread.
595 if(result == TIMED_OUT && !isMain) {
596 break;
597 }
598 } while (result != -ECONNREFUSED && result != -EBADF);
599
Wei Wangc7341432016-10-19 10:23:59 -0700600 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
601 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800602
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 mOut.writeInt32(BC_EXIT_LOOPER);
604 talkWithDriver(false);
605}
606
Todd Poynor8d96cab2013-06-25 19:12:18 -0700607int IPCThreadState::setupPolling(int* fd)
608{
609 if (mProcess->mDriverFD <= 0) {
610 return -EBADF;
611 }
612
613 mOut.writeInt32(BC_ENTER_LOOPER);
614 *fd = mProcess->mDriverFD;
615 return 0;
616}
617
618status_t IPCThreadState::handlePolledCommands()
619{
620 status_t result;
621
622 do {
623 result = getAndExecuteCommand();
624 } while (mIn.dataPosition() < mIn.dataSize());
625
626 processPendingDerefs();
627 flushCommands();
628 return result;
629}
630
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800631void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632{
Steve Blocka19954a2012-01-04 20:05:49 +0000633 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 flushCommands();
635 int fd = mProcess->mDriverFD;
636 mProcess->mDriverFD = -1;
637 close(fd);
638 //kill(getpid(), SIGKILL);
639}
640
641status_t IPCThreadState::transact(int32_t handle,
642 uint32_t code, const Parcel& data,
643 Parcel* reply, uint32_t flags)
644{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800645 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
647 flags |= TF_ACCEPT_FDS;
648
649 IF_LOG_TRANSACTIONS() {
650 TextOutput::Bundle _b(alog);
651 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
652 << handle << " / code " << TypeCode(code) << ": "
653 << indent << data << dedent << endl;
654 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800655
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800656 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
657 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700658 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800659
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 if (err != NO_ERROR) {
661 if (reply) reply->setError(err);
662 return (mLastError = err);
663 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800664
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665 if ((flags & TF_ONE_WAY) == 0) {
Steven Moreland7732a092019-01-02 17:54:16 -0800666 if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) {
667 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
668 ALOGE("Process making non-oneway call but is restricted.");
669 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
670 ANDROID_LOG_ERROR);
671 } else /* FATAL_IF_NOT_ONEWAY */ {
672 LOG_ALWAYS_FATAL("Process may not make oneway calls.");
673 }
674 }
675
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700676 #if 0
677 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000678 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700679 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000680 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700681 }
682 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 if (reply) {
684 err = waitForResponse(reply);
685 } else {
686 Parcel fakeReply;
687 err = waitForResponse(&fakeReply);
688 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700689 #if 0
690 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000691 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700692 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000693 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700694 }
695 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800696
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 IF_LOG_TRANSACTIONS() {
698 TextOutput::Bundle _b(alog);
699 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
700 << handle << ": ";
701 if (reply) alog << indent << *reply << dedent << endl;
702 else alog << "(none requested)" << endl;
703 }
704 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700705 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800707
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708 return err;
709}
710
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700711void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712{
713 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
714 mOut.writeInt32(BC_ACQUIRE);
715 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700716 // Create a temp reference until the driver has handled this command.
717 proxy->incStrong(mProcess.get());
718 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719}
720
721void IPCThreadState::decStrongHandle(int32_t handle)
722{
723 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
724 mOut.writeInt32(BC_RELEASE);
725 mOut.writeInt32(handle);
726}
727
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700728void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729{
730 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
731 mOut.writeInt32(BC_INCREFS);
732 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700733 // Create a temp reference until the driver has handled this command.
734 proxy->getWeakRefs()->incWeak(mProcess.get());
735 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736}
737
738void IPCThreadState::decWeakHandle(int32_t handle)
739{
740 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
741 mOut.writeInt32(BC_DECREFS);
742 mOut.writeInt32(handle);
743}
744
745status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
746{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800747#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700748 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
750 mOut.writeInt32(0); // xxx was thread priority
751 mOut.writeInt32(handle);
752 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800753
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800755
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800757 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
759#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800760
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800762#else
763 (void)handle;
764 ALOGE("%s(%d): Not supported\n", __func__, handle);
765 return INVALID_OPERATION;
766#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767}
768
769void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
770{
771#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800772 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700774 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775}
776
777status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
778{
779 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
780 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000781 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 return NO_ERROR;
783}
784
785status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
786{
787 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
788 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000789 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 return NO_ERROR;
791}
792
793IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700794 : mProcess(ProcessState::self()),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100795 mWorkSource(kUnsetWorkSource),
Olivier Gaillard91a04802018-11-14 17:32:41 +0000796 mPropagateWorkSource(false),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700797 mStrictModePolicy(0),
Steven Moreland7732a092019-01-02 17:54:16 -0800798 mLastTransactionBinderFlags(0),
799 mCallRestriction(mProcess->mCallRestriction)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800{
801 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800802 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 mIn.setDataCapacity(256);
804 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000805 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806}
807
808IPCThreadState::~IPCThreadState()
809{
810}
811
Martijn Coenenea0090a2017-11-02 18:54:40 +0000812status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
813{
814 status_t err;
815 status_t statusBuffer;
816 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
817 if (err < NO_ERROR) return err;
818
Yi Kongfdd8da92018-06-07 17:52:27 -0700819 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000820}
821
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
823{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100824 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 int32_t err;
826
827 while (1) {
828 if ((err=talkWithDriver()) < NO_ERROR) break;
829 err = mIn.errorCheck();
830 if (err < NO_ERROR) break;
831 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800832
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100833 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835 IF_LOG_COMMANDS() {
836 alog << "Processing waitForResponse Command: "
837 << getReturnString(cmd) << endl;
838 }
839
840 switch (cmd) {
841 case BR_TRANSACTION_COMPLETE:
842 if (!reply && !acquireResult) goto finish;
843 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800844
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 case BR_DEAD_REPLY:
846 err = DEAD_OBJECT;
847 goto finish;
848
849 case BR_FAILED_REPLY:
850 err = FAILED_TRANSACTION;
851 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800852
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853 case BR_ACQUIRE_RESULT:
854 {
Steve Block67263472012-01-09 18:35:44 +0000855 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 const int32_t result = mIn.readInt32();
857 if (!acquireResult) continue;
858 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
859 }
860 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800861
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 case BR_REPLY:
863 {
864 binder_transaction_data tr;
865 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000866 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 if (err != NO_ERROR) goto finish;
868
869 if (reply) {
870 if ((tr.flags & TF_STATUS_CODE) == 0) {
871 reply->ipcSetDataReference(
872 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
873 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800874 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
875 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 freeBuffer, this);
877 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800878 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700879 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
881 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800882 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
883 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 }
885 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700886 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
888 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800889 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
890 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 continue;
892 }
893 }
894 goto finish;
895
896 default:
897 err = executeCommand(cmd);
898 if (err != NO_ERROR) goto finish;
899 break;
900 }
901 }
902
903finish:
904 if (err != NO_ERROR) {
905 if (acquireResult) *acquireResult = err;
906 if (reply) reply->setError(err);
907 mLastError = err;
908 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800909
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 return err;
911}
912
913status_t IPCThreadState::talkWithDriver(bool doReceive)
914{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100915 if (mProcess->mDriverFD <= 0) {
916 return -EBADF;
917 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800918
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800920
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 // Is the read buffer empty?
922 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800923
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 // We don't want to write anything if we are still reading
925 // from data left in the input buffer and the caller
926 // has requested to read the next data.
927 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800930 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931
932 // This is what we'll read.
933 if (doReceive && needRead) {
934 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800935 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 } else {
937 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800938 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700940
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 IF_LOG_COMMANDS() {
942 TextOutput::Bundle _b(alog);
943 if (outAvail != 0) {
944 alog << "Sending commands to driver: " << indent;
945 const void* cmds = (const void*)bwr.write_buffer;
946 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
947 alog << HexDump(cmds, bwr.write_size) << endl;
948 while (cmds < end) cmds = printCommand(alog, cmds);
949 alog << dedent;
950 }
951 alog << "Size of receive buffer: " << bwr.read_size
952 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
953 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800954
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 // Return immediately if there is nothing to do.
956 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700957
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958 bwr.write_consumed = 0;
959 bwr.read_consumed = 0;
960 status_t err;
961 do {
962 IF_LOG_COMMANDS() {
963 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
964 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700965#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
967 err = NO_ERROR;
968 else
969 err = -errno;
970#else
971 err = INVALID_OPERATION;
972#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100973 if (mProcess->mDriverFD <= 0) {
974 err = -EBADF;
975 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 IF_LOG_COMMANDS() {
977 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
978 }
979 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700980
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800982 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700984 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 }
986
987 if (err >= NO_ERROR) {
988 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800989 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700991 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700993 processPostWriteDerefs();
994 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 }
996 if (bwr.read_consumed > 0) {
997 mIn.setDataSize(bwr.read_consumed);
998 mIn.setDataPosition(0);
999 }
1000 IF_LOG_COMMANDS() {
1001 TextOutput::Bundle _b(alog);
1002 alog << "Remaining data size: " << mOut.dataSize() << endl;
1003 alog << "Received commands from driver: " << indent;
1004 const void* cmds = mIn.data();
1005 const void* end = mIn.data() + mIn.dataSize();
1006 alog << HexDump(cmds, mIn.dataSize()) << endl;
1007 while (cmds < end) cmds = printReturnCommand(alog, cmds);
1008 alog << dedent;
1009 }
1010 return NO_ERROR;
1011 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001012
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 return err;
1014}
1015
1016status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1017 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1018{
1019 binder_transaction_data tr;
1020
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001021 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 tr.target.handle = handle;
1023 tr.code = code;
1024 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001025 tr.cookie = 0;
1026 tr.sender_pid = 0;
1027 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001028
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 const status_t err = data.errorCheck();
1030 if (err == NO_ERROR) {
1031 tr.data_size = data.ipcDataSize();
1032 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001033 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 tr.data.ptr.offsets = data.ipcObjects();
1035 } else if (statusBuffer) {
1036 tr.flags |= TF_STATUS_CODE;
1037 *statusBuffer = err;
1038 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -08001039 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001041 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 } else {
1043 return (mLastError = err);
1044 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 mOut.writeInt32(cmd);
1047 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049 return NO_ERROR;
1050}
1051
1052sp<BBinder> the_context_object;
1053
1054void setTheContextObject(sp<BBinder> obj)
1055{
1056 the_context_object = obj;
1057}
1058
1059status_t IPCThreadState::executeCommand(int32_t cmd)
1060{
1061 BBinder* obj;
1062 RefBase::weakref_type* refs;
1063 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001064
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001065 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 case BR_ERROR:
1067 result = mIn.readInt32();
1068 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001069
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 case BR_OK:
1071 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001074 refs = (RefBase::weakref_type*)mIn.readPointer();
1075 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001076 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1078 refs, obj, refs->refBase());
1079 obj->incStrong(mProcess.get());
1080 IF_LOG_REMOTEREFS() {
1081 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1082 obj->printRefs();
1083 }
1084 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001085 mOut.writePointer((uintptr_t)refs);
1086 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001090 refs = (RefBase::weakref_type*)mIn.readPointer();
1091 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001092 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1094 refs, obj, refs->refBase());
1095 IF_LOG_REMOTEREFS() {
1096 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1097 obj->printRefs();
1098 }
1099 mPendingStrongDerefs.push(obj);
1100 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001103 refs = (RefBase::weakref_type*)mIn.readPointer();
1104 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 refs->incWeak(mProcess.get());
1106 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001107 mOut.writePointer((uintptr_t)refs);
1108 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001112 refs = (RefBase::weakref_type*)mIn.readPointer();
1113 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 // NOTE: This assertion is not valid, because the object may no
1115 // longer exist (thus the (BBinder*)cast above resulting in a different
1116 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001117 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1119 // refs, obj, refs->refBase());
1120 mPendingWeakDerefs.push(refs);
1121 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001124 refs = (RefBase::weakref_type*)mIn.readPointer();
1125 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 {
1128 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001129 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1131 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133 mOut.writeInt32(BC_ACQUIRE_RESULT);
1134 mOut.writeInt32((int32_t)success);
1135 }
1136 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 case BR_TRANSACTION:
1139 {
1140 binder_transaction_data tr;
1141 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001142 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 "Not enough command data for brTRANSACTION");
1144 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001145
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001146 //Record the fact that we're in a binder call.
1147 mIPCThreadStateBase->pushCurrentState(
1148 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001149 Parcel buffer;
1150 buffer.ipcSetDataReference(
1151 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1152 tr.data_size,
1153 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1154 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1155
1156 const pid_t origPid = mCallingPid;
1157 const uid_t origUid = mCallingUid;
1158 const int32_t origStrictModePolicy = mStrictModePolicy;
1159 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001160 const int32_t origWorkSource = mWorkSource;
1161 const bool origPropagateWorkSet = mPropagateWorkSource;
1162 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1163 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1164 // here to never propagate it.
1165 clearCallingWorkSource();
1166 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001167
1168 mCallingPid = tr.sender_pid;
1169 mCallingUid = tr.sender_euid;
1170 mLastTransactionBinderFlags = tr.flags;
1171
1172 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
1173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001175 status_t error;
1176 IF_LOG_TRANSACTIONS() {
1177 TextOutput::Bundle _b(alog);
1178 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1179 << " / obj " << tr.target.ptr << " / code "
1180 << TypeCode(tr.code) << ": " << indent << buffer
1181 << dedent << endl
1182 << "Data addr = "
1183 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1184 << ", offsets addr="
1185 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1186 }
1187 if (tr.target.ptr) {
1188 // We only have a weak reference on the target object, so we must first try to
1189 // safely acquire a strong reference before doing anything else with it.
1190 if (reinterpret_cast<RefBase::weakref_type*>(
1191 tr.target.ptr)->attemptIncStrong(this)) {
1192 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1193 &reply, tr.flags);
1194 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001195 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001196 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001197 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001198
Martijn Coenenea0090a2017-11-02 18:54:40 +00001199 } else {
1200 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001202
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001203 mIPCThreadStateBase->popCurrentState();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001204 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
1205 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001206
Martijn Coenenea0090a2017-11-02 18:54:40 +00001207 if ((tr.flags & TF_ONE_WAY) == 0) {
1208 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1209 if (error < NO_ERROR) reply.setError(error);
1210 sendReply(reply, 0);
1211 } else {
1212 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1213 }
1214
1215 mCallingPid = origPid;
1216 mCallingUid = origUid;
1217 mStrictModePolicy = origStrictModePolicy;
1218 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001219 mWorkSource = origWorkSource;
1220 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 IF_LOG_TRANSACTIONS() {
1223 TextOutput::Bundle _b(alog);
1224 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1225 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1226 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 }
1229 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 case BR_DEAD_BINDER:
1232 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001233 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 proxy->sendObituary();
1235 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001236 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1240 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001241 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 proxy->getWeakRefs()->decWeak(proxy);
1243 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001244
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 case BR_FINISHED:
1246 result = TIMED_OUT;
1247 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 case BR_NOOP:
1250 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 case BR_SPAWN_LOOPER:
1253 mProcess->spawnPooledThread(false);
1254 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 default:
liangweikanga43ee152016-10-25 16:37:54 +08001257 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 result = UNKNOWN_ERROR;
1259 break;
1260 }
1261
1262 if (result != NO_ERROR) {
1263 mLastError = result;
1264 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 return result;
1267}
1268
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001269bool IPCThreadState::isServingCall() const {
1270 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1271}
1272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273void IPCThreadState::threadDestructor(void *st)
1274{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001275 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1276 if (self) {
1277 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001278#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001279 if (self->mProcess->mDriverFD > 0) {
1280 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1281 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001283 delete self;
1284 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285}
1286
1287
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001288void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1289 size_t /*dataSize*/,
1290 const binder_size_t* /*objects*/,
1291 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292{
Steve Blocka19954a2012-01-04 20:05:49 +00001293 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 IF_LOG_COMMANDS() {
1295 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1296 }
Steve Block67263472012-01-09 18:35:44 +00001297 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001298 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 IPCThreadState* state = self();
1300 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001301 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302}
1303
1304}; // namespace android