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