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