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); |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 406 | // The flush could have caused post-write refcount decrements to have |
| 407 | // been executed, which in turn could result in BC_RELEASE/BC_DECREFS |
| 408 | // being queued in mOut. So flush again, if we need to. |
| 409 | if (mOut.dataSize() > 0) { |
| 410 | talkWithDriver(false); |
| 411 | } |
| 412 | if (mOut.dataSize() > 0) { |
| 413 | ALOGW("mOut.dataSize() > 0 after flushCommands()"); |
| 414 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 417 | void IPCThreadState::blockUntilThreadAvailable() |
| 418 | { |
| 419 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 420 | while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) { |
Wale Ogunwale | 319900a | 2015-04-21 12:29:50 -0700 | [diff] [blame] | 421 | ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n", |
| 422 | static_cast<unsigned long>(mProcess->mExecutingThreadsCount), |
| 423 | static_cast<unsigned long>(mProcess->mMaxThreads)); |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 424 | pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock); |
| 425 | } |
| 426 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 427 | } |
| 428 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 429 | status_t IPCThreadState::getAndExecuteCommand() |
| 430 | { |
| 431 | status_t result; |
| 432 | int32_t cmd; |
| 433 | |
| 434 | result = talkWithDriver(); |
| 435 | if (result >= NO_ERROR) { |
| 436 | size_t IN = mIn.dataAvail(); |
| 437 | if (IN < sizeof(int32_t)) return result; |
| 438 | cmd = mIn.readInt32(); |
| 439 | IF_LOG_COMMANDS() { |
| 440 | alog << "Processing top-level Command: " |
| 441 | << getReturnString(cmd) << endl; |
| 442 | } |
| 443 | |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 444 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 445 | mProcess->mExecutingThreadsCount++; |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 446 | if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads && |
Martijn Coenen | 0ce07ce | 2017-07-14 15:37:25 +0200 | [diff] [blame] | 447 | mProcess->mMaxThreads > 1 && mProcess->mStarvationStartTimeMs == 0) { |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 448 | mProcess->mStarvationStartTimeMs = uptimeMillis(); |
| 449 | } |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 450 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 451 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 452 | result = executeCommand(cmd); |
| 453 | |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 454 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 455 | mProcess->mExecutingThreadsCount--; |
Wei Wang | df7f40d | 2018-03-29 15:41:59 -0700 | [diff] [blame] | 456 | if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads && |
Martijn Coenen | 0ce07ce | 2017-07-14 15:37:25 +0200 | [diff] [blame] | 457 | mProcess->mStarvationStartTimeMs != 0) { |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 458 | int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs; |
| 459 | if (starvationTimeMs > 100) { |
Wei Wang | df7f40d | 2018-03-29 15:41:59 -0700 | [diff] [blame] | 460 | // If there is only a single-threaded client, nobody would be blocked |
| 461 | // on this, and it's not really starvation. (see b/37647467) |
| 462 | ALOGW("All binder threads in pool (%zu threads) busy for %" PRId64 " ms%s", |
| 463 | mProcess->mMaxThreads, starvationTimeMs, |
| 464 | mProcess->mMaxThreads > 1 ? "" : " (may be a false alarm)"); |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 465 | } |
| 466 | mProcess->mStarvationStartTimeMs = 0; |
| 467 | } |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 468 | pthread_cond_broadcast(&mProcess->mThreadCountDecrement); |
| 469 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return result; |
| 473 | } |
| 474 | |
| 475 | // When we've cleared the incoming command queue, process any pending derefs |
| 476 | void IPCThreadState::processPendingDerefs() |
| 477 | { |
| 478 | if (mIn.dataPosition() >= mIn.dataSize()) { |
Martijn Coenen | c9f105b | 2017-08-08 15:36:16 +0200 | [diff] [blame] | 479 | /* |
| 480 | * The decWeak()/decStrong() calls may cause a destructor to run, |
| 481 | * which in turn could have initiated an outgoing transaction, |
| 482 | * which in turn could cause us to add to the pending refs |
| 483 | * vectors; so instead of simply iterating, loop until they're empty. |
| 484 | * |
| 485 | * We do this in an outer loop, because calling decStrong() |
| 486 | * may result in something being added to mPendingWeakDerefs, |
| 487 | * which could be delayed until the next incoming command |
| 488 | * from the driver if we don't process it now. |
| 489 | */ |
| 490 | while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) { |
| 491 | while (mPendingWeakDerefs.size() > 0) { |
| 492 | RefBase::weakref_type* refs = mPendingWeakDerefs[0]; |
| 493 | mPendingWeakDerefs.removeAt(0); |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 494 | refs->decWeak(mProcess.get()); |
| 495 | } |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 496 | |
Martijn Coenen | c9f105b | 2017-08-08 15:36:16 +0200 | [diff] [blame] | 497 | if (mPendingStrongDerefs.size() > 0) { |
| 498 | // We don't use while() here because we don't want to re-order |
| 499 | // strong and weak decs at all; if this decStrong() causes both a |
| 500 | // decWeak() and a decStrong() to be queued, we want to process |
| 501 | // the decWeak() first. |
| 502 | BHwBinder* obj = mPendingStrongDerefs[0]; |
| 503 | mPendingStrongDerefs.removeAt(0); |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 504 | obj->decStrong(mProcess.get()); |
| 505 | } |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 510 | void IPCThreadState::processPostWriteDerefs() |
| 511 | { |
| 512 | /* |
| 513 | * libhwbinder has a flushCommands() in the BpHwBinder destructor, |
| 514 | * which makes this function (potentially) reentrant. |
| 515 | * New entries shouldn't be added though, so just iterating until empty |
| 516 | * should be safe. |
| 517 | */ |
| 518 | while (mPostWriteWeakDerefs.size() > 0) { |
| 519 | RefBase::weakref_type* refs = mPostWriteWeakDerefs[0]; |
| 520 | mPostWriteWeakDerefs.removeAt(0); |
| 521 | refs->decWeak(mProcess.get()); |
| 522 | } |
| 523 | |
| 524 | while (mPostWriteStrongDerefs.size() > 0) { |
| 525 | RefBase* obj = mPostWriteStrongDerefs[0]; |
| 526 | mPostWriteStrongDerefs.removeAt(0); |
| 527 | obj->decStrong(mProcess.get()); |
| 528 | } |
| 529 | } |
| 530 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 531 | void IPCThreadState::joinThreadPool(bool isMain) |
| 532 | { |
| 533 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid()); |
| 534 | |
| 535 | mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 536 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 537 | status_t result; |
Martijn Coenen | 420d4bb | 2017-10-24 11:43:55 +0200 | [diff] [blame] | 538 | mIsLooper = true; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 539 | do { |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 540 | processPendingDerefs(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 541 | // now get the next command to be processed, waiting if necessary |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 542 | result = getAndExecuteCommand(); |
Jason Parks | 2b17f14 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 543 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 544 | if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) { |
| 545 | ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting", |
Jeff Tinker | ee711ec | 2013-06-11 11:30:21 -0700 | [diff] [blame] | 546 | mProcess->mDriverFD, result); |
| 547 | abort(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 548 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 549 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 550 | // Let this thread exit the thread pool if it is no longer |
| 551 | // needed and it is not the main process thread. |
| 552 | if(result == TIMED_OUT && !isMain) { |
| 553 | break; |
| 554 | } |
| 555 | } while (result != -ECONNREFUSED && result != -EBADF); |
| 556 | |
Wei Wang | 8a2e8ac | 2016-10-14 09:54:27 -0700 | [diff] [blame] | 557 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n", |
| 558 | (void*)pthread_self(), getpid(), result); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 559 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 560 | mOut.writeInt32(BC_EXIT_LOOPER); |
Martijn Coenen | 420d4bb | 2017-10-24 11:43:55 +0200 | [diff] [blame] | 561 | mIsLooper = false; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 562 | talkWithDriver(false); |
| 563 | } |
| 564 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 565 | int IPCThreadState::setupPolling(int* fd) |
| 566 | { |
| 567 | if (mProcess->mDriverFD <= 0) { |
| 568 | return -EBADF; |
| 569 | } |
| 570 | |
Martijn Coenen | 80b88ab | 2017-09-25 14:50:05 +0200 | [diff] [blame] | 571 | // Tells the kernel to not spawn any additional binder threads, |
| 572 | // as that won't work with polling. Also, the caller is responsible |
| 573 | // for subsequently calling handlePolledCommands() |
| 574 | mProcess->setThreadPoolConfiguration(1, true /* callerWillJoin */); |
Tobias Lindskog | a36d576 | 2018-01-05 10:28:31 +0100 | [diff] [blame] | 575 | mIsPollingThread = true; |
Martijn Coenen | 80b88ab | 2017-09-25 14:50:05 +0200 | [diff] [blame] | 576 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 577 | mOut.writeInt32(BC_ENTER_LOOPER); |
| 578 | *fd = mProcess->mDriverFD; |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | status_t IPCThreadState::handlePolledCommands() |
| 583 | { |
| 584 | status_t result; |
| 585 | |
| 586 | do { |
| 587 | result = getAndExecuteCommand(); |
| 588 | } while (mIn.dataPosition() < mIn.dataSize()); |
| 589 | |
| 590 | processPendingDerefs(); |
| 591 | flushCommands(); |
| 592 | return result; |
| 593 | } |
| 594 | |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 595 | void IPCThreadState::stopProcess(bool /*immediate*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 596 | { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 597 | //ALOGI("**** STOPPING PROCESS"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 598 | flushCommands(); |
| 599 | int fd = mProcess->mDriverFD; |
| 600 | mProcess->mDriverFD = -1; |
| 601 | close(fd); |
| 602 | //kill(getpid(), SIGKILL); |
| 603 | } |
| 604 | |
| 605 | status_t IPCThreadState::transact(int32_t handle, |
| 606 | uint32_t code, const Parcel& data, |
| 607 | Parcel* reply, uint32_t flags) |
| 608 | { |
Ganesh Mahendran | ce45b89 | 2017-10-11 18:05:13 +0800 | [diff] [blame] | 609 | status_t err; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 610 | |
| 611 | flags |= TF_ACCEPT_FDS; |
| 612 | |
| 613 | IF_LOG_TRANSACTIONS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 614 | alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " |
| 615 | << handle << " / code " << TypeCode(code) << ": " |
| 616 | << indent << data << dedent << endl; |
| 617 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 618 | |
Ganesh Mahendran | ce45b89 | 2017-10-11 18:05:13 +0800 | [diff] [blame] | 619 | LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(), |
| 620 | (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY"); |
| 621 | err = writeTransactionData(BC_TRANSACTION_SG, flags, handle, code, data, NULL); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 622 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 623 | if (err != NO_ERROR) { |
| 624 | if (reply) reply->setError(err); |
| 625 | return (mLastError = err); |
| 626 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 627 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 628 | if ((flags & TF_ONE_WAY) == 0) { |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 629 | #if 0 |
| 630 | if (code == 4) { // relayout |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 631 | ALOGI(">>>>>> CALLING transaction 4"); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 632 | } else { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 633 | ALOGI(">>>>>> CALLING transaction %d", code); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 634 | } |
| 635 | #endif |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 636 | if (reply) { |
| 637 | err = waitForResponse(reply); |
| 638 | } else { |
| 639 | Parcel fakeReply; |
| 640 | err = waitForResponse(&fakeReply); |
| 641 | } |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 642 | #if 0 |
| 643 | if (code == 4) { // relayout |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 644 | ALOGI("<<<<<< RETURNING transaction 4"); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 645 | } else { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 646 | ALOGI("<<<<<< RETURNING transaction %d", code); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 647 | } |
| 648 | #endif |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 649 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 650 | IF_LOG_TRANSACTIONS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 651 | alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand " |
| 652 | << handle << ": "; |
| 653 | if (reply) alog << indent << *reply << dedent << endl; |
| 654 | else alog << "(none requested)" << endl; |
| 655 | } |
| 656 | } else { |
| 657 | err = waitForResponse(NULL, NULL); |
| 658 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 659 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 660 | return err; |
| 661 | } |
| 662 | |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 663 | void IPCThreadState::incStrongHandle(int32_t handle, BpHwBinder *proxy) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 664 | { |
| 665 | LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle); |
| 666 | mOut.writeInt32(BC_ACQUIRE); |
| 667 | mOut.writeInt32(handle); |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 668 | // Create a temp reference until the driver has handled this command. |
| 669 | proxy->incStrong(mProcess.get()); |
| 670 | mPostWriteStrongDerefs.push(proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | void IPCThreadState::decStrongHandle(int32_t handle) |
| 674 | { |
| 675 | LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle); |
| 676 | mOut.writeInt32(BC_RELEASE); |
| 677 | mOut.writeInt32(handle); |
| 678 | } |
| 679 | |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 680 | void IPCThreadState::incWeakHandle(int32_t handle, BpHwBinder *proxy) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 681 | { |
| 682 | LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle); |
| 683 | mOut.writeInt32(BC_INCREFS); |
| 684 | mOut.writeInt32(handle); |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 685 | // Create a temp reference until the driver has handled this command. |
| 686 | proxy->getWeakRefs()->incWeak(mProcess.get()); |
| 687 | mPostWriteWeakDerefs.push(proxy->getWeakRefs()); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | void IPCThreadState::decWeakHandle(int32_t handle) |
| 691 | { |
| 692 | LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle); |
| 693 | mOut.writeInt32(BC_DECREFS); |
| 694 | mOut.writeInt32(handle); |
| 695 | } |
| 696 | |
| 697 | status_t IPCThreadState::attemptIncStrongHandle(int32_t handle) |
| 698 | { |
Arve Hjønnevåg | 304dcae | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 699 | #if HAS_BC_ATTEMPT_ACQUIRE |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 700 | LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 701 | mOut.writeInt32(BC_ATTEMPT_ACQUIRE); |
| 702 | mOut.writeInt32(0); // xxx was thread priority |
| 703 | mOut.writeInt32(handle); |
| 704 | status_t result = UNKNOWN_ERROR; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 705 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 706 | waitForResponse(NULL, &result); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 707 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 708 | #if LOG_REFCOUNTS |
| 709 | printf("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n", |
| 710 | handle, result == NO_ERROR ? "SUCCESS" : "FAILURE"); |
| 711 | #endif |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 712 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 713 | return result; |
Arve Hjønnevåg | 304dcae | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 714 | #else |
| 715 | (void)handle; |
| 716 | ALOGE("%s(%d): Not supported\n", __func__, handle); |
| 717 | return INVALID_OPERATION; |
| 718 | #endif |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder) |
| 722 | { |
| 723 | #if LOG_REFCOUNTS |
| 724 | printf("IPCThreadState::expungeHandle(%ld)\n", handle); |
| 725 | #endif |
Yunlian Jiang | 6b65ac3 | 2017-09-19 15:51:07 -0700 | [diff] [blame] | 726 | self()->mProcess->expungeHandle(handle, binder); // NOLINT |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 729 | status_t IPCThreadState::requestDeathNotification(int32_t handle, BpHwBinder* proxy) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 730 | { |
| 731 | mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION); |
| 732 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 733 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 734 | return NO_ERROR; |
| 735 | } |
| 736 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 737 | status_t IPCThreadState::clearDeathNotification(int32_t handle, BpHwBinder* proxy) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 738 | { |
| 739 | mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION); |
| 740 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 741 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 742 | return NO_ERROR; |
| 743 | } |
| 744 | |
| 745 | IPCThreadState::IPCThreadState() |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 746 | : mProcess(ProcessState::self()), |
Elliott Hughes | 07cf48a | 2014-08-18 10:38:38 -0700 | [diff] [blame] | 747 | mMyThreadId(gettid()), |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 748 | mStrictModePolicy(0), |
Martijn Coenen | 9bd3d3b | 2017-12-12 09:29:14 +0100 | [diff] [blame] | 749 | mLastTransactionBinderFlags(0), |
Tobias Lindskog | a36d576 | 2018-01-05 10:28:31 +0100 | [diff] [blame] | 750 | mIsLooper(false), |
| 751 | mIsPollingThread(false) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 752 | pthread_setspecific(gTLS, this); |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 753 | clearCaller(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 754 | mIn.setDataCapacity(256); |
| 755 | mOut.setDataCapacity(256); |
Chih-Hung Hsieh | 29dbdcf | 2017-10-02 10:42:37 -0700 | [diff] [blame] | 756 | |
| 757 | // TODO(b/67742352): remove this variable from the class |
| 758 | (void)mMyThreadId; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | IPCThreadState::~IPCThreadState() |
| 762 | { |
| 763 | } |
| 764 | |
| 765 | status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags) |
| 766 | { |
| 767 | status_t err; |
| 768 | status_t statusBuffer; |
Martijn Coenen | d39a168 | 2016-06-03 21:27:28 +0200 | [diff] [blame] | 769 | err = writeTransactionData(BC_REPLY_SG, flags, -1, 0, reply, &statusBuffer); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 770 | if (err < NO_ERROR) return err; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 771 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 772 | return waitForResponse(NULL, NULL); |
| 773 | } |
| 774 | |
| 775 | status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult) |
| 776 | { |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 777 | uint32_t cmd; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 778 | int32_t err; |
| 779 | |
| 780 | while (1) { |
| 781 | if ((err=talkWithDriver()) < NO_ERROR) break; |
| 782 | err = mIn.errorCheck(); |
| 783 | if (err < NO_ERROR) break; |
| 784 | if (mIn.dataAvail() == 0) continue; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 785 | |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 786 | cmd = (uint32_t)mIn.readInt32(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 787 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 788 | IF_LOG_COMMANDS() { |
| 789 | alog << "Processing waitForResponse Command: " |
| 790 | << getReturnString(cmd) << endl; |
| 791 | } |
| 792 | |
| 793 | switch (cmd) { |
| 794 | case BR_TRANSACTION_COMPLETE: |
| 795 | if (!reply && !acquireResult) goto finish; |
| 796 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 797 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 798 | case BR_DEAD_REPLY: |
| 799 | err = DEAD_OBJECT; |
| 800 | goto finish; |
| 801 | |
| 802 | case BR_FAILED_REPLY: |
| 803 | err = FAILED_TRANSACTION; |
| 804 | goto finish; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 805 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 806 | case BR_ACQUIRE_RESULT: |
| 807 | { |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 808 | ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 809 | const int32_t result = mIn.readInt32(); |
| 810 | if (!acquireResult) continue; |
| 811 | *acquireResult = result ? NO_ERROR : INVALID_OPERATION; |
| 812 | } |
| 813 | goto finish; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 814 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 815 | case BR_REPLY: |
| 816 | { |
| 817 | binder_transaction_data tr; |
| 818 | err = mIn.read(&tr, sizeof(tr)); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 819 | ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 820 | if (err != NO_ERROR) goto finish; |
| 821 | |
| 822 | if (reply) { |
| 823 | if ((tr.flags & TF_STATUS_CODE) == 0) { |
| 824 | reply->ipcSetDataReference( |
| 825 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 826 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 827 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 828 | tr.offsets_size/sizeof(binder_size_t), |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 829 | freeBuffer, this); |
| 830 | } else { |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 831 | err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 832 | freeBuffer(NULL, |
| 833 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 834 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 835 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 836 | tr.offsets_size/sizeof(binder_size_t), this); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 837 | } |
| 838 | } else { |
| 839 | freeBuffer(NULL, |
| 840 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 841 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 842 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 843 | tr.offsets_size/sizeof(binder_size_t), this); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 844 | continue; |
| 845 | } |
| 846 | } |
| 847 | goto finish; |
| 848 | |
| 849 | default: |
| 850 | err = executeCommand(cmd); |
| 851 | if (err != NO_ERROR) goto finish; |
| 852 | break; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | finish: |
| 857 | if (err != NO_ERROR) { |
| 858 | if (acquireResult) *acquireResult = err; |
| 859 | if (reply) reply->setError(err); |
| 860 | mLastError = err; |
| 861 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 862 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 863 | return err; |
| 864 | } |
| 865 | |
| 866 | status_t IPCThreadState::talkWithDriver(bool doReceive) |
| 867 | { |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 868 | if (mProcess->mDriverFD <= 0) { |
| 869 | return -EBADF; |
| 870 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 871 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 872 | binder_write_read bwr; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 873 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 874 | // Is the read buffer empty? |
| 875 | const bool needRead = mIn.dataPosition() >= mIn.dataSize(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 876 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 877 | // We don't want to write anything if we are still reading |
| 878 | // from data left in the input buffer and the caller |
| 879 | // has requested to read the next data. |
| 880 | const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 881 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 882 | bwr.write_size = outAvail; |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 883 | bwr.write_buffer = (uintptr_t)mOut.data(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 884 | |
| 885 | // This is what we'll read. |
| 886 | if (doReceive && needRead) { |
| 887 | bwr.read_size = mIn.dataCapacity(); |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 888 | bwr.read_buffer = (uintptr_t)mIn.data(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 889 | } else { |
| 890 | bwr.read_size = 0; |
Ben Cheng | 455a70a | 2011-12-01 17:11:32 -0800 | [diff] [blame] | 891 | bwr.read_buffer = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 892 | } |
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() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 895 | if (outAvail != 0) { |
| 896 | alog << "Sending commands to driver: " << indent; |
| 897 | const void* cmds = (const void*)bwr.write_buffer; |
| 898 | const void* end = ((const uint8_t*)cmds)+bwr.write_size; |
| 899 | alog << HexDump(cmds, bwr.write_size) << endl; |
| 900 | while (cmds < end) cmds = printCommand(alog, cmds); |
| 901 | alog << dedent; |
| 902 | } |
| 903 | alog << "Size of receive buffer: " << bwr.read_size |
| 904 | << ", needRead: " << needRead << ", doReceive: " << doReceive << endl; |
| 905 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 906 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 907 | // Return immediately if there is nothing to do. |
| 908 | if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 909 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 910 | bwr.write_consumed = 0; |
| 911 | bwr.read_consumed = 0; |
| 912 | status_t err; |
| 913 | do { |
| 914 | IF_LOG_COMMANDS() { |
| 915 | alog << "About to read/write, write size = " << mOut.dataSize() << endl; |
| 916 | } |
Elliott Hughes | e5e7055 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 917 | #if defined(__ANDROID__) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 918 | if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0) |
| 919 | err = NO_ERROR; |
| 920 | else |
| 921 | err = -errno; |
| 922 | #else |
| 923 | err = INVALID_OPERATION; |
| 924 | #endif |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 925 | if (mProcess->mDriverFD <= 0) { |
| 926 | err = -EBADF; |
| 927 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 928 | IF_LOG_COMMANDS() { |
| 929 | alog << "Finished read/write, write size = " << mOut.dataSize() << endl; |
| 930 | } |
| 931 | } while (err == -EINTR); |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 932 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 933 | IF_LOG_COMMANDS() { |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 934 | alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: " |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 935 | << bwr.write_consumed << " (of " << mOut.dataSize() |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 936 | << "), read consumed: " << bwr.read_consumed << endl; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | if (err >= NO_ERROR) { |
| 940 | if (bwr.write_consumed > 0) { |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 941 | if (bwr.write_consumed < mOut.dataSize()) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 942 | mOut.remove(0, bwr.write_consumed); |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 943 | else { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 944 | mOut.setDataSize(0); |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 945 | processPostWriteDerefs(); |
| 946 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 947 | } |
| 948 | if (bwr.read_consumed > 0) { |
| 949 | mIn.setDataSize(bwr.read_consumed); |
| 950 | mIn.setDataPosition(0); |
| 951 | } |
| 952 | IF_LOG_COMMANDS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 953 | alog << "Remaining data size: " << mOut.dataSize() << endl; |
| 954 | alog << "Received commands from driver: " << indent; |
| 955 | const void* cmds = mIn.data(); |
| 956 | const void* end = mIn.data() + mIn.dataSize(); |
| 957 | alog << HexDump(cmds, mIn.dataSize()) << endl; |
| 958 | while (cmds < end) cmds = printReturnCommand(alog, cmds); |
| 959 | alog << dedent; |
| 960 | } |
| 961 | return NO_ERROR; |
| 962 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 963 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 964 | return err; |
| 965 | } |
| 966 | |
| 967 | status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags, |
| 968 | int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer) |
| 969 | { |
Martijn Coenen | fd51ebb | 2016-07-05 17:00:39 +0200 | [diff] [blame] | 970 | binder_transaction_data_sg tr_sg; |
Christopher Ferris | 678434f | 2017-07-27 10:42:20 -0700 | [diff] [blame] | 971 | /* Don't pass uninitialized stack data to a remote process */ |
| 972 | tr_sg.transaction_data.target.ptr = 0; |
| 973 | tr_sg.transaction_data.target.handle = handle; |
| 974 | tr_sg.transaction_data.code = code; |
| 975 | tr_sg.transaction_data.flags = binderFlags; |
| 976 | tr_sg.transaction_data.cookie = 0; |
| 977 | tr_sg.transaction_data.sender_pid = 0; |
| 978 | tr_sg.transaction_data.sender_euid = 0; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 979 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 980 | const status_t err = data.errorCheck(); |
| 981 | if (err == NO_ERROR) { |
Christopher Ferris | 678434f | 2017-07-27 10:42:20 -0700 | [diff] [blame] | 982 | tr_sg.transaction_data.data_size = data.ipcDataSize(); |
| 983 | tr_sg.transaction_data.data.ptr.buffer = data.ipcData(); |
| 984 | tr_sg.transaction_data.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t); |
| 985 | tr_sg.transaction_data.data.ptr.offsets = data.ipcObjects(); |
Martijn Coenen | fd51ebb | 2016-07-05 17:00:39 +0200 | [diff] [blame] | 986 | tr_sg.buffers_size = data.ipcBufferSize(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 987 | } else if (statusBuffer) { |
Christopher Ferris | 678434f | 2017-07-27 10:42:20 -0700 | [diff] [blame] | 988 | tr_sg.transaction_data.flags |= TF_STATUS_CODE; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 989 | *statusBuffer = err; |
Christopher Ferris | 678434f | 2017-07-27 10:42:20 -0700 | [diff] [blame] | 990 | tr_sg.transaction_data.data_size = sizeof(status_t); |
| 991 | tr_sg.transaction_data.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer); |
| 992 | tr_sg.transaction_data.offsets_size = 0; |
| 993 | tr_sg.transaction_data.data.ptr.offsets = 0; |
Martijn Coenen | fd51ebb | 2016-07-05 17:00:39 +0200 | [diff] [blame] | 994 | tr_sg.buffers_size = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 995 | } else { |
| 996 | return (mLastError = err); |
| 997 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 998 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 999 | mOut.writeInt32(cmd); |
Martijn Coenen | fd51ebb | 2016-07-05 17:00:39 +0200 | [diff] [blame] | 1000 | mOut.write(&tr_sg, sizeof(tr_sg)); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1001 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1002 | return NO_ERROR; |
| 1003 | } |
| 1004 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1005 | void IPCThreadState::setTheContextObject(sp<BHwBinder> obj) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1006 | { |
Martijn Coenen | a660cbc | 2016-05-12 11:29:23 +0200 | [diff] [blame] | 1007 | mContextObject = obj; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Martijn Coenen | 420d4bb | 2017-10-24 11:43:55 +0200 | [diff] [blame] | 1010 | bool IPCThreadState::isLooperThread() |
| 1011 | { |
| 1012 | return mIsLooper; |
| 1013 | } |
| 1014 | |
Tobias Lindskog | a36d576 | 2018-01-05 10:28:31 +0100 | [diff] [blame] | 1015 | bool IPCThreadState::isOnlyBinderThread() { |
| 1016 | return (mIsLooper && mProcess->mMaxThreads <= 1) || mIsPollingThread; |
| 1017 | } |
| 1018 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1019 | status_t IPCThreadState::executeCommand(int32_t cmd) |
| 1020 | { |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1021 | BHwBinder* obj; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1022 | RefBase::weakref_type* refs; |
| 1023 | status_t result = NO_ERROR; |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 1024 | switch ((uint32_t)cmd) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1025 | case BR_ERROR: |
| 1026 | result = mIn.readInt32(); |
| 1027 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1028 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1029 | case BR_OK: |
| 1030 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1031 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1032 | case BR_ACQUIRE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1033 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1034 | obj = (BHwBinder*)mIn.readPointer(); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1035 | ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1036 | "BR_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 1037 | refs, obj, refs->refBase()); |
| 1038 | obj->incStrong(mProcess.get()); |
| 1039 | IF_LOG_REMOTEREFS() { |
| 1040 | LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj); |
| 1041 | obj->printRefs(); |
| 1042 | } |
| 1043 | mOut.writeInt32(BC_ACQUIRE_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1044 | mOut.writePointer((uintptr_t)refs); |
| 1045 | mOut.writePointer((uintptr_t)obj); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1046 | 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 | case BR_RELEASE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1049 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1050 | obj = (BHwBinder*)mIn.readPointer(); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1051 | ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1052 | "BR_RELEASE: object %p does not match cookie %p (expected %p)", |
| 1053 | refs, obj, refs->refBase()); |
| 1054 | IF_LOG_REMOTEREFS() { |
| 1055 | LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj); |
| 1056 | obj->printRefs(); |
| 1057 | } |
| 1058 | mPendingStrongDerefs.push(obj); |
| 1059 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1060 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1061 | case BR_INCREFS: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1062 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1063 | obj = (BHwBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1064 | refs->incWeak(mProcess.get()); |
| 1065 | mOut.writeInt32(BC_INCREFS_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1066 | mOut.writePointer((uintptr_t)refs); |
| 1067 | mOut.writePointer((uintptr_t)obj); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1068 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1069 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1070 | case BR_DECREFS: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1071 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1072 | obj = (BHwBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1073 | // NOTE: This assertion is not valid, because the object may no |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1074 | // longer exist (thus the (BHwBinder*)cast above resulting in a different |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1075 | // memory address). |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1076 | //ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1077 | // "BR_DECREFS: object %p does not match cookie %p (expected %p)", |
| 1078 | // refs, obj, refs->refBase()); |
| 1079 | mPendingWeakDerefs.push(refs); |
| 1080 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1081 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1082 | case BR_ATTEMPT_ACQUIRE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1083 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1084 | obj = (BHwBinder*)mIn.readPointer(); |
| 1085 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1086 | { |
| 1087 | const bool success = refs->attemptIncStrong(mProcess.get()); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1088 | ALOG_ASSERT(success && refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1089 | "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 1090 | refs, obj, refs->refBase()); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1091 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1092 | mOut.writeInt32(BC_ACQUIRE_RESULT); |
| 1093 | mOut.writeInt32((int32_t)success); |
| 1094 | } |
| 1095 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1096 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1097 | case BR_TRANSACTION: |
| 1098 | { |
| 1099 | binder_transaction_data tr; |
| 1100 | result = mIn.read(&tr, sizeof(tr)); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1101 | ALOG_ASSERT(result == NO_ERROR, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1102 | "Not enough command data for brTRANSACTION"); |
| 1103 | if (result != NO_ERROR) break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1104 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1105 | Parcel buffer; |
| 1106 | buffer.ipcSetDataReference( |
| 1107 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 1108 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1109 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 1110 | tr.offsets_size/sizeof(binder_size_t), freeBuffer, this); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1111 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1112 | const pid_t origPid = mCallingPid; |
| 1113 | const uid_t origUid = mCallingUid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1114 | const int32_t origStrictModePolicy = mStrictModePolicy; |
| 1115 | const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags; |
| 1116 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1117 | mCallingPid = tr.sender_pid; |
| 1118 | mCallingUid = tr.sender_euid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1119 | mLastTransactionBinderFlags = tr.flags; |
| 1120 | |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1121 | //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid); |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1122 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1123 | Parcel reply; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1124 | status_t error; |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1125 | bool reply_sent = false; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1126 | IF_LOG_TRANSACTIONS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1127 | alog << "BR_TRANSACTION thr " << (void*)pthread_self() |
| 1128 | << " / obj " << tr.target.ptr << " / code " |
| 1129 | << TypeCode(tr.code) << ": " << indent << buffer |
| 1130 | << dedent << endl |
| 1131 | << "Data addr = " |
| 1132 | << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer) |
| 1133 | << ", offsets addr=" |
| 1134 | << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl; |
| 1135 | } |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1136 | |
| 1137 | auto reply_callback = [&] (auto &replyParcel) { |
| 1138 | if (reply_sent) { |
| 1139 | // Reply was sent earlier, ignore it. |
| 1140 | ALOGE("Dropping binder reply, it was sent already."); |
| 1141 | return; |
| 1142 | } |
| 1143 | reply_sent = true; |
| 1144 | if ((tr.flags & TF_ONE_WAY) == 0) { |
| 1145 | replyParcel.setError(NO_ERROR); |
| 1146 | sendReply(replyParcel, 0); |
| 1147 | } else { |
| 1148 | ALOGE("Not sending reply in one-way transaction"); |
| 1149 | } |
| 1150 | }; |
| 1151 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1152 | if (tr.target.ptr) { |
Dianne Hackborn | 839f707 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1153 | // We only have a weak reference on the target object, so we must first try to |
| 1154 | // safely acquire a strong reference before doing anything else with it. |
| 1155 | if (reinterpret_cast<RefBase::weakref_type*>( |
| 1156 | tr.target.ptr)->attemptIncStrong(this)) { |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1157 | error = reinterpret_cast<BHwBinder*>(tr.cookie)->transact(tr.code, buffer, |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1158 | &reply, tr.flags, reply_callback); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1159 | reinterpret_cast<BHwBinder*>(tr.cookie)->decStrong(this); |
Dianne Hackborn | 839f707 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1160 | } else { |
| 1161 | error = UNKNOWN_TRANSACTION; |
| 1162 | } |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 1163 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1164 | } else { |
Martijn Coenen | a660cbc | 2016-05-12 11:29:23 +0200 | [diff] [blame] | 1165 | error = mContextObject->transact(tr.code, buffer, &reply, tr.flags, reply_callback); |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | if ((tr.flags & TF_ONE_WAY) == 0) { |
| 1169 | if (!reply_sent) { |
| 1170 | // Should have been a reply but there wasn't, so there |
| 1171 | // must have been an error instead. |
| 1172 | reply.setError(error); |
| 1173 | sendReply(reply, 0); |
| 1174 | } else { |
| 1175 | if (error != NO_ERROR) { |
| 1176 | ALOGE("transact() returned error after sending reply."); |
| 1177 | } else { |
| 1178 | // Ok, reply sent and transact didn't return an error. |
| 1179 | } |
| 1180 | } |
| 1181 | } else { |
| 1182 | // One-way transaction, don't care about return value or reply. |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1183 | } |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1184 | |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1185 | //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1186 | // mCallingPid, origPid, origUid); |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1187 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1188 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1189 | mCallingPid = origPid; |
| 1190 | mCallingUid = origUid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1191 | mStrictModePolicy = origStrictModePolicy; |
| 1192 | mLastTransactionBinderFlags = origTransactionBinderFlags; |
Christopher Tate | 7c4dfec | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1193 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1194 | IF_LOG_TRANSACTIONS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1195 | alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj " |
| 1196 | << tr.target.ptr << ": " << indent << reply << dedent << endl; |
| 1197 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1198 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1199 | } |
| 1200 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1201 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1202 | case BR_DEAD_BINDER: |
| 1203 | { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 1204 | BpHwBinder *proxy = (BpHwBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1205 | proxy->sendObituary(); |
| 1206 | mOut.writeInt32(BC_DEAD_BINDER_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1207 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1208 | } break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1209 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1210 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: |
| 1211 | { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 1212 | BpHwBinder *proxy = (BpHwBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1213 | proxy->getWeakRefs()->decWeak(proxy); |
| 1214 | } break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1215 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1216 | case BR_FINISHED: |
| 1217 | result = TIMED_OUT; |
| 1218 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1219 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1220 | case BR_NOOP: |
| 1221 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1222 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1223 | case BR_SPAWN_LOOPER: |
| 1224 | mProcess->spawnPooledThread(false); |
| 1225 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1226 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1227 | default: |
| 1228 | printf("*** BAD COMMAND %d received from Binder driver\n", cmd); |
| 1229 | result = UNKNOWN_ERROR; |
| 1230 | break; |
| 1231 | } |
| 1232 | |
| 1233 | if (result != NO_ERROR) { |
| 1234 | mLastError = result; |
| 1235 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1236 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1237 | return result; |
| 1238 | } |
| 1239 | |
| 1240 | void IPCThreadState::threadDestructor(void *st) |
| 1241 | { |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1242 | IPCThreadState* const self = static_cast<IPCThreadState*>(st); |
| 1243 | if (self) { |
| 1244 | self->flushCommands(); |
Elliott Hughes | e5e7055 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1245 | #if defined(__ANDROID__) |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1246 | if (self->mProcess->mDriverFD > 0) { |
| 1247 | ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0); |
| 1248 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1249 | #endif |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1250 | delete self; |
| 1251 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1255 | void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data, |
| 1256 | size_t /*dataSize*/, |
| 1257 | const binder_size_t* /*objects*/, |
| 1258 | size_t /*objectsSize*/, void* /*cookie*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1259 | { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1260 | //ALOGI("Freeing parcel %p", &parcel); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1261 | IF_LOG_COMMANDS() { |
| 1262 | alog << "Writing BC_FREE_BUFFER for " << data << endl; |
| 1263 | } |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1264 | ALOG_ASSERT(data != NULL, "Called with NULL data"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1265 | if (parcel != NULL) parcel->closeFileDescriptors(); |
| 1266 | IPCThreadState* state = self(); |
| 1267 | state->mOut.writeInt32(BC_FREE_BUFFER); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1268 | state->mOut.writePointer((uintptr_t)data); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 1271 | }; // namespace hardware |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1272 | }; // namespace android |