blob: 614b0b33dd9b48d9f26106ea0d0fbed472aafbaf [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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_IPC_THREAD_STATE_H
18#define ANDROID_IPC_THREAD_STATE_H
19
20#include <utils/Errors.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/Parcel.h>
22#include <binder/ProcessState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Vector.h>
24
Yabin Cui0dd549a2014-11-11 09:26:00 -080025#if defined(_WIN32)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026typedef int uid_t;
27#endif
28
29// ---------------------------------------------------------------------------
30namespace android {
31
Jayant Chowdharydac6dc82018-10-01 22:52:44 +000032class IPCThreadStateBase;
33
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034class IPCThreadState
35{
36public:
37 static IPCThreadState* self();
Brad Fitzpatrick1b608432010-12-13 16:52:35 -080038 static IPCThreadState* selfOrNull(); // self(), but won't instantiate
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40 sp<ProcessState> process();
41
42 status_t clearLastError();
43
Dan Stoza9c634fd2014-11-26 12:23:23 -080044 pid_t getCallingPid() const;
Steven Morelandf0212002018-12-26 13:59:23 -080045 // nullptr if unavailable
46 //
47 // this can't be restored once it's cleared, and it does not return the
48 // context of the current process when not in a binder call.
49 const char* getCallingSid() const;
Dan Stoza9c634fd2014-11-26 12:23:23 -080050 uid_t getCallingUid() const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070051
52 void setStrictModePolicy(int32_t policy);
53 int32_t getStrictModePolicy() const;
Brad Fitzpatrick52736032010-08-30 16:01:16 -070054
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +000055 // See Binder#setCallingWorkSourceUid in Binder.java.
56 int64_t setCallingWorkSourceUid(uid_t uid);
Olivier Gaillard91a04802018-11-14 17:32:41 +000057 // Internal only. Use setCallingWorkSourceUid(uid) instead.
58 int64_t setCallingWorkSourceUidWithoutPropagation(uid_t uid);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +000059 // See Binder#getCallingWorkSourceUid in Binder.java.
60 uid_t getCallingWorkSourceUid() const;
61 // See Binder#clearCallingWorkSource in Binder.java.
62 int64_t clearCallingWorkSource();
63 // See Binder#restoreCallingWorkSource in Binder.java.
64 void restoreCallingWorkSource(int64_t token);
Olivier Gaillard91a04802018-11-14 17:32:41 +000065 void clearPropagateWorkSource();
66 bool shouldPropagateWorkSource() const;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010067
Brad Fitzpatrick52736032010-08-30 16:01:16 -070068 void setLastTransactionBinderFlags(int32_t flags);
69 int32_t getLastTransactionBinderFlags() const;
70
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 int64_t clearCallingIdentity();
Steven Morelandf0212002018-12-26 13:59:23 -080072 // Restores PID/UID (not SID)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 void restoreCallingIdentity(int64_t token);
74
Todd Poynor8d96cab2013-06-25 19:12:18 -070075 int setupPolling(int* fd);
76 status_t handlePolledCommands();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 void flushCommands();
78
79 void joinThreadPool(bool isMain = true);
80
81 // Stop the local process.
82 void stopProcess(bool immediate = true);
83
84 status_t transact(int32_t handle,
85 uint32_t code, const Parcel& data,
86 Parcel* reply, uint32_t flags);
87
Martijn Coenen7c170bb2018-05-04 17:28:55 -070088 void incStrongHandle(int32_t handle, BpBinder *proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 void decStrongHandle(int32_t handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -070090 void incWeakHandle(int32_t handle, BpBinder *proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 void decWeakHandle(int32_t handle);
92 status_t attemptIncStrongHandle(int32_t handle);
93 static void expungeHandle(int32_t handle, IBinder* binder);
94 status_t requestDeathNotification( int32_t handle,
95 BpBinder* proxy);
96 status_t clearDeathNotification( int32_t handle,
97 BpBinder* proxy);
98
99 static void shutdown();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700100
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800101 // Call this to disable switching threads to background scheduling when
102 // receiving incoming IPC calls. This is specifically here for the
103 // Android system process, since it expects to have background apps calling
104 // in to it but doesn't want to acquire locks in its services while in
105 // the background.
106 static void disableBackgroundScheduling(bool disable);
Martijn Coenen2b631742017-05-05 11:16:59 -0700107 bool backgroundSchedulingDisabled();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700108
109 // Call blocks until the number of executing binder threads is less than
110 // the maximum number of binder threads threads allowed for this process.
111 void blockUntilThreadAvailable();
112
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000113
114 // Is this thread currently serving a binder call. This method
115 // returns true if while traversing backwards from the function call
116 // stack for this thread, we encounter a function serving a binder
117 // call before encountering a hwbinder call / hitting the end of the
118 // call stack.
119 // Eg: If thread T1 went through the following call pattern
120 // 1) T1 receives and executes hwbinder call H1.
121 // 2) While handling H1, T1 makes binder call B1.
122 // 3) The handler of B1, calls into T1 with a callback B2.
123 // If isServingCall() is called during H1 before 3), this method
124 // will return false, else true.
125 //
126 // ----
127 // | B2 | ---> While callback B2 is being handled, during 3).
128 // ----
129 // | H1 | ---> While H1 is being handled.
130 // ----
131 // Fig: Thread Call stack while handling B2
132 //
133 // This is since after 3), while traversing the thread call stack,
134 // we hit a binder call before a hwbinder call / end of stack. This
135 // method may be typically used to determine whether to use
136 // hardware::IPCThreadState methods or IPCThreadState methods to
137 // infer information about thread state.
138 bool isServingCall() const;
139
Olivier Gaillard91a04802018-11-14 17:32:41 +0000140 // The work source represents the UID of the process we should attribute the transaction
Olivier Gaillard68c9eea2018-11-30 10:49:42 +0000141 // to. We use -1 to specify that the work source was not set using #setWorkSource.
142 //
143 // This constant needs to be kept in sync with Binder.UNSET_WORKSOURCE from the Java
144 // side.
Olivier Gaillard91a04802018-11-14 17:32:41 +0000145 static const int32_t kUnsetWorkSource = -1;
146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147private:
148 IPCThreadState();
149 ~IPCThreadState();
150
Martijn Coenenea0090a2017-11-02 18:54:40 +0000151 status_t sendReply(const Parcel& reply, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 status_t waitForResponse(Parcel *reply,
Yi Kong87d465c2018-07-24 01:14:06 -0700153 status_t *acquireResult=nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 status_t talkWithDriver(bool doReceive=true);
155 status_t writeTransactionData(int32_t cmd,
156 uint32_t binderFlags,
157 int32_t handle,
158 uint32_t code,
159 const Parcel& data,
160 status_t* statusBuffer);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700161 status_t getAndExecuteCommand();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 status_t executeCommand(int32_t command);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700163 void processPendingDerefs();
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700164 void processPostWriteDerefs();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 void clearCaller();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700167
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 static void threadDestructor(void *st);
169 static void freeBuffer(Parcel* parcel,
170 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800171 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 void* cookie);
173
174 const sp<ProcessState> mProcess;
175 Vector<BBinder*> mPendingStrongDerefs;
176 Vector<RefBase::weakref_type*> mPendingWeakDerefs;
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700177 Vector<RefBase*> mPostWriteStrongDerefs;
178 Vector<RefBase::weakref_type*> mPostWriteWeakDerefs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 Parcel mIn;
180 Parcel mOut;
181 status_t mLastError;
182 pid_t mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -0800183 const char* mCallingSid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 uid_t mCallingUid;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100185 // The UID of the process who is responsible for this transaction.
186 // This is used for resource attribution.
187 int32_t mWorkSource;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000188 // Whether the work source should be propagated.
189 bool mPropagateWorkSource;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700190 int32_t mStrictModePolicy;
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700191 int32_t mLastTransactionBinderFlags;
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000192 IPCThreadStateBase *mIPCThreadStateBase;
Steven Moreland7732a092019-01-02 17:54:16 -0800193
194 ProcessState::CallRestriction mCallRestriction;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195};
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}; // namespace android
198
199// ---------------------------------------------------------------------------
200
201#endif // ANDROID_IPC_THREAD_STATE_H