blob: f6536f6f81178787670e68b1a5f1425832732dff [file] [log] [blame]
Mathias Agopian7922fa22009-05-18 15:08:03 -07001/*
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
Steven Morelandc4dd2102017-02-23 13:57:21 -080017#define LOG_TAG "hw-IPCThreadState"
Jason Parks2b17f142009-11-03 12:14:38 -080018
Martijn Coenen4080edc2016-05-04 14:17:02 +020019#include <hwbinder/IPCThreadState.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070020
Martijn Coenen4080edc2016-05-04 14:17:02 +020021#include <hwbinder/Binder.h>
Yifan Hong1e118d22017-01-12 14:42:28 -080022#include <hwbinder/BpHwBinder.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020023#include <hwbinder/TextOutput.h>
Chia-I Wu0e72fd52016-10-06 14:20:41 +080024#include <hwbinder/binder_kernel.h>
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070025
Steven Morelandd7bbfdb2018-05-01 16:30:46 -070026#include <android-base/macros.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070027#include <utils/Log.h>
Colin Crossb1dc6542016-04-15 14:29:55 -070028#include <utils/SystemClock.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070029#include <utils/threads.h>
30
31#include <private/binder/binder_module.h>
Martijn Coenene01f4f22016-05-12 12:33:28 +020032#include <hwbinder/Static.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070033
Mathias Agopian7922fa22009-05-18 15:08:03 -070034#include <errno.h>
Colin Crossb1dc6542016-04-15 14:29:55 -070035#include <inttypes.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070036#include <pthread.h>
37#include <sched.h>
Yabin Cuibbef2ba2015-01-26 19:45:47 -080038#include <signal.h>
39#include <stdio.h>
40#include <sys/ioctl.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070041#include <sys/resource.h>
Yabin Cuibbef2ba2015-01-26 19:45:47 -080042#include <unistd.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070043
44#if LOG_NDEBUG
45
46#define IF_LOG_TRANSACTIONS() if (false)
47#define IF_LOG_COMMANDS() if (false)
Yifan Hongdde40f32017-01-12 14:22:45 -080048#define LOG_REMOTEREFS(...)
Mathias Agopian7922fa22009-05-18 15:08:03 -070049#define IF_LOG_REMOTEREFS() if (false)
Yifan Hongdde40f32017-01-12 14:22:45 -080050#define LOG_THREADPOOL(...)
51#define LOG_ONEWAY(...)
Mathias Agopian7922fa22009-05-18 15:08:03 -070052
53#else
54
Steve Block5854b912011-10-12 17:27:03 +010055#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
56#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
57#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
58#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
59#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
60#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
Mathias Agopian7922fa22009-05-18 15:08:03 -070061
62#endif
63
64// ---------------------------------------------------------------------------
65
66namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020067namespace hardware {
Mathias Agopian7922fa22009-05-18 15:08:03 -070068
Chih-Hung Hsieh30dcad72014-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.
Mathias Agopian7922fa22009-05-18 15:08:03 -070071static const char *kReturnStrings[] = {
Andy McFadden457d51f2011-08-31 07:43:40 -070072 "BR_ERROR",
Mathias Agopian7922fa22009-05-18 15:08:03 -070073 "BR_OK",
Mathias Agopian7922fa22009-05-18 15:08:03 -070074 "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",
Mathias Agopian7922fa22009-05-18 15:08:03 -070084 "BR_NOOP",
85 "BR_SPAWN_LOOPER",
86 "BR_FINISHED",
87 "BR_DEAD_BINDER",
Andy McFadden457d51f2011-08-31 07:43:40 -070088 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
89 "BR_FAILED_REPLY"
Mathias Agopian7922fa22009-05-18 15:08:03 -070090};
91
92static const char *kCommandStrings[] = {
Mathias Agopian7922fa22009-05-18 15:08:03 -070093 "BC_TRANSACTION",
94 "BC_REPLY",
95 "BC_ACQUIRE_RESULT",
96 "BC_FREE_BUFFER",
Mathias Agopian7922fa22009-05-18 15:08:03 -070097 "BC_INCREFS",
98 "BC_ACQUIRE",
99 "BC_RELEASE",
100 "BC_DECREFS",
101 "BC_INCREFS_DONE",
102 "BC_ACQUIRE_DONE",
103 "BC_ATTEMPT_ACQUIRE",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700104 "BC_REGISTER_LOOPER",
105 "BC_ENTER_LOOPER",
106 "BC_EXIT_LOOPER",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700107 "BC_REQUEST_DEATH_NOTIFICATION",
108 "BC_CLEAR_DEATH_NOTIFICATION",
109 "BC_DEAD_BINDER_DONE"
Mathias Agopian7922fa22009-05-18 15:08:03 -0700110};
111
112static const char* getReturnString(size_t idx)
113{
114 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
115 return kReturnStrings[idx];
116 else
117 return "unknown";
118}
119
Mathias Agopian7922fa22009-05-18 15:08:03 -0700120static const void* printBinderTransactionData(TextOutput& out, const void* data)
121{
122 const binder_transaction_data* btd =
123 (const binder_transaction_data*)data;
Andy McFadden457d51f2011-08-31 07:43:40 -0700124 if (btd->target.handle < 1024) {
125 /* want to print descriptors in decimal; guess based on value */
126 out << "target.desc=" << btd->target.handle;
127 } else {
128 out << "target.ptr=" << btd->target.ptr;
129 }
130 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700131 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
Mathias Agopian7922fa22009-05-18 15:08:03 -0700132 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
133 << " bytes)" << endl
134 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFadden457d51f2011-08-31 07:43:40 -0700135 << " bytes)";
Mathias Agopian7922fa22009-05-18 15:08:03 -0700136 return btd+1;
137}
138
139static const void* printReturnCommand(TextOutput& out, const void* _cmd)
140{
Andy McFadden457d51f2011-08-31 07:43:40 -0700141 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700142 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100143 uint32_t code = (uint32_t)*cmd++;
Andy McFadden457d51f2011-08-31 07:43:40 -0700144 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100145 if (code == BR_ERROR) {
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700146 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700147 return cmd;
Andy McFadden457d51f2011-08-31 07:43:40 -0700148 } else if (cmdIndex >= N) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700149 out << "Unknown reply: " << code << endl;
150 return cmd;
151 }
Andy McFadden457d51f2011-08-31 07:43:40 -0700152 out << kReturnStrings[cmdIndex];
Yifan Hongdde40f32017-01-12 14:22:45 -0800153
Mathias Agopian7922fa22009-05-18 15:08:03 -0700154 switch (code) {
155 case BR_TRANSACTION:
156 case BR_REPLY: {
157 out << ": " << indent;
158 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
159 out << dedent;
160 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800161
Mathias Agopian7922fa22009-05-18 15:08:03 -0700162 case BR_ACQUIRE_RESULT: {
163 const int32_t res = *cmd++;
164 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
165 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800166
Mathias Agopian7922fa22009-05-18 15:08:03 -0700167 case BR_INCREFS:
168 case BR_ACQUIRE:
169 case BR_RELEASE:
170 case BR_DECREFS: {
171 const int32_t b = *cmd++;
172 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700173 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
Mathias Agopian7922fa22009-05-18 15:08:03 -0700174 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800175
Mathias Agopian7922fa22009-05-18 15:08:03 -0700176 case BR_ATTEMPT_ACQUIRE: {
177 const int32_t p = *cmd++;
178 const int32_t b = *cmd++;
179 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700180 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
Mathias Agopian7922fa22009-05-18 15:08:03 -0700181 << "), pri=" << p;
182 } break;
183
184 case BR_DEAD_BINDER:
185 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
186 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700187 out << ": death cookie " << (void*)(long)c;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700188 } break;
Andy McFadden457d51f2011-08-31 07:43:40 -0700189
190 default:
191 // no details to show for: BR_OK, BR_DEAD_REPLY,
192 // BR_TRANSACTION_COMPLETE, BR_FINISHED
193 break;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700194 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800195
Mathias Agopian7922fa22009-05-18 15:08:03 -0700196 out << endl;
197 return cmd;
198}
199
200static const void* printCommand(TextOutput& out, const void* _cmd)
201{
Andy McFadden457d51f2011-08-31 07:43:40 -0700202 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700203 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100204 uint32_t code = (uint32_t)*cmd++;
Andy McFadden457d51f2011-08-31 07:43:40 -0700205 size_t cmdIndex = code & 0xff;
206
207 if (cmdIndex >= N) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700208 out << "Unknown command: " << code << endl;
209 return cmd;
210 }
Andy McFadden457d51f2011-08-31 07:43:40 -0700211 out << kCommandStrings[cmdIndex];
212
Mathias Agopian7922fa22009-05-18 15:08:03 -0700213 switch (code) {
214 case BC_TRANSACTION:
215 case BC_REPLY: {
216 out << ": " << indent;
217 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
218 out << dedent;
219 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800220
Mathias Agopian7922fa22009-05-18 15:08:03 -0700221 case BC_ACQUIRE_RESULT: {
222 const int32_t res = *cmd++;
223 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
224 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800225
Mathias Agopian7922fa22009-05-18 15:08:03 -0700226 case BC_FREE_BUFFER: {
227 const int32_t buf = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700228 out << ": buffer=" << (void*)(long)buf;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700229 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800230
Mathias Agopian7922fa22009-05-18 15:08:03 -0700231 case BC_INCREFS:
232 case BC_ACQUIRE:
233 case BC_RELEASE:
234 case BC_DECREFS: {
235 const int32_t d = *cmd++;
Andy McFadden457d51f2011-08-31 07:43:40 -0700236 out << ": desc=" << d;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700237 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800238
Mathias Agopian7922fa22009-05-18 15:08:03 -0700239 case BC_INCREFS_DONE:
240 case BC_ACQUIRE_DONE: {
241 const int32_t b = *cmd++;
242 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700243 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
Mathias Agopian7922fa22009-05-18 15:08:03 -0700244 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800245
Mathias Agopian7922fa22009-05-18 15:08:03 -0700246 case BC_ATTEMPT_ACQUIRE: {
247 const int32_t p = *cmd++;
248 const int32_t d = *cmd++;
Andy McFadden457d51f2011-08-31 07:43:40 -0700249 out << ": desc=" << d << ", pri=" << p;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700250 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800251
Mathias Agopian7922fa22009-05-18 15:08:03 -0700252 case BC_REQUEST_DEATH_NOTIFICATION:
253 case BC_CLEAR_DEATH_NOTIFICATION: {
254 const int32_t h = *cmd++;
255 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700256 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
Mathias Agopian7922fa22009-05-18 15:08:03 -0700257 } break;
258
259 case BC_DEAD_BINDER_DONE: {
260 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700261 out << ": death cookie " << (void*)(long)c;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700262 } break;
Andy McFadden457d51f2011-08-31 07:43:40 -0700263
264 default:
265 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
266 // BC_EXIT_LOOPER
267 break;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700268 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800269
Mathias Agopian7922fa22009-05-18 15:08:03 -0700270 out << endl;
271 return cmd;
272}
Mathias Agopian7922fa22009-05-18 15:08:03 -0700273
274static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
275static bool gHaveTLS = false;
276static pthread_key_t gTLS = 0;
277static bool gShutdown = false;
278
279IPCThreadState* IPCThreadState::self()
280{
281 if (gHaveTLS) {
282restart:
283 const pthread_key_t k = gTLS;
284 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
285 if (st) return st;
286 return new IPCThreadState;
287 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800288
Andreas Gampe1d5dc2b2016-02-01 13:21:56 -0800289 if (gShutdown) {
290 ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n");
291 return NULL;
292 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800293
Mathias Agopian7922fa22009-05-18 15:08:03 -0700294 pthread_mutex_lock(&gTLSMutex);
295 if (!gHaveTLS) {
Andreas Gampe1d5dc2b2016-02-01 13:21:56 -0800296 int key_create_value = pthread_key_create(&gTLS, threadDestructor);
297 if (key_create_value != 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700298 pthread_mutex_unlock(&gTLSMutex);
Andreas Gampe1d5dc2b2016-02-01 13:21:56 -0800299 ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n",
300 strerror(key_create_value));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700301 return NULL;
302 }
303 gHaveTLS = true;
304 }
305 pthread_mutex_unlock(&gTLSMutex);
306 goto restart;
307}
308
Brad Fitzpatrick77949942010-12-13 16:52:35 -0800309IPCThreadState* IPCThreadState::selfOrNull()
310{
311 if (gHaveTLS) {
312 const pthread_key_t k = gTLS;
313 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
314 return st;
315 }
316 return NULL;
317}
318
Mathias Agopian7922fa22009-05-18 15:08:03 -0700319void IPCThreadState::shutdown()
320{
321 gShutdown = true;
Yifan Hongdde40f32017-01-12 14:22:45 -0800322
Mathias Agopian7922fa22009-05-18 15:08:03 -0700323 if (gHaveTLS) {
324 // XXX Need to wait for all thread pool threads to exit!
325 IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
326 if (st) {
327 delete st;
328 pthread_setspecific(gTLS, NULL);
329 }
zhongjie8e8a0252016-03-09 15:05:04 +0800330 pthread_key_delete(gTLS);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700331 gHaveTLS = false;
332 }
333}
334
Steven Moreland8c3f4bd2018-05-07 15:45:23 -0700335// TODO(b/66905301): remove symbol
336void IPCThreadState::disableBackgroundScheduling(bool /* disable */) {}
Dianne Hackborn5f4d7e82009-12-07 17:59:37 -0800337
Mathias Agopian7922fa22009-05-18 15:08:03 -0700338sp<ProcessState> IPCThreadState::process()
339{
340 return mProcess;
341}
342
343status_t IPCThreadState::clearLastError()
344{
345 const status_t err = mLastError;
346 mLastError = NO_ERROR;
347 return err;
348}
349
Dan Stozae8da8a42014-11-26 12:23:23 -0800350pid_t IPCThreadState::getCallingPid() const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700351{
352 return mCallingPid;
353}
354
Dan Stozae8da8a42014-11-26 12:23:23 -0800355uid_t IPCThreadState::getCallingUid() const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700356{
357 return mCallingUid;
358}
359
360int64_t IPCThreadState::clearCallingIdentity()
361{
362 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
363 clearCaller();
364 return token;
365}
366
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700367void IPCThreadState::setStrictModePolicy(int32_t policy)
368{
369 mStrictModePolicy = policy;
370}
371
Brad Fitzpatrick3f4ef592010-07-07 16:06:39 -0700372int32_t IPCThreadState::getStrictModePolicy() const
373{
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700374 return mStrictModePolicy;
375}
376
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -0700377void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
378{
379 mLastTransactionBinderFlags = flags;
380}
381
382int32_t IPCThreadState::getLastTransactionBinderFlags() const
383{
384 return mLastTransactionBinderFlags;
385}
386
Mathias Agopian7922fa22009-05-18 15:08:03 -0700387void IPCThreadState::restoreCallingIdentity(int64_t token)
388{
389 mCallingUid = (int)(token>>32);
390 mCallingPid = (int)token;
391}
392
393void IPCThreadState::clearCaller()
394{
Marco Nelissenb4f35d02009-07-17 07:59:17 -0700395 mCallingPid = getpid();
396 mCallingUid = getuid();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700397}
398
399void IPCThreadState::flushCommands()
400{
401 if (mProcess->mDriverFD <= 0)
402 return;
403 talkWithDriver(false);
404}
405
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700406void IPCThreadState::blockUntilThreadAvailable()
407{
408 pthread_mutex_lock(&mProcess->mThreadCountLock);
409 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwale319900a2015-04-21 12:29:50 -0700410 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
411 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
412 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700413 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
414 }
415 pthread_mutex_unlock(&mProcess->mThreadCountLock);
416}
417
Todd Poynor0646cb02013-06-25 19:12:18 -0700418status_t IPCThreadState::getAndExecuteCommand()
419{
420 status_t result;
421 int32_t cmd;
422
423 result = talkWithDriver();
424 if (result >= NO_ERROR) {
425 size_t IN = mIn.dataAvail();
426 if (IN < sizeof(int32_t)) return result;
427 cmd = mIn.readInt32();
428 IF_LOG_COMMANDS() {
429 alog << "Processing top-level Command: "
430 << getReturnString(cmd) << endl;
431 }
432
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700433 pthread_mutex_lock(&mProcess->mThreadCountLock);
434 mProcess->mExecutingThreadsCount++;
Colin Crossb1dc6542016-04-15 14:29:55 -0700435 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
Martijn Coenen0ce07ce2017-07-14 15:37:25 +0200436 mProcess->mMaxThreads > 1 && mProcess->mStarvationStartTimeMs == 0) {
Colin Crossb1dc6542016-04-15 14:29:55 -0700437 mProcess->mStarvationStartTimeMs = uptimeMillis();
438 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700439 pthread_mutex_unlock(&mProcess->mThreadCountLock);
440
Todd Poynor0646cb02013-06-25 19:12:18 -0700441 result = executeCommand(cmd);
442
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700443 pthread_mutex_lock(&mProcess->mThreadCountLock);
444 mProcess->mExecutingThreadsCount--;
Martijn Coenen0ce07ce2017-07-14 15:37:25 +0200445 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads && mProcess->mMaxThreads > 1 &&
446 mProcess->mStarvationStartTimeMs != 0) {
Colin Crossb1dc6542016-04-15 14:29:55 -0700447 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
448 if (starvationTimeMs > 100) {
Martijn Coenen0ce07ce2017-07-14 15:37:25 +0200449 ALOGW("All binder threads in pool (%zu threads) busy for %" PRId64 " ms",
Colin Crossb1dc6542016-04-15 14:29:55 -0700450 mProcess->mMaxThreads, starvationTimeMs);
451 }
452 mProcess->mStarvationStartTimeMs = 0;
453 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700454 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
455 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor0646cb02013-06-25 19:12:18 -0700456 }
457
Steven Morelandd7bbfdb2018-05-01 16:30:46 -0700458 if (UNLIKELY(!mPostCommandTasks.empty())) {
459 // make a copy in case the post transaction task makes a binder
460 // call and that other process calls back into us
461 std::vector<std::function<void(void)>> tasks = mPostCommandTasks;
462 mPostCommandTasks.clear();
463 for (auto func : tasks) {
464 func();
465 }
466 }
467
Todd Poynor0646cb02013-06-25 19:12:18 -0700468 return result;
469}
470
471// When we've cleared the incoming command queue, process any pending derefs
472void IPCThreadState::processPendingDerefs()
473{
474 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenenc9f105b2017-08-08 15:36:16 +0200475 /*
476 * The decWeak()/decStrong() calls may cause a destructor to run,
477 * which in turn could have initiated an outgoing transaction,
478 * which in turn could cause us to add to the pending refs
479 * vectors; so instead of simply iterating, loop until they're empty.
480 *
481 * We do this in an outer loop, because calling decStrong()
482 * may result in something being added to mPendingWeakDerefs,
483 * which could be delayed until the next incoming command
484 * from the driver if we don't process it now.
485 */
486 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
487 while (mPendingWeakDerefs.size() > 0) {
488 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
489 mPendingWeakDerefs.removeAt(0);
Todd Poynor0646cb02013-06-25 19:12:18 -0700490 refs->decWeak(mProcess.get());
491 }
Todd Poynor0646cb02013-06-25 19:12:18 -0700492
Martijn Coenenc9f105b2017-08-08 15:36:16 +0200493 if (mPendingStrongDerefs.size() > 0) {
494 // We don't use while() here because we don't want to re-order
495 // strong and weak decs at all; if this decStrong() causes both a
496 // decWeak() and a decStrong() to be queued, we want to process
497 // the decWeak() first.
498 BHwBinder* obj = mPendingStrongDerefs[0];
499 mPendingStrongDerefs.removeAt(0);
Todd Poynor0646cb02013-06-25 19:12:18 -0700500 obj->decStrong(mProcess.get());
501 }
Todd Poynor0646cb02013-06-25 19:12:18 -0700502 }
503 }
504}
505
Mathias Agopian7922fa22009-05-18 15:08:03 -0700506void IPCThreadState::joinThreadPool(bool isMain)
507{
508 LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
509
510 mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
Yifan Hongdde40f32017-01-12 14:22:45 -0800511
Mathias Agopian7922fa22009-05-18 15:08:03 -0700512 status_t result;
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200513 mIsLooper = true;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700514 do {
Todd Poynor0646cb02013-06-25 19:12:18 -0700515 processPendingDerefs();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700516 // now get the next command to be processed, waiting if necessary
Todd Poynor0646cb02013-06-25 19:12:18 -0700517 result = getAndExecuteCommand();
Jason Parks2b17f142009-11-03 12:14:38 -0800518
Todd Poynor0646cb02013-06-25 19:12:18 -0700519 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
520 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeree711ec2013-06-11 11:30:21 -0700521 mProcess->mDriverFD, result);
522 abort();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700523 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800524
Mathias Agopian7922fa22009-05-18 15:08:03 -0700525 // Let this thread exit the thread pool if it is no longer
526 // needed and it is not the main process thread.
527 if(result == TIMED_OUT && !isMain) {
528 break;
529 }
530 } while (result != -ECONNREFUSED && result != -EBADF);
531
Wei Wang8a2e8ac2016-10-14 09:54:27 -0700532 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
533 (void*)pthread_self(), getpid(), result);
Yifan Hongdde40f32017-01-12 14:22:45 -0800534
Mathias Agopian7922fa22009-05-18 15:08:03 -0700535 mOut.writeInt32(BC_EXIT_LOOPER);
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200536 mIsLooper = false;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700537 talkWithDriver(false);
538}
539
Todd Poynor0646cb02013-06-25 19:12:18 -0700540int IPCThreadState::setupPolling(int* fd)
541{
542 if (mProcess->mDriverFD <= 0) {
543 return -EBADF;
544 }
545
Martijn Coenen80b88ab2017-09-25 14:50:05 +0200546 // Tells the kernel to not spawn any additional binder threads,
547 // as that won't work with polling. Also, the caller is responsible
548 // for subsequently calling handlePolledCommands()
549 mProcess->setThreadPoolConfiguration(1, true /* callerWillJoin */);
Tobias Lindskoga36d5762018-01-05 10:28:31 +0100550 mIsPollingThread = true;
Martijn Coenen80b88ab2017-09-25 14:50:05 +0200551
Todd Poynor0646cb02013-06-25 19:12:18 -0700552 mOut.writeInt32(BC_ENTER_LOOPER);
553 *fd = mProcess->mDriverFD;
554 return 0;
555}
556
557status_t IPCThreadState::handlePolledCommands()
558{
559 status_t result;
560
561 do {
562 result = getAndExecuteCommand();
563 } while (mIn.dataPosition() < mIn.dataSize());
564
565 processPendingDerefs();
566 flushCommands();
567 return result;
568}
569
Colin Crossf0487982014-02-05 17:42:44 -0800570void IPCThreadState::stopProcess(bool /*immediate*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700571{
Steve Block93cf8542012-01-04 20:05:49 +0000572 //ALOGI("**** STOPPING PROCESS");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700573 flushCommands();
574 int fd = mProcess->mDriverFD;
575 mProcess->mDriverFD = -1;
576 close(fd);
577 //kill(getpid(), SIGKILL);
578}
579
580status_t IPCThreadState::transact(int32_t handle,
581 uint32_t code, const Parcel& data,
582 Parcel* reply, uint32_t flags)
583{
Ganesh Mahendrance45b892017-10-11 18:05:13 +0800584 status_t err;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700585
586 flags |= TF_ACCEPT_FDS;
587
588 IF_LOG_TRANSACTIONS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700589 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
590 << handle << " / code " << TypeCode(code) << ": "
591 << indent << data << dedent << endl;
592 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800593
Ganesh Mahendrance45b892017-10-11 18:05:13 +0800594 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
595 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
596 err = writeTransactionData(BC_TRANSACTION_SG, flags, handle, code, data, NULL);
Yifan Hongdde40f32017-01-12 14:22:45 -0800597
Mathias Agopian7922fa22009-05-18 15:08:03 -0700598 if (err != NO_ERROR) {
599 if (reply) reply->setError(err);
600 return (mLastError = err);
601 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800602
Mathias Agopian7922fa22009-05-18 15:08:03 -0700603 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn98878262010-09-24 11:16:23 -0700604 #if 0
605 if (code == 4) { // relayout
Steve Block93cf8542012-01-04 20:05:49 +0000606 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn98878262010-09-24 11:16:23 -0700607 } else {
Steve Block93cf8542012-01-04 20:05:49 +0000608 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn98878262010-09-24 11:16:23 -0700609 }
610 #endif
Mathias Agopian7922fa22009-05-18 15:08:03 -0700611 if (reply) {
612 err = waitForResponse(reply);
613 } else {
614 Parcel fakeReply;
615 err = waitForResponse(&fakeReply);
616 }
Dianne Hackborn98878262010-09-24 11:16:23 -0700617 #if 0
618 if (code == 4) { // relayout
Steve Block93cf8542012-01-04 20:05:49 +0000619 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn98878262010-09-24 11:16:23 -0700620 } else {
Steve Block93cf8542012-01-04 20:05:49 +0000621 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn98878262010-09-24 11:16:23 -0700622 }
623 #endif
Yifan Hongdde40f32017-01-12 14:22:45 -0800624
Mathias Agopian7922fa22009-05-18 15:08:03 -0700625 IF_LOG_TRANSACTIONS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700626 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
627 << handle << ": ";
628 if (reply) alog << indent << *reply << dedent << endl;
629 else alog << "(none requested)" << endl;
630 }
631 } else {
632 err = waitForResponse(NULL, NULL);
633 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800634
Mathias Agopian7922fa22009-05-18 15:08:03 -0700635 return err;
636}
637
638void IPCThreadState::incStrongHandle(int32_t handle)
639{
640 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
641 mOut.writeInt32(BC_ACQUIRE);
642 mOut.writeInt32(handle);
643}
644
645void IPCThreadState::decStrongHandle(int32_t handle)
646{
647 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
648 mOut.writeInt32(BC_RELEASE);
649 mOut.writeInt32(handle);
650}
651
652void IPCThreadState::incWeakHandle(int32_t handle)
653{
654 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
655 mOut.writeInt32(BC_INCREFS);
656 mOut.writeInt32(handle);
657}
658
659void IPCThreadState::decWeakHandle(int32_t handle)
660{
661 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
662 mOut.writeInt32(BC_DECREFS);
663 mOut.writeInt32(handle);
664}
665
666status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
667{
Arve Hjønnevåg304dcae2014-02-14 20:14:02 -0800668#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFadden457d51f2011-08-31 07:43:40 -0700669 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700670 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
671 mOut.writeInt32(0); // xxx was thread priority
672 mOut.writeInt32(handle);
673 status_t result = UNKNOWN_ERROR;
Yifan Hongdde40f32017-01-12 14:22:45 -0800674
Mathias Agopian7922fa22009-05-18 15:08:03 -0700675 waitForResponse(NULL, &result);
Yifan Hongdde40f32017-01-12 14:22:45 -0800676
Mathias Agopian7922fa22009-05-18 15:08:03 -0700677#if LOG_REFCOUNTS
678 printf("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
679 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
680#endif
Yifan Hongdde40f32017-01-12 14:22:45 -0800681
Mathias Agopian7922fa22009-05-18 15:08:03 -0700682 return result;
Arve Hjønnevåg304dcae2014-02-14 20:14:02 -0800683#else
684 (void)handle;
685 ALOGE("%s(%d): Not supported\n", __func__, handle);
686 return INVALID_OPERATION;
687#endif
Mathias Agopian7922fa22009-05-18 15:08:03 -0700688}
689
690void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
691{
692#if LOG_REFCOUNTS
693 printf("IPCThreadState::expungeHandle(%ld)\n", handle);
694#endif
Yunlian Jiang6b65ac32017-09-19 15:51:07 -0700695 self()->mProcess->expungeHandle(handle, binder); // NOLINT
Mathias Agopian7922fa22009-05-18 15:08:03 -0700696}
697
Yifan Hong1e118d22017-01-12 14:42:28 -0800698status_t IPCThreadState::requestDeathNotification(int32_t handle, BpHwBinder* proxy)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700699{
700 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
701 mOut.writeInt32((int32_t)handle);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +0000702 mOut.writePointer((uintptr_t)proxy);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700703 return NO_ERROR;
704}
705
Yifan Hong1e118d22017-01-12 14:42:28 -0800706status_t IPCThreadState::clearDeathNotification(int32_t handle, BpHwBinder* proxy)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700707{
708 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
709 mOut.writeInt32((int32_t)handle);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +0000710 mOut.writePointer((uintptr_t)proxy);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700711 return NO_ERROR;
712}
713
714IPCThreadState::IPCThreadState()
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -0700715 : mProcess(ProcessState::self()),
Elliott Hughes07cf48a2014-08-18 10:38:38 -0700716 mMyThreadId(gettid()),
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -0700717 mStrictModePolicy(0),
Martijn Coenen9bd3d3b2017-12-12 09:29:14 +0100718 mLastTransactionBinderFlags(0),
Tobias Lindskoga36d5762018-01-05 10:28:31 +0100719 mIsLooper(false),
720 mIsPollingThread(false) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700721 pthread_setspecific(gTLS, this);
Dianne Hackborn5f4d7e82009-12-07 17:59:37 -0800722 clearCaller();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700723 mIn.setDataCapacity(256);
724 mOut.setDataCapacity(256);
Chih-Hung Hsieh29dbdcf2017-10-02 10:42:37 -0700725
726 // TODO(b/67742352): remove this variable from the class
727 (void)mMyThreadId;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700728}
729
730IPCThreadState::~IPCThreadState()
731{
732}
733
734status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
735{
736 status_t err;
737 status_t statusBuffer;
Martijn Coenend39a1682016-06-03 21:27:28 +0200738 err = writeTransactionData(BC_REPLY_SG, flags, -1, 0, reply, &statusBuffer);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700739 if (err < NO_ERROR) return err;
Yifan Hongdde40f32017-01-12 14:22:45 -0800740
Mathias Agopian7922fa22009-05-18 15:08:03 -0700741 return waitForResponse(NULL, NULL);
742}
743
744status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
745{
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100746 uint32_t cmd;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700747 int32_t err;
748
749 while (1) {
750 if ((err=talkWithDriver()) < NO_ERROR) break;
751 err = mIn.errorCheck();
752 if (err < NO_ERROR) break;
753 if (mIn.dataAvail() == 0) continue;
Yifan Hongdde40f32017-01-12 14:22:45 -0800754
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100755 cmd = (uint32_t)mIn.readInt32();
Yifan Hongdde40f32017-01-12 14:22:45 -0800756
Mathias Agopian7922fa22009-05-18 15:08:03 -0700757 IF_LOG_COMMANDS() {
758 alog << "Processing waitForResponse Command: "
759 << getReturnString(cmd) << endl;
760 }
761
762 switch (cmd) {
763 case BR_TRANSACTION_COMPLETE:
764 if (!reply && !acquireResult) goto finish;
765 break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800766
Mathias Agopian7922fa22009-05-18 15:08:03 -0700767 case BR_DEAD_REPLY:
768 err = DEAD_OBJECT;
769 goto finish;
770
771 case BR_FAILED_REPLY:
772 err = FAILED_TRANSACTION;
773 goto finish;
Yifan Hongdde40f32017-01-12 14:22:45 -0800774
Mathias Agopian7922fa22009-05-18 15:08:03 -0700775 case BR_ACQUIRE_RESULT:
776 {
Steve Blockd0bfabc2012-01-09 18:35:44 +0000777 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700778 const int32_t result = mIn.readInt32();
779 if (!acquireResult) continue;
780 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
781 }
782 goto finish;
Yifan Hongdde40f32017-01-12 14:22:45 -0800783
Mathias Agopian7922fa22009-05-18 15:08:03 -0700784 case BR_REPLY:
785 {
786 binder_transaction_data tr;
787 err = mIn.read(&tr, sizeof(tr));
Steve Blockd0bfabc2012-01-09 18:35:44 +0000788 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700789 if (err != NO_ERROR) goto finish;
790
791 if (reply) {
792 if ((tr.flags & TF_STATUS_CODE) == 0) {
793 reply->ipcSetDataReference(
794 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
795 tr.data_size,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800796 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
797 tr.offsets_size/sizeof(binder_size_t),
Mathias Agopian7922fa22009-05-18 15:08:03 -0700798 freeBuffer, this);
799 } else {
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800800 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700801 freeBuffer(NULL,
802 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
803 tr.data_size,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800804 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
805 tr.offsets_size/sizeof(binder_size_t), this);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700806 }
807 } else {
808 freeBuffer(NULL,
809 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
810 tr.data_size,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800811 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
812 tr.offsets_size/sizeof(binder_size_t), this);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700813 continue;
814 }
815 }
816 goto finish;
817
818 default:
819 err = executeCommand(cmd);
820 if (err != NO_ERROR) goto finish;
821 break;
822 }
823 }
824
825finish:
826 if (err != NO_ERROR) {
827 if (acquireResult) *acquireResult = err;
828 if (reply) reply->setError(err);
829 mLastError = err;
830 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800831
Mathias Agopian7922fa22009-05-18 15:08:03 -0700832 return err;
833}
834
835status_t IPCThreadState::talkWithDriver(bool doReceive)
836{
Johannes Carlsson597a3c72011-02-17 14:06:53 +0100837 if (mProcess->mDriverFD <= 0) {
838 return -EBADF;
839 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800840
Mathias Agopian7922fa22009-05-18 15:08:03 -0700841 binder_write_read bwr;
Yifan Hongdde40f32017-01-12 14:22:45 -0800842
Mathias Agopian7922fa22009-05-18 15:08:03 -0700843 // Is the read buffer empty?
844 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Yifan Hongdde40f32017-01-12 14:22:45 -0800845
Mathias Agopian7922fa22009-05-18 15:08:03 -0700846 // We don't want to write anything if we are still reading
847 // from data left in the input buffer and the caller
848 // has requested to read the next data.
849 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Yifan Hongdde40f32017-01-12 14:22:45 -0800850
Mathias Agopian7922fa22009-05-18 15:08:03 -0700851 bwr.write_size = outAvail;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800852 bwr.write_buffer = (uintptr_t)mOut.data();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700853
854 // This is what we'll read.
855 if (doReceive && needRead) {
856 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800857 bwr.read_buffer = (uintptr_t)mIn.data();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700858 } else {
859 bwr.read_size = 0;
Ben Cheng455a70a2011-12-01 17:11:32 -0800860 bwr.read_buffer = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700861 }
Andy McFadden457d51f2011-08-31 07:43:40 -0700862
Mathias Agopian7922fa22009-05-18 15:08:03 -0700863 IF_LOG_COMMANDS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700864 if (outAvail != 0) {
865 alog << "Sending commands to driver: " << indent;
866 const void* cmds = (const void*)bwr.write_buffer;
867 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
868 alog << HexDump(cmds, bwr.write_size) << endl;
869 while (cmds < end) cmds = printCommand(alog, cmds);
870 alog << dedent;
871 }
872 alog << "Size of receive buffer: " << bwr.read_size
873 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
874 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800875
Mathias Agopian7922fa22009-05-18 15:08:03 -0700876 // Return immediately if there is nothing to do.
877 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFadden457d51f2011-08-31 07:43:40 -0700878
Mathias Agopian7922fa22009-05-18 15:08:03 -0700879 bwr.write_consumed = 0;
880 bwr.read_consumed = 0;
881 status_t err;
882 do {
883 IF_LOG_COMMANDS() {
884 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
885 }
Elliott Hughese5e70552015-08-12 15:27:47 -0700886#if defined(__ANDROID__)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700887 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
888 err = NO_ERROR;
889 else
890 err = -errno;
891#else
892 err = INVALID_OPERATION;
893#endif
Johannes Carlsson597a3c72011-02-17 14:06:53 +0100894 if (mProcess->mDriverFD <= 0) {
895 err = -EBADF;
896 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700897 IF_LOG_COMMANDS() {
898 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
899 }
900 } while (err == -EINTR);
Andy McFadden457d51f2011-08-31 07:43:40 -0700901
Mathias Agopian7922fa22009-05-18 15:08:03 -0700902 IF_LOG_COMMANDS() {
Colin Crossf0487982014-02-05 17:42:44 -0800903 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
Mathias Agopian7922fa22009-05-18 15:08:03 -0700904 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor0646cb02013-06-25 19:12:18 -0700905 << "), read consumed: " << bwr.read_consumed << endl;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700906 }
907
908 if (err >= NO_ERROR) {
909 if (bwr.write_consumed > 0) {
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800910 if (bwr.write_consumed < mOut.dataSize())
Mathias Agopian7922fa22009-05-18 15:08:03 -0700911 mOut.remove(0, bwr.write_consumed);
912 else
913 mOut.setDataSize(0);
914 }
915 if (bwr.read_consumed > 0) {
916 mIn.setDataSize(bwr.read_consumed);
917 mIn.setDataPosition(0);
918 }
919 IF_LOG_COMMANDS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700920 alog << "Remaining data size: " << mOut.dataSize() << endl;
921 alog << "Received commands from driver: " << indent;
922 const void* cmds = mIn.data();
923 const void* end = mIn.data() + mIn.dataSize();
924 alog << HexDump(cmds, mIn.dataSize()) << endl;
925 while (cmds < end) cmds = printReturnCommand(alog, cmds);
926 alog << dedent;
927 }
928 return NO_ERROR;
929 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800930
Mathias Agopian7922fa22009-05-18 15:08:03 -0700931 return err;
932}
933
934status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
935 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
936{
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200937 binder_transaction_data_sg tr_sg;
Christopher Ferris678434f2017-07-27 10:42:20 -0700938 /* Don't pass uninitialized stack data to a remote process */
939 tr_sg.transaction_data.target.ptr = 0;
940 tr_sg.transaction_data.target.handle = handle;
941 tr_sg.transaction_data.code = code;
942 tr_sg.transaction_data.flags = binderFlags;
943 tr_sg.transaction_data.cookie = 0;
944 tr_sg.transaction_data.sender_pid = 0;
945 tr_sg.transaction_data.sender_euid = 0;
Yifan Hongdde40f32017-01-12 14:22:45 -0800946
Mathias Agopian7922fa22009-05-18 15:08:03 -0700947 const status_t err = data.errorCheck();
948 if (err == NO_ERROR) {
Christopher Ferris678434f2017-07-27 10:42:20 -0700949 tr_sg.transaction_data.data_size = data.ipcDataSize();
950 tr_sg.transaction_data.data.ptr.buffer = data.ipcData();
951 tr_sg.transaction_data.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
952 tr_sg.transaction_data.data.ptr.offsets = data.ipcObjects();
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200953 tr_sg.buffers_size = data.ipcBufferSize();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700954 } else if (statusBuffer) {
Christopher Ferris678434f2017-07-27 10:42:20 -0700955 tr_sg.transaction_data.flags |= TF_STATUS_CODE;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700956 *statusBuffer = err;
Christopher Ferris678434f2017-07-27 10:42:20 -0700957 tr_sg.transaction_data.data_size = sizeof(status_t);
958 tr_sg.transaction_data.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
959 tr_sg.transaction_data.offsets_size = 0;
960 tr_sg.transaction_data.data.ptr.offsets = 0;
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200961 tr_sg.buffers_size = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700962 } else {
963 return (mLastError = err);
964 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800965
Mathias Agopian7922fa22009-05-18 15:08:03 -0700966 mOut.writeInt32(cmd);
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200967 mOut.write(&tr_sg, sizeof(tr_sg));
Yifan Hongdde40f32017-01-12 14:22:45 -0800968
Mathias Agopian7922fa22009-05-18 15:08:03 -0700969 return NO_ERROR;
970}
971
Yifan Hongdde40f32017-01-12 14:22:45 -0800972void IPCThreadState::setTheContextObject(sp<BHwBinder> obj)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700973{
Martijn Coenena660cbc2016-05-12 11:29:23 +0200974 mContextObject = obj;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700975}
976
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200977bool IPCThreadState::isLooperThread()
978{
979 return mIsLooper;
980}
981
Tobias Lindskoga36d5762018-01-05 10:28:31 +0100982bool IPCThreadState::isOnlyBinderThread() {
983 return (mIsLooper && mProcess->mMaxThreads <= 1) || mIsPollingThread;
984}
985
Steven Morelandd7bbfdb2018-05-01 16:30:46 -0700986void IPCThreadState::addPostCommandTask(const std::function<void(void)>& task) {
987 mPostCommandTasks.push_back(task);
988}
989
Mathias Agopian7922fa22009-05-18 15:08:03 -0700990status_t IPCThreadState::executeCommand(int32_t cmd)
991{
Yifan Hongdde40f32017-01-12 14:22:45 -0800992 BHwBinder* obj;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700993 RefBase::weakref_type* refs;
994 status_t result = NO_ERROR;
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100995 switch ((uint32_t)cmd) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700996 case BR_ERROR:
997 result = mIn.readInt32();
998 break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800999
Mathias Agopian7922fa22009-05-18 15:08:03 -07001000 case BR_OK:
1001 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001002
Mathias Agopian7922fa22009-05-18 15:08:03 -07001003 case BR_ACQUIRE:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001004 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001005 obj = (BHwBinder*)mIn.readPointer();
Steve Blockd0bfabc2012-01-09 18:35:44 +00001006 ALOG_ASSERT(refs->refBase() == obj,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001007 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1008 refs, obj, refs->refBase());
1009 obj->incStrong(mProcess.get());
1010 IF_LOG_REMOTEREFS() {
1011 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1012 obj->printRefs();
1013 }
1014 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001015 mOut.writePointer((uintptr_t)refs);
1016 mOut.writePointer((uintptr_t)obj);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001017 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001018
Mathias Agopian7922fa22009-05-18 15:08:03 -07001019 case BR_RELEASE:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001020 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001021 obj = (BHwBinder*)mIn.readPointer();
Steve Blockd0bfabc2012-01-09 18:35:44 +00001022 ALOG_ASSERT(refs->refBase() == obj,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001023 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1024 refs, obj, refs->refBase());
1025 IF_LOG_REMOTEREFS() {
1026 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1027 obj->printRefs();
1028 }
1029 mPendingStrongDerefs.push(obj);
1030 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001031
Mathias Agopian7922fa22009-05-18 15:08:03 -07001032 case BR_INCREFS:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001033 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001034 obj = (BHwBinder*)mIn.readPointer();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001035 refs->incWeak(mProcess.get());
1036 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001037 mOut.writePointer((uintptr_t)refs);
1038 mOut.writePointer((uintptr_t)obj);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001039 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001040
Mathias Agopian7922fa22009-05-18 15:08:03 -07001041 case BR_DECREFS:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001042 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001043 obj = (BHwBinder*)mIn.readPointer();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001044 // NOTE: This assertion is not valid, because the object may no
Yifan Hongdde40f32017-01-12 14:22:45 -08001045 // longer exist (thus the (BHwBinder*)cast above resulting in a different
Mathias Agopian7922fa22009-05-18 15:08:03 -07001046 // memory address).
Steve Blockd0bfabc2012-01-09 18:35:44 +00001047 //ALOG_ASSERT(refs->refBase() == obj,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001048 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1049 // refs, obj, refs->refBase());
1050 mPendingWeakDerefs.push(refs);
1051 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001052
Mathias Agopian7922fa22009-05-18 15:08:03 -07001053 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001054 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001055 obj = (BHwBinder*)mIn.readPointer();
1056
Mathias Agopian7922fa22009-05-18 15:08:03 -07001057 {
1058 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Blockd0bfabc2012-01-09 18:35:44 +00001059 ALOG_ASSERT(success && refs->refBase() == obj,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001060 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1061 refs, obj, refs->refBase());
Yifan Hongdde40f32017-01-12 14:22:45 -08001062
Mathias Agopian7922fa22009-05-18 15:08:03 -07001063 mOut.writeInt32(BC_ACQUIRE_RESULT);
1064 mOut.writeInt32((int32_t)success);
1065 }
1066 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001067
Mathias Agopian7922fa22009-05-18 15:08:03 -07001068 case BR_TRANSACTION:
1069 {
1070 binder_transaction_data tr;
1071 result = mIn.read(&tr, sizeof(tr));
Steve Blockd0bfabc2012-01-09 18:35:44 +00001072 ALOG_ASSERT(result == NO_ERROR,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001073 "Not enough command data for brTRANSACTION");
1074 if (result != NO_ERROR) break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001075
Mathias Agopian7922fa22009-05-18 15:08:03 -07001076 Parcel buffer;
1077 buffer.ipcSetDataReference(
1078 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1079 tr.data_size,
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001080 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1081 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
Yifan Hongdde40f32017-01-12 14:22:45 -08001082
Mathias Agopian7922fa22009-05-18 15:08:03 -07001083 const pid_t origPid = mCallingPid;
1084 const uid_t origUid = mCallingUid;
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001085 const int32_t origStrictModePolicy = mStrictModePolicy;
1086 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1087
Mathias Agopian7922fa22009-05-18 15:08:03 -07001088 mCallingPid = tr.sender_pid;
1089 mCallingUid = tr.sender_euid;
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001090 mLastTransactionBinderFlags = tr.flags;
1091
Steve Block93cf8542012-01-04 20:05:49 +00001092 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001093
Mathias Agopian7922fa22009-05-18 15:08:03 -07001094 Parcel reply;
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001095 status_t error;
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001096 bool reply_sent = false;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001097 IF_LOG_TRANSACTIONS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001098 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1099 << " / obj " << tr.target.ptr << " / code "
1100 << TypeCode(tr.code) << ": " << indent << buffer
1101 << dedent << endl
1102 << "Data addr = "
1103 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1104 << ", offsets addr="
1105 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1106 }
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001107
1108 auto reply_callback = [&] (auto &replyParcel) {
1109 if (reply_sent) {
1110 // Reply was sent earlier, ignore it.
1111 ALOGE("Dropping binder reply, it was sent already.");
1112 return;
1113 }
1114 reply_sent = true;
1115 if ((tr.flags & TF_ONE_WAY) == 0) {
1116 replyParcel.setError(NO_ERROR);
1117 sendReply(replyParcel, 0);
1118 } else {
1119 ALOGE("Not sending reply in one-way transaction");
1120 }
1121 };
1122
Mathias Agopian7922fa22009-05-18 15:08:03 -07001123 if (tr.target.ptr) {
Dianne Hackborn839f7072016-03-21 10:36:54 -07001124 // We only have a weak reference on the target object, so we must first try to
1125 // safely acquire a strong reference before doing anything else with it.
1126 if (reinterpret_cast<RefBase::weakref_type*>(
1127 tr.target.ptr)->attemptIncStrong(this)) {
Yifan Hongdde40f32017-01-12 14:22:45 -08001128 error = reinterpret_cast<BHwBinder*>(tr.cookie)->transact(tr.code, buffer,
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001129 &reply, tr.flags, reply_callback);
Yifan Hongdde40f32017-01-12 14:22:45 -08001130 reinterpret_cast<BHwBinder*>(tr.cookie)->decStrong(this);
Dianne Hackborn839f7072016-03-21 10:36:54 -07001131 } else {
1132 error = UNKNOWN_TRANSACTION;
1133 }
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -07001134
Mathias Agopian7922fa22009-05-18 15:08:03 -07001135 } else {
Martijn Coenena660cbc2016-05-12 11:29:23 +02001136 error = mContextObject->transact(tr.code, buffer, &reply, tr.flags, reply_callback);
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001137 }
1138
1139 if ((tr.flags & TF_ONE_WAY) == 0) {
1140 if (!reply_sent) {
1141 // Should have been a reply but there wasn't, so there
1142 // must have been an error instead.
1143 reply.setError(error);
1144 sendReply(reply, 0);
1145 } else {
1146 if (error != NO_ERROR) {
1147 ALOGE("transact() returned error after sending reply.");
1148 } else {
1149 // Ok, reply sent and transact didn't return an error.
1150 }
1151 }
1152 } else {
1153 // One-way transaction, don't care about return value or reply.
Mathias Agopian7922fa22009-05-18 15:08:03 -07001154 }
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001155
Steve Block93cf8542012-01-04 20:05:49 +00001156 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
Mathias Agopian7922fa22009-05-18 15:08:03 -07001157 // mCallingPid, origPid, origUid);
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001158
Yifan Hongdde40f32017-01-12 14:22:45 -08001159
Mathias Agopian7922fa22009-05-18 15:08:03 -07001160 mCallingPid = origPid;
1161 mCallingUid = origUid;
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001162 mStrictModePolicy = origStrictModePolicy;
1163 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate7c4dfec2010-03-18 17:55:03 -07001164
Mathias Agopian7922fa22009-05-18 15:08:03 -07001165 IF_LOG_TRANSACTIONS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001166 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1167 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1168 }
Yifan Hongdde40f32017-01-12 14:22:45 -08001169
Mathias Agopian7922fa22009-05-18 15:08:03 -07001170 }
1171 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001172
Mathias Agopian7922fa22009-05-18 15:08:03 -07001173 case BR_DEAD_BINDER:
1174 {
Yifan Hong1e118d22017-01-12 14:42:28 -08001175 BpHwBinder *proxy = (BpHwBinder*)mIn.readPointer();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001176 proxy->sendObituary();
1177 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001178 mOut.writePointer((uintptr_t)proxy);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001179 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001180
Mathias Agopian7922fa22009-05-18 15:08:03 -07001181 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1182 {
Yifan Hong1e118d22017-01-12 14:42:28 -08001183 BpHwBinder *proxy = (BpHwBinder*)mIn.readPointer();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001184 proxy->getWeakRefs()->decWeak(proxy);
1185 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001186
Mathias Agopian7922fa22009-05-18 15:08:03 -07001187 case BR_FINISHED:
1188 result = TIMED_OUT;
1189 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001190
Mathias Agopian7922fa22009-05-18 15:08:03 -07001191 case BR_NOOP:
1192 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001193
Mathias Agopian7922fa22009-05-18 15:08:03 -07001194 case BR_SPAWN_LOOPER:
1195 mProcess->spawnPooledThread(false);
1196 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001197
Mathias Agopian7922fa22009-05-18 15:08:03 -07001198 default:
1199 printf("*** BAD COMMAND %d received from Binder driver\n", cmd);
1200 result = UNKNOWN_ERROR;
1201 break;
1202 }
1203
1204 if (result != NO_ERROR) {
1205 mLastError = result;
1206 }
Yifan Hongdde40f32017-01-12 14:22:45 -08001207
Mathias Agopian7922fa22009-05-18 15:08:03 -07001208 return result;
1209}
1210
1211void IPCThreadState::threadDestructor(void *st)
1212{
Todd Poynor0646cb02013-06-25 19:12:18 -07001213 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1214 if (self) {
1215 self->flushCommands();
Elliott Hughese5e70552015-08-12 15:27:47 -07001216#if defined(__ANDROID__)
Johannes Carlsson597a3c72011-02-17 14:06:53 +01001217 if (self->mProcess->mDriverFD > 0) {
1218 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1219 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07001220#endif
Todd Poynor0646cb02013-06-25 19:12:18 -07001221 delete self;
1222 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07001223}
1224
1225
Colin Crossf0487982014-02-05 17:42:44 -08001226void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1227 size_t /*dataSize*/,
1228 const binder_size_t* /*objects*/,
1229 size_t /*objectsSize*/, void* /*cookie*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -07001230{
Steve Block93cf8542012-01-04 20:05:49 +00001231 //ALOGI("Freeing parcel %p", &parcel);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001232 IF_LOG_COMMANDS() {
1233 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1234 }
Steve Blockd0bfabc2012-01-09 18:35:44 +00001235 ALOG_ASSERT(data != NULL, "Called with NULL data");
Mathias Agopian7922fa22009-05-18 15:08:03 -07001236 if (parcel != NULL) parcel->closeFileDescriptors();
1237 IPCThreadState* state = self();
1238 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001239 state->mOut.writePointer((uintptr_t)data);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001240}
1241
Martijn Coenenf75a23d2016-08-01 11:55:17 +02001242}; // namespace hardware
Mathias Agopian7922fa22009-05-18 15:08:03 -07001243}; // namespace android