blob: 2bd39a7759ec096ba8d0fbbaa8235409ef120aef [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
32class IPCThreadState
33{
34public:
35 static IPCThreadState* self();
Brad Fitzpatrick1b608432010-12-13 16:52:35 -080036 static IPCThreadState* selfOrNull(); // self(), but won't instantiate
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38 sp<ProcessState> process();
39
40 status_t clearLastError();
41
Steven Moreland70012962020-03-12 17:32:26 -070042 /**
43 * Returns the PID of the process which has made the current binder
44 * call. If not in a binder call, this will return getpid. If the
45 * call is oneway, this will return 0.
46 */
Dan Stoza9c634fd2014-11-26 12:23:23 -080047 pid_t getCallingPid() const;
Steven Moreland70012962020-03-12 17:32:26 -070048
49 /**
50 * Returns the SELinux security identifier of the process which has
51 * made the current binder call. If not in a binder call this will
52 * return nullptr. If this isn't requested with
Steven Moreland2f405f52020-07-08 22:24:29 +000053 * Binder::setRequestingSid, it will also return nullptr.
Steven Moreland70012962020-03-12 17:32:26 -070054 *
55 * This can't be restored once it's cleared, and it does not return the
56 * context of the current process when not in a binder call.
57 */
Steven Morelandf0212002018-12-26 13:59:23 -080058 const char* getCallingSid() const;
Steven Moreland70012962020-03-12 17:32:26 -070059
60 /**
61 * Returns the UID of the process which has made the current binder
62 * call. If not in a binder call, this will return 0.
63 */
Dan Stoza9c634fd2014-11-26 12:23:23 -080064 uid_t getCallingUid() const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070065
66 void setStrictModePolicy(int32_t policy);
67 int32_t getStrictModePolicy() const;
Brad Fitzpatrick52736032010-08-30 16:01:16 -070068
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +000069 // See Binder#setCallingWorkSourceUid in Binder.java.
70 int64_t setCallingWorkSourceUid(uid_t uid);
Olivier Gaillard91a04802018-11-14 17:32:41 +000071 // Internal only. Use setCallingWorkSourceUid(uid) instead.
72 int64_t setCallingWorkSourceUidWithoutPropagation(uid_t uid);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +000073 // See Binder#getCallingWorkSourceUid in Binder.java.
74 uid_t getCallingWorkSourceUid() const;
75 // See Binder#clearCallingWorkSource in Binder.java.
76 int64_t clearCallingWorkSource();
77 // See Binder#restoreCallingWorkSource in Binder.java.
78 void restoreCallingWorkSource(int64_t token);
Olivier Gaillard91a04802018-11-14 17:32:41 +000079 void clearPropagateWorkSource();
80 bool shouldPropagateWorkSource() const;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010081
Brad Fitzpatrick52736032010-08-30 16:01:16 -070082 void setLastTransactionBinderFlags(int32_t flags);
83 int32_t getLastTransactionBinderFlags() const;
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 int64_t clearCallingIdentity();
Steven Morelandf0212002018-12-26 13:59:23 -080086 // Restores PID/UID (not SID)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 void restoreCallingIdentity(int64_t token);
Steven Morelandd8c85672020-07-24 21:30:41 +000088
89 status_t setupPolling(int* fd);
Todd Poynor8d96cab2013-06-25 19:12:18 -070090 status_t handlePolledCommands();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 void flushCommands();
92
93 void joinThreadPool(bool isMain = true);
94
95 // Stop the local process.
96 void stopProcess(bool immediate = true);
97
98 status_t transact(int32_t handle,
99 uint32_t code, const Parcel& data,
100 Parcel* reply, uint32_t flags);
101
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700102 void incStrongHandle(int32_t handle, BpBinder *proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 void decStrongHandle(int32_t handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700104 void incWeakHandle(int32_t handle, BpBinder *proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 void decWeakHandle(int32_t handle);
106 status_t attemptIncStrongHandle(int32_t handle);
107 static void expungeHandle(int32_t handle, IBinder* binder);
108 status_t requestDeathNotification( int32_t handle,
109 BpBinder* proxy);
110 status_t clearDeathNotification( int32_t handle,
111 BpBinder* proxy);
112
113 static void shutdown();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700114
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -0800115 // Call this to disable switching threads to background scheduling when
116 // receiving incoming IPC calls. This is specifically here for the
117 // Android system process, since it expects to have background apps calling
118 // in to it but doesn't want to acquire locks in its services while in
119 // the background.
120 static void disableBackgroundScheduling(bool disable);
Martijn Coenen2b631742017-05-05 11:16:59 -0700121 bool backgroundSchedulingDisabled();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700122
123 // Call blocks until the number of executing binder threads is less than
124 // the maximum number of binder threads threads allowed for this process.
125 void blockUntilThreadAvailable();
126
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700127 // Service manager registration
128 void setTheContextObject(sp<BBinder> obj);
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000129
Steven Moreland39d887d2020-01-31 14:56:45 -0800130 // WARNING: DO NOT USE THIS API
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000131 //
Steven Moreland39d887d2020-01-31 14:56:45 -0800132 // Returns a pointer to the stack from the last time a transaction
133 // was initiated by the kernel. Used to compare when making nested
134 // calls between multiple different transports.
135 const void* getServingStackPointer() const;
Jayant Chowdharydac6dc82018-10-01 22:52:44 +0000136
Olivier Gaillard91a04802018-11-14 17:32:41 +0000137 // The work source represents the UID of the process we should attribute the transaction
Olivier Gaillard68c9eea2018-11-30 10:49:42 +0000138 // to. We use -1 to specify that the work source was not set using #setWorkSource.
139 //
140 // This constant needs to be kept in sync with Binder.UNSET_WORKSOURCE from the Java
141 // side.
Olivier Gaillard91a04802018-11-14 17:32:41 +0000142 static const int32_t kUnsetWorkSource = -1;
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144private:
145 IPCThreadState();
146 ~IPCThreadState();
147
Martijn Coenenea0090a2017-11-02 18:54:40 +0000148 status_t sendReply(const Parcel& reply, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 status_t waitForResponse(Parcel *reply,
Yi Kong87d465c2018-07-24 01:14:06 -0700150 status_t *acquireResult=nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 status_t talkWithDriver(bool doReceive=true);
152 status_t writeTransactionData(int32_t cmd,
153 uint32_t binderFlags,
154 int32_t handle,
155 uint32_t code,
156 const Parcel& data,
157 status_t* statusBuffer);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700158 status_t getAndExecuteCommand();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 status_t executeCommand(int32_t command);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700160 void processPendingDerefs();
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700161 void processPostWriteDerefs();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 void clearCaller();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 static void threadDestructor(void *st);
166 static void freeBuffer(Parcel* parcel,
167 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800168 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 void* cookie);
170
171 const sp<ProcessState> mProcess;
172 Vector<BBinder*> mPendingStrongDerefs;
173 Vector<RefBase::weakref_type*> mPendingWeakDerefs;
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700174 Vector<RefBase*> mPostWriteStrongDerefs;
175 Vector<RefBase::weakref_type*> mPostWriteWeakDerefs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 Parcel mIn;
177 Parcel mOut;
178 status_t mLastError;
Steven Moreland39d887d2020-01-31 14:56:45 -0800179 const void* mServingStackPointer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 pid_t mCallingPid;
Steven Morelandf0212002018-12-26 13:59:23 -0800181 const char* mCallingSid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 uid_t mCallingUid;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100183 // The UID of the process who is responsible for this transaction.
184 // This is used for resource attribution.
185 int32_t mWorkSource;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000186 // Whether the work source should be propagated.
187 bool mPropagateWorkSource;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700188 int32_t mStrictModePolicy;
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700189 int32_t mLastTransactionBinderFlags;
Steven Moreland7732a092019-01-02 17:54:16 -0800190
191 ProcessState::CallRestriction mCallRestriction;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192};
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700193
Steven Moreland61ff8492019-09-26 16:05:45 -0700194} // namespace android
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195
196// ---------------------------------------------------------------------------
197
198#endif // ANDROID_IPC_THREAD_STATE_H