• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, bool *sync_received,
55                                                     bool *async_received);
56             sp<ProcessState>    process();
57 
58             status_t            clearLastError();
59 
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              */
65             pid_t               getCallingPid() const;
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
71              * Binder::setRequestingSid, it will also return nullptr.
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              */
76             const char*         getCallingSid() const;
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              */
82             uid_t               getCallingUid() const;
83 
84             void                setStrictModePolicy(int32_t policy);
85             int32_t             getStrictModePolicy() const;
86 
87             // See Binder#setCallingWorkSourceUid in Binder.java.
88             int64_t             setCallingWorkSourceUid(uid_t uid);
89             // Internal only. Use setCallingWorkSourceUid(uid) instead.
90             int64_t             setCallingWorkSourceUidWithoutPropagation(uid_t uid);
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);
97             void                clearPropagateWorkSource();
98             bool                shouldPropagateWorkSource() const;
99 
100             void                setLastTransactionBinderFlags(int32_t flags);
101             int32_t             getLastTransactionBinderFlags() const;
102 
103             void                setCallRestriction(CallRestriction restriction);
104             CallRestriction     getCallRestriction() const;
105 
106             int64_t             clearCallingIdentity();
107             // Restores PID/UID (not SID)
108             void                restoreCallingIdentity(int64_t token);
109 
110             status_t            setupPolling(int* fd);
111             status_t            handlePolledCommands();
112             void                flushCommands();
113             bool                flushIfNeeded();
114 
115             void                joinThreadPool(bool isMain = true);
116 
117             // Stop the local process.
118             void                stopProcess(bool immediate = true);
119 
120             status_t            transact(int32_t handle,
121                                          uint32_t code, const Parcel& data,
122                                          Parcel* reply, uint32_t flags);
123 
124             void                incStrongHandle(int32_t handle, BpBinder *proxy);
125             void                decStrongHandle(int32_t handle);
126             void                incWeakHandle(int32_t handle, BpBinder *proxy);
127             void                decWeakHandle(int32_t handle);
128             status_t            attemptIncStrongHandle(int32_t handle);
129     static  void                expungeHandle(int32_t handle, IBinder* binder);
130             status_t            requestDeathNotification(   int32_t handle,
131                                                             BpBinder* proxy);
132             status_t            clearDeathNotification( int32_t handle,
133                                                         BpBinder* proxy);
134 
135     static  void                shutdown();
136 
137     // Call this to disable switching threads to background scheduling when
138     // receiving incoming IPC calls.  This is specifically here for the
139     // Android system process, since it expects to have background apps calling
140     // in to it but doesn't want to acquire locks in its services while in
141     // the background.
142     static  void                disableBackgroundScheduling(bool disable);
143             bool                backgroundSchedulingDisabled();
144 
145             // Call blocks until the number of executing binder threads is less than
146             // the maximum number of binder threads threads allowed for this process.
147             void                blockUntilThreadAvailable();
148 
149             // Service manager registration
150             void                setTheContextObject(const sp<BBinder>& obj);
151 
152             // WARNING: DO NOT USE THIS API
153             //
154             // Returns a pointer to the stack from the last time a transaction
155             // was initiated by the kernel. Used to compare when making nested
156             // calls between multiple different transports.
157             const void*         getServingStackPointer() const;
158 
159             // The work source represents the UID of the process we should attribute the transaction
160             // to. We use -1 to specify that the work source was not set using #setWorkSource.
161             //
162             // This constant needs to be kept in sync with Binder.UNSET_WORKSOURCE from the Java
163             // side.
164             static const int32_t kUnsetWorkSource = -1;
165 private:
166                                 IPCThreadState();
167                                 ~IPCThreadState();
168 
169             status_t            sendReply(const Parcel& reply, uint32_t flags);
170             status_t            waitForResponse(Parcel *reply,
171                                                 status_t *acquireResult=nullptr);
172             status_t            talkWithDriver(bool doReceive=true);
173             status_t            writeTransactionData(int32_t cmd,
174                                                      uint32_t binderFlags,
175                                                      int32_t handle,
176                                                      uint32_t code,
177                                                      const Parcel& data,
178                                                      status_t* statusBuffer);
179             status_t            getAndExecuteCommand();
180             status_t            executeCommand(int32_t command);
181             void                processPendingDerefs();
182             void                processPostWriteDerefs();
183 
184             void                clearCaller();
185 
186     static  void                threadDestructor(void *st);
187     static  void                freeBuffer(Parcel* parcel,
188                                            const uint8_t* data, size_t dataSize,
189                                            const binder_size_t* objects, size_t objectsSize);
190 
191     const   sp<ProcessState>    mProcess;
192             Vector<BBinder*>    mPendingStrongDerefs;
193             Vector<RefBase::weakref_type*> mPendingWeakDerefs;
194             Vector<RefBase*>    mPostWriteStrongDerefs;
195             Vector<RefBase::weakref_type*> mPostWriteWeakDerefs;
196             Parcel              mIn;
197             Parcel              mOut;
198             status_t            mLastError;
199             const void*         mServingStackPointer;
200             pid_t               mCallingPid;
201             const char*         mCallingSid;
202             uid_t               mCallingUid;
203             // The UID of the process who is responsible for this transaction.
204             // This is used for resource attribution.
205             int32_t             mWorkSource;
206             // Whether the work source should be propagated.
207             bool                mPropagateWorkSource;
208             bool                mIsLooper;
209             bool mIsFlushing;
210             int32_t             mStrictModePolicy;
211             int32_t             mLastTransactionBinderFlags;
212             CallRestriction     mCallRestriction;
213 };
214 
215 } // namespace android
216 
217 // ---------------------------------------------------------------------------
218