blob: 8df83f1afe3a4d3e28045ab135e230012c235d2b [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",
89 "BR_FAILED_REPLY"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090};
91
92static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 "BC_TRANSACTION",
94 "BC_REPLY",
95 "BC_ACQUIRE_RESULT",
96 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 "BC_INCREFS",
98 "BC_ACQUIRE",
99 "BC_RELEASE",
100 "BC_DECREFS",
101 "BC_INCREFS_DONE",
102 "BC_ACQUIRE_DONE",
103 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 "BC_REGISTER_LOOPER",
105 "BC_ENTER_LOOPER",
106 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 "BC_REQUEST_DEATH_NOTIFICATION",
108 "BC_CLEAR_DEATH_NOTIFICATION",
109 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110};
111
Olivier Gaillard91a04802018-11-14 17:32:41 +0000112static const int64_t kWorkSourcePropagatedBitIndex = 32;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100113
songjinshi73a7dde2016-10-18 21:05:56 +0800114static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115{
songjinshi73a7dde2016-10-18 21:05:56 +0800116 size_t idx = cmd & 0xff;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
118 return kReturnStrings[idx];
119 else
120 return "unknown";
121}
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123static const void* printBinderTransactionData(TextOutput& out, const void* data)
124{
125 const binder_transaction_data* btd =
126 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700127 if (btd->target.handle < 1024) {
128 /* want to print descriptors in decimal; guess based on value */
129 out << "target.desc=" << btd->target.handle;
130 } else {
131 out << "target.ptr=" << btd->target.ptr;
132 }
133 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700134 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
136 << " bytes)" << endl
137 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700138 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 return btd+1;
140}
141
142static const void* printReturnCommand(TextOutput& out, const void* _cmd)
143{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700144 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100146 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700147 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100148 if (code == BR_ERROR) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700149 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700151 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 out << "Unknown reply: " << code << endl;
153 return cmd;
154 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700155 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 switch (code) {
158 case BR_TRANSACTION:
159 case BR_REPLY: {
160 out << ": " << indent;
161 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
162 out << dedent;
163 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 case BR_ACQUIRE_RESULT: {
166 const int32_t res = *cmd++;
167 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
168 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 case BR_INCREFS:
171 case BR_ACQUIRE:
172 case BR_RELEASE:
173 case BR_DECREFS: {
174 const int32_t b = *cmd++;
175 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700176 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 case BR_ATTEMPT_ACQUIRE: {
180 const int32_t p = *cmd++;
181 const int32_t b = *cmd++;
182 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700183 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 << "), pri=" << p;
185 } break;
186
187 case BR_DEAD_BINDER:
188 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
189 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700190 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700192
193 default:
194 // no details to show for: BR_OK, BR_DEAD_REPLY,
195 // BR_TRANSACTION_COMPLETE, BR_FINISHED
196 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 out << endl;
200 return cmd;
201}
202
203static const void* printCommand(TextOutput& out, const void* _cmd)
204{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700205 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100207 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700208 size_t cmdIndex = code & 0xff;
209
210 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 out << "Unknown command: " << code << endl;
212 return cmd;
213 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700214 out << kCommandStrings[cmdIndex];
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 switch (code) {
217 case BC_TRANSACTION:
218 case BC_REPLY: {
219 out << ": " << indent;
220 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
221 out << dedent;
222 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 case BC_ACQUIRE_RESULT: {
225 const int32_t res = *cmd++;
226 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
227 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 case BC_FREE_BUFFER: {
230 const int32_t buf = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700231 out << ": buffer=" << (void*)(long)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 case BC_INCREFS:
235 case BC_ACQUIRE:
236 case BC_RELEASE:
237 case BC_DECREFS: {
238 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700239 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 case BC_INCREFS_DONE:
243 case BC_ACQUIRE_DONE: {
244 const int32_t b = *cmd++;
245 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700246 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 case BC_ATTEMPT_ACQUIRE: {
250 const int32_t p = *cmd++;
251 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700252 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800254
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 case BC_REQUEST_DEATH_NOTIFICATION:
256 case BC_CLEAR_DEATH_NOTIFICATION: {
257 const int32_t h = *cmd++;
258 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700259 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 } break;
261
262 case BC_DEAD_BINDER_DONE: {
263 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700264 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700266
267 default:
268 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
269 // BC_EXIT_LOOPER
270 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 out << endl;
274 return cmd;
275}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276
277static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
278static bool gHaveTLS = false;
279static pthread_key_t gTLS = 0;
280static bool gShutdown = false;
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800281static bool gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282
283IPCThreadState* IPCThreadState::self()
284{
285 if (gHaveTLS) {
286restart:
287 const pthread_key_t k = gTLS;
288 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
289 if (st) return st;
290 return new IPCThreadState;
291 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800292
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800293 if (gShutdown) {
294 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700295 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800296 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 pthread_mutex_lock(&gTLSMutex);
299 if (!gHaveTLS) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800300 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
301 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800303 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
304 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700305 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 }
307 gHaveTLS = true;
308 }
309 pthread_mutex_unlock(&gTLSMutex);
310 goto restart;
311}
312
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800313IPCThreadState* IPCThreadState::selfOrNull()
314{
315 if (gHaveTLS) {
316 const pthread_key_t k = gTLS;
317 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
318 return st;
319 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700320 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800321}
322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323void IPCThreadState::shutdown()
324{
325 gShutdown = true;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 if (gHaveTLS) {
328 // XXX Need to wait for all thread pool threads to exit!
329 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
330 if (st) {
331 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700332 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 }
zhongjieff405782016-03-09 15:05:04 +0800334 pthread_key_delete(gTLS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 gHaveTLS = false;
336 }
337}
338
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800339void IPCThreadState::disableBackgroundScheduling(bool disable)
340{
341 gDisableBackgroundScheduling = disable;
342}
343
Martijn Coenen2b631742017-05-05 11:16:59 -0700344bool IPCThreadState::backgroundSchedulingDisabled()
345{
346 return gDisableBackgroundScheduling;
347}
348
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349sp<ProcessState> IPCThreadState::process()
350{
351 return mProcess;
352}
353
354status_t IPCThreadState::clearLastError()
355{
356 const status_t err = mLastError;
357 mLastError = NO_ERROR;
358 return err;
359}
360
Dan Stoza9c634fd2014-11-26 12:23:23 -0800361pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362{
363 return mCallingPid;
364}
365
Dan Stoza9c634fd2014-11-26 12:23:23 -0800366uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367{
368 return mCallingUid;
369}
370
371int64_t IPCThreadState::clearCallingIdentity()
372{
373 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
374 clearCaller();
375 return token;
376}
377
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700378void IPCThreadState::setStrictModePolicy(int32_t policy)
379{
380 mStrictModePolicy = policy;
381}
382
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700383int32_t IPCThreadState::getStrictModePolicy() const
384{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700385 return mStrictModePolicy;
386}
387
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000388int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid)
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100389{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000390 int64_t token = setCallingWorkSourceUidWithoutPropagation(uid);
391 mPropagateWorkSource = true;
392 return token;
393}
394
395int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid)
396{
397 const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex;
398 int64_t token = propagatedBit | mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100399 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000400 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100401}
402
Olivier Gaillard91a04802018-11-14 17:32:41 +0000403void IPCThreadState::clearPropagateWorkSource()
404{
405 mPropagateWorkSource = false;
406}
407
408bool IPCThreadState::shouldPropagateWorkSource() const
409{
410 return mPropagateWorkSource;
411}
412
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000413uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100414{
415 return mWorkSource;
416}
417
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000418int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100419{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000420 return setCallingWorkSourceUid(kUnsetWorkSource);
421}
422
423void IPCThreadState::restoreCallingWorkSource(int64_t token)
424{
425 uid_t uid = (int)token;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000426 setCallingWorkSourceUidWithoutPropagation(uid);
427 mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100428}
429
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700430void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
431{
432 mLastTransactionBinderFlags = flags;
433}
434
435int32_t IPCThreadState::getLastTransactionBinderFlags() const
436{
437 return mLastTransactionBinderFlags;
438}
439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440void IPCThreadState::restoreCallingIdentity(int64_t token)
441{
442 mCallingUid = (int)(token>>32);
443 mCallingPid = (int)token;
444}
445
446void IPCThreadState::clearCaller()
447{
Marco Nelissend43b1942009-07-17 07:59:17 -0700448 mCallingPid = getpid();
449 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450}
451
452void IPCThreadState::flushCommands()
453{
454 if (mProcess->mDriverFD <= 0)
455 return;
456 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700457 // The flush could have caused post-write refcount decrements to have
458 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
459 // being queued in mOut. So flush again, if we need to.
460 if (mOut.dataSize() > 0) {
461 talkWithDriver(false);
462 }
463 if (mOut.dataSize() > 0) {
464 ALOGW("mOut.dataSize() > 0 after flushCommands()");
465 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466}
467
Wale Ogunwale376b8222015-04-13 16:16:10 -0700468void IPCThreadState::blockUntilThreadAvailable()
469{
470 pthread_mutex_lock(&mProcess->mThreadCountLock);
471 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700472 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
473 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
474 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700475 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
476 }
477 pthread_mutex_unlock(&mProcess->mThreadCountLock);
478}
479
Todd Poynor8d96cab2013-06-25 19:12:18 -0700480status_t IPCThreadState::getAndExecuteCommand()
481{
482 status_t result;
483 int32_t cmd;
484
485 result = talkWithDriver();
486 if (result >= NO_ERROR) {
487 size_t IN = mIn.dataAvail();
488 if (IN < sizeof(int32_t)) return result;
489 cmd = mIn.readInt32();
490 IF_LOG_COMMANDS() {
491 alog << "Processing top-level Command: "
492 << getReturnString(cmd) << endl;
493 }
494
Wale Ogunwale376b8222015-04-13 16:16:10 -0700495 pthread_mutex_lock(&mProcess->mThreadCountLock);
496 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700497 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
498 mProcess->mStarvationStartTimeMs == 0) {
499 mProcess->mStarvationStartTimeMs = uptimeMillis();
500 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700501 pthread_mutex_unlock(&mProcess->mThreadCountLock);
502
Todd Poynor8d96cab2013-06-25 19:12:18 -0700503 result = executeCommand(cmd);
504
Wale Ogunwale376b8222015-04-13 16:16:10 -0700505 pthread_mutex_lock(&mProcess->mThreadCountLock);
506 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700507 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
508 mProcess->mStarvationStartTimeMs != 0) {
509 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
510 if (starvationTimeMs > 100) {
511 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
512 mProcess->mMaxThreads, starvationTimeMs);
513 }
514 mProcess->mStarvationStartTimeMs = 0;
515 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700516 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
517 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700518 }
519
520 return result;
521}
522
523// When we've cleared the incoming command queue, process any pending derefs
524void IPCThreadState::processPendingDerefs()
525{
526 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200527 /*
528 * The decWeak()/decStrong() calls may cause a destructor to run,
529 * which in turn could have initiated an outgoing transaction,
530 * which in turn could cause us to add to the pending refs
531 * vectors; so instead of simply iterating, loop until they're empty.
532 *
533 * We do this in an outer loop, because calling decStrong()
534 * may result in something being added to mPendingWeakDerefs,
535 * which could be delayed until the next incoming command
536 * from the driver if we don't process it now.
537 */
538 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
539 while (mPendingWeakDerefs.size() > 0) {
540 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
541 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700542 refs->decWeak(mProcess.get());
543 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700544
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200545 if (mPendingStrongDerefs.size() > 0) {
546 // We don't use while() here because we don't want to re-order
547 // strong and weak decs at all; if this decStrong() causes both a
548 // decWeak() and a decStrong() to be queued, we want to process
549 // the decWeak() first.
550 BBinder* obj = mPendingStrongDerefs[0];
551 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700552 obj->decStrong(mProcess.get());
553 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700554 }
555 }
556}
557
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700558void IPCThreadState::processPostWriteDerefs()
559{
560 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
561 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
562 refs->decWeak(mProcess.get());
563 }
564 mPostWriteWeakDerefs.clear();
565
566 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
567 RefBase* obj = mPostWriteStrongDerefs[i];
568 obj->decStrong(mProcess.get());
569 }
570 mPostWriteStrongDerefs.clear();
571}
572
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573void IPCThreadState::joinThreadPool(bool isMain)
574{
575 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
576
577 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 status_t result;
580 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700581 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700583 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800584
Todd Poynor8d96cab2013-06-25 19:12:18 -0700585 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
586 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700587 mProcess->mDriverFD, result);
588 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800590
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 // Let this thread exit the thread pool if it is no longer
592 // needed and it is not the main process thread.
593 if(result == TIMED_OUT && !isMain) {
594 break;
595 }
596 } while (result != -ECONNREFUSED && result != -EBADF);
597
Wei Wangc7341432016-10-19 10:23:59 -0700598 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
599 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800600
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 mOut.writeInt32(BC_EXIT_LOOPER);
602 talkWithDriver(false);
603}
604
Todd Poynor8d96cab2013-06-25 19:12:18 -0700605int IPCThreadState::setupPolling(int* fd)
606{
607 if (mProcess->mDriverFD <= 0) {
608 return -EBADF;
609 }
610
611 mOut.writeInt32(BC_ENTER_LOOPER);
612 *fd = mProcess->mDriverFD;
613 return 0;
614}
615
616status_t IPCThreadState::handlePolledCommands()
617{
618 status_t result;
619
620 do {
621 result = getAndExecuteCommand();
622 } while (mIn.dataPosition() < mIn.dataSize());
623
624 processPendingDerefs();
625 flushCommands();
626 return result;
627}
628
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800629void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630{
Steve Blocka19954a2012-01-04 20:05:49 +0000631 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 flushCommands();
633 int fd = mProcess->mDriverFD;
634 mProcess->mDriverFD = -1;
635 close(fd);
636 //kill(getpid(), SIGKILL);
637}
638
639status_t IPCThreadState::transact(int32_t handle,
640 uint32_t code, const Parcel& data,
641 Parcel* reply, uint32_t flags)
642{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800643 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644
645 flags |= TF_ACCEPT_FDS;
646
647 IF_LOG_TRANSACTIONS() {
648 TextOutput::Bundle _b(alog);
649 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
650 << handle << " / code " << TypeCode(code) << ": "
651 << indent << data << dedent << endl;
652 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800653
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800654 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
655 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700656 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800657
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 if (err != NO_ERROR) {
659 if (reply) reply->setError(err);
660 return (mLastError = err);
661 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800662
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700664 #if 0
665 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000666 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700667 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000668 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700669 }
670 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 if (reply) {
672 err = waitForResponse(reply);
673 } else {
674 Parcel fakeReply;
675 err = waitForResponse(&fakeReply);
676 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700677 #if 0
678 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000679 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700680 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000681 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700682 }
683 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800684
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 IF_LOG_TRANSACTIONS() {
686 TextOutput::Bundle _b(alog);
687 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
688 << handle << ": ";
689 if (reply) alog << indent << *reply << dedent << endl;
690 else alog << "(none requested)" << endl;
691 }
692 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700693 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800695
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 return err;
697}
698
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700699void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700{
701 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
702 mOut.writeInt32(BC_ACQUIRE);
703 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700704 // Create a temp reference until the driver has handled this command.
705 proxy->incStrong(mProcess.get());
706 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707}
708
709void IPCThreadState::decStrongHandle(int32_t handle)
710{
711 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
712 mOut.writeInt32(BC_RELEASE);
713 mOut.writeInt32(handle);
714}
715
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700716void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717{
718 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
719 mOut.writeInt32(BC_INCREFS);
720 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700721 // Create a temp reference until the driver has handled this command.
722 proxy->getWeakRefs()->incWeak(mProcess.get());
723 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724}
725
726void IPCThreadState::decWeakHandle(int32_t handle)
727{
728 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
729 mOut.writeInt32(BC_DECREFS);
730 mOut.writeInt32(handle);
731}
732
733status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
734{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800735#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700736 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
738 mOut.writeInt32(0); // xxx was thread priority
739 mOut.writeInt32(handle);
740 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800745 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
747#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800748
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800750#else
751 (void)handle;
752 ALOGE("%s(%d): Not supported\n", __func__, handle);
753 return INVALID_OPERATION;
754#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755}
756
757void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
758{
759#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800760 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700762 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763}
764
765status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
766{
767 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
768 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000769 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 return NO_ERROR;
771}
772
773status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
774{
775 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
776 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000777 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 return NO_ERROR;
779}
780
781IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700782 : mProcess(ProcessState::self()),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100783 mWorkSource(kUnsetWorkSource),
Olivier Gaillard91a04802018-11-14 17:32:41 +0000784 mPropagateWorkSource(false),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700785 mStrictModePolicy(0),
786 mLastTransactionBinderFlags(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787{
788 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800789 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 mIn.setDataCapacity(256);
791 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000792 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793}
794
795IPCThreadState::~IPCThreadState()
796{
797}
798
Martijn Coenenea0090a2017-11-02 18:54:40 +0000799status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
800{
801 status_t err;
802 status_t statusBuffer;
803 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
804 if (err < NO_ERROR) return err;
805
Yi Kongfdd8da92018-06-07 17:52:27 -0700806 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000807}
808
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
810{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100811 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 int32_t err;
813
814 while (1) {
815 if ((err=talkWithDriver()) < NO_ERROR) break;
816 err = mIn.errorCheck();
817 if (err < NO_ERROR) break;
818 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800819
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100820 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800821
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 IF_LOG_COMMANDS() {
823 alog << "Processing waitForResponse Command: "
824 << getReturnString(cmd) << endl;
825 }
826
827 switch (cmd) {
828 case BR_TRANSACTION_COMPLETE:
829 if (!reply && !acquireResult) goto finish;
830 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800831
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 case BR_DEAD_REPLY:
833 err = DEAD_OBJECT;
834 goto finish;
835
836 case BR_FAILED_REPLY:
837 err = FAILED_TRANSACTION;
838 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800839
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840 case BR_ACQUIRE_RESULT:
841 {
Steve Block67263472012-01-09 18:35:44 +0000842 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 const int32_t result = mIn.readInt32();
844 if (!acquireResult) continue;
845 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
846 }
847 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800848
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 case BR_REPLY:
850 {
851 binder_transaction_data tr;
852 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000853 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854 if (err != NO_ERROR) goto finish;
855
856 if (reply) {
857 if ((tr.flags & TF_STATUS_CODE) == 0) {
858 reply->ipcSetDataReference(
859 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
860 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800861 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
862 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 freeBuffer, this);
864 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800865 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700866 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
868 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800869 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
870 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871 }
872 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700873 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
875 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800876 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
877 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 continue;
879 }
880 }
881 goto finish;
882
883 default:
884 err = executeCommand(cmd);
885 if (err != NO_ERROR) goto finish;
886 break;
887 }
888 }
889
890finish:
891 if (err != NO_ERROR) {
892 if (acquireResult) *acquireResult = err;
893 if (reply) reply->setError(err);
894 mLastError = err;
895 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800896
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 return err;
898}
899
900status_t IPCThreadState::talkWithDriver(bool doReceive)
901{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100902 if (mProcess->mDriverFD <= 0) {
903 return -EBADF;
904 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800905
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800907
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908 // Is the read buffer empty?
909 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800910
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911 // We don't want to write anything if we are still reading
912 // from data left in the input buffer and the caller
913 // has requested to read the next data.
914 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800915
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800917 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918
919 // This is what we'll read.
920 if (doReceive && needRead) {
921 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800922 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 } else {
924 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800925 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700927
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 IF_LOG_COMMANDS() {
929 TextOutput::Bundle _b(alog);
930 if (outAvail != 0) {
931 alog << "Sending commands to driver: " << indent;
932 const void* cmds = (const void*)bwr.write_buffer;
933 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
934 alog << HexDump(cmds, bwr.write_size) << endl;
935 while (cmds < end) cmds = printCommand(alog, cmds);
936 alog << dedent;
937 }
938 alog << "Size of receive buffer: " << bwr.read_size
939 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
940 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800941
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 // Return immediately if there is nothing to do.
943 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700944
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945 bwr.write_consumed = 0;
946 bwr.read_consumed = 0;
947 status_t err;
948 do {
949 IF_LOG_COMMANDS() {
950 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
951 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700952#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
954 err = NO_ERROR;
955 else
956 err = -errno;
957#else
958 err = INVALID_OPERATION;
959#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100960 if (mProcess->mDriverFD <= 0) {
961 err = -EBADF;
962 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963 IF_LOG_COMMANDS() {
964 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
965 }
966 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700967
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800969 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700971 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 }
973
974 if (err >= NO_ERROR) {
975 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800976 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700978 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700980 processPostWriteDerefs();
981 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982 }
983 if (bwr.read_consumed > 0) {
984 mIn.setDataSize(bwr.read_consumed);
985 mIn.setDataPosition(0);
986 }
987 IF_LOG_COMMANDS() {
988 TextOutput::Bundle _b(alog);
989 alog << "Remaining data size: " << mOut.dataSize() << endl;
990 alog << "Received commands from driver: " << indent;
991 const void* cmds = mIn.data();
992 const void* end = mIn.data() + mIn.dataSize();
993 alog << HexDump(cmds, mIn.dataSize()) << endl;
994 while (cmds < end) cmds = printReturnCommand(alog, cmds);
995 alog << dedent;
996 }
997 return NO_ERROR;
998 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800999
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 return err;
1001}
1002
1003status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
1004 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
1005{
1006 binder_transaction_data tr;
1007
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001008 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 tr.target.handle = handle;
1010 tr.code = code;
1011 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +04001012 tr.cookie = 0;
1013 tr.sender_pid = 0;
1014 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001015
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 const status_t err = data.errorCheck();
1017 if (err == NO_ERROR) {
1018 tr.data_size = data.ipcDataSize();
1019 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001020 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 tr.data.ptr.offsets = data.ipcObjects();
1022 } else if (statusBuffer) {
1023 tr.flags |= TF_STATUS_CODE;
1024 *statusBuffer = err;
1025 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -08001026 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001028 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 } else {
1030 return (mLastError = err);
1031 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 mOut.writeInt32(cmd);
1034 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001035
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 return NO_ERROR;
1037}
1038
1039sp<BBinder> the_context_object;
1040
1041void setTheContextObject(sp<BBinder> obj)
1042{
1043 the_context_object = obj;
1044}
1045
1046status_t IPCThreadState::executeCommand(int32_t cmd)
1047{
1048 BBinder* obj;
1049 RefBase::weakref_type* refs;
1050 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001051
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001052 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 case BR_ERROR:
1054 result = mIn.readInt32();
1055 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 case BR_OK:
1058 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001061 refs = (RefBase::weakref_type*)mIn.readPointer();
1062 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001063 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1065 refs, obj, refs->refBase());
1066 obj->incStrong(mProcess.get());
1067 IF_LOG_REMOTEREFS() {
1068 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1069 obj->printRefs();
1070 }
1071 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001072 mOut.writePointer((uintptr_t)refs);
1073 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001075
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001077 refs = (RefBase::weakref_type*)mIn.readPointer();
1078 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001079 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1081 refs, obj, refs->refBase());
1082 IF_LOG_REMOTEREFS() {
1083 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1084 obj->printRefs();
1085 }
1086 mPendingStrongDerefs.push(obj);
1087 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001090 refs = (RefBase::weakref_type*)mIn.readPointer();
1091 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 refs->incWeak(mProcess.get());
1093 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001094 mOut.writePointer((uintptr_t)refs);
1095 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 case BR_DECREFS:
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 // NOTE: This assertion is not valid, because the object may no
1102 // longer exist (thus the (BBinder*)cast above resulting in a different
1103 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001104 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1106 // refs, obj, refs->refBase());
1107 mPendingWeakDerefs.push(refs);
1108 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001111 refs = (RefBase::weakref_type*)mIn.readPointer();
1112 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 {
1115 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001116 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1118 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 mOut.writeInt32(BC_ACQUIRE_RESULT);
1121 mOut.writeInt32((int32_t)success);
1122 }
1123 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 case BR_TRANSACTION:
1126 {
1127 binder_transaction_data tr;
1128 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001129 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 "Not enough command data for brTRANSACTION");
1131 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001132
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001133 //Record the fact that we're in a binder call.
1134 mIPCThreadStateBase->pushCurrentState(
1135 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001136 Parcel buffer;
1137 buffer.ipcSetDataReference(
1138 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1139 tr.data_size,
1140 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1141 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1142
1143 const pid_t origPid = mCallingPid;
1144 const uid_t origUid = mCallingUid;
1145 const int32_t origStrictModePolicy = mStrictModePolicy;
1146 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001147 const int32_t origWorkSource = mWorkSource;
1148 const bool origPropagateWorkSet = mPropagateWorkSource;
1149 // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface
1150 // is only guaranteed to be called for AIDL-generated stubs so we reset the work source
1151 // here to never propagate it.
1152 clearCallingWorkSource();
1153 clearPropagateWorkSource();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001154
1155 mCallingPid = tr.sender_pid;
1156 mCallingUid = tr.sender_euid;
1157 mLastTransactionBinderFlags = tr.flags;
1158
1159 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
1160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001162 status_t error;
1163 IF_LOG_TRANSACTIONS() {
1164 TextOutput::Bundle _b(alog);
1165 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1166 << " / obj " << tr.target.ptr << " / code "
1167 << TypeCode(tr.code) << ": " << indent << buffer
1168 << dedent << endl
1169 << "Data addr = "
1170 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1171 << ", offsets addr="
1172 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1173 }
1174 if (tr.target.ptr) {
1175 // We only have a weak reference on the target object, so we must first try to
1176 // safely acquire a strong reference before doing anything else with it.
1177 if (reinterpret_cast<RefBase::weakref_type*>(
1178 tr.target.ptr)->attemptIncStrong(this)) {
1179 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1180 &reply, tr.flags);
1181 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001182 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001183 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001184 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001185
Martijn Coenenea0090a2017-11-02 18:54:40 +00001186 } else {
1187 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001189
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001190 mIPCThreadStateBase->popCurrentState();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001191 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
1192 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001193
Martijn Coenenea0090a2017-11-02 18:54:40 +00001194 if ((tr.flags & TF_ONE_WAY) == 0) {
1195 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1196 if (error < NO_ERROR) reply.setError(error);
1197 sendReply(reply, 0);
1198 } else {
1199 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1200 }
1201
1202 mCallingPid = origPid;
1203 mCallingUid = origUid;
1204 mStrictModePolicy = origStrictModePolicy;
1205 mLastTransactionBinderFlags = origTransactionBinderFlags;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001206 mWorkSource = origWorkSource;
1207 mPropagateWorkSource = origPropagateWorkSet;
Christopher Tate440fd872010-03-18 17:55:03 -07001208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 IF_LOG_TRANSACTIONS() {
1210 TextOutput::Bundle _b(alog);
1211 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1212 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1213 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 }
1216 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 case BR_DEAD_BINDER:
1219 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001220 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 proxy->sendObituary();
1222 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001223 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1227 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001228 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 proxy->getWeakRefs()->decWeak(proxy);
1230 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 case BR_FINISHED:
1233 result = TIMED_OUT;
1234 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 case BR_NOOP:
1237 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 case BR_SPAWN_LOOPER:
1240 mProcess->spawnPooledThread(false);
1241 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 default:
liangweikanga43ee152016-10-25 16:37:54 +08001244 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 result = UNKNOWN_ERROR;
1246 break;
1247 }
1248
1249 if (result != NO_ERROR) {
1250 mLastError = result;
1251 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 return result;
1254}
1255
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001256bool IPCThreadState::isServingCall() const {
1257 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1258}
1259
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260void IPCThreadState::threadDestructor(void *st)
1261{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001262 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1263 if (self) {
1264 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001265#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001266 if (self->mProcess->mDriverFD > 0) {
1267 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1268 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001270 delete self;
1271 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272}
1273
1274
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001275void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1276 size_t /*dataSize*/,
1277 const binder_size_t* /*objects*/,
1278 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279{
Steve Blocka19954a2012-01-04 20:05:49 +00001280 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 IF_LOG_COMMANDS() {
1282 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1283 }
Steve Block67263472012-01-09 18:35:44 +00001284 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001285 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 IPCThreadState* state = self();
1287 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001288 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289}
1290
1291}; // namespace android