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