blob: dce3f3856bfa536890214e1cd7a810496d5e30e9 [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
Dan Stoza9c634fd2014-11-26 12:23:23 -080042 pid_t getCallingPid() const;
43 uid_t getCallingUid() const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070044
45 void setStrictModePolicy(int32_t policy);
46 int32_t getStrictModePolicy() const;
Brad Fitzpatrick52736032010-08-30 16:01:16 -070047
48 void setLastTransactionBinderFlags(int32_t flags);
49 int32_t getLastTransactionBinderFlags() const;
50
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 int64_t clearCallingIdentity();
52 void restoreCallingIdentity(int64_t token);
53
Todd Poynor8d96cab2013-06-25 19:12:18 -070054 int setupPolling(int* fd);
55 status_t handlePolledCommands();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056 void flushCommands();
57
58 void joinThreadPool(bool isMain = true);
59
60 // Stop the local process.
61 void stopProcess(bool immediate = true);
62
63 status_t transact(int32_t handle,
64 uint32_t code, const Parcel& data,
65 Parcel* reply, uint32_t flags);
66
Martijn Coenen7c170bb2018-05-04 17:28:55 -070067 void incStrongHandle(int32_t handle, BpBinder *proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 void decStrongHandle(int32_t handle);
Martijn Coenen7c170bb2018-05-04 17:28:55 -070069 void incWeakHandle(int32_t handle, BpBinder *proxy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 void decWeakHandle(int32_t handle);
71 status_t attemptIncStrongHandle(int32_t handle);
72 static void expungeHandle(int32_t handle, IBinder* binder);
73 status_t requestDeathNotification( int32_t handle,
74 BpBinder* proxy);
75 status_t clearDeathNotification( int32_t handle,
76 BpBinder* proxy);
77
78 static void shutdown();
Wale Ogunwale376b8222015-04-13 16:16:10 -070079
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -080080 // Call this to disable switching threads to background scheduling when
81 // receiving incoming IPC calls. This is specifically here for the
82 // Android system process, since it expects to have background apps calling
83 // in to it but doesn't want to acquire locks in its services while in
84 // the background.
85 static void disableBackgroundScheduling(bool disable);
Martijn Coenen2b631742017-05-05 11:16:59 -070086 bool backgroundSchedulingDisabled();
Wale Ogunwale376b8222015-04-13 16:16:10 -070087
88 // Call blocks until the number of executing binder threads is less than
89 // the maximum number of binder threads threads allowed for this process.
90 void blockUntilThreadAvailable();
91
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092private:
93 IPCThreadState();
94 ~IPCThreadState();
95
Martijn Coenenea0090a2017-11-02 18:54:40 +000096 status_t sendReply(const Parcel& reply, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 status_t waitForResponse(Parcel *reply,
Yi Kong87d465c2018-07-24 01:14:06 -070098 status_t *acquireResult=nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 status_t talkWithDriver(bool doReceive=true);
100 status_t writeTransactionData(int32_t cmd,
101 uint32_t binderFlags,
102 int32_t handle,
103 uint32_t code,
104 const Parcel& data,
105 status_t* statusBuffer);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700106 status_t getAndExecuteCommand();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 status_t executeCommand(int32_t command);
Todd Poynor8d96cab2013-06-25 19:12:18 -0700108 void processPendingDerefs();
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700109 void processPostWriteDerefs();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 void clearCaller();
Wale Ogunwale376b8222015-04-13 16:16:10 -0700112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 static void threadDestructor(void *st);
114 static void freeBuffer(Parcel* parcel,
115 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800116 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 void* cookie);
118
119 const sp<ProcessState> mProcess;
120 Vector<BBinder*> mPendingStrongDerefs;
121 Vector<RefBase::weakref_type*> mPendingWeakDerefs;
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700122 Vector<RefBase*> mPostWriteStrongDerefs;
123 Vector<RefBase::weakref_type*> mPostWriteWeakDerefs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 Parcel mIn;
125 Parcel mOut;
126 status_t mLastError;
127 pid_t mCallingPid;
128 uid_t mCallingUid;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700129 int32_t mStrictModePolicy;
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700130 int32_t mLastTransactionBinderFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131};
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133}; // namespace android
134
135// ---------------------------------------------------------------------------
136
137#endif // ANDROID_IPC_THREAD_STATE_H