blob: f052bcb982c5745297891baab8b7b67b1af13c68 [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
songjinshi73a7dde2016-10-18 21:05:56 +0800112static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113{
songjinshi73a7dde2016-10-18 21:05:56 +0800114 size_t idx = cmd & 0xff;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
116 return kReturnStrings[idx];
117 else
118 return "unknown";
119}
120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121static const void* printBinderTransactionData(TextOutput& out, const void* data)
122{
123 const binder_transaction_data* btd =
124 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700125 if (btd->target.handle < 1024) {
126 /* want to print descriptors in decimal; guess based on value */
127 out << "target.desc=" << btd->target.handle;
128 } else {
129 out << "target.ptr=" << btd->target.ptr;
130 }
131 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700132 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
134 << " bytes)" << endl
135 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700136 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 return btd+1;
138}
139
140static const void* printReturnCommand(TextOutput& out, const void* _cmd)
141{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700142 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100144 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700145 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100146 if (code == BR_ERROR) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700147 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700149 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 out << "Unknown reply: " << code << endl;
151 return cmd;
152 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700153 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 switch (code) {
156 case BR_TRANSACTION:
157 case BR_REPLY: {
158 out << ": " << indent;
159 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
160 out << dedent;
161 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 case BR_ACQUIRE_RESULT: {
164 const int32_t res = *cmd++;
165 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
166 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800167
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 case BR_INCREFS:
169 case BR_ACQUIRE:
170 case BR_RELEASE:
171 case BR_DECREFS: {
172 const int32_t b = *cmd++;
173 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700174 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 case BR_ATTEMPT_ACQUIRE: {
178 const int32_t p = *cmd++;
179 const int32_t b = *cmd++;
180 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700181 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 << "), pri=" << p;
183 } break;
184
185 case BR_DEAD_BINDER:
186 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
187 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700188 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700190
191 default:
192 // no details to show for: BR_OK, BR_DEAD_REPLY,
193 // BR_TRANSACTION_COMPLETE, BR_FINISHED
194 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 out << endl;
198 return cmd;
199}
200
201static const void* printCommand(TextOutput& out, const void* _cmd)
202{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700203 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100205 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700206 size_t cmdIndex = code & 0xff;
207
208 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 out << "Unknown command: " << code << endl;
210 return cmd;
211 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700212 out << kCommandStrings[cmdIndex];
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 switch (code) {
215 case BC_TRANSACTION:
216 case BC_REPLY: {
217 out << ": " << indent;
218 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
219 out << dedent;
220 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 case BC_ACQUIRE_RESULT: {
223 const int32_t res = *cmd++;
224 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
225 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 case BC_FREE_BUFFER: {
228 const int32_t buf = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700229 out << ": buffer=" << (void*)(long)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 case BC_INCREFS:
233 case BC_ACQUIRE:
234 case BC_RELEASE:
235 case BC_DECREFS: {
236 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700237 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 case BC_INCREFS_DONE:
241 case BC_ACQUIRE_DONE: {
242 const int32_t b = *cmd++;
243 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700244 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 case BC_ATTEMPT_ACQUIRE: {
248 const int32_t p = *cmd++;
249 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700250 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 case BC_REQUEST_DEATH_NOTIFICATION:
254 case BC_CLEAR_DEATH_NOTIFICATION: {
255 const int32_t h = *cmd++;
256 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700257 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 } break;
259
260 case BC_DEAD_BINDER_DONE: {
261 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700262 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700264
265 default:
266 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
267 // BC_EXIT_LOOPER
268 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 out << endl;
272 return cmd;
273}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274
275static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
276static bool gHaveTLS = false;
277static pthread_key_t gTLS = 0;
278static bool gShutdown = false;
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800279static bool gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280
281IPCThreadState* IPCThreadState::self()
282{
283 if (gHaveTLS) {
284restart:
285 const pthread_key_t k = gTLS;
286 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
287 if (st) return st;
288 return new IPCThreadState;
289 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800290
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800291 if (gShutdown) {
292 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700293 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800294 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 pthread_mutex_lock(&gTLSMutex);
297 if (!gHaveTLS) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800298 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
299 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800301 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
302 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700303 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 }
305 gHaveTLS = true;
306 }
307 pthread_mutex_unlock(&gTLSMutex);
308 goto restart;
309}
310
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800311IPCThreadState* IPCThreadState::selfOrNull()
312{
313 if (gHaveTLS) {
314 const pthread_key_t k = gTLS;
315 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
316 return st;
317 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700318 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800319}
320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321void IPCThreadState::shutdown()
322{
323 gShutdown = true;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 if (gHaveTLS) {
326 // XXX Need to wait for all thread pool threads to exit!
327 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
328 if (st) {
329 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700330 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 }
zhongjieff405782016-03-09 15:05:04 +0800332 pthread_key_delete(gTLS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 gHaveTLS = false;
334 }
335}
336
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800337void IPCThreadState::disableBackgroundScheduling(bool disable)
338{
339 gDisableBackgroundScheduling = disable;
340}
341
Martijn Coenen2b631742017-05-05 11:16:59 -0700342bool IPCThreadState::backgroundSchedulingDisabled()
343{
344 return gDisableBackgroundScheduling;
345}
346
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347sp<ProcessState> IPCThreadState::process()
348{
349 return mProcess;
350}
351
352status_t IPCThreadState::clearLastError()
353{
354 const status_t err = mLastError;
355 mLastError = NO_ERROR;
356 return err;
357}
358
Dan Stoza9c634fd2014-11-26 12:23:23 -0800359pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360{
361 return mCallingPid;
362}
363
Dan Stoza9c634fd2014-11-26 12:23:23 -0800364uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365{
366 return mCallingUid;
367}
368
369int64_t IPCThreadState::clearCallingIdentity()
370{
371 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
372 clearCaller();
373 return token;
374}
375
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700376void IPCThreadState::setStrictModePolicy(int32_t policy)
377{
378 mStrictModePolicy = policy;
379}
380
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700381int32_t IPCThreadState::getStrictModePolicy() const
382{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700383 return mStrictModePolicy;
384}
385
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700386void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
387{
388 mLastTransactionBinderFlags = flags;
389}
390
391int32_t IPCThreadState::getLastTransactionBinderFlags() const
392{
393 return mLastTransactionBinderFlags;
394}
395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396void IPCThreadState::restoreCallingIdentity(int64_t token)
397{
398 mCallingUid = (int)(token>>32);
399 mCallingPid = (int)token;
400}
401
402void IPCThreadState::clearCaller()
403{
Marco Nelissend43b1942009-07-17 07:59:17 -0700404 mCallingPid = getpid();
405 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406}
407
408void IPCThreadState::flushCommands()
409{
410 if (mProcess->mDriverFD <= 0)
411 return;
412 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700413 // The flush could have caused post-write refcount decrements to have
414 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
415 // being queued in mOut. So flush again, if we need to.
416 if (mOut.dataSize() > 0) {
417 talkWithDriver(false);
418 }
419 if (mOut.dataSize() > 0) {
420 ALOGW("mOut.dataSize() > 0 after flushCommands()");
421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422}
423
Wale Ogunwale376b8222015-04-13 16:16:10 -0700424void IPCThreadState::blockUntilThreadAvailable()
425{
426 pthread_mutex_lock(&mProcess->mThreadCountLock);
427 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700428 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
429 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
430 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700431 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
432 }
433 pthread_mutex_unlock(&mProcess->mThreadCountLock);
434}
435
Todd Poynor8d96cab2013-06-25 19:12:18 -0700436status_t IPCThreadState::getAndExecuteCommand()
437{
438 status_t result;
439 int32_t cmd;
440
441 result = talkWithDriver();
442 if (result >= NO_ERROR) {
443 size_t IN = mIn.dataAvail();
444 if (IN < sizeof(int32_t)) return result;
445 cmd = mIn.readInt32();
446 IF_LOG_COMMANDS() {
447 alog << "Processing top-level Command: "
448 << getReturnString(cmd) << endl;
449 }
450
Wale Ogunwale376b8222015-04-13 16:16:10 -0700451 pthread_mutex_lock(&mProcess->mThreadCountLock);
452 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700453 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
454 mProcess->mStarvationStartTimeMs == 0) {
455 mProcess->mStarvationStartTimeMs = uptimeMillis();
456 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700457 pthread_mutex_unlock(&mProcess->mThreadCountLock);
458
Todd Poynor8d96cab2013-06-25 19:12:18 -0700459 result = executeCommand(cmd);
460
Wale Ogunwale376b8222015-04-13 16:16:10 -0700461 pthread_mutex_lock(&mProcess->mThreadCountLock);
462 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700463 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
464 mProcess->mStarvationStartTimeMs != 0) {
465 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
466 if (starvationTimeMs > 100) {
467 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
468 mProcess->mMaxThreads, starvationTimeMs);
469 }
470 mProcess->mStarvationStartTimeMs = 0;
471 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700472 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
473 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700474 }
475
476 return result;
477}
478
479// When we've cleared the incoming command queue, process any pending derefs
480void IPCThreadState::processPendingDerefs()
481{
482 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200483 /*
484 * The decWeak()/decStrong() calls may cause a destructor to run,
485 * which in turn could have initiated an outgoing transaction,
486 * which in turn could cause us to add to the pending refs
487 * vectors; so instead of simply iterating, loop until they're empty.
488 *
489 * We do this in an outer loop, because calling decStrong()
490 * may result in something being added to mPendingWeakDerefs,
491 * which could be delayed until the next incoming command
492 * from the driver if we don't process it now.
493 */
494 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
495 while (mPendingWeakDerefs.size() > 0) {
496 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
497 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700498 refs->decWeak(mProcess.get());
499 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700500
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200501 if (mPendingStrongDerefs.size() > 0) {
502 // We don't use while() here because we don't want to re-order
503 // strong and weak decs at all; if this decStrong() causes both a
504 // decWeak() and a decStrong() to be queued, we want to process
505 // the decWeak() first.
506 BBinder* obj = mPendingStrongDerefs[0];
507 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700508 obj->decStrong(mProcess.get());
509 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700510 }
511 }
512}
513
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700514void IPCThreadState::processPostWriteDerefs()
515{
516 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
517 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
518 refs->decWeak(mProcess.get());
519 }
520 mPostWriteWeakDerefs.clear();
521
522 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
523 RefBase* obj = mPostWriteStrongDerefs[i];
524 obj->decStrong(mProcess.get());
525 }
526 mPostWriteStrongDerefs.clear();
527}
528
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529void IPCThreadState::joinThreadPool(bool isMain)
530{
531 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
532
533 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 status_t result;
536 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700537 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700539 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800540
Todd Poynor8d96cab2013-06-25 19:12:18 -0700541 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
542 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700543 mProcess->mDriverFD, result);
544 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800546
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 // Let this thread exit the thread pool if it is no longer
548 // needed and it is not the main process thread.
549 if(result == TIMED_OUT && !isMain) {
550 break;
551 }
552 } while (result != -ECONNREFUSED && result != -EBADF);
553
Wei Wangc7341432016-10-19 10:23:59 -0700554 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
555 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 mOut.writeInt32(BC_EXIT_LOOPER);
558 talkWithDriver(false);
559}
560
Todd Poynor8d96cab2013-06-25 19:12:18 -0700561int IPCThreadState::setupPolling(int* fd)
562{
563 if (mProcess->mDriverFD <= 0) {
564 return -EBADF;
565 }
566
567 mOut.writeInt32(BC_ENTER_LOOPER);
568 *fd = mProcess->mDriverFD;
569 return 0;
570}
571
572status_t IPCThreadState::handlePolledCommands()
573{
574 status_t result;
575
576 do {
577 result = getAndExecuteCommand();
578 } while (mIn.dataPosition() < mIn.dataSize());
579
580 processPendingDerefs();
581 flushCommands();
582 return result;
583}
584
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800585void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586{
Steve Blocka19954a2012-01-04 20:05:49 +0000587 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 flushCommands();
589 int fd = mProcess->mDriverFD;
590 mProcess->mDriverFD = -1;
591 close(fd);
592 //kill(getpid(), SIGKILL);
593}
594
595status_t IPCThreadState::transact(int32_t handle,
596 uint32_t code, const Parcel& data,
597 Parcel* reply, uint32_t flags)
598{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800599 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600
601 flags |= TF_ACCEPT_FDS;
602
603 IF_LOG_TRANSACTIONS() {
604 TextOutput::Bundle _b(alog);
605 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
606 << handle << " / code " << TypeCode(code) << ": "
607 << indent << data << dedent << endl;
608 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800609
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800610 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
611 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700612 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800613
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 if (err != NO_ERROR) {
615 if (reply) reply->setError(err);
616 return (mLastError = err);
617 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700620 #if 0
621 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000622 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700623 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000624 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700625 }
626 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 if (reply) {
628 err = waitForResponse(reply);
629 } else {
630 Parcel fakeReply;
631 err = waitForResponse(&fakeReply);
632 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700633 #if 0
634 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000635 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700636 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000637 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700638 }
639 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800640
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 IF_LOG_TRANSACTIONS() {
642 TextOutput::Bundle _b(alog);
643 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
644 << handle << ": ";
645 if (reply) alog << indent << *reply << dedent << endl;
646 else alog << "(none requested)" << endl;
647 }
648 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700649 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 return err;
653}
654
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700655void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656{
657 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
658 mOut.writeInt32(BC_ACQUIRE);
659 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700660 // Create a temp reference until the driver has handled this command.
661 proxy->incStrong(mProcess.get());
662 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663}
664
665void IPCThreadState::decStrongHandle(int32_t handle)
666{
667 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
668 mOut.writeInt32(BC_RELEASE);
669 mOut.writeInt32(handle);
670}
671
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700672void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673{
674 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
675 mOut.writeInt32(BC_INCREFS);
676 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700677 // Create a temp reference until the driver has handled this command.
678 proxy->getWeakRefs()->incWeak(mProcess.get());
679 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680}
681
682void IPCThreadState::decWeakHandle(int32_t handle)
683{
684 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
685 mOut.writeInt32(BC_DECREFS);
686 mOut.writeInt32(handle);
687}
688
689status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
690{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800691#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700692 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
694 mOut.writeInt32(0); // xxx was thread priority
695 mOut.writeInt32(handle);
696 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800697
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800699
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800701 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
703#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800706#else
707 (void)handle;
708 ALOGE("%s(%d): Not supported\n", __func__, handle);
709 return INVALID_OPERATION;
710#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711}
712
713void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
714{
715#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800716 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700718 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719}
720
721status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
722{
723 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
724 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000725 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 return NO_ERROR;
727}
728
729status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
730{
731 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
732 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000733 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 return NO_ERROR;
735}
736
737IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700738 : mProcess(ProcessState::self()),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700739 mStrictModePolicy(0),
740 mLastTransactionBinderFlags(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741{
742 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800743 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 mIn.setDataCapacity(256);
745 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000746 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747}
748
749IPCThreadState::~IPCThreadState()
750{
751}
752
Martijn Coenenea0090a2017-11-02 18:54:40 +0000753status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
754{
755 status_t err;
756 status_t statusBuffer;
757 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
758 if (err < NO_ERROR) return err;
759
Yi Kongfdd8da92018-06-07 17:52:27 -0700760 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000761}
762
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
764{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100765 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 int32_t err;
767
768 while (1) {
769 if ((err=talkWithDriver()) < NO_ERROR) break;
770 err = mIn.errorCheck();
771 if (err < NO_ERROR) break;
772 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800773
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100774 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800775
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 IF_LOG_COMMANDS() {
777 alog << "Processing waitForResponse Command: "
778 << getReturnString(cmd) << endl;
779 }
780
781 switch (cmd) {
782 case BR_TRANSACTION_COMPLETE:
783 if (!reply && !acquireResult) goto finish;
784 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800785
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 case BR_DEAD_REPLY:
787 err = DEAD_OBJECT;
788 goto finish;
789
790 case BR_FAILED_REPLY:
791 err = FAILED_TRANSACTION;
792 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800793
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 case BR_ACQUIRE_RESULT:
795 {
Steve Block67263472012-01-09 18:35:44 +0000796 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800797 const int32_t result = mIn.readInt32();
798 if (!acquireResult) continue;
799 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
800 }
801 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 case BR_REPLY:
804 {
805 binder_transaction_data tr;
806 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000807 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 if (err != NO_ERROR) goto finish;
809
810 if (reply) {
811 if ((tr.flags & TF_STATUS_CODE) == 0) {
812 reply->ipcSetDataReference(
813 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
814 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800815 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
816 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 freeBuffer, this);
818 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800819 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700820 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
822 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800823 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
824 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 }
826 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700827 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
829 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800830 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
831 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 continue;
833 }
834 }
835 goto finish;
836
837 default:
838 err = executeCommand(cmd);
839 if (err != NO_ERROR) goto finish;
840 break;
841 }
842 }
843
844finish:
845 if (err != NO_ERROR) {
846 if (acquireResult) *acquireResult = err;
847 if (reply) reply->setError(err);
848 mLastError = err;
849 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800850
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 return err;
852}
853
854status_t IPCThreadState::talkWithDriver(bool doReceive)
855{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100856 if (mProcess->mDriverFD <= 0) {
857 return -EBADF;
858 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800859
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800861
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 // Is the read buffer empty?
863 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800864
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865 // We don't want to write anything if we are still reading
866 // from data left in the input buffer and the caller
867 // has requested to read the next data.
868 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800869
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800871 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872
873 // This is what we'll read.
874 if (doReceive && needRead) {
875 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800876 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 } else {
878 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800879 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700881
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 IF_LOG_COMMANDS() {
883 TextOutput::Bundle _b(alog);
884 if (outAvail != 0) {
885 alog << "Sending commands to driver: " << indent;
886 const void* cmds = (const void*)bwr.write_buffer;
887 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
888 alog << HexDump(cmds, bwr.write_size) << endl;
889 while (cmds < end) cmds = printCommand(alog, cmds);
890 alog << dedent;
891 }
892 alog << "Size of receive buffer: " << bwr.read_size
893 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
894 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800895
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 // Return immediately if there is nothing to do.
897 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700898
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 bwr.write_consumed = 0;
900 bwr.read_consumed = 0;
901 status_t err;
902 do {
903 IF_LOG_COMMANDS() {
904 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
905 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700906#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
908 err = NO_ERROR;
909 else
910 err = -errno;
911#else
912 err = INVALID_OPERATION;
913#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100914 if (mProcess->mDriverFD <= 0) {
915 err = -EBADF;
916 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 IF_LOG_COMMANDS() {
918 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
919 }
920 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700921
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800923 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700925 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 }
927
928 if (err >= NO_ERROR) {
929 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800930 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700932 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700934 processPostWriteDerefs();
935 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 }
937 if (bwr.read_consumed > 0) {
938 mIn.setDataSize(bwr.read_consumed);
939 mIn.setDataPosition(0);
940 }
941 IF_LOG_COMMANDS() {
942 TextOutput::Bundle _b(alog);
943 alog << "Remaining data size: " << mOut.dataSize() << endl;
944 alog << "Received commands from driver: " << indent;
945 const void* cmds = mIn.data();
946 const void* end = mIn.data() + mIn.dataSize();
947 alog << HexDump(cmds, mIn.dataSize()) << endl;
948 while (cmds < end) cmds = printReturnCommand(alog, cmds);
949 alog << dedent;
950 }
951 return NO_ERROR;
952 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800953
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954 return err;
955}
956
957status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
958 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
959{
960 binder_transaction_data tr;
961
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800962 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963 tr.target.handle = handle;
964 tr.code = code;
965 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +0400966 tr.cookie = 0;
967 tr.sender_pid = 0;
968 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800969
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 const status_t err = data.errorCheck();
971 if (err == NO_ERROR) {
972 tr.data_size = data.ipcDataSize();
973 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800974 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 tr.data.ptr.offsets = data.ipcObjects();
976 } else if (statusBuffer) {
977 tr.flags |= TF_STATUS_CODE;
978 *statusBuffer = err;
979 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -0800980 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800982 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 } else {
984 return (mLastError = err);
985 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800986
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 mOut.writeInt32(cmd);
988 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -0800989
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 return NO_ERROR;
991}
992
993sp<BBinder> the_context_object;
994
995void setTheContextObject(sp<BBinder> obj)
996{
997 the_context_object = obj;
998}
999
1000status_t IPCThreadState::executeCommand(int32_t cmd)
1001{
1002 BBinder* obj;
1003 RefBase::weakref_type* refs;
1004 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001005
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001006 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 case BR_ERROR:
1008 result = mIn.readInt32();
1009 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001010
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 case BR_OK:
1012 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001013
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001015 refs = (RefBase::weakref_type*)mIn.readPointer();
1016 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001017 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1019 refs, obj, refs->refBase());
1020 obj->incStrong(mProcess.get());
1021 IF_LOG_REMOTEREFS() {
1022 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1023 obj->printRefs();
1024 }
1025 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001026 mOut.writePointer((uintptr_t)refs);
1027 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001029
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001031 refs = (RefBase::weakref_type*)mIn.readPointer();
1032 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001033 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1035 refs, obj, refs->refBase());
1036 IF_LOG_REMOTEREFS() {
1037 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1038 obj->printRefs();
1039 }
1040 mPendingStrongDerefs.push(obj);
1041 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001042
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001044 refs = (RefBase::weakref_type*)mIn.readPointer();
1045 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 refs->incWeak(mProcess.get());
1047 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001048 mOut.writePointer((uintptr_t)refs);
1049 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001051
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001053 refs = (RefBase::weakref_type*)mIn.readPointer();
1054 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 // NOTE: This assertion is not valid, because the object may no
1056 // longer exist (thus the (BBinder*)cast above resulting in a different
1057 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001058 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1060 // refs, obj, refs->refBase());
1061 mPendingWeakDerefs.push(refs);
1062 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001065 refs = (RefBase::weakref_type*)mIn.readPointer();
1066 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001067
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 {
1069 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001070 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1072 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 mOut.writeInt32(BC_ACQUIRE_RESULT);
1075 mOut.writeInt32((int32_t)success);
1076 }
1077 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 case BR_TRANSACTION:
1080 {
1081 binder_transaction_data tr;
1082 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001083 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 "Not enough command data for brTRANSACTION");
1085 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001086
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001087 //Record the fact that we're in a binder call.
1088 mIPCThreadStateBase->pushCurrentState(
1089 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001090 Parcel buffer;
1091 buffer.ipcSetDataReference(
1092 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1093 tr.data_size,
1094 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1095 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1096
1097 const pid_t origPid = mCallingPid;
1098 const uid_t origUid = mCallingUid;
1099 const int32_t origStrictModePolicy = mStrictModePolicy;
1100 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1101
1102 mCallingPid = tr.sender_pid;
1103 mCallingUid = tr.sender_euid;
1104 mLastTransactionBinderFlags = tr.flags;
1105
1106 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
1107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001109 status_t error;
1110 IF_LOG_TRANSACTIONS() {
1111 TextOutput::Bundle _b(alog);
1112 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1113 << " / obj " << tr.target.ptr << " / code "
1114 << TypeCode(tr.code) << ": " << indent << buffer
1115 << dedent << endl
1116 << "Data addr = "
1117 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1118 << ", offsets addr="
1119 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1120 }
1121 if (tr.target.ptr) {
1122 // We only have a weak reference on the target object, so we must first try to
1123 // safely acquire a strong reference before doing anything else with it.
1124 if (reinterpret_cast<RefBase::weakref_type*>(
1125 tr.target.ptr)->attemptIncStrong(this)) {
1126 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1127 &reply, tr.flags);
1128 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001129 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001130 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001131 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001132
Martijn Coenenea0090a2017-11-02 18:54:40 +00001133 } else {
1134 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001136
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001137 mIPCThreadStateBase->popCurrentState();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001138 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
1139 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001140
Martijn Coenenea0090a2017-11-02 18:54:40 +00001141 if ((tr.flags & TF_ONE_WAY) == 0) {
1142 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1143 if (error < NO_ERROR) reply.setError(error);
1144 sendReply(reply, 0);
1145 } else {
1146 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1147 }
1148
1149 mCallingPid = origPid;
1150 mCallingUid = origUid;
1151 mStrictModePolicy = origStrictModePolicy;
1152 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate440fd872010-03-18 17:55:03 -07001153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154 IF_LOG_TRANSACTIONS() {
1155 TextOutput::Bundle _b(alog);
1156 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1157 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1158 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 }
1161 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163 case BR_DEAD_BINDER:
1164 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001165 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 proxy->sendObituary();
1167 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001168 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001170
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1172 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001173 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174 proxy->getWeakRefs()->decWeak(proxy);
1175 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 case BR_FINISHED:
1178 result = TIMED_OUT;
1179 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 case BR_NOOP:
1182 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 case BR_SPAWN_LOOPER:
1185 mProcess->spawnPooledThread(false);
1186 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001187
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 default:
liangweikanga43ee152016-10-25 16:37:54 +08001189 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 result = UNKNOWN_ERROR;
1191 break;
1192 }
1193
1194 if (result != NO_ERROR) {
1195 mLastError = result;
1196 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 return result;
1199}
1200
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001201bool IPCThreadState::isServingCall() const {
1202 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1203}
1204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205void IPCThreadState::threadDestructor(void *st)
1206{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001207 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1208 if (self) {
1209 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001210#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001211 if (self->mProcess->mDriverFD > 0) {
1212 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1213 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001215 delete self;
1216 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217}
1218
1219
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001220void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1221 size_t /*dataSize*/,
1222 const binder_size_t* /*objects*/,
1223 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224{
Steve Blocka19954a2012-01-04 20:05:49 +00001225 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 IF_LOG_COMMANDS() {
1227 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1228 }
Steve Block67263472012-01-09 18:35:44 +00001229 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001230 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 IPCThreadState* state = self();
1232 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001233 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234}
1235
1236}; // namespace android