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