blob: 56ac84639ac82283d9bea52f60c44848ebcf0151 [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_PROCESS_STATE_H
18#define ANDROID_HARDWARE_PROCESS_STATE_H
Mathias Agopian16475702009-05-19 19:08:10 -070019
Martijn Coenen4080edc2016-05-04 14:17:02 +020020#include <hwbinder/IBinder.h>
Mathias Agopian16475702009-05-19 19:08:10 -070021#include <utils/KeyedVector.h>
22#include <utils/String8.h>
23#include <utils/String16.h>
24
25#include <utils/threads.h>
26
Wale Ogunwale2e604f02015-04-13 16:16:10 -070027#include <pthread.h>
28
Steven Morelandb5f6e002021-02-26 01:51:20 +000029// WARNING: this code is part of libhwbinder, a fork of libbinder. Generally,
30// this means that it is only relevant to HIDL. Any AIDL- or libbinder-specific
31// code should not try to use these things.
32
Mathias Agopian16475702009-05-19 19:08:10 -070033// ---------------------------------------------------------------------------
34namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020035namespace hardware {
Mathias Agopian16475702009-05-19 19:08:10 -070036
Mathias Agopian16475702009-05-19 19:08:10 -070037class IPCThreadState;
38
39class ProcessState : public virtual RefBase
40{
41public:
42 static sp<ProcessState> self();
Colin Crossdd6dafb2017-06-22 12:29:13 -070043 static sp<ProcessState> selfOrNull();
Martijn Coenen32507d32018-03-22 17:12:57 +010044 // Note: don't call self() or selfOrNull() before initWithMmapSize()
Steven Morelandf050d282020-07-21 02:42:12 +000045 // with '0' as an argument, this is the same as selfOrNull
Martijn Coenen32507d32018-03-22 17:12:57 +010046 static sp<ProcessState> initWithMmapSize(size_t mmapSize); // size in bytes
Mathias Agopian16475702009-05-19 19:08:10 -070047
Mathias Agopian16475702009-05-19 19:08:10 -070048 void startThreadPool();
Martijn Coenenab00b6a2016-11-22 15:03:13 +010049
Steven Morelande9b4f912020-11-05 00:25:37 +000050 sp<IBinder> getContextObject(const sp<IBinder>& /*caller*/);
51 // only call once, without creating a pool
52 void becomeContextManager();
Mathias Agopian16475702009-05-19 19:08:10 -070053
54 sp<IBinder> getStrongProxyForHandle(int32_t handle);
55 wp<IBinder> getWeakProxyForHandle(int32_t handle);
56 void expungeHandle(int32_t handle, IBinder* binder);
57
Mathias Agopian16475702009-05-19 19:08:10 -070058 void spawnPooledThread(bool isMain);
Martijn Coenenab00b6a2016-11-22 15:03:13 +010059
60 status_t setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool);
Hang Lud1388da2021-03-24 14:30:06 +080061 status_t enableOnewaySpamDetection(bool enable);
Martijn Coenen420d4bb2017-10-24 11:43:55 +020062 size_t getMaxThreads();
Mathias Agopiand191ed72013-03-07 15:34:28 -080063 void giveThreadPoolName();
Mathias Agopian8297f502012-04-17 16:11:08 -070064
Colin Crossdd6dafb2017-06-22 12:29:13 -070065 ssize_t getKernelReferences(size_t count, uintptr_t* buf);
Martijn Coenen320e1d32018-09-07 10:41:33 +020066 // This refcount includes:
67 // 1. Strong references to the node by this and other processes
68 // 2. Temporary strong references held by the kernel during a
69 // transaction on the node.
70 // It does NOT include local strong references to the node
71 ssize_t getStrongRefCountForNodeByHandle(int32_t handle);
Martijn Coenen32507d32018-03-22 17:12:57 +010072 size_t getMmapSize();
Steven Moreland14603002019-01-02 17:54:16 -080073
74 enum class CallRestriction {
75 // all calls okay
76 NONE,
77 // log when calls are blocking
78 ERROR_IF_NOT_ONEWAY,
79 // abort process on blocking calls
80 FATAL_IF_NOT_ONEWAY,
81 };
82 // Sets calling restrictions for all transactions in this process. This must be called
83 // before any threads are spawned.
84 void setCallRestriction(CallRestriction restriction);
85
Mathias Agopian16475702009-05-19 19:08:10 -070086private:
Steven Morelandf050d282020-07-21 02:42:12 +000087 static sp<ProcessState> init(size_t mmapSize, bool requireMmapSize);
88
Mathias Agopian16475702009-05-19 19:08:10 -070089 friend class IPCThreadState;
Steven Morelandf050d282020-07-21 02:42:12 +000090 explicit ProcessState(size_t mmapSize);
Mathias Agopian16475702009-05-19 19:08:10 -070091 ~ProcessState();
92
93 ProcessState(const ProcessState& o);
94 ProcessState& operator=(const ProcessState& o);
Mathias Agopiand191ed72013-03-07 15:34:28 -080095 String8 makeBinderThreadName();
Wale Ogunwale2e604f02015-04-13 16:16:10 -070096
Mathias Agopian16475702009-05-19 19:08:10 -070097 struct handle_entry {
98 IBinder* binder;
99 RefBase::weakref_type* refs;
100 };
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700101
Mathias Agopian16475702009-05-19 19:08:10 -0700102 handle_entry* lookupHandleLocked(int32_t handle);
103
104 int mDriverFD;
105 void* mVMStart;
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700106
107 // Protects thread count variable below.
108 pthread_mutex_t mThreadCountLock;
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700109 // Number of binder threads current executing a command.
110 size_t mExecutingThreadsCount;
111 // Maximum number for binder threads allowed for this process.
112 size_t mMaxThreads;
Colin Crossb1dc6542016-04-15 14:29:55 -0700113 // Time when thread pool was emptied
114 int64_t mStarvationStartTimeMs;
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700115
Mathias Agopian16475702009-05-19 19:08:10 -0700116 mutable Mutex mLock; // protects everything below.
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700117
Mathias Agopian16475702009-05-19 19:08:10 -0700118 Vector<handle_entry>mHandleToObject;
119
Mathias Agopian16475702009-05-19 19:08:10 -0700120 String8 mRootDir;
121 bool mThreadPoolStarted;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100122 bool mSpawnThreadOnStart;
Mathias Agopian16475702009-05-19 19:08:10 -0700123 volatile int32_t mThreadPoolSeq;
Martijn Coenen418856e2018-03-27 09:28:07 +0200124 const size_t mMmapSize;
Steven Moreland14603002019-01-02 17:54:16 -0800125
126 CallRestriction mCallRestriction;
Mathias Agopian16475702009-05-19 19:08:10 -0700127};
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100128
Steven Moreland7173a4c2019-09-26 15:55:02 -0700129} // namespace hardware
130} // namespace android
Mathias Agopian16475702009-05-19 19:08:10 -0700131
132// ---------------------------------------------------------------------------
133
Martijn Coenenf75a23d2016-08-01 11:55:17 +0200134#endif // ANDROID_HARDWARE_PROCESS_STATE_H