1 /* 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 #pragma once 18 19 #include <binder/Common.h> 20 #include <binder/IBinder.h> 21 #include <utils/String16.h> 22 #include <utils/String8.h> 23 24 #include <pthread.h> 25 26 #include <mutex> 27 28 // --------------------------------------------------------------------------- 29 namespace android { 30 31 class IPCThreadState; 32 33 /** 34 * Kernel binder process state. All operations here refer to kernel binder. This 35 * object is allocated per process. 36 */ 37 class ProcessState : public virtual RefBase { 38 public: 39 LIBBINDER_EXPORTED static sp<ProcessState> self(); 40 LIBBINDER_EXPORTED static sp<ProcessState> selfOrNull(); 41 42 LIBBINDER_EXPORTED static bool isVndservicemanagerEnabled(); 43 44 /* initWithDriver() can be used to configure libbinder to use 45 * a different binder driver dev node. It must be called *before* 46 * any call to ProcessState::self(). The default is /dev/vndbinder 47 * for processes built with the VNDK and /dev/binder for those 48 * which are not. 49 * 50 * If this is called with nullptr, the behavior is the same as selfOrNull. 51 */ 52 LIBBINDER_EXPORTED static sp<ProcessState> initWithDriver(const char* driver); 53 54 LIBBINDER_EXPORTED sp<IBinder> getContextObject(const sp<IBinder>& caller); 55 56 // This should be called before startThreadPool at the beginning 57 // of a program, and libraries should never call it because programs 58 // should configure their own threadpools. The threadpool size can 59 // never be decreased. 60 // 61 // The 'maxThreads' value refers to the total number of threads 62 // that will be started by the kernel. This is in addition to any 63 // threads started by 'startThreadPool' or 'joinRpcThreadpool'. 64 LIBBINDER_EXPORTED status_t setThreadPoolMaxThreadCount(size_t maxThreads); 65 66 // Libraries should not call this, as processes should configure 67 // threadpools themselves. Should be called in the main function 68 // directly before any code executes or joins the threadpool. 69 // 70 // Starts one thread, PLUS those requested in setThreadPoolMaxThreadCount, 71 // PLUS those manually requested in joinThreadPool. 72 // 73 // For instance, if setThreadPoolMaxCount(3) is called and 74 // startThreadpPool (+1 thread) and joinThreadPool (+1 thread) 75 // are all called, then up to 5 threads can be started. 76 LIBBINDER_EXPORTED void startThreadPool(); 77 78 [[nodiscard]] LIBBINDER_EXPORTED bool becomeContextManager(); 79 80 LIBBINDER_EXPORTED sp<IBinder> getStrongProxyForHandle(int32_t handle); 81 LIBBINDER_EXPORTED void expungeHandle(int32_t handle, IBinder* binder); 82 83 // TODO: deprecate. 84 LIBBINDER_EXPORTED void spawnPooledThread(bool isMain); 85 86 LIBBINDER_EXPORTED status_t enableOnewaySpamDetection(bool enable); 87 88 // Set the name of the current thread to look like a threadpool 89 // thread. Typically this is called before joinThreadPool. 90 // 91 // TODO: remove this API, and automatically set it intelligently. 92 LIBBINDER_EXPORTED void giveThreadPoolName(); 93 94 LIBBINDER_EXPORTED String8 getDriverName(); 95 96 LIBBINDER_EXPORTED ssize_t getKernelReferences(size_t count, uintptr_t* buf); 97 98 // Only usable by the context manager. 99 // This refcount includes: 100 // 1. Strong references to the node by this and other processes 101 // 2. Temporary strong references held by the kernel during a 102 // transaction on the node. 103 // It does NOT include local strong references to the node 104 LIBBINDER_EXPORTED ssize_t getStrongRefCountForNode(const sp<BpBinder>& binder); 105 106 enum class CallRestriction { 107 // all calls okay 108 NONE, 109 // log when calls are blocking 110 ERROR_IF_NOT_ONEWAY, 111 // abort process on blocking calls 112 FATAL_IF_NOT_ONEWAY, 113 }; 114 // Sets calling restrictions for all transactions in this process. This must be called 115 // before any threads are spawned. 116 LIBBINDER_EXPORTED void setCallRestriction(CallRestriction restriction); 117 118 /** 119 * Get the max number of threads that have joined the thread pool. 120 * This includes kernel started threads, user joined threads and polling 121 * threads if used. 122 */ 123 LIBBINDER_EXPORTED size_t getThreadPoolMaxTotalThreadCount() const; 124 125 /** 126 * Check to see if the thread pool has started. 127 */ 128 LIBBINDER_EXPORTED bool isThreadPoolStarted() const; 129 130 enum class DriverFeature { 131 ONEWAY_SPAM_DETECTION, 132 EXTENDED_ERROR, 133 }; 134 // Determine whether a feature is supported by the binder driver. 135 LIBBINDER_EXPORTED static bool isDriverFeatureEnabled(const DriverFeature feature); 136 137 private: 138 static sp<ProcessState> init(const char* defaultDriver, bool requireDefault); 139 140 static void onFork(); 141 static void parentPostFork(); 142 static void childPostFork(); 143 144 friend class IPCThreadState; 145 friend class sp<ProcessState>; 146 147 explicit ProcessState(const char* driver); 148 ~ProcessState(); 149 150 ProcessState(const ProcessState& o); 151 ProcessState& operator=(const ProcessState& o); 152 String8 makeBinderThreadName(); 153 154 struct handle_entry { 155 IBinder* binder; 156 RefBase::weakref_type* refs; 157 }; 158 159 handle_entry* lookupHandleLocked(int32_t handle); 160 161 String8 mDriverName; 162 int mDriverFD; 163 void* mVMStart; 164 165 // Protects thread count and wait variables below. 166 mutable pthread_mutex_t mThreadCountLock; 167 // Broadcast whenever mWaitingForThreads > 0 168 pthread_cond_t mThreadCountDecrement; 169 // Number of binder threads current executing a command. 170 size_t mExecutingThreadsCount; 171 // Number of threads calling IPCThreadState::blockUntilThreadAvailable() 172 size_t mWaitingForThreads; 173 // Maximum number of lazy threads to be started in the threadpool by the kernel. 174 size_t mMaxThreads; 175 // Current number of threads inside the thread pool. 176 size_t mCurrentThreads; 177 // Current number of pooled threads inside the thread pool. 178 size_t mKernelStartedThreads; 179 // Time when thread pool was emptied 180 int64_t mStarvationStartTimeMs; 181 182 mutable std::mutex mLock; // protects everything below. 183 184 Vector<handle_entry> mHandleToObject; 185 186 bool mForked; 187 bool mThreadPoolStarted; 188 volatile int32_t mThreadPoolSeq; 189 190 CallRestriction mCallRestriction; 191 }; 192 193 } // namespace android 194 195 // --------------------------------------------------------------------------- 196