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