Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jason Parks | 2b17f14 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 17 | #define LOG_TAG "IPCThreadState" |
| 18 | |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 19 | #include <hwbinder/IPCThreadState.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 20 | |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 21 | #include <hwbinder/Binder.h> |
| 22 | #include <hwbinder/BpBinder.h> |
| 23 | #include <hwbinder/TextOutput.h> |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 24 | |
Glenn Kasten | cb5e242 | 2012-03-16 07:15:23 -0700 | [diff] [blame] | 25 | #include <cutils/sched_policy.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 26 | #include <utils/Log.h> |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 27 | #include <utils/SystemClock.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 28 | #include <utils/threads.h> |
| 29 | |
| 30 | #include <private/binder/binder_module.h> |
Martijn Coenen | e01f4f2 | 2016-05-12 12:33:28 +0200 | [diff] [blame] | 31 | #include <hwbinder/Static.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 32 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 33 | #include <errno.h> |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 34 | #include <inttypes.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 35 | #include <pthread.h> |
| 36 | #include <sched.h> |
Yabin Cui | bbef2ba | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 37 | #include <signal.h> |
| 38 | #include <stdio.h> |
| 39 | #include <sys/ioctl.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 40 | #include <sys/resource.h> |
Yabin Cui | bbef2ba | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 41 | #include <unistd.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 42 | |
| 43 | #if LOG_NDEBUG |
| 44 | |
| 45 | #define IF_LOG_TRANSACTIONS() if (false) |
| 46 | #define IF_LOG_COMMANDS() if (false) |
| 47 | #define LOG_REMOTEREFS(...) |
| 48 | #define IF_LOG_REMOTEREFS() if (false) |
| 49 | #define LOG_THREADPOOL(...) |
| 50 | #define LOG_ONEWAY(...) |
| 51 | |
| 52 | #else |
| 53 | |
Steve Block | 5854b91 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 54 | #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 Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 60 | |
| 61 | #endif |
| 62 | |
| 63 | // --------------------------------------------------------------------------- |
| 64 | |
| 65 | namespace android { |
Martijn Coenen | e01f4f2 | 2016-05-12 12:33:28 +0200 | [diff] [blame] | 66 | namespace hidl { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 67 | |
| 68 | static const char* getReturnString(size_t idx); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 69 | static const void* printReturnCommand(TextOutput& out, const void* _cmd); |
| 70 | static const void* printCommand(TextOutput& out, const void* _cmd); |
| 71 | |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 72 | // Static const and functions will be optimized out if not used, |
| 73 | // when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out. |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 74 | static const char *kReturnStrings[] = { |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 75 | "BR_ERROR", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 76 | "BR_OK", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 77 | "BR_TRANSACTION", |
| 78 | "BR_REPLY", |
| 79 | "BR_ACQUIRE_RESULT", |
| 80 | "BR_DEAD_REPLY", |
| 81 | "BR_TRANSACTION_COMPLETE", |
| 82 | "BR_INCREFS", |
| 83 | "BR_ACQUIRE", |
| 84 | "BR_RELEASE", |
| 85 | "BR_DECREFS", |
| 86 | "BR_ATTEMPT_ACQUIRE", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 87 | "BR_NOOP", |
| 88 | "BR_SPAWN_LOOPER", |
| 89 | "BR_FINISHED", |
| 90 | "BR_DEAD_BINDER", |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 91 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 92 | "BR_FAILED_REPLY" |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | static const char *kCommandStrings[] = { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 96 | "BC_TRANSACTION", |
| 97 | "BC_REPLY", |
| 98 | "BC_ACQUIRE_RESULT", |
| 99 | "BC_FREE_BUFFER", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 100 | "BC_INCREFS", |
| 101 | "BC_ACQUIRE", |
| 102 | "BC_RELEASE", |
| 103 | "BC_DECREFS", |
| 104 | "BC_INCREFS_DONE", |
| 105 | "BC_ACQUIRE_DONE", |
| 106 | "BC_ATTEMPT_ACQUIRE", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 107 | "BC_REGISTER_LOOPER", |
| 108 | "BC_ENTER_LOOPER", |
| 109 | "BC_EXIT_LOOPER", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 110 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 111 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 112 | "BC_DEAD_BINDER_DONE" |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static const char* getReturnString(size_t idx) |
| 116 | { |
| 117 | if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0])) |
| 118 | return kReturnStrings[idx]; |
| 119 | else |
| 120 | return "unknown"; |
| 121 | } |
| 122 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 123 | static const void* printBinderTransactionData(TextOutput& out, const void* data) |
| 124 | { |
| 125 | const binder_transaction_data* btd = |
| 126 | (const binder_transaction_data*)data; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 127 | if (btd->target.handle < 1024) { |
| 128 | /* want to print descriptors in decimal; guess based on value */ |
| 129 | out << "target.desc=" << btd->target.handle; |
| 130 | } else { |
| 131 | out << "target.ptr=" << btd->target.ptr; |
| 132 | } |
| 133 | out << " (cookie " << btd->cookie << ")" << endl |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 134 | << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 135 | << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size |
| 136 | << " bytes)" << endl |
| 137 | << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 138 | << " bytes)"; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 139 | return btd+1; |
| 140 | } |
| 141 | |
| 142 | static const void* printReturnCommand(TextOutput& out, const void* _cmd) |
| 143 | { |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 144 | static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 145 | const int32_t* cmd = (const int32_t*)_cmd; |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 146 | uint32_t code = (uint32_t)*cmd++; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 147 | size_t cmdIndex = code & 0xff; |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 148 | if (code == BR_ERROR) { |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 149 | out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 150 | return cmd; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 151 | } else if (cmdIndex >= N) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 152 | out << "Unknown reply: " << code << endl; |
| 153 | return cmd; |
| 154 | } |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 155 | out << kReturnStrings[cmdIndex]; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 156 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 157 | switch (code) { |
| 158 | case BR_TRANSACTION: |
| 159 | case BR_REPLY: { |
| 160 | out << ": " << indent; |
| 161 | cmd = (const int32_t *)printBinderTransactionData(out, cmd); |
| 162 | out << dedent; |
| 163 | } break; |
| 164 | |
| 165 | case BR_ACQUIRE_RESULT: { |
| 166 | const int32_t res = *cmd++; |
| 167 | out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)"); |
| 168 | } break; |
| 169 | |
| 170 | case BR_INCREFS: |
| 171 | case BR_ACQUIRE: |
| 172 | case BR_RELEASE: |
| 173 | case BR_DECREFS: { |
| 174 | const int32_t b = *cmd++; |
| 175 | const int32_t c = *cmd++; |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 176 | out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")"; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 177 | } break; |
| 178 | |
| 179 | case BR_ATTEMPT_ACQUIRE: { |
| 180 | const int32_t p = *cmd++; |
| 181 | const int32_t b = *cmd++; |
| 182 | const int32_t c = *cmd++; |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 183 | out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 184 | << "), pri=" << p; |
| 185 | } break; |
| 186 | |
| 187 | case BR_DEAD_BINDER: |
| 188 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: { |
| 189 | const int32_t c = *cmd++; |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 190 | out << ": death cookie " << (void*)(long)c; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 191 | } break; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 192 | |
| 193 | default: |
| 194 | // no details to show for: BR_OK, BR_DEAD_REPLY, |
| 195 | // BR_TRANSACTION_COMPLETE, BR_FINISHED |
| 196 | break; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | out << endl; |
| 200 | return cmd; |
| 201 | } |
| 202 | |
| 203 | static const void* printCommand(TextOutput& out, const void* _cmd) |
| 204 | { |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 205 | static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 206 | const int32_t* cmd = (const int32_t*)_cmd; |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 207 | uint32_t code = (uint32_t)*cmd++; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 208 | size_t cmdIndex = code & 0xff; |
| 209 | |
| 210 | if (cmdIndex >= N) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 211 | out << "Unknown command: " << code << endl; |
| 212 | return cmd; |
| 213 | } |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 214 | out << kCommandStrings[cmdIndex]; |
| 215 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 216 | switch (code) { |
| 217 | case BC_TRANSACTION: |
| 218 | case BC_REPLY: { |
| 219 | out << ": " << indent; |
| 220 | cmd = (const int32_t *)printBinderTransactionData(out, cmd); |
| 221 | out << dedent; |
| 222 | } break; |
| 223 | |
| 224 | case BC_ACQUIRE_RESULT: { |
| 225 | const int32_t res = *cmd++; |
| 226 | out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)"); |
| 227 | } break; |
| 228 | |
| 229 | case BC_FREE_BUFFER: { |
| 230 | const int32_t buf = *cmd++; |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 231 | out << ": buffer=" << (void*)(long)buf; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 232 | } break; |
| 233 | |
| 234 | case BC_INCREFS: |
| 235 | case BC_ACQUIRE: |
| 236 | case BC_RELEASE: |
| 237 | case BC_DECREFS: { |
| 238 | const int32_t d = *cmd++; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 239 | out << ": desc=" << d; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 240 | } break; |
| 241 | |
| 242 | case BC_INCREFS_DONE: |
| 243 | case BC_ACQUIRE_DONE: { |
| 244 | const int32_t b = *cmd++; |
| 245 | const int32_t c = *cmd++; |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 246 | out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")"; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 247 | } break; |
| 248 | |
| 249 | case BC_ATTEMPT_ACQUIRE: { |
| 250 | const int32_t p = *cmd++; |
| 251 | const int32_t d = *cmd++; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 252 | out << ": desc=" << d << ", pri=" << p; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 253 | } break; |
| 254 | |
| 255 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 256 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 257 | const int32_t h = *cmd++; |
| 258 | const int32_t c = *cmd++; |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 259 | out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")"; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 260 | } break; |
| 261 | |
| 262 | case BC_DEAD_BINDER_DONE: { |
| 263 | const int32_t c = *cmd++; |
Chih-Hung Hsieh | 30dcad7 | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 264 | out << ": death cookie " << (void*)(long)c; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 265 | } break; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 266 | |
| 267 | default: |
| 268 | // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER, |
| 269 | // BC_EXIT_LOOPER |
| 270 | break; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | out << endl; |
| 274 | return cmd; |
| 275 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 276 | |
| 277 | static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER; |
| 278 | static bool gHaveTLS = false; |
| 279 | static pthread_key_t gTLS = 0; |
| 280 | static bool gShutdown = false; |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 281 | static bool gDisableBackgroundScheduling = false; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 282 | |
| 283 | IPCThreadState* IPCThreadState::self() |
| 284 | { |
| 285 | if (gHaveTLS) { |
| 286 | restart: |
| 287 | const pthread_key_t k = gTLS; |
| 288 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k); |
| 289 | if (st) return st; |
| 290 | return new IPCThreadState; |
| 291 | } |
| 292 | |
Andreas Gampe | 1d5dc2b | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 293 | if (gShutdown) { |
| 294 | ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n"); |
| 295 | return NULL; |
| 296 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 297 | |
| 298 | pthread_mutex_lock(&gTLSMutex); |
| 299 | if (!gHaveTLS) { |
Andreas Gampe | 1d5dc2b | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 300 | int key_create_value = pthread_key_create(&gTLS, threadDestructor); |
| 301 | if (key_create_value != 0) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 302 | pthread_mutex_unlock(&gTLSMutex); |
Andreas Gampe | 1d5dc2b | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 303 | ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n", |
| 304 | strerror(key_create_value)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
| 307 | gHaveTLS = true; |
| 308 | } |
| 309 | pthread_mutex_unlock(&gTLSMutex); |
| 310 | goto restart; |
| 311 | } |
| 312 | |
Brad Fitzpatrick | 7794994 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 313 | IPCThreadState* IPCThreadState::selfOrNull() |
| 314 | { |
| 315 | if (gHaveTLS) { |
| 316 | const pthread_key_t k = gTLS; |
| 317 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k); |
| 318 | return st; |
| 319 | } |
| 320 | return NULL; |
| 321 | } |
| 322 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 323 | void IPCThreadState::shutdown() |
| 324 | { |
| 325 | gShutdown = true; |
| 326 | |
| 327 | if (gHaveTLS) { |
| 328 | // XXX Need to wait for all thread pool threads to exit! |
| 329 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS); |
| 330 | if (st) { |
| 331 | delete st; |
| 332 | pthread_setspecific(gTLS, NULL); |
| 333 | } |
zhongjie | 8e8a025 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 334 | pthread_key_delete(gTLS); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 335 | gHaveTLS = false; |
| 336 | } |
| 337 | } |
| 338 | |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 339 | void IPCThreadState::disableBackgroundScheduling(bool disable) |
| 340 | { |
| 341 | gDisableBackgroundScheduling = disable; |
| 342 | } |
| 343 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 344 | sp<ProcessState> IPCThreadState::process() |
| 345 | { |
| 346 | return mProcess; |
| 347 | } |
| 348 | |
| 349 | status_t IPCThreadState::clearLastError() |
| 350 | { |
| 351 | const status_t err = mLastError; |
| 352 | mLastError = NO_ERROR; |
| 353 | return err; |
| 354 | } |
| 355 | |
Dan Stoza | e8da8a4 | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 356 | pid_t IPCThreadState::getCallingPid() const |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 357 | { |
| 358 | return mCallingPid; |
| 359 | } |
| 360 | |
Dan Stoza | e8da8a4 | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 361 | uid_t IPCThreadState::getCallingUid() const |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 362 | { |
| 363 | return mCallingUid; |
| 364 | } |
| 365 | |
| 366 | int64_t IPCThreadState::clearCallingIdentity() |
| 367 | { |
| 368 | int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid; |
| 369 | clearCaller(); |
| 370 | return token; |
| 371 | } |
| 372 | |
Brad Fitzpatrick | 94c3634 | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 373 | void IPCThreadState::setStrictModePolicy(int32_t policy) |
| 374 | { |
| 375 | mStrictModePolicy = policy; |
| 376 | } |
| 377 | |
Brad Fitzpatrick | 3f4ef59 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 378 | int32_t IPCThreadState::getStrictModePolicy() const |
| 379 | { |
Brad Fitzpatrick | 94c3634 | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 380 | return mStrictModePolicy; |
| 381 | } |
| 382 | |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 383 | void IPCThreadState::setLastTransactionBinderFlags(int32_t flags) |
| 384 | { |
| 385 | mLastTransactionBinderFlags = flags; |
| 386 | } |
| 387 | |
| 388 | int32_t IPCThreadState::getLastTransactionBinderFlags() const |
| 389 | { |
| 390 | return mLastTransactionBinderFlags; |
| 391 | } |
| 392 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 393 | void IPCThreadState::restoreCallingIdentity(int64_t token) |
| 394 | { |
| 395 | mCallingUid = (int)(token>>32); |
| 396 | mCallingPid = (int)token; |
| 397 | } |
| 398 | |
| 399 | void IPCThreadState::clearCaller() |
| 400 | { |
Marco Nelissen | b4f35d0 | 2009-07-17 07:59:17 -0700 | [diff] [blame] | 401 | mCallingPid = getpid(); |
| 402 | mCallingUid = getuid(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void IPCThreadState::flushCommands() |
| 406 | { |
| 407 | if (mProcess->mDriverFD <= 0) |
| 408 | return; |
| 409 | talkWithDriver(false); |
| 410 | } |
| 411 | |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 412 | void IPCThreadState::blockUntilThreadAvailable() |
| 413 | { |
| 414 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 415 | while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) { |
Wale Ogunwale | 319900a | 2015-04-21 12:29:50 -0700 | [diff] [blame] | 416 | ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n", |
| 417 | static_cast<unsigned long>(mProcess->mExecutingThreadsCount), |
| 418 | static_cast<unsigned long>(mProcess->mMaxThreads)); |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 419 | pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock); |
| 420 | } |
| 421 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 422 | } |
| 423 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 424 | status_t IPCThreadState::getAndExecuteCommand() |
| 425 | { |
| 426 | status_t result; |
| 427 | int32_t cmd; |
| 428 | |
| 429 | result = talkWithDriver(); |
| 430 | if (result >= NO_ERROR) { |
| 431 | size_t IN = mIn.dataAvail(); |
| 432 | if (IN < sizeof(int32_t)) return result; |
| 433 | cmd = mIn.readInt32(); |
| 434 | IF_LOG_COMMANDS() { |
| 435 | alog << "Processing top-level Command: " |
| 436 | << getReturnString(cmd) << endl; |
| 437 | } |
| 438 | |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 439 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 440 | mProcess->mExecutingThreadsCount++; |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 441 | if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads && |
| 442 | mProcess->mStarvationStartTimeMs == 0) { |
| 443 | mProcess->mStarvationStartTimeMs = uptimeMillis(); |
| 444 | } |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 445 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 446 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 447 | result = executeCommand(cmd); |
| 448 | |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 449 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 450 | mProcess->mExecutingThreadsCount--; |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 451 | if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads && |
| 452 | mProcess->mStarvationStartTimeMs != 0) { |
| 453 | int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs; |
| 454 | if (starvationTimeMs > 100) { |
| 455 | ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms", |
| 456 | mProcess->mMaxThreads, starvationTimeMs); |
| 457 | } |
| 458 | mProcess->mStarvationStartTimeMs = 0; |
| 459 | } |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 460 | pthread_cond_broadcast(&mProcess->mThreadCountDecrement); |
| 461 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 462 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 463 | // After executing the command, ensure that the thread is returned to the |
| 464 | // foreground cgroup before rejoining the pool. The driver takes care of |
| 465 | // restoring the priority, but doesn't do anything with cgroups so we |
| 466 | // need to take care of that here in userspace. Note that we do make |
| 467 | // sure to go in the foreground after executing a transaction, but |
| 468 | // there are other callbacks into user code that could have changed |
| 469 | // our group so we want to make absolutely sure it is put back. |
| 470 | set_sched_policy(mMyThreadId, SP_FOREGROUND); |
| 471 | } |
| 472 | |
| 473 | return result; |
| 474 | } |
| 475 | |
| 476 | // When we've cleared the incoming command queue, process any pending derefs |
| 477 | void IPCThreadState::processPendingDerefs() |
| 478 | { |
| 479 | if (mIn.dataPosition() >= mIn.dataSize()) { |
| 480 | size_t numPending = mPendingWeakDerefs.size(); |
| 481 | if (numPending > 0) { |
| 482 | for (size_t i = 0; i < numPending; i++) { |
| 483 | RefBase::weakref_type* refs = mPendingWeakDerefs[i]; |
| 484 | refs->decWeak(mProcess.get()); |
| 485 | } |
| 486 | mPendingWeakDerefs.clear(); |
| 487 | } |
| 488 | |
| 489 | numPending = mPendingStrongDerefs.size(); |
| 490 | if (numPending > 0) { |
| 491 | for (size_t i = 0; i < numPending; i++) { |
| 492 | BBinder* obj = mPendingStrongDerefs[i]; |
| 493 | obj->decStrong(mProcess.get()); |
| 494 | } |
| 495 | mPendingStrongDerefs.clear(); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 500 | void IPCThreadState::joinThreadPool(bool isMain) |
| 501 | { |
| 502 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid()); |
| 503 | |
| 504 | mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER); |
| 505 | |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 506 | // This thread may have been spawned by a thread that was in the background |
Glenn Kasten | cb5e242 | 2012-03-16 07:15:23 -0700 | [diff] [blame] | 507 | // scheduling group, so first we will make sure it is in the foreground |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 508 | // one to avoid performing an initial transaction in the background. |
Glenn Kasten | cb5e242 | 2012-03-16 07:15:23 -0700 | [diff] [blame] | 509 | set_sched_policy(mMyThreadId, SP_FOREGROUND); |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 510 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 511 | status_t result; |
| 512 | do { |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 513 | processPendingDerefs(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 514 | // now get the next command to be processed, waiting if necessary |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 515 | result = getAndExecuteCommand(); |
Jason Parks | 2b17f14 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 516 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 517 | if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) { |
| 518 | ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting", |
Jeff Tinker | ee711ec | 2013-06-11 11:30:21 -0700 | [diff] [blame] | 519 | mProcess->mDriverFD, result); |
| 520 | abort(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 523 | // Let this thread exit the thread pool if it is no longer |
| 524 | // needed and it is not the main process thread. |
| 525 | if(result == TIMED_OUT && !isMain) { |
| 526 | break; |
| 527 | } |
| 528 | } while (result != -ECONNREFUSED && result != -EBADF); |
| 529 | |
| 530 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%p\n", |
| 531 | (void*)pthread_self(), getpid(), (void*)result); |
| 532 | |
| 533 | mOut.writeInt32(BC_EXIT_LOOPER); |
| 534 | talkWithDriver(false); |
| 535 | } |
| 536 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 537 | int IPCThreadState::setupPolling(int* fd) |
| 538 | { |
| 539 | if (mProcess->mDriverFD <= 0) { |
| 540 | return -EBADF; |
| 541 | } |
| 542 | |
| 543 | mOut.writeInt32(BC_ENTER_LOOPER); |
| 544 | *fd = mProcess->mDriverFD; |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | status_t IPCThreadState::handlePolledCommands() |
| 549 | { |
| 550 | status_t result; |
| 551 | |
| 552 | do { |
| 553 | result = getAndExecuteCommand(); |
| 554 | } while (mIn.dataPosition() < mIn.dataSize()); |
| 555 | |
| 556 | processPendingDerefs(); |
| 557 | flushCommands(); |
| 558 | return result; |
| 559 | } |
| 560 | |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 561 | void IPCThreadState::stopProcess(bool /*immediate*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 562 | { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 563 | //ALOGI("**** STOPPING PROCESS"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 564 | flushCommands(); |
| 565 | int fd = mProcess->mDriverFD; |
| 566 | mProcess->mDriverFD = -1; |
| 567 | close(fd); |
| 568 | //kill(getpid(), SIGKILL); |
| 569 | } |
| 570 | |
| 571 | status_t IPCThreadState::transact(int32_t handle, |
| 572 | uint32_t code, const Parcel& data, |
| 573 | Parcel* reply, uint32_t flags) |
| 574 | { |
| 575 | status_t err = data.errorCheck(); |
| 576 | |
| 577 | flags |= TF_ACCEPT_FDS; |
| 578 | |
| 579 | IF_LOG_TRANSACTIONS() { |
| 580 | TextOutput::Bundle _b(alog); |
| 581 | alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " |
| 582 | << handle << " / code " << TypeCode(code) << ": " |
| 583 | << indent << data << dedent << endl; |
| 584 | } |
| 585 | |
| 586 | if (err == NO_ERROR) { |
| 587 | LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(), |
| 588 | (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY"); |
| 589 | err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL); |
| 590 | } |
| 591 | |
| 592 | if (err != NO_ERROR) { |
| 593 | if (reply) reply->setError(err); |
| 594 | return (mLastError = err); |
| 595 | } |
| 596 | |
| 597 | if ((flags & TF_ONE_WAY) == 0) { |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 598 | #if 0 |
| 599 | if (code == 4) { // relayout |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 600 | ALOGI(">>>>>> CALLING transaction 4"); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 601 | } else { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 602 | ALOGI(">>>>>> CALLING transaction %d", code); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 603 | } |
| 604 | #endif |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 605 | if (reply) { |
| 606 | err = waitForResponse(reply); |
| 607 | } else { |
| 608 | Parcel fakeReply; |
| 609 | err = waitForResponse(&fakeReply); |
| 610 | } |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 611 | #if 0 |
| 612 | if (code == 4) { // relayout |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 613 | ALOGI("<<<<<< RETURNING transaction 4"); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 614 | } else { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 615 | ALOGI("<<<<<< RETURNING transaction %d", code); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 616 | } |
| 617 | #endif |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 618 | |
| 619 | IF_LOG_TRANSACTIONS() { |
| 620 | TextOutput::Bundle _b(alog); |
| 621 | alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand " |
| 622 | << handle << ": "; |
| 623 | if (reply) alog << indent << *reply << dedent << endl; |
| 624 | else alog << "(none requested)" << endl; |
| 625 | } |
| 626 | } else { |
| 627 | err = waitForResponse(NULL, NULL); |
| 628 | } |
| 629 | |
| 630 | return err; |
| 631 | } |
| 632 | |
| 633 | void IPCThreadState::incStrongHandle(int32_t handle) |
| 634 | { |
| 635 | LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle); |
| 636 | mOut.writeInt32(BC_ACQUIRE); |
| 637 | mOut.writeInt32(handle); |
| 638 | } |
| 639 | |
| 640 | void IPCThreadState::decStrongHandle(int32_t handle) |
| 641 | { |
| 642 | LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle); |
| 643 | mOut.writeInt32(BC_RELEASE); |
| 644 | mOut.writeInt32(handle); |
| 645 | } |
| 646 | |
| 647 | void IPCThreadState::incWeakHandle(int32_t handle) |
| 648 | { |
| 649 | LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle); |
| 650 | mOut.writeInt32(BC_INCREFS); |
| 651 | mOut.writeInt32(handle); |
| 652 | } |
| 653 | |
| 654 | void IPCThreadState::decWeakHandle(int32_t handle) |
| 655 | { |
| 656 | LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle); |
| 657 | mOut.writeInt32(BC_DECREFS); |
| 658 | mOut.writeInt32(handle); |
| 659 | } |
| 660 | |
| 661 | status_t IPCThreadState::attemptIncStrongHandle(int32_t handle) |
| 662 | { |
Arve Hjønnevåg | 304dcae | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 663 | #if HAS_BC_ATTEMPT_ACQUIRE |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 664 | LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 665 | mOut.writeInt32(BC_ATTEMPT_ACQUIRE); |
| 666 | mOut.writeInt32(0); // xxx was thread priority |
| 667 | mOut.writeInt32(handle); |
| 668 | status_t result = UNKNOWN_ERROR; |
| 669 | |
| 670 | waitForResponse(NULL, &result); |
| 671 | |
| 672 | #if LOG_REFCOUNTS |
| 673 | printf("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n", |
| 674 | handle, result == NO_ERROR ? "SUCCESS" : "FAILURE"); |
| 675 | #endif |
| 676 | |
| 677 | return result; |
Arve Hjønnevåg | 304dcae | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 678 | #else |
| 679 | (void)handle; |
| 680 | ALOGE("%s(%d): Not supported\n", __func__, handle); |
| 681 | return INVALID_OPERATION; |
| 682 | #endif |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder) |
| 686 | { |
| 687 | #if LOG_REFCOUNTS |
| 688 | printf("IPCThreadState::expungeHandle(%ld)\n", handle); |
| 689 | #endif |
| 690 | self()->mProcess->expungeHandle(handle, binder); |
| 691 | } |
| 692 | |
| 693 | status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy) |
| 694 | { |
| 695 | mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION); |
| 696 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 697 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 698 | return NO_ERROR; |
| 699 | } |
| 700 | |
| 701 | status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy) |
| 702 | { |
| 703 | mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION); |
| 704 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 705 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 706 | return NO_ERROR; |
| 707 | } |
| 708 | |
| 709 | IPCThreadState::IPCThreadState() |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 710 | : mProcess(ProcessState::self()), |
Elliott Hughes | 07cf48a | 2014-08-18 10:38:38 -0700 | [diff] [blame] | 711 | mMyThreadId(gettid()), |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 712 | mStrictModePolicy(0), |
| 713 | mLastTransactionBinderFlags(0) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 714 | { |
| 715 | pthread_setspecific(gTLS, this); |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 716 | clearCaller(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 717 | mIn.setDataCapacity(256); |
| 718 | mOut.setDataCapacity(256); |
| 719 | } |
| 720 | |
| 721 | IPCThreadState::~IPCThreadState() |
| 722 | { |
| 723 | } |
| 724 | |
| 725 | status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags) |
| 726 | { |
| 727 | status_t err; |
| 728 | status_t statusBuffer; |
| 729 | err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer); |
| 730 | if (err < NO_ERROR) return err; |
| 731 | |
| 732 | return waitForResponse(NULL, NULL); |
| 733 | } |
| 734 | |
| 735 | status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult) |
| 736 | { |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 737 | uint32_t cmd; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 738 | int32_t err; |
| 739 | |
| 740 | while (1) { |
| 741 | if ((err=talkWithDriver()) < NO_ERROR) break; |
| 742 | err = mIn.errorCheck(); |
| 743 | if (err < NO_ERROR) break; |
| 744 | if (mIn.dataAvail() == 0) continue; |
| 745 | |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 746 | cmd = (uint32_t)mIn.readInt32(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 747 | |
| 748 | IF_LOG_COMMANDS() { |
| 749 | alog << "Processing waitForResponse Command: " |
| 750 | << getReturnString(cmd) << endl; |
| 751 | } |
| 752 | |
| 753 | switch (cmd) { |
| 754 | case BR_TRANSACTION_COMPLETE: |
| 755 | if (!reply && !acquireResult) goto finish; |
| 756 | break; |
| 757 | |
| 758 | case BR_DEAD_REPLY: |
| 759 | err = DEAD_OBJECT; |
| 760 | goto finish; |
| 761 | |
| 762 | case BR_FAILED_REPLY: |
| 763 | err = FAILED_TRANSACTION; |
| 764 | goto finish; |
| 765 | |
| 766 | case BR_ACQUIRE_RESULT: |
| 767 | { |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 768 | ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 769 | const int32_t result = mIn.readInt32(); |
| 770 | if (!acquireResult) continue; |
| 771 | *acquireResult = result ? NO_ERROR : INVALID_OPERATION; |
| 772 | } |
| 773 | goto finish; |
| 774 | |
| 775 | case BR_REPLY: |
| 776 | { |
| 777 | binder_transaction_data tr; |
| 778 | err = mIn.read(&tr, sizeof(tr)); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 779 | ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 780 | if (err != NO_ERROR) goto finish; |
| 781 | |
| 782 | if (reply) { |
| 783 | if ((tr.flags & TF_STATUS_CODE) == 0) { |
| 784 | reply->ipcSetDataReference( |
| 785 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 786 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 787 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 788 | tr.offsets_size/sizeof(binder_size_t), |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 789 | freeBuffer, this); |
| 790 | } else { |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 791 | err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 792 | freeBuffer(NULL, |
| 793 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 794 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 795 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 796 | tr.offsets_size/sizeof(binder_size_t), this); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 797 | } |
| 798 | } else { |
| 799 | freeBuffer(NULL, |
| 800 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 801 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 802 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 803 | tr.offsets_size/sizeof(binder_size_t), this); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 804 | continue; |
| 805 | } |
| 806 | } |
| 807 | goto finish; |
| 808 | |
| 809 | default: |
| 810 | err = executeCommand(cmd); |
| 811 | if (err != NO_ERROR) goto finish; |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | finish: |
| 817 | if (err != NO_ERROR) { |
| 818 | if (acquireResult) *acquireResult = err; |
| 819 | if (reply) reply->setError(err); |
| 820 | mLastError = err; |
| 821 | } |
| 822 | |
| 823 | return err; |
| 824 | } |
| 825 | |
| 826 | status_t IPCThreadState::talkWithDriver(bool doReceive) |
| 827 | { |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 828 | if (mProcess->mDriverFD <= 0) { |
| 829 | return -EBADF; |
| 830 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 831 | |
| 832 | binder_write_read bwr; |
| 833 | |
| 834 | // Is the read buffer empty? |
| 835 | const bool needRead = mIn.dataPosition() >= mIn.dataSize(); |
| 836 | |
| 837 | // We don't want to write anything if we are still reading |
| 838 | // from data left in the input buffer and the caller |
| 839 | // has requested to read the next data. |
| 840 | const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0; |
| 841 | |
| 842 | bwr.write_size = outAvail; |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 843 | bwr.write_buffer = (uintptr_t)mOut.data(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 844 | |
| 845 | // This is what we'll read. |
| 846 | if (doReceive && needRead) { |
| 847 | bwr.read_size = mIn.dataCapacity(); |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 848 | bwr.read_buffer = (uintptr_t)mIn.data(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 849 | } else { |
| 850 | bwr.read_size = 0; |
Ben Cheng | 455a70a | 2011-12-01 17:11:32 -0800 | [diff] [blame] | 851 | bwr.read_buffer = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 852 | } |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 853 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 854 | IF_LOG_COMMANDS() { |
| 855 | TextOutput::Bundle _b(alog); |
| 856 | if (outAvail != 0) { |
| 857 | alog << "Sending commands to driver: " << indent; |
| 858 | const void* cmds = (const void*)bwr.write_buffer; |
| 859 | const void* end = ((const uint8_t*)cmds)+bwr.write_size; |
| 860 | alog << HexDump(cmds, bwr.write_size) << endl; |
| 861 | while (cmds < end) cmds = printCommand(alog, cmds); |
| 862 | alog << dedent; |
| 863 | } |
| 864 | alog << "Size of receive buffer: " << bwr.read_size |
| 865 | << ", needRead: " << needRead << ", doReceive: " << doReceive << endl; |
| 866 | } |
| 867 | |
| 868 | // Return immediately if there is nothing to do. |
| 869 | if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 870 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 871 | bwr.write_consumed = 0; |
| 872 | bwr.read_consumed = 0; |
| 873 | status_t err; |
| 874 | do { |
| 875 | IF_LOG_COMMANDS() { |
| 876 | alog << "About to read/write, write size = " << mOut.dataSize() << endl; |
| 877 | } |
Elliott Hughes | e5e7055 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 878 | #if defined(__ANDROID__) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 879 | if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0) |
| 880 | err = NO_ERROR; |
| 881 | else |
| 882 | err = -errno; |
| 883 | #else |
| 884 | err = INVALID_OPERATION; |
| 885 | #endif |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 886 | if (mProcess->mDriverFD <= 0) { |
| 887 | err = -EBADF; |
| 888 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 889 | IF_LOG_COMMANDS() { |
| 890 | alog << "Finished read/write, write size = " << mOut.dataSize() << endl; |
| 891 | } |
| 892 | } while (err == -EINTR); |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 893 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 894 | IF_LOG_COMMANDS() { |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 895 | alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: " |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 896 | << bwr.write_consumed << " (of " << mOut.dataSize() |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 897 | << "), read consumed: " << bwr.read_consumed << endl; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | if (err >= NO_ERROR) { |
| 901 | if (bwr.write_consumed > 0) { |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 902 | if (bwr.write_consumed < mOut.dataSize()) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 903 | mOut.remove(0, bwr.write_consumed); |
| 904 | else |
| 905 | mOut.setDataSize(0); |
| 906 | } |
| 907 | if (bwr.read_consumed > 0) { |
| 908 | mIn.setDataSize(bwr.read_consumed); |
| 909 | mIn.setDataPosition(0); |
| 910 | } |
| 911 | IF_LOG_COMMANDS() { |
| 912 | TextOutput::Bundle _b(alog); |
| 913 | alog << "Remaining data size: " << mOut.dataSize() << endl; |
| 914 | alog << "Received commands from driver: " << indent; |
| 915 | const void* cmds = mIn.data(); |
| 916 | const void* end = mIn.data() + mIn.dataSize(); |
| 917 | alog << HexDump(cmds, mIn.dataSize()) << endl; |
| 918 | while (cmds < end) cmds = printReturnCommand(alog, cmds); |
| 919 | alog << dedent; |
| 920 | } |
| 921 | return NO_ERROR; |
| 922 | } |
| 923 | |
| 924 | return err; |
| 925 | } |
| 926 | |
| 927 | status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags, |
| 928 | int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer) |
| 929 | { |
| 930 | binder_transaction_data tr; |
| 931 | |
Arve Hjønnevåg | ea8e05d | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 932 | tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */ |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 933 | tr.target.handle = handle; |
| 934 | tr.code = code; |
| 935 | tr.flags = binderFlags; |
Evgeniy Stepanov | 68c8a65 | 2011-04-21 14:15:00 +0400 | [diff] [blame] | 936 | tr.cookie = 0; |
| 937 | tr.sender_pid = 0; |
| 938 | tr.sender_euid = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 939 | |
| 940 | const status_t err = data.errorCheck(); |
| 941 | if (err == NO_ERROR) { |
| 942 | tr.data_size = data.ipcDataSize(); |
| 943 | tr.data.ptr.buffer = data.ipcData(); |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 944 | tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 945 | tr.data.ptr.offsets = data.ipcObjects(); |
| 946 | } else if (statusBuffer) { |
| 947 | tr.flags |= TF_STATUS_CODE; |
| 948 | *statusBuffer = err; |
| 949 | tr.data_size = sizeof(status_t); |
Arve Hjønnevåg | 4bdf7e9 | 2014-02-18 21:04:31 -0800 | [diff] [blame] | 950 | tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 951 | tr.offsets_size = 0; |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 952 | tr.data.ptr.offsets = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 953 | } else { |
| 954 | return (mLastError = err); |
| 955 | } |
| 956 | |
| 957 | mOut.writeInt32(cmd); |
| 958 | mOut.write(&tr, sizeof(tr)); |
| 959 | |
| 960 | return NO_ERROR; |
| 961 | } |
| 962 | |
| 963 | sp<BBinder> the_context_object; |
| 964 | |
| 965 | void setTheContextObject(sp<BBinder> obj) |
| 966 | { |
| 967 | the_context_object = obj; |
| 968 | } |
| 969 | |
| 970 | status_t IPCThreadState::executeCommand(int32_t cmd) |
| 971 | { |
| 972 | BBinder* obj; |
| 973 | RefBase::weakref_type* refs; |
| 974 | status_t result = NO_ERROR; |
| 975 | |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 976 | switch ((uint32_t)cmd) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 977 | case BR_ERROR: |
| 978 | result = mIn.readInt32(); |
| 979 | break; |
| 980 | |
| 981 | case BR_OK: |
| 982 | break; |
| 983 | |
| 984 | case BR_ACQUIRE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 985 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 986 | obj = (BBinder*)mIn.readPointer(); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 987 | ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 988 | "BR_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 989 | refs, obj, refs->refBase()); |
| 990 | obj->incStrong(mProcess.get()); |
| 991 | IF_LOG_REMOTEREFS() { |
| 992 | LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj); |
| 993 | obj->printRefs(); |
| 994 | } |
| 995 | mOut.writeInt32(BC_ACQUIRE_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 996 | mOut.writePointer((uintptr_t)refs); |
| 997 | mOut.writePointer((uintptr_t)obj); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 998 | break; |
| 999 | |
| 1000 | case BR_RELEASE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1001 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1002 | obj = (BBinder*)mIn.readPointer(); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1003 | ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1004 | "BR_RELEASE: object %p does not match cookie %p (expected %p)", |
| 1005 | refs, obj, refs->refBase()); |
| 1006 | IF_LOG_REMOTEREFS() { |
| 1007 | LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj); |
| 1008 | obj->printRefs(); |
| 1009 | } |
| 1010 | mPendingStrongDerefs.push(obj); |
| 1011 | break; |
| 1012 | |
| 1013 | case BR_INCREFS: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1014 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1015 | obj = (BBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1016 | refs->incWeak(mProcess.get()); |
| 1017 | mOut.writeInt32(BC_INCREFS_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1018 | mOut.writePointer((uintptr_t)refs); |
| 1019 | mOut.writePointer((uintptr_t)obj); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1020 | break; |
| 1021 | |
| 1022 | case BR_DECREFS: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1023 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1024 | obj = (BBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1025 | // NOTE: This assertion is not valid, because the object may no |
| 1026 | // longer exist (thus the (BBinder*)cast above resulting in a different |
| 1027 | // memory address). |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1028 | //ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1029 | // "BR_DECREFS: object %p does not match cookie %p (expected %p)", |
| 1030 | // refs, obj, refs->refBase()); |
| 1031 | mPendingWeakDerefs.push(refs); |
| 1032 | break; |
| 1033 | |
| 1034 | case BR_ATTEMPT_ACQUIRE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1035 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1036 | obj = (BBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1037 | |
| 1038 | { |
| 1039 | const bool success = refs->attemptIncStrong(mProcess.get()); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1040 | ALOG_ASSERT(success && refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1041 | "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 1042 | refs, obj, refs->refBase()); |
| 1043 | |
| 1044 | mOut.writeInt32(BC_ACQUIRE_RESULT); |
| 1045 | mOut.writeInt32((int32_t)success); |
| 1046 | } |
| 1047 | break; |
| 1048 | |
| 1049 | case BR_TRANSACTION: |
| 1050 | { |
| 1051 | binder_transaction_data tr; |
| 1052 | result = mIn.read(&tr, sizeof(tr)); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1053 | ALOG_ASSERT(result == NO_ERROR, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1054 | "Not enough command data for brTRANSACTION"); |
| 1055 | if (result != NO_ERROR) break; |
| 1056 | |
| 1057 | Parcel buffer; |
| 1058 | buffer.ipcSetDataReference( |
| 1059 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 1060 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1061 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 1062 | tr.offsets_size/sizeof(binder_size_t), freeBuffer, this); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1063 | |
| 1064 | const pid_t origPid = mCallingPid; |
| 1065 | const uid_t origUid = mCallingUid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1066 | const int32_t origStrictModePolicy = mStrictModePolicy; |
| 1067 | const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags; |
| 1068 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1069 | mCallingPid = tr.sender_pid; |
| 1070 | mCallingUid = tr.sender_euid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1071 | mLastTransactionBinderFlags = tr.flags; |
| 1072 | |
Christopher Tate | 7c4dfec | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1073 | int curPrio = getpriority(PRIO_PROCESS, mMyThreadId); |
| 1074 | if (gDisableBackgroundScheduling) { |
| 1075 | if (curPrio > ANDROID_PRIORITY_NORMAL) { |
| 1076 | // We have inherited a reduced priority from the caller, but do not |
| 1077 | // want to run in that state in this process. The driver set our |
| 1078 | // priority already (though not our scheduling class), so bounce |
| 1079 | // it back to the default before invoking the transaction. |
| 1080 | setpriority(PRIO_PROCESS, mMyThreadId, ANDROID_PRIORITY_NORMAL); |
| 1081 | } |
| 1082 | } else { |
| 1083 | if (curPrio >= ANDROID_PRIORITY_BACKGROUND) { |
| 1084 | // We want to use the inherited priority from the caller. |
| 1085 | // Ensure this thread is in the background scheduling class, |
| 1086 | // since the driver won't modify scheduling classes for us. |
| 1087 | // The scheduling group is reset to default by the caller |
| 1088 | // once this method returns after the transaction is complete. |
Glenn Kasten | cb5e242 | 2012-03-16 07:15:23 -0700 | [diff] [blame] | 1089 | set_sched_policy(mMyThreadId, SP_BACKGROUND); |
Christopher Tate | 7c4dfec | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1090 | } |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 1091 | } |
Christopher Tate | 7c4dfec | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1092 | |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1093 | //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid); |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1094 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1095 | Parcel reply; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1096 | status_t error; |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame^] | 1097 | bool reply_sent = false; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1098 | IF_LOG_TRANSACTIONS() { |
| 1099 | TextOutput::Bundle _b(alog); |
| 1100 | alog << "BR_TRANSACTION thr " << (void*)pthread_self() |
| 1101 | << " / obj " << tr.target.ptr << " / code " |
| 1102 | << TypeCode(tr.code) << ": " << indent << buffer |
| 1103 | << dedent << endl |
| 1104 | << "Data addr = " |
| 1105 | << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer) |
| 1106 | << ", offsets addr=" |
| 1107 | << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl; |
| 1108 | } |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame^] | 1109 | |
| 1110 | auto reply_callback = [&] (auto &replyParcel) { |
| 1111 | if (reply_sent) { |
| 1112 | // Reply was sent earlier, ignore it. |
| 1113 | ALOGE("Dropping binder reply, it was sent already."); |
| 1114 | return; |
| 1115 | } |
| 1116 | reply_sent = true; |
| 1117 | if ((tr.flags & TF_ONE_WAY) == 0) { |
| 1118 | replyParcel.setError(NO_ERROR); |
| 1119 | sendReply(replyParcel, 0); |
| 1120 | } else { |
| 1121 | ALOGE("Not sending reply in one-way transaction"); |
| 1122 | } |
| 1123 | }; |
| 1124 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1125 | if (tr.target.ptr) { |
Dianne Hackborn | 839f707 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1126 | // We only have a weak reference on the target object, so we must first try to |
| 1127 | // safely acquire a strong reference before doing anything else with it. |
| 1128 | if (reinterpret_cast<RefBase::weakref_type*>( |
| 1129 | tr.target.ptr)->attemptIncStrong(this)) { |
| 1130 | error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer, |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame^] | 1131 | &reply, tr.flags, reply_callback); |
Dianne Hackborn | 839f707 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1132 | reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this); |
| 1133 | } else { |
| 1134 | error = UNKNOWN_TRANSACTION; |
| 1135 | } |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 1136 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1137 | } else { |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame^] | 1138 | error = the_context_object->transact(tr.code, buffer, &reply, tr.flags, |
| 1139 | reply_callback); |
| 1140 | } |
| 1141 | |
| 1142 | if ((tr.flags & TF_ONE_WAY) == 0) { |
| 1143 | if (!reply_sent) { |
| 1144 | // Should have been a reply but there wasn't, so there |
| 1145 | // must have been an error instead. |
| 1146 | reply.setError(error); |
| 1147 | sendReply(reply, 0); |
| 1148 | } else { |
| 1149 | if (error != NO_ERROR) { |
| 1150 | ALOGE("transact() returned error after sending reply."); |
| 1151 | } else { |
| 1152 | // Ok, reply sent and transact didn't return an error. |
| 1153 | } |
| 1154 | } |
| 1155 | } else { |
| 1156 | // One-way transaction, don't care about return value or reply. |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1157 | } |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1158 | |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1159 | //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1160 | // mCallingPid, origPid, origUid); |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame^] | 1161 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1162 | |
| 1163 | mCallingPid = origPid; |
| 1164 | mCallingUid = origUid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1165 | mStrictModePolicy = origStrictModePolicy; |
| 1166 | mLastTransactionBinderFlags = origTransactionBinderFlags; |
Christopher Tate | 7c4dfec | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1167 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1168 | IF_LOG_TRANSACTIONS() { |
| 1169 | TextOutput::Bundle _b(alog); |
| 1170 | alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj " |
| 1171 | << tr.target.ptr << ": " << indent << reply << dedent << endl; |
| 1172 | } |
| 1173 | |
| 1174 | } |
| 1175 | break; |
| 1176 | |
| 1177 | case BR_DEAD_BINDER: |
| 1178 | { |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1179 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1180 | proxy->sendObituary(); |
| 1181 | mOut.writeInt32(BC_DEAD_BINDER_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1182 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1183 | } break; |
| 1184 | |
| 1185 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: |
| 1186 | { |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1187 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1188 | proxy->getWeakRefs()->decWeak(proxy); |
| 1189 | } break; |
| 1190 | |
| 1191 | case BR_FINISHED: |
| 1192 | result = TIMED_OUT; |
| 1193 | break; |
| 1194 | |
| 1195 | case BR_NOOP: |
| 1196 | break; |
| 1197 | |
| 1198 | case BR_SPAWN_LOOPER: |
| 1199 | mProcess->spawnPooledThread(false); |
| 1200 | break; |
| 1201 | |
| 1202 | default: |
| 1203 | printf("*** BAD COMMAND %d received from Binder driver\n", cmd); |
| 1204 | result = UNKNOWN_ERROR; |
| 1205 | break; |
| 1206 | } |
| 1207 | |
| 1208 | if (result != NO_ERROR) { |
| 1209 | mLastError = result; |
| 1210 | } |
| 1211 | |
| 1212 | return result; |
| 1213 | } |
| 1214 | |
| 1215 | void IPCThreadState::threadDestructor(void *st) |
| 1216 | { |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1217 | IPCThreadState* const self = static_cast<IPCThreadState*>(st); |
| 1218 | if (self) { |
| 1219 | self->flushCommands(); |
Elliott Hughes | e5e7055 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1220 | #if defined(__ANDROID__) |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1221 | if (self->mProcess->mDriverFD > 0) { |
| 1222 | ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0); |
| 1223 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1224 | #endif |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1225 | delete self; |
| 1226 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1230 | void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data, |
| 1231 | size_t /*dataSize*/, |
| 1232 | const binder_size_t* /*objects*/, |
| 1233 | size_t /*objectsSize*/, void* /*cookie*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1234 | { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1235 | //ALOGI("Freeing parcel %p", &parcel); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1236 | IF_LOG_COMMANDS() { |
| 1237 | alog << "Writing BC_FREE_BUFFER for " << data << endl; |
| 1238 | } |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1239 | ALOG_ASSERT(data != NULL, "Called with NULL data"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1240 | if (parcel != NULL) parcel->closeFileDescriptors(); |
| 1241 | IPCThreadState* state = self(); |
| 1242 | state->mOut.writeInt32(BC_FREE_BUFFER); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1243 | state->mOut.writePointer((uintptr_t)data); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Martijn Coenen | e01f4f2 | 2016-05-12 12:33:28 +0200 | [diff] [blame] | 1246 | }; // namespace hidl |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1247 | }; // namespace android |