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 namespace hardware {
35 
36 class IPCThreadState
37 {
38 public:
39     static  IPCThreadState*     self();
40     static  IPCThreadState*     selfOrNull();  // self(), but won't instantiate
41 
42             sp<ProcessState>    process();
43 
44             status_t            clearLastError();
45 
46             pid_t               getCallingPid() const;
47             // nullptr if unavailable
48             //
49             // this can't be restored once it's cleared, and it does not return the
50             // context of the current process when not in a binder call.
51             const char*         getCallingSid() const;
52             uid_t               getCallingUid() const;
53 
54             void                setStrictModePolicy(int32_t policy);
55             int32_t             getStrictModePolicy() const;
56 
57             void                setLastTransactionBinderFlags(int32_t flags);
58             int32_t             getLastTransactionBinderFlags() const;
59 
60             int64_t             clearCallingIdentity();
61             // Restores PID/UID (not SID)
62             void                restoreCallingIdentity(int64_t token);
63 
64             int                 setupPolling(int* fd);
65             status_t            handlePolledCommands();
66             void                flushCommands();
67 
68             void                joinThreadPool(bool isMain = true);
69 
70             // Stop the local process.
71             void                stopProcess(bool immediate = true);
72 
73             status_t            transact(int32_t handle,
74                                          uint32_t code, const Parcel& data,
75                                          Parcel* reply, uint32_t flags);
76 
77             void                incStrongHandle(int32_t handle, BpHwBinder *proxy);
78             void                decStrongHandle(int32_t handle);
79             void                incWeakHandle(int32_t handle, BpHwBinder *proxy);
80             void                decWeakHandle(int32_t handle);
81             status_t            attemptIncStrongHandle(int32_t handle);
82     static  void                expungeHandle(int32_t handle, IBinder* binder);
83             status_t            requestDeathNotification(   int32_t handle,
84                                                             BpHwBinder* proxy);
85             status_t            clearDeathNotification( int32_t handle,
86                                                         BpHwBinder* proxy);
87 
88     static  void                shutdown();
89 
90             // Call blocks until the number of executing binder threads is less than
91             // the maximum number of binder threads threads allowed for this process.
92             void                blockUntilThreadAvailable();
93 
94             // Service manager registration
95             void                setTheContextObject(sp<BHwBinder> obj);
96 
97             bool                isLooperThread();
98             bool                isOnlyBinderThread();
99 
100             // WARNING: DO NOT USE THIS API
101             //
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;
106 
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();
114 
115             status_t            sendReply(const Parcel& reply, uint32_t flags);
116             status_t            waitForResponse(Parcel *reply,
117                                                 status_t *acquireResult=nullptr);
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);
125             status_t            getAndExecuteCommand();
126             status_t            executeCommand(int32_t command);
127             void                processPendingDerefs();
128             void                processPostWriteDerefs();
129 
130             void                clearCaller();
131 
132     static  void                threadDestructor(void *st);
133     static  void                freeBuffer(Parcel* parcel,
134                                            const uint8_t* data, size_t dataSize,
135                                            const binder_size_t* objects, size_t objectsSize,
136                                            void* cookie);
137 
138     const   sp<ProcessState>    mProcess;
139             Vector<BHwBinder*>    mPendingStrongDerefs;
140             Vector<RefBase::weakref_type*> mPendingWeakDerefs;
141             Vector<RefBase*>    mPostWriteStrongDerefs;
142             Vector<RefBase::weakref_type*> mPostWriteWeakDerefs;
143             Parcel              mIn;
144             Parcel              mOut;
145             status_t            mLastError;
146             const void*         mServingStackPointer;
147             pid_t               mCallingPid;
148             const char*         mCallingSid;
149             uid_t               mCallingUid;
150             int32_t             mStrictModePolicy;
151             int32_t             mLastTransactionBinderFlags;
152             bool                mIsLooper;
153             bool mIsPollingThread;
154 
155             std::vector<std::function<void(void)>> mPostCommandTasks;
156 
157             ProcessState::CallRestriction mCallRestriction;
158 };
159 
160 } // namespace hardware
161 } // namespace android
162 
163 // ---------------------------------------------------------------------------
164 
165 #endif // ANDROID_HARDWARE_IPC_THREAD_STATE_H
166