blob: 7a477249dd58d3daa5e63b5074bdd6b6df72d0aa [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 Gaillard0e0f1de2018-08-16 14:04:09 +0100390uid_t IPCThreadState::setWorkSource(uid_t uid)
391{
392 uid_t returnValue = mWorkSource;
393 mWorkSource = uid;
394 return returnValue;
395}
396
397uid_t IPCThreadState::getWorkSource() const
398{
399 return mWorkSource;
400}
401
402uid_t IPCThreadState::clearWorkSource()
403{
404 uid_t returnValue = mWorkSource;
405 mWorkSource = kUnsetWorkSource;
406 return returnValue;
407}
408
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700409void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
410{
411 mLastTransactionBinderFlags = flags;
412}
413
414int32_t IPCThreadState::getLastTransactionBinderFlags() const
415{
416 return mLastTransactionBinderFlags;
417}
418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419void IPCThreadState::restoreCallingIdentity(int64_t token)
420{
421 mCallingUid = (int)(token>>32);
422 mCallingPid = (int)token;
423}
424
425void IPCThreadState::clearCaller()
426{
Marco Nelissend43b1942009-07-17 07:59:17 -0700427 mCallingPid = getpid();
428 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429}
430
431void IPCThreadState::flushCommands()
432{
433 if (mProcess->mDriverFD <= 0)
434 return;
435 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700436 // The flush could have caused post-write refcount decrements to have
437 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
438 // being queued in mOut. So flush again, if we need to.
439 if (mOut.dataSize() > 0) {
440 talkWithDriver(false);
441 }
442 if (mOut.dataSize() > 0) {
443 ALOGW("mOut.dataSize() > 0 after flushCommands()");
444 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445}
446
Wale Ogunwale376b8222015-04-13 16:16:10 -0700447void IPCThreadState::blockUntilThreadAvailable()
448{
449 pthread_mutex_lock(&mProcess->mThreadCountLock);
450 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700451 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
452 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
453 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700454 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
455 }
456 pthread_mutex_unlock(&mProcess->mThreadCountLock);
457}
458
Todd Poynor8d96cab2013-06-25 19:12:18 -0700459status_t IPCThreadState::getAndExecuteCommand()
460{
461 status_t result;
462 int32_t cmd;
463
464 result = talkWithDriver();
465 if (result >= NO_ERROR) {
466 size_t IN = mIn.dataAvail();
467 if (IN < sizeof(int32_t)) return result;
468 cmd = mIn.readInt32();
469 IF_LOG_COMMANDS() {
470 alog << "Processing top-level Command: "
471 << getReturnString(cmd) << endl;
472 }
473
Wale Ogunwale376b8222015-04-13 16:16:10 -0700474 pthread_mutex_lock(&mProcess->mThreadCountLock);
475 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700476 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
477 mProcess->mStarvationStartTimeMs == 0) {
478 mProcess->mStarvationStartTimeMs = uptimeMillis();
479 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700480 pthread_mutex_unlock(&mProcess->mThreadCountLock);
481
Todd Poynor8d96cab2013-06-25 19:12:18 -0700482 result = executeCommand(cmd);
483
Wale Ogunwale376b8222015-04-13 16:16:10 -0700484 pthread_mutex_lock(&mProcess->mThreadCountLock);
485 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700486 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
487 mProcess->mStarvationStartTimeMs != 0) {
488 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
489 if (starvationTimeMs > 100) {
490 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
491 mProcess->mMaxThreads, starvationTimeMs);
492 }
493 mProcess->mStarvationStartTimeMs = 0;
494 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700495 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
496 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700497 }
498
499 return result;
500}
501
502// When we've cleared the incoming command queue, process any pending derefs
503void IPCThreadState::processPendingDerefs()
504{
505 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200506 /*
507 * The decWeak()/decStrong() calls may cause a destructor to run,
508 * which in turn could have initiated an outgoing transaction,
509 * which in turn could cause us to add to the pending refs
510 * vectors; so instead of simply iterating, loop until they're empty.
511 *
512 * We do this in an outer loop, because calling decStrong()
513 * may result in something being added to mPendingWeakDerefs,
514 * which could be delayed until the next incoming command
515 * from the driver if we don't process it now.
516 */
517 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
518 while (mPendingWeakDerefs.size() > 0) {
519 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
520 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700521 refs->decWeak(mProcess.get());
522 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700523
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200524 if (mPendingStrongDerefs.size() > 0) {
525 // We don't use while() here because we don't want to re-order
526 // strong and weak decs at all; if this decStrong() causes both a
527 // decWeak() and a decStrong() to be queued, we want to process
528 // the decWeak() first.
529 BBinder* obj = mPendingStrongDerefs[0];
530 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700531 obj->decStrong(mProcess.get());
532 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700533 }
534 }
535}
536
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700537void IPCThreadState::processPostWriteDerefs()
538{
539 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
540 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
541 refs->decWeak(mProcess.get());
542 }
543 mPostWriteWeakDerefs.clear();
544
545 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
546 RefBase* obj = mPostWriteStrongDerefs[i];
547 obj->decStrong(mProcess.get());
548 }
549 mPostWriteStrongDerefs.clear();
550}
551
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552void IPCThreadState::joinThreadPool(bool isMain)
553{
554 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
555
556 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 status_t result;
559 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700560 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700562 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800563
Todd Poynor8d96cab2013-06-25 19:12:18 -0700564 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
565 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700566 mProcess->mDriverFD, result);
567 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800569
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 // Let this thread exit the thread pool if it is no longer
571 // needed and it is not the main process thread.
572 if(result == TIMED_OUT && !isMain) {
573 break;
574 }
575 } while (result != -ECONNREFUSED && result != -EBADF);
576
Wei Wangc7341432016-10-19 10:23:59 -0700577 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
578 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 mOut.writeInt32(BC_EXIT_LOOPER);
581 talkWithDriver(false);
582}
583
Todd Poynor8d96cab2013-06-25 19:12:18 -0700584int IPCThreadState::setupPolling(int* fd)
585{
586 if (mProcess->mDriverFD <= 0) {
587 return -EBADF;
588 }
589
590 mOut.writeInt32(BC_ENTER_LOOPER);
591 *fd = mProcess->mDriverFD;
592 return 0;
593}
594
595status_t IPCThreadState::handlePolledCommands()
596{
597 status_t result;
598
599 do {
600 result = getAndExecuteCommand();
601 } while (mIn.dataPosition() < mIn.dataSize());
602
603 processPendingDerefs();
604 flushCommands();
605 return result;
606}
607
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800608void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609{
Steve Blocka19954a2012-01-04 20:05:49 +0000610 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 flushCommands();
612 int fd = mProcess->mDriverFD;
613 mProcess->mDriverFD = -1;
614 close(fd);
615 //kill(getpid(), SIGKILL);
616}
617
618status_t IPCThreadState::transact(int32_t handle,
619 uint32_t code, const Parcel& data,
620 Parcel* reply, uint32_t flags)
621{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800622 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623
624 flags |= TF_ACCEPT_FDS;
625
626 IF_LOG_TRANSACTIONS() {
627 TextOutput::Bundle _b(alog);
628 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
629 << handle << " / code " << TypeCode(code) << ": "
630 << indent << data << dedent << endl;
631 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800632
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800633 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
634 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700635 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 if (err != NO_ERROR) {
638 if (reply) reply->setError(err);
639 return (mLastError = err);
640 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700643 #if 0
644 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000645 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700646 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000647 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700648 }
649 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650 if (reply) {
651 err = waitForResponse(reply);
652 } else {
653 Parcel fakeReply;
654 err = waitForResponse(&fakeReply);
655 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700656 #if 0
657 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000658 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700659 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000660 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700661 }
662 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 IF_LOG_TRANSACTIONS() {
665 TextOutput::Bundle _b(alog);
666 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
667 << handle << ": ";
668 if (reply) alog << indent << *reply << dedent << endl;
669 else alog << "(none requested)" << endl;
670 }
671 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700672 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800674
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 return err;
676}
677
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700678void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679{
680 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
681 mOut.writeInt32(BC_ACQUIRE);
682 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700683 // Create a temp reference until the driver has handled this command.
684 proxy->incStrong(mProcess.get());
685 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686}
687
688void IPCThreadState::decStrongHandle(int32_t handle)
689{
690 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
691 mOut.writeInt32(BC_RELEASE);
692 mOut.writeInt32(handle);
693}
694
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700695void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696{
697 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
698 mOut.writeInt32(BC_INCREFS);
699 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700700 // Create a temp reference until the driver has handled this command.
701 proxy->getWeakRefs()->incWeak(mProcess.get());
702 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703}
704
705void IPCThreadState::decWeakHandle(int32_t handle)
706{
707 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
708 mOut.writeInt32(BC_DECREFS);
709 mOut.writeInt32(handle);
710}
711
712status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
713{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800714#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700715 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
717 mOut.writeInt32(0); // xxx was thread priority
718 mOut.writeInt32(handle);
719 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800722
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800724 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
726#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800729#else
730 (void)handle;
731 ALOGE("%s(%d): Not supported\n", __func__, handle);
732 return INVALID_OPERATION;
733#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734}
735
736void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
737{
738#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800739 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700741 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742}
743
744status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
745{
746 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
747 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000748 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 return NO_ERROR;
750}
751
752status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
753{
754 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
755 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000756 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 return NO_ERROR;
758}
759
760IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700761 : mProcess(ProcessState::self()),
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100762 mWorkSource(kUnsetWorkSource),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700763 mStrictModePolicy(0),
764 mLastTransactionBinderFlags(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765{
766 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800767 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768 mIn.setDataCapacity(256);
769 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000770 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771}
772
773IPCThreadState::~IPCThreadState()
774{
775}
776
Martijn Coenenea0090a2017-11-02 18:54:40 +0000777status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
778{
779 status_t err;
780 status_t statusBuffer;
781 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
782 if (err < NO_ERROR) return err;
783
Yi Kongfdd8da92018-06-07 17:52:27 -0700784 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000785}
786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
788{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100789 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 int32_t err;
791
792 while (1) {
793 if ((err=talkWithDriver()) < NO_ERROR) break;
794 err = mIn.errorCheck();
795 if (err < NO_ERROR) break;
796 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800797
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100798 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800799
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800 IF_LOG_COMMANDS() {
801 alog << "Processing waitForResponse Command: "
802 << getReturnString(cmd) << endl;
803 }
804
805 switch (cmd) {
806 case BR_TRANSACTION_COMPLETE:
807 if (!reply && !acquireResult) goto finish;
808 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800809
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810 case BR_DEAD_REPLY:
811 err = DEAD_OBJECT;
812 goto finish;
813
814 case BR_FAILED_REPLY:
815 err = FAILED_TRANSACTION;
816 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800817
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 case BR_ACQUIRE_RESULT:
819 {
Steve Block67263472012-01-09 18:35:44 +0000820 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 const int32_t result = mIn.readInt32();
822 if (!acquireResult) continue;
823 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
824 }
825 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800826
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827 case BR_REPLY:
828 {
829 binder_transaction_data tr;
830 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000831 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 if (err != NO_ERROR) goto finish;
833
834 if (reply) {
835 if ((tr.flags & TF_STATUS_CODE) == 0) {
836 reply->ipcSetDataReference(
837 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
838 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800839 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
840 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841 freeBuffer, this);
842 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800843 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700844 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
846 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800847 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
848 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 }
850 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700851 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
853 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800854 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
855 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 continue;
857 }
858 }
859 goto finish;
860
861 default:
862 err = executeCommand(cmd);
863 if (err != NO_ERROR) goto finish;
864 break;
865 }
866 }
867
868finish:
869 if (err != NO_ERROR) {
870 if (acquireResult) *acquireResult = err;
871 if (reply) reply->setError(err);
872 mLastError = err;
873 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800874
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 return err;
876}
877
878status_t IPCThreadState::talkWithDriver(bool doReceive)
879{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100880 if (mProcess->mDriverFD <= 0) {
881 return -EBADF;
882 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800883
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800885
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886 // Is the read buffer empty?
887 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800888
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 // We don't want to write anything if we are still reading
890 // from data left in the input buffer and the caller
891 // has requested to read the next data.
892 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800893
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800895 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896
897 // This is what we'll read.
898 if (doReceive && needRead) {
899 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800900 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901 } else {
902 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800903 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700905
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 IF_LOG_COMMANDS() {
907 TextOutput::Bundle _b(alog);
908 if (outAvail != 0) {
909 alog << "Sending commands to driver: " << indent;
910 const void* cmds = (const void*)bwr.write_buffer;
911 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
912 alog << HexDump(cmds, bwr.write_size) << endl;
913 while (cmds < end) cmds = printCommand(alog, cmds);
914 alog << dedent;
915 }
916 alog << "Size of receive buffer: " << bwr.read_size
917 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
918 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800919
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 // Return immediately if there is nothing to do.
921 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700922
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 bwr.write_consumed = 0;
924 bwr.read_consumed = 0;
925 status_t err;
926 do {
927 IF_LOG_COMMANDS() {
928 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
929 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700930#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
932 err = NO_ERROR;
933 else
934 err = -errno;
935#else
936 err = INVALID_OPERATION;
937#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100938 if (mProcess->mDriverFD <= 0) {
939 err = -EBADF;
940 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 IF_LOG_COMMANDS() {
942 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
943 }
944 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700945
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800947 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700949 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 }
951
952 if (err >= NO_ERROR) {
953 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800954 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700956 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700958 processPostWriteDerefs();
959 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 }
961 if (bwr.read_consumed > 0) {
962 mIn.setDataSize(bwr.read_consumed);
963 mIn.setDataPosition(0);
964 }
965 IF_LOG_COMMANDS() {
966 TextOutput::Bundle _b(alog);
967 alog << "Remaining data size: " << mOut.dataSize() << endl;
968 alog << "Received commands from driver: " << indent;
969 const void* cmds = mIn.data();
970 const void* end = mIn.data() + mIn.dataSize();
971 alog << HexDump(cmds, mIn.dataSize()) << endl;
972 while (cmds < end) cmds = printReturnCommand(alog, cmds);
973 alog << dedent;
974 }
975 return NO_ERROR;
976 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800977
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978 return err;
979}
980
981status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
982 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
983{
984 binder_transaction_data tr;
985
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800986 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 tr.target.handle = handle;
988 tr.code = code;
989 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +0400990 tr.cookie = 0;
991 tr.sender_pid = 0;
992 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800993
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 const status_t err = data.errorCheck();
995 if (err == NO_ERROR) {
996 tr.data_size = data.ipcDataSize();
997 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800998 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999 tr.data.ptr.offsets = data.ipcObjects();
1000 } else if (statusBuffer) {
1001 tr.flags |= TF_STATUS_CODE;
1002 *statusBuffer = err;
1003 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -08001004 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001006 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 } else {
1008 return (mLastError = err);
1009 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001010
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 mOut.writeInt32(cmd);
1012 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001013
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 return NO_ERROR;
1015}
1016
1017sp<BBinder> the_context_object;
1018
1019void setTheContextObject(sp<BBinder> obj)
1020{
1021 the_context_object = obj;
1022}
1023
1024status_t IPCThreadState::executeCommand(int32_t cmd)
1025{
1026 BBinder* obj;
1027 RefBase::weakref_type* refs;
1028 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001029
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001030 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 case BR_ERROR:
1032 result = mIn.readInt32();
1033 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001034
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 case BR_OK:
1036 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001039 refs = (RefBase::weakref_type*)mIn.readPointer();
1040 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001041 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1043 refs, obj, refs->refBase());
1044 obj->incStrong(mProcess.get());
1045 IF_LOG_REMOTEREFS() {
1046 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1047 obj->printRefs();
1048 }
1049 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001050 mOut.writePointer((uintptr_t)refs);
1051 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001053
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001055 refs = (RefBase::weakref_type*)mIn.readPointer();
1056 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001057 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1059 refs, obj, refs->refBase());
1060 IF_LOG_REMOTEREFS() {
1061 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1062 obj->printRefs();
1063 }
1064 mPendingStrongDerefs.push(obj);
1065 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001066
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001068 refs = (RefBase::weakref_type*)mIn.readPointer();
1069 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 refs->incWeak(mProcess.get());
1071 mOut.writeInt32(BC_INCREFS_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_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001077 refs = (RefBase::weakref_type*)mIn.readPointer();
1078 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 // NOTE: This assertion is not valid, because the object may no
1080 // longer exist (thus the (BBinder*)cast above resulting in a different
1081 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001082 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1084 // refs, obj, refs->refBase());
1085 mPendingWeakDerefs.push(refs);
1086 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001089 refs = (RefBase::weakref_type*)mIn.readPointer();
1090 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 {
1093 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001094 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1096 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 mOut.writeInt32(BC_ACQUIRE_RESULT);
1099 mOut.writeInt32((int32_t)success);
1100 }
1101 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 case BR_TRANSACTION:
1104 {
1105 binder_transaction_data tr;
1106 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001107 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 "Not enough command data for brTRANSACTION");
1109 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001110
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001111 //Record the fact that we're in a binder call.
1112 mIPCThreadStateBase->pushCurrentState(
1113 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001114 Parcel buffer;
1115 buffer.ipcSetDataReference(
1116 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1117 tr.data_size,
1118 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1119 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1120
1121 const pid_t origPid = mCallingPid;
1122 const uid_t origUid = mCallingUid;
1123 const int32_t origStrictModePolicy = mStrictModePolicy;
1124 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1125
1126 mCallingPid = tr.sender_pid;
1127 mCallingUid = tr.sender_euid;
1128 mLastTransactionBinderFlags = tr.flags;
1129
1130 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
1131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001133 status_t error;
1134 IF_LOG_TRANSACTIONS() {
1135 TextOutput::Bundle _b(alog);
1136 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1137 << " / obj " << tr.target.ptr << " / code "
1138 << TypeCode(tr.code) << ": " << indent << buffer
1139 << dedent << endl
1140 << "Data addr = "
1141 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1142 << ", offsets addr="
1143 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1144 }
1145 if (tr.target.ptr) {
1146 // We only have a weak reference on the target object, so we must first try to
1147 // safely acquire a strong reference before doing anything else with it.
1148 if (reinterpret_cast<RefBase::weakref_type*>(
1149 tr.target.ptr)->attemptIncStrong(this)) {
1150 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1151 &reply, tr.flags);
1152 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001153 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001154 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001155 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001156
Martijn Coenenea0090a2017-11-02 18:54:40 +00001157 } else {
1158 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001160
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001161 mIPCThreadStateBase->popCurrentState();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001162 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
1163 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001164
Martijn Coenenea0090a2017-11-02 18:54:40 +00001165 if ((tr.flags & TF_ONE_WAY) == 0) {
1166 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1167 if (error < NO_ERROR) reply.setError(error);
1168 sendReply(reply, 0);
1169 } else {
1170 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1171 }
1172
1173 mCallingPid = origPid;
1174 mCallingUid = origUid;
1175 mStrictModePolicy = origStrictModePolicy;
1176 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate440fd872010-03-18 17:55:03 -07001177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 IF_LOG_TRANSACTIONS() {
1179 TextOutput::Bundle _b(alog);
1180 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1181 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1182 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 }
1185 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 case BR_DEAD_BINDER:
1188 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001189 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 proxy->sendObituary();
1191 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001192 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1196 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001197 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 proxy->getWeakRefs()->decWeak(proxy);
1199 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 case BR_FINISHED:
1202 result = TIMED_OUT;
1203 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 case BR_NOOP:
1206 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 case BR_SPAWN_LOOPER:
1209 mProcess->spawnPooledThread(false);
1210 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 default:
liangweikanga43ee152016-10-25 16:37:54 +08001213 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 result = UNKNOWN_ERROR;
1215 break;
1216 }
1217
1218 if (result != NO_ERROR) {
1219 mLastError = result;
1220 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 return result;
1223}
1224
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001225bool IPCThreadState::isServingCall() const {
1226 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1227}
1228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229void IPCThreadState::threadDestructor(void *st)
1230{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001231 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1232 if (self) {
1233 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001234#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001235 if (self->mProcess->mDriverFD > 0) {
1236 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1237 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001239 delete self;
1240 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241}
1242
1243
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001244void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1245 size_t /*dataSize*/,
1246 const binder_size_t* /*objects*/,
1247 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248{
Steve Blocka19954a2012-01-04 20:05:49 +00001249 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 IF_LOG_COMMANDS() {
1251 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1252 }
Steve Block67263472012-01-09 18:35:44 +00001253 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001254 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 IPCThreadState* state = self();
1256 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001257 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258}
1259
1260}; // namespace android