Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [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 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 17 | #ifndef ANDROID_HARDWARE_IPC_THREAD_STATE_H |
| 18 | #define ANDROID_HARDWARE_IPC_THREAD_STATE_H |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | |
| 20 | #include <utils/Errors.h> |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 21 | #include <hwbinder/Parcel.h> |
| 22 | #include <hwbinder/ProcessState.h> |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <utils/Vector.h> |
| 24 | |
Steven Moreland | d7bbfdb | 2018-05-01 16:30:46 -0700 | [diff] [blame] | 25 | #include <functional> |
| 26 | |
Steven Moreland | b5f6e00 | 2021-02-26 01:51:20 +0000 | [diff] [blame] | 27 | // WARNING: this code is part of libhwbinder, a fork of libbinder. Generally, |
| 28 | // this means that it is only relevant to HIDL. Any AIDL- or libbinder-specific |
| 29 | // code should not try to use these things. |
| 30 | |
Yabin Cui | 6db6ea5 | 2014-11-11 09:26:00 -0800 | [diff] [blame] | 31 | #if defined(_WIN32) |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 32 | typedef int uid_t; |
| 33 | #endif |
| 34 | |
| 35 | // --------------------------------------------------------------------------- |
| 36 | namespace android { |
Jayant Chowdhary | 985fc89 | 2018-10-01 22:54:05 +0000 | [diff] [blame] | 37 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 38 | namespace hardware { |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 39 | |
| 40 | class IPCThreadState |
| 41 | { |
| 42 | public: |
| 43 | static IPCThreadState* self(); |
Brad Fitzpatrick | 7794994 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 44 | static IPCThreadState* selfOrNull(); // self(), but won't instantiate |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 45 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 46 | sp<ProcessState> process(); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 47 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 48 | status_t clearLastError(); |
| 49 | |
Dan Stoza | e8da8a4 | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 50 | pid_t getCallingPid() const; |
Steven Moreland | 7375a87 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 51 | // nullptr if unavailable |
| 52 | // |
| 53 | // this can't be restored once it's cleared, and it does not return the |
| 54 | // context of the current process when not in a binder call. |
| 55 | const char* getCallingSid() const; |
Dan Stoza | e8da8a4 | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 56 | uid_t getCallingUid() const; |
Brad Fitzpatrick | 94c3634 | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 57 | |
| 58 | void setStrictModePolicy(int32_t policy); |
| 59 | int32_t getStrictModePolicy() const; |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 60 | |
| 61 | void setLastTransactionBinderFlags(int32_t flags); |
| 62 | int32_t getLastTransactionBinderFlags() const; |
| 63 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 64 | int64_t clearCallingIdentity(); |
Steven Moreland | 7375a87 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 65 | // Restores PID/UID (not SID) |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 66 | void restoreCallingIdentity(int64_t token); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 67 | |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 68 | int setupPolling(int* fd); |
| 69 | status_t handlePolledCommands(); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 70 | void flushCommands(); |
| 71 | |
| 72 | void joinThreadPool(bool isMain = true); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 73 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 74 | // Stop the local process. |
| 75 | void stopProcess(bool immediate = true); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 76 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 77 | status_t transact(int32_t handle, |
| 78 | uint32_t code, const Parcel& data, |
| 79 | Parcel* reply, uint32_t flags); |
| 80 | |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 81 | void incStrongHandle(int32_t handle, BpHwBinder *proxy); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 82 | void decStrongHandle(int32_t handle); |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 83 | void incWeakHandle(int32_t handle, BpHwBinder *proxy); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 84 | void decWeakHandle(int32_t handle); |
| 85 | status_t attemptIncStrongHandle(int32_t handle); |
| 86 | static void expungeHandle(int32_t handle, IBinder* binder); |
| 87 | status_t requestDeathNotification( int32_t handle, |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 88 | BpHwBinder* proxy); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 89 | status_t clearDeathNotification( int32_t handle, |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 90 | BpHwBinder* proxy); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 91 | |
| 92 | static void shutdown(); |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 93 | |
Martijn Coenen | a660cbc | 2016-05-12 11:29:23 +0200 | [diff] [blame] | 94 | // Service manager registration |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 95 | void setTheContextObject(sp<BHwBinder> obj); |
Martijn Coenen | 420d4bb | 2017-10-24 11:43:55 +0200 | [diff] [blame] | 96 | |
| 97 | bool isLooperThread(); |
Tobias Lindskog | a36d576 | 2018-01-05 10:28:31 +0100 | [diff] [blame] | 98 | bool isOnlyBinderThread(); |
| 99 | |
Steven Moreland | e3785d0 | 2020-01-31 14:58:48 -0800 | [diff] [blame] | 100 | // WARNING: DO NOT USE THIS API |
Jayant Chowdhary | 985fc89 | 2018-10-01 22:54:05 +0000 | [diff] [blame] | 101 | // |
Steven Moreland | e3785d0 | 2020-01-31 14:58:48 -0800 | [diff] [blame] | 102 | // Returns a pointer to the stack from the last time a transaction |
| 103 | // was initiated by the kernel. Used to compare when making nested |
| 104 | // calls between multiple different transports. |
| 105 | const void* getServingStackPointer() const; |
Jayant Chowdhary | 985fc89 | 2018-10-01 22:54:05 +0000 | [diff] [blame] | 106 | |
Steven Moreland | d7bbfdb | 2018-05-01 16:30:46 -0700 | [diff] [blame] | 107 | // Tasks which are done on the binder thread after the thread returns to the |
| 108 | // threadpool. |
| 109 | void addPostCommandTask(const std::function<void(void)>& task); |
| 110 | |
| 111 | private: |
| 112 | IPCThreadState(); |
| 113 | ~IPCThreadState(); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 114 | |
| 115 | status_t sendReply(const Parcel& reply, uint32_t flags); |
| 116 | status_t waitForResponse(Parcel *reply, |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 117 | status_t *acquireResult=nullptr); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 118 | status_t talkWithDriver(bool doReceive=true); |
| 119 | status_t writeTransactionData(int32_t cmd, |
| 120 | uint32_t binderFlags, |
| 121 | int32_t handle, |
| 122 | uint32_t code, |
| 123 | const Parcel& data, |
| 124 | status_t* statusBuffer); |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 125 | status_t getAndExecuteCommand(); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 126 | status_t executeCommand(int32_t command); |
Todd Poynor | 0646cb0 | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 127 | void processPendingDerefs(); |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 128 | void processPostWriteDerefs(); |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 129 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 130 | void clearCaller(); |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 131 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 132 | static void threadDestructor(void *st); |
| 133 | static void freeBuffer(Parcel* parcel, |
| 134 | const uint8_t* data, size_t dataSize, |
Arve Hjønnevåg | a544070 | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 135 | const binder_size_t* objects, size_t objectsSize, |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 136 | void* cookie); |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 137 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 138 | const sp<ProcessState> mProcess; |
Yifan Hong | dde40f3 | 2017-01-12 14:22:45 -0800 | [diff] [blame] | 139 | Vector<BHwBinder*> mPendingStrongDerefs; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 140 | Vector<RefBase::weakref_type*> mPendingWeakDerefs; |
Martijn Coenen | b825372 | 2018-05-23 15:33:22 +0200 | [diff] [blame] | 141 | Vector<RefBase*> mPostWriteStrongDerefs; |
| 142 | Vector<RefBase::weakref_type*> mPostWriteWeakDerefs; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 143 | Parcel mIn; |
| 144 | Parcel mOut; |
| 145 | status_t mLastError; |
Steven Moreland | e3785d0 | 2020-01-31 14:58:48 -0800 | [diff] [blame] | 146 | const void* mServingStackPointer; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 147 | pid_t mCallingPid; |
Steven Moreland | 7375a87 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 148 | const char* mCallingSid; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 149 | uid_t mCallingUid; |
Brad Fitzpatrick | 94c3634 | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 150 | int32_t mStrictModePolicy; |
Brad Fitzpatrick | 24f8bca | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 151 | int32_t mLastTransactionBinderFlags; |
Martijn Coenen | 420d4bb | 2017-10-24 11:43:55 +0200 | [diff] [blame] | 152 | bool mIsLooper; |
Tobias Lindskog | a36d576 | 2018-01-05 10:28:31 +0100 | [diff] [blame] | 153 | bool mIsPollingThread; |
Steven Moreland | d7bbfdb | 2018-05-01 16:30:46 -0700 | [diff] [blame] | 154 | |
| 155 | std::vector<std::function<void(void)>> mPostCommandTasks; |
Steven Moreland | 1460300 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 156 | |
| 157 | ProcessState::CallRestriction mCallRestriction; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 158 | }; |
Brad Fitzpatrick | 94c3634 | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 159 | |
Steven Moreland | 7173a4c | 2019-09-26 15:55:02 -0700 | [diff] [blame] | 160 | } // namespace hardware |
| 161 | } // namespace android |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 162 | |
| 163 | // --------------------------------------------------------------------------- |
| 164 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 165 | #endif // ANDROID_HARDWARE_IPC_THREAD_STATE_H |