• 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 // 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 
31 #if defined(_WIN32)
32 typedef  int  uid_t;
33 #endif
34 
35 // ---------------------------------------------------------------------------
36 namespace android {
37 
38 namespace hardware {
39 
40 class IPCThreadState
41 {
42 public:
43     static  IPCThreadState*     self();
44     static  IPCThreadState*     selfOrNull();  // self(), but won't instantiate
45 
46             sp<ProcessState>    process();
47 
48             status_t            clearLastError();
49 
50             pid_t               getCallingPid() const;
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;
56             uid_t               getCallingUid() const;
57 
58             void                setStrictModePolicy(int32_t policy);
59             int32_t             getStrictModePolicy() const;
60 
61             void                setLastTransactionBinderFlags(int32_t flags);
62             int32_t             getLastTransactionBinderFlags() const;
63 
64             int64_t             clearCallingIdentity();
65             // Restores PID/UID (not SID)
66             void                restoreCallingIdentity(int64_t token);
67 
68             int                 setupPolling(int* fd);
69             status_t            handlePolledCommands();
70             void                flushCommands();
71 
72             void                joinThreadPool(bool isMain = true);
73 
74             // Stop the local process.
75             void                stopProcess(bool immediate = true);
76 
77             status_t            transact(int32_t handle,
78                                          uint32_t code, const Parcel& data,
79                                          Parcel* reply, uint32_t flags);
80 
81             void                incStrongHandle(int32_t handle, BpHwBinder *proxy);
82             void                decStrongHandle(int32_t handle);
83             void                incWeakHandle(int32_t handle, BpHwBinder *proxy);
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,
88                                                             BpHwBinder* proxy);
89             status_t            clearDeathNotification( int32_t handle,
90                                                         BpHwBinder* proxy);
91 
92     static  void                shutdown();
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