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