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); 75 void decStrongHandle(int32_t handle); 76 void incWeakHandle(int32_t handle); 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 private: 101 IPCThreadState(); 102 ~IPCThreadState(); 103 104 status_t sendReply(const Parcel& reply, uint32_t flags); 105 status_t waitForResponse(Parcel *reply, 106 status_t *acquireResult=NULL); 107 status_t talkWithDriver(bool doReceive=true); 108 status_t writeTransactionData(int32_t cmd, 109 uint32_t binderFlags, 110 int32_t handle, 111 uint32_t code, 112 const Parcel& data, 113 status_t* statusBuffer); 114 status_t getAndExecuteCommand(); 115 status_t executeCommand(int32_t command); 116 void processPendingDerefs(); 117 118 void clearCaller(); 119 120 static void threadDestructor(void *st); 121 static void freeBuffer(Parcel* parcel, 122 const uint8_t* data, size_t dataSize, 123 const binder_size_t* objects, size_t objectsSize, 124 void* cookie); 125 126 const sp<ProcessState> mProcess; 127 const pid_t mMyThreadId; 128 Vector<BHwBinder*> mPendingStrongDerefs; 129 Vector<RefBase::weakref_type*> mPendingWeakDerefs; 130 131 Parcel mIn; 132 Parcel mOut; 133 status_t mLastError; 134 pid_t mCallingPid; 135 const char* mCallingSid; 136 uid_t mCallingUid; 137 int32_t mStrictModePolicy; 138 int32_t mLastTransactionBinderFlags; 139 sp<BHwBinder> mContextObject; 140 }; 141 142 }; // namespace hardware 143 }; // namespace android 144 145 // --------------------------------------------------------------------------- 146 147 #endif // ANDROID_HARDWARE_IPC_THREAD_STATE_H 148