blob: d29ae5181343c496d994c5fb711d1b5664a2a1aa [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
Mathias Agopian7922fa22009-05-18 15:08:03 -070026#include <utils/Log.h>
Colin Crossb1dc6542016-04-15 14:29:55 -070027#include <utils/SystemClock.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070028#include <utils/threads.h>
29
30#include <private/binder/binder_module.h>
Martijn Coenene01f4f22016-05-12 12:33:28 +020031#include <hwbinder/Static.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070032
Mathias Agopian7922fa22009-05-18 15:08:03 -070033#include <errno.h>
Colin Crossb1dc6542016-04-15 14:29:55 -070034#include <inttypes.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070035#include <pthread.h>
36#include <sched.h>
Yabin Cuibbef2ba2015-01-26 19:45:47 -080037#include <signal.h>
38#include <stdio.h>
39#include <sys/ioctl.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070040#include <sys/resource.h>
Yabin Cuibbef2ba2015-01-26 19:45:47 -080041#include <unistd.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070042
43#if LOG_NDEBUG
44
45#define IF_LOG_TRANSACTIONS() if (false)
46#define IF_LOG_COMMANDS() if (false)
Yifan Hongdde40f32017-01-12 14:22:45 -080047#define LOG_REMOTEREFS(...)
Mathias Agopian7922fa22009-05-18 15:08:03 -070048#define IF_LOG_REMOTEREFS() if (false)
Yifan Hongdde40f32017-01-12 14:22:45 -080049#define LOG_THREADPOOL(...)
50#define LOG_ONEWAY(...)
Mathias Agopian7922fa22009-05-18 15:08:03 -070051
52#else
53
Steve Block5854b912011-10-12 17:27:03 +010054#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
55#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
56#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
57#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
58#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
59#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
Mathias Agopian7922fa22009-05-18 15:08:03 -070060
61#endif
62
63// ---------------------------------------------------------------------------
64
65namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020066namespace hardware {
Mathias Agopian7922fa22009-05-18 15:08:03 -070067
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -070068// Static const and functions will be optimized out if not used,
69// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
Mathias Agopian7922fa22009-05-18 15:08:03 -070070static const char *kReturnStrings[] = {
Andy McFadden457d51f2011-08-31 07:43:40 -070071 "BR_ERROR",
Mathias Agopian7922fa22009-05-18 15:08:03 -070072 "BR_OK",
Mathias Agopian7922fa22009-05-18 15:08:03 -070073 "BR_TRANSACTION",
74 "BR_REPLY",
75 "BR_ACQUIRE_RESULT",
76 "BR_DEAD_REPLY",
77 "BR_TRANSACTION_COMPLETE",
78 "BR_INCREFS",
79 "BR_ACQUIRE",
80 "BR_RELEASE",
81 "BR_DECREFS",
82 "BR_ATTEMPT_ACQUIRE",
Mathias Agopian7922fa22009-05-18 15:08:03 -070083 "BR_NOOP",
84 "BR_SPAWN_LOOPER",
85 "BR_FINISHED",
86 "BR_DEAD_BINDER",
Andy McFadden457d51f2011-08-31 07:43:40 -070087 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
88 "BR_FAILED_REPLY"
Mathias Agopian7922fa22009-05-18 15:08:03 -070089};
90
91static const char *kCommandStrings[] = {
Mathias Agopian7922fa22009-05-18 15:08:03 -070092 "BC_TRANSACTION",
93 "BC_REPLY",
94 "BC_ACQUIRE_RESULT",
95 "BC_FREE_BUFFER",
Mathias Agopian7922fa22009-05-18 15:08:03 -070096 "BC_INCREFS",
97 "BC_ACQUIRE",
98 "BC_RELEASE",
99 "BC_DECREFS",
100 "BC_INCREFS_DONE",
101 "BC_ACQUIRE_DONE",
102 "BC_ATTEMPT_ACQUIRE",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700103 "BC_REGISTER_LOOPER",
104 "BC_ENTER_LOOPER",
105 "BC_EXIT_LOOPER",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700106 "BC_REQUEST_DEATH_NOTIFICATION",
107 "BC_CLEAR_DEATH_NOTIFICATION",
108 "BC_DEAD_BINDER_DONE"
Mathias Agopian7922fa22009-05-18 15:08:03 -0700109};
110
111static const char* getReturnString(size_t idx)
112{
113 if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
114 return kReturnStrings[idx];
115 else
116 return "unknown";
117}
118
Mathias Agopian7922fa22009-05-18 15:08:03 -0700119static const void* printBinderTransactionData(TextOutput& out, const void* data)
120{
121 const binder_transaction_data* btd =
122 (const binder_transaction_data*)data;
Andy McFadden457d51f2011-08-31 07:43:40 -0700123 if (btd->target.handle < 1024) {
124 /* want to print descriptors in decimal; guess based on value */
125 out << "target.desc=" << btd->target.handle;
126 } else {
127 out << "target.ptr=" << btd->target.ptr;
128 }
129 out << " (cookie " << btd->cookie << ")" << endl
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700130 << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
Mathias Agopian7922fa22009-05-18 15:08:03 -0700131 << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
132 << " bytes)" << endl
133 << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
Andy McFadden457d51f2011-08-31 07:43:40 -0700134 << " bytes)";
Mathias Agopian7922fa22009-05-18 15:08:03 -0700135 return btd+1;
136}
137
138static const void* printReturnCommand(TextOutput& out, const void* _cmd)
139{
Andy McFadden457d51f2011-08-31 07:43:40 -0700140 static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700141 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100142 uint32_t code = (uint32_t)*cmd++;
Andy McFadden457d51f2011-08-31 07:43:40 -0700143 size_t cmdIndex = code & 0xff;
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100144 if (code == BR_ERROR) {
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700145 out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700146 return cmd;
Andy McFadden457d51f2011-08-31 07:43:40 -0700147 } else if (cmdIndex >= N) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700148 out << "Unknown reply: " << code << endl;
149 return cmd;
150 }
Andy McFadden457d51f2011-08-31 07:43:40 -0700151 out << kReturnStrings[cmdIndex];
Yifan Hongdde40f32017-01-12 14:22:45 -0800152
Mathias Agopian7922fa22009-05-18 15:08:03 -0700153 switch (code) {
154 case BR_TRANSACTION:
155 case BR_REPLY: {
156 out << ": " << indent;
157 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
158 out << dedent;
159 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800160
Mathias Agopian7922fa22009-05-18 15:08:03 -0700161 case BR_ACQUIRE_RESULT: {
162 const int32_t res = *cmd++;
163 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
164 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800165
Mathias Agopian7922fa22009-05-18 15:08:03 -0700166 case BR_INCREFS:
167 case BR_ACQUIRE:
168 case BR_RELEASE:
169 case BR_DECREFS: {
170 const int32_t b = *cmd++;
171 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700172 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
Mathias Agopian7922fa22009-05-18 15:08:03 -0700173 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800174
Mathias Agopian7922fa22009-05-18 15:08:03 -0700175 case BR_ATTEMPT_ACQUIRE: {
176 const int32_t p = *cmd++;
177 const int32_t b = *cmd++;
178 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700179 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
Mathias Agopian7922fa22009-05-18 15:08:03 -0700180 << "), pri=" << p;
181 } break;
182
183 case BR_DEAD_BINDER:
184 case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
185 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700186 out << ": death cookie " << (void*)(long)c;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700187 } break;
Andy McFadden457d51f2011-08-31 07:43:40 -0700188
189 default:
190 // no details to show for: BR_OK, BR_DEAD_REPLY,
191 // BR_TRANSACTION_COMPLETE, BR_FINISHED
192 break;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700193 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800194
Mathias Agopian7922fa22009-05-18 15:08:03 -0700195 out << endl;
196 return cmd;
197}
198
199static const void* printCommand(TextOutput& out, const void* _cmd)
200{
Andy McFadden457d51f2011-08-31 07:43:40 -0700201 static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700202 const int32_t* cmd = (const int32_t*)_cmd;
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100203 uint32_t code = (uint32_t)*cmd++;
Andy McFadden457d51f2011-08-31 07:43:40 -0700204 size_t cmdIndex = code & 0xff;
205
206 if (cmdIndex >= N) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700207 out << "Unknown command: " << code << endl;
208 return cmd;
209 }
Andy McFadden457d51f2011-08-31 07:43:40 -0700210 out << kCommandStrings[cmdIndex];
211
Mathias Agopian7922fa22009-05-18 15:08:03 -0700212 switch (code) {
213 case BC_TRANSACTION:
214 case BC_REPLY: {
215 out << ": " << indent;
216 cmd = (const int32_t *)printBinderTransactionData(out, cmd);
217 out << dedent;
218 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800219
Mathias Agopian7922fa22009-05-18 15:08:03 -0700220 case BC_ACQUIRE_RESULT: {
221 const int32_t res = *cmd++;
222 out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
223 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800224
Mathias Agopian7922fa22009-05-18 15:08:03 -0700225 case BC_FREE_BUFFER: {
226 const int32_t buf = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700227 out << ": buffer=" << (void*)(long)buf;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700228 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800229
Mathias Agopian7922fa22009-05-18 15:08:03 -0700230 case BC_INCREFS:
231 case BC_ACQUIRE:
232 case BC_RELEASE:
233 case BC_DECREFS: {
234 const int32_t d = *cmd++;
Andy McFadden457d51f2011-08-31 07:43:40 -0700235 out << ": desc=" << d;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700236 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800237
Mathias Agopian7922fa22009-05-18 15:08:03 -0700238 case BC_INCREFS_DONE:
239 case BC_ACQUIRE_DONE: {
240 const int32_t b = *cmd++;
241 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700242 out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
Mathias Agopian7922fa22009-05-18 15:08:03 -0700243 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800244
Mathias Agopian7922fa22009-05-18 15:08:03 -0700245 case BC_ATTEMPT_ACQUIRE: {
246 const int32_t p = *cmd++;
247 const int32_t d = *cmd++;
Andy McFadden457d51f2011-08-31 07:43:40 -0700248 out << ": desc=" << d << ", pri=" << p;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700249 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800250
Mathias Agopian7922fa22009-05-18 15:08:03 -0700251 case BC_REQUEST_DEATH_NOTIFICATION:
252 case BC_CLEAR_DEATH_NOTIFICATION: {
253 const int32_t h = *cmd++;
254 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700255 out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
Mathias Agopian7922fa22009-05-18 15:08:03 -0700256 } break;
257
258 case BC_DEAD_BINDER_DONE: {
259 const int32_t c = *cmd++;
Chih-Hung Hsieh30dcad72014-10-24 14:10:09 -0700260 out << ": death cookie " << (void*)(long)c;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700261 } break;
Andy McFadden457d51f2011-08-31 07:43:40 -0700262
263 default:
264 // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
265 // BC_EXIT_LOOPER
266 break;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700267 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800268
Mathias Agopian7922fa22009-05-18 15:08:03 -0700269 out << endl;
270 return cmd;
271}
Mathias Agopian7922fa22009-05-18 15:08:03 -0700272
273static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
274static bool gHaveTLS = false;
275static pthread_key_t gTLS = 0;
276static bool gShutdown = false;
Dianne Hackborn5f4d7e82009-12-07 17:59:37 -0800277static bool gDisableBackgroundScheduling = false;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700278
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
Dianne Hackborn5f4d7e82009-12-07 17:59:37 -0800335void IPCThreadState::disableBackgroundScheduling(bool disable)
336{
337 gDisableBackgroundScheduling = disable;
338}
339
Mathias Agopian7922fa22009-05-18 15:08:03 -0700340sp<ProcessState> IPCThreadState::process()
341{
342 return mProcess;
343}
344
345status_t IPCThreadState::clearLastError()
346{
347 const status_t err = mLastError;
348 mLastError = NO_ERROR;
349 return err;
350}
351
Dan Stozae8da8a42014-11-26 12:23:23 -0800352pid_t IPCThreadState::getCallingPid() const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700353{
354 return mCallingPid;
355}
356
Dan Stozae8da8a42014-11-26 12:23:23 -0800357uid_t IPCThreadState::getCallingUid() const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700358{
359 return mCallingUid;
360}
361
362int64_t IPCThreadState::clearCallingIdentity()
363{
364 int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
365 clearCaller();
366 return token;
367}
368
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700369void IPCThreadState::setStrictModePolicy(int32_t policy)
370{
371 mStrictModePolicy = policy;
372}
373
Brad Fitzpatrick3f4ef592010-07-07 16:06:39 -0700374int32_t IPCThreadState::getStrictModePolicy() const
375{
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700376 return mStrictModePolicy;
377}
378
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -0700379void IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
380{
381 mLastTransactionBinderFlags = flags;
382}
383
384int32_t IPCThreadState::getLastTransactionBinderFlags() const
385{
386 return mLastTransactionBinderFlags;
387}
388
Mathias Agopian7922fa22009-05-18 15:08:03 -0700389void IPCThreadState::restoreCallingIdentity(int64_t token)
390{
391 mCallingUid = (int)(token>>32);
392 mCallingPid = (int)token;
393}
394
395void IPCThreadState::clearCaller()
396{
Marco Nelissenb4f35d02009-07-17 07:59:17 -0700397 mCallingPid = getpid();
398 mCallingUid = getuid();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700399}
400
401void IPCThreadState::flushCommands()
402{
403 if (mProcess->mDriverFD <= 0)
404 return;
405 talkWithDriver(false);
Martijn Coenenb8253722018-05-23 15:33:22 +0200406 // The flush could have caused post-write refcount decrements to have
407 // been executed, which in turn could result in BC_RELEASE/BC_DECREFS
408 // being queued in mOut. So flush again, if we need to.
409 if (mOut.dataSize() > 0) {
410 talkWithDriver(false);
411 }
412 if (mOut.dataSize() > 0) {
413 ALOGW("mOut.dataSize() > 0 after flushCommands()");
414 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700415}
416
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700417void IPCThreadState::blockUntilThreadAvailable()
418{
419 pthread_mutex_lock(&mProcess->mThreadCountLock);
420 while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
Wale Ogunwale319900a2015-04-21 12:29:50 -0700421 ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n",
422 static_cast<unsigned long>(mProcess->mExecutingThreadsCount),
423 static_cast<unsigned long>(mProcess->mMaxThreads));
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700424 pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
425 }
426 pthread_mutex_unlock(&mProcess->mThreadCountLock);
427}
428
Todd Poynor0646cb02013-06-25 19:12:18 -0700429status_t IPCThreadState::getAndExecuteCommand()
430{
431 status_t result;
432 int32_t cmd;
433
434 result = talkWithDriver();
435 if (result >= NO_ERROR) {
436 size_t IN = mIn.dataAvail();
437 if (IN < sizeof(int32_t)) return result;
438 cmd = mIn.readInt32();
439 IF_LOG_COMMANDS() {
440 alog << "Processing top-level Command: "
441 << getReturnString(cmd) << endl;
442 }
443
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700444 pthread_mutex_lock(&mProcess->mThreadCountLock);
445 mProcess->mExecutingThreadsCount++;
Colin Crossb1dc6542016-04-15 14:29:55 -0700446 if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads &&
Martijn Coenen0ce07ce2017-07-14 15:37:25 +0200447 mProcess->mMaxThreads > 1 && mProcess->mStarvationStartTimeMs == 0) {
Colin Crossb1dc6542016-04-15 14:29:55 -0700448 mProcess->mStarvationStartTimeMs = uptimeMillis();
449 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700450 pthread_mutex_unlock(&mProcess->mThreadCountLock);
451
Todd Poynor0646cb02013-06-25 19:12:18 -0700452 result = executeCommand(cmd);
453
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700454 pthread_mutex_lock(&mProcess->mThreadCountLock);
455 mProcess->mExecutingThreadsCount--;
Wei Wangdf7f40d2018-03-29 15:41:59 -0700456 if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads &&
Martijn Coenen0ce07ce2017-07-14 15:37:25 +0200457 mProcess->mStarvationStartTimeMs != 0) {
Colin Crossb1dc6542016-04-15 14:29:55 -0700458 int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs;
459 if (starvationTimeMs > 100) {
Wei Wangdf7f40d2018-03-29 15:41:59 -0700460 // If there is only a single-threaded client, nobody would be blocked
461 // on this, and it's not really starvation. (see b/37647467)
462 ALOGW("All binder threads in pool (%zu threads) busy for %" PRId64 " ms%s",
463 mProcess->mMaxThreads, starvationTimeMs,
464 mProcess->mMaxThreads > 1 ? "" : " (may be a false alarm)");
Colin Crossb1dc6542016-04-15 14:29:55 -0700465 }
466 mProcess->mStarvationStartTimeMs = 0;
467 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700468 pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
469 pthread_mutex_unlock(&mProcess->mThreadCountLock);
Todd Poynor0646cb02013-06-25 19:12:18 -0700470 }
471
472 return result;
473}
474
475// When we've cleared the incoming command queue, process any pending derefs
476void IPCThreadState::processPendingDerefs()
477{
478 if (mIn.dataPosition() >= mIn.dataSize()) {
Martijn Coenenc9f105b2017-08-08 15:36:16 +0200479 /*
480 * The decWeak()/decStrong() calls may cause a destructor to run,
481 * which in turn could have initiated an outgoing transaction,
482 * which in turn could cause us to add to the pending refs
483 * vectors; so instead of simply iterating, loop until they're empty.
484 *
485 * We do this in an outer loop, because calling decStrong()
486 * may result in something being added to mPendingWeakDerefs,
487 * which could be delayed until the next incoming command
488 * from the driver if we don't process it now.
489 */
490 while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) {
491 while (mPendingWeakDerefs.size() > 0) {
492 RefBase::weakref_type* refs = mPendingWeakDerefs[0];
493 mPendingWeakDerefs.removeAt(0);
Todd Poynor0646cb02013-06-25 19:12:18 -0700494 refs->decWeak(mProcess.get());
495 }
Todd Poynor0646cb02013-06-25 19:12:18 -0700496
Martijn Coenenc9f105b2017-08-08 15:36:16 +0200497 if (mPendingStrongDerefs.size() > 0) {
498 // We don't use while() here because we don't want to re-order
499 // strong and weak decs at all; if this decStrong() causes both a
500 // decWeak() and a decStrong() to be queued, we want to process
501 // the decWeak() first.
502 BHwBinder* obj = mPendingStrongDerefs[0];
503 mPendingStrongDerefs.removeAt(0);
Todd Poynor0646cb02013-06-25 19:12:18 -0700504 obj->decStrong(mProcess.get());
505 }
Todd Poynor0646cb02013-06-25 19:12:18 -0700506 }
507 }
508}
509
Martijn Coenenb8253722018-05-23 15:33:22 +0200510void IPCThreadState::processPostWriteDerefs()
511{
512 /*
513 * libhwbinder has a flushCommands() in the BpHwBinder destructor,
514 * which makes this function (potentially) reentrant.
515 * New entries shouldn't be added though, so just iterating until empty
516 * should be safe.
517 */
518 while (mPostWriteWeakDerefs.size() > 0) {
519 RefBase::weakref_type* refs = mPostWriteWeakDerefs[0];
520 mPostWriteWeakDerefs.removeAt(0);
521 refs->decWeak(mProcess.get());
522 }
523
524 while (mPostWriteStrongDerefs.size() > 0) {
525 RefBase* obj = mPostWriteStrongDerefs[0];
526 mPostWriteStrongDerefs.removeAt(0);
527 obj->decStrong(mProcess.get());
528 }
529}
530
Mathias Agopian7922fa22009-05-18 15:08:03 -0700531void 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);
Yifan Hongdde40f32017-01-12 14:22:45 -0800536
Mathias Agopian7922fa22009-05-18 15:08:03 -0700537 status_t result;
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200538 mIsLooper = true;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700539 do {
Todd Poynor0646cb02013-06-25 19:12:18 -0700540 processPendingDerefs();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700541 // now get the next command to be processed, waiting if necessary
Todd Poynor0646cb02013-06-25 19:12:18 -0700542 result = getAndExecuteCommand();
Jason Parks2b17f142009-11-03 12:14:38 -0800543
Todd Poynor0646cb02013-06-25 19:12:18 -0700544 if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
545 ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
Jeff Tinkeree711ec2013-06-11 11:30:21 -0700546 mProcess->mDriverFD, result);
547 abort();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700548 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800549
Mathias Agopian7922fa22009-05-18 15:08:03 -0700550 // Let this thread exit the thread pool if it is no longer
551 // needed and it is not the main process thread.
552 if(result == TIMED_OUT && !isMain) {
553 break;
554 }
555 } while (result != -ECONNREFUSED && result != -EBADF);
556
Wei Wang8a2e8ac2016-10-14 09:54:27 -0700557 LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n",
558 (void*)pthread_self(), getpid(), result);
Yifan Hongdde40f32017-01-12 14:22:45 -0800559
Mathias Agopian7922fa22009-05-18 15:08:03 -0700560 mOut.writeInt32(BC_EXIT_LOOPER);
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200561 mIsLooper = false;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700562 talkWithDriver(false);
563}
564
Todd Poynor0646cb02013-06-25 19:12:18 -0700565int IPCThreadState::setupPolling(int* fd)
566{
567 if (mProcess->mDriverFD <= 0) {
568 return -EBADF;
569 }
570
Martijn Coenen80b88ab2017-09-25 14:50:05 +0200571 // Tells the kernel to not spawn any additional binder threads,
572 // as that won't work with polling. Also, the caller is responsible
573 // for subsequently calling handlePolledCommands()
574 mProcess->setThreadPoolConfiguration(1, true /* callerWillJoin */);
Tobias Lindskoga36d5762018-01-05 10:28:31 +0100575 mIsPollingThread = true;
Martijn Coenen80b88ab2017-09-25 14:50:05 +0200576
Todd Poynor0646cb02013-06-25 19:12:18 -0700577 mOut.writeInt32(BC_ENTER_LOOPER);
578 *fd = mProcess->mDriverFD;
579 return 0;
580}
581
582status_t IPCThreadState::handlePolledCommands()
583{
584 status_t result;
585
586 do {
587 result = getAndExecuteCommand();
588 } while (mIn.dataPosition() < mIn.dataSize());
589
590 processPendingDerefs();
591 flushCommands();
592 return result;
593}
594
Colin Crossf0487982014-02-05 17:42:44 -0800595void IPCThreadState::stopProcess(bool /*immediate*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700596{
Steve Block93cf8542012-01-04 20:05:49 +0000597 //ALOGI("**** STOPPING PROCESS");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700598 flushCommands();
599 int fd = mProcess->mDriverFD;
600 mProcess->mDriverFD = -1;
601 close(fd);
602 //kill(getpid(), SIGKILL);
603}
604
605status_t IPCThreadState::transact(int32_t handle,
606 uint32_t code, const Parcel& data,
607 Parcel* reply, uint32_t flags)
608{
Ganesh Mahendrance45b892017-10-11 18:05:13 +0800609 status_t err;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700610
611 flags |= TF_ACCEPT_FDS;
612
613 IF_LOG_TRANSACTIONS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700614 alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
615 << handle << " / code " << TypeCode(code) << ": "
616 << indent << data << dedent << endl;
617 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800618
Ganesh Mahendrance45b892017-10-11 18:05:13 +0800619 LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
620 (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
621 err = writeTransactionData(BC_TRANSACTION_SG, flags, handle, code, data, NULL);
Yifan Hongdde40f32017-01-12 14:22:45 -0800622
Mathias Agopian7922fa22009-05-18 15:08:03 -0700623 if (err != NO_ERROR) {
624 if (reply) reply->setError(err);
625 return (mLastError = err);
626 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800627
Mathias Agopian7922fa22009-05-18 15:08:03 -0700628 if ((flags & TF_ONE_WAY) == 0) {
Dianne Hackborn98878262010-09-24 11:16:23 -0700629 #if 0
630 if (code == 4) { // relayout
Steve Block93cf8542012-01-04 20:05:49 +0000631 ALOGI(">>>>>> CALLING transaction 4");
Dianne Hackborn98878262010-09-24 11:16:23 -0700632 } else {
Steve Block93cf8542012-01-04 20:05:49 +0000633 ALOGI(">>>>>> CALLING transaction %d", code);
Dianne Hackborn98878262010-09-24 11:16:23 -0700634 }
635 #endif
Mathias Agopian7922fa22009-05-18 15:08:03 -0700636 if (reply) {
637 err = waitForResponse(reply);
638 } else {
639 Parcel fakeReply;
640 err = waitForResponse(&fakeReply);
641 }
Dianne Hackborn98878262010-09-24 11:16:23 -0700642 #if 0
643 if (code == 4) { // relayout
Steve Block93cf8542012-01-04 20:05:49 +0000644 ALOGI("<<<<<< RETURNING transaction 4");
Dianne Hackborn98878262010-09-24 11:16:23 -0700645 } else {
Steve Block93cf8542012-01-04 20:05:49 +0000646 ALOGI("<<<<<< RETURNING transaction %d", code);
Dianne Hackborn98878262010-09-24 11:16:23 -0700647 }
648 #endif
Yifan Hongdde40f32017-01-12 14:22:45 -0800649
Mathias Agopian7922fa22009-05-18 15:08:03 -0700650 IF_LOG_TRANSACTIONS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700651 alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
652 << handle << ": ";
653 if (reply) alog << indent << *reply << dedent << endl;
654 else alog << "(none requested)" << endl;
655 }
656 } else {
657 err = waitForResponse(NULL, NULL);
658 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800659
Mathias Agopian7922fa22009-05-18 15:08:03 -0700660 return err;
661}
662
Martijn Coenenb8253722018-05-23 15:33:22 +0200663void IPCThreadState::incStrongHandle(int32_t handle, BpHwBinder *proxy)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700664{
665 LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
666 mOut.writeInt32(BC_ACQUIRE);
667 mOut.writeInt32(handle);
Martijn Coenenb8253722018-05-23 15:33:22 +0200668 // Create a temp reference until the driver has handled this command.
669 proxy->incStrong(mProcess.get());
670 mPostWriteStrongDerefs.push(proxy);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700671}
672
673void IPCThreadState::decStrongHandle(int32_t handle)
674{
675 LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
676 mOut.writeInt32(BC_RELEASE);
677 mOut.writeInt32(handle);
678}
679
Martijn Coenenb8253722018-05-23 15:33:22 +0200680void IPCThreadState::incWeakHandle(int32_t handle, BpHwBinder *proxy)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700681{
682 LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
683 mOut.writeInt32(BC_INCREFS);
684 mOut.writeInt32(handle);
Martijn Coenenb8253722018-05-23 15:33:22 +0200685 // Create a temp reference until the driver has handled this command.
686 proxy->getWeakRefs()->incWeak(mProcess.get());
687 mPostWriteWeakDerefs.push(proxy->getWeakRefs());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700688}
689
690void IPCThreadState::decWeakHandle(int32_t handle)
691{
692 LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
693 mOut.writeInt32(BC_DECREFS);
694 mOut.writeInt32(handle);
695}
696
697status_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
698{
Arve Hjønnevåg304dcae2014-02-14 20:14:02 -0800699#if HAS_BC_ATTEMPT_ACQUIRE
Andy McFadden457d51f2011-08-31 07:43:40 -0700700 LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700701 mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
702 mOut.writeInt32(0); // xxx was thread priority
703 mOut.writeInt32(handle);
704 status_t result = UNKNOWN_ERROR;
Yifan Hongdde40f32017-01-12 14:22:45 -0800705
Mathias Agopian7922fa22009-05-18 15:08:03 -0700706 waitForResponse(NULL, &result);
Yifan Hongdde40f32017-01-12 14:22:45 -0800707
Mathias Agopian7922fa22009-05-18 15:08:03 -0700708#if LOG_REFCOUNTS
709 printf("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
710 handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
711#endif
Yifan Hongdde40f32017-01-12 14:22:45 -0800712
Mathias Agopian7922fa22009-05-18 15:08:03 -0700713 return result;
Arve Hjønnevåg304dcae2014-02-14 20:14:02 -0800714#else
715 (void)handle;
716 ALOGE("%s(%d): Not supported\n", __func__, handle);
717 return INVALID_OPERATION;
718#endif
Mathias Agopian7922fa22009-05-18 15:08:03 -0700719}
720
721void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
722{
723#if LOG_REFCOUNTS
724 printf("IPCThreadState::expungeHandle(%ld)\n", handle);
725#endif
Yunlian Jiang6b65ac32017-09-19 15:51:07 -0700726 self()->mProcess->expungeHandle(handle, binder); // NOLINT
Mathias Agopian7922fa22009-05-18 15:08:03 -0700727}
728
Yifan Hong1e118d22017-01-12 14:42:28 -0800729status_t IPCThreadState::requestDeathNotification(int32_t handle, BpHwBinder* proxy)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700730{
731 mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
732 mOut.writeInt32((int32_t)handle);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +0000733 mOut.writePointer((uintptr_t)proxy);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700734 return NO_ERROR;
735}
736
Yifan Hong1e118d22017-01-12 14:42:28 -0800737status_t IPCThreadState::clearDeathNotification(int32_t handle, BpHwBinder* proxy)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700738{
739 mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
740 mOut.writeInt32((int32_t)handle);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +0000741 mOut.writePointer((uintptr_t)proxy);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700742 return NO_ERROR;
743}
744
745IPCThreadState::IPCThreadState()
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -0700746 : mProcess(ProcessState::self()),
Elliott Hughes07cf48a2014-08-18 10:38:38 -0700747 mMyThreadId(gettid()),
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -0700748 mStrictModePolicy(0),
Martijn Coenen9bd3d3b2017-12-12 09:29:14 +0100749 mLastTransactionBinderFlags(0),
Tobias Lindskoga36d5762018-01-05 10:28:31 +0100750 mIsLooper(false),
751 mIsPollingThread(false) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700752 pthread_setspecific(gTLS, this);
Dianne Hackborn5f4d7e82009-12-07 17:59:37 -0800753 clearCaller();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700754 mIn.setDataCapacity(256);
755 mOut.setDataCapacity(256);
Chih-Hung Hsieh29dbdcf2017-10-02 10:42:37 -0700756
757 // TODO(b/67742352): remove this variable from the class
758 (void)mMyThreadId;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700759}
760
761IPCThreadState::~IPCThreadState()
762{
763}
764
765status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
766{
767 status_t err;
768 status_t statusBuffer;
Martijn Coenend39a1682016-06-03 21:27:28 +0200769 err = writeTransactionData(BC_REPLY_SG, flags, -1, 0, reply, &statusBuffer);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700770 if (err < NO_ERROR) return err;
Yifan Hongdde40f32017-01-12 14:22:45 -0800771
Mathias Agopian7922fa22009-05-18 15:08:03 -0700772 return waitForResponse(NULL, NULL);
773}
774
775status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
776{
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100777 uint32_t cmd;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700778 int32_t err;
779
780 while (1) {
781 if ((err=talkWithDriver()) < NO_ERROR) break;
782 err = mIn.errorCheck();
783 if (err < NO_ERROR) break;
784 if (mIn.dataAvail() == 0) continue;
Yifan Hongdde40f32017-01-12 14:22:45 -0800785
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +0100786 cmd = (uint32_t)mIn.readInt32();
Yifan Hongdde40f32017-01-12 14:22:45 -0800787
Mathias Agopian7922fa22009-05-18 15:08:03 -0700788 IF_LOG_COMMANDS() {
789 alog << "Processing waitForResponse Command: "
790 << getReturnString(cmd) << endl;
791 }
792
793 switch (cmd) {
794 case BR_TRANSACTION_COMPLETE:
795 if (!reply && !acquireResult) goto finish;
796 break;
Yifan Hongdde40f32017-01-12 14:22:45 -0800797
Mathias Agopian7922fa22009-05-18 15:08:03 -0700798 case BR_DEAD_REPLY:
799 err = DEAD_OBJECT;
800 goto finish;
801
802 case BR_FAILED_REPLY:
803 err = FAILED_TRANSACTION;
804 goto finish;
Yifan Hongdde40f32017-01-12 14:22:45 -0800805
Mathias Agopian7922fa22009-05-18 15:08:03 -0700806 case BR_ACQUIRE_RESULT:
807 {
Steve Blockd0bfabc2012-01-09 18:35:44 +0000808 ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700809 const int32_t result = mIn.readInt32();
810 if (!acquireResult) continue;
811 *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
812 }
813 goto finish;
Yifan Hongdde40f32017-01-12 14:22:45 -0800814
Mathias Agopian7922fa22009-05-18 15:08:03 -0700815 case BR_REPLY:
816 {
817 binder_transaction_data tr;
818 err = mIn.read(&tr, sizeof(tr));
Steve Blockd0bfabc2012-01-09 18:35:44 +0000819 ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700820 if (err != NO_ERROR) goto finish;
821
822 if (reply) {
823 if ((tr.flags & TF_STATUS_CODE) == 0) {
824 reply->ipcSetDataReference(
825 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
826 tr.data_size,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800827 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
828 tr.offsets_size/sizeof(binder_size_t),
Mathias Agopian7922fa22009-05-18 15:08:03 -0700829 freeBuffer, this);
830 } else {
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800831 err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700832 freeBuffer(NULL,
833 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
834 tr.data_size,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800835 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
836 tr.offsets_size/sizeof(binder_size_t), this);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700837 }
838 } else {
839 freeBuffer(NULL,
840 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
841 tr.data_size,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800842 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
843 tr.offsets_size/sizeof(binder_size_t), this);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700844 continue;
845 }
846 }
847 goto finish;
848
849 default:
850 err = executeCommand(cmd);
851 if (err != NO_ERROR) goto finish;
852 break;
853 }
854 }
855
856finish:
857 if (err != NO_ERROR) {
858 if (acquireResult) *acquireResult = err;
859 if (reply) reply->setError(err);
860 mLastError = err;
861 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800862
Mathias Agopian7922fa22009-05-18 15:08:03 -0700863 return err;
864}
865
866status_t IPCThreadState::talkWithDriver(bool doReceive)
867{
Johannes Carlsson597a3c72011-02-17 14:06:53 +0100868 if (mProcess->mDriverFD <= 0) {
869 return -EBADF;
870 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800871
Mathias Agopian7922fa22009-05-18 15:08:03 -0700872 binder_write_read bwr;
Yifan Hongdde40f32017-01-12 14:22:45 -0800873
Mathias Agopian7922fa22009-05-18 15:08:03 -0700874 // Is the read buffer empty?
875 const bool needRead = mIn.dataPosition() >= mIn.dataSize();
Yifan Hongdde40f32017-01-12 14:22:45 -0800876
Mathias Agopian7922fa22009-05-18 15:08:03 -0700877 // We don't want to write anything if we are still reading
878 // from data left in the input buffer and the caller
879 // has requested to read the next data.
880 const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
Yifan Hongdde40f32017-01-12 14:22:45 -0800881
Mathias Agopian7922fa22009-05-18 15:08:03 -0700882 bwr.write_size = outAvail;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800883 bwr.write_buffer = (uintptr_t)mOut.data();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700884
885 // This is what we'll read.
886 if (doReceive && needRead) {
887 bwr.read_size = mIn.dataCapacity();
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800888 bwr.read_buffer = (uintptr_t)mIn.data();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700889 } else {
890 bwr.read_size = 0;
Ben Cheng455a70a2011-12-01 17:11:32 -0800891 bwr.read_buffer = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700892 }
Andy McFadden457d51f2011-08-31 07:43:40 -0700893
Mathias Agopian7922fa22009-05-18 15:08:03 -0700894 IF_LOG_COMMANDS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700895 if (outAvail != 0) {
896 alog << "Sending commands to driver: " << indent;
897 const void* cmds = (const void*)bwr.write_buffer;
898 const void* end = ((const uint8_t*)cmds)+bwr.write_size;
899 alog << HexDump(cmds, bwr.write_size) << endl;
900 while (cmds < end) cmds = printCommand(alog, cmds);
901 alog << dedent;
902 }
903 alog << "Size of receive buffer: " << bwr.read_size
904 << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
905 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800906
Mathias Agopian7922fa22009-05-18 15:08:03 -0700907 // Return immediately if there is nothing to do.
908 if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
Andy McFadden457d51f2011-08-31 07:43:40 -0700909
Mathias Agopian7922fa22009-05-18 15:08:03 -0700910 bwr.write_consumed = 0;
911 bwr.read_consumed = 0;
912 status_t err;
913 do {
914 IF_LOG_COMMANDS() {
915 alog << "About to read/write, write size = " << mOut.dataSize() << endl;
916 }
Elliott Hughese5e70552015-08-12 15:27:47 -0700917#if defined(__ANDROID__)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700918 if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
919 err = NO_ERROR;
920 else
921 err = -errno;
922#else
923 err = INVALID_OPERATION;
924#endif
Johannes Carlsson597a3c72011-02-17 14:06:53 +0100925 if (mProcess->mDriverFD <= 0) {
926 err = -EBADF;
927 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700928 IF_LOG_COMMANDS() {
929 alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
930 }
931 } while (err == -EINTR);
Andy McFadden457d51f2011-08-31 07:43:40 -0700932
Mathias Agopian7922fa22009-05-18 15:08:03 -0700933 IF_LOG_COMMANDS() {
Colin Crossf0487982014-02-05 17:42:44 -0800934 alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
Mathias Agopian7922fa22009-05-18 15:08:03 -0700935 << bwr.write_consumed << " (of " << mOut.dataSize()
Todd Poynor0646cb02013-06-25 19:12:18 -0700936 << "), read consumed: " << bwr.read_consumed << endl;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700937 }
938
939 if (err >= NO_ERROR) {
940 if (bwr.write_consumed > 0) {
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800941 if (bwr.write_consumed < mOut.dataSize())
Mathias Agopian7922fa22009-05-18 15:08:03 -0700942 mOut.remove(0, bwr.write_consumed);
Martijn Coenenb8253722018-05-23 15:33:22 +0200943 else {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700944 mOut.setDataSize(0);
Martijn Coenenb8253722018-05-23 15:33:22 +0200945 processPostWriteDerefs();
946 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700947 }
948 if (bwr.read_consumed > 0) {
949 mIn.setDataSize(bwr.read_consumed);
950 mIn.setDataPosition(0);
951 }
952 IF_LOG_COMMANDS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700953 alog << "Remaining data size: " << mOut.dataSize() << endl;
954 alog << "Received commands from driver: " << indent;
955 const void* cmds = mIn.data();
956 const void* end = mIn.data() + mIn.dataSize();
957 alog << HexDump(cmds, mIn.dataSize()) << endl;
958 while (cmds < end) cmds = printReturnCommand(alog, cmds);
959 alog << dedent;
960 }
961 return NO_ERROR;
962 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800963
Mathias Agopian7922fa22009-05-18 15:08:03 -0700964 return err;
965}
966
967status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
968 int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
969{
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200970 binder_transaction_data_sg tr_sg;
Christopher Ferris678434f2017-07-27 10:42:20 -0700971 /* Don't pass uninitialized stack data to a remote process */
972 tr_sg.transaction_data.target.ptr = 0;
973 tr_sg.transaction_data.target.handle = handle;
974 tr_sg.transaction_data.code = code;
975 tr_sg.transaction_data.flags = binderFlags;
976 tr_sg.transaction_data.cookie = 0;
977 tr_sg.transaction_data.sender_pid = 0;
978 tr_sg.transaction_data.sender_euid = 0;
Yifan Hongdde40f32017-01-12 14:22:45 -0800979
Mathias Agopian7922fa22009-05-18 15:08:03 -0700980 const status_t err = data.errorCheck();
981 if (err == NO_ERROR) {
Christopher Ferris678434f2017-07-27 10:42:20 -0700982 tr_sg.transaction_data.data_size = data.ipcDataSize();
983 tr_sg.transaction_data.data.ptr.buffer = data.ipcData();
984 tr_sg.transaction_data.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
985 tr_sg.transaction_data.data.ptr.offsets = data.ipcObjects();
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200986 tr_sg.buffers_size = data.ipcBufferSize();
Mathias Agopian7922fa22009-05-18 15:08:03 -0700987 } else if (statusBuffer) {
Christopher Ferris678434f2017-07-27 10:42:20 -0700988 tr_sg.transaction_data.flags |= TF_STATUS_CODE;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700989 *statusBuffer = err;
Christopher Ferris678434f2017-07-27 10:42:20 -0700990 tr_sg.transaction_data.data_size = sizeof(status_t);
991 tr_sg.transaction_data.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
992 tr_sg.transaction_data.offsets_size = 0;
993 tr_sg.transaction_data.data.ptr.offsets = 0;
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200994 tr_sg.buffers_size = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700995 } else {
996 return (mLastError = err);
997 }
Yifan Hongdde40f32017-01-12 14:22:45 -0800998
Mathias Agopian7922fa22009-05-18 15:08:03 -0700999 mOut.writeInt32(cmd);
Martijn Coenenfd51ebb2016-07-05 17:00:39 +02001000 mOut.write(&tr_sg, sizeof(tr_sg));
Yifan Hongdde40f32017-01-12 14:22:45 -08001001
Mathias Agopian7922fa22009-05-18 15:08:03 -07001002 return NO_ERROR;
1003}
1004
Yifan Hongdde40f32017-01-12 14:22:45 -08001005void IPCThreadState::setTheContextObject(sp<BHwBinder> obj)
Mathias Agopian7922fa22009-05-18 15:08:03 -07001006{
Martijn Coenena660cbc2016-05-12 11:29:23 +02001007 mContextObject = obj;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001008}
1009
Martijn Coenen420d4bb2017-10-24 11:43:55 +02001010bool IPCThreadState::isLooperThread()
1011{
1012 return mIsLooper;
1013}
1014
Tobias Lindskoga36d5762018-01-05 10:28:31 +01001015bool IPCThreadState::isOnlyBinderThread() {
1016 return (mIsLooper && mProcess->mMaxThreads <= 1) || mIsPollingThread;
1017}
1018
Mathias Agopian7922fa22009-05-18 15:08:03 -07001019status_t IPCThreadState::executeCommand(int32_t cmd)
1020{
Yifan Hongdde40f32017-01-12 14:22:45 -08001021 BHwBinder* obj;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001022 RefBase::weakref_type* refs;
1023 status_t result = NO_ERROR;
Bernhard Rosenkränzerb184ed02014-11-25 21:55:33 +01001024 switch ((uint32_t)cmd) {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001025 case BR_ERROR:
1026 result = mIn.readInt32();
1027 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001028
Mathias Agopian7922fa22009-05-18 15:08:03 -07001029 case BR_OK:
1030 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001031
Mathias Agopian7922fa22009-05-18 15:08:03 -07001032 case BR_ACQUIRE:
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();
Steve Blockd0bfabc2012-01-09 18:35:44 +00001035 ALOG_ASSERT(refs->refBase() == obj,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001036 "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
1037 refs, obj, refs->refBase());
1038 obj->incStrong(mProcess.get());
1039 IF_LOG_REMOTEREFS() {
1040 LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
1041 obj->printRefs();
1042 }
1043 mOut.writeInt32(BC_ACQUIRE_DONE);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001044 mOut.writePointer((uintptr_t)refs);
1045 mOut.writePointer((uintptr_t)obj);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001046 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001047
Mathias Agopian7922fa22009-05-18 15:08:03 -07001048 case BR_RELEASE:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001049 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001050 obj = (BHwBinder*)mIn.readPointer();
Steve Blockd0bfabc2012-01-09 18:35:44 +00001051 ALOG_ASSERT(refs->refBase() == obj,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001052 "BR_RELEASE: object %p does not match cookie %p (expected %p)",
1053 refs, obj, refs->refBase());
1054 IF_LOG_REMOTEREFS() {
1055 LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
1056 obj->printRefs();
1057 }
1058 mPendingStrongDerefs.push(obj);
1059 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001060
Mathias Agopian7922fa22009-05-18 15:08:03 -07001061 case BR_INCREFS:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001062 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001063 obj = (BHwBinder*)mIn.readPointer();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001064 refs->incWeak(mProcess.get());
1065 mOut.writeInt32(BC_INCREFS_DONE);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001066 mOut.writePointer((uintptr_t)refs);
1067 mOut.writePointer((uintptr_t)obj);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001068 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001069
Mathias Agopian7922fa22009-05-18 15:08:03 -07001070 case BR_DECREFS:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001071 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001072 obj = (BHwBinder*)mIn.readPointer();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001073 // NOTE: This assertion is not valid, because the object may no
Yifan Hongdde40f32017-01-12 14:22:45 -08001074 // longer exist (thus the (BHwBinder*)cast above resulting in a different
Mathias Agopian7922fa22009-05-18 15:08:03 -07001075 // memory address).
Steve Blockd0bfabc2012-01-09 18:35:44 +00001076 //ALOG_ASSERT(refs->refBase() == obj,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001077 // "BR_DECREFS: object %p does not match cookie %p (expected %p)",
1078 // refs, obj, refs->refBase());
1079 mPendingWeakDerefs.push(refs);
1080 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001081
Mathias Agopian7922fa22009-05-18 15:08:03 -07001082 case BR_ATTEMPT_ACQUIRE:
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001083 refs = (RefBase::weakref_type*)mIn.readPointer();
Yifan Hongdde40f32017-01-12 14:22:45 -08001084 obj = (BHwBinder*)mIn.readPointer();
1085
Mathias Agopian7922fa22009-05-18 15:08:03 -07001086 {
1087 const bool success = refs->attemptIncStrong(mProcess.get());
Steve Blockd0bfabc2012-01-09 18:35:44 +00001088 ALOG_ASSERT(success && refs->refBase() == obj,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001089 "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1090 refs, obj, refs->refBase());
Yifan Hongdde40f32017-01-12 14:22:45 -08001091
Mathias Agopian7922fa22009-05-18 15:08:03 -07001092 mOut.writeInt32(BC_ACQUIRE_RESULT);
1093 mOut.writeInt32((int32_t)success);
1094 }
1095 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001096
Mathias Agopian7922fa22009-05-18 15:08:03 -07001097 case BR_TRANSACTION:
1098 {
1099 binder_transaction_data tr;
1100 result = mIn.read(&tr, sizeof(tr));
Steve Blockd0bfabc2012-01-09 18:35:44 +00001101 ALOG_ASSERT(result == NO_ERROR,
Mathias Agopian7922fa22009-05-18 15:08:03 -07001102 "Not enough command data for brTRANSACTION");
1103 if (result != NO_ERROR) break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001104
Mathias Agopian7922fa22009-05-18 15:08:03 -07001105 Parcel buffer;
1106 buffer.ipcSetDataReference(
1107 reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1108 tr.data_size,
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001109 reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1110 tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
Yifan Hongdde40f32017-01-12 14:22:45 -08001111
Mathias Agopian7922fa22009-05-18 15:08:03 -07001112 const pid_t origPid = mCallingPid;
1113 const uid_t origUid = mCallingUid;
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001114 const int32_t origStrictModePolicy = mStrictModePolicy;
1115 const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1116
Mathias Agopian7922fa22009-05-18 15:08:03 -07001117 mCallingPid = tr.sender_pid;
1118 mCallingUid = tr.sender_euid;
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001119 mLastTransactionBinderFlags = tr.flags;
1120
Steve Block93cf8542012-01-04 20:05:49 +00001121 //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001122
Mathias Agopian7922fa22009-05-18 15:08:03 -07001123 Parcel reply;
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001124 status_t error;
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001125 bool reply_sent = false;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001126 IF_LOG_TRANSACTIONS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001127 alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1128 << " / obj " << tr.target.ptr << " / code "
1129 << TypeCode(tr.code) << ": " << indent << buffer
1130 << dedent << endl
1131 << "Data addr = "
1132 << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1133 << ", offsets addr="
1134 << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1135 }
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001136
1137 auto reply_callback = [&] (auto &replyParcel) {
1138 if (reply_sent) {
1139 // Reply was sent earlier, ignore it.
1140 ALOGE("Dropping binder reply, it was sent already.");
1141 return;
1142 }
1143 reply_sent = true;
1144 if ((tr.flags & TF_ONE_WAY) == 0) {
1145 replyParcel.setError(NO_ERROR);
1146 sendReply(replyParcel, 0);
1147 } else {
1148 ALOGE("Not sending reply in one-way transaction");
1149 }
1150 };
1151
Mathias Agopian7922fa22009-05-18 15:08:03 -07001152 if (tr.target.ptr) {
Dianne Hackborn839f7072016-03-21 10:36:54 -07001153 // We only have a weak reference on the target object, so we must first try to
1154 // safely acquire a strong reference before doing anything else with it.
1155 if (reinterpret_cast<RefBase::weakref_type*>(
1156 tr.target.ptr)->attemptIncStrong(this)) {
Yifan Hongdde40f32017-01-12 14:22:45 -08001157 error = reinterpret_cast<BHwBinder*>(tr.cookie)->transact(tr.code, buffer,
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001158 &reply, tr.flags, reply_callback);
Yifan Hongdde40f32017-01-12 14:22:45 -08001159 reinterpret_cast<BHwBinder*>(tr.cookie)->decStrong(this);
Dianne Hackborn839f7072016-03-21 10:36:54 -07001160 } else {
1161 error = UNKNOWN_TRANSACTION;
1162 }
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -07001163
Mathias Agopian7922fa22009-05-18 15:08:03 -07001164 } else {
Martijn Coenena660cbc2016-05-12 11:29:23 +02001165 error = mContextObject->transact(tr.code, buffer, &reply, tr.flags, reply_callback);
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001166 }
1167
1168 if ((tr.flags & TF_ONE_WAY) == 0) {
1169 if (!reply_sent) {
1170 // Should have been a reply but there wasn't, so there
1171 // must have been an error instead.
1172 reply.setError(error);
1173 sendReply(reply, 0);
1174 } else {
1175 if (error != NO_ERROR) {
1176 ALOGE("transact() returned error after sending reply.");
1177 } else {
1178 // Ok, reply sent and transact didn't return an error.
1179 }
1180 }
1181 } else {
1182 // One-way transaction, don't care about return value or reply.
Mathias Agopian7922fa22009-05-18 15:08:03 -07001183 }
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001184
Steve Block93cf8542012-01-04 20:05:49 +00001185 //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
Mathias Agopian7922fa22009-05-18 15:08:03 -07001186 // mCallingPid, origPid, origUid);
Martijn Coenen79c2f4d2016-05-20 10:55:59 +02001187
Yifan Hongdde40f32017-01-12 14:22:45 -08001188
Mathias Agopian7922fa22009-05-18 15:08:03 -07001189 mCallingPid = origPid;
1190 mCallingUid = origUid;
Dianne Hackbornf99aec62014-09-30 11:30:03 -07001191 mStrictModePolicy = origStrictModePolicy;
1192 mLastTransactionBinderFlags = origTransactionBinderFlags;
Christopher Tate7c4dfec2010-03-18 17:55:03 -07001193
Mathias Agopian7922fa22009-05-18 15:08:03 -07001194 IF_LOG_TRANSACTIONS() {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001195 alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1196 << tr.target.ptr << ": " << indent << reply << dedent << endl;
1197 }
Yifan Hongdde40f32017-01-12 14:22:45 -08001198
Mathias Agopian7922fa22009-05-18 15:08:03 -07001199 }
1200 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001201
Mathias Agopian7922fa22009-05-18 15:08:03 -07001202 case BR_DEAD_BINDER:
1203 {
Yifan Hong1e118d22017-01-12 14:42:28 -08001204 BpHwBinder *proxy = (BpHwBinder*)mIn.readPointer();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001205 proxy->sendObituary();
1206 mOut.writeInt32(BC_DEAD_BINDER_DONE);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001207 mOut.writePointer((uintptr_t)proxy);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001208 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001209
Mathias Agopian7922fa22009-05-18 15:08:03 -07001210 case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1211 {
Yifan Hong1e118d22017-01-12 14:42:28 -08001212 BpHwBinder *proxy = (BpHwBinder*)mIn.readPointer();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001213 proxy->getWeakRefs()->decWeak(proxy);
1214 } break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001215
Mathias Agopian7922fa22009-05-18 15:08:03 -07001216 case BR_FINISHED:
1217 result = TIMED_OUT;
1218 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001219
Mathias Agopian7922fa22009-05-18 15:08:03 -07001220 case BR_NOOP:
1221 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001222
Mathias Agopian7922fa22009-05-18 15:08:03 -07001223 case BR_SPAWN_LOOPER:
1224 mProcess->spawnPooledThread(false);
1225 break;
Yifan Hongdde40f32017-01-12 14:22:45 -08001226
Mathias Agopian7922fa22009-05-18 15:08:03 -07001227 default:
1228 printf("*** BAD COMMAND %d received from Binder driver\n", cmd);
1229 result = UNKNOWN_ERROR;
1230 break;
1231 }
1232
1233 if (result != NO_ERROR) {
1234 mLastError = result;
1235 }
Yifan Hongdde40f32017-01-12 14:22:45 -08001236
Mathias Agopian7922fa22009-05-18 15:08:03 -07001237 return result;
1238}
1239
1240void IPCThreadState::threadDestructor(void *st)
1241{
Todd Poynor0646cb02013-06-25 19:12:18 -07001242 IPCThreadState* const self = static_cast<IPCThreadState*>(st);
1243 if (self) {
1244 self->flushCommands();
Elliott Hughese5e70552015-08-12 15:27:47 -07001245#if defined(__ANDROID__)
Johannes Carlsson597a3c72011-02-17 14:06:53 +01001246 if (self->mProcess->mDriverFD > 0) {
1247 ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
1248 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07001249#endif
Todd Poynor0646cb02013-06-25 19:12:18 -07001250 delete self;
1251 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07001252}
1253
1254
Colin Crossf0487982014-02-05 17:42:44 -08001255void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1256 size_t /*dataSize*/,
1257 const binder_size_t* /*objects*/,
1258 size_t /*objectsSize*/, void* /*cookie*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -07001259{
Steve Block93cf8542012-01-04 20:05:49 +00001260 //ALOGI("Freeing parcel %p", &parcel);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001261 IF_LOG_COMMANDS() {
1262 alog << "Writing BC_FREE_BUFFER for " << data << endl;
1263 }
Steve Blockd0bfabc2012-01-09 18:35:44 +00001264 ALOG_ASSERT(data != NULL, "Called with NULL data");
Mathias Agopian7922fa22009-05-18 15:08:03 -07001265 if (parcel != NULL) parcel->closeFileDescriptors();
1266 IPCThreadState* state = self();
1267 state->mOut.writeInt32(BC_FREE_BUFFER);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001268 state->mOut.writePointer((uintptr_t)data);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001269}
1270
Martijn Coenenf75a23d2016-08-01 11:55:17 +02001271}; // namespace hardware
Mathias Agopian7922fa22009-05-18 15:08:03 -07001272}; // namespace android