xref: /aosp_15_r20/external/libbrillo/brillo/binder_watcher.cc (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li /*
2*1a96fba6SXin Li  * Copyright (C) 2015 The Android Open Source Project
3*1a96fba6SXin Li  *
4*1a96fba6SXin Li  * Licensed under the Apache License, Version 2.0 (the "License");
5*1a96fba6SXin Li  * you may not use this file except in compliance with the License.
6*1a96fba6SXin Li  * You may obtain a copy of the License at
7*1a96fba6SXin Li  *
8*1a96fba6SXin Li  *      http://www.apache.org/licenses/LICENSE-2.0
9*1a96fba6SXin Li  *
10*1a96fba6SXin Li  * Unless required by applicable law or agreed to in writing, software
11*1a96fba6SXin Li  * distributed under the License is distributed on an "AS IS" BASIS,
12*1a96fba6SXin Li  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*1a96fba6SXin Li  * See the License for the specific language governing permissions and
14*1a96fba6SXin Li  * limitations under the License.
15*1a96fba6SXin Li  */
16*1a96fba6SXin Li 
17*1a96fba6SXin Li #include <brillo/binder_watcher.h>
18*1a96fba6SXin Li 
19*1a96fba6SXin Li #include <base/bind.h>
20*1a96fba6SXin Li #include <base/logging.h>
21*1a96fba6SXin Li #include <binder/IPCThreadState.h>
22*1a96fba6SXin Li #include <binder/ProcessState.h>
23*1a96fba6SXin Li 
24*1a96fba6SXin Li using android::IPCThreadState;
25*1a96fba6SXin Li using android::ProcessState;
26*1a96fba6SXin Li 
27*1a96fba6SXin Li namespace {
28*1a96fba6SXin Li // Called from the message loop whenever the binder file descriptor is ready.
OnBinderReadReady()29*1a96fba6SXin Li void OnBinderReadReady() {
30*1a96fba6SXin Li   IPCThreadState::self()->handlePolledCommands();
31*1a96fba6SXin Li }
32*1a96fba6SXin Li }  // namespace
33*1a96fba6SXin Li 
34*1a96fba6SXin Li namespace brillo {
35*1a96fba6SXin Li 
36*1a96fba6SXin Li BinderWatcher::BinderWatcher() = default;
37*1a96fba6SXin Li 
38*1a96fba6SXin Li BinderWatcher::~BinderWatcher() = default;
39*1a96fba6SXin Li 
Init()40*1a96fba6SXin Li bool BinderWatcher::Init() {
41*1a96fba6SXin Li   int binder_fd = -1;
42*1a96fba6SXin Li   ProcessState::self()->setThreadPoolMaxThreadCount(0);
43*1a96fba6SXin Li   IPCThreadState::self()->disableBackgroundScheduling(true);
44*1a96fba6SXin Li   int err = IPCThreadState::self()->setupPolling(&binder_fd);
45*1a96fba6SXin Li   if (err != 0) {
46*1a96fba6SXin Li     LOG(ERROR) << "Error setting up binder polling: "
47*1a96fba6SXin Li                << logging::SystemErrorCodeToString(err);
48*1a96fba6SXin Li     return false;
49*1a96fba6SXin Li   }
50*1a96fba6SXin Li   if (binder_fd < 0) {
51*1a96fba6SXin Li     LOG(ERROR) << "Invalid binder FD " << binder_fd;
52*1a96fba6SXin Li     return false;
53*1a96fba6SXin Li   }
54*1a96fba6SXin Li   VLOG(1) << "Got binder FD " << binder_fd;
55*1a96fba6SXin Li 
56*1a96fba6SXin Li   watcher_ = base::FileDescriptorWatcher::WatchReadable(
57*1a96fba6SXin Li       binder_fd,
58*1a96fba6SXin Li       base::BindRepeating(&OnBinderReadReady));
59*1a96fba6SXin Li   if (!watcher_) {
60*1a96fba6SXin Li     LOG(ERROR) << "Failed to watch binder FD";
61*1a96fba6SXin Li     return false;
62*1a96fba6SXin Li   }
63*1a96fba6SXin Li   return true;
64*1a96fba6SXin Li }
65*1a96fba6SXin Li 
66*1a96fba6SXin Li }  // namespace brillo
67