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