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> 21 #include <binder/Parcel.h> 22 #include <binder/ProcessState.h> 23 #include <utils/Vector.h> 24 25 #if defined(_WIN32) 26 typedef int uid_t; 27 #endif 28 29 // --------------------------------------------------------------------------- 30 namespace android { 31 32 class IPCThreadStateBase; 33 34 class IPCThreadState 35 { 36 public: 37 static IPCThreadState* self(); 38 static IPCThreadState* selfOrNull(); // self(), but won't instantiate 39 40 sp<ProcessState> process(); 41 42 status_t clearLastError(); 43 44 pid_t getCallingPid() const; 45 // nullptr if unavailable 46 // 47 // this can't be restored once it's cleared, and it does not return the 48 // context of the current process when not in a binder call. 49 const char* getCallingSid() const; 50 uid_t getCallingUid() const; 51 52 void setStrictModePolicy(int32_t policy); 53 int32_t getStrictModePolicy() const; 54 55 // See Binder#setCallingWorkSourceUid in Binder.java. 56 int64_t setCallingWorkSourceUid(uid_t uid); 57 // Internal only. Use setCallingWorkSourceUid(uid) instead. 58 int64_t setCallingWorkSourceUidWithoutPropagation(uid_t uid); 59 // See Binder#getCallingWorkSourceUid in Binder.java. 60 uid_t getCallingWorkSourceUid() const; 61 // See Binder#clearCallingWorkSource in Binder.java. 62 int64_t clearCallingWorkSource(); 63 // See Binder#restoreCallingWorkSource in Binder.java. 64 void restoreCallingWorkSource(int64_t token); 65 void clearPropagateWorkSource(); 66 bool shouldPropagateWorkSource() const; 67 68 void setLastTransactionBinderFlags(int32_t flags); 69 int32_t getLastTransactionBinderFlags() const; 70 71 int64_t clearCallingIdentity(); 72 // Restores PID/UID (not SID) 73 void restoreCallingIdentity(int64_t token); 74 75 int setupPolling(int* fd); 76 status_t handlePolledCommands(); 77 void flushCommands(); 78 79 void joinThreadPool(bool isMain = true); 80 81 // Stop the local process. 82 void stopProcess(bool immediate = true); 83 84 status_t transact(int32_t handle, 85 uint32_t code, const Parcel& data, 86 Parcel* reply, uint32_t flags); 87 88 void incStrongHandle(int32_t handle, BpBinder *proxy); 89 void decStrongHandle(int32_t handle); 90 void incWeakHandle(int32_t handle, BpBinder *proxy); 91 void decWeakHandle(int32_t handle); 92 status_t attemptIncStrongHandle(int32_t handle); 93 static void expungeHandle(int32_t handle, IBinder* binder); 94 status_t requestDeathNotification( int32_t handle, 95 BpBinder* proxy); 96 status_t clearDeathNotification( int32_t handle, 97 BpBinder* proxy); 98 99 static void shutdown(); 100 101 // Call this to disable switching threads to background scheduling when 102 // receiving incoming IPC calls. This is specifically here for the 103 // Android system process, since it expects to have background apps calling 104 // in to it but doesn't want to acquire locks in its services while in 105 // the background. 106 static void disableBackgroundScheduling(bool disable); 107 bool backgroundSchedulingDisabled(); 108 109 // Call blocks until the number of executing binder threads is less than 110 // the maximum number of binder threads threads allowed for this process. 111 void blockUntilThreadAvailable(); 112 113 114 // Is this thread currently serving a binder call. This method 115 // returns true if while traversing backwards from the function call 116 // stack for this thread, we encounter a function serving a binder 117 // call before encountering a hwbinder call / hitting the end of the 118 // call stack. 119 // Eg: If thread T1 went through the following call pattern 120 // 1) T1 receives and executes hwbinder call H1. 121 // 2) While handling H1, T1 makes binder call B1. 122 // 3) The handler of B1, calls into T1 with a callback B2. 123 // If isServingCall() is called during H1 before 3), this method 124 // will return false, else true. 125 // 126 // ---- 127 // | B2 | ---> While callback B2 is being handled, during 3). 128 // ---- 129 // | H1 | ---> While H1 is being handled. 130 // ---- 131 // Fig: Thread Call stack while handling B2 132 // 133 // This is since after 3), while traversing the thread call stack, 134 // we hit a binder call before a hwbinder call / end of stack. This 135 // method may be typically used to determine whether to use 136 // hardware::IPCThreadState methods or IPCThreadState methods to 137 // infer information about thread state. 138 bool isServingCall() const; 139 140 // The work source represents the UID of the process we should attribute the transaction 141 // to. We use -1 to specify that the work source was not set using #setWorkSource. 142 // 143 // This constant needs to be kept in sync with Binder.UNSET_WORKSOURCE from the Java 144 // side. 145 static const int32_t kUnsetWorkSource = -1; 146 147 private: 148 IPCThreadState(); 149 ~IPCThreadState(); 150 151 status_t sendReply(const Parcel& reply, uint32_t flags); 152 status_t waitForResponse(Parcel *reply, 153 status_t *acquireResult=nullptr); 154 status_t talkWithDriver(bool doReceive=true); 155 status_t writeTransactionData(int32_t cmd, 156 uint32_t binderFlags, 157 int32_t handle, 158 uint32_t code, 159 const Parcel& data, 160 status_t* statusBuffer); 161 status_t getAndExecuteCommand(); 162 status_t executeCommand(int32_t command); 163 void processPendingDerefs(); 164 void processPostWriteDerefs(); 165 166 void clearCaller(); 167 168 static void threadDestructor(void *st); 169 static void freeBuffer(Parcel* parcel, 170 const uint8_t* data, size_t dataSize, 171 const binder_size_t* objects, size_t objectsSize, 172 void* cookie); 173 174 const sp<ProcessState> mProcess; 175 Vector<BBinder*> mPendingStrongDerefs; 176 Vector<RefBase::weakref_type*> mPendingWeakDerefs; 177 Vector<RefBase*> mPostWriteStrongDerefs; 178 Vector<RefBase::weakref_type*> mPostWriteWeakDerefs; 179 Parcel mIn; 180 Parcel mOut; 181 status_t mLastError; 182 pid_t mCallingPid; 183 const char* mCallingSid; 184 uid_t mCallingUid; 185 // The UID of the process who is responsible for this transaction. 186 // This is used for resource attribution. 187 int32_t mWorkSource; 188 // Whether the work source should be propagated. 189 bool mPropagateWorkSource; 190 int32_t mStrictModePolicy; 191 int32_t mLastTransactionBinderFlags; 192 IPCThreadStateBase *mIPCThreadStateBase; 193 194 ProcessState::CallRestriction mCallRestriction; 195 }; 196 197 }; // namespace android 198 199 // --------------------------------------------------------------------------- 200 201 #endif // ANDROID_IPC_THREAD_STATE_H 202