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/IBinder.h> 20 #include <utils/KeyedVector.h> 21 #include <utils/Mutex.h> 22 #include <utils/String16.h> 23 #include <utils/String8.h> 24 25 #include <pthread.h> 26 27 // --------------------------------------------------------------------------- 28 namespace android { 29 30 class IPCThreadState; 31 32 class ProcessState : public virtual RefBase { 33 public: 34 static sp<ProcessState> self(); 35 static sp<ProcessState> selfOrNull(); 36 37 /* initWithDriver() can be used to configure libbinder to use 38 * a different binder driver dev node. It must be called *before* 39 * any call to ProcessState::self(). The default is /dev/vndbinder 40 * for processes built with the VNDK and /dev/binder for those 41 * which are not. 42 * 43 * If this is called with nullptr, the behavior is the same as selfOrNull. 44 */ 45 static sp<ProcessState> initWithDriver(const char* driver); 46 47 sp<IBinder> getContextObject(const sp<IBinder>& caller); 48 49 void startThreadPool(); 50 51 bool becomeContextManager(); 52 53 sp<IBinder> getStrongProxyForHandle(int32_t handle); 54 void expungeHandle(int32_t handle, IBinder* binder); 55 56 void spawnPooledThread(bool isMain); 57 58 status_t setThreadPoolMaxThreadCount(size_t maxThreads); 59 status_t enableOnewaySpamDetection(bool enable); 60 void giveThreadPoolName(); 61 62 String8 getDriverName(); 63 64 ssize_t getKernelReferences(size_t count, uintptr_t* buf); 65 66 // Only usable by the context manager. 67 // This refcount includes: 68 // 1. Strong references to the node by this and other processes 69 // 2. Temporary strong references held by the kernel during a 70 // transaction on the node. 71 // It does NOT include local strong references to the node 72 ssize_t getStrongRefCountForNode(const sp<BpBinder>& binder); 73 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 86 /** 87 * Get the max number of threads that the kernel can start. 88 * 89 * Note: this is the lower bound. Additional threads may be started. 90 */ 91 size_t getThreadPoolMaxThreadCount() const; 92 93 enum class DriverFeature { 94 ONEWAY_SPAM_DETECTION, 95 }; 96 // Determine whether a feature is supported by the binder driver. 97 static bool isDriverFeatureEnabled(const DriverFeature feature); 98 99 private: 100 static sp<ProcessState> init(const char* defaultDriver, bool requireDefault); 101 102 static void onFork(); 103 static void parentPostFork(); 104 static void childPostFork(); 105 106 friend class IPCThreadState; 107 friend class sp<ProcessState>; 108 109 explicit ProcessState(const char* driver); 110 ~ProcessState(); 111 112 ProcessState(const ProcessState& o); 113 ProcessState& operator=(const ProcessState& o); 114 String8 makeBinderThreadName(); 115 116 struct handle_entry { 117 IBinder* binder; 118 RefBase::weakref_type* refs; 119 }; 120 121 handle_entry* lookupHandleLocked(int32_t handle); 122 123 String8 mDriverName; 124 int mDriverFD; 125 void* mVMStart; 126 127 // Protects thread count and wait variables below. 128 pthread_mutex_t mThreadCountLock; 129 // Broadcast whenever mWaitingForThreads > 0 130 pthread_cond_t mThreadCountDecrement; 131 // Number of binder threads current executing a command. 132 size_t mExecutingThreadsCount; 133 // Number of threads calling IPCThreadState::blockUntilThreadAvailable() 134 size_t mWaitingForThreads; 135 // Maximum number for binder threads allowed for this process. 136 size_t mMaxThreads; 137 // Time when thread pool was emptied 138 int64_t mStarvationStartTimeMs; 139 140 mutable Mutex mLock; // protects everything below. 141 142 Vector<handle_entry> mHandleToObject; 143 144 bool mForked; 145 bool mThreadPoolStarted; 146 volatile int32_t mThreadPoolSeq; 147 148 CallRestriction mCallRestriction; 149 }; 150 151 } // namespace android 152 153 // --------------------------------------------------------------------------- 154