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_HARDWARE_IPC_THREAD_STATE_H 18 #define ANDROID_HARDWARE_IPC_THREAD_STATE_H 19 20 #include <utils/Errors.h> 21 #include <hwbinder/Parcel.h> 22 #include <hwbinder/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 namespace hardware { 32 33 class IPCThreadState 34 { 35 public: 36 static IPCThreadState* self(); 37 static IPCThreadState* selfOrNull(); // self(), but won't instantiate 38 39 sp<ProcessState> process(); 40 41 status_t clearLastError(); 42 43 pid_t getCallingPid() const; 44 // nullptr if unavailable 45 // 46 // this can't be restored once it's cleared, and it does not return the 47 // context of the current process when not in a binder call. 48 const char* getCallingSid() const; 49 uid_t getCallingUid() const; 50 51 void setStrictModePolicy(int32_t policy); 52 int32_t getStrictModePolicy() const; 53 54 void setLastTransactionBinderFlags(int32_t flags); 55 int32_t getLastTransactionBinderFlags() const; 56 57 int64_t clearCallingIdentity(); 58 // Restores PID/UID (not SID) 59 void restoreCallingIdentity(int64_t token); 60 61 int setupPolling(int* fd); 62 status_t handlePolledCommands(); 63 void flushCommands(); 64 65 void joinThreadPool(bool isMain = true); 66 67 // Stop the local process. 68 void stopProcess(bool immediate = true); 69 70 status_t transact(int32_t handle, 71 uint32_t code, const Parcel& data, 72 Parcel* reply, uint32_t flags); 73 74 void incStrongHandle(int32_t handle, BpHwBinder *proxy); 75 void decStrongHandle(int32_t handle); 76 void incWeakHandle(int32_t handle, BpHwBinder *proxy); 77 void decWeakHandle(int32_t handle); 78 status_t attemptIncStrongHandle(int32_t handle); 79 static void expungeHandle(int32_t handle, IBinder* binder); 80 status_t requestDeathNotification( int32_t handle, 81 BpHwBinder* proxy); 82 status_t clearDeathNotification( int32_t handle, 83 BpHwBinder* proxy); 84 85 static void shutdown(); 86 87 // Call this to disable switching threads to background scheduling when 88 // receiving incoming IPC calls. This is specifically here for the 89 // Android system process, since it expects to have background apps calling 90 // in to it but doesn't want to acquire locks in its services while in 91 // the background. 92 static void disableBackgroundScheduling(bool disable); 93 94 // Call blocks until the number of executing binder threads is less than 95 // the maximum number of binder threads threads allowed for this process. 96 void blockUntilThreadAvailable(); 97 98 // Service manager registration 99 void setTheContextObject(sp<BHwBinder> obj); 100 101 bool isLooperThread(); 102 bool isOnlyBinderThread(); 103 104 private: 105 IPCThreadState(); 106 ~IPCThreadState(); 107 108 status_t sendReply(const Parcel& reply, uint32_t flags); 109 status_t waitForResponse(Parcel *reply, 110 status_t *acquireResult=NULL); 111 status_t talkWithDriver(bool doReceive=true); 112 status_t writeTransactionData(int32_t cmd, 113 uint32_t binderFlags, 114 int32_t handle, 115 uint32_t code, 116 const Parcel& data, 117 status_t* statusBuffer); 118 status_t getAndExecuteCommand(); 119 status_t executeCommand(int32_t command); 120 void processPendingDerefs(); 121 void processPostWriteDerefs(); 122 123 void clearCaller(); 124 125 static void threadDestructor(void *st); 126 static void freeBuffer(Parcel* parcel, 127 const uint8_t* data, size_t dataSize, 128 const binder_size_t* objects, size_t objectsSize, 129 void* cookie); 130 131 const sp<ProcessState> mProcess; 132 const pid_t mMyThreadId; 133 Vector<BHwBinder*> mPendingStrongDerefs; 134 Vector<RefBase::weakref_type*> mPendingWeakDerefs; 135 Vector<RefBase*> mPostWriteStrongDerefs; 136 Vector<RefBase::weakref_type*> mPostWriteWeakDerefs; 137 Parcel mIn; 138 Parcel mOut; 139 status_t mLastError; 140 pid_t mCallingPid; 141 const char* mCallingSid; 142 uid_t mCallingUid; 143 int32_t mStrictModePolicy; 144 int32_t mLastTransactionBinderFlags; 145 sp<BHwBinder> mContextObject; 146 bool mIsLooper; 147 bool mIsPollingThread; 148 }; 149 150 }; // namespace hardware 151 }; // namespace android 152 153 // --------------------------------------------------------------------------- 154 155 #endif // ANDROID_HARDWARE_IPC_THREAD_STATE_H 156