• 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 #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 #include <functional>
26 
27 #if defined(_WIN32)
28 typedef  int  uid_t;
29 #endif
30 
31 // ---------------------------------------------------------------------------
32 namespace android {
33 
34 class IPCThreadStateBase;
35 
36 namespace hardware {
37 
38 class IPCThreadState
39 {
40 public:
41     static  IPCThreadState*     self();
42     static  IPCThreadState*     selfOrNull();  // self(), but won't instantiate
43 
44             sp<ProcessState>    process();
45 
46             status_t            clearLastError();
47 
48             pid_t               getCallingPid() const;
49             // nullptr if unavailable
50             //
51             // this can't be restored once it's cleared, and it does not return the
52             // context of the current process when not in a binder call.
53             const char*         getCallingSid() const;
54             uid_t               getCallingUid() const;
55 
56             void                setStrictModePolicy(int32_t policy);
57             int32_t             getStrictModePolicy() const;
58 
59             void                setLastTransactionBinderFlags(int32_t flags);
60             int32_t             getLastTransactionBinderFlags() const;
61 
62             int64_t             clearCallingIdentity();
63             // Restores PID/UID (not SID)
64             void                restoreCallingIdentity(int64_t token);
65 
66             int                 setupPolling(int* fd);
67             status_t            handlePolledCommands();
68             void                flushCommands();
69 
70             void                joinThreadPool(bool isMain = true);
71 
72             // Stop the local process.
73             void                stopProcess(bool immediate = true);
74 
75             status_t            transact(int32_t handle,
76                                          uint32_t code, const Parcel& data,
77                                          Parcel* reply, uint32_t flags);
78 
79             void                incStrongHandle(int32_t handle, BpHwBinder *proxy);
80             void                decStrongHandle(int32_t handle);
81             void                incWeakHandle(int32_t handle, BpHwBinder *proxy);
82             void                decWeakHandle(int32_t handle);
83             status_t            attemptIncStrongHandle(int32_t handle);
84     static  void                expungeHandle(int32_t handle, IBinder* binder);
85             status_t            requestDeathNotification(   int32_t handle,
86                                                             BpHwBinder* proxy);
87             status_t            clearDeathNotification( int32_t handle,
88                                                         BpHwBinder* proxy);
89 
90     static  void                shutdown();
91 
92             // TODO(b/66905301): remove symbol
93 private:
94     static  void                disableBackgroundScheduling(bool disable);
95 public:
96 
97             // Call blocks until the number of executing binder threads is less than
98             // the maximum number of binder threads threads allowed for this process.
99             void                blockUntilThreadAvailable();
100 
101             // Service manager registration
102             void                setTheContextObject(sp<BHwBinder> obj);
103 
104             bool                isLooperThread();
105             bool                isOnlyBinderThread();
106 
107             // Is this thread currently serving a hwbinder call. This method
108             // returns true if while traversing backwards from the function call
109             // stack for this thread, we encounter a function serving a hwbinder
110             // call before encountering a binder call / hitting the end of the
111             // call stack.
112             // Eg: If thread T1 went through the following call pattern
113             //     1) T1 receives and executes binder call B1.
114             //     2) While handling B1, T1 makes hwbinder call H1.
115             //     3) The handler of H1, calls into T1 with a callback H2.
116             // If isServingCall() is called during B1 before 3), this method
117             // will return false, else true.
118             //
119             //  ----
120             // | H2 | ---> While callback H2 is being handled during 3).
121             //  ----
122             // | B1 | ---> While B1 is being handled, hwbinder call H1 made.
123             //  ----
124             // Fig: Thread Call stack while handling H2.
125             //
126             // This is since after 3), while traversing the thread call stack,
127             // we hit a hwbinder call before a binder call / end of stack.
128             // This method may be typically used to determine whether to use
129             // hardware::IPCThreadState methods or IPCThreadState methods to
130             // infer information about thread state.
131             bool isServingCall() const;
132 
133             // Tasks which are done on the binder thread after the thread returns to the
134             // threadpool.
135             void addPostCommandTask(const std::function<void(void)>& task);
136 
137            private:
138             IPCThreadState();
139             ~IPCThreadState();
140 
141             status_t            sendReply(const Parcel& reply, uint32_t flags);
142             status_t            waitForResponse(Parcel *reply,
143                                                 status_t *acquireResult=nullptr);
144             status_t            talkWithDriver(bool doReceive=true);
145             status_t            writeTransactionData(int32_t cmd,
146                                                      uint32_t binderFlags,
147                                                      int32_t handle,
148                                                      uint32_t code,
149                                                      const Parcel& data,
150                                                      status_t* statusBuffer);
151             status_t            getAndExecuteCommand();
152             status_t            executeCommand(int32_t command);
153             void                processPendingDerefs();
154             void                processPostWriteDerefs();
155 
156             void                clearCaller();
157 
158     static  void                threadDestructor(void *st);
159     static  void                freeBuffer(Parcel* parcel,
160                                            const uint8_t* data, size_t dataSize,
161                                            const binder_size_t* objects, size_t objectsSize,
162                                            void* cookie);
163 
164     const   sp<ProcessState>    mProcess;
165             Vector<BHwBinder*>    mPendingStrongDerefs;
166             Vector<RefBase::weakref_type*> mPendingWeakDerefs;
167             Vector<RefBase*>    mPostWriteStrongDerefs;
168             Vector<RefBase::weakref_type*> mPostWriteWeakDerefs;
169             Parcel              mIn;
170             Parcel              mOut;
171             status_t            mLastError;
172             pid_t               mCallingPid;
173             const char*         mCallingSid;
174             uid_t               mCallingUid;
175             int32_t             mStrictModePolicy;
176             int32_t             mLastTransactionBinderFlags;
177             sp<BHwBinder>         mContextObject;
178             bool                mIsLooper;
179             bool mIsPollingThread;
180 
181             std::vector<std::function<void(void)>> mPostCommandTasks;
182             IPCThreadStateBase *mIPCThreadStateBase;
183 
184             ProcessState::CallRestriction mCallRestriction;
185 };
186 
187 }; // namespace hardware
188 }; // namespace android
189 
190 // ---------------------------------------------------------------------------
191 
192 #endif // ANDROID_HARDWARE_IPC_THREAD_STATE_H
193