blob: f4c9f9d885413d9df657352642bec303781daeb8 [file] [log] [blame]
Mathias Agopian16475702009-05-19 19:08:10 -07001/*
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
Martijn Coenenf75a23d2016-08-01 11:55:17 +020017#ifndef ANDROID_HARDWARE_IPC_THREAD_STATE_H
18#define ANDROID_HARDWARE_IPC_THREAD_STATE_H
Mathias Agopian16475702009-05-19 19:08:10 -070019
20#include <utils/Errors.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020021#include <hwbinder/Parcel.h>
22#include <hwbinder/ProcessState.h>
Mathias Agopian16475702009-05-19 19:08:10 -070023#include <utils/Vector.h>
24
Steven Morelandd7bbfdb2018-05-01 16:30:46 -070025#include <functional>
26
Steven Morelandb5f6e002021-02-26 01:51:20 +000027// 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
Yabin Cui6db6ea52014-11-11 09:26:00 -080031#if defined(_WIN32)
Mathias Agopian16475702009-05-19 19:08:10 -070032typedef int uid_t;
33#endif
34
35// ---------------------------------------------------------------------------
36namespace android {
Jayant Chowdhary985fc892018-10-01 22:54:05 +000037
Martijn Coenenf75a23d2016-08-01 11:55:17 +020038namespace hardware {
Mathias Agopian16475702009-05-19 19:08:10 -070039
40class IPCThreadState
41{
42public:
43 static IPCThreadState* self();
Brad Fitzpatrick77949942010-12-13 16:52:35 -080044 static IPCThreadState* selfOrNull(); // self(), but won't instantiate
Yifan Hongdde40f32017-01-12 14:22:45 -080045
Mathias Agopian16475702009-05-19 19:08:10 -070046 sp<ProcessState> process();
Yifan Hongdde40f32017-01-12 14:22:45 -080047
Mathias Agopian16475702009-05-19 19:08:10 -070048 status_t clearLastError();
49
Dan Stozae8da8a42014-11-26 12:23:23 -080050 pid_t getCallingPid() const;
Steven Moreland7375a872018-12-26 13:59:23 -080051 // 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;
Dan Stozae8da8a42014-11-26 12:23:23 -080056 uid_t getCallingUid() const;
Brad Fitzpatrick94c36342010-06-18 13:07:53 -070057
58 void setStrictModePolicy(int32_t policy);
59 int32_t getStrictModePolicy() const;
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -070060
61 void setLastTransactionBinderFlags(int32_t flags);
62 int32_t getLastTransactionBinderFlags() const;
63
Mathias Agopian16475702009-05-19 19:08:10 -070064 int64_t clearCallingIdentity();
Steven Moreland7375a872018-12-26 13:59:23 -080065 // Restores PID/UID (not SID)
Mathias Agopian16475702009-05-19 19:08:10 -070066 void restoreCallingIdentity(int64_t token);
Yifan Hongdde40f32017-01-12 14:22:45 -080067
Todd Poynor0646cb02013-06-25 19:12:18 -070068 int setupPolling(int* fd);
69 status_t handlePolledCommands();
Mathias Agopian16475702009-05-19 19:08:10 -070070 void flushCommands();
71
72 void joinThreadPool(bool isMain = true);
Yifan Hongdde40f32017-01-12 14:22:45 -080073
Mathias Agopian16475702009-05-19 19:08:10 -070074 // Stop the local process.
75 void stopProcess(bool immediate = true);
Yifan Hongdde40f32017-01-12 14:22:45 -080076
Mathias Agopian16475702009-05-19 19:08:10 -070077 status_t transact(int32_t handle,
78 uint32_t code, const Parcel& data,
79 Parcel* reply, uint32_t flags);
80
Martijn Coenenb8253722018-05-23 15:33:22 +020081 void incStrongHandle(int32_t handle, BpHwBinder *proxy);
Mathias Agopian16475702009-05-19 19:08:10 -070082 void decStrongHandle(int32_t handle);
Martijn Coenenb8253722018-05-23 15:33:22 +020083 void incWeakHandle(int32_t handle, BpHwBinder *proxy);
Mathias Agopian16475702009-05-19 19:08:10 -070084 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,
Yifan Hong1e118d22017-01-12 14:42:28 -080088 BpHwBinder* proxy);
Mathias Agopian16475702009-05-19 19:08:10 -070089 status_t clearDeathNotification( int32_t handle,
Yifan Hong1e118d22017-01-12 14:42:28 -080090 BpHwBinder* proxy);
Mathias Agopian16475702009-05-19 19:08:10 -070091
92 static void shutdown();
Wale Ogunwale2e604f02015-04-13 16:16:10 -070093
Martijn Coenena660cbc2016-05-12 11:29:23 +020094 // Service manager registration
Yifan Hongdde40f32017-01-12 14:22:45 -080095 void setTheContextObject(sp<BHwBinder> obj);
Martijn Coenen420d4bb2017-10-24 11:43:55 +020096
97 bool isLooperThread();
Tobias Lindskoga36d5762018-01-05 10:28:31 +010098 bool isOnlyBinderThread();
99
Steven Morelande3785d02020-01-31 14:58:48 -0800100 // WARNING: DO NOT USE THIS API
Jayant Chowdhary985fc892018-10-01 22:54:05 +0000101 //
Steven Morelande3785d02020-01-31 14:58:48 -0800102 // 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;
Jayant Chowdhary985fc892018-10-01 22:54:05 +0000106
Steven Morelandd7bbfdb2018-05-01 16:30:46 -0700107 // 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();
Mathias Agopian16475702009-05-19 19:08:10 -0700114
115 status_t sendReply(const Parcel& reply, uint32_t flags);
116 status_t waitForResponse(Parcel *reply,
Yi Kong55d41072018-07-23 14:55:39 -0700117 status_t *acquireResult=nullptr);
Mathias Agopian16475702009-05-19 19:08:10 -0700118 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);
Todd Poynor0646cb02013-06-25 19:12:18 -0700125 status_t getAndExecuteCommand();
Mathias Agopian16475702009-05-19 19:08:10 -0700126 status_t executeCommand(int32_t command);
Todd Poynor0646cb02013-06-25 19:12:18 -0700127 void processPendingDerefs();
Martijn Coenenb8253722018-05-23 15:33:22 +0200128 void processPostWriteDerefs();
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700129
Mathias Agopian16475702009-05-19 19:08:10 -0700130 void clearCaller();
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700131
Mathias Agopian16475702009-05-19 19:08:10 -0700132 static void threadDestructor(void *st);
133 static void freeBuffer(Parcel* parcel,
134 const uint8_t* data, size_t dataSize,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800135 const binder_size_t* objects, size_t objectsSize,
Mathias Agopian16475702009-05-19 19:08:10 -0700136 void* cookie);
Yifan Hongdde40f32017-01-12 14:22:45 -0800137
Mathias Agopian16475702009-05-19 19:08:10 -0700138 const sp<ProcessState> mProcess;
Yifan Hongdde40f32017-01-12 14:22:45 -0800139 Vector<BHwBinder*> mPendingStrongDerefs;
Mathias Agopian16475702009-05-19 19:08:10 -0700140 Vector<RefBase::weakref_type*> mPendingWeakDerefs;
Martijn Coenenb8253722018-05-23 15:33:22 +0200141 Vector<RefBase*> mPostWriteStrongDerefs;
142 Vector<RefBase::weakref_type*> mPostWriteWeakDerefs;
Mathias Agopian16475702009-05-19 19:08:10 -0700143 Parcel mIn;
144 Parcel mOut;
145 status_t mLastError;
Steven Morelande3785d02020-01-31 14:58:48 -0800146 const void* mServingStackPointer;
Mathias Agopian16475702009-05-19 19:08:10 -0700147 pid_t mCallingPid;
Steven Moreland7375a872018-12-26 13:59:23 -0800148 const char* mCallingSid;
Mathias Agopian16475702009-05-19 19:08:10 -0700149 uid_t mCallingUid;
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700150 int32_t mStrictModePolicy;
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -0700151 int32_t mLastTransactionBinderFlags;
Martijn Coenen420d4bb2017-10-24 11:43:55 +0200152 bool mIsLooper;
Tobias Lindskoga36d5762018-01-05 10:28:31 +0100153 bool mIsPollingThread;
Steven Morelandd7bbfdb2018-05-01 16:30:46 -0700154
155 std::vector<std::function<void(void)>> mPostCommandTasks;
Steven Moreland14603002019-01-02 17:54:16 -0800156
157 ProcessState::CallRestriction mCallRestriction;
Mathias Agopian16475702009-05-19 19:08:10 -0700158};
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700159
Steven Moreland7173a4c2019-09-26 15:55:02 -0700160} // namespace hardware
161} // namespace android
Mathias Agopian16475702009-05-19 19:08:10 -0700162
163// ---------------------------------------------------------------------------
164
Martijn Coenenf75a23d2016-08-01 11:55:17 +0200165#endif // ANDROID_HARDWARE_IPC_THREAD_STATE_H