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