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 | { |
| 573 | status_t err = data.errorCheck(); |
| 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 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 583 | if (err == NO_ERROR) { |
| 584 | LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(), |
| 585 | (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY"); |
Martijn Coenen | d39a168 | 2016-06-03 21:27:28 +0200 | [diff] [blame] | 586 | err = writeTransactionData(BC_TRANSACTION_SG, flags, handle, code, data, NULL); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 587 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 588 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 589 | if (err != NO_ERROR) { |
| 590 | if (reply) reply->setError(err); |
| 591 | return (mLastError = err); |
| 592 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 593 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 594 | if ((flags & TF_ONE_WAY) == 0) { |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 595 | #if 0 |
| 596 | if (code == 4) { // relayout |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 597 | ALOGI(">>>>>> CALLING transaction 4"); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 598 | } else { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 599 | ALOGI(">>>>>> CALLING transaction %d", code); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 600 | } |
| 601 | #endif |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 602 | if (reply) { |
| 603 | err = waitForResponse(reply); |
| 604 | } else { |
| 605 | Parcel fakeReply; |
| 606 | err = waitForResponse(&fakeReply); |
| 607 | } |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 608 | #if 0 |
| 609 | if (code == 4) { // relayout |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 610 | ALOGI("<<<<<< RETURNING transaction 4"); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 611 | } else { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 612 | ALOGI("<<<<<< RETURNING transaction %d", code); |
Dianne Hackborn | 9887826 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 613 | } |
| 614 | #endif |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 615 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 616 | IF_LOG_TRANSACTIONS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 617 | alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand " |
| 618 | << handle << ": "; |
| 619 | if (reply) alog << indent << *reply << dedent << endl; |
| 620 | else alog << "(none requested)" << endl; |
| 621 | } |
| 622 | } else { |
| 623 | err = waitForResponse(NULL, NULL); |
| 624 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 625 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 626 | return err; |
| 627 | } |
| 628 | |
| 629 | void IPCThreadState::incStrongHandle(int32_t handle) |
| 630 | { |
| 631 | LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle); |
| 632 | mOut.writeInt32(BC_ACQUIRE); |
| 633 | mOut.writeInt32(handle); |
| 634 | } |
| 635 | |
| 636 | void IPCThreadState::decStrongHandle(int32_t handle) |
| 637 | { |
| 638 | LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle); |
| 639 | mOut.writeInt32(BC_RELEASE); |
| 640 | mOut.writeInt32(handle); |
| 641 | } |
| 642 | |
| 643 | void IPCThreadState::incWeakHandle(int32_t handle) |
| 644 | { |
| 645 | LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle); |
| 646 | mOut.writeInt32(BC_INCREFS); |
| 647 | mOut.writeInt32(handle); |
| 648 | } |
| 649 | |
| 650 | void IPCThreadState::decWeakHandle(int32_t handle) |
| 651 | { |
| 652 | LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle); |
| 653 | mOut.writeInt32(BC_DECREFS); |
| 654 | mOut.writeInt32(handle); |
| 655 | } |
| 656 | |
| 657 | status_t IPCThreadState::attemptIncStrongHandle(int32_t handle) |
| 658 | { |
Arve Hjønnevåg | 304dcae | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 659 | #if HAS_BC_ATTEMPT_ACQUIRE |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 660 | LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 661 | mOut.writeInt32(BC_ATTEMPT_ACQUIRE); |
| 662 | mOut.writeInt32(0); // xxx was thread priority |
| 663 | mOut.writeInt32(handle); |
| 664 | status_t result = UNKNOWN_ERROR; |
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 | waitForResponse(NULL, &result); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 667 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 668 | #if LOG_REFCOUNTS |
| 669 | printf("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n", |
| 670 | handle, result == NO_ERROR ? "SUCCESS" : "FAILURE"); |
| 671 | #endif |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 672 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 673 | return result; |
Arve Hjønnevåg | 304dcae | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 674 | #else |
| 675 | (void)handle; |
| 676 | ALOGE("%s(%d): Not supported\n", __func__, handle); |
| 677 | return INVALID_OPERATION; |
| 678 | #endif |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder) |
| 682 | { |
| 683 | #if LOG_REFCOUNTS |
| 684 | printf("IPCThreadState::expungeHandle(%ld)\n", handle); |
| 685 | #endif |
| 686 | self()->mProcess->expungeHandle(handle, binder); |
| 687 | } |
| 688 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 689 | status_t IPCThreadState::requestDeathNotification(int32_t handle, BpHwBinder* proxy) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 690 | { |
| 691 | mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION); |
| 692 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 693 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 694 | return NO_ERROR; |
| 695 | } |
| 696 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 697 | status_t IPCThreadState::clearDeathNotification(int32_t handle, BpHwBinder* proxy) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 698 | { |
| 699 | mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION); |
| 700 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 701 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 702 | return NO_ERROR; |
| 703 | } |
| 704 | |
| 705 | IPCThreadState::IPCThreadState() |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 706 | : mProcess(ProcessState::self()), |
Elliott Hughes | 07cf48a | 2014-08-18 10:38:38 -0700 | [diff] [blame] | 707 | mMyThreadId(gettid()), |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 708 | mStrictModePolicy(0), |
| 709 | mLastTransactionBinderFlags(0) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 710 | { |
| 711 | pthread_setspecific(gTLS, this); |
Dianne Hackborn | 5f4d7e8 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 712 | clearCaller(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 713 | mIn.setDataCapacity(256); |
| 714 | mOut.setDataCapacity(256); |
| 715 | } |
| 716 | |
| 717 | IPCThreadState::~IPCThreadState() |
| 718 | { |
| 719 | } |
| 720 | |
| 721 | status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags) |
| 722 | { |
| 723 | status_t err; |
| 724 | status_t statusBuffer; |
Martijn Coenen | d39a168 | 2016-06-03 21:27:28 +0200 | [diff] [blame] | 725 | err = writeTransactionData(BC_REPLY_SG, flags, -1, 0, reply, &statusBuffer); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 726 | if (err < NO_ERROR) return err; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 727 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 728 | return waitForResponse(NULL, NULL); |
| 729 | } |
| 730 | |
| 731 | status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult) |
| 732 | { |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 733 | uint32_t cmd; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 734 | int32_t err; |
| 735 | |
| 736 | while (1) { |
| 737 | if ((err=talkWithDriver()) < NO_ERROR) break; |
| 738 | err = mIn.errorCheck(); |
| 739 | if (err < NO_ERROR) break; |
| 740 | if (mIn.dataAvail() == 0) continue; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 741 | |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 742 | cmd = (uint32_t)mIn.readInt32(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 743 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 744 | IF_LOG_COMMANDS() { |
| 745 | alog << "Processing waitForResponse Command: " |
| 746 | << getReturnString(cmd) << endl; |
| 747 | } |
| 748 | |
| 749 | switch (cmd) { |
| 750 | case BR_TRANSACTION_COMPLETE: |
| 751 | if (!reply && !acquireResult) goto finish; |
| 752 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 753 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 754 | case BR_DEAD_REPLY: |
| 755 | err = DEAD_OBJECT; |
| 756 | goto finish; |
| 757 | |
| 758 | case BR_FAILED_REPLY: |
| 759 | err = FAILED_TRANSACTION; |
| 760 | goto finish; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 761 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 762 | case BR_ACQUIRE_RESULT: |
| 763 | { |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 764 | ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 765 | const int32_t result = mIn.readInt32(); |
| 766 | if (!acquireResult) continue; |
| 767 | *acquireResult = result ? NO_ERROR : INVALID_OPERATION; |
| 768 | } |
| 769 | goto finish; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 770 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 771 | case BR_REPLY: |
| 772 | { |
| 773 | binder_transaction_data tr; |
| 774 | err = mIn.read(&tr, sizeof(tr)); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 775 | ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 776 | if (err != NO_ERROR) goto finish; |
| 777 | |
| 778 | if (reply) { |
| 779 | if ((tr.flags & TF_STATUS_CODE) == 0) { |
| 780 | reply->ipcSetDataReference( |
| 781 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 782 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 783 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 784 | tr.offsets_size/sizeof(binder_size_t), |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 785 | freeBuffer, this); |
| 786 | } else { |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 787 | err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 788 | freeBuffer(NULL, |
| 789 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 790 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 791 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 792 | tr.offsets_size/sizeof(binder_size_t), this); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 793 | } |
| 794 | } else { |
| 795 | freeBuffer(NULL, |
| 796 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 797 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 798 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 799 | tr.offsets_size/sizeof(binder_size_t), this); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 800 | continue; |
| 801 | } |
| 802 | } |
| 803 | goto finish; |
| 804 | |
| 805 | default: |
| 806 | err = executeCommand(cmd); |
| 807 | if (err != NO_ERROR) goto finish; |
| 808 | break; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | finish: |
| 813 | if (err != NO_ERROR) { |
| 814 | if (acquireResult) *acquireResult = err; |
| 815 | if (reply) reply->setError(err); |
| 816 | mLastError = err; |
| 817 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 818 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 819 | return err; |
| 820 | } |
| 821 | |
| 822 | status_t IPCThreadState::talkWithDriver(bool doReceive) |
| 823 | { |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 824 | if (mProcess->mDriverFD <= 0) { |
| 825 | return -EBADF; |
| 826 | } |
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 | binder_write_read bwr; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 829 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 830 | // Is the read buffer empty? |
| 831 | const bool needRead = mIn.dataPosition() >= mIn.dataSize(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 832 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 833 | // We don't want to write anything if we are still reading |
| 834 | // from data left in the input buffer and the caller |
| 835 | // has requested to read the next data. |
| 836 | const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 837 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 838 | bwr.write_size = outAvail; |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 839 | bwr.write_buffer = (uintptr_t)mOut.data(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 840 | |
| 841 | // This is what we'll read. |
| 842 | if (doReceive && needRead) { |
| 843 | bwr.read_size = mIn.dataCapacity(); |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 844 | bwr.read_buffer = (uintptr_t)mIn.data(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 845 | } else { |
| 846 | bwr.read_size = 0; |
Ben Cheng | 455a70a | 2011-12-01 17:11:32 -0800 | [diff] [blame] | 847 | bwr.read_buffer = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 848 | } |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 849 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 850 | IF_LOG_COMMANDS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 851 | if (outAvail != 0) { |
| 852 | alog << "Sending commands to driver: " << indent; |
| 853 | const void* cmds = (const void*)bwr.write_buffer; |
| 854 | const void* end = ((const uint8_t*)cmds)+bwr.write_size; |
| 855 | alog << HexDump(cmds, bwr.write_size) << endl; |
| 856 | while (cmds < end) cmds = printCommand(alog, cmds); |
| 857 | alog << dedent; |
| 858 | } |
| 859 | alog << "Size of receive buffer: " << bwr.read_size |
| 860 | << ", needRead: " << needRead << ", doReceive: " << doReceive << endl; |
| 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 immediately if there is nothing to do. |
| 864 | if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR; |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 865 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 866 | bwr.write_consumed = 0; |
| 867 | bwr.read_consumed = 0; |
| 868 | status_t err; |
| 869 | do { |
| 870 | IF_LOG_COMMANDS() { |
| 871 | alog << "About to read/write, write size = " << mOut.dataSize() << endl; |
| 872 | } |
Elliott Hughes | e5e7055 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 873 | #if defined(__ANDROID__) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 874 | if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0) |
| 875 | err = NO_ERROR; |
| 876 | else |
| 877 | err = -errno; |
| 878 | #else |
| 879 | err = INVALID_OPERATION; |
| 880 | #endif |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 881 | if (mProcess->mDriverFD <= 0) { |
| 882 | err = -EBADF; |
| 883 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 884 | IF_LOG_COMMANDS() { |
| 885 | alog << "Finished read/write, write size = " << mOut.dataSize() << endl; |
| 886 | } |
| 887 | } while (err == -EINTR); |
Andy McFadden | 457d51f | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 888 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 889 | IF_LOG_COMMANDS() { |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 890 | alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: " |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 891 | << bwr.write_consumed << " (of " << mOut.dataSize() |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 892 | << "), read consumed: " << bwr.read_consumed << endl; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | if (err >= NO_ERROR) { |
| 896 | if (bwr.write_consumed > 0) { |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 897 | if (bwr.write_consumed < mOut.dataSize()) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 898 | mOut.remove(0, bwr.write_consumed); |
| 899 | else |
| 900 | mOut.setDataSize(0); |
| 901 | } |
| 902 | if (bwr.read_consumed > 0) { |
| 903 | mIn.setDataSize(bwr.read_consumed); |
| 904 | mIn.setDataPosition(0); |
| 905 | } |
| 906 | IF_LOG_COMMANDS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 907 | alog << "Remaining data size: " << mOut.dataSize() << endl; |
| 908 | alog << "Received commands from driver: " << indent; |
| 909 | const void* cmds = mIn.data(); |
| 910 | const void* end = mIn.data() + mIn.dataSize(); |
| 911 | alog << HexDump(cmds, mIn.dataSize()) << endl; |
| 912 | while (cmds < end) cmds = printReturnCommand(alog, cmds); |
| 913 | alog << dedent; |
| 914 | } |
| 915 | return NO_ERROR; |
| 916 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 917 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 918 | return err; |
| 919 | } |
| 920 | |
| 921 | status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags, |
| 922 | int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer) |
| 923 | { |
Martijn Coenen | fd51ebb | 2016-07-05 17:00:39 +0200 | [diff] [blame] | 924 | binder_transaction_data_sg tr_sg; |
Christopher Ferris | 678434f | 2017-07-27 10:42:20 -0700 | [diff] [blame] | 925 | /* Don't pass uninitialized stack data to a remote process */ |
| 926 | tr_sg.transaction_data.target.ptr = 0; |
| 927 | tr_sg.transaction_data.target.handle = handle; |
| 928 | tr_sg.transaction_data.code = code; |
| 929 | tr_sg.transaction_data.flags = binderFlags; |
| 930 | tr_sg.transaction_data.cookie = 0; |
| 931 | tr_sg.transaction_data.sender_pid = 0; |
| 932 | tr_sg.transaction_data.sender_euid = 0; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 933 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 934 | const status_t err = data.errorCheck(); |
| 935 | if (err == NO_ERROR) { |
Christopher Ferris | 678434f | 2017-07-27 10:42:20 -0700 | [diff] [blame] | 936 | tr_sg.transaction_data.data_size = data.ipcDataSize(); |
| 937 | tr_sg.transaction_data.data.ptr.buffer = data.ipcData(); |
| 938 | tr_sg.transaction_data.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t); |
| 939 | tr_sg.transaction_data.data.ptr.offsets = data.ipcObjects(); |
Martijn Coenen | fd51ebb | 2016-07-05 17:00:39 +0200 | [diff] [blame] | 940 | tr_sg.buffers_size = data.ipcBufferSize(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 941 | } else if (statusBuffer) { |
Christopher Ferris | 678434f | 2017-07-27 10:42:20 -0700 | [diff] [blame] | 942 | tr_sg.transaction_data.flags |= TF_STATUS_CODE; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 943 | *statusBuffer = err; |
Christopher Ferris | 678434f | 2017-07-27 10:42:20 -0700 | [diff] [blame] | 944 | tr_sg.transaction_data.data_size = sizeof(status_t); |
| 945 | tr_sg.transaction_data.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer); |
| 946 | tr_sg.transaction_data.offsets_size = 0; |
| 947 | tr_sg.transaction_data.data.ptr.offsets = 0; |
Martijn Coenen | fd51ebb | 2016-07-05 17:00:39 +0200 | [diff] [blame] | 948 | tr_sg.buffers_size = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 949 | } else { |
| 950 | return (mLastError = err); |
| 951 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 952 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 953 | mOut.writeInt32(cmd); |
Martijn Coenen | fd51ebb | 2016-07-05 17:00:39 +0200 | [diff] [blame] | 954 | mOut.write(&tr_sg, sizeof(tr_sg)); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 955 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 956 | return NO_ERROR; |
| 957 | } |
| 958 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 959 | void IPCThreadState::setTheContextObject(sp<BHwBinder> obj) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 960 | { |
Martijn Coenen | a660cbc | 2016-05-12 11:29:23 +0200 | [diff] [blame] | 961 | mContextObject = obj; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | status_t IPCThreadState::executeCommand(int32_t cmd) |
| 965 | { |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 966 | BHwBinder* obj; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 967 | RefBase::weakref_type* refs; |
| 968 | status_t result = NO_ERROR; |
Bernhard Rosenkränzer | b184ed0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 969 | switch ((uint32_t)cmd) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 970 | case BR_ERROR: |
| 971 | result = mIn.readInt32(); |
| 972 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 973 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 974 | case BR_OK: |
| 975 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 976 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 977 | case BR_ACQUIRE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 978 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 979 | obj = (BHwBinder*)mIn.readPointer(); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 980 | ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 981 | "BR_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 982 | refs, obj, refs->refBase()); |
| 983 | obj->incStrong(mProcess.get()); |
| 984 | IF_LOG_REMOTEREFS() { |
| 985 | LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj); |
| 986 | obj->printRefs(); |
| 987 | } |
| 988 | mOut.writeInt32(BC_ACQUIRE_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 989 | mOut.writePointer((uintptr_t)refs); |
| 990 | mOut.writePointer((uintptr_t)obj); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 991 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 992 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 993 | case BR_RELEASE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 994 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 995 | obj = (BHwBinder*)mIn.readPointer(); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 996 | ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 997 | "BR_RELEASE: object %p does not match cookie %p (expected %p)", |
| 998 | refs, obj, refs->refBase()); |
| 999 | IF_LOG_REMOTEREFS() { |
| 1000 | LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj); |
| 1001 | obj->printRefs(); |
| 1002 | } |
| 1003 | mPendingStrongDerefs.push(obj); |
| 1004 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1005 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1006 | case BR_INCREFS: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1007 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1008 | obj = (BHwBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1009 | refs->incWeak(mProcess.get()); |
| 1010 | mOut.writeInt32(BC_INCREFS_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1011 | mOut.writePointer((uintptr_t)refs); |
| 1012 | mOut.writePointer((uintptr_t)obj); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1013 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1014 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1015 | case BR_DECREFS: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1016 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1017 | obj = (BHwBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1018 | // NOTE: This assertion is not valid, because the object may no |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1019 | // longer exist (thus the (BHwBinder*)cast above resulting in a different |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1020 | // memory address). |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1021 | //ALOG_ASSERT(refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1022 | // "BR_DECREFS: object %p does not match cookie %p (expected %p)", |
| 1023 | // refs, obj, refs->refBase()); |
| 1024 | mPendingWeakDerefs.push(refs); |
| 1025 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1026 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1027 | case BR_ATTEMPT_ACQUIRE: |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1028 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1029 | obj = (BHwBinder*)mIn.readPointer(); |
| 1030 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1031 | { |
| 1032 | const bool success = refs->attemptIncStrong(mProcess.get()); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1033 | ALOG_ASSERT(success && refs->refBase() == obj, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1034 | "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 1035 | refs, obj, refs->refBase()); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1036 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1037 | mOut.writeInt32(BC_ACQUIRE_RESULT); |
| 1038 | mOut.writeInt32((int32_t)success); |
| 1039 | } |
| 1040 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1041 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1042 | case BR_TRANSACTION: |
| 1043 | { |
| 1044 | binder_transaction_data tr; |
| 1045 | result = mIn.read(&tr, sizeof(tr)); |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1046 | ALOG_ASSERT(result == NO_ERROR, |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1047 | "Not enough command data for brTRANSACTION"); |
| 1048 | if (result != NO_ERROR) break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1049 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1050 | Parcel buffer; |
| 1051 | buffer.ipcSetDataReference( |
| 1052 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 1053 | tr.data_size, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1054 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 1055 | tr.offsets_size/sizeof(binder_size_t), freeBuffer, this); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1056 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1057 | const pid_t origPid = mCallingPid; |
| 1058 | const uid_t origUid = mCallingUid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1059 | const int32_t origStrictModePolicy = mStrictModePolicy; |
| 1060 | const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags; |
| 1061 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1062 | mCallingPid = tr.sender_pid; |
| 1063 | mCallingUid = tr.sender_euid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1064 | mLastTransactionBinderFlags = tr.flags; |
| 1065 | |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1066 | //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid); |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1067 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1068 | Parcel reply; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1069 | status_t error; |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1070 | bool reply_sent = false; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1071 | IF_LOG_TRANSACTIONS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1072 | alog << "BR_TRANSACTION thr " << (void*)pthread_self() |
| 1073 | << " / obj " << tr.target.ptr << " / code " |
| 1074 | << TypeCode(tr.code) << ": " << indent << buffer |
| 1075 | << dedent << endl |
| 1076 | << "Data addr = " |
| 1077 | << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer) |
| 1078 | << ", offsets addr=" |
| 1079 | << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl; |
| 1080 | } |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1081 | |
| 1082 | auto reply_callback = [&] (auto &replyParcel) { |
| 1083 | if (reply_sent) { |
| 1084 | // Reply was sent earlier, ignore it. |
| 1085 | ALOGE("Dropping binder reply, it was sent already."); |
| 1086 | return; |
| 1087 | } |
| 1088 | reply_sent = true; |
| 1089 | if ((tr.flags & TF_ONE_WAY) == 0) { |
| 1090 | replyParcel.setError(NO_ERROR); |
| 1091 | sendReply(replyParcel, 0); |
| 1092 | } else { |
| 1093 | ALOGE("Not sending reply in one-way transaction"); |
| 1094 | } |
| 1095 | }; |
| 1096 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1097 | if (tr.target.ptr) { |
Dianne Hackborn | 839f707 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1098 | // We only have a weak reference on the target object, so we must first try to |
| 1099 | // safely acquire a strong reference before doing anything else with it. |
| 1100 | if (reinterpret_cast<RefBase::weakref_type*>( |
| 1101 | tr.target.ptr)->attemptIncStrong(this)) { |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1102 | error = reinterpret_cast<BHwBinder*>(tr.cookie)->transact(tr.code, buffer, |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1103 | &reply, tr.flags, reply_callback); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1104 | reinterpret_cast<BHwBinder*>(tr.cookie)->decStrong(this); |
Dianne Hackborn | 839f707 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1105 | } else { |
| 1106 | error = UNKNOWN_TRANSACTION; |
| 1107 | } |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 1108 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1109 | } else { |
Martijn Coenen | a660cbc | 2016-05-12 11:29:23 +0200 | [diff] [blame] | 1110 | error = mContextObject->transact(tr.code, buffer, &reply, tr.flags, reply_callback); |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | if ((tr.flags & TF_ONE_WAY) == 0) { |
| 1114 | if (!reply_sent) { |
| 1115 | // Should have been a reply but there wasn't, so there |
| 1116 | // must have been an error instead. |
| 1117 | reply.setError(error); |
| 1118 | sendReply(reply, 0); |
| 1119 | } else { |
| 1120 | if (error != NO_ERROR) { |
| 1121 | ALOGE("transact() returned error after sending reply."); |
| 1122 | } else { |
| 1123 | // Ok, reply sent and transact didn't return an error. |
| 1124 | } |
| 1125 | } |
| 1126 | } else { |
| 1127 | // One-way transaction, don't care about return value or reply. |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1128 | } |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1129 | |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1130 | //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1131 | // mCallingPid, origPid, origUid); |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 1132 | |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1133 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1134 | mCallingPid = origPid; |
| 1135 | mCallingUid = origUid; |
Dianne Hackborn | f99aec6 | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1136 | mStrictModePolicy = origStrictModePolicy; |
| 1137 | mLastTransactionBinderFlags = origTransactionBinderFlags; |
Christopher Tate | 7c4dfec | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1138 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1139 | IF_LOG_TRANSACTIONS() { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1140 | alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj " |
| 1141 | << tr.target.ptr << ": " << indent << reply << dedent << endl; |
| 1142 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1143 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1144 | } |
| 1145 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1146 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1147 | case BR_DEAD_BINDER: |
| 1148 | { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 1149 | BpHwBinder *proxy = (BpHwBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1150 | proxy->sendObituary(); |
| 1151 | mOut.writeInt32(BC_DEAD_BINDER_DONE); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1152 | mOut.writePointer((uintptr_t)proxy); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1153 | } break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1154 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1155 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: |
| 1156 | { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 1157 | BpHwBinder *proxy = (BpHwBinder*)mIn.readPointer(); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1158 | proxy->getWeakRefs()->decWeak(proxy); |
| 1159 | } break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1160 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1161 | case BR_FINISHED: |
| 1162 | result = TIMED_OUT; |
| 1163 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1164 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1165 | case BR_NOOP: |
| 1166 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1167 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1168 | case BR_SPAWN_LOOPER: |
| 1169 | mProcess->spawnPooledThread(false); |
| 1170 | break; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1171 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1172 | default: |
| 1173 | printf("*** BAD COMMAND %d received from Binder driver\n", cmd); |
| 1174 | result = UNKNOWN_ERROR; |
| 1175 | break; |
| 1176 | } |
| 1177 | |
| 1178 | if (result != NO_ERROR) { |
| 1179 | mLastError = result; |
| 1180 | } |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 1181 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1182 | return result; |
| 1183 | } |
| 1184 | |
| 1185 | void IPCThreadState::threadDestructor(void *st) |
| 1186 | { |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1187 | IPCThreadState* const self = static_cast<IPCThreadState*>(st); |
| 1188 | if (self) { |
| 1189 | self->flushCommands(); |
Elliott Hughes | e5e7055 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1190 | #if defined(__ANDROID__) |
Johannes Carlsson | 597a3c7 | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1191 | if (self->mProcess->mDriverFD > 0) { |
| 1192 | ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0); |
| 1193 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1194 | #endif |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1195 | delete self; |
| 1196 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1200 | void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data, |
| 1201 | size_t /*dataSize*/, |
| 1202 | const binder_size_t* /*objects*/, |
| 1203 | size_t /*objectsSize*/, void* /*cookie*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1204 | { |
Steve Block | 93cf854 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1205 | //ALOGI("Freeing parcel %p", &parcel); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1206 | IF_LOG_COMMANDS() { |
| 1207 | alog << "Writing BC_FREE_BUFFER for " << data << endl; |
| 1208 | } |
Steve Block | d0bfabc | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1209 | ALOG_ASSERT(data != NULL, "Called with NULL data"); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1210 | if (parcel != NULL) parcel->closeFileDescriptors(); |
| 1211 | IPCThreadState* state = self(); |
| 1212 | state->mOut.writeInt32(BC_FREE_BUFFER); |
Serban Constantinescu | 4ca5baf | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1213 | state->mOut.writePointer((uintptr_t)data); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 1216 | }; // namespace hardware |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1217 | }; // namespace android |