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