blob: 66117bf5a5950a6fe97e9db1067763f788235700 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jason Parksdcd39582009-11-03 12:14:38 -080017#define LOG_TAG "IPCThreadState"
18
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IPCThreadState.h>
Jayant Chowdharydac6dc82018-10-01 22:52:44 +000020#include <binderthreadstate/IPCThreadStateBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/Binder.h>
23#include <binder/BpBinder.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070024#include <binder/TextOutput.h>
25
Steven Moreland7732a092019-01-02 17:54:16 -080026#include <android-base/macros.h>
Glenn Kastena26e1cf2012-03-16 07:15:23 -070027#include <cutils/sched_policy.h>
Steven Moreland7732a092019-01-02 17:54:16 -080028#include <utils/CallStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/Log.h>
Colin Cross96e83222016-04-15 14:29:55 -070030#include <utils/SystemClock.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/threads.h>
32
Mathias Agopian208059f2009-05-18 15:08:03 -070033#include <private/binder/binder_module.h>
34#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <errno.h>
Colin Cross96e83222016-04-15 14:29:55 -070037#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <pthread.h>
39#include <sched.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080040#include <signal.h>
41#include <stdio.h>
42#include <sys/ioctl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <sys/resource.h>
Yabin Cui8fb2d252015-01-26 19:45:47 -080044#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46#if LOG_NDEBUG
47
48#define IF_LOG_TRANSACTIONS() if (false)
49#define IF_LOG_COMMANDS() if (false)
50#define LOG_REMOTEREFS(...)
51#define IF_LOG_REMOTEREFS() if (false)
Tim Murrayd429f4a2017-03-07 09:31:09 -080052
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#define LOG_THREADPOOL(...)
54#define LOG_ONEWAY(...)
55
56#else
57
Steve Block9f760152011-10-12 17:27:03 +010058#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
59#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
60#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
61#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
62#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
63#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
65#endif
66
67// ---------------------------------------------------------------------------
68
69namespace android {
70
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -070071// Static const and functions will be optimized out if not used,
72// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073static const char *kReturnStrings[] = {
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070074 "BR_ERROR",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 "BR_OK",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 "BR_TRANSACTION",
77 "BR_REPLY",
78 "BR_ACQUIRE_RESULT",
79 "BR_DEAD_REPLY",
80 "BR_TRANSACTION_COMPLETE",
81 "BR_INCREFS",
82 "BR_ACQUIRE",
83 "BR_RELEASE",
84 "BR_DECREFS",
85 "BR_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 "BR_NOOP",
87 "BR_SPAWN_LOOPER",
88 "BR_FINISHED",
89 "BR_DEAD_BINDER",
Andy McFaddenaefc9cd2011-08-31 07:43:40 -070090 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
91 "BR_FAILED_REPLY"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092};
93
94static const char *kCommandStrings[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 "BC_TRANSACTION",
96 "BC_REPLY",
97 "BC_ACQUIRE_RESULT",
98 "BC_FREE_BUFFER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 "BC_INCREFS",
100 "BC_ACQUIRE",
101 "BC_RELEASE",
102 "BC_DECREFS",
103 "BC_INCREFS_DONE",
104 "BC_ACQUIRE_DONE",
105 "BC_ATTEMPT_ACQUIRE",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 "BC_REGISTER_LOOPER",
107 "BC_ENTER_LOOPER",
108 "BC_EXIT_LOOPER",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 "BC_REQUEST_DEATH_NOTIFICATION",
110 "BC_CLEAR_DEATH_NOTIFICATION",
111 "BC_DEAD_BINDER_DONE"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112};
113
songjinshi73a7dde2016-10-18 21:05:56 +0800114static const char* getReturnString(uint32_t cmd)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115{
songjinshi8e486c62019-04-04 11:22:52 +0800116 size_t idx = cmd & _IOC_NRMASK;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
118 return kReturnStrings[idx];
119 else
120 return "unknown";
121}
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123static const void* printBinderTransactionData(TextOutput& out, const void* data)
124{
125 const binder_transaction_data* btd =
126 (const binder_transaction_data*)data;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700127 if (btd->target.handle < 1024) {
128 /* want to print descriptors in decimal; guess based on value */
129 out << "target.desc=" << btd->target.handle;
130 } else {
131 out << "target.ptr=" << btd->target.ptr;
132 }
133 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700134 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
136 << " bytes)" << endl
137 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700138 << " bytes)";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 return btd+1;
140}
141
142static const void* printReturnCommand(TextOutput& out, const void* _cmd)
143{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700144 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100146 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700147 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100148 if (code == BR_ERROR) {
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700149 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 return cmd;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700151 } else if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 out << "Unknown reply: " << code << endl;
153 return cmd;
154 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700155 out << kReturnStrings[cmdIndex];
Tim Murrayd429f4a2017-03-07 09:31:09 -0800156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 switch (code) {
158 case BR_TRANSACTION:
159 case BR_REPLY: {
160 out << ": " << indent;
161 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
162 out << dedent;
163 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 case BR_ACQUIRE_RESULT: {
166 const int32_t res = *cmd++;
167 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
168 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 case BR_INCREFS:
171 case BR_ACQUIRE:
172 case BR_RELEASE:
173 case BR_DECREFS: {
174 const int32_t b = *cmd++;
175 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700176 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 case BR_ATTEMPT_ACQUIRE: {
180 const int32_t p = *cmd++;
181 const int32_t b = *cmd++;
182 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700183 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 << "), pri=" << p;
185 } break;
186
187 case BR_DEAD_BINDER:
188 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
189 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700190 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700192
193 default:
194 // no details to show for: BR_OK, BR_DEAD_REPLY,
195 // BR_TRANSACTION_COMPLETE, BR_FINISHED
196 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 out << endl;
200 return cmd;
201}
202
203static const void* printCommand(TextOutput& out, const void* _cmd)
204{
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700205 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100207 uint32_t code = (uint32_t)*cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700208 size_t cmdIndex = code & 0xff;
209
210 if (cmdIndex >= N) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 out << "Unknown command: " << code << endl;
212 return cmd;
213 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700214 out << kCommandStrings[cmdIndex];
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 switch (code) {
217 case BC_TRANSACTION:
218 case BC_REPLY: {
219 out << ": " << indent;
220 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
221 out << dedent;
222 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 case BC_ACQUIRE_RESULT: {
225 const int32_t res = *cmd++;
226 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
227 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 case BC_FREE_BUFFER: {
230 const int32_t buf = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700231 out << ": buffer=" << (void*)(long)buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 case BC_INCREFS:
235 case BC_ACQUIRE:
236 case BC_RELEASE:
237 case BC_DECREFS: {
238 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700239 out << ": desc=" << d;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 case BC_INCREFS_DONE:
243 case BC_ACQUIRE_DONE: {
244 const int32_t b = *cmd++;
245 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700246 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 case BC_ATTEMPT_ACQUIRE: {
250 const int32_t p = *cmd++;
251 const int32_t d = *cmd++;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700252 out << ": desc=" << d << ", pri=" << p;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800254
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 case BC_REQUEST_DEATH_NOTIFICATION:
256 case BC_CLEAR_DEATH_NOTIFICATION: {
257 const int32_t h = *cmd++;
258 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700259 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 } break;
261
262 case BC_DEAD_BINDER_DONE: {
263 const int32_t c = *cmd++;
Chih-Hung Hsieh8e5337d2014-10-24 14:10:09 -0700264 out << ": death cookie " << (void*)(long)c;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 } break;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700266
267 default:
268 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
269 // BC_EXIT_LOOPER
270 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 out << endl;
274 return cmd;
275}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276
277static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
278static bool gHaveTLS = false;
279static pthread_key_t gTLS = 0;
280static bool gShutdown = false;
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800281static bool gDisableBackgroundScheduling = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282
283IPCThreadState* IPCThreadState::self()
284{
285 if (gHaveTLS) {
286restart:
287 const pthread_key_t k = gTLS;
288 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
289 if (st) return st;
290 return new IPCThreadState;
291 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800292
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800293 if (gShutdown) {
294 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
Yi Kongfdd8da92018-06-07 17:52:27 -0700295 return nullptr;
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800296 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 pthread_mutex_lock(&gTLSMutex);
299 if (!gHaveTLS) {
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800300 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
301 if (key_create_value != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampef31a3eb2016-02-01 13:21:56 -0800303 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
304 strerror(key_create_value));
Yi Kongfdd8da92018-06-07 17:52:27 -0700305 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 }
307 gHaveTLS = true;
308 }
309 pthread_mutex_unlock(&gTLSMutex);
310 goto restart;
311}
312
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800313IPCThreadState* IPCThreadState::selfOrNull()
314{
315 if (gHaveTLS) {
316 const pthread_key_t k = gTLS;
317 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
318 return st;
319 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700320 return nullptr;
Brad Fitzpatrick1b608432010-12-13 16:52:35 -0800321}
322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323void IPCThreadState::shutdown()
324{
325 gShutdown = true;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 if (gHaveTLS) {
328 // XXX Need to wait for all thread pool threads to exit!
329 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
330 if (st) {
331 delete st;
Yi Kongfdd8da92018-06-07 17:52:27 -0700332 pthread_setspecific(gTLS, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 }
zhongjieff405782016-03-09 15:05:04 +0800334 pthread_key_delete(gTLS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 gHaveTLS = false;
336 }
337}
338
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800339void IPCThreadState::disableBackgroundScheduling(bool disable)
340{
341 gDisableBackgroundScheduling = disable;
342}
343
Martijn Coenen2b631742017-05-05 11:16:59 -0700344bool IPCThreadState::backgroundSchedulingDisabled()
345{
346 return gDisableBackgroundScheduling;
347}
348
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349sp<ProcessState> IPCThreadState::process()
350{
351 return mProcess;
352}
353
354status_t IPCThreadState::clearLastError()
355{
356 const status_t err = mLastError;
357 mLastError = NO_ERROR;
358 return err;
359}
360
Dan Stoza9c634fd2014-11-26 12:23:23 -0800361pid_t IPCThreadState::getCallingPid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362{
363 return mCallingPid;
364}
365
Dan Stoza9c634fd2014-11-26 12:23:23 -0800366uid_t IPCThreadState::getCallingUid() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367{
368 return mCallingUid;
369}
370
371int64_t IPCThreadState::clearCallingIdentity()
372{
373 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
374 clearCaller();
375 return token;
376}
377
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700378void IPCThreadState::setStrictModePolicy(int32_t policy)
379{
380 mStrictModePolicy = policy;
381}
382
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700383int32_t IPCThreadState::getStrictModePolicy() const
384{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700385 return mStrictModePolicy;
386}
387
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700388void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
389{
390 mLastTransactionBinderFlags = flags;
391}
392
393int32_t IPCThreadState::getLastTransactionBinderFlags() const
394{
395 return mLastTransactionBinderFlags;
396}
397
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398void IPCThreadState::restoreCallingIdentity(int64_t token)
399{
400 mCallingUid = (int)(token>>32);
401 mCallingPid = (int)token;
402}
403
404void IPCThreadState::clearCaller()
405{
Marco Nelissend43b1942009-07-17 07:59:17 -0700406 mCallingPid = getpid();
407 mCallingUid = getuid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408}
409
410void IPCThreadState::flushCommands()
411{
412 if (mProcess->mDriverFD <= 0)
413 return;
414 talkWithDriver(false);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700415 // The flush could have caused post-write refcount decrements to have
416 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
417 // being queued in mOut. So flush again, if we need to.
418 if (mOut.dataSize() > 0) {
419 talkWithDriver(false);
420 }
421 if (mOut.dataSize() > 0) {
422 ALOGW("mOut.dataSize() > 0 after flushCommands()");
423 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424}
425
Wale Ogunwale376b8222015-04-13 16:16:10 -0700426void IPCThreadState::blockUntilThreadAvailable()
427{
428 pthread_mutex_lock(&mProcess->mThreadCountLock);
429 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwalea3206e62015-04-21 12:29:50 -0700430 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
431 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
432 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale376b8222015-04-13 16:16:10 -0700433 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
434 }
435 pthread_mutex_unlock(&mProcess->mThreadCountLock);
436}
437
Todd Poynor8d96cab2013-06-25 19:12:18 -0700438status_t IPCThreadState::getAndExecuteCommand()
439{
440 status_t result;
441 int32_t cmd;
442
443 result = talkWithDriver();
444 if (result >= NO_ERROR) {
445 size_t IN = mIn.dataAvail();
446 if (IN < sizeof(int32_t)) return result;
447 cmd = mIn.readInt32();
448 IF_LOG_COMMANDS() {
449 alog << "Processing top-level Command: "
450 << getReturnString(cmd) << endl;
451 }
452
Wale Ogunwale376b8222015-04-13 16:16:10 -0700453 pthread_mutex_lock(&mProcess->mThreadCountLock);
454 mProcess->mExecutingThreadsCount++;
Colin Cross96e83222016-04-15 14:29:55 -0700455 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
456 mProcess->mStarvationStartTimeMs == 0) {
457 mProcess->mStarvationStartTimeMs = uptimeMillis();
458 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700459 pthread_mutex_unlock(&mProcess->mThreadCountLock);
460
Todd Poynor8d96cab2013-06-25 19:12:18 -0700461 result = executeCommand(cmd);
462
Wale Ogunwale376b8222015-04-13 16:16:10 -0700463 pthread_mutex_lock(&mProcess->mThreadCountLock);
464 mProcess->mExecutingThreadsCount--;
Colin Cross96e83222016-04-15 14:29:55 -0700465 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
466 mProcess->mStarvationStartTimeMs != 0) {
467 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
468 if (starvationTimeMs > 100) {
469 ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms",
470 mProcess->mMaxThreads, starvationTimeMs);
471 }
472 mProcess->mStarvationStartTimeMs = 0;
473 }
Wale Ogunwale376b8222015-04-13 16:16:10 -0700474 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
475 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700476 }
477
478 return result;
479}
480
481// When we've cleared the incoming command queue, process any pending derefs
482void IPCThreadState::processPendingDerefs()
483{
484 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200485 /*
486 * The decWeak()/decStrong() calls may cause a destructor to run,
487 * which in turn could have initiated an outgoing transaction,
488 * which in turn could cause us to add to the pending refs
489 * vectors; so instead of simply iterating, loop until they're empty.
490 *
491 * We do this in an outer loop, because calling decStrong()
492 * may result in something being added to mPendingWeakDerefs,
493 * which could be delayed until the next incoming command
494 * from the driver if we don't process it now.
495 */
496 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
497 while (mPendingWeakDerefs.size() > 0) {
498 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
499 mPendingWeakDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700500 refs->decWeak(mProcess.get());
501 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700502
Martijn Coenen0791fbf2017-08-08 15:36:16 +0200503 if (mPendingStrongDerefs.size() > 0) {
504 // We don't use while() here because we don't want to re-order
505 // strong and weak decs at all; if this decStrong() causes both a
506 // decWeak() and a decStrong() to be queued, we want to process
507 // the decWeak() first.
508 BBinder* obj = mPendingStrongDerefs[0];
509 mPendingStrongDerefs.removeAt(0);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700510 obj->decStrong(mProcess.get());
511 }
Todd Poynor8d96cab2013-06-25 19:12:18 -0700512 }
513 }
514}
515
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700516void IPCThreadState::processPostWriteDerefs()
517{
518 for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) {
519 RefBase::weakref_type* refs = mPostWriteWeakDerefs[i];
520 refs->decWeak(mProcess.get());
521 }
522 mPostWriteWeakDerefs.clear();
523
524 for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) {
525 RefBase* obj = mPostWriteStrongDerefs[i];
526 obj->decStrong(mProcess.get());
527 }
528 mPostWriteStrongDerefs.clear();
529}
530
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531void IPCThreadState::joinThreadPool(bool isMain)
532{
533 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
534
535 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800536
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 status_t result;
538 do {
Todd Poynor8d96cab2013-06-25 19:12:18 -0700539 processPendingDerefs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 // now get the next command to be processed, waiting if necessary
Todd Poynor8d96cab2013-06-25 19:12:18 -0700541 result = getAndExecuteCommand();
Jason Parksdcd39582009-11-03 12:14:38 -0800542
Todd Poynor8d96cab2013-06-25 19:12:18 -0700543 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
544 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeref073862013-06-11 11:30:21 -0700545 mProcess->mDriverFD, result);
546 abort();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800548
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 // Let this thread exit the thread pool if it is no longer
550 // needed and it is not the main process thread.
551 if(result == TIMED_OUT && !isMain) {
552 break;
553 }
554 } while (result != -ECONNREFUSED && result != -EBADF);
555
Wei Wangc7341432016-10-19 10:23:59 -0700556 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
557 (void*)pthread_self(), getpid(), result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800558
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 mOut.writeInt32(BC_EXIT_LOOPER);
560 talkWithDriver(false);
561}
562
Todd Poynor8d96cab2013-06-25 19:12:18 -0700563int IPCThreadState::setupPolling(int* fd)
564{
565 if (mProcess->mDriverFD <= 0) {
566 return -EBADF;
567 }
568
569 mOut.writeInt32(BC_ENTER_LOOPER);
570 *fd = mProcess->mDriverFD;
571 return 0;
572}
573
574status_t IPCThreadState::handlePolledCommands()
575{
576 status_t result;
577
578 do {
579 result = getAndExecuteCommand();
580 } while (mIn.dataPosition() < mIn.dataSize());
581
582 processPendingDerefs();
583 flushCommands();
584 return result;
585}
586
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800587void IPCThreadState::stopProcess(bool /*immediate*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588{
Steve Blocka19954a2012-01-04 20:05:49 +0000589 //ALOGI("**** STOPPING PROCESS");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 flushCommands();
591 int fd = mProcess->mDriverFD;
592 mProcess->mDriverFD = -1;
593 close(fd);
594 //kill(getpid(), SIGKILL);
595}
596
597status_t IPCThreadState::transact(int32_t handle,
598 uint32_t code, const Parcel& data,
599 Parcel* reply, uint32_t flags)
600{
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800601 status_t err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602
603 flags |= TF_ACCEPT_FDS;
604
605 IF_LOG_TRANSACTIONS() {
606 TextOutput::Bundle _b(alog);
607 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
608 << handle << " / code " << TypeCode(code) << ": "
609 << indent << data << dedent << endl;
610 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800611
Ganesh Mahendran58e5daa2017-10-11 18:05:13 +0800612 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
613 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
Yi Kongfdd8da92018-06-07 17:52:27 -0700614 err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800615
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 if (err != NO_ERROR) {
617 if (reply) reply->setError(err);
618 return (mLastError = err);
619 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800620
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 if ((flags & TF_ONE_WAY) == 0) {
Steven Moreland7732a092019-01-02 17:54:16 -0800622 if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) {
623 if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) {
624 ALOGE("Process making non-oneway call but is restricted.");
625 CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(),
626 ANDROID_LOG_ERROR);
627 } else /* FATAL_IF_NOT_ONEWAY */ {
628 LOG_ALWAYS_FATAL("Process may not make oneway calls.");
629 }
630 }
631
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700632 #if 0
633 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000634 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700635 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000636 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700637 }
638 #endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 if (reply) {
640 err = waitForResponse(reply);
641 } else {
642 Parcel fakeReply;
643 err = waitForResponse(&fakeReply);
644 }
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700645 #if 0
646 if (code == 4) { // relayout
Steve Blocka19954a2012-01-04 20:05:49 +0000647 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700648 } else {
Steve Blocka19954a2012-01-04 20:05:49 +0000649 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn67f78c42010-09-24 11:16:23 -0700650 }
651 #endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800652
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 IF_LOG_TRANSACTIONS() {
654 TextOutput::Bundle _b(alog);
655 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
656 << handle << ": ";
657 if (reply) alog << indent << *reply << dedent << endl;
658 else alog << "(none requested)" << endl;
659 }
660 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700661 err = waitForResponse(nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 return err;
665}
666
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700667void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668{
669 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
670 mOut.writeInt32(BC_ACQUIRE);
671 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700672 // Create a temp reference until the driver has handled this command.
673 proxy->incStrong(mProcess.get());
674 mPostWriteStrongDerefs.push(proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675}
676
677void IPCThreadState::decStrongHandle(int32_t handle)
678{
679 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
680 mOut.writeInt32(BC_RELEASE);
681 mOut.writeInt32(handle);
682}
683
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700684void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685{
686 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
687 mOut.writeInt32(BC_INCREFS);
688 mOut.writeInt32(handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700689 // Create a temp reference until the driver has handled this command.
690 proxy->getWeakRefs()->incWeak(mProcess.get());
691 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692}
693
694void IPCThreadState::decWeakHandle(int32_t handle)
695{
696 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
697 mOut.writeInt32(BC_DECREFS);
698 mOut.writeInt32(handle);
699}
700
701status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
702{
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800703#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700704 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
706 mOut.writeInt32(0); // xxx was thread priority
707 mOut.writeInt32(handle);
708 status_t result = UNKNOWN_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 waitForResponse(NULL, &result);
Tim Murrayd429f4a2017-03-07 09:31:09 -0800711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800713 ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
715#endif
Tim Murrayd429f4a2017-03-07 09:31:09 -0800716
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717 return result;
Arve Hjønnevåg11cfdcc2014-02-14 20:14:02 -0800718#else
719 (void)handle;
720 ALOGE("%s(%d): Not supported\n", __func__, handle);
721 return INVALID_OPERATION;
722#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723}
724
725void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
726{
727#if LOG_REFCOUNTS
liangweikanga43ee152016-10-25 16:37:54 +0800728 ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729#endif
Manoj Gupta9cec85b2017-09-19 16:34:29 -0700730 self()->mProcess->expungeHandle(handle, binder); // NOLINT
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731}
732
733status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
734{
735 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
736 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000737 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 return NO_ERROR;
739}
740
741status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
742{
743 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
744 mOut.writeInt32((int32_t)handle);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000745 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 return NO_ERROR;
747}
748
749IPCThreadState::IPCThreadState()
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700750 : mProcess(ProcessState::self()),
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700751 mStrictModePolicy(0),
Steven Moreland7732a092019-01-02 17:54:16 -0800752 mLastTransactionBinderFlags(0),
753 mCallRestriction(mProcess->mCallRestriction)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754{
755 pthread_setspecific(gTLS, this);
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800756 clearCaller();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 mIn.setDataCapacity(256);
758 mOut.setDataCapacity(256);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000759 mIPCThreadStateBase = IPCThreadStateBase::self();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760}
761
762IPCThreadState::~IPCThreadState()
763{
764}
765
Martijn Coenenea0090a2017-11-02 18:54:40 +0000766status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
767{
768 status_t err;
769 status_t statusBuffer;
770 err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
771 if (err < NO_ERROR) return err;
772
Yi Kongfdd8da92018-06-07 17:52:27 -0700773 return waitForResponse(nullptr, nullptr);
Martijn Coenenea0090a2017-11-02 18:54:40 +0000774}
775
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
777{
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100778 uint32_t cmd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 int32_t err;
780
781 while (1) {
782 if ((err=talkWithDriver()) < NO_ERROR) break;
783 err = mIn.errorCheck();
784 if (err < NO_ERROR) break;
785 if (mIn.dataAvail() == 0) continue;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800786
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +0100787 cmd = (uint32_t)mIn.readInt32();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800788
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 IF_LOG_COMMANDS() {
790 alog << "Processing waitForResponse Command: "
791 << getReturnString(cmd) << endl;
792 }
793
794 switch (cmd) {
795 case BR_TRANSACTION_COMPLETE:
796 if (!reply && !acquireResult) goto finish;
797 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800798
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 case BR_DEAD_REPLY:
800 err = DEAD_OBJECT;
801 goto finish;
802
803 case BR_FAILED_REPLY:
804 err = FAILED_TRANSACTION;
805 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800806
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807 case BR_ACQUIRE_RESULT:
808 {
Steve Block67263472012-01-09 18:35:44 +0000809 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810 const int32_t result = mIn.readInt32();
811 if (!acquireResult) continue;
812 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
813 }
814 goto finish;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 case BR_REPLY:
817 {
818 binder_transaction_data tr;
819 err = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +0000820 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 if (err != NO_ERROR) goto finish;
822
823 if (reply) {
824 if ((tr.flags & TF_STATUS_CODE) == 0) {
825 reply->ipcSetDataReference(
826 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
827 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800828 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
829 tr.offsets_size/sizeof(binder_size_t),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 freeBuffer, this);
831 } else {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800832 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Yi Kongfdd8da92018-06-07 17:52:27 -0700833 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
835 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800836 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
837 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 }
839 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700840 freeBuffer(nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
842 tr.data_size,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800843 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
844 tr.offsets_size/sizeof(binder_size_t), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 continue;
846 }
847 }
848 goto finish;
849
850 default:
851 err = executeCommand(cmd);
852 if (err != NO_ERROR) goto finish;
853 break;
854 }
855 }
856
857finish:
858 if (err != NO_ERROR) {
859 if (acquireResult) *acquireResult = err;
860 if (reply) reply->setError(err);
861 mLastError = err;
862 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800863
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 return err;
865}
866
867status_t IPCThreadState::talkWithDriver(bool doReceive)
868{
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100869 if (mProcess->mDriverFD <= 0) {
870 return -EBADF;
871 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800872
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 binder_write_read bwr;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800874
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 // Is the read buffer empty?
876 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Tim Murrayd429f4a2017-03-07 09:31:09 -0800877
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 // We don't want to write anything if we are still reading
879 // from data left in the input buffer and the caller
880 // has requested to read the next data.
881 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800882
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 bwr.write_size = outAvail;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800884 bwr.write_buffer = (uintptr_t)mOut.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885
886 // This is what we'll read.
887 if (doReceive && needRead) {
888 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800889 bwr.read_buffer = (uintptr_t)mIn.data();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890 } else {
891 bwr.read_size = 0;
Ben Chengd640f892011-12-01 17:11:32 -0800892 bwr.read_buffer = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 }
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700894
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 IF_LOG_COMMANDS() {
896 TextOutput::Bundle _b(alog);
897 if (outAvail != 0) {
898 alog << "Sending commands to driver: " << indent;
899 const void* cmds = (const void*)bwr.write_buffer;
900 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
901 alog << HexDump(cmds, bwr.write_size) << endl;
902 while (cmds < end) cmds = printCommand(alog, cmds);
903 alog << dedent;
904 }
905 alog << "Size of receive buffer: " << bwr.read_size
906 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
907 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800908
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 // Return immediately if there is nothing to do.
910 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700911
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912 bwr.write_consumed = 0;
913 bwr.read_consumed = 0;
914 status_t err;
915 do {
916 IF_LOG_COMMANDS() {
917 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
918 }
Elliott Hughes6071da72015-08-12 15:27:47 -0700919#if defined(__ANDROID__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
921 err = NO_ERROR;
922 else
923 err = -errno;
924#else
925 err = INVALID_OPERATION;
926#endif
Johannes Carlssondb1597a2011-02-17 14:06:53 +0100927 if (mProcess->mDriverFD <= 0) {
928 err = -EBADF;
929 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930 IF_LOG_COMMANDS() {
931 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
932 }
933 } while (err == -EINTR);
Andy McFaddenaefc9cd2011-08-31 07:43:40 -0700934
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 IF_LOG_COMMANDS() {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800936 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor8d96cab2013-06-25 19:12:18 -0700938 << "), read consumed: " << bwr.read_consumed << endl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 }
940
941 if (err >= NO_ERROR) {
942 if (bwr.write_consumed > 0) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800943 if (bwr.write_consumed < mOut.dataSize())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944 mOut.remove(0, bwr.write_consumed);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700945 else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 mOut.setDataSize(0);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700947 processPostWriteDerefs();
948 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 }
950 if (bwr.read_consumed > 0) {
951 mIn.setDataSize(bwr.read_consumed);
952 mIn.setDataPosition(0);
953 }
954 IF_LOG_COMMANDS() {
955 TextOutput::Bundle _b(alog);
956 alog << "Remaining data size: " << mOut.dataSize() << endl;
957 alog << "Received commands from driver: " << indent;
958 const void* cmds = mIn.data();
959 const void* end = mIn.data() + mIn.dataSize();
960 alog << HexDump(cmds, mIn.dataSize()) << endl;
961 while (cmds < end) cmds = printReturnCommand(alog, cmds);
962 alog << dedent;
963 }
964 return NO_ERROR;
965 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800966
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967 return err;
968}
969
970status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
971 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
972{
973 binder_transaction_data tr;
974
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800975 tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 tr.target.handle = handle;
977 tr.code = code;
978 tr.flags = binderFlags;
Evgeniy Stepanovd5474322011-04-21 14:15:00 +0400979 tr.cookie = 0;
980 tr.sender_pid = 0;
981 tr.sender_euid = 0;
Tim Murrayd429f4a2017-03-07 09:31:09 -0800982
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 const status_t err = data.errorCheck();
984 if (err == NO_ERROR) {
985 tr.data_size = data.ipcDataSize();
986 tr.data.ptr.buffer = data.ipcData();
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800987 tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 tr.data.ptr.offsets = data.ipcObjects();
989 } else if (statusBuffer) {
990 tr.flags |= TF_STATUS_CODE;
991 *statusBuffer = err;
992 tr.data_size = sizeof(status_t);
Arve Hjønnevåg87b30d02014-02-18 21:04:31 -0800993 tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 tr.offsets_size = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800995 tr.data.ptr.offsets = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 } else {
997 return (mLastError = err);
998 }
Tim Murrayd429f4a2017-03-07 09:31:09 -0800999
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 mOut.writeInt32(cmd);
1001 mOut.write(&tr, sizeof(tr));
Tim Murrayd429f4a2017-03-07 09:31:09 -08001002
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 return NO_ERROR;
1004}
1005
1006sp<BBinder> the_context_object;
1007
1008void setTheContextObject(sp<BBinder> obj)
1009{
1010 the_context_object = obj;
1011}
1012
1013status_t IPCThreadState::executeCommand(int32_t cmd)
1014{
1015 BBinder* obj;
1016 RefBase::weakref_type* refs;
1017 status_t result = NO_ERROR;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001018
Bernhard Rosenkränzer74debb02014-11-25 21:55:33 +01001019 switch ((uint32_t)cmd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 case BR_ERROR:
1021 result = mIn.readInt32();
1022 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 case BR_OK:
1025 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001026
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 case BR_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001028 refs = (RefBase::weakref_type*)mIn.readPointer();
1029 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001030 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1032 refs, obj, refs->refBase());
1033 obj->incStrong(mProcess.get());
1034 IF_LOG_REMOTEREFS() {
1035 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1036 obj->printRefs();
1037 }
1038 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001039 mOut.writePointer((uintptr_t)refs);
1040 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001042
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 case BR_RELEASE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001044 refs = (RefBase::weakref_type*)mIn.readPointer();
1045 obj = (BBinder*)mIn.readPointer();
Steve Block67263472012-01-09 18:35:44 +00001046 ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1048 refs, obj, refs->refBase());
1049 IF_LOG_REMOTEREFS() {
1050 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1051 obj->printRefs();
1052 }
1053 mPendingStrongDerefs.push(obj);
1054 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001055
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 case BR_INCREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001057 refs = (RefBase::weakref_type*)mIn.readPointer();
1058 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 refs->incWeak(mProcess.get());
1060 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001061 mOut.writePointer((uintptr_t)refs);
1062 mOut.writePointer((uintptr_t)obj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 case BR_DECREFS:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001066 refs = (RefBase::weakref_type*)mIn.readPointer();
1067 obj = (BBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 // NOTE: This assertion is not valid, because the object may no
1069 // longer exist (thus the (BBinder*)cast above resulting in a different
1070 // memory address).
Steve Block67263472012-01-09 18:35:44 +00001071 //ALOG_ASSERT(refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1073 // refs, obj, refs->refBase());
1074 mPendingWeakDerefs.push(refs);
1075 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescuf683e012013-11-05 16:53:55 +00001078 refs = (RefBase::weakref_type*)mIn.readPointer();
1079 obj = (BBinder*)mIn.readPointer();
Tim Murrayd429f4a2017-03-07 09:31:09 -08001080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081 {
1082 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Block67263472012-01-09 18:35:44 +00001083 ALOG_ASSERT(success && refs->refBase() == obj,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1085 refs, obj, refs->refBase());
Tim Murrayd429f4a2017-03-07 09:31:09 -08001086
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 mOut.writeInt32(BC_ACQUIRE_RESULT);
1088 mOut.writeInt32((int32_t)success);
1089 }
1090 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 case BR_TRANSACTION:
1093 {
1094 binder_transaction_data tr;
1095 result = mIn.read(&tr, sizeof(tr));
Steve Block67263472012-01-09 18:35:44 +00001096 ALOG_ASSERT(result == NO_ERROR,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 "Not enough command data for brTRANSACTION");
1098 if (result != NO_ERROR) break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001099
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001100 //Record the fact that we're in a binder call.
1101 mIPCThreadStateBase->pushCurrentState(
1102 IPCThreadStateBase::CallState::BINDER);
Martijn Coenenea0090a2017-11-02 18:54:40 +00001103 Parcel buffer;
1104 buffer.ipcSetDataReference(
1105 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1106 tr.data_size,
1107 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1108 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1109
1110 const pid_t origPid = mCallingPid;
1111 const uid_t origUid = mCallingUid;
1112 const int32_t origStrictModePolicy = mStrictModePolicy;
1113 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1114
1115 mCallingPid = tr.sender_pid;
1116 mCallingUid = tr.sender_euid;
1117 mLastTransactionBinderFlags = tr.flags;
1118
1119 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
1120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 Parcel reply;
Martijn Coenenea0090a2017-11-02 18:54:40 +00001122 status_t error;
1123 IF_LOG_TRANSACTIONS() {
1124 TextOutput::Bundle _b(alog);
1125 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1126 << " / obj " << tr.target.ptr << " / code "
1127 << TypeCode(tr.code) << ": " << indent << buffer
1128 << dedent << endl
1129 << "Data addr = "
1130 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1131 << ", offsets addr="
1132 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1133 }
1134 if (tr.target.ptr) {
1135 // We only have a weak reference on the target object, so we must first try to
1136 // safely acquire a strong reference before doing anything else with it.
1137 if (reinterpret_cast<RefBase::weakref_type*>(
1138 tr.target.ptr)->attemptIncStrong(this)) {
1139 error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer,
1140 &reply, tr.flags);
1141 reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this);
Dianne Hackbornc1114612016-03-21 10:36:54 -07001142 } else {
Martijn Coenenea0090a2017-11-02 18:54:40 +00001143 error = UNKNOWN_TRANSACTION;
Dianne Hackbornc1114612016-03-21 10:36:54 -07001144 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -07001145
Martijn Coenenea0090a2017-11-02 18:54:40 +00001146 } else {
1147 error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 }
Dianne Hackborn5ee2c9d2014-09-30 11:30:03 -07001149
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001150 mIPCThreadStateBase->popCurrentState();
Martijn Coenenea0090a2017-11-02 18:54:40 +00001151 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
1152 // mCallingPid, origPid, origUid);
Tim Murrayd429f4a2017-03-07 09:31:09 -08001153
Martijn Coenenea0090a2017-11-02 18:54:40 +00001154 if ((tr.flags & TF_ONE_WAY) == 0) {
1155 LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1156 if (error < NO_ERROR) reply.setError(error);
1157 sendReply(reply, 0);
1158 } else {
1159 LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1160 }
1161
1162 mCallingPid = origPid;
1163 mCallingUid = origUid;
1164 mStrictModePolicy = origStrictModePolicy;
1165 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate440fd872010-03-18 17:55:03 -07001166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 IF_LOG_TRANSACTIONS() {
1168 TextOutput::Bundle _b(alog);
1169 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1170 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1171 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 }
1174 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176 case BR_DEAD_BINDER:
1177 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001178 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179 proxy->sendObituary();
1180 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001181 mOut.writePointer((uintptr_t)proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1185 {
Serban Constantinescuf683e012013-11-05 16:53:55 +00001186 BpBinder *proxy = (BpBinder*)mIn.readPointer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 proxy->getWeakRefs()->decWeak(proxy);
1188 } break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 case BR_FINISHED:
1191 result = TIMED_OUT;
1192 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 case BR_NOOP:
1195 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197 case BR_SPAWN_LOOPER:
1198 mProcess->spawnPooledThread(false);
1199 break;
Tim Murrayd429f4a2017-03-07 09:31:09 -08001200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 default:
liangweikanga43ee152016-10-25 16:37:54 +08001202 ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 result = UNKNOWN_ERROR;
1204 break;
1205 }
1206
1207 if (result != NO_ERROR) {
1208 mLastError = result;
1209 }
Tim Murrayd429f4a2017-03-07 09:31:09 -08001210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 return result;
1212}
1213
Jayant Chowdharydac6dc82018-10-01 22:52:44 +00001214bool IPCThreadState::isServingCall() const {
1215 return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER;
1216}
1217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218void IPCThreadState::threadDestructor(void *st)
1219{
Todd Poynor8d96cab2013-06-25 19:12:18 -07001220 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1221 if (self) {
1222 self->flushCommands();
Elliott Hughes6071da72015-08-12 15:27:47 -07001223#if defined(__ANDROID__)
Johannes Carlssondb1597a2011-02-17 14:06:53 +01001224 if (self->mProcess->mDriverFD > 0) {
1225 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1226 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227#endif
Todd Poynor8d96cab2013-06-25 19:12:18 -07001228 delete self;
1229 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230}
1231
1232
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001233void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1234 size_t /*dataSize*/,
1235 const binder_size_t* /*objects*/,
1236 size_t /*objectsSize*/, void* /*cookie*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237{
Steve Blocka19954a2012-01-04 20:05:49 +00001238 //ALOGI("Freeing parcel %p", &parcel);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 IF_LOG_COMMANDS() {
1240 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1241 }
Steve Block67263472012-01-09 18:35:44 +00001242 ALOG_ASSERT(data != NULL, "Called with NULL data");
Yi Kongfdd8da92018-06-07 17:52:27 -07001243 if (parcel != nullptr) parcel->closeFileDescriptors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 IPCThreadState* state = self();
1245 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001246 state->mOut.writePointer((uintptr_t)data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247}
1248
1249}; // namespace android