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