The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jason Parks | dcd3958 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 17 | #define LOG_TAG "IPCThreadState" |
| 18 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
| 22 | #include <binder/BpBinder.h> |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 23 | #include <binder/TextOutput.h> |
| 24 | |
Glenn Kasten | a26e1cf | 2012-03-16 07:15:23 -0700 | [diff] [blame] | 25 | #include <cutils/sched_policy.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 27 | #include <utils/SystemClock.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <utils/threads.h> |
| 29 | |
Mathias Agopian | 208059f | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 30 | #include <private/binder/binder_module.h> |
| 31 | #include <private/binder/Static.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include <errno.h> |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 34 | #include <inttypes.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include <pthread.h> |
| 36 | #include <sched.h> |
Yabin Cui | 8fb2d25 | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 37 | #include <signal.h> |
| 38 | #include <stdio.h> |
| 39 | #include <sys/ioctl.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | #include <sys/resource.h> |
Yabin Cui | 8fb2d25 | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 41 | #include <unistd.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | #if LOG_NDEBUG |
| 44 | |
| 45 | #define IF_LOG_TRANSACTIONS() if (false) |
| 46 | #define IF_LOG_COMMANDS() if (false) |
| 47 | #define LOG_REMOTEREFS(...) |
| 48 | #define IF_LOG_REMOTEREFS() if (false) |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 49 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | #define LOG_THREADPOOL(...) |
| 51 | #define LOG_ONEWAY(...) |
| 52 | |
| 53 | #else |
| 54 | |
Steve Block | 9f76015 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 55 | #define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact") |
| 56 | #define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc") |
| 57 | #define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__) |
| 58 | #define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs") |
| 59 | #define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__) |
| 60 | #define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
| 62 | #endif |
| 63 | |
| 64 | // --------------------------------------------------------------------------- |
| 65 | |
| 66 | namespace android { |
| 67 | |
| 68 | static const char* getReturnString(size_t idx); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | static const void* printReturnCommand(TextOutput& out, const void* _cmd); |
| 70 | static const void* printCommand(TextOutput& out, const void* _cmd); |
| 71 | |
Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 72 | // Static const and functions will be optimized out if not used, |
| 73 | // 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] | 74 | static const char *kReturnStrings[] = { |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 75 | "BR_ERROR", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | "BR_OK", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | "BR_TRANSACTION", |
| 78 | "BR_REPLY", |
| 79 | "BR_ACQUIRE_RESULT", |
| 80 | "BR_DEAD_REPLY", |
| 81 | "BR_TRANSACTION_COMPLETE", |
| 82 | "BR_INCREFS", |
| 83 | "BR_ACQUIRE", |
| 84 | "BR_RELEASE", |
| 85 | "BR_DECREFS", |
| 86 | "BR_ATTEMPT_ACQUIRE", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | "BR_NOOP", |
| 88 | "BR_SPAWN_LOOPER", |
| 89 | "BR_FINISHED", |
| 90 | "BR_DEAD_BINDER", |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 91 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 92 | "BR_FAILED_REPLY" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | static const char *kCommandStrings[] = { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | "BC_TRANSACTION", |
| 97 | "BC_REPLY", |
| 98 | "BC_ACQUIRE_RESULT", |
| 99 | "BC_FREE_BUFFER", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | "BC_INCREFS", |
| 101 | "BC_ACQUIRE", |
| 102 | "BC_RELEASE", |
| 103 | "BC_DECREFS", |
| 104 | "BC_INCREFS_DONE", |
| 105 | "BC_ACQUIRE_DONE", |
| 106 | "BC_ATTEMPT_ACQUIRE", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | "BC_REGISTER_LOOPER", |
| 108 | "BC_ENTER_LOOPER", |
| 109 | "BC_EXIT_LOOPER", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 111 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 112 | "BC_DEAD_BINDER_DONE" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static const char* getReturnString(size_t idx) |
| 116 | { |
| 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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 | 05f4679 | 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"); |
| 295 | return NULL; |
| 296 | } |
Tim Murray | 05f4679 | 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)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | return NULL; |
| 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 | } |
| 320 | return NULL; |
| 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 | 05f4679 | 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; |
| 332 | pthread_setspecific(gTLS, NULL); |
| 333 | } |
| 334 | gHaveTLS = false; |
| 335 | } |
| 336 | } |
| 337 | |
Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 338 | void IPCThreadState::disableBackgroundScheduling(bool disable) |
| 339 | { |
| 340 | gDisableBackgroundScheduling = disable; |
| 341 | } |
| 342 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | sp<ProcessState> IPCThreadState::process() |
| 344 | { |
| 345 | return mProcess; |
| 346 | } |
| 347 | |
| 348 | status_t IPCThreadState::clearLastError() |
| 349 | { |
| 350 | const status_t err = mLastError; |
| 351 | mLastError = NO_ERROR; |
| 352 | return err; |
| 353 | } |
| 354 | |
Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 355 | pid_t IPCThreadState::getCallingPid() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | { |
| 357 | return mCallingPid; |
| 358 | } |
| 359 | |
Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 360 | uid_t IPCThreadState::getCallingUid() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | { |
| 362 | return mCallingUid; |
| 363 | } |
| 364 | |
| 365 | int64_t IPCThreadState::clearCallingIdentity() |
| 366 | { |
| 367 | int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid; |
| 368 | clearCaller(); |
| 369 | return token; |
| 370 | } |
| 371 | |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 372 | void IPCThreadState::setStrictModePolicy(int32_t policy) |
| 373 | { |
| 374 | mStrictModePolicy = policy; |
| 375 | } |
| 376 | |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 377 | int32_t IPCThreadState::getStrictModePolicy() const |
| 378 | { |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 379 | return mStrictModePolicy; |
| 380 | } |
| 381 | |
Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 382 | void IPCThreadState::setLastTransactionBinderFlags(int32_t flags) |
| 383 | { |
| 384 | mLastTransactionBinderFlags = flags; |
| 385 | } |
| 386 | |
| 387 | int32_t IPCThreadState::getLastTransactionBinderFlags() const |
| 388 | { |
| 389 | return mLastTransactionBinderFlags; |
| 390 | } |
| 391 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | void IPCThreadState::restoreCallingIdentity(int64_t token) |
| 393 | { |
| 394 | mCallingUid = (int)(token>>32); |
| 395 | mCallingPid = (int)token; |
| 396 | } |
| 397 | |
| 398 | void IPCThreadState::clearCaller() |
| 399 | { |
Marco Nelissen | d43b194 | 2009-07-17 07:59:17 -0700 | [diff] [blame] | 400 | mCallingPid = getpid(); |
| 401 | mCallingUid = getuid(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void IPCThreadState::flushCommands() |
| 405 | { |
| 406 | if (mProcess->mDriverFD <= 0) |
| 407 | return; |
| 408 | talkWithDriver(false); |
| 409 | } |
| 410 | |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 411 | void IPCThreadState::blockUntilThreadAvailable() |
| 412 | { |
| 413 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 414 | while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) { |
Wale Ogunwale | a3206e6 | 2015-04-21 12:29:50 -0700 | [diff] [blame] | 415 | ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n", |
| 416 | static_cast<unsigned long>(mProcess->mExecutingThreadsCount), |
| 417 | static_cast<unsigned long>(mProcess->mMaxThreads)); |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 418 | pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock); |
| 419 | } |
| 420 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 421 | } |
| 422 | |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 423 | status_t IPCThreadState::getAndExecuteCommand() |
| 424 | { |
| 425 | status_t result; |
| 426 | int32_t cmd; |
| 427 | |
| 428 | result = talkWithDriver(); |
| 429 | if (result >= NO_ERROR) { |
| 430 | size_t IN = mIn.dataAvail(); |
| 431 | if (IN < sizeof(int32_t)) return result; |
| 432 | cmd = mIn.readInt32(); |
| 433 | IF_LOG_COMMANDS() { |
| 434 | alog << "Processing top-level Command: " |
| 435 | << getReturnString(cmd) << endl; |
| 436 | } |
| 437 | |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 438 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 439 | mProcess->mExecutingThreadsCount++; |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 440 | if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads && |
| 441 | mProcess->mStarvationStartTimeMs == 0) { |
| 442 | mProcess->mStarvationStartTimeMs = uptimeMillis(); |
| 443 | } |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 444 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
| 445 | |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 446 | result = executeCommand(cmd); |
| 447 | |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 448 | pthread_mutex_lock(&mProcess->mThreadCountLock); |
| 449 | mProcess->mExecutingThreadsCount--; |
Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 450 | if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads && |
| 451 | mProcess->mStarvationStartTimeMs != 0) { |
| 452 | int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs; |
| 453 | if (starvationTimeMs > 100) { |
| 454 | ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms", |
| 455 | mProcess->mMaxThreads, starvationTimeMs); |
| 456 | } |
| 457 | mProcess->mStarvationStartTimeMs = 0; |
| 458 | } |
Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 459 | pthread_cond_broadcast(&mProcess->mThreadCountDecrement); |
| 460 | pthread_mutex_unlock(&mProcess->mThreadCountLock); |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | return result; |
| 464 | } |
| 465 | |
| 466 | // When we've cleared the incoming command queue, process any pending derefs |
| 467 | void IPCThreadState::processPendingDerefs() |
| 468 | { |
| 469 | if (mIn.dataPosition() >= mIn.dataSize()) { |
| 470 | size_t numPending = mPendingWeakDerefs.size(); |
| 471 | if (numPending > 0) { |
| 472 | for (size_t i = 0; i < numPending; i++) { |
| 473 | RefBase::weakref_type* refs = mPendingWeakDerefs[i]; |
| 474 | refs->decWeak(mProcess.get()); |
| 475 | } |
| 476 | mPendingWeakDerefs.clear(); |
| 477 | } |
| 478 | |
| 479 | numPending = mPendingStrongDerefs.size(); |
| 480 | if (numPending > 0) { |
| 481 | for (size_t i = 0; i < numPending; i++) { |
| 482 | BBinder* obj = mPendingStrongDerefs[i]; |
| 483 | obj->decStrong(mProcess.get()); |
| 484 | } |
| 485 | mPendingStrongDerefs.clear(); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | void IPCThreadState::joinThreadPool(bool isMain) |
| 491 | { |
| 492 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid()); |
| 493 | |
| 494 | mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 495 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | status_t result; |
| 497 | do { |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 498 | processPendingDerefs(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | // now get the next command to be processed, waiting if necessary |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 500 | result = getAndExecuteCommand(); |
Jason Parks | dcd3958 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 501 | |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 502 | if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) { |
| 503 | ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting", |
Jeff Tinker | ef07386 | 2013-06-11 11:30:21 -0700 | [diff] [blame] | 504 | mProcess->mDriverFD, result); |
| 505 | abort(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 507 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | // Let this thread exit the thread pool if it is no longer |
| 509 | // needed and it is not the main process thread. |
| 510 | if(result == TIMED_OUT && !isMain) { |
| 511 | break; |
| 512 | } |
| 513 | } while (result != -ECONNREFUSED && result != -EBADF); |
| 514 | |
| 515 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%p\n", |
| 516 | (void*)pthread_self(), getpid(), (void*)result); |
| 517 | |
| 518 | mOut.writeInt32(BC_EXIT_LOOPER); |
| 519 | talkWithDriver(false); |
| 520 | } |
| 521 | |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 522 | int IPCThreadState::setupPolling(int* fd) |
| 523 | { |
| 524 | if (mProcess->mDriverFD <= 0) { |
| 525 | return -EBADF; |
| 526 | } |
| 527 | |
| 528 | mOut.writeInt32(BC_ENTER_LOOPER); |
| 529 | *fd = mProcess->mDriverFD; |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | status_t IPCThreadState::handlePolledCommands() |
| 534 | { |
| 535 | status_t result; |
| 536 | |
| 537 | do { |
| 538 | result = getAndExecuteCommand(); |
| 539 | } while (mIn.dataPosition() < mIn.dataSize()); |
| 540 | |
| 541 | processPendingDerefs(); |
| 542 | flushCommands(); |
| 543 | return result; |
| 544 | } |
| 545 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 546 | void IPCThreadState::stopProcess(bool /*immediate*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 548 | //ALOGI("**** STOPPING PROCESS"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | flushCommands(); |
| 550 | int fd = mProcess->mDriverFD; |
| 551 | mProcess->mDriverFD = -1; |
| 552 | close(fd); |
| 553 | //kill(getpid(), SIGKILL); |
| 554 | } |
| 555 | |
| 556 | status_t IPCThreadState::transact(int32_t handle, |
| 557 | uint32_t code, const Parcel& data, |
| 558 | Parcel* reply, uint32_t flags) |
| 559 | { |
| 560 | status_t err = data.errorCheck(); |
| 561 | |
| 562 | flags |= TF_ACCEPT_FDS; |
| 563 | |
| 564 | IF_LOG_TRANSACTIONS() { |
| 565 | TextOutput::Bundle _b(alog); |
| 566 | alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " |
| 567 | << handle << " / code " << TypeCode(code) << ": " |
| 568 | << indent << data << dedent << endl; |
| 569 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 570 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | if (err == NO_ERROR) { |
| 572 | LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(), |
| 573 | (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY"); |
| 574 | err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL); |
| 575 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 576 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | if (err != NO_ERROR) { |
| 578 | if (reply) reply->setError(err); |
| 579 | return (mLastError = err); |
| 580 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 581 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | if ((flags & TF_ONE_WAY) == 0) { |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 583 | #if 0 |
| 584 | if (code == 4) { // relayout |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 585 | ALOGI(">>>>>> CALLING transaction 4"); |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 586 | } else { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 587 | ALOGI(">>>>>> CALLING transaction %d", code); |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 588 | } |
| 589 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | if (reply) { |
| 591 | err = waitForResponse(reply); |
| 592 | } else { |
| 593 | Parcel fakeReply; |
| 594 | err = waitForResponse(&fakeReply); |
| 595 | } |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 596 | #if 0 |
| 597 | if (code == 4) { // relayout |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 598 | ALOGI("<<<<<< RETURNING transaction 4"); |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 599 | } else { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 600 | ALOGI("<<<<<< RETURNING transaction %d", code); |
Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 601 | } |
| 602 | #endif |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 603 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | IF_LOG_TRANSACTIONS() { |
| 605 | TextOutput::Bundle _b(alog); |
| 606 | alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand " |
| 607 | << handle << ": "; |
| 608 | if (reply) alog << indent << *reply << dedent << endl; |
| 609 | else alog << "(none requested)" << endl; |
| 610 | } |
| 611 | } else { |
| 612 | err = waitForResponse(NULL, NULL); |
| 613 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 614 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | return err; |
| 616 | } |
| 617 | |
| 618 | void IPCThreadState::incStrongHandle(int32_t handle) |
| 619 | { |
| 620 | LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle); |
| 621 | mOut.writeInt32(BC_ACQUIRE); |
| 622 | mOut.writeInt32(handle); |
| 623 | } |
| 624 | |
| 625 | void IPCThreadState::decStrongHandle(int32_t handle) |
| 626 | { |
| 627 | LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle); |
| 628 | mOut.writeInt32(BC_RELEASE); |
| 629 | mOut.writeInt32(handle); |
| 630 | } |
| 631 | |
| 632 | void IPCThreadState::incWeakHandle(int32_t handle) |
| 633 | { |
| 634 | LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle); |
| 635 | mOut.writeInt32(BC_INCREFS); |
| 636 | mOut.writeInt32(handle); |
| 637 | } |
| 638 | |
| 639 | void IPCThreadState::decWeakHandle(int32_t handle) |
| 640 | { |
| 641 | LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle); |
| 642 | mOut.writeInt32(BC_DECREFS); |
| 643 | mOut.writeInt32(handle); |
| 644 | } |
| 645 | |
| 646 | status_t IPCThreadState::attemptIncStrongHandle(int32_t handle) |
| 647 | { |
Arve Hjønnevåg | 11cfdcc | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 648 | #if HAS_BC_ATTEMPT_ACQUIRE |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 649 | LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | mOut.writeInt32(BC_ATTEMPT_ACQUIRE); |
| 651 | mOut.writeInt32(0); // xxx was thread priority |
| 652 | mOut.writeInt32(handle); |
| 653 | status_t result = UNKNOWN_ERROR; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 654 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | waitForResponse(NULL, &result); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 656 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | #if LOG_REFCOUNTS |
| 658 | printf("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n", |
| 659 | handle, result == NO_ERROR ? "SUCCESS" : "FAILURE"); |
| 660 | #endif |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 661 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | return result; |
Arve Hjønnevåg | 11cfdcc | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 663 | #else |
| 664 | (void)handle; |
| 665 | ALOGE("%s(%d): Not supported\n", __func__, handle); |
| 666 | return INVALID_OPERATION; |
| 667 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder) |
| 671 | { |
| 672 | #if LOG_REFCOUNTS |
| 673 | printf("IPCThreadState::expungeHandle(%ld)\n", handle); |
| 674 | #endif |
| 675 | self()->mProcess->expungeHandle(handle, binder); |
| 676 | } |
| 677 | |
| 678 | status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy) |
| 679 | { |
| 680 | mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION); |
| 681 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 682 | mOut.writePointer((uintptr_t)proxy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | return NO_ERROR; |
| 684 | } |
| 685 | |
| 686 | status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy) |
| 687 | { |
| 688 | mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION); |
| 689 | mOut.writeInt32((int32_t)handle); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 690 | mOut.writePointer((uintptr_t)proxy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | return NO_ERROR; |
| 692 | } |
| 693 | |
| 694 | IPCThreadState::IPCThreadState() |
Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 695 | : mProcess(ProcessState::self()), |
Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 696 | mStrictModePolicy(0), |
| 697 | mLastTransactionBinderFlags(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | { |
| 699 | pthread_setspecific(gTLS, this); |
Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 700 | clearCaller(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | mIn.setDataCapacity(256); |
| 702 | mOut.setDataCapacity(256); |
| 703 | } |
| 704 | |
| 705 | IPCThreadState::~IPCThreadState() |
| 706 | { |
| 707 | } |
| 708 | |
| 709 | status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags) |
| 710 | { |
| 711 | status_t err; |
| 712 | status_t statusBuffer; |
| 713 | err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer); |
| 714 | if (err < NO_ERROR) return err; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 715 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | return waitForResponse(NULL, NULL); |
| 717 | } |
| 718 | |
| 719 | status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult) |
| 720 | { |
Bernhard Rosenkränzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 721 | uint32_t cmd; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | int32_t err; |
| 723 | |
| 724 | while (1) { |
| 725 | if ((err=talkWithDriver()) < NO_ERROR) break; |
| 726 | err = mIn.errorCheck(); |
| 727 | if (err < NO_ERROR) break; |
| 728 | if (mIn.dataAvail() == 0) continue; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 729 | |
Bernhard Rosenkränzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 730 | cmd = (uint32_t)mIn.readInt32(); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 731 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | IF_LOG_COMMANDS() { |
| 733 | alog << "Processing waitForResponse Command: " |
| 734 | << getReturnString(cmd) << endl; |
| 735 | } |
| 736 | |
| 737 | switch (cmd) { |
| 738 | case BR_TRANSACTION_COMPLETE: |
| 739 | if (!reply && !acquireResult) goto finish; |
| 740 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 741 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | case BR_DEAD_REPLY: |
| 743 | err = DEAD_OBJECT; |
| 744 | goto finish; |
| 745 | |
| 746 | case BR_FAILED_REPLY: |
| 747 | err = FAILED_TRANSACTION; |
| 748 | goto finish; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 749 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | case BR_ACQUIRE_RESULT: |
| 751 | { |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 752 | ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | const int32_t result = mIn.readInt32(); |
| 754 | if (!acquireResult) continue; |
| 755 | *acquireResult = result ? NO_ERROR : INVALID_OPERATION; |
| 756 | } |
| 757 | goto finish; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 758 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | case BR_REPLY: |
| 760 | { |
| 761 | binder_transaction_data tr; |
| 762 | err = mIn.read(&tr, sizeof(tr)); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 763 | 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] | 764 | if (err != NO_ERROR) goto finish; |
| 765 | |
| 766 | if (reply) { |
| 767 | if ((tr.flags & TF_STATUS_CODE) == 0) { |
| 768 | reply->ipcSetDataReference( |
| 769 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 770 | tr.data_size, |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 771 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 772 | tr.offsets_size/sizeof(binder_size_t), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | freeBuffer, this); |
| 774 | } else { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 775 | err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | freeBuffer(NULL, |
| 777 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 778 | tr.data_size, |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 779 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 780 | tr.offsets_size/sizeof(binder_size_t), this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | } |
| 782 | } else { |
| 783 | freeBuffer(NULL, |
| 784 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 785 | tr.data_size, |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 786 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 787 | tr.offsets_size/sizeof(binder_size_t), this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | continue; |
| 789 | } |
| 790 | } |
| 791 | goto finish; |
| 792 | |
| 793 | default: |
| 794 | err = executeCommand(cmd); |
| 795 | if (err != NO_ERROR) goto finish; |
| 796 | break; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | finish: |
| 801 | if (err != NO_ERROR) { |
| 802 | if (acquireResult) *acquireResult = err; |
| 803 | if (reply) reply->setError(err); |
| 804 | mLastError = err; |
| 805 | } |
Tim Murray | 05f4679 | 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 | return err; |
| 808 | } |
| 809 | |
| 810 | status_t IPCThreadState::talkWithDriver(bool doReceive) |
| 811 | { |
Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 812 | if (mProcess->mDriverFD <= 0) { |
| 813 | return -EBADF; |
| 814 | } |
Tim Murray | 05f4679 | 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 | binder_write_read bwr; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 817 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | // Is the read buffer empty? |
| 819 | const bool needRead = mIn.dataPosition() >= mIn.dataSize(); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 820 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | // We don't want to write anything if we are still reading |
| 822 | // from data left in the input buffer and the caller |
| 823 | // has requested to read the next data. |
| 824 | const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 825 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | bwr.write_size = outAvail; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 827 | bwr.write_buffer = (uintptr_t)mOut.data(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | |
| 829 | // This is what we'll read. |
| 830 | if (doReceive && needRead) { |
| 831 | bwr.read_size = mIn.dataCapacity(); |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 832 | bwr.read_buffer = (uintptr_t)mIn.data(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | } else { |
| 834 | bwr.read_size = 0; |
Ben Cheng | d640f89 | 2011-12-01 17:11:32 -0800 | [diff] [blame] | 835 | bwr.read_buffer = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | } |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 837 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | IF_LOG_COMMANDS() { |
| 839 | TextOutput::Bundle _b(alog); |
| 840 | if (outAvail != 0) { |
| 841 | alog << "Sending commands to driver: " << indent; |
| 842 | const void* cmds = (const void*)bwr.write_buffer; |
| 843 | const void* end = ((const uint8_t*)cmds)+bwr.write_size; |
| 844 | alog << HexDump(cmds, bwr.write_size) << endl; |
| 845 | while (cmds < end) cmds = printCommand(alog, cmds); |
| 846 | alog << dedent; |
| 847 | } |
| 848 | alog << "Size of receive buffer: " << bwr.read_size |
| 849 | << ", needRead: " << needRead << ", doReceive: " << doReceive << endl; |
| 850 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 851 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | // Return immediately if there is nothing to do. |
| 853 | if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR; |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 854 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | bwr.write_consumed = 0; |
| 856 | bwr.read_consumed = 0; |
| 857 | status_t err; |
| 858 | do { |
| 859 | IF_LOG_COMMANDS() { |
| 860 | alog << "About to read/write, write size = " << mOut.dataSize() << endl; |
| 861 | } |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 862 | #if defined(__ANDROID__) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0) |
| 864 | err = NO_ERROR; |
| 865 | else |
| 866 | err = -errno; |
| 867 | #else |
| 868 | err = INVALID_OPERATION; |
| 869 | #endif |
Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 870 | if (mProcess->mDriverFD <= 0) { |
| 871 | err = -EBADF; |
| 872 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | IF_LOG_COMMANDS() { |
| 874 | alog << "Finished read/write, write size = " << mOut.dataSize() << endl; |
| 875 | } |
| 876 | } while (err == -EINTR); |
Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 877 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | IF_LOG_COMMANDS() { |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 879 | alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: " |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | << bwr.write_consumed << " (of " << mOut.dataSize() |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 881 | << "), read consumed: " << bwr.read_consumed << endl; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | if (err >= NO_ERROR) { |
| 885 | if (bwr.write_consumed > 0) { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 886 | if (bwr.write_consumed < mOut.dataSize()) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | mOut.remove(0, bwr.write_consumed); |
| 888 | else |
| 889 | mOut.setDataSize(0); |
| 890 | } |
| 891 | if (bwr.read_consumed > 0) { |
| 892 | mIn.setDataSize(bwr.read_consumed); |
| 893 | mIn.setDataPosition(0); |
| 894 | } |
| 895 | IF_LOG_COMMANDS() { |
| 896 | TextOutput::Bundle _b(alog); |
| 897 | alog << "Remaining data size: " << mOut.dataSize() << endl; |
| 898 | alog << "Received commands from driver: " << indent; |
| 899 | const void* cmds = mIn.data(); |
| 900 | const void* end = mIn.data() + mIn.dataSize(); |
| 901 | alog << HexDump(cmds, mIn.dataSize()) << endl; |
| 902 | while (cmds < end) cmds = printReturnCommand(alog, cmds); |
| 903 | alog << dedent; |
| 904 | } |
| 905 | return NO_ERROR; |
| 906 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 907 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | return err; |
| 909 | } |
| 910 | |
| 911 | status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags, |
| 912 | int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer) |
| 913 | { |
| 914 | binder_transaction_data tr; |
| 915 | |
Arve Hjønnevåg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 916 | 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] | 917 | tr.target.handle = handle; |
| 918 | tr.code = code; |
| 919 | tr.flags = binderFlags; |
Evgeniy Stepanov | d547432 | 2011-04-21 14:15:00 +0400 | [diff] [blame] | 920 | tr.cookie = 0; |
| 921 | tr.sender_pid = 0; |
| 922 | tr.sender_euid = 0; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 923 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | const status_t err = data.errorCheck(); |
| 925 | if (err == NO_ERROR) { |
| 926 | tr.data_size = data.ipcDataSize(); |
| 927 | tr.data.ptr.buffer = data.ipcData(); |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 928 | tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | tr.data.ptr.offsets = data.ipcObjects(); |
| 930 | } else if (statusBuffer) { |
| 931 | tr.flags |= TF_STATUS_CODE; |
| 932 | *statusBuffer = err; |
| 933 | tr.data_size = sizeof(status_t); |
Arve Hjønnevåg | 87b30d0 | 2014-02-18 21:04:31 -0800 | [diff] [blame] | 934 | tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | tr.offsets_size = 0; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 936 | tr.data.ptr.offsets = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | } else { |
| 938 | return (mLastError = err); |
| 939 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 940 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | mOut.writeInt32(cmd); |
| 942 | mOut.write(&tr, sizeof(tr)); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 943 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | return NO_ERROR; |
| 945 | } |
| 946 | |
| 947 | sp<BBinder> the_context_object; |
| 948 | |
| 949 | void setTheContextObject(sp<BBinder> obj) |
| 950 | { |
| 951 | the_context_object = obj; |
| 952 | } |
| 953 | |
| 954 | status_t IPCThreadState::executeCommand(int32_t cmd) |
| 955 | { |
| 956 | BBinder* obj; |
| 957 | RefBase::weakref_type* refs; |
| 958 | status_t result = NO_ERROR; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 959 | |
Bernhard Rosenkränzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 960 | switch ((uint32_t)cmd) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | case BR_ERROR: |
| 962 | result = mIn.readInt32(); |
| 963 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 964 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | case BR_OK: |
| 966 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 967 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | case BR_ACQUIRE: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 969 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 970 | obj = (BBinder*)mIn.readPointer(); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 971 | ALOG_ASSERT(refs->refBase() == obj, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | "BR_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 973 | refs, obj, refs->refBase()); |
| 974 | obj->incStrong(mProcess.get()); |
| 975 | IF_LOG_REMOTEREFS() { |
| 976 | LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj); |
| 977 | obj->printRefs(); |
| 978 | } |
| 979 | mOut.writeInt32(BC_ACQUIRE_DONE); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 980 | mOut.writePointer((uintptr_t)refs); |
| 981 | mOut.writePointer((uintptr_t)obj); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 983 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | case BR_RELEASE: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 985 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 986 | obj = (BBinder*)mIn.readPointer(); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 987 | ALOG_ASSERT(refs->refBase() == obj, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | "BR_RELEASE: object %p does not match cookie %p (expected %p)", |
| 989 | refs, obj, refs->refBase()); |
| 990 | IF_LOG_REMOTEREFS() { |
| 991 | LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj); |
| 992 | obj->printRefs(); |
| 993 | } |
| 994 | mPendingStrongDerefs.push(obj); |
| 995 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 996 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | case BR_INCREFS: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 998 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 999 | obj = (BBinder*)mIn.readPointer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | refs->incWeak(mProcess.get()); |
| 1001 | mOut.writeInt32(BC_INCREFS_DONE); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1002 | mOut.writePointer((uintptr_t)refs); |
| 1003 | mOut.writePointer((uintptr_t)obj); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1005 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | case BR_DECREFS: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1007 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1008 | obj = (BBinder*)mIn.readPointer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | // NOTE: This assertion is not valid, because the object may no |
| 1010 | // longer exist (thus the (BBinder*)cast above resulting in a different |
| 1011 | // memory address). |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1012 | //ALOG_ASSERT(refs->refBase() == obj, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | // "BR_DECREFS: object %p does not match cookie %p (expected %p)", |
| 1014 | // refs, obj, refs->refBase()); |
| 1015 | mPendingWeakDerefs.push(refs); |
| 1016 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1017 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | case BR_ATTEMPT_ACQUIRE: |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1019 | refs = (RefBase::weakref_type*)mIn.readPointer(); |
| 1020 | obj = (BBinder*)mIn.readPointer(); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1021 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | { |
| 1023 | const bool success = refs->attemptIncStrong(mProcess.get()); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1024 | ALOG_ASSERT(success && refs->refBase() == obj, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)", |
| 1026 | refs, obj, refs->refBase()); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1027 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | mOut.writeInt32(BC_ACQUIRE_RESULT); |
| 1029 | mOut.writeInt32((int32_t)success); |
| 1030 | } |
| 1031 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1032 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | case BR_TRANSACTION: |
| 1034 | { |
| 1035 | binder_transaction_data tr; |
| 1036 | result = mIn.read(&tr, sizeof(tr)); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1037 | ALOG_ASSERT(result == NO_ERROR, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | "Not enough command data for brTRANSACTION"); |
| 1039 | if (result != NO_ERROR) break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1040 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | Parcel buffer; |
| 1042 | buffer.ipcSetDataReference( |
| 1043 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), |
| 1044 | tr.data_size, |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1045 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), |
| 1046 | tr.offsets_size/sizeof(binder_size_t), freeBuffer, this); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1047 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | const pid_t origPid = mCallingPid; |
| 1049 | const uid_t origUid = mCallingUid; |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1050 | const int32_t origStrictModePolicy = mStrictModePolicy; |
| 1051 | const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags; |
| 1052 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | mCallingPid = tr.sender_pid; |
| 1054 | mCallingUid = tr.sender_euid; |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1055 | mLastTransactionBinderFlags = tr.flags; |
| 1056 | |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1057 | //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid); |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1058 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | Parcel reply; |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1060 | status_t error; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | IF_LOG_TRANSACTIONS() { |
| 1062 | TextOutput::Bundle _b(alog); |
| 1063 | alog << "BR_TRANSACTION thr " << (void*)pthread_self() |
| 1064 | << " / obj " << tr.target.ptr << " / code " |
| 1065 | << TypeCode(tr.code) << ": " << indent << buffer |
| 1066 | << dedent << endl |
| 1067 | << "Data addr = " |
| 1068 | << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer) |
| 1069 | << ", offsets addr=" |
| 1070 | << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl; |
| 1071 | } |
| 1072 | if (tr.target.ptr) { |
Dianne Hackborn | c111461 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1073 | // We only have a weak reference on the target object, so we must first try to |
| 1074 | // safely acquire a strong reference before doing anything else with it. |
| 1075 | if (reinterpret_cast<RefBase::weakref_type*>( |
| 1076 | tr.target.ptr)->attemptIncStrong(this)) { |
| 1077 | error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer, |
| 1078 | &reply, tr.flags); |
| 1079 | reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this); |
| 1080 | } else { |
| 1081 | error = UNKNOWN_TRANSACTION; |
| 1082 | } |
Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 1083 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1084 | } else { |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1085 | 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] | 1086 | } |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1087 | |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1088 | //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | // mCallingPid, origPid, origUid); |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1090 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | if ((tr.flags & TF_ONE_WAY) == 0) { |
| 1092 | LOG_ONEWAY("Sending reply to %d!", mCallingPid); |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1093 | if (error < NO_ERROR) reply.setError(error); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | sendReply(reply, 0); |
| 1095 | } else { |
| 1096 | LOG_ONEWAY("NOT sending reply to %d!", mCallingPid); |
| 1097 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1098 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | mCallingPid = origPid; |
| 1100 | mCallingUid = origUid; |
Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1101 | mStrictModePolicy = origStrictModePolicy; |
| 1102 | mLastTransactionBinderFlags = origTransactionBinderFlags; |
Christopher Tate | 440fd87 | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1103 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | IF_LOG_TRANSACTIONS() { |
| 1105 | TextOutput::Bundle _b(alog); |
| 1106 | alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj " |
| 1107 | << tr.target.ptr << ": " << indent << reply << dedent << endl; |
| 1108 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1109 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | } |
| 1111 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1112 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | case BR_DEAD_BINDER: |
| 1114 | { |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1115 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | proxy->sendObituary(); |
| 1117 | mOut.writeInt32(BC_DEAD_BINDER_DONE); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1118 | mOut.writePointer((uintptr_t)proxy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | } break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1120 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: |
| 1122 | { |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1123 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1124 | proxy->getWeakRefs()->decWeak(proxy); |
| 1125 | } break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1126 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | case BR_FINISHED: |
| 1128 | result = TIMED_OUT; |
| 1129 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1130 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1131 | case BR_NOOP: |
| 1132 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1133 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | case BR_SPAWN_LOOPER: |
| 1135 | mProcess->spawnPooledThread(false); |
| 1136 | break; |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1137 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | default: |
| 1139 | printf("*** BAD COMMAND %d received from Binder driver\n", cmd); |
| 1140 | result = UNKNOWN_ERROR; |
| 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | if (result != NO_ERROR) { |
| 1145 | mLastError = result; |
| 1146 | } |
Tim Murray | 05f4679 | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1147 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | return result; |
| 1149 | } |
| 1150 | |
| 1151 | void IPCThreadState::threadDestructor(void *st) |
| 1152 | { |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1153 | IPCThreadState* const self = static_cast<IPCThreadState*>(st); |
| 1154 | if (self) { |
| 1155 | self->flushCommands(); |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1156 | #if defined(__ANDROID__) |
Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1157 | if (self->mProcess->mDriverFD > 0) { |
| 1158 | ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0); |
| 1159 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | #endif |
Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1161 | delete self; |
| 1162 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1166 | void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data, |
| 1167 | size_t /*dataSize*/, |
| 1168 | const binder_size_t* /*objects*/, |
| 1169 | size_t /*objectsSize*/, void* /*cookie*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1171 | //ALOGI("Freeing parcel %p", &parcel); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | IF_LOG_COMMANDS() { |
| 1173 | alog << "Writing BC_FREE_BUFFER for " << data << endl; |
| 1174 | } |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1175 | ALOG_ASSERT(data != NULL, "Called with NULL data"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1176 | if (parcel != NULL) parcel->closeFileDescriptors(); |
| 1177 | IPCThreadState* state = self(); |
| 1178 | state->mOut.writeInt32(BC_FREE_BUFFER); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1179 | state->mOut.writePointer((uintptr_t)data); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | }; // namespace android |