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 #pragma once 18 19 #include <utils/Errors.h> 20 #include <binder/Parcel.h> 21 #include <binder/ProcessState.h> 22 #include <utils/Vector.h> 23 24 #if defined(_WIN32) 25 typedef int uid_t; 26 #endif 27 28 // --------------------------------------------------------------------------- 29 namespace android { 30 31 class IPCThreadState 32 { 33 public: 34 using CallRestriction = ProcessState::CallRestriction; 35 36 static IPCThreadState* self(); 37 static IPCThreadState* selfOrNull(); // self(), but won't instantiate 38 39 // Freeze or unfreeze the binder interface to a specific process. When freezing, this method 40 // will block up to timeout_ms to process pending transactions directed to pid. Unfreeze 41 // is immediate. Transactions to processes frozen via this method won't be delivered and the 42 // driver will return BR_FROZEN_REPLY to the client sending them. After unfreeze, 43 // transactions will be delivered normally. 44 // 45 // pid: id for the process for which the binder interface is to be frozen 46 // enable: freeze (true) or unfreeze (false) 47 // timeout_ms: maximum time this function is allowed to block the caller waiting for pending 48 // binder transactions to be processed. 49 // 50 // returns: 0 in case of success, a value < 0 in case of error 51 static status_t freeze(pid_t pid, bool enabled, uint32_t timeout_ms); 52 53 // Provide information about the state of a frozen process 54 static status_t getProcessFreezeInfo(pid_t pid, uint32_t *sync_received, 55 uint32_t *async_received); 56 57 status_t clearLastError(); 58 59 /** 60 * Returns the PID of the process which has made the current binder 61 * call. If not in a binder call, this will return getpid. If the 62 * call is oneway, this will return 0. 63 */ 64 [[nodiscard]] pid_t getCallingPid() const; 65 66 /** 67 * Returns the SELinux security identifier of the process which has 68 * made the current binder call. If not in a binder call this will 69 * return nullptr. If this isn't requested with 70 * Binder::setRequestingSid, it will also return nullptr. 71 * 72 * This can't be restored once it's cleared, and it does not return the 73 * context of the current process when not in a binder call. 74 */ 75 [[nodiscard]] const char* getCallingSid() const; 76 77 /** 78 * Returns the UID of the process which has made the current binder 79 * call. If not in a binder call, this will return 0. 80 */ 81 [[nodiscard]] uid_t getCallingUid() const; 82 83 /** 84 * Make it an abort to rely on getCalling* for a section of 85 * execution. 86 * 87 * Usage: 88 * IPCThreadState::SpGuard guard { 89 * .address = __builtin_frame_address(0), 90 * .context = "...", 91 * }; 92 * const auto* orig = pushGetCallingSpGuard(&guard); 93 * { 94 * // will abort if you call getCalling*, unless you are 95 * // serving a nested binder transaction 96 * } 97 * restoreCallingSpGuard(orig); 98 */ 99 struct SpGuard { 100 const void* address; 101 const char* context; 102 }; 103 const SpGuard* pushGetCallingSpGuard(const SpGuard* guard); 104 void restoreGetCallingSpGuard(const SpGuard* guard); 105 /** 106 * Used internally by getCalling*. Can also be used to assert that 107 * you are in a binder context (getCalling* is valid). This is 108 * intentionally not exposed as a boolean API since code should be 109 * written to know its environment. 110 */ 111 void checkContextIsBinderForUse(const char* use) const; 112 113 void setStrictModePolicy(int32_t policy); 114 int32_t getStrictModePolicy() const; 115 116 // See Binder#setCallingWorkSourceUid in Binder.java. 117 int64_t setCallingWorkSourceUid(uid_t uid); 118 // Internal only. Use setCallingWorkSourceUid(uid) instead. 119 int64_t setCallingWorkSourceUidWithoutPropagation(uid_t uid); 120 // See Binder#getCallingWorkSourceUid in Binder.java. 121 uid_t getCallingWorkSourceUid() const; 122 // See Binder#clearCallingWorkSource in Binder.java. 123 int64_t clearCallingWorkSource(); 124 // See Binder#restoreCallingWorkSource in Binder.java. 125 void restoreCallingWorkSource(int64_t token); 126 void clearPropagateWorkSource(); 127 bool shouldPropagateWorkSource() const; 128 129 void setLastTransactionBinderFlags(int32_t flags); 130 int32_t getLastTransactionBinderFlags() const; 131 132 void setCallRestriction(CallRestriction restriction); 133 CallRestriction getCallRestriction() const; 134 135 int64_t clearCallingIdentity(); 136 // Restores PID/UID (not SID) 137 void restoreCallingIdentity(int64_t token); 138 139 status_t setupPolling(int* fd); 140 status_t handlePolledCommands(); 141 void flushCommands(); 142 bool flushIfNeeded(); 143 144 void joinThreadPool(bool isMain = true); 145 146 // Stop the local process. 147 void stopProcess(bool immediate = true); 148 149 status_t transact(int32_t handle, 150 uint32_t code, const Parcel& data, 151 Parcel* reply, uint32_t flags); 152 153 void incStrongHandle(int32_t handle, BpBinder *proxy); 154 void decStrongHandle(int32_t handle); 155 void incWeakHandle(int32_t handle, BpBinder *proxy); 156 void decWeakHandle(int32_t handle); 157 status_t attemptIncStrongHandle(int32_t handle); 158 static void expungeHandle(int32_t handle, IBinder* binder); 159 status_t requestDeathNotification( int32_t handle, 160 BpBinder* proxy); 161 status_t clearDeathNotification( int32_t handle, 162 BpBinder* proxy); 163 164 static void shutdown(); 165 166 // Call this to disable switching threads to background scheduling when 167 // receiving incoming IPC calls. This is specifically here for the 168 // Android system process, since it expects to have background apps calling 169 // in to it but doesn't want to acquire locks in its services while in 170 // the background. 171 static void disableBackgroundScheduling(bool disable); 172 bool backgroundSchedulingDisabled(); 173 174 // Call blocks until the number of executing binder threads is less than 175 // the maximum number of binder threads threads allowed for this process. 176 void blockUntilThreadAvailable(); 177 178 // Service manager registration 179 void setTheContextObject(const sp<BBinder>& obj); 180 181 // WARNING: DO NOT USE THIS API 182 // 183 // Returns a pointer to the stack from the last time a transaction 184 // was initiated by the kernel. Used to compare when making nested 185 // calls between multiple different transports. 186 const void* getServingStackPointer() const; 187 188 // The work source represents the UID of the process we should attribute the transaction 189 // to. We use -1 to specify that the work source was not set using #setWorkSource. 190 // 191 // This constant needs to be kept in sync with Binder.UNSET_WORKSOURCE from the Java 192 // side. 193 static const int32_t kUnsetWorkSource = -1; 194 private: 195 IPCThreadState(); 196 ~IPCThreadState(); 197 198 status_t sendReply(const Parcel& reply, uint32_t flags); 199 status_t waitForResponse(Parcel *reply, 200 status_t *acquireResult=nullptr); 201 status_t talkWithDriver(bool doReceive=true); 202 status_t writeTransactionData(int32_t cmd, 203 uint32_t binderFlags, 204 int32_t handle, 205 uint32_t code, 206 const Parcel& data, 207 status_t* statusBuffer); 208 status_t getAndExecuteCommand(); 209 status_t executeCommand(int32_t command); 210 void processPendingDerefs(); 211 void processPostWriteDerefs(); 212 213 void clearCaller(); 214 215 static void threadDestructor(void *st); 216 static void freeBuffer(Parcel* parcel, 217 const uint8_t* data, size_t dataSize, 218 const binder_size_t* objects, size_t objectsSize); 219 220 const sp<ProcessState> mProcess; 221 Vector<BBinder*> mPendingStrongDerefs; 222 Vector<RefBase::weakref_type*> mPendingWeakDerefs; 223 Vector<RefBase*> mPostWriteStrongDerefs; 224 Vector<RefBase::weakref_type*> mPostWriteWeakDerefs; 225 Parcel mIn; 226 Parcel mOut; 227 status_t mLastError; 228 const void* mServingStackPointer; 229 const SpGuard* mServingStackPointerGuard; 230 pid_t mCallingPid; 231 const char* mCallingSid; 232 uid_t mCallingUid; 233 // The UID of the process who is responsible for this transaction. 234 // This is used for resource attribution. 235 int32_t mWorkSource; 236 // Whether the work source should be propagated. 237 bool mPropagateWorkSource; 238 bool mIsLooper; 239 bool mIsFlushing; 240 int32_t mStrictModePolicy; 241 int32_t mLastTransactionBinderFlags; 242 CallRestriction mCallRestriction; 243 }; 244 245 } // namespace android 246 247 // --------------------------------------------------------------------------- 248