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> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
| 22 | #include <binder/BpBinder.h> |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 23 | #include <binder/TextOutput.h> |
| 24 | |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 25 | #include <android-base/macros.h> |
Glenn Kasten | a26e1cf | 2012-03-16 07:15:23 -0700 | [diff] [blame] | 26 | #include <cutils/sched_policy.h> |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 27 | #include <utils/CallStack.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <utils/Log.h> |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 29 | #include <utils/SystemClock.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | #include <utils/threads.h> |
| 31 | |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 32 | #include <atomic> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include <errno.h> |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 34 | #include <inttypes.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include <pthread.h> |
| 36 | #include <sched.h> |
Yabin Cui | 8fb2d25 | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 37 | #include <signal.h> |
| 38 | #include <stdio.h> |
| 39 | #include <sys/ioctl.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | #include <sys/resource.h> |
Yabin Cui | 8fb2d25 | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 41 | #include <unistd.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 43 | #include "Static.h" |
Steven Moreland | 6ba5a25 | 2021-05-04 22:49:00 +0000 | [diff] [blame] | 44 | #include "binder_module.h" |
Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 45 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | #if LOG_NDEBUG |
| 47 | |
| 48 | #define IF_LOG_TRANSACTIONS() if (false) |
| 49 | #define IF_LOG_COMMANDS() if (false) |
| 50 | #define LOG_REMOTEREFS(...) |
| 51 | #define IF_LOG_REMOTEREFS() if (false) |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 52 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | #define LOG_THREADPOOL(...) |
| 54 | #define LOG_ONEWAY(...) |
| 55 | |
| 56 | #else |
| 57 | |
Steve Block | 9f76015 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 58 | #define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact") |
| 59 | #define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc") |
| 60 | #define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__) |
| 61 | #define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs") |
| 62 | #define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__) |
| 63 | #define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | |
| 65 | #endif |
| 66 | |
| 67 | // --------------------------------------------------------------------------- |
| 68 | |
| 69 | namespace android { |
| 70 | |
Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 71 | // Static const and functions will be optimized out if not used, |
| 72 | // 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] | 73 | static const char *kReturnStrings[] = { |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 74 | "BR_ERROR", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | "BR_OK", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | "BR_TRANSACTION", |
| 77 | "BR_REPLY", |
| 78 | "BR_ACQUIRE_RESULT", |
| 79 | "BR_DEAD_REPLY", |
| 80 | "BR_TRANSACTION_COMPLETE", |
| 81 | "BR_INCREFS", |
| 82 | "BR_ACQUIRE", |
| 83 | "BR_RELEASE", |
| 84 | "BR_DECREFS", |
| 85 | "BR_ATTEMPT_ACQUIRE", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | "BR_NOOP", |
| 87 | "BR_SPAWN_LOOPER", |
| 88 | "BR_FINISHED", |
| 89 | "BR_DEAD_BINDER", |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 90 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 91 | "BR_FAILED_REPLY", |
Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 92 | "BR_FROZEN_REPLY", |
| 93 | "BR_ONEWAY_SPAM_SUSPECT", |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 94 | "BR_TRANSACTION_SEC_CTX", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static const char *kCommandStrings[] = { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | "BC_TRANSACTION", |
| 99 | "BC_REPLY", |
| 100 | "BC_ACQUIRE_RESULT", |
| 101 | "BC_FREE_BUFFER", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | "BC_INCREFS", |
| 103 | "BC_ACQUIRE", |
| 104 | "BC_RELEASE", |
| 105 | "BC_DECREFS", |
| 106 | "BC_INCREFS_DONE", |
| 107 | "BC_ACQUIRE_DONE", |
| 108 | "BC_ATTEMPT_ACQUIRE", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | "BC_REGISTER_LOOPER", |
| 110 | "BC_ENTER_LOOPER", |
| 111 | "BC_EXIT_LOOPER", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 113 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 114 | "BC_DEAD_BINDER_DONE" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 117 | static const int64_t kWorkSourcePropagatedBitIndex = 32; |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 118 | |
songjinshi | 73a7dde | 2016-10-18 21:05:56 +0800 | [diff] [blame] | 119 | static const char* getReturnString(uint32_t cmd) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | { |
songjinshi | 8e486c6 | 2019-04-04 11:22:52 +0800 | [diff] [blame] | 121 | size_t idx = cmd & _IOC_NRMASK; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0])) |
| 123 | return kReturnStrings[idx]; |
| 124 | else |
| 125 | return "unknown"; |
| 126 | } |
| 127 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | static const void* printBinderTransactionData(TextOutput& out, const void* data) |
| 129 | { |
| 130 | const binder_transaction_data* btd = |
| 131 | (const binder_transaction_data*)data; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 132 | if (btd->target.handle < 1024) { |
| 133 | /* want to print descriptors in decimal; guess based on value */ |
| 134 | out << "target.desc=" << btd->target.handle; |
| 135 | } else { |
| 136 | out << "target.ptr=" << btd->target.ptr; |
| 137 | } |
| 138 | out << " (cookie " << btd->cookie << ")" << endl |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 139 | << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(uint64_t)btd->flags << endl |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size |
| 141 | << " bytes)" << endl |
| 142 | << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 143 | << " bytes)"; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | return btd+1; |
| 145 | } |
| 146 | |
| 147 | static const void* printReturnCommand(TextOutput& out, const void* _cmd) |
| 148 | { |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 149 | 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] | 150 | const int32_t* cmd = (const int32_t*)_cmd; |
Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 151 | uint32_t code = (uint32_t)*cmd++; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 152 | size_t cmdIndex = code & 0xff; |
Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 153 | if (code == BR_ERROR) { |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 154 | out << "BR_ERROR: " << (void*)(uint64_t)(*cmd++) << endl; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | return cmd; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 156 | } else if (cmdIndex >= N) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | out << "Unknown reply: " << code << endl; |
| 158 | return cmd; |
| 159 | } |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 160 | out << kReturnStrings[cmdIndex]; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 161 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | switch (code) { |
| 163 | case BR_TRANSACTION: |
| 164 | case BR_REPLY: { |
| 165 | out << ": " << indent; |
| 166 | cmd = (const int32_t *)printBinderTransactionData(out, cmd); |
| 167 | out << dedent; |
| 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_ACQUIRE_RESULT: { |
| 171 | const int32_t res = *cmd++; |
| 172 | out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)"); |
| 173 | } break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 174 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | case BR_INCREFS: |
| 176 | case BR_ACQUIRE: |
| 177 | case BR_RELEASE: |
| 178 | case BR_DECREFS: { |
| 179 | const int32_t b = *cmd++; |
| 180 | const int32_t c = *cmd++; |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 181 | out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")"; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | } break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 183 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | case BR_ATTEMPT_ACQUIRE: { |
| 185 | const int32_t p = *cmd++; |
| 186 | const int32_t b = *cmd++; |
| 187 | const int32_t c = *cmd++; |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 188 | out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | << "), pri=" << p; |
| 190 | } break; |
| 191 | |
| 192 | case BR_DEAD_BINDER: |
| 193 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: { |
| 194 | const int32_t c = *cmd++; |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 195 | out << ": death cookie " << (void*)(uint64_t)c; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } break; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 197 | |
| 198 | default: |
| 199 | // no details to show for: BR_OK, BR_DEAD_REPLY, |
| 200 | // BR_TRANSACTION_COMPLETE, BR_FINISHED |
| 201 | break; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 203 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | out << endl; |
| 205 | return cmd; |
| 206 | } |
| 207 | |
| 208 | static const void* printCommand(TextOutput& out, const void* _cmd) |
| 209 | { |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 210 | 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] | 211 | const int32_t* cmd = (const int32_t*)_cmd; |
Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 212 | uint32_t code = (uint32_t)*cmd++; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 213 | size_t cmdIndex = code & 0xff; |
| 214 | |
| 215 | if (cmdIndex >= N) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | out << "Unknown command: " << code << endl; |
| 217 | return cmd; |
| 218 | } |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 219 | out << kCommandStrings[cmdIndex]; |
| 220 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | switch (code) { |
| 222 | case BC_TRANSACTION: |
| 223 | case BC_REPLY: { |
| 224 | out << ": " << indent; |
| 225 | cmd = (const int32_t *)printBinderTransactionData(out, cmd); |
| 226 | out << dedent; |
| 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_ACQUIRE_RESULT: { |
| 230 | const int32_t res = *cmd++; |
| 231 | out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)"); |
| 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_FREE_BUFFER: { |
| 235 | const int32_t buf = *cmd++; |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 236 | out << ": buffer=" << (void*)(uint64_t)buf; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 238 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | case BC_INCREFS: |
| 240 | case BC_ACQUIRE: |
| 241 | case BC_RELEASE: |
| 242 | case BC_DECREFS: { |
| 243 | const int32_t d = *cmd++; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 244 | out << ": desc=" << d; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 246 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | case BC_INCREFS_DONE: |
| 248 | case BC_ACQUIRE_DONE: { |
| 249 | const int32_t b = *cmd++; |
| 250 | const int32_t c = *cmd++; |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 251 | out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)c << ")"; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | } break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 253 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | case BC_ATTEMPT_ACQUIRE: { |
| 255 | const int32_t p = *cmd++; |
| 256 | const int32_t d = *cmd++; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 257 | out << ": desc=" << d << ", pri=" << p; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 259 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 261 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 262 | const int32_t h = *cmd++; |
| 263 | const int32_t c = *cmd++; |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 264 | out << ": handle=" << h << " (death cookie " << (void*)(uint64_t)c << ")"; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | } break; |
| 266 | |
| 267 | case BC_DEAD_BINDER_DONE: { |
| 268 | const int32_t c = *cmd++; |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 269 | out << ": death cookie " << (void*)(uint64_t)c; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | } break; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 271 | |
| 272 | default: |
| 273 | // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER, |
| 274 | // BC_EXIT_LOOPER |
| 275 | break; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 277 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | out << endl; |
| 279 | return cmd; |
| 280 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | |
| 282 | static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER; |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 283 | static std::atomic<bool> gHaveTLS(false); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | static pthread_key_t gTLS = 0; |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 285 | static std::atomic<bool> gShutdown = false; |
| 286 | static std::atomic<bool> gDisableBackgroundScheduling = false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | |
| 288 | IPCThreadState* IPCThreadState::self() |
| 289 | { |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 290 | if (gHaveTLS.load(std::memory_order_acquire)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | restart: |
| 292 | const pthread_key_t k = gTLS; |
| 293 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k); |
| 294 | if (st) return st; |
| 295 | return new IPCThreadState; |
| 296 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 297 | |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 298 | // Racey, heuristic test for simultaneous shutdown. |
| 299 | if (gShutdown.load(std::memory_order_relaxed)) { |
Andreas Gampe | f31a3eb | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 300 | ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n"); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 301 | return nullptr; |
Andreas Gampe | f31a3eb | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 302 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 303 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | pthread_mutex_lock(&gTLSMutex); |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 305 | if (!gHaveTLS.load(std::memory_order_relaxed)) { |
Andreas Gampe | f31a3eb | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 306 | int key_create_value = pthread_key_create(&gTLS, threadDestructor); |
| 307 | if (key_create_value != 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | pthread_mutex_unlock(&gTLSMutex); |
Andreas Gampe | f31a3eb | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 309 | ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n", |
| 310 | strerror(key_create_value)); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 311 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 313 | gHaveTLS.store(true, std::memory_order_release); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | } |
| 315 | pthread_mutex_unlock(&gTLSMutex); |
| 316 | goto restart; |
| 317 | } |
| 318 | |
Brad Fitzpatrick | 1b60843 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 319 | IPCThreadState* IPCThreadState::selfOrNull() |
| 320 | { |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 321 | if (gHaveTLS.load(std::memory_order_acquire)) { |
Brad Fitzpatrick | 1b60843 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 322 | const pthread_key_t k = gTLS; |
| 323 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k); |
| 324 | return st; |
| 325 | } |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 326 | return nullptr; |
Brad Fitzpatrick | 1b60843 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 327 | } |
| 328 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | void IPCThreadState::shutdown() |
| 330 | { |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 331 | gShutdown.store(true, std::memory_order_relaxed); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 332 | |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 333 | if (gHaveTLS.load(std::memory_order_acquire)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | // XXX Need to wait for all thread pool threads to exit! |
| 335 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS); |
| 336 | if (st) { |
| 337 | delete st; |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 338 | pthread_setspecific(gTLS, nullptr); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | } |
zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 340 | pthread_key_delete(gTLS); |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 341 | gHaveTLS.store(false, std::memory_order_release); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 345 | void IPCThreadState::disableBackgroundScheduling(bool disable) |
| 346 | { |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 347 | gDisableBackgroundScheduling.store(disable, std::memory_order_relaxed); |
Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Martijn Coenen | 2b63174 | 2017-05-05 11:16:59 -0700 | [diff] [blame] | 350 | bool IPCThreadState::backgroundSchedulingDisabled() |
| 351 | { |
Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 352 | return gDisableBackgroundScheduling.load(std::memory_order_relaxed); |
Martijn Coenen | 2b63174 | 2017-05-05 11:16:59 -0700 | [diff] [blame] | 353 | } |
| 354 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | sp<ProcessState> IPCThreadState::process() |
| 356 | { |
| 357 | return mProcess; |
| 358 | } |
| 359 | |
| 360 | status_t IPCThreadState::clearLastError() |
| 361 | { |
| 362 | const status_t err = mLastError; |
| 363 | mLastError = NO_ERROR; |
| 364 | return err; |
| 365 | } |
| 366 | |
Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 367 | pid_t IPCThreadState::getCallingPid() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | { |
| 369 | return mCallingPid; |
| 370 | } |
| 371 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 372 | const char* IPCThreadState::getCallingSid() const |
| 373 | { |
| 374 | return mCallingSid; |
| 375 | } |
| 376 | |
Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 377 | uid_t IPCThreadState::getCallingUid() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | { |
| 379 | return mCallingUid; |
| 380 | } |
| 381 | |
| 382 | int64_t IPCThreadState::clearCallingIdentity() |
| 383 | { |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 384 | // ignore mCallingSid for legacy reasons |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid; |
| 386 | clearCaller(); |
| 387 | return token; |
| 388 | } |
| 389 | |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 390 | void IPCThreadState::setStrictModePolicy(int32_t policy) |
| 391 | { |
| 392 | mStrictModePolicy = policy; |
| 393 | } |
| 394 | |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 395 | int32_t IPCThreadState::getStrictModePolicy() const |
| 396 | { |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 397 | return mStrictModePolicy; |
| 398 | } |
| 399 | |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 400 | int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid) |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 401 | { |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 402 | int64_t token = setCallingWorkSourceUidWithoutPropagation(uid); |
| 403 | mPropagateWorkSource = true; |
| 404 | return token; |
| 405 | } |
| 406 | |
| 407 | int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid) |
| 408 | { |
| 409 | const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex; |
| 410 | int64_t token = propagatedBit | mWorkSource; |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 411 | mWorkSource = uid; |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 412 | return token; |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 415 | void IPCThreadState::clearPropagateWorkSource() |
| 416 | { |
| 417 | mPropagateWorkSource = false; |
| 418 | } |
| 419 | |
| 420 | bool IPCThreadState::shouldPropagateWorkSource() const |
| 421 | { |
| 422 | return mPropagateWorkSource; |
| 423 | } |
| 424 | |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 425 | uid_t IPCThreadState::getCallingWorkSourceUid() const |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 426 | { |
| 427 | return mWorkSource; |
| 428 | } |
| 429 | |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 430 | int64_t IPCThreadState::clearCallingWorkSource() |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 431 | { |
Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 432 | return setCallingWorkSourceUid(kUnsetWorkSource); |
| 433 | } |
| 434 | |
| 435 | void IPCThreadState::restoreCallingWorkSource(int64_t token) |
| 436 | { |
| 437 | uid_t uid = (int)token; |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 438 | setCallingWorkSourceUidWithoutPropagation(uid); |
| 439 | mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1; |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 442 | void IPCThreadState::setLastTransactionBinderFlags(int32_t flags) |
| 443 | { |
| 444 | mLastTransactionBinderFlags = flags; |
| 445 | } |
| 446 | |
| 447 | int32_t IPCThreadState::getLastTransactionBinderFlags() const |
| 448 | { |
| 449 | return mLastTransactionBinderFlags; |
| 450 | } |
| 451 | |
Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 452 | void IPCThreadState::setCallRestriction(ProcessState::CallRestriction restriction) { |
| 453 | mCallRestriction = restriction; |
| 454 | } |
| 455 | |
| 456 | ProcessState::CallRestriction IPCThreadState::getCallRestriction() const { |
| 457 | return mCallRestriction; |
| 458 | } |
| 459 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | void IPCThreadState::restoreCallingIdentity(int64_t token) |
| 461 | { |
| 462 | mCallingUid = (int)(token>>32); |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 463 | mCallingSid = nullptr; // not enough data to restore |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | mCallingPid = (int)token; |
| 465 | } |
| 466 | |
| 467 | void IPCThreadState::clearCaller() |
| 468 | { |
Marco Nelissen | d43b194 | 2009-07-17 07:59:17 -0700 | [diff] [blame] | 469 | mCallingPid = getpid(); |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 470 | mCallingSid = nullptr; // expensive to lookup |
Marco Nelissen | d43b194 | 2009-07-17 07:59:17 -0700 | [diff] [blame] | 471 | mCallingUid = getuid(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void IPCThreadState::flushCommands() |
| 475 | { |
Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 476 | if (mProcess->mDriverFD < 0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | return; |
| 478 | talkWithDriver(false); |
Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 479 | // The flush could have caused post-write refcount decrements to have |
| 480 | // been executed, which in turn could result in BC_RELEASE/BC_DECREFS |
| 481 | // being queued in mOut. So flush again, if we need to. |
| 482 | if (mOut.dataSize() > 0) { |
| 483 | talkWithDriver(false); |
| 484 | } |
| 485 | if (mOut.dataSize() > 0) { |
| 486 | ALOGW("mOut.dataSize() > 0 after flushCommands()"); |
| 487 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 490 | bool IPCThreadState::flushIfNeeded() |
| 491 | { |
Frankie Chang | 617911d | 2021-05-18 13:08:05 +0800 | [diff] [blame^] | 492 | if (mIsLooper || mServingStackPointer != nullptr || mIsFlushing) { |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 493 | return false; |
| 494 | } |
Frankie Chang | 617911d | 2021-05-18 13:08:05 +0800 | [diff] [blame^] | 495 | mIsFlushing = true; |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 496 | // In case this thread is not a looper and is not currently serving a binder transaction, |
| 497 | // there's no guarantee that this thread will call back into the kernel driver any time |
| 498 | // soon. Therefore, flush pending commands such as BC_FREE_BUFFER, to prevent them from getting |
| 499 | // stuck in this thread's out buffer. |
| 500 | flushCommands(); |
Frankie Chang | 617911d | 2021-05-18 13:08:05 +0800 | [diff] [blame^] | 501 | mIsFlushing = false; |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 502 | return true; |
| 503 | } |
| 504 | |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 505 | void IPCThreadState::blockUntilThreadAvailable() |
| 506 | { |
| 507 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
Steven Moreland | c648a76 | 2021-01-16 02:39:45 +0000 | [diff] [blame] | 508 | mProcess->mWaitingForThreads++; |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 509 | while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) { |
Wale Ogunwale | a3206e6 | 2015-04-21 12:29:50 -0700 | [diff] [blame] | 510 | ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n", |
| 511 | static_cast<unsigned long>(mProcess->mExecutingThreadsCount), |
| 512 | static_cast<unsigned long>(mProcess->mMaxThreads)); |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 513 | pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock); |
| 514 | } |
Steven Moreland | c648a76 | 2021-01-16 02:39:45 +0000 | [diff] [blame] | 515 | mProcess->mWaitingForThreads--; |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 516 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 517 | } |
| 518 | |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 519 | status_t IPCThreadState::getAndExecuteCommand() |
| 520 | { |
| 521 | status_t result; |
| 522 | int32_t cmd; |
| 523 | |
| 524 | result = talkWithDriver(); |
| 525 | if (result >= NO_ERROR) { |
| 526 | size_t IN = mIn.dataAvail(); |
| 527 | if (IN < sizeof(int32_t)) return result; |
| 528 | cmd = mIn.readInt32(); |
| 529 | IF_LOG_COMMANDS() { |
| 530 | alog << "Processing top-level Command: " |
| 531 | << getReturnString(cmd) << endl; |
| 532 | } |
| 533 | |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 534 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 535 | mProcess->mExecutingThreadsCount++; |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 536 | if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads && |
| 537 | mProcess->mStarvationStartTimeMs == 0) { |
| 538 | mProcess->mStarvationStartTimeMs = uptimeMillis(); |
| 539 | } |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 540 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 541 | |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 542 | result = executeCommand(cmd); |
| 543 | |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 544 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 545 | mProcess->mExecutingThreadsCount--; |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 546 | if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads && |
| 547 | mProcess->mStarvationStartTimeMs != 0) { |
| 548 | int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs; |
| 549 | if (starvationTimeMs > 100) { |
| 550 | ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms", |
| 551 | mProcess->mMaxThreads, starvationTimeMs); |
| 552 | } |
| 553 | mProcess->mStarvationStartTimeMs = 0; |
| 554 | } |
Steven Moreland | c648a76 | 2021-01-16 02:39:45 +0000 | [diff] [blame] | 555 | |
| 556 | // Cond broadcast can be expensive, so don't send it every time a binder |
| 557 | // call is processed. b/168806193 |
| 558 | if (mProcess->mWaitingForThreads > 0) { |
| 559 | pthread_cond_broadcast(&mProcess->mThreadCountDecrement); |
| 560 | } |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 561 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | return result; |
| 565 | } |
| 566 | |
| 567 | // When we've cleared the incoming command queue, process any pending derefs |
| 568 | void IPCThreadState::processPendingDerefs() |
| 569 | { |
| 570 | if (mIn.dataPosition() >= mIn.dataSize()) { |
Martijn Coenen | 0791fbf | 2017-08-08 15:36:16 +0200 | [diff] [blame] | 571 | /* |
| 572 | * The decWeak()/decStrong() calls may cause a destructor to run, |
| 573 | * which in turn could have initiated an outgoing transaction, |
| 574 | * which in turn could cause us to add to the pending refs |
| 575 | * vectors; so instead of simply iterating, loop until they're empty. |
| 576 | * |
| 577 | * We do this in an outer loop, because calling decStrong() |
| 578 | * may result in something being added to mPendingWeakDerefs, |
| 579 | * which could be delayed until the next incoming command |
| 580 | * from the driver if we don't process it now. |
| 581 | */ |
| 582 | while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) { |
| 583 | while (mPendingWeakDerefs.size() > 0) { |
| 584 | RefBase::weakref_type* refs = mPendingWeakDerefs[0]; |
| 585 | mPendingWeakDerefs.removeAt(0); |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 586 | refs->decWeak(mProcess.get()); |
| 587 | } |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 588 | |
Martijn Coenen | 0791fbf | 2017-08-08 15:36:16 +0200 | [diff] [blame] | 589 | if (mPendingStrongDerefs.size() > 0) { |
| 590 | // We don't use while() here because we don't want to re-order |
| 591 | // strong and weak decs at all; if this decStrong() causes both a |
| 592 | // decWeak() and a decStrong() to be queued, we want to process |
| 593 | // the decWeak() first. |
| 594 | BBinder* obj = mPendingStrongDerefs[0]; |
| 595 | mPendingStrongDerefs.removeAt(0); |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 596 | obj->decStrong(mProcess.get()); |
| 597 | } |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 602 | void IPCThreadState::processPostWriteDerefs() |
| 603 | { |
| 604 | for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) { |
| 605 | RefBase::weakref_type* refs = mPostWriteWeakDerefs[i]; |
| 606 | refs->decWeak(mProcess.get()); |
| 607 | } |
| 608 | mPostWriteWeakDerefs.clear(); |
| 609 | |
| 610 | for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) { |
| 611 | RefBase* obj = mPostWriteStrongDerefs[i]; |
| 612 | obj->decStrong(mProcess.get()); |
| 613 | } |
| 614 | mPostWriteStrongDerefs.clear(); |
| 615 | } |
| 616 | |
Jintao Zhu | 413a00e | 2021-01-16 17:42:00 +0800 | [diff] [blame] | 617 | void IPCThreadState::createTransactionReference(RefBase* ref) |
| 618 | { |
| 619 | ref->incStrong(mProcess.get()); |
| 620 | mPostWriteStrongDerefs.push(ref); |
| 621 | } |
| 622 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | void IPCThreadState::joinThreadPool(bool isMain) |
| 624 | { |
| 625 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid()); |
| 626 | |
| 627 | mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 628 | |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 629 | mIsLooper = true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | status_t result; |
| 631 | do { |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 632 | processPendingDerefs(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | // now get the next command to be processed, waiting if necessary |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 634 | result = getAndExecuteCommand(); |
Jason Parks | dcd3958 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 635 | |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 636 | if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) { |
Steven Moreland | 6adf33c | 2019-09-25 13:18:09 -0700 | [diff] [blame] | 637 | LOG_ALWAYS_FATAL("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting", |
Jeff Tinker | ef07386 | 2013-06-11 11:30:21 -0700 | [diff] [blame] | 638 | mProcess->mDriverFD, result); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 640 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | // Let this thread exit the thread pool if it is no longer |
| 642 | // needed and it is not the main process thread. |
| 643 | if(result == TIMED_OUT && !isMain) { |
| 644 | break; |
| 645 | } |
| 646 | } while (result != -ECONNREFUSED && result != -EBADF); |
| 647 | |
Wei Wang | c734143 | 2016-10-19 10:23:59 -0700 | [diff] [blame] | 648 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n", |
| 649 | (void*)pthread_self(), getpid(), result); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 650 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | mOut.writeInt32(BC_EXIT_LOOPER); |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 652 | mIsLooper = false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | talkWithDriver(false); |
| 654 | } |
| 655 | |
Steven Moreland | d8c8567 | 2020-07-24 21:30:41 +0000 | [diff] [blame] | 656 | status_t IPCThreadState::setupPolling(int* fd) |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 657 | { |
Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 658 | if (mProcess->mDriverFD < 0) { |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 659 | return -EBADF; |
| 660 | } |
| 661 | |
| 662 | mOut.writeInt32(BC_ENTER_LOOPER); |
Steven Moreland | f210b50 | 2021-01-15 23:40:32 +0000 | [diff] [blame] | 663 | flushCommands(); |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 664 | *fd = mProcess->mDriverFD; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | status_t IPCThreadState::handlePolledCommands() |
| 669 | { |
| 670 | status_t result; |
| 671 | |
| 672 | do { |
| 673 | result = getAndExecuteCommand(); |
| 674 | } while (mIn.dataPosition() < mIn.dataSize()); |
| 675 | |
| 676 | processPendingDerefs(); |
| 677 | flushCommands(); |
| 678 | return result; |
| 679 | } |
| 680 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 681 | void IPCThreadState::stopProcess(bool /*immediate*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 683 | //ALOGI("**** STOPPING PROCESS"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | flushCommands(); |
| 685 | int fd = mProcess->mDriverFD; |
| 686 | mProcess->mDriverFD = -1; |
| 687 | close(fd); |
| 688 | //kill(getpid(), SIGKILL); |
| 689 | } |
| 690 | |
| 691 | status_t IPCThreadState::transact(int32_t handle, |
| 692 | uint32_t code, const Parcel& data, |
| 693 | Parcel* reply, uint32_t flags) |
| 694 | { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 695 | LOG_ALWAYS_FATAL_IF(data.isForRpc(), "Parcel constructed for RPC, but being used with binder."); |
| 696 | |
Ganesh Mahendran | 58e5daa | 2017-10-11 18:05:13 +0800 | [diff] [blame] | 697 | status_t err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | |
| 699 | flags |= TF_ACCEPT_FDS; |
| 700 | |
| 701 | IF_LOG_TRANSACTIONS() { |
| 702 | TextOutput::Bundle _b(alog); |
| 703 | alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " |
| 704 | << handle << " / code " << TypeCode(code) << ": " |
| 705 | << indent << data << dedent << endl; |
| 706 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 707 | |
Ganesh Mahendran | 58e5daa | 2017-10-11 18:05:13 +0800 | [diff] [blame] | 708 | LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(), |
| 709 | (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY"); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 710 | err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 711 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | if (err != NO_ERROR) { |
| 713 | if (reply) reply->setError(err); |
| 714 | return (mLastError = err); |
| 715 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 716 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | if ((flags & TF_ONE_WAY) == 0) { |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 718 | if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) { |
| 719 | if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) { |
Steven Moreland | 8cb34fc | 2019-05-13 11:44:55 -0700 | [diff] [blame] | 720 | ALOGE("Process making non-oneway call (code: %u) but is restricted.", code); |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 721 | CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(), |
| 722 | ANDROID_LOG_ERROR); |
| 723 | } else /* FATAL_IF_NOT_ONEWAY */ { |
Steven Moreland | fcc77f1 | 2020-09-01 01:16:11 +0000 | [diff] [blame] | 724 | LOG_ALWAYS_FATAL("Process may not make non-oneway calls (code: %u).", code); |
Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 728 | #if 0 |
| 729 | if (code == 4) { // relayout |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 730 | ALOGI(">>>>>> CALLING transaction 4"); |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 731 | } else { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 732 | ALOGI(">>>>>> CALLING transaction %d", code); |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 733 | } |
| 734 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | if (reply) { |
| 736 | err = waitForResponse(reply); |
| 737 | } else { |
| 738 | Parcel fakeReply; |
| 739 | err = waitForResponse(&fakeReply); |
| 740 | } |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 741 | #if 0 |
| 742 | if (code == 4) { // relayout |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 743 | ALOGI("<<<<<< RETURNING transaction 4"); |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 744 | } else { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 745 | ALOGI("<<<<<< RETURNING transaction %d", code); |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 746 | } |
| 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 | IF_LOG_TRANSACTIONS() { |
| 750 | TextOutput::Bundle _b(alog); |
| 751 | alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand " |
| 752 | << handle << ": "; |
| 753 | if (reply) alog << indent << *reply << dedent << endl; |
| 754 | else alog << "(none requested)" << endl; |
| 755 | } |
| 756 | } else { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 757 | err = waitForResponse(nullptr, nullptr); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 759 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | return err; |
| 761 | } |
| 762 | |
Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 763 | void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | { |
| 765 | LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle); |
| 766 | mOut.writeInt32(BC_ACQUIRE); |
| 767 | mOut.writeInt32(handle); |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 768 | if (!flushIfNeeded()) { |
| 769 | // Create a temp reference until the driver has handled this command. |
| 770 | proxy->incStrong(mProcess.get()); |
| 771 | mPostWriteStrongDerefs.push(proxy); |
| 772 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | void IPCThreadState::decStrongHandle(int32_t handle) |
| 776 | { |
| 777 | LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle); |
| 778 | mOut.writeInt32(BC_RELEASE); |
| 779 | mOut.writeInt32(handle); |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 780 | flushIfNeeded(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 783 | void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | { |
| 785 | LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle); |
| 786 | mOut.writeInt32(BC_INCREFS); |
| 787 | mOut.writeInt32(handle); |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 788 | if (!flushIfNeeded()) { |
| 789 | // Create a temp reference until the driver has handled this command. |
| 790 | proxy->getWeakRefs()->incWeak(mProcess.get()); |
| 791 | mPostWriteWeakDerefs.push(proxy->getWeakRefs()); |
| 792 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | void IPCThreadState::decWeakHandle(int32_t handle) |
| 796 | { |
| 797 | LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle); |
| 798 | mOut.writeInt32(BC_DECREFS); |
| 799 | mOut.writeInt32(handle); |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 800 | flushIfNeeded(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | status_t IPCThreadState::attemptIncStrongHandle(int32_t handle) |
| 804 | { |
Arve HjønnevÄg | 11cfdcc | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 805 | #if HAS_BC_ATTEMPT_ACQUIRE |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 806 | LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | mOut.writeInt32(BC_ATTEMPT_ACQUIRE); |
| 808 | mOut.writeInt32(0); // xxx was thread priority |
| 809 | mOut.writeInt32(handle); |
| 810 | status_t result = UNKNOWN_ERROR; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 811 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | waitForResponse(NULL, &result); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 813 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | #if LOG_REFCOUNTS |
liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 815 | ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | handle, result == NO_ERROR ? "SUCCESS" : "FAILURE"); |
| 817 | #endif |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 818 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | return result; |
Arve HjønnevÄg | 11cfdcc | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 820 | #else |
| 821 | (void)handle; |
| 822 | ALOGE("%s(%d): Not supported\n", __func__, handle); |
| 823 | return INVALID_OPERATION; |
| 824 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder) |
| 828 | { |
| 829 | #if LOG_REFCOUNTS |
liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 830 | ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | #endif |
Manoj Gupta | 9cec85b | 2017-09-19 16:34:29 -0700 | [diff] [blame] | 832 | self()->mProcess->expungeHandle(handle, binder); // NOLINT |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy) |
| 836 | { |
| 837 | mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION); |
| 838 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 839 | mOut.writePointer((uintptr_t)proxy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | return NO_ERROR; |
| 841 | } |
| 842 | |
| 843 | status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy) |
| 844 | { |
| 845 | mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION); |
| 846 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 847 | mOut.writePointer((uintptr_t)proxy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | return NO_ERROR; |
| 849 | } |
| 850 | |
| 851 | IPCThreadState::IPCThreadState() |
Frankie Chang | 617911d | 2021-05-18 13:08:05 +0800 | [diff] [blame^] | 852 | : mProcess(ProcessState::self()), |
| 853 | mServingStackPointer(nullptr), |
| 854 | mWorkSource(kUnsetWorkSource), |
| 855 | mPropagateWorkSource(false), |
| 856 | mIsLooper(false), |
| 857 | mIsFlushing(false), |
| 858 | mStrictModePolicy(0), |
| 859 | mLastTransactionBinderFlags(0), |
| 860 | mCallRestriction(mProcess->mCallRestriction) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | pthread_setspecific(gTLS, this); |
Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 862 | clearCaller(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | mIn.setDataCapacity(256); |
| 864 | mOut.setDataCapacity(256); |
| 865 | } |
| 866 | |
| 867 | IPCThreadState::~IPCThreadState() |
| 868 | { |
| 869 | } |
| 870 | |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 871 | status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags) |
| 872 | { |
| 873 | status_t err; |
| 874 | status_t statusBuffer; |
| 875 | err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer); |
| 876 | if (err < NO_ERROR) return err; |
| 877 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 878 | return waitForResponse(nullptr, nullptr); |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 879 | } |
| 880 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult) |
| 882 | { |
Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 883 | uint32_t cmd; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | int32_t err; |
| 885 | |
| 886 | while (1) { |
| 887 | if ((err=talkWithDriver()) < NO_ERROR) break; |
| 888 | err = mIn.errorCheck(); |
| 889 | if (err < NO_ERROR) break; |
| 890 | if (mIn.dataAvail() == 0) continue; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 891 | |
Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 892 | cmd = (uint32_t)mIn.readInt32(); |
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 | IF_LOG_COMMANDS() { |
| 895 | alog << "Processing waitForResponse Command: " |
| 896 | << getReturnString(cmd) << endl; |
| 897 | } |
| 898 | |
| 899 | switch (cmd) { |
Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 900 | case BR_ONEWAY_SPAM_SUSPECT: |
| 901 | ALOGE("Process seems to be sending too many oneway calls."); |
| 902 | CallStack::logStack("oneway spamming", CallStack::getCurrent().get(), |
| 903 | ANDROID_LOG_ERROR); |
| 904 | [[fallthrough]]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | case BR_TRANSACTION_COMPLETE: |
| 906 | if (!reply && !acquireResult) goto finish; |
| 907 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 908 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | case BR_DEAD_REPLY: |
| 910 | err = DEAD_OBJECT; |
| 911 | goto finish; |
| 912 | |
| 913 | case BR_FAILED_REPLY: |
| 914 | err = FAILED_TRANSACTION; |
| 915 | goto finish; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 916 | |
Marco Ballesio | 7ee1757 | 2020-09-08 10:30:03 -0700 | [diff] [blame] | 917 | case BR_FROZEN_REPLY: |
| 918 | err = FAILED_TRANSACTION; |
| 919 | goto finish; |
| 920 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | case BR_ACQUIRE_RESULT: |
| 922 | { |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 923 | ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | const int32_t result = mIn.readInt32(); |
| 925 | if (!acquireResult) continue; |
| 926 | *acquireResult = result ? NO_ERROR : INVALID_OPERATION; |
| 927 | } |
| 928 | goto finish; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 929 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | case BR_REPLY: |
| 931 | { |
| 932 | binder_transaction_data tr; |
| 933 | err = mIn.read(&tr, sizeof(tr)); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 934 | 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] | 935 | if (err != NO_ERROR) goto finish; |
| 936 | |
| 937 | if (reply) { |
| 938 | if ((tr.flags & TF_STATUS_CODE) == 0) { |
| 939 | reply->ipcSetDataReference( |
| 940 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 941 | tr.data_size, |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 942 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 943 | tr.offsets_size/sizeof(binder_size_t), |
Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 944 | freeBuffer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | } else { |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 946 | err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 947 | freeBuffer(nullptr, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 949 | tr.data_size, |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 950 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 951 | tr.offsets_size/sizeof(binder_size_t)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | } |
| 953 | } else { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 954 | freeBuffer(nullptr, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 956 | tr.data_size, |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 957 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 958 | tr.offsets_size/sizeof(binder_size_t)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | continue; |
| 960 | } |
| 961 | } |
| 962 | goto finish; |
| 963 | |
| 964 | default: |
| 965 | err = executeCommand(cmd); |
| 966 | if (err != NO_ERROR) goto finish; |
| 967 | break; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | finish: |
| 972 | if (err != NO_ERROR) { |
| 973 | if (acquireResult) *acquireResult = err; |
| 974 | if (reply) reply->setError(err); |
| 975 | mLastError = err; |
| 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::talkWithDriver(bool doReceive) |
| 982 | { |
Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 983 | if (mProcess->mDriverFD < 0) { |
Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 984 | return -EBADF; |
| 985 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 986 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | binder_write_read bwr; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 988 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | // Is the read buffer empty? |
| 990 | const bool needRead = mIn.dataPosition() >= mIn.dataSize(); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 991 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | // We don't want to write anything if we are still reading |
| 993 | // from data left in the input buffer and the caller |
| 994 | // has requested to read the next data. |
| 995 | const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 996 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | bwr.write_size = outAvail; |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 998 | bwr.write_buffer = (uintptr_t)mOut.data(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | |
| 1000 | // This is what we'll read. |
| 1001 | if (doReceive && needRead) { |
| 1002 | bwr.read_size = mIn.dataCapacity(); |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1003 | bwr.read_buffer = (uintptr_t)mIn.data(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | } else { |
| 1005 | bwr.read_size = 0; |
Ben Cheng | d640f89 | 2011-12-01 17:11:32 -0800 | [diff] [blame] | 1006 | bwr.read_buffer = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | } |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 1008 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | IF_LOG_COMMANDS() { |
| 1010 | TextOutput::Bundle _b(alog); |
| 1011 | if (outAvail != 0) { |
| 1012 | alog << "Sending commands to driver: " << indent; |
| 1013 | const void* cmds = (const void*)bwr.write_buffer; |
| 1014 | const void* end = ((const uint8_t*)cmds)+bwr.write_size; |
| 1015 | alog << HexDump(cmds, bwr.write_size) << endl; |
| 1016 | while (cmds < end) cmds = printCommand(alog, cmds); |
| 1017 | alog << dedent; |
| 1018 | } |
| 1019 | alog << "Size of receive buffer: " << bwr.read_size |
| 1020 | << ", needRead: " << needRead << ", doReceive: " << doReceive << endl; |
| 1021 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1022 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | // Return immediately if there is nothing to do. |
| 1024 | if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 1025 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | bwr.write_consumed = 0; |
| 1027 | bwr.read_consumed = 0; |
| 1028 | status_t err; |
| 1029 | do { |
| 1030 | IF_LOG_COMMANDS() { |
| 1031 | alog << "About to read/write, write size = " << mOut.dataSize() << endl; |
| 1032 | } |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1033 | #if defined(__ANDROID__) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0) |
| 1035 | err = NO_ERROR; |
| 1036 | else |
| 1037 | err = -errno; |
| 1038 | #else |
| 1039 | err = INVALID_OPERATION; |
| 1040 | #endif |
Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 1041 | if (mProcess->mDriverFD < 0) { |
Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1042 | err = -EBADF; |
| 1043 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | IF_LOG_COMMANDS() { |
| 1045 | alog << "Finished read/write, write size = " << mOut.dataSize() << endl; |
| 1046 | } |
| 1047 | } while (err == -EINTR); |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 1048 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | IF_LOG_COMMANDS() { |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1050 | alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: " |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | << bwr.write_consumed << " (of " << mOut.dataSize() |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1052 | << "), read consumed: " << bwr.read_consumed << endl; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | if (err >= NO_ERROR) { |
| 1056 | if (bwr.write_consumed > 0) { |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1057 | if (bwr.write_consumed < mOut.dataSize()) |
Steven Moreland | b077deb | 2020-04-16 16:22:52 -0700 | [diff] [blame] | 1058 | LOG_ALWAYS_FATAL("Driver did not consume write buffer. " |
| 1059 | "err: %s consumed: %zu of %zu", |
| 1060 | statusToString(err).c_str(), |
| 1061 | (size_t)bwr.write_consumed, |
| 1062 | mOut.dataSize()); |
Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 1063 | else { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | mOut.setDataSize(0); |
Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 1065 | processPostWriteDerefs(); |
| 1066 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | } |
| 1068 | if (bwr.read_consumed > 0) { |
| 1069 | mIn.setDataSize(bwr.read_consumed); |
| 1070 | mIn.setDataPosition(0); |
| 1071 | } |
| 1072 | IF_LOG_COMMANDS() { |
| 1073 | TextOutput::Bundle _b(alog); |
| 1074 | alog << "Remaining data size: " << mOut.dataSize() << endl; |
| 1075 | alog << "Received commands from driver: " << indent; |
| 1076 | const void* cmds = mIn.data(); |
| 1077 | const void* end = mIn.data() + mIn.dataSize(); |
| 1078 | alog << HexDump(cmds, mIn.dataSize()) << endl; |
| 1079 | while (cmds < end) cmds = printReturnCommand(alog, cmds); |
| 1080 | alog << dedent; |
| 1081 | } |
| 1082 | return NO_ERROR; |
| 1083 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1084 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | return err; |
| 1086 | } |
| 1087 | |
| 1088 | status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags, |
| 1089 | int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer) |
| 1090 | { |
| 1091 | binder_transaction_data tr; |
| 1092 | |
Arve HjønnevÄg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 1093 | 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] | 1094 | tr.target.handle = handle; |
| 1095 | tr.code = code; |
| 1096 | tr.flags = binderFlags; |
Evgeniy Stepanov | d547432 | 2011-04-21 14:15:00 +0400 | [diff] [blame] | 1097 | tr.cookie = 0; |
| 1098 | tr.sender_pid = 0; |
| 1099 | tr.sender_euid = 0; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1100 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | const status_t err = data.errorCheck(); |
| 1102 | if (err == NO_ERROR) { |
| 1103 | tr.data_size = data.ipcDataSize(); |
| 1104 | tr.data.ptr.buffer = data.ipcData(); |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1105 | tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | tr.data.ptr.offsets = data.ipcObjects(); |
| 1107 | } else if (statusBuffer) { |
| 1108 | tr.flags |= TF_STATUS_CODE; |
| 1109 | *statusBuffer = err; |
| 1110 | tr.data_size = sizeof(status_t); |
Arve HjønnevÄg | 87b30d0 | 2014-02-18 21:04:31 -0800 | [diff] [blame] | 1111 | tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | tr.offsets_size = 0; |
Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1113 | tr.data.ptr.offsets = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | } else { |
| 1115 | return (mLastError = err); |
| 1116 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1117 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | mOut.writeInt32(cmd); |
| 1119 | mOut.write(&tr, sizeof(tr)); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1120 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | return NO_ERROR; |
| 1122 | } |
| 1123 | |
| 1124 | sp<BBinder> the_context_object; |
| 1125 | |
Jiyong Park | 384328e | 2020-11-13 17:16:48 +0900 | [diff] [blame] | 1126 | void IPCThreadState::setTheContextObject(const sp<BBinder>& obj) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | { |
| 1128 | the_context_object = obj; |
| 1129 | } |
| 1130 | |
| 1131 | status_t IPCThreadState::executeCommand(int32_t cmd) |
| 1132 | { |
| 1133 | BBinder* obj; |
| 1134 | RefBase::weakref_type* refs; |
| 1135 | status_t result = NO_ERROR; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1136 | |
Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 1137 | switch ((uint32_t)cmd) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | case BR_ERROR: |
| 1139 | result = mIn.readInt32(); |
| 1140 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1141 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | case BR_OK: |
| 1143 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1144 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | case BR_ACQUIRE: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1146 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1147 | obj = (BBinder*)mIn.readPointer(); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1148 | ALOG_ASSERT(refs->refBase() == obj, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | "BR_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 1150 | refs, obj, refs->refBase()); |
| 1151 | obj->incStrong(mProcess.get()); |
| 1152 | IF_LOG_REMOTEREFS() { |
| 1153 | LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj); |
| 1154 | obj->printRefs(); |
| 1155 | } |
| 1156 | mOut.writeInt32(BC_ACQUIRE_DONE); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1157 | mOut.writePointer((uintptr_t)refs); |
| 1158 | mOut.writePointer((uintptr_t)obj); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1160 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | case BR_RELEASE: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1162 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1163 | obj = (BBinder*)mIn.readPointer(); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1164 | ALOG_ASSERT(refs->refBase() == obj, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | "BR_RELEASE: object %p does not match cookie %p (expected %p)", |
| 1166 | refs, obj, refs->refBase()); |
| 1167 | IF_LOG_REMOTEREFS() { |
| 1168 | LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj); |
| 1169 | obj->printRefs(); |
| 1170 | } |
| 1171 | mPendingStrongDerefs.push(obj); |
| 1172 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1173 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | case BR_INCREFS: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1175 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1176 | obj = (BBinder*)mIn.readPointer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1177 | refs->incWeak(mProcess.get()); |
| 1178 | mOut.writeInt32(BC_INCREFS_DONE); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1179 | mOut.writePointer((uintptr_t)refs); |
| 1180 | mOut.writePointer((uintptr_t)obj); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1182 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | case BR_DECREFS: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1184 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1185 | obj = (BBinder*)mIn.readPointer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | // NOTE: This assertion is not valid, because the object may no |
| 1187 | // longer exist (thus the (BBinder*)cast above resulting in a different |
| 1188 | // memory address). |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1189 | //ALOG_ASSERT(refs->refBase() == obj, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | // "BR_DECREFS: object %p does not match cookie %p (expected %p)", |
| 1191 | // refs, obj, refs->refBase()); |
| 1192 | mPendingWeakDerefs.push(refs); |
| 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_ATTEMPT_ACQUIRE: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1196 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1197 | obj = (BBinder*)mIn.readPointer(); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1198 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | { |
| 1200 | const bool success = refs->attemptIncStrong(mProcess.get()); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1201 | ALOG_ASSERT(success && refs->refBase() == obj, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 1203 | refs, obj, refs->refBase()); |
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 | mOut.writeInt32(BC_ACQUIRE_RESULT); |
| 1206 | mOut.writeInt32((int32_t)success); |
| 1207 | } |
| 1208 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1209 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1210 | case BR_TRANSACTION_SEC_CTX: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | case BR_TRANSACTION: |
| 1212 | { |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1213 | binder_transaction_data_secctx tr_secctx; |
| 1214 | binder_transaction_data& tr = tr_secctx.transaction_data; |
| 1215 | |
| 1216 | if (cmd == (int) BR_TRANSACTION_SEC_CTX) { |
| 1217 | result = mIn.read(&tr_secctx, sizeof(tr_secctx)); |
| 1218 | } else { |
| 1219 | result = mIn.read(&tr, sizeof(tr)); |
| 1220 | tr_secctx.secctx = 0; |
| 1221 | } |
| 1222 | |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1223 | ALOG_ASSERT(result == NO_ERROR, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | "Not enough command data for brTRANSACTION"); |
| 1225 | if (result != NO_ERROR) break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1226 | |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1227 | Parcel buffer; |
| 1228 | buffer.ipcSetDataReference( |
| 1229 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 1230 | tr.data_size, |
| 1231 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 1232 | tr.offsets_size/sizeof(binder_size_t), freeBuffer); |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1233 | |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 1234 | const void* origServingStackPointer = mServingStackPointer; |
| 1235 | mServingStackPointer = &origServingStackPointer; // anything on the stack |
| 1236 | |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1237 | const pid_t origPid = mCallingPid; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1238 | const char* origSid = mCallingSid; |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1239 | const uid_t origUid = mCallingUid; |
| 1240 | const int32_t origStrictModePolicy = mStrictModePolicy; |
| 1241 | const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags; |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 1242 | const int32_t origWorkSource = mWorkSource; |
| 1243 | const bool origPropagateWorkSet = mPropagateWorkSource; |
| 1244 | // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface |
| 1245 | // is only guaranteed to be called for AIDL-generated stubs so we reset the work source |
| 1246 | // here to never propagate it. |
| 1247 | clearCallingWorkSource(); |
| 1248 | clearPropagateWorkSource(); |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1249 | |
| 1250 | mCallingPid = tr.sender_pid; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1251 | mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx); |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1252 | mCallingUid = tr.sender_euid; |
| 1253 | mLastTransactionBinderFlags = tr.flags; |
| 1254 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1255 | // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid, |
| 1256 | // (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid); |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1257 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | Parcel reply; |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1259 | status_t error; |
| 1260 | IF_LOG_TRANSACTIONS() { |
| 1261 | TextOutput::Bundle _b(alog); |
| 1262 | alog << "BR_TRANSACTION thr " << (void*)pthread_self() |
| 1263 | << " / obj " << tr.target.ptr << " / code " |
| 1264 | << TypeCode(tr.code) << ": " << indent << buffer |
| 1265 | << dedent << endl |
| 1266 | << "Data addr = " |
| 1267 | << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer) |
| 1268 | << ", offsets addr=" |
| 1269 | << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl; |
| 1270 | } |
| 1271 | if (tr.target.ptr) { |
| 1272 | // We only have a weak reference on the target object, so we must first try to |
| 1273 | // safely acquire a strong reference before doing anything else with it. |
| 1274 | if (reinterpret_cast<RefBase::weakref_type*>( |
| 1275 | tr.target.ptr)->attemptIncStrong(this)) { |
| 1276 | error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer, |
| 1277 | &reply, tr.flags); |
| 1278 | reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this); |
Dianne Hackborn | c111461 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1279 | } else { |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1280 | error = UNKNOWN_TRANSACTION; |
Dianne Hackborn | c111461 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1281 | } |
Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 1282 | |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1283 | } else { |
| 1284 | 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] | 1285 | } |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1286 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1287 | //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n", |
| 1288 | // mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid); |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1289 | |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1290 | if ((tr.flags & TF_ONE_WAY) == 0) { |
| 1291 | LOG_ONEWAY("Sending reply to %d!", mCallingPid); |
| 1292 | if (error < NO_ERROR) reply.setError(error); |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 1293 | |
| 1294 | constexpr uint32_t kForwardReplyFlags = TF_CLEAR_BUF; |
| 1295 | sendReply(reply, (tr.flags & kForwardReplyFlags)); |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1296 | } else { |
Steven Moreland | 80844f7 | 2020-12-12 02:06:08 +0000 | [diff] [blame] | 1297 | if (error != OK) { |
| 1298 | alog << "oneway function results for code " << tr.code |
| 1299 | << " on binder at " |
| 1300 | << reinterpret_cast<void*>(tr.target.ptr) |
| 1301 | << " will be dropped but finished with status " |
| 1302 | << statusToString(error); |
| 1303 | |
| 1304 | // ideally we could log this even when error == OK, but it |
| 1305 | // causes too much logspam because some manually-written |
| 1306 | // interfaces have clients that call methods which always |
| 1307 | // write results, sometimes as oneway methods. |
| 1308 | if (reply.dataSize() != 0) { |
| 1309 | alog << " and reply parcel size " << reply.dataSize(); |
| 1310 | } |
| 1311 | |
| 1312 | alog << endl; |
Steven Moreland | ce66b8a | 2020-02-10 14:43:14 -0800 | [diff] [blame] | 1313 | } |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1314 | LOG_ONEWAY("NOT sending reply to %d!", mCallingPid); |
| 1315 | } |
| 1316 | |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 1317 | mServingStackPointer = origServingStackPointer; |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1318 | mCallingPid = origPid; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1319 | mCallingSid = origSid; |
Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1320 | mCallingUid = origUid; |
| 1321 | mStrictModePolicy = origStrictModePolicy; |
| 1322 | mLastTransactionBinderFlags = origTransactionBinderFlags; |
Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 1323 | mWorkSource = origWorkSource; |
| 1324 | mPropagateWorkSource = origPropagateWorkSet; |
Christopher Tate | 440fd87 | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1325 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | IF_LOG_TRANSACTIONS() { |
| 1327 | TextOutput::Bundle _b(alog); |
| 1328 | alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj " |
| 1329 | << tr.target.ptr << ": " << indent << reply << dedent << endl; |
| 1330 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1331 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | } |
| 1333 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1334 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | case BR_DEAD_BINDER: |
| 1336 | { |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1337 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | proxy->sendObituary(); |
| 1339 | mOut.writeInt32(BC_DEAD_BINDER_DONE); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1340 | mOut.writePointer((uintptr_t)proxy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1341 | } break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1342 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: |
| 1344 | { |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1345 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | proxy->getWeakRefs()->decWeak(proxy); |
| 1347 | } break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1348 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | case BR_FINISHED: |
| 1350 | result = TIMED_OUT; |
| 1351 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1352 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | case BR_NOOP: |
| 1354 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1355 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | case BR_SPAWN_LOOPER: |
| 1357 | mProcess->spawnPooledThread(false); |
| 1358 | break; |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1359 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | default: |
liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 1361 | 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] | 1362 | result = UNKNOWN_ERROR; |
| 1363 | break; |
| 1364 | } |
| 1365 | |
| 1366 | if (result != NO_ERROR) { |
| 1367 | mLastError = result; |
| 1368 | } |
Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1369 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | return result; |
| 1371 | } |
| 1372 | |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 1373 | const void* IPCThreadState::getServingStackPointer() const { |
| 1374 | return mServingStackPointer; |
Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | void IPCThreadState::threadDestructor(void *st) |
| 1378 | { |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1379 | IPCThreadState* const self = static_cast<IPCThreadState*>(st); |
| 1380 | if (self) { |
| 1381 | self->flushCommands(); |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1382 | #if defined(__ANDROID__) |
Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 1383 | if (self->mProcess->mDriverFD >= 0) { |
Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1384 | ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0); |
| 1385 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1386 | #endif |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1387 | delete self; |
| 1388 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
Marco Ballesio | b09fc4a | 2020-09-11 16:17:21 -0700 | [diff] [blame] | 1391 | status_t IPCThreadState::getProcessFreezeInfo(pid_t pid, bool *sync_received, bool *async_received) |
| 1392 | { |
| 1393 | int ret = 0; |
| 1394 | binder_frozen_status_info info; |
| 1395 | info.pid = pid; |
| 1396 | |
| 1397 | #if defined(__ANDROID__) |
| 1398 | if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_FROZEN_INFO, &info) < 0) |
| 1399 | ret = -errno; |
| 1400 | #endif |
| 1401 | *sync_received = info.sync_recv; |
| 1402 | *async_received = info.async_recv; |
| 1403 | |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
Marco Ballesio | 7ee1757 | 2020-09-08 10:30:03 -0700 | [diff] [blame] | 1407 | status_t IPCThreadState::freeze(pid_t pid, bool enable, uint32_t timeout_ms) { |
| 1408 | struct binder_freeze_info info; |
| 1409 | int ret = 0; |
| 1410 | |
| 1411 | info.pid = pid; |
| 1412 | info.enable = enable; |
| 1413 | info.timeout_ms = timeout_ms; |
| 1414 | |
| 1415 | |
| 1416 | #if defined(__ANDROID__) |
| 1417 | if (ioctl(self()->mProcess->mDriverFD, BINDER_FREEZE, &info) < 0) |
| 1418 | ret = -errno; |
| 1419 | #endif |
| 1420 | |
| 1421 | // |
| 1422 | // ret==-EAGAIN indicates that transactions have not drained. |
| 1423 | // Call again to poll for completion. |
| 1424 | // |
| 1425 | return ret; |
| 1426 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1428 | void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data, |
| 1429 | size_t /*dataSize*/, |
| 1430 | const binder_size_t* /*objects*/, |
Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 1431 | size_t /*objectsSize*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1433 | //ALOGI("Freeing parcel %p", &parcel); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | IF_LOG_COMMANDS() { |
| 1435 | alog << "Writing BC_FREE_BUFFER for " << data << endl; |
| 1436 | } |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1437 | ALOG_ASSERT(data != NULL, "Called with NULL data"); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 1438 | if (parcel != nullptr) parcel->closeFileDescriptors(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | IPCThreadState* state = self(); |
| 1440 | state->mOut.writeInt32(BC_FREE_BUFFER); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1441 | state->mOut.writePointer((uintptr_t)data); |
Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 1442 | state->flushIfNeeded(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | } |
| 1444 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 1445 | } // namespace android |