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 #ifndef ANDROID_HARDWARE_PROCESS_STATE_H
18 #define ANDROID_HARDWARE_PROCESS_STATE_H
19 
20 #include <hwbinder/IBinder.h>
21 #include <utils/KeyedVector.h>
22 #include <utils/String8.h>
23 #include <utils/String16.h>
24 
25 #include <utils/threads.h>
26 
27 #include <pthread.h>
28 
29 // ---------------------------------------------------------------------------
30 namespace android {
31 namespace hardware {
32 
33 class IPCThreadState;
34 
35 class ProcessState : public virtual RefBase
36 {
37 public:
38     static  sp<ProcessState>    self();
39     static  sp<ProcessState>    selfOrNull();
40     // Note: don't call self() or selfOrNull() before initWithMmapSize()
41     // with '0' as an argument, this is the same as selfOrNull
42     static  sp<ProcessState>    initWithMmapSize(size_t mmapSize); // size in bytes
43 
44             void                setContextObject(const sp<IBinder>& object);
45             sp<IBinder>         getContextObject(const sp<IBinder>& caller);
46 
47             void                setContextObject(const sp<IBinder>& object,
48                                                  const String16& name);
49             sp<IBinder>         getContextObject(const String16& name,
50                                                  const sp<IBinder>& caller);
51 
52             void                startThreadPool();
53 
54     typedef bool (*context_check_func)(const String16& name,
55                                        const sp<IBinder>& caller,
56                                        void* userData);
57 
58             bool                isContextManager(void) const;
59             bool                becomeContextManager(
60                                     context_check_func checkFunc,
61                                     void* userData);
62 
63             sp<IBinder>         getStrongProxyForHandle(int32_t handle);
64             wp<IBinder>         getWeakProxyForHandle(int32_t handle);
65             void                expungeHandle(int32_t handle, IBinder* binder);
66 
67             void                spawnPooledThread(bool isMain);
68 
69             status_t            setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool);
70             size_t              getMaxThreads();
71             void                giveThreadPoolName();
72 
73             ssize_t             getKernelReferences(size_t count, uintptr_t* buf);
74                                 // This refcount includes:
75                                 // 1. Strong references to the node by this  and other processes
76                                 // 2. Temporary strong references held by the kernel during a
77                                 //    transaction on the node.
78                                 // It does NOT include local strong references to the node
79             ssize_t             getStrongRefCountForNodeByHandle(int32_t handle);
80             size_t              getMmapSize();
81 
82             enum class CallRestriction {
83                 // all calls okay
84                 NONE,
85                 // log when calls are blocking
86                 ERROR_IF_NOT_ONEWAY,
87                 // abort process on blocking calls
88                 FATAL_IF_NOT_ONEWAY,
89             };
90             // Sets calling restrictions for all transactions in this process. This must be called
91             // before any threads are spawned.
92             void setCallRestriction(CallRestriction restriction);
93 
94 private:
95     static  sp<ProcessState>    init(size_t mmapSize, bool requireMmapSize);
96 
97     friend class IPCThreadState;
98             explicit            ProcessState(size_t mmapSize);
99                                 ~ProcessState();
100 
101                                 ProcessState(const ProcessState& o);
102             ProcessState&       operator=(const ProcessState& o);
103             String8             makeBinderThreadName();
104 
105             struct handle_entry {
106                 IBinder* binder;
107                 RefBase::weakref_type* refs;
108             };
109 
110             handle_entry*       lookupHandleLocked(int32_t handle);
111 
112             int                 mDriverFD;
113             void*               mVMStart;
114 
115             // Protects thread count variable below.
116             pthread_mutex_t     mThreadCountLock;
117             pthread_cond_t      mThreadCountDecrement;
118             // Number of binder threads current executing a command.
119             size_t              mExecutingThreadsCount;
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                mManagesContexts;
130             context_check_func  mBinderContextCheckFunc;
131             void*               mBinderContextUserData;
132 
133             KeyedVector<String16, sp<IBinder> >
134                                 mContexts;
135 
136 
137             String8             mRootDir;
138             bool                mThreadPoolStarted;
139             bool                mSpawnThreadOnStart;
140     volatile int32_t            mThreadPoolSeq;
141             const size_t        mMmapSize;
142 
143             CallRestriction     mCallRestriction;
144 };
145 
146 } // namespace hardware
147 } // namespace android
148 
149 // ---------------------------------------------------------------------------
150 
151 #endif // ANDROID_HARDWARE_PROCESS_STATE_H
152