1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2005 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "ProcessState"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <binder/ProcessState.h>
20*38e8c45fSAndroid Build Coastguard Worker
21*38e8c45fSAndroid Build Coastguard Worker #include <android-base/strings.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <binder/BpBinder.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <binder/Functional.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <binder/Stability.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <utils/AndroidThreads.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <utils/String8.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <utils/Thread.h>
30*38e8c45fSAndroid Build Coastguard Worker
31*38e8c45fSAndroid Build Coastguard Worker #include "Static.h"
32*38e8c45fSAndroid Build Coastguard Worker #include "Utils.h"
33*38e8c45fSAndroid Build Coastguard Worker #include "binder_module.h"
34*38e8c45fSAndroid Build Coastguard Worker
35*38e8c45fSAndroid Build Coastguard Worker #include <errno.h>
36*38e8c45fSAndroid Build Coastguard Worker #include <fcntl.h>
37*38e8c45fSAndroid Build Coastguard Worker #include <pthread.h>
38*38e8c45fSAndroid Build Coastguard Worker #include <stdio.h>
39*38e8c45fSAndroid Build Coastguard Worker #include <stdlib.h>
40*38e8c45fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
41*38e8c45fSAndroid Build Coastguard Worker #include <sys/mman.h>
42*38e8c45fSAndroid Build Coastguard Worker #include <sys/stat.h>
43*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
44*38e8c45fSAndroid Build Coastguard Worker #include <unistd.h>
45*38e8c45fSAndroid Build Coastguard Worker #include <mutex>
46*38e8c45fSAndroid Build Coastguard Worker
47*38e8c45fSAndroid Build Coastguard Worker #define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2)
48*38e8c45fSAndroid Build Coastguard Worker #define DEFAULT_MAX_BINDER_THREADS 15
49*38e8c45fSAndroid Build Coastguard Worker #define DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION 1
50*38e8c45fSAndroid Build Coastguard Worker
51*38e8c45fSAndroid Build Coastguard Worker #ifdef __ANDROID_VNDK__
52*38e8c45fSAndroid Build Coastguard Worker const char* kDefaultDriver = "/dev/vndbinder";
53*38e8c45fSAndroid Build Coastguard Worker #else
54*38e8c45fSAndroid Build Coastguard Worker const char* kDefaultDriver = "/dev/binder";
55*38e8c45fSAndroid Build Coastguard Worker #endif
56*38e8c45fSAndroid Build Coastguard Worker
57*38e8c45fSAndroid Build Coastguard Worker // -------------------------------------------------------------------------
58*38e8c45fSAndroid Build Coastguard Worker
59*38e8c45fSAndroid Build Coastguard Worker namespace {
readDriverFeatureFile(const char * filename)60*38e8c45fSAndroid Build Coastguard Worker bool readDriverFeatureFile(const char* filename) {
61*38e8c45fSAndroid Build Coastguard Worker int fd = open(filename, O_RDONLY | O_CLOEXEC);
62*38e8c45fSAndroid Build Coastguard Worker char on;
63*38e8c45fSAndroid Build Coastguard Worker if (fd == -1) {
64*38e8c45fSAndroid Build Coastguard Worker ALOGE_IF(errno != ENOENT, "%s: cannot open %s: %s", __func__, filename, strerror(errno));
65*38e8c45fSAndroid Build Coastguard Worker return false;
66*38e8c45fSAndroid Build Coastguard Worker }
67*38e8c45fSAndroid Build Coastguard Worker if (read(fd, &on, sizeof(on)) == -1) {
68*38e8c45fSAndroid Build Coastguard Worker ALOGE("%s: error reading to %s: %s", __func__, filename, strerror(errno));
69*38e8c45fSAndroid Build Coastguard Worker close(fd);
70*38e8c45fSAndroid Build Coastguard Worker return false;
71*38e8c45fSAndroid Build Coastguard Worker }
72*38e8c45fSAndroid Build Coastguard Worker close(fd);
73*38e8c45fSAndroid Build Coastguard Worker return on == '1';
74*38e8c45fSAndroid Build Coastguard Worker }
75*38e8c45fSAndroid Build Coastguard Worker
76*38e8c45fSAndroid Build Coastguard Worker } // namespace
77*38e8c45fSAndroid Build Coastguard Worker
78*38e8c45fSAndroid Build Coastguard Worker namespace android {
79*38e8c45fSAndroid Build Coastguard Worker
80*38e8c45fSAndroid Build Coastguard Worker using namespace android::binder::impl;
81*38e8c45fSAndroid Build Coastguard Worker using android::binder::unique_fd;
82*38e8c45fSAndroid Build Coastguard Worker
83*38e8c45fSAndroid Build Coastguard Worker class PoolThread : public Thread
84*38e8c45fSAndroid Build Coastguard Worker {
85*38e8c45fSAndroid Build Coastguard Worker public:
PoolThread(bool isMain)86*38e8c45fSAndroid Build Coastguard Worker explicit PoolThread(bool isMain)
87*38e8c45fSAndroid Build Coastguard Worker : mIsMain(isMain)
88*38e8c45fSAndroid Build Coastguard Worker {
89*38e8c45fSAndroid Build Coastguard Worker }
90*38e8c45fSAndroid Build Coastguard Worker
91*38e8c45fSAndroid Build Coastguard Worker protected:
threadLoop()92*38e8c45fSAndroid Build Coastguard Worker virtual bool threadLoop()
93*38e8c45fSAndroid Build Coastguard Worker {
94*38e8c45fSAndroid Build Coastguard Worker IPCThreadState::self()->joinThreadPool(mIsMain);
95*38e8c45fSAndroid Build Coastguard Worker return false;
96*38e8c45fSAndroid Build Coastguard Worker }
97*38e8c45fSAndroid Build Coastguard Worker
98*38e8c45fSAndroid Build Coastguard Worker const bool mIsMain;
99*38e8c45fSAndroid Build Coastguard Worker };
100*38e8c45fSAndroid Build Coastguard Worker
self()101*38e8c45fSAndroid Build Coastguard Worker sp<ProcessState> ProcessState::self()
102*38e8c45fSAndroid Build Coastguard Worker {
103*38e8c45fSAndroid Build Coastguard Worker return init(kDefaultDriver, false /*requireDefault*/);
104*38e8c45fSAndroid Build Coastguard Worker }
105*38e8c45fSAndroid Build Coastguard Worker
initWithDriver(const char * driver)106*38e8c45fSAndroid Build Coastguard Worker sp<ProcessState> ProcessState::initWithDriver(const char* driver)
107*38e8c45fSAndroid Build Coastguard Worker {
108*38e8c45fSAndroid Build Coastguard Worker return init(driver, true /*requireDefault*/);
109*38e8c45fSAndroid Build Coastguard Worker }
110*38e8c45fSAndroid Build Coastguard Worker
selfOrNull()111*38e8c45fSAndroid Build Coastguard Worker sp<ProcessState> ProcessState::selfOrNull()
112*38e8c45fSAndroid Build Coastguard Worker {
113*38e8c45fSAndroid Build Coastguard Worker return init(nullptr, false /*requireDefault*/);
114*38e8c45fSAndroid Build Coastguard Worker }
115*38e8c45fSAndroid Build Coastguard Worker
116*38e8c45fSAndroid Build Coastguard Worker [[clang::no_destroy]] static sp<ProcessState> gProcess;
117*38e8c45fSAndroid Build Coastguard Worker [[clang::no_destroy]] static std::mutex gProcessMutex;
118*38e8c45fSAndroid Build Coastguard Worker
verifyNotForked(bool forked)119*38e8c45fSAndroid Build Coastguard Worker static void verifyNotForked(bool forked) {
120*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(forked, "libbinder ProcessState can not be used after fork");
121*38e8c45fSAndroid Build Coastguard Worker }
122*38e8c45fSAndroid Build Coastguard Worker
isVndservicemanagerEnabled()123*38e8c45fSAndroid Build Coastguard Worker bool ProcessState::isVndservicemanagerEnabled() {
124*38e8c45fSAndroid Build Coastguard Worker return access("/vendor/bin/vndservicemanager", R_OK) == 0;
125*38e8c45fSAndroid Build Coastguard Worker }
126*38e8c45fSAndroid Build Coastguard Worker
init(const char * driver,bool requireDefault)127*38e8c45fSAndroid Build Coastguard Worker sp<ProcessState> ProcessState::init(const char* driver, bool requireDefault) {
128*38e8c45fSAndroid Build Coastguard Worker if (driver == nullptr) {
129*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> l(gProcessMutex);
130*38e8c45fSAndroid Build Coastguard Worker if (gProcess) {
131*38e8c45fSAndroid Build Coastguard Worker verifyNotForked(gProcess->mForked);
132*38e8c45fSAndroid Build Coastguard Worker }
133*38e8c45fSAndroid Build Coastguard Worker return gProcess;
134*38e8c45fSAndroid Build Coastguard Worker }
135*38e8c45fSAndroid Build Coastguard Worker
136*38e8c45fSAndroid Build Coastguard Worker [[clang::no_destroy]] static std::once_flag gProcessOnce;
137*38e8c45fSAndroid Build Coastguard Worker std::call_once(gProcessOnce, [&](){
138*38e8c45fSAndroid Build Coastguard Worker if (access(driver, R_OK) == -1) {
139*38e8c45fSAndroid Build Coastguard Worker ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver);
140*38e8c45fSAndroid Build Coastguard Worker driver = "/dev/binder";
141*38e8c45fSAndroid Build Coastguard Worker }
142*38e8c45fSAndroid Build Coastguard Worker
143*38e8c45fSAndroid Build Coastguard Worker if (0 == strcmp(driver, "/dev/vndbinder") && !isVndservicemanagerEnabled()) {
144*38e8c45fSAndroid Build Coastguard Worker ALOGE("vndservicemanager is not started on this device, you can save resources/threads "
145*38e8c45fSAndroid Build Coastguard Worker "by not initializing ProcessState with /dev/vndbinder.");
146*38e8c45fSAndroid Build Coastguard Worker }
147*38e8c45fSAndroid Build Coastguard Worker
148*38e8c45fSAndroid Build Coastguard Worker // we must install these before instantiating the gProcess object,
149*38e8c45fSAndroid Build Coastguard Worker // otherwise this would race with creating it, and there could be the
150*38e8c45fSAndroid Build Coastguard Worker // possibility of an invalid gProcess object forked by another thread
151*38e8c45fSAndroid Build Coastguard Worker // before these are installed
152*38e8c45fSAndroid Build Coastguard Worker int ret = pthread_atfork(ProcessState::onFork, ProcessState::parentPostFork,
153*38e8c45fSAndroid Build Coastguard Worker ProcessState::childPostFork);
154*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(ret != 0, "pthread_atfork error %s", strerror(ret));
155*38e8c45fSAndroid Build Coastguard Worker
156*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> l(gProcessMutex);
157*38e8c45fSAndroid Build Coastguard Worker gProcess = sp<ProcessState>::make(driver);
158*38e8c45fSAndroid Build Coastguard Worker });
159*38e8c45fSAndroid Build Coastguard Worker
160*38e8c45fSAndroid Build Coastguard Worker if (requireDefault) {
161*38e8c45fSAndroid Build Coastguard Worker // Detect if we are trying to initialize with a different driver, and
162*38e8c45fSAndroid Build Coastguard Worker // consider that an error. ProcessState will only be initialized once above.
163*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(gProcess->getDriverName() != driver,
164*38e8c45fSAndroid Build Coastguard Worker "ProcessState was already initialized with %s,"
165*38e8c45fSAndroid Build Coastguard Worker " can't initialize with %s.",
166*38e8c45fSAndroid Build Coastguard Worker gProcess->getDriverName().c_str(), driver);
167*38e8c45fSAndroid Build Coastguard Worker }
168*38e8c45fSAndroid Build Coastguard Worker
169*38e8c45fSAndroid Build Coastguard Worker verifyNotForked(gProcess->mForked);
170*38e8c45fSAndroid Build Coastguard Worker return gProcess;
171*38e8c45fSAndroid Build Coastguard Worker }
172*38e8c45fSAndroid Build Coastguard Worker
getContextObject(const sp<IBinder> &)173*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
174*38e8c45fSAndroid Build Coastguard Worker {
175*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> context = getStrongProxyForHandle(0);
176*38e8c45fSAndroid Build Coastguard Worker
177*38e8c45fSAndroid Build Coastguard Worker if (context) {
178*38e8c45fSAndroid Build Coastguard Worker // The root object is special since we get it directly from the driver, it is never
179*38e8c45fSAndroid Build Coastguard Worker // written by Parcell::writeStrongBinder.
180*38e8c45fSAndroid Build Coastguard Worker internal::Stability::markCompilationUnit(context.get());
181*38e8c45fSAndroid Build Coastguard Worker } else {
182*38e8c45fSAndroid Build Coastguard Worker ALOGW("Not able to get context object on %s.", mDriverName.c_str());
183*38e8c45fSAndroid Build Coastguard Worker }
184*38e8c45fSAndroid Build Coastguard Worker
185*38e8c45fSAndroid Build Coastguard Worker return context;
186*38e8c45fSAndroid Build Coastguard Worker }
187*38e8c45fSAndroid Build Coastguard Worker
onFork()188*38e8c45fSAndroid Build Coastguard Worker void ProcessState::onFork() {
189*38e8c45fSAndroid Build Coastguard Worker // make sure another thread isn't currently retrieving ProcessState
190*38e8c45fSAndroid Build Coastguard Worker gProcessMutex.lock();
191*38e8c45fSAndroid Build Coastguard Worker }
192*38e8c45fSAndroid Build Coastguard Worker
parentPostFork()193*38e8c45fSAndroid Build Coastguard Worker void ProcessState::parentPostFork() {
194*38e8c45fSAndroid Build Coastguard Worker gProcessMutex.unlock();
195*38e8c45fSAndroid Build Coastguard Worker }
196*38e8c45fSAndroid Build Coastguard Worker
childPostFork()197*38e8c45fSAndroid Build Coastguard Worker void ProcessState::childPostFork() {
198*38e8c45fSAndroid Build Coastguard Worker // another thread might call fork before gProcess is instantiated, but after
199*38e8c45fSAndroid Build Coastguard Worker // the thread handler is installed
200*38e8c45fSAndroid Build Coastguard Worker if (gProcess) {
201*38e8c45fSAndroid Build Coastguard Worker gProcess->mForked = true;
202*38e8c45fSAndroid Build Coastguard Worker
203*38e8c45fSAndroid Build Coastguard Worker // "O_CLOFORK"
204*38e8c45fSAndroid Build Coastguard Worker close(gProcess->mDriverFD);
205*38e8c45fSAndroid Build Coastguard Worker gProcess->mDriverFD = -1;
206*38e8c45fSAndroid Build Coastguard Worker }
207*38e8c45fSAndroid Build Coastguard Worker gProcessMutex.unlock();
208*38e8c45fSAndroid Build Coastguard Worker }
209*38e8c45fSAndroid Build Coastguard Worker
startThreadPool()210*38e8c45fSAndroid Build Coastguard Worker void ProcessState::startThreadPool()
211*38e8c45fSAndroid Build Coastguard Worker {
212*38e8c45fSAndroid Build Coastguard Worker std::unique_lock<std::mutex> _l(mLock);
213*38e8c45fSAndroid Build Coastguard Worker if (!mThreadPoolStarted) {
214*38e8c45fSAndroid Build Coastguard Worker if (mMaxThreads == 0) {
215*38e8c45fSAndroid Build Coastguard Worker // see also getThreadPoolMaxTotalThreadCount
216*38e8c45fSAndroid Build Coastguard Worker ALOGW("Extra binder thread started, but 0 threads requested. Do not use "
217*38e8c45fSAndroid Build Coastguard Worker "*startThreadPool when zero threads are requested.");
218*38e8c45fSAndroid Build Coastguard Worker }
219*38e8c45fSAndroid Build Coastguard Worker mThreadPoolStarted = true;
220*38e8c45fSAndroid Build Coastguard Worker spawnPooledThread(true);
221*38e8c45fSAndroid Build Coastguard Worker }
222*38e8c45fSAndroid Build Coastguard Worker }
223*38e8c45fSAndroid Build Coastguard Worker
becomeContextManager()224*38e8c45fSAndroid Build Coastguard Worker bool ProcessState::becomeContextManager()
225*38e8c45fSAndroid Build Coastguard Worker {
226*38e8c45fSAndroid Build Coastguard Worker std::unique_lock<std::mutex> _l(mLock);
227*38e8c45fSAndroid Build Coastguard Worker
228*38e8c45fSAndroid Build Coastguard Worker flat_binder_object obj {
229*38e8c45fSAndroid Build Coastguard Worker .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX,
230*38e8c45fSAndroid Build Coastguard Worker };
231*38e8c45fSAndroid Build Coastguard Worker
232*38e8c45fSAndroid Build Coastguard Worker int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj);
233*38e8c45fSAndroid Build Coastguard Worker
234*38e8c45fSAndroid Build Coastguard Worker // fallback to original method
235*38e8c45fSAndroid Build Coastguard Worker if (result != 0) {
236*38e8c45fSAndroid Build Coastguard Worker android_errorWriteLog(0x534e4554, "121035042");
237*38e8c45fSAndroid Build Coastguard Worker
238*38e8c45fSAndroid Build Coastguard Worker int unused = 0;
239*38e8c45fSAndroid Build Coastguard Worker result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused);
240*38e8c45fSAndroid Build Coastguard Worker }
241*38e8c45fSAndroid Build Coastguard Worker
242*38e8c45fSAndroid Build Coastguard Worker if (result == -1) {
243*38e8c45fSAndroid Build Coastguard Worker ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
244*38e8c45fSAndroid Build Coastguard Worker }
245*38e8c45fSAndroid Build Coastguard Worker
246*38e8c45fSAndroid Build Coastguard Worker return result == 0;
247*38e8c45fSAndroid Build Coastguard Worker }
248*38e8c45fSAndroid Build Coastguard Worker
249*38e8c45fSAndroid Build Coastguard Worker // Get references to userspace objects held by the kernel binder driver
250*38e8c45fSAndroid Build Coastguard Worker // Writes up to count elements into buf, and returns the total number
251*38e8c45fSAndroid Build Coastguard Worker // of references the kernel has, which may be larger than count.
252*38e8c45fSAndroid Build Coastguard Worker // buf may be NULL if count is 0. The pointers returned by this method
253*38e8c45fSAndroid Build Coastguard Worker // should only be used for debugging and not dereferenced, they may
254*38e8c45fSAndroid Build Coastguard Worker // already be invalid.
getKernelReferences(size_t buf_count,uintptr_t * buf)255*38e8c45fSAndroid Build Coastguard Worker ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf)
256*38e8c45fSAndroid Build Coastguard Worker {
257*38e8c45fSAndroid Build Coastguard Worker binder_node_debug_info info = {};
258*38e8c45fSAndroid Build Coastguard Worker
259*38e8c45fSAndroid Build Coastguard Worker uintptr_t* end = buf ? buf + buf_count : nullptr;
260*38e8c45fSAndroid Build Coastguard Worker size_t count = 0;
261*38e8c45fSAndroid Build Coastguard Worker
262*38e8c45fSAndroid Build Coastguard Worker do {
263*38e8c45fSAndroid Build Coastguard Worker status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info);
264*38e8c45fSAndroid Build Coastguard Worker if (result < 0) {
265*38e8c45fSAndroid Build Coastguard Worker return -1;
266*38e8c45fSAndroid Build Coastguard Worker }
267*38e8c45fSAndroid Build Coastguard Worker if (info.ptr != 0) {
268*38e8c45fSAndroid Build Coastguard Worker if (buf && buf < end)
269*38e8c45fSAndroid Build Coastguard Worker *buf++ = info.ptr;
270*38e8c45fSAndroid Build Coastguard Worker count++;
271*38e8c45fSAndroid Build Coastguard Worker if (buf && buf < end)
272*38e8c45fSAndroid Build Coastguard Worker *buf++ = info.cookie;
273*38e8c45fSAndroid Build Coastguard Worker count++;
274*38e8c45fSAndroid Build Coastguard Worker }
275*38e8c45fSAndroid Build Coastguard Worker } while (info.ptr != 0);
276*38e8c45fSAndroid Build Coastguard Worker
277*38e8c45fSAndroid Build Coastguard Worker return count;
278*38e8c45fSAndroid Build Coastguard Worker }
279*38e8c45fSAndroid Build Coastguard Worker
280*38e8c45fSAndroid Build Coastguard Worker // Queries the driver for the current strong reference count of the node
281*38e8c45fSAndroid Build Coastguard Worker // that the handle points to. Can only be used by the servicemanager.
282*38e8c45fSAndroid Build Coastguard Worker //
283*38e8c45fSAndroid Build Coastguard Worker // Returns -1 in case of failure, otherwise the strong reference count.
getStrongRefCountForNode(const sp<BpBinder> & binder)284*38e8c45fSAndroid Build Coastguard Worker ssize_t ProcessState::getStrongRefCountForNode(const sp<BpBinder>& binder) {
285*38e8c45fSAndroid Build Coastguard Worker if (binder->isRpcBinder()) return -1;
286*38e8c45fSAndroid Build Coastguard Worker
287*38e8c45fSAndroid Build Coastguard Worker binder_node_info_for_ref info;
288*38e8c45fSAndroid Build Coastguard Worker memset(&info, 0, sizeof(binder_node_info_for_ref));
289*38e8c45fSAndroid Build Coastguard Worker
290*38e8c45fSAndroid Build Coastguard Worker info.handle = binder->getPrivateAccessor().binderHandle();
291*38e8c45fSAndroid Build Coastguard Worker
292*38e8c45fSAndroid Build Coastguard Worker status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info);
293*38e8c45fSAndroid Build Coastguard Worker
294*38e8c45fSAndroid Build Coastguard Worker if (result != OK) {
295*38e8c45fSAndroid Build Coastguard Worker static bool logged = false;
296*38e8c45fSAndroid Build Coastguard Worker if (!logged) {
297*38e8c45fSAndroid Build Coastguard Worker ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF.");
298*38e8c45fSAndroid Build Coastguard Worker logged = true;
299*38e8c45fSAndroid Build Coastguard Worker }
300*38e8c45fSAndroid Build Coastguard Worker return -1;
301*38e8c45fSAndroid Build Coastguard Worker }
302*38e8c45fSAndroid Build Coastguard Worker
303*38e8c45fSAndroid Build Coastguard Worker return info.strong_count;
304*38e8c45fSAndroid Build Coastguard Worker }
305*38e8c45fSAndroid Build Coastguard Worker
setCallRestriction(CallRestriction restriction)306*38e8c45fSAndroid Build Coastguard Worker void ProcessState::setCallRestriction(CallRestriction restriction) {
307*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr,
308*38e8c45fSAndroid Build Coastguard Worker "Call restrictions must be set before the threadpool is started.");
309*38e8c45fSAndroid Build Coastguard Worker
310*38e8c45fSAndroid Build Coastguard Worker mCallRestriction = restriction;
311*38e8c45fSAndroid Build Coastguard Worker }
312*38e8c45fSAndroid Build Coastguard Worker
lookupHandleLocked(int32_t handle)313*38e8c45fSAndroid Build Coastguard Worker ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
314*38e8c45fSAndroid Build Coastguard Worker {
315*38e8c45fSAndroid Build Coastguard Worker const size_t N=mHandleToObject.size();
316*38e8c45fSAndroid Build Coastguard Worker if (N <= (size_t)handle) {
317*38e8c45fSAndroid Build Coastguard Worker handle_entry e;
318*38e8c45fSAndroid Build Coastguard Worker e.binder = nullptr;
319*38e8c45fSAndroid Build Coastguard Worker e.refs = nullptr;
320*38e8c45fSAndroid Build Coastguard Worker status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
321*38e8c45fSAndroid Build Coastguard Worker if (err < NO_ERROR) return nullptr;
322*38e8c45fSAndroid Build Coastguard Worker }
323*38e8c45fSAndroid Build Coastguard Worker return &mHandleToObject.editItemAt(handle);
324*38e8c45fSAndroid Build Coastguard Worker }
325*38e8c45fSAndroid Build Coastguard Worker
326*38e8c45fSAndroid Build Coastguard Worker // see b/166779391: cannot change the VNDK interface, so access like this
327*38e8c45fSAndroid Build Coastguard Worker extern sp<BBinder> the_context_object;
328*38e8c45fSAndroid Build Coastguard Worker
getStrongProxyForHandle(int32_t handle)329*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
330*38e8c45fSAndroid Build Coastguard Worker {
331*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> result;
332*38e8c45fSAndroid Build Coastguard Worker std::function<void()> postTask;
333*38e8c45fSAndroid Build Coastguard Worker
334*38e8c45fSAndroid Build Coastguard Worker std::unique_lock<std::mutex> _l(mLock);
335*38e8c45fSAndroid Build Coastguard Worker
336*38e8c45fSAndroid Build Coastguard Worker if (handle == 0 && the_context_object != nullptr) return the_context_object;
337*38e8c45fSAndroid Build Coastguard Worker
338*38e8c45fSAndroid Build Coastguard Worker handle_entry* e = lookupHandleLocked(handle);
339*38e8c45fSAndroid Build Coastguard Worker
340*38e8c45fSAndroid Build Coastguard Worker if (e != nullptr) {
341*38e8c45fSAndroid Build Coastguard Worker // We need to create a new BpBinder if there isn't currently one, OR we
342*38e8c45fSAndroid Build Coastguard Worker // are unable to acquire a weak reference on this current one. The
343*38e8c45fSAndroid Build Coastguard Worker // attemptIncWeak() is safe because we know the BpBinder destructor will always
344*38e8c45fSAndroid Build Coastguard Worker // call expungeHandle(), which acquires the same lock we are holding now.
345*38e8c45fSAndroid Build Coastguard Worker // We need to do this because there is a race condition between someone
346*38e8c45fSAndroid Build Coastguard Worker // releasing a reference on this BpBinder, and a new reference on its handle
347*38e8c45fSAndroid Build Coastguard Worker // arriving from the driver.
348*38e8c45fSAndroid Build Coastguard Worker IBinder* b = e->binder;
349*38e8c45fSAndroid Build Coastguard Worker if (b == nullptr || !e->refs->attemptIncWeak(this)) {
350*38e8c45fSAndroid Build Coastguard Worker if (handle == 0) {
351*38e8c45fSAndroid Build Coastguard Worker // Special case for context manager...
352*38e8c45fSAndroid Build Coastguard Worker // The context manager is the only object for which we create
353*38e8c45fSAndroid Build Coastguard Worker // a BpBinder proxy without already holding a reference.
354*38e8c45fSAndroid Build Coastguard Worker // Perform a dummy transaction to ensure the context manager
355*38e8c45fSAndroid Build Coastguard Worker // is registered before we create the first local reference
356*38e8c45fSAndroid Build Coastguard Worker // to it (which will occur when creating the BpBinder).
357*38e8c45fSAndroid Build Coastguard Worker // If a local reference is created for the BpBinder when the
358*38e8c45fSAndroid Build Coastguard Worker // context manager is not present, the driver will fail to
359*38e8c45fSAndroid Build Coastguard Worker // provide a reference to the context manager, but the
360*38e8c45fSAndroid Build Coastguard Worker // driver API does not return status.
361*38e8c45fSAndroid Build Coastguard Worker //
362*38e8c45fSAndroid Build Coastguard Worker // Note that this is not race-free if the context manager
363*38e8c45fSAndroid Build Coastguard Worker // dies while this code runs.
364*38e8c45fSAndroid Build Coastguard Worker
365*38e8c45fSAndroid Build Coastguard Worker IPCThreadState* ipc = IPCThreadState::self();
366*38e8c45fSAndroid Build Coastguard Worker
367*38e8c45fSAndroid Build Coastguard Worker CallRestriction originalCallRestriction = ipc->getCallRestriction();
368*38e8c45fSAndroid Build Coastguard Worker ipc->setCallRestriction(CallRestriction::NONE);
369*38e8c45fSAndroid Build Coastguard Worker
370*38e8c45fSAndroid Build Coastguard Worker Parcel data;
371*38e8c45fSAndroid Build Coastguard Worker status_t status = ipc->transact(
372*38e8c45fSAndroid Build Coastguard Worker 0, IBinder::PING_TRANSACTION, data, nullptr, 0);
373*38e8c45fSAndroid Build Coastguard Worker
374*38e8c45fSAndroid Build Coastguard Worker ipc->setCallRestriction(originalCallRestriction);
375*38e8c45fSAndroid Build Coastguard Worker
376*38e8c45fSAndroid Build Coastguard Worker if (status == DEAD_OBJECT)
377*38e8c45fSAndroid Build Coastguard Worker return nullptr;
378*38e8c45fSAndroid Build Coastguard Worker }
379*38e8c45fSAndroid Build Coastguard Worker
380*38e8c45fSAndroid Build Coastguard Worker sp<BpBinder> b = BpBinder::PrivateAccessor::create(handle, &postTask);
381*38e8c45fSAndroid Build Coastguard Worker e->binder = b.get();
382*38e8c45fSAndroid Build Coastguard Worker if (b) e->refs = b->getWeakRefs();
383*38e8c45fSAndroid Build Coastguard Worker result = b;
384*38e8c45fSAndroid Build Coastguard Worker } else {
385*38e8c45fSAndroid Build Coastguard Worker // This little bit of nastyness is to allow us to add a primary
386*38e8c45fSAndroid Build Coastguard Worker // reference to the remote proxy when this team doesn't have one
387*38e8c45fSAndroid Build Coastguard Worker // but another team is sending the handle to us.
388*38e8c45fSAndroid Build Coastguard Worker result.force_set(b);
389*38e8c45fSAndroid Build Coastguard Worker e->refs->decWeak(this);
390*38e8c45fSAndroid Build Coastguard Worker }
391*38e8c45fSAndroid Build Coastguard Worker }
392*38e8c45fSAndroid Build Coastguard Worker
393*38e8c45fSAndroid Build Coastguard Worker _l.unlock();
394*38e8c45fSAndroid Build Coastguard Worker
395*38e8c45fSAndroid Build Coastguard Worker if (postTask) postTask();
396*38e8c45fSAndroid Build Coastguard Worker
397*38e8c45fSAndroid Build Coastguard Worker return result;
398*38e8c45fSAndroid Build Coastguard Worker }
399*38e8c45fSAndroid Build Coastguard Worker
expungeHandle(int32_t handle,IBinder * binder)400*38e8c45fSAndroid Build Coastguard Worker void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
401*38e8c45fSAndroid Build Coastguard Worker {
402*38e8c45fSAndroid Build Coastguard Worker std::unique_lock<std::mutex> _l(mLock);
403*38e8c45fSAndroid Build Coastguard Worker
404*38e8c45fSAndroid Build Coastguard Worker handle_entry* e = lookupHandleLocked(handle);
405*38e8c45fSAndroid Build Coastguard Worker
406*38e8c45fSAndroid Build Coastguard Worker // This handle may have already been replaced with a new BpBinder
407*38e8c45fSAndroid Build Coastguard Worker // (if someone failed the AttemptIncWeak() above); we don't want
408*38e8c45fSAndroid Build Coastguard Worker // to overwrite it.
409*38e8c45fSAndroid Build Coastguard Worker if (e && e->binder == binder) e->binder = nullptr;
410*38e8c45fSAndroid Build Coastguard Worker }
411*38e8c45fSAndroid Build Coastguard Worker
makeBinderThreadName()412*38e8c45fSAndroid Build Coastguard Worker String8 ProcessState::makeBinderThreadName() {
413*38e8c45fSAndroid Build Coastguard Worker int32_t s = mThreadPoolSeq.fetch_add(1, std::memory_order_release);
414*38e8c45fSAndroid Build Coastguard Worker pid_t pid = getpid();
415*38e8c45fSAndroid Build Coastguard Worker
416*38e8c45fSAndroid Build Coastguard Worker std::string_view driverName = mDriverName.c_str();
417*38e8c45fSAndroid Build Coastguard Worker android::base::ConsumePrefix(&driverName, "/dev/");
418*38e8c45fSAndroid Build Coastguard Worker
419*38e8c45fSAndroid Build Coastguard Worker String8 name;
420*38e8c45fSAndroid Build Coastguard Worker name.appendFormat("%.*s:%d_%X", static_cast<int>(driverName.length()), driverName.data(), pid,
421*38e8c45fSAndroid Build Coastguard Worker s);
422*38e8c45fSAndroid Build Coastguard Worker return name;
423*38e8c45fSAndroid Build Coastguard Worker }
424*38e8c45fSAndroid Build Coastguard Worker
spawnPooledThread(bool isMain)425*38e8c45fSAndroid Build Coastguard Worker void ProcessState::spawnPooledThread(bool isMain)
426*38e8c45fSAndroid Build Coastguard Worker {
427*38e8c45fSAndroid Build Coastguard Worker if (mThreadPoolStarted) {
428*38e8c45fSAndroid Build Coastguard Worker String8 name = makeBinderThreadName();
429*38e8c45fSAndroid Build Coastguard Worker ALOGV("Spawning new pooled thread, name=%s\n", name.c_str());
430*38e8c45fSAndroid Build Coastguard Worker sp<Thread> t = sp<PoolThread>::make(isMain);
431*38e8c45fSAndroid Build Coastguard Worker t->run(name.c_str());
432*38e8c45fSAndroid Build Coastguard Worker mKernelStartedThreads++;
433*38e8c45fSAndroid Build Coastguard Worker }
434*38e8c45fSAndroid Build Coastguard Worker // TODO: if startThreadPool is called on another thread after the process
435*38e8c45fSAndroid Build Coastguard Worker // starts up, the kernel might think that it already requested those
436*38e8c45fSAndroid Build Coastguard Worker // binder threads, and additional won't be started. This is likely to
437*38e8c45fSAndroid Build Coastguard Worker // cause deadlocks, and it will also cause getThreadPoolMaxTotalThreadCount
438*38e8c45fSAndroid Build Coastguard Worker // to return too high of a value.
439*38e8c45fSAndroid Build Coastguard Worker }
440*38e8c45fSAndroid Build Coastguard Worker
setThreadPoolMaxThreadCount(size_t maxThreads)441*38e8c45fSAndroid Build Coastguard Worker status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
442*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads,
443*38e8c45fSAndroid Build Coastguard Worker "Binder threadpool cannot be shrunk after starting");
444*38e8c45fSAndroid Build Coastguard Worker status_t result = NO_ERROR;
445*38e8c45fSAndroid Build Coastguard Worker if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
446*38e8c45fSAndroid Build Coastguard Worker mMaxThreads = maxThreads;
447*38e8c45fSAndroid Build Coastguard Worker } else {
448*38e8c45fSAndroid Build Coastguard Worker result = -errno;
449*38e8c45fSAndroid Build Coastguard Worker ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
450*38e8c45fSAndroid Build Coastguard Worker }
451*38e8c45fSAndroid Build Coastguard Worker return result;
452*38e8c45fSAndroid Build Coastguard Worker }
453*38e8c45fSAndroid Build Coastguard Worker
getThreadPoolMaxTotalThreadCount() const454*38e8c45fSAndroid Build Coastguard Worker size_t ProcessState::getThreadPoolMaxTotalThreadCount() const {
455*38e8c45fSAndroid Build Coastguard Worker // Need to read `mKernelStartedThreads` before `mThreadPoolStarted` (with
456*38e8c45fSAndroid Build Coastguard Worker // non-relaxed memory ordering) to avoid a race like the following:
457*38e8c45fSAndroid Build Coastguard Worker //
458*38e8c45fSAndroid Build Coastguard Worker // thread A: if (mThreadPoolStarted) { // evaluates false
459*38e8c45fSAndroid Build Coastguard Worker // thread B: mThreadPoolStarted = true;
460*38e8c45fSAndroid Build Coastguard Worker // thread B: mKernelStartedThreads++;
461*38e8c45fSAndroid Build Coastguard Worker // thread A: size_t kernelStarted = mKernelStartedThreads;
462*38e8c45fSAndroid Build Coastguard Worker // thread A: LOG_ALWAYS_FATAL_IF(kernelStarted != 0, ...);
463*38e8c45fSAndroid Build Coastguard Worker size_t kernelStarted = mKernelStartedThreads;
464*38e8c45fSAndroid Build Coastguard Worker
465*38e8c45fSAndroid Build Coastguard Worker if (mThreadPoolStarted) {
466*38e8c45fSAndroid Build Coastguard Worker size_t max = mMaxThreads;
467*38e8c45fSAndroid Build Coastguard Worker size_t current = mCurrentThreads;
468*38e8c45fSAndroid Build Coastguard Worker
469*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(kernelStarted > max + 1,
470*38e8c45fSAndroid Build Coastguard Worker "too many kernel-started threads: %zu > %zu + 1", kernelStarted, max);
471*38e8c45fSAndroid Build Coastguard Worker
472*38e8c45fSAndroid Build Coastguard Worker // calling startThreadPool starts a thread
473*38e8c45fSAndroid Build Coastguard Worker size_t threads = 1;
474*38e8c45fSAndroid Build Coastguard Worker
475*38e8c45fSAndroid Build Coastguard Worker // the kernel is configured to start up to mMaxThreads more threads
476*38e8c45fSAndroid Build Coastguard Worker threads += max;
477*38e8c45fSAndroid Build Coastguard Worker
478*38e8c45fSAndroid Build Coastguard Worker // Users may call IPCThreadState::joinThreadPool directly. We don't
479*38e8c45fSAndroid Build Coastguard Worker // currently have a way to count this directly (it could be added by
480*38e8c45fSAndroid Build Coastguard Worker // adding a separate private joinKernelThread method in IPCThreadState).
481*38e8c45fSAndroid Build Coastguard Worker // So, if we are in a race between the kernel thread variable being
482*38e8c45fSAndroid Build Coastguard Worker // incremented in this file and mCurrentThreads being incremented
483*38e8c45fSAndroid Build Coastguard Worker // in IPCThreadState, temporarily forget about the extra join threads.
484*38e8c45fSAndroid Build Coastguard Worker // This is okay, because most callers of this method only care about
485*38e8c45fSAndroid Build Coastguard Worker // having 0, 1, or more threads.
486*38e8c45fSAndroid Build Coastguard Worker if (current > kernelStarted) {
487*38e8c45fSAndroid Build Coastguard Worker threads += current - kernelStarted;
488*38e8c45fSAndroid Build Coastguard Worker }
489*38e8c45fSAndroid Build Coastguard Worker
490*38e8c45fSAndroid Build Coastguard Worker return threads;
491*38e8c45fSAndroid Build Coastguard Worker }
492*38e8c45fSAndroid Build Coastguard Worker
493*38e8c45fSAndroid Build Coastguard Worker // must not be initialized or maybe has poll thread setup, we
494*38e8c45fSAndroid Build Coastguard Worker // currently don't track this in libbinder
495*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(kernelStarted != 0, "Expecting 0 kernel started threads but have %zu",
496*38e8c45fSAndroid Build Coastguard Worker kernelStarted);
497*38e8c45fSAndroid Build Coastguard Worker return mCurrentThreads;
498*38e8c45fSAndroid Build Coastguard Worker }
499*38e8c45fSAndroid Build Coastguard Worker
isThreadPoolStarted() const500*38e8c45fSAndroid Build Coastguard Worker bool ProcessState::isThreadPoolStarted() const {
501*38e8c45fSAndroid Build Coastguard Worker return mThreadPoolStarted;
502*38e8c45fSAndroid Build Coastguard Worker }
503*38e8c45fSAndroid Build Coastguard Worker
504*38e8c45fSAndroid Build Coastguard Worker #define DRIVER_FEATURES_PATH "/dev/binderfs/features/"
isDriverFeatureEnabled(const DriverFeature feature)505*38e8c45fSAndroid Build Coastguard Worker bool ProcessState::isDriverFeatureEnabled(const DriverFeature feature) {
506*38e8c45fSAndroid Build Coastguard Worker // Use static variable to cache the results.
507*38e8c45fSAndroid Build Coastguard Worker if (feature == DriverFeature::ONEWAY_SPAM_DETECTION) {
508*38e8c45fSAndroid Build Coastguard Worker static bool enabled = readDriverFeatureFile(DRIVER_FEATURES_PATH "oneway_spam_detection");
509*38e8c45fSAndroid Build Coastguard Worker return enabled;
510*38e8c45fSAndroid Build Coastguard Worker }
511*38e8c45fSAndroid Build Coastguard Worker if (feature == DriverFeature::EXTENDED_ERROR) {
512*38e8c45fSAndroid Build Coastguard Worker static bool enabled = readDriverFeatureFile(DRIVER_FEATURES_PATH "extended_error");
513*38e8c45fSAndroid Build Coastguard Worker return enabled;
514*38e8c45fSAndroid Build Coastguard Worker }
515*38e8c45fSAndroid Build Coastguard Worker if (feature == DriverFeature::FREEZE_NOTIFICATION) {
516*38e8c45fSAndroid Build Coastguard Worker static bool enabled = readDriverFeatureFile(DRIVER_FEATURES_PATH "freeze_notification");
517*38e8c45fSAndroid Build Coastguard Worker return enabled;
518*38e8c45fSAndroid Build Coastguard Worker }
519*38e8c45fSAndroid Build Coastguard Worker return false;
520*38e8c45fSAndroid Build Coastguard Worker }
521*38e8c45fSAndroid Build Coastguard Worker
enableOnewaySpamDetection(bool enable)522*38e8c45fSAndroid Build Coastguard Worker status_t ProcessState::enableOnewaySpamDetection(bool enable) {
523*38e8c45fSAndroid Build Coastguard Worker uint32_t enableDetection = enable ? 1 : 0;
524*38e8c45fSAndroid Build Coastguard Worker if (ioctl(mDriverFD, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enableDetection) == -1) {
525*38e8c45fSAndroid Build Coastguard Worker ALOGI("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
526*38e8c45fSAndroid Build Coastguard Worker return -errno;
527*38e8c45fSAndroid Build Coastguard Worker }
528*38e8c45fSAndroid Build Coastguard Worker return NO_ERROR;
529*38e8c45fSAndroid Build Coastguard Worker }
530*38e8c45fSAndroid Build Coastguard Worker
giveThreadPoolName()531*38e8c45fSAndroid Build Coastguard Worker void ProcessState::giveThreadPoolName() {
532*38e8c45fSAndroid Build Coastguard Worker androidSetThreadName(makeBinderThreadName().c_str());
533*38e8c45fSAndroid Build Coastguard Worker }
534*38e8c45fSAndroid Build Coastguard Worker
getDriverName()535*38e8c45fSAndroid Build Coastguard Worker String8 ProcessState::getDriverName() {
536*38e8c45fSAndroid Build Coastguard Worker return mDriverName;
537*38e8c45fSAndroid Build Coastguard Worker }
538*38e8c45fSAndroid Build Coastguard Worker
open_driver(const char * driver,String8 * error)539*38e8c45fSAndroid Build Coastguard Worker static unique_fd open_driver(const char* driver, String8* error) {
540*38e8c45fSAndroid Build Coastguard Worker auto fd = unique_fd(open(driver, O_RDWR | O_CLOEXEC));
541*38e8c45fSAndroid Build Coastguard Worker if (!fd.ok()) {
542*38e8c45fSAndroid Build Coastguard Worker error->appendFormat("%d (%s) Opening '%s' failed", errno, strerror(errno), driver);
543*38e8c45fSAndroid Build Coastguard Worker return {};
544*38e8c45fSAndroid Build Coastguard Worker }
545*38e8c45fSAndroid Build Coastguard Worker int vers = 0;
546*38e8c45fSAndroid Build Coastguard Worker int result = ioctl(fd.get(), BINDER_VERSION, &vers);
547*38e8c45fSAndroid Build Coastguard Worker if (result == -1) {
548*38e8c45fSAndroid Build Coastguard Worker error->appendFormat("%d (%s) Binder ioctl to obtain version failed", errno,
549*38e8c45fSAndroid Build Coastguard Worker strerror(errno));
550*38e8c45fSAndroid Build Coastguard Worker return {};
551*38e8c45fSAndroid Build Coastguard Worker }
552*38e8c45fSAndroid Build Coastguard Worker if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
553*38e8c45fSAndroid Build Coastguard Worker error->appendFormat("Binder driver protocol(%d) does not match user space protocol(%d)! "
554*38e8c45fSAndroid Build Coastguard Worker "ioctl() return value: %d",
555*38e8c45fSAndroid Build Coastguard Worker vers, BINDER_CURRENT_PROTOCOL_VERSION, result);
556*38e8c45fSAndroid Build Coastguard Worker return {};
557*38e8c45fSAndroid Build Coastguard Worker }
558*38e8c45fSAndroid Build Coastguard Worker size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
559*38e8c45fSAndroid Build Coastguard Worker result = ioctl(fd.get(), BINDER_SET_MAX_THREADS, &maxThreads);
560*38e8c45fSAndroid Build Coastguard Worker if (result == -1) {
561*38e8c45fSAndroid Build Coastguard Worker ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
562*38e8c45fSAndroid Build Coastguard Worker }
563*38e8c45fSAndroid Build Coastguard Worker uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION;
564*38e8c45fSAndroid Build Coastguard Worker result = ioctl(fd.get(), BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable);
565*38e8c45fSAndroid Build Coastguard Worker if (result == -1) {
566*38e8c45fSAndroid Build Coastguard Worker ALOGE_IF(ProcessState::isDriverFeatureEnabled(
567*38e8c45fSAndroid Build Coastguard Worker ProcessState::DriverFeature::ONEWAY_SPAM_DETECTION),
568*38e8c45fSAndroid Build Coastguard Worker "Binder ioctl to enable oneway spam detection failed: %s", strerror(errno));
569*38e8c45fSAndroid Build Coastguard Worker }
570*38e8c45fSAndroid Build Coastguard Worker return fd;
571*38e8c45fSAndroid Build Coastguard Worker }
572*38e8c45fSAndroid Build Coastguard Worker
ProcessState(const char * driver)573*38e8c45fSAndroid Build Coastguard Worker ProcessState::ProcessState(const char* driver)
574*38e8c45fSAndroid Build Coastguard Worker : mDriverName(String8(driver)),
575*38e8c45fSAndroid Build Coastguard Worker mDriverFD(-1),
576*38e8c45fSAndroid Build Coastguard Worker mVMStart(MAP_FAILED),
577*38e8c45fSAndroid Build Coastguard Worker mExecutingThreadsCount(0),
578*38e8c45fSAndroid Build Coastguard Worker mMaxThreads(DEFAULT_MAX_BINDER_THREADS),
579*38e8c45fSAndroid Build Coastguard Worker mCurrentThreads(0),
580*38e8c45fSAndroid Build Coastguard Worker mKernelStartedThreads(0),
581*38e8c45fSAndroid Build Coastguard Worker mStarvationStartTime(never()),
582*38e8c45fSAndroid Build Coastguard Worker mForked(false),
583*38e8c45fSAndroid Build Coastguard Worker mThreadPoolStarted(false),
584*38e8c45fSAndroid Build Coastguard Worker mThreadPoolSeq(1),
585*38e8c45fSAndroid Build Coastguard Worker mCallRestriction(CallRestriction::NONE) {
586*38e8c45fSAndroid Build Coastguard Worker String8 error;
587*38e8c45fSAndroid Build Coastguard Worker unique_fd opened = open_driver(driver, &error);
588*38e8c45fSAndroid Build Coastguard Worker
589*38e8c45fSAndroid Build Coastguard Worker if (opened.ok()) {
590*38e8c45fSAndroid Build Coastguard Worker // mmap the binder, providing a chunk of virtual address space to receive transactions.
591*38e8c45fSAndroid Build Coastguard Worker mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE,
592*38e8c45fSAndroid Build Coastguard Worker opened.get(), 0);
593*38e8c45fSAndroid Build Coastguard Worker if (mVMStart == MAP_FAILED) {
594*38e8c45fSAndroid Build Coastguard Worker // *sigh*
595*38e8c45fSAndroid Build Coastguard Worker ALOGE("Using %s failed: unable to mmap transaction memory.", driver);
596*38e8c45fSAndroid Build Coastguard Worker opened.reset();
597*38e8c45fSAndroid Build Coastguard Worker mDriverName.clear();
598*38e8c45fSAndroid Build Coastguard Worker }
599*38e8c45fSAndroid Build Coastguard Worker }
600*38e8c45fSAndroid Build Coastguard Worker
601*38e8c45fSAndroid Build Coastguard Worker #ifdef __ANDROID__
602*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(!opened.ok(),
603*38e8c45fSAndroid Build Coastguard Worker "Binder driver '%s' could not be opened. Error: %s. Terminating.",
604*38e8c45fSAndroid Build Coastguard Worker driver, error.c_str());
605*38e8c45fSAndroid Build Coastguard Worker #endif
606*38e8c45fSAndroid Build Coastguard Worker
607*38e8c45fSAndroid Build Coastguard Worker if (opened.ok()) {
608*38e8c45fSAndroid Build Coastguard Worker mDriverFD = opened.release();
609*38e8c45fSAndroid Build Coastguard Worker }
610*38e8c45fSAndroid Build Coastguard Worker }
611*38e8c45fSAndroid Build Coastguard Worker
~ProcessState()612*38e8c45fSAndroid Build Coastguard Worker ProcessState::~ProcessState()
613*38e8c45fSAndroid Build Coastguard Worker {
614*38e8c45fSAndroid Build Coastguard Worker if (mDriverFD >= 0) {
615*38e8c45fSAndroid Build Coastguard Worker if (mVMStart != MAP_FAILED) {
616*38e8c45fSAndroid Build Coastguard Worker munmap(mVMStart, BINDER_VM_SIZE);
617*38e8c45fSAndroid Build Coastguard Worker }
618*38e8c45fSAndroid Build Coastguard Worker close(mDriverFD);
619*38e8c45fSAndroid Build Coastguard Worker }
620*38e8c45fSAndroid Build Coastguard Worker mDriverFD = -1;
621*38e8c45fSAndroid Build Coastguard Worker }
622*38e8c45fSAndroid Build Coastguard Worker
623*38e8c45fSAndroid Build Coastguard Worker } // namespace android
624