| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [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 |  | 
| Steven Moreland | c7a871e | 2020-11-10 21:56:57 +0000 | [diff] [blame^] | 17 | #pragma once | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 |  | 
|  | 19 | #include <utils/Errors.h> | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 20 | #include <binder/Parcel.h> | 
|  | 21 | #include <binder/ProcessState.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/Vector.h> | 
|  | 23 |  | 
| Yabin Cui | 0dd549a | 2014-11-11 09:26:00 -0800 | [diff] [blame] | 24 | #if defined(_WIN32) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | typedef  int  uid_t; | 
|  | 26 | #endif | 
|  | 27 |  | 
|  | 28 | // --------------------------------------------------------------------------- | 
|  | 29 | namespace android { | 
|  | 30 |  | 
|  | 31 | class IPCThreadState | 
|  | 32 | { | 
|  | 33 | public: | 
| Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 34 | using CallRestriction = ProcessState::CallRestriction; | 
|  | 35 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | static  IPCThreadState*     self(); | 
| Brad Fitzpatrick | 1b60843 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 37 | static  IPCThreadState*     selfOrNull();  // self(), but won't instantiate | 
| Marco Ballesio | 7ee1757 | 2020-09-08 10:30:03 -0700 | [diff] [blame] | 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 |  | 
| Marco Ballesio | b09fc4a | 2020-09-11 16:17:21 -0700 | [diff] [blame] | 53 | // Provide information about the state of a frozen process | 
|  | 54 | static  status_t            getProcessFreezeInfo(pid_t pid, bool *sync_received, | 
|  | 55 | bool *async_received); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | sp<ProcessState>    process(); | 
|  | 57 |  | 
|  | 58 | status_t            clearLastError(); | 
|  | 59 |  | 
| Steven Moreland | 7001296 | 2020-03-12 17:32:26 -0700 | [diff] [blame] | 60 | /** | 
|  | 61 | * Returns the PID of the process which has made the current binder | 
|  | 62 | * call. If not in a binder call, this will return getpid. If the | 
|  | 63 | * call is oneway, this will return 0. | 
|  | 64 | */ | 
| Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 65 | pid_t               getCallingPid() const; | 
| Steven Moreland | 7001296 | 2020-03-12 17:32:26 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | /** | 
|  | 68 | * Returns the SELinux security identifier of the process which has | 
|  | 69 | * made the current binder call. If not in a binder call this will | 
|  | 70 | * return nullptr. If this isn't requested with | 
| Steven Moreland | 2f405f5 | 2020-07-08 22:24:29 +0000 | [diff] [blame] | 71 | * Binder::setRequestingSid, it will also return nullptr. | 
| Steven Moreland | 7001296 | 2020-03-12 17:32:26 -0700 | [diff] [blame] | 72 | * | 
|  | 73 | * This can't be restored once it's cleared, and it does not return the | 
|  | 74 | * context of the current process when not in a binder call. | 
|  | 75 | */ | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 76 | const char*         getCallingSid() const; | 
| Steven Moreland | 7001296 | 2020-03-12 17:32:26 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | /** | 
|  | 79 | * Returns the UID of the process which has made the current binder | 
|  | 80 | * call. If not in a binder call, this will return 0. | 
|  | 81 | */ | 
| Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 82 | uid_t               getCallingUid() const; | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 83 |  | 
|  | 84 | void                setStrictModePolicy(int32_t policy); | 
|  | 85 | int32_t             getStrictModePolicy() const; | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 86 |  | 
| Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 87 | // See Binder#setCallingWorkSourceUid in Binder.java. | 
|  | 88 | int64_t             setCallingWorkSourceUid(uid_t uid); | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 89 | // Internal only. Use setCallingWorkSourceUid(uid) instead. | 
|  | 90 | int64_t             setCallingWorkSourceUidWithoutPropagation(uid_t uid); | 
| Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 91 | // See Binder#getCallingWorkSourceUid in Binder.java. | 
|  | 92 | uid_t               getCallingWorkSourceUid() const; | 
|  | 93 | // See Binder#clearCallingWorkSource in Binder.java. | 
|  | 94 | int64_t             clearCallingWorkSource(); | 
|  | 95 | // See Binder#restoreCallingWorkSource in Binder.java. | 
|  | 96 | void                restoreCallingWorkSource(int64_t token); | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 97 | void                clearPropagateWorkSource(); | 
|  | 98 | bool                shouldPropagateWorkSource() const; | 
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 99 |  | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 100 | void                setLastTransactionBinderFlags(int32_t flags); | 
|  | 101 | int32_t             getLastTransactionBinderFlags() const; | 
|  | 102 |  | 
| Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 103 | void                setCallRestriction(CallRestriction restriction); | 
|  | 104 | CallRestriction     getCallRestriction() const; | 
|  | 105 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | int64_t             clearCallingIdentity(); | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 107 | // Restores PID/UID (not SID) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | void                restoreCallingIdentity(int64_t token); | 
| Steven Moreland | d8c8567 | 2020-07-24 21:30:41 +0000 | [diff] [blame] | 109 |  | 
|  | 110 | status_t            setupPolling(int* fd); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 111 | status_t            handlePolledCommands(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | void                flushCommands(); | 
|  | 113 |  | 
|  | 114 | void                joinThreadPool(bool isMain = true); | 
|  | 115 |  | 
|  | 116 | // Stop the local process. | 
|  | 117 | void                stopProcess(bool immediate = true); | 
|  | 118 |  | 
|  | 119 | status_t            transact(int32_t handle, | 
|  | 120 | uint32_t code, const Parcel& data, | 
|  | 121 | Parcel* reply, uint32_t flags); | 
|  | 122 |  | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 123 | void                incStrongHandle(int32_t handle, BpBinder *proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | void                decStrongHandle(int32_t handle); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 125 | void                incWeakHandle(int32_t handle, BpBinder *proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | void                decWeakHandle(int32_t handle); | 
|  | 127 | status_t            attemptIncStrongHandle(int32_t handle); | 
|  | 128 | static  void                expungeHandle(int32_t handle, IBinder* binder); | 
|  | 129 | status_t            requestDeathNotification(   int32_t handle, | 
|  | 130 | BpBinder* proxy); | 
|  | 131 | status_t            clearDeathNotification( int32_t handle, | 
|  | 132 | BpBinder* proxy); | 
|  | 133 |  | 
|  | 134 | static  void                shutdown(); | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 135 |  | 
| Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 136 | // Call this to disable switching threads to background scheduling when | 
|  | 137 | // receiving incoming IPC calls.  This is specifically here for the | 
|  | 138 | // Android system process, since it expects to have background apps calling | 
|  | 139 | // in to it but doesn't want to acquire locks in its services while in | 
|  | 140 | // the background. | 
|  | 141 | static  void                disableBackgroundScheduling(bool disable); | 
| Martijn Coenen | 2b63174 | 2017-05-05 11:16:59 -0700 | [diff] [blame] | 142 | bool                backgroundSchedulingDisabled(); | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 143 |  | 
|  | 144 | // Call blocks until the number of executing binder threads is less than | 
|  | 145 | // the maximum number of binder threads threads allowed for this process. | 
|  | 146 | void                blockUntilThreadAvailable(); | 
|  | 147 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 148 | // Service manager registration | 
|  | 149 | void                setTheContextObject(sp<BBinder> obj); | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 150 |  | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 151 | // WARNING: DO NOT USE THIS API | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 152 | // | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 153 | // Returns a pointer to the stack from the last time a transaction | 
|  | 154 | // was initiated by the kernel. Used to compare when making nested | 
|  | 155 | // calls between multiple different transports. | 
|  | 156 | const void*         getServingStackPointer() const; | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 157 |  | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 158 | // The work source represents the UID of the process we should attribute the transaction | 
| Olivier Gaillard | 68c9eea | 2018-11-30 10:49:42 +0000 | [diff] [blame] | 159 | // to. We use -1 to specify that the work source was not set using #setWorkSource. | 
|  | 160 | // | 
|  | 161 | // This constant needs to be kept in sync with Binder.UNSET_WORKSOURCE from the Java | 
|  | 162 | // side. | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 163 | static const int32_t kUnsetWorkSource = -1; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | private: | 
|  | 165 | IPCThreadState(); | 
|  | 166 | ~IPCThreadState(); | 
|  | 167 |  | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 168 | status_t            sendReply(const Parcel& reply, uint32_t flags); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | status_t            waitForResponse(Parcel *reply, | 
| Yi Kong | 87d465c | 2018-07-24 01:14:06 -0700 | [diff] [blame] | 170 | status_t *acquireResult=nullptr); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | status_t            talkWithDriver(bool doReceive=true); | 
|  | 172 | status_t            writeTransactionData(int32_t cmd, | 
|  | 173 | uint32_t binderFlags, | 
|  | 174 | int32_t handle, | 
|  | 175 | uint32_t code, | 
|  | 176 | const Parcel& data, | 
|  | 177 | status_t* statusBuffer); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 178 | status_t            getAndExecuteCommand(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | status_t            executeCommand(int32_t command); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 180 | void                processPendingDerefs(); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 181 | void                processPostWriteDerefs(); | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 182 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | void                clearCaller(); | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 184 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | static  void                threadDestructor(void *st); | 
|  | 186 | static  void                freeBuffer(Parcel* parcel, | 
|  | 187 | const uint8_t* data, size_t dataSize, | 
| Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 188 | const binder_size_t* objects, size_t objectsSize, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | void* cookie); | 
|  | 190 |  | 
|  | 191 | const   sp<ProcessState>    mProcess; | 
|  | 192 | Vector<BBinder*>    mPendingStrongDerefs; | 
|  | 193 | Vector<RefBase::weakref_type*> mPendingWeakDerefs; | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 194 | Vector<RefBase*>    mPostWriteStrongDerefs; | 
|  | 195 | Vector<RefBase::weakref_type*> mPostWriteWeakDerefs; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | Parcel              mIn; | 
|  | 197 | Parcel              mOut; | 
|  | 198 | status_t            mLastError; | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 199 | const void*         mServingStackPointer; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | pid_t               mCallingPid; | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 201 | const char*         mCallingSid; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | uid_t               mCallingUid; | 
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 203 | // The UID of the process who is responsible for this transaction. | 
|  | 204 | // This is used for resource attribution. | 
|  | 205 | int32_t             mWorkSource; | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 206 | // Whether the work source should be propagated. | 
|  | 207 | bool                mPropagateWorkSource; | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 208 | int32_t             mStrictModePolicy; | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 209 | int32_t             mLastTransactionBinderFlags; | 
| Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 210 | CallRestriction     mCallRestriction; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | }; | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 212 |  | 
| Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 213 | } // namespace android | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 |  | 
|  | 215 | // --------------------------------------------------------------------------- |