| 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 | |
| 17 | #ifndef ANDROID_IPC_THREAD_STATE_H |
| 18 | #define ANDROID_IPC_THREAD_STATE_H |
| 19 | |
| 20 | #include <utils/Errors.h> |
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/Parcel.h> |
| 22 | #include <binder/ProcessState.h> |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <utils/Vector.h> |
| 24 | |
| Yabin Cui | 0dd549a | 2014-11-11 09:26:00 -0800 | [diff] [blame] | 25 | #if defined(_WIN32) |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | typedef int uid_t; |
| 27 | #endif |
| 28 | |
| 29 | // --------------------------------------------------------------------------- |
| 30 | namespace android { |
| 31 | |
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 32 | class IPCThreadStateBase; |
| 33 | |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | class IPCThreadState |
| 35 | { |
| 36 | public: |
| 37 | static IPCThreadState* self(); |
| Brad Fitzpatrick | 1b60843 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 38 | static IPCThreadState* selfOrNull(); // self(), but won't instantiate |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | sp<ProcessState> process(); |
| 41 | |
| 42 | status_t clearLastError(); |
| 43 | |
| Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 44 | pid_t getCallingPid() const; |
| 45 | uid_t getCallingUid() const; |
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 46 | |
| 47 | void setStrictModePolicy(int32_t policy); |
| 48 | int32_t getStrictModePolicy() const; |
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 49 | |
| Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame^] | 50 | // See Binder#setCallingWorkSourceUid in Binder.java. |
| 51 | int64_t setCallingWorkSourceUid(uid_t uid); |
| 52 | // See Binder#getCallingWorkSourceUid in Binder.java. |
| 53 | uid_t getCallingWorkSourceUid() const; |
| 54 | // See Binder#clearCallingWorkSource in Binder.java. |
| 55 | int64_t clearCallingWorkSource(); |
| 56 | // See Binder#restoreCallingWorkSource in Binder.java. |
| 57 | void restoreCallingWorkSource(int64_t token); |
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 58 | |
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 59 | void setLastTransactionBinderFlags(int32_t flags); |
| 60 | int32_t getLastTransactionBinderFlags() const; |
| 61 | |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | int64_t clearCallingIdentity(); |
| 63 | void restoreCallingIdentity(int64_t token); |
| 64 | |
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 65 | int setupPolling(int* fd); |
| 66 | status_t handlePolledCommands(); |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | void flushCommands(); |
| 68 | |
| 69 | void joinThreadPool(bool isMain = true); |
| 70 | |
| 71 | // Stop the local process. |
| 72 | void stopProcess(bool immediate = true); |
| 73 | |
| 74 | status_t transact(int32_t handle, |
| 75 | uint32_t code, const Parcel& data, |
| 76 | Parcel* reply, uint32_t flags); |
| 77 | |
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 78 | void incStrongHandle(int32_t handle, BpBinder *proxy); |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | void decStrongHandle(int32_t handle); |
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 80 | void incWeakHandle(int32_t handle, BpBinder *proxy); |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | void decWeakHandle(int32_t handle); |
| 82 | status_t attemptIncStrongHandle(int32_t handle); |
| 83 | static void expungeHandle(int32_t handle, IBinder* binder); |
| 84 | status_t requestDeathNotification( int32_t handle, |
| 85 | BpBinder* proxy); |
| 86 | status_t clearDeathNotification( int32_t handle, |
| 87 | BpBinder* proxy); |
| 88 | |
| 89 | static void shutdown(); |
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 90 | |
| Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 91 | // Call this to disable switching threads to background scheduling when |
| 92 | // receiving incoming IPC calls. This is specifically here for the |
| 93 | // Android system process, since it expects to have background apps calling |
| 94 | // in to it but doesn't want to acquire locks in its services while in |
| 95 | // the background. |
| 96 | static void disableBackgroundScheduling(bool disable); |
| Martijn Coenen | 2b63174 | 2017-05-05 11:16:59 -0700 | [diff] [blame] | 97 | bool backgroundSchedulingDisabled(); |
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 98 | |
| 99 | // Call blocks until the number of executing binder threads is less than |
| 100 | // the maximum number of binder threads threads allowed for this process. |
| 101 | void blockUntilThreadAvailable(); |
| 102 | |
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 103 | |
| 104 | // Is this thread currently serving a binder call. This method |
| 105 | // returns true if while traversing backwards from the function call |
| 106 | // stack for this thread, we encounter a function serving a binder |
| 107 | // call before encountering a hwbinder call / hitting the end of the |
| 108 | // call stack. |
| 109 | // Eg: If thread T1 went through the following call pattern |
| 110 | // 1) T1 receives and executes hwbinder call H1. |
| 111 | // 2) While handling H1, T1 makes binder call B1. |
| 112 | // 3) The handler of B1, calls into T1 with a callback B2. |
| 113 | // If isServingCall() is called during H1 before 3), this method |
| 114 | // will return false, else true. |
| 115 | // |
| 116 | // ---- |
| 117 | // | B2 | ---> While callback B2 is being handled, during 3). |
| 118 | // ---- |
| 119 | // | H1 | ---> While H1 is being handled. |
| 120 | // ---- |
| 121 | // Fig: Thread Call stack while handling B2 |
| 122 | // |
| 123 | // This is since after 3), while traversing the thread call stack, |
| 124 | // we hit a binder call before a hwbinder call / end of stack. This |
| 125 | // method may be typically used to determine whether to use |
| 126 | // hardware::IPCThreadState methods or IPCThreadState methods to |
| 127 | // infer information about thread state. |
| 128 | bool isServingCall() const; |
| 129 | |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | private: |
| 131 | IPCThreadState(); |
| 132 | ~IPCThreadState(); |
| 133 | |
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 134 | status_t sendReply(const Parcel& reply, uint32_t flags); |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | status_t waitForResponse(Parcel *reply, |
| Yi Kong | 87d465c | 2018-07-24 01:14:06 -0700 | [diff] [blame] | 136 | status_t *acquireResult=nullptr); |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | status_t talkWithDriver(bool doReceive=true); |
| 138 | status_t writeTransactionData(int32_t cmd, |
| 139 | uint32_t binderFlags, |
| 140 | int32_t handle, |
| 141 | uint32_t code, |
| 142 | const Parcel& data, |
| 143 | status_t* statusBuffer); |
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 144 | status_t getAndExecuteCommand(); |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | status_t executeCommand(int32_t command); |
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 146 | void processPendingDerefs(); |
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 147 | void processPostWriteDerefs(); |
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 148 | |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | void clearCaller(); |
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 150 | |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | static void threadDestructor(void *st); |
| 152 | static void freeBuffer(Parcel* parcel, |
| 153 | const uint8_t* data, size_t dataSize, |
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 154 | const binder_size_t* objects, size_t objectsSize, |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | void* cookie); |
| 156 | |
| 157 | const sp<ProcessState> mProcess; |
| 158 | Vector<BBinder*> mPendingStrongDerefs; |
| 159 | Vector<RefBase::weakref_type*> mPendingWeakDerefs; |
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 160 | Vector<RefBase*> mPostWriteStrongDerefs; |
| 161 | Vector<RefBase::weakref_type*> mPostWriteWeakDerefs; |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | Parcel mIn; |
| 163 | Parcel mOut; |
| 164 | status_t mLastError; |
| 165 | pid_t mCallingPid; |
| 166 | uid_t mCallingUid; |
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 167 | // The UID of the process who is responsible for this transaction. |
| 168 | // This is used for resource attribution. |
| 169 | int32_t mWorkSource; |
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 170 | int32_t mStrictModePolicy; |
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 171 | int32_t mLastTransactionBinderFlags; |
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 172 | IPCThreadStateBase *mIPCThreadStateBase; |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | }; |
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 174 | |
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | }; // namespace android |
| 176 | |
| 177 | // --------------------------------------------------------------------------- |
| 178 | |
| 179 | #endif // ANDROID_IPC_THREAD_STATE_H |