blob: f0c21f5ff60c4c9f75d111cdd7d8cb5fba933bc8 [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 Gaillard0e0f1de2018-08-16 14:04:09 +0100112// The work source represents the UID of the process we should attribute the transaction to.
113// We use -1 to specify that the work source was not set using #setWorkSource.
114static const int kUnsetWorkSource = -1;
115
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 Gaillarda8e7bf22018-11-14 15:35:50 +0000392 // Note: we currently only use half of the int64. We return an int64 for extensibility.
393 int64_t token = mWorkSource;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100394 mWorkSource = uid;
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000395 return token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100396}
397
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000398uid_t IPCThreadState::getCallingWorkSourceUid() const
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100399{
400 return mWorkSource;
401}
402
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000403int64_t IPCThreadState::clearCallingWorkSource()
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100404{
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000405 return setCallingWorkSourceUid(kUnsetWorkSource);
406}
407
408void IPCThreadState::restoreCallingWorkSource(int64_t token)
409{
410 uid_t uid = (int)token;
411 setCallingWorkSourceUid(uid);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100412}
413
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700414void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
415{
416 mLastTransactionBinderFlags = flags;
417}
418
419int32_t IPCThreadState::getLastTransactionBinderFlags() const
420{
421 return mLastTransactionBinderFlags;
422}
423
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424void IPCThreadState::restoreCallingIdentity(int64_t token)
425{
426 mCallingUid = (int)(token>>32);
427 mCallingPid = (int)token;
428}
429
430void IPCThreadState::clearCaller()
431{
Marco Nelissend43b1942009-07-17 07:59:17 -0700432 mCallingPid = getpid();
433 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434}
435
436void IPCThreadState::flushCommands()
437{
438 if (mProcess->mDriverFD <= 0)
439 return;
440 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700441 // The flush could have caused post-write refcount decrements to have
442 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
443 // being queued in mOut. So flush again, if we need to.
444 if (mOut.dataSize() > 0) {
445 talkWithDriver(false);
446 }
447 if (mOut.dataSize() > 0) {
448 ALOGW("mOut.dataSize() > 0 after flushCommands()");
449 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450}
451
Wale Ogunwale376b8222015-04-13 16:16:10 -0700452void IPCThreadState::blockUntilThreadAvailable()
453{
454 pthread_mutex_lock(&mProcess->mThreadCountLock);
455 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700456 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
457 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
458 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700459 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
460 }
461 pthread_mutex_unlock(&mProcess->mThreadCountLock);
462}
463
Todd Poynor8d96cab2013-06-25 19:12:18 -0700464status_t IPCThreadState::getAndExecuteCommand()
465{
466 status_t result;
467 int32_t cmd;
468
469 result = talkWithDriver();
470 if (result >= NO_ERROR) {
471 size_t IN = mIn.dataAvail();
472 if (IN < sizeof(int32_t)) return result;
473 cmd = mIn.readInt32();
474 IF_LOG_COMMANDS() {
475 alog << "Processing top-level Command: "
476 << getReturnString(cmd) << endl;
477 }
478
Wale Ogunwale376b8222015-04-13 16:16:10 -0700479 pthread_mutex_lock(&mProcess->mThreadCountLock);
480 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700481 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
482 mProcess->mStarvationStartTimeMs == 0) {
483 mProcess->mStarvationStartTimeMs = uptimeMillis();
484 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700485 pthread_mutex_unlock(&mProcess->mThreadCountLock);
486
Todd Poynor8d96cab2013-06-25 19:12:18 -0700487 result = executeCommand(cmd);
488
Wale Ogunwale376b8222015-04-13 16:16:10 -0700489 pthread_mutex_lock(&mProcess->mThreadCountLock);
490 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700491 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
492 mProcess->mStarvationStartTimeMs != 0) {
493 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
494 if (starvationTimeMs > 100) {
495 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
496 mProcess->mMaxThreads, starvationTimeMs);
497 }
498 mProcess->mStarvationStartTimeMs = 0;
499 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700500 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
501 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700502 }
503
504 return result;
505}
506
507// When we've cleared the incoming command queue, process any pending derefs
508void IPCThreadState::processPendingDerefs()
509{
510 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200511 /*
512 * The decWeak()/decStrong() calls may cause a destructor to run,
513 * which in turn could have initiated an outgoing transaction,
514 * which in turn could cause us to add to the pending refs
515 * vectors; so instead of simply iterating, loop until they're empty.
516 *
517 * We do this in an outer loop, because calling decStrong()
518 * may result in something being added to mPendingWeakDerefs,
519 * which could be delayed until the next incoming command
520 * from the driver if we don't process it now.
521 */
522 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
523 while (mPendingWeakDerefs.size() > 0) {
524 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
525 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700526 refs->decWeak(mProcess.get());
527 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700528
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200529 if (mPendingStrongDerefs.size() > 0) {
530 // We don't use while() here because we don't want to re-order
531 // strong and weak decs at all; if this decStrong() causes both a
532 // decWeak() and a decStrong() to be queued, we want to process
533 // the decWeak() first.
534 BBinder* obj = mPendingStrongDerefs[0];
535 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700536 obj->decStrong(mProcess.get());
537 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700538 }
539 }
540}
541
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700542void IPCThreadState::processPostWriteDerefs()
543{
544 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
545 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
546 refs->decWeak(mProcess.get());
547 }
548 mPostWriteWeakDerefs.clear();
549
550 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
551 RefBase* obj = mPostWriteStrongDerefs[i];
552 obj->decStrong(mProcess.get());
553 }
554 mPostWriteStrongDerefs.clear();
555}
556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557void IPCThreadState::joinThreadPool(bool isMain)
558{
559 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
560
561 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800562
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 status_t result;
564 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700565 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700567 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800568
Todd Poynor8d96cab2013-06-25 19:12:18 -0700569 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
570 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700571 mProcess->mDriverFD, result);
572 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800574
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 // Let this thread exit the thread pool if it is no longer
576 // needed and it is not the main process thread.
577 if(result == TIMED_OUT && !isMain) {
578 break;
579 }
580 } while (result != -ECONNREFUSED && result != -EBADF);
581
Wei Wangc7341432016-10-19 10:23:59 -0700582 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
583 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800584
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 mOut.writeInt32(BC_EXIT_LOOPER);
586 talkWithDriver(false);
587}
588
Todd Poynor8d96cab2013-06-25 19:12:18 -0700589int IPCThreadState::setupPolling(int* fd)
590{
591 if (mProcess->mDriverFD <= 0) {
592 return -EBADF;
593 }
594
595 mOut.writeInt32(BC_ENTER_LOOPER);
596 *fd = mProcess->mDriverFD;
597 return 0;
598}
599
600status_t IPCThreadState::handlePolledCommands()
601{
602 status_t result;
603
604 do {
605 result = getAndExecuteCommand();
606 } while (mIn.dataPosition() < mIn.dataSize());
607
608 processPendingDerefs();
609 flushCommands();
610 return result;
611}
612
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800613void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614{
Steve Blocka19954a2012-01-04 20:05:49 +0000615 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 flushCommands();
617 int fd = mProcess->mDriverFD;
618 mProcess->mDriverFD = -1;
619 close(fd);
620 //kill(getpid(), SIGKILL);
621}
622
623status_t IPCThreadState::transact(int32_t handle,
624 uint32_t code, const Parcel& data,
625 Parcel* reply, uint32_t flags)
626{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800627 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628
629 flags |= TF_ACCEPT_FDS;
630
631 IF_LOG_TRANSACTIONS() {
632 TextOutput::Bundle _b(alog);
633 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
634 << handle << " / code " << TypeCode(code) << ": "
635 << indent << data << dedent << endl;
636 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800637
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800638 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
639 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700640 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642 if (err != NO_ERROR) {
643 if (reply) reply->setError(err);
644 return (mLastError = err);
645 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800646
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700648 #if 0
649 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000650 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700651 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000652 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700653 }
654 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 if (reply) {
656 err = waitForResponse(reply);
657 } else {
658 Parcel fakeReply;
659 err = waitForResponse(&fakeReply);
660 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700661 #if 0
662 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000663 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700664 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000665 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700666 }
667 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800668
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 IF_LOG_TRANSACTIONS() {
670 TextOutput::Bundle _b(alog);
671 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
672 << handle << ": ";
673 if (reply) alog << indent << *reply << dedent << endl;
674 else alog << "(none requested)" << endl;
675 }
676 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700677 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800679
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 return err;
681}
682
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700683void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684{
685 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
686 mOut.writeInt32(BC_ACQUIRE);
687 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700688 // Create a temp reference until the driver has handled this command.
689 proxy->incStrong(mProcess.get());
690 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691}
692
693void IPCThreadState::decStrongHandle(int32_t handle)
694{
695 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
696 mOut.writeInt32(BC_RELEASE);
697 mOut.writeInt32(handle);
698}
699
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700700void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701{
702 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
703 mOut.writeInt32(BC_INCREFS);
704 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700705 // Create a temp reference until the driver has handled this command.
706 proxy->getWeakRefs()->incWeak(mProcess.get());
707 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708}
709
710void IPCThreadState::decWeakHandle(int32_t handle)
711{
712 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
713 mOut.writeInt32(BC_DECREFS);
714 mOut.writeInt32(handle);
715}
716
717status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
718{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800719#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700720 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
722 mOut.writeInt32(0); // xxx was thread priority
723 mOut.writeInt32(handle);
724 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800725
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800729 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
731#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800732
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800734#else
735 (void)handle;
736 ALOGE("%s(%d): Not supported\n", __func__, handle);
737 return INVALID_OPERATION;
738#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739}
740
741void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
742{
743#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800744 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700746 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747}
748
749status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
750{
751 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
752 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000753 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 return NO_ERROR;
755}
756
757status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
758{
759 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
760 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000761 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 return NO_ERROR;
763}
764
765IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700766 : mProcess(ProcessState::self()),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100767 mWorkSource(kUnsetWorkSource),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700768 mStrictModePolicy(0),
769 mLastTransactionBinderFlags(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770{
771 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800772 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 mIn.setDataCapacity(256);
774 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000775 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776}
777
778IPCThreadState::~IPCThreadState()
779{
780}
781
Martijn Coenenea0090a2017-11-02 18:54:40 +0000782status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
783{
784 status_t err;
785 status_t statusBuffer;
786 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
787 if (err < NO_ERROR) return err;
788
Yi Kongfdd8da92018-06-07 17:52:27 -0700789 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000790}
791
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
793{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100794 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795 int32_t err;
796
797 while (1) {
798 if ((err=talkWithDriver()) < NO_ERROR) break;
799 err = mIn.errorCheck();
800 if (err < NO_ERROR) break;
801 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800802
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100803 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800804
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 IF_LOG_COMMANDS() {
806 alog << "Processing waitForResponse Command: "
807 << getReturnString(cmd) << endl;
808 }
809
810 switch (cmd) {
811 case BR_TRANSACTION_COMPLETE:
812 if (!reply && !acquireResult) goto finish;
813 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800814
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 case BR_DEAD_REPLY:
816 err = DEAD_OBJECT;
817 goto finish;
818
819 case BR_FAILED_REPLY:
820 err = FAILED_TRANSACTION;
821 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800822
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 case BR_ACQUIRE_RESULT:
824 {
Steve Block67263472012-01-09 18:35:44 +0000825 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826 const int32_t result = mIn.readInt32();
827 if (!acquireResult) continue;
828 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
829 }
830 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800831
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 case BR_REPLY:
833 {
834 binder_transaction_data tr;
835 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000836 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 if (err != NO_ERROR) goto finish;
838
839 if (reply) {
840 if ((tr.flags & TF_STATUS_CODE) == 0) {
841 reply->ipcSetDataReference(
842 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
843 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800844 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
845 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 freeBuffer, this);
847 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800848 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700849 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
851 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800852 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
853 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854 }
855 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700856 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
858 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800859 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
860 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 continue;
862 }
863 }
864 goto finish;
865
866 default:
867 err = executeCommand(cmd);
868 if (err != NO_ERROR) goto finish;
869 break;
870 }
871 }
872
873finish:
874 if (err != NO_ERROR) {
875 if (acquireResult) *acquireResult = err;
876 if (reply) reply->setError(err);
877 mLastError = err;
878 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800879
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 return err;
881}
882
883status_t IPCThreadState::talkWithDriver(bool doReceive)
884{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100885 if (mProcess->mDriverFD <= 0) {
886 return -EBADF;
887 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800888
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800890
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 // Is the read buffer empty?
892 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800893
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 // We don't want to write anything if we are still reading
895 // from data left in the input buffer and the caller
896 // has requested to read the next data.
897 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800898
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800900 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901
902 // This is what we'll read.
903 if (doReceive && needRead) {
904 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800905 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 } else {
907 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800908 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700910
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911 IF_LOG_COMMANDS() {
912 TextOutput::Bundle _b(alog);
913 if (outAvail != 0) {
914 alog << "Sending commands to driver: " << indent;
915 const void* cmds = (const void*)bwr.write_buffer;
916 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
917 alog << HexDump(cmds, bwr.write_size) << endl;
918 while (cmds < end) cmds = printCommand(alog, cmds);
919 alog << dedent;
920 }
921 alog << "Size of receive buffer: " << bwr.read_size
922 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
923 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800924
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 // Return immediately if there is nothing to do.
926 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700927
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 bwr.write_consumed = 0;
929 bwr.read_consumed = 0;
930 status_t err;
931 do {
932 IF_LOG_COMMANDS() {
933 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
934 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700935#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
937 err = NO_ERROR;
938 else
939 err = -errno;
940#else
941 err = INVALID_OPERATION;
942#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100943 if (mProcess->mDriverFD <= 0) {
944 err = -EBADF;
945 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 IF_LOG_COMMANDS() {
947 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
948 }
949 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700950
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800952 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700954 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 }
956
957 if (err >= NO_ERROR) {
958 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800959 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700961 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700963 processPostWriteDerefs();
964 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965 }
966 if (bwr.read_consumed > 0) {
967 mIn.setDataSize(bwr.read_consumed);
968 mIn.setDataPosition(0);
969 }
970 IF_LOG_COMMANDS() {
971 TextOutput::Bundle _b(alog);
972 alog << "Remaining data size: " << mOut.dataSize() << endl;
973 alog << "Received commands from driver: " << indent;
974 const void* cmds = mIn.data();
975 const void* end = mIn.data() + mIn.dataSize();
976 alog << HexDump(cmds, mIn.dataSize()) << endl;
977 while (cmds < end) cmds = printReturnCommand(alog, cmds);
978 alog << dedent;
979 }
980 return NO_ERROR;
981 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800982
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 return err;
984}
985
986status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
987 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
988{
989 binder_transaction_data tr;
990
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800991 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 tr.target.handle = handle;
993 tr.code = code;
994 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +0400995 tr.cookie = 0;
996 tr.sender_pid = 0;
997 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800998
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999 const status_t err = data.errorCheck();
1000 if (err == NO_ERROR) {
1001 tr.data_size = data.ipcDataSize();
1002 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001003 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 tr.data.ptr.offsets = data.ipcObjects();
1005 } else if (statusBuffer) {
1006 tr.flags |= TF_STATUS_CODE;
1007 *statusBuffer = err;
1008 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -08001009 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001011 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 } else {
1013 return (mLastError = err);
1014 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001015
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 mOut.writeInt32(cmd);
1017 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001018
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 return NO_ERROR;
1020}
1021
1022sp<BBinder> the_context_object;
1023
1024void setTheContextObject(sp<BBinder> obj)
1025{
1026 the_context_object = obj;
1027}
1028
1029status_t IPCThreadState::executeCommand(int32_t cmd)
1030{
1031 BBinder* obj;
1032 RefBase::weakref_type* refs;
1033 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001034
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001035 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 case BR_ERROR:
1037 result = mIn.readInt32();
1038 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 case BR_OK:
1041 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001042
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001044 refs = (RefBase::weakref_type*)mIn.readPointer();
1045 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001046 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1048 refs, obj, refs->refBase());
1049 obj->incStrong(mProcess.get());
1050 IF_LOG_REMOTEREFS() {
1051 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1052 obj->printRefs();
1053 }
1054 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001055 mOut.writePointer((uintptr_t)refs);
1056 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001060 refs = (RefBase::weakref_type*)mIn.readPointer();
1061 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001062 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1064 refs, obj, refs->refBase());
1065 IF_LOG_REMOTEREFS() {
1066 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1067 obj->printRefs();
1068 }
1069 mPendingStrongDerefs.push(obj);
1070 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001073 refs = (RefBase::weakref_type*)mIn.readPointer();
1074 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 refs->incWeak(mProcess.get());
1076 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001077 mOut.writePointer((uintptr_t)refs);
1078 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001082 refs = (RefBase::weakref_type*)mIn.readPointer();
1083 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 // NOTE: This assertion is not valid, because the object may no
1085 // longer exist (thus the (BBinder*)cast above resulting in a different
1086 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001087 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1089 // refs, obj, refs->refBase());
1090 mPendingWeakDerefs.push(refs);
1091 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001094 refs = (RefBase::weakref_type*)mIn.readPointer();
1095 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 {
1098 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001099 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1101 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 mOut.writeInt32(BC_ACQUIRE_RESULT);
1104 mOut.writeInt32((int32_t)success);
1105 }
1106 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 case BR_TRANSACTION:
1109 {
1110 binder_transaction_data tr;
1111 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001112 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 "Not enough command data for brTRANSACTION");
1114 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001115
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001116 //Record the fact that we're in a binder call.
1117 mIPCThreadStateBase->pushCurrentState(
1118 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001119 Parcel buffer;
1120 buffer.ipcSetDataReference(
1121 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1122 tr.data_size,
1123 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1124 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1125
1126 const pid_t origPid = mCallingPid;
1127 const uid_t origUid = mCallingUid;
1128 const int32_t origStrictModePolicy = mStrictModePolicy;
1129 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1130
1131 mCallingPid = tr.sender_pid;
1132 mCallingUid = tr.sender_euid;
1133 mLastTransactionBinderFlags = tr.flags;
1134
1135 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
1136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001138 status_t error;
1139 IF_LOG_TRANSACTIONS() {
1140 TextOutput::Bundle _b(alog);
1141 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1142 << " / obj " << tr.target.ptr << " / code "
1143 << TypeCode(tr.code) << ": " << indent << buffer
1144 << dedent << endl
1145 << "Data addr = "
1146 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1147 << ", offsets addr="
1148 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1149 }
1150 if (tr.target.ptr) {
1151 // We only have a weak reference on the target object, so we must first try to
1152 // safely acquire a strong reference before doing anything else with it.
1153 if (reinterpret_cast<RefBase::weakref_type*>(
1154 tr.target.ptr)->attemptIncStrong(this)) {
1155 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1156 &reply, tr.flags);
1157 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001158 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001159 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001160 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001161
Martijn Coenenea0090a2017-11-02 18:54:40 +00001162 } else {
1163 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001165
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001166 mIPCThreadStateBase->popCurrentState();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001167 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
1168 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001169
Martijn Coenenea0090a2017-11-02 18:54:40 +00001170 if ((tr.flags & TF_ONE_WAY) == 0) {
1171 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1172 if (error < NO_ERROR) reply.setError(error);
1173 sendReply(reply, 0);
1174 } else {
1175 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1176 }
1177
1178 mCallingPid = origPid;
1179 mCallingUid = origUid;
1180 mStrictModePolicy = origStrictModePolicy;
1181 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate440fd872010-03-18 17:55:03 -07001182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 IF_LOG_TRANSACTIONS() {
1184 TextOutput::Bundle _b(alog);
1185 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1186 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1187 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001188
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 }
1190 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 case BR_DEAD_BINDER:
1193 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001194 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 proxy->sendObituary();
1196 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001197 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1201 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001202 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 proxy->getWeakRefs()->decWeak(proxy);
1204 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 case BR_FINISHED:
1207 result = TIMED_OUT;
1208 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 case BR_NOOP:
1211 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 case BR_SPAWN_LOOPER:
1214 mProcess->spawnPooledThread(false);
1215 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 default:
liangweikanga43ee152016-10-25 16:37:54 +08001218 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 result = UNKNOWN_ERROR;
1220 break;
1221 }
1222
1223 if (result != NO_ERROR) {
1224 mLastError = result;
1225 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 return result;
1228}
1229
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001230bool IPCThreadState::isServingCall() const {
1231 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1232}
1233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234void IPCThreadState::threadDestructor(void *st)
1235{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001236 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1237 if (self) {
1238 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001239#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001240 if (self->mProcess->mDriverFD > 0) {
1241 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1242 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001244 delete self;
1245 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246}
1247
1248
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001249void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1250 size_t /*dataSize*/,
1251 const binder_size_t* /*objects*/,
1252 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253{
Steve Blocka19954a2012-01-04 20:05:49 +00001254 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 IF_LOG_COMMANDS() {
1256 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1257 }
Steve Block67263472012-01-09 18:35:44 +00001258 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001259 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 IPCThreadState* state = self();
1261 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001262 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263}
1264
1265}; // namespace android