1 /*
2  * Copyright (C) 2019 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 #define LOG_TAG "android.hardware.gatekeeper-service.remote"
17 
18 #include <android-base/logging.h>
19 #include <android/binder_manager.h>
20 #include <android/binder_process.h>
21 #include <cutils/properties.h>
22 #include <gflags/gflags.h>
23 
24 #include "common/libs/fs/shared_fd.h"
25 #include "common/libs/security/gatekeeper_channel.h"
26 #include "guest/hals/gatekeeper/remote/remote_gatekeeper.h"
27 #include "remote_gatekeeper.h"
28 
29 using aidl::android::hardware::gatekeeper::RemoteGateKeeperDevice;
30 
31 const char device[] = "/dev/hvc4";
32 
main(int argc,char ** argv)33 int main(int argc, char** argv) {
34     ::android::base::InitLogging(argv, ::android::base::KernelLogger);
35     gflags::ParseCommandLineFlags(&argc, &argv, true);
36     ABinderProcess_setThreadPoolMaxThreadCount(0);
37 
38     auto fd = cuttlefish::SharedFD::Open(device, O_RDWR);
39     if (!fd->IsOpen()) {
40         LOG(FATAL) << "Could not connect to gatekeeper: " << fd->StrError();
41     }
42 
43     if (fd->SetTerminalRaw() < 0) {
44         LOG(FATAL) << "Could not make " << device << " a raw terminal: " << fd->StrError();
45     }
46 
47     cuttlefish::SharedFdGatekeeperChannel gatekeeperChannel(fd, fd);
48 
49     std::shared_ptr<RemoteGateKeeperDevice> gatekeeper =
50         ndk::SharedRefBase::make<RemoteGateKeeperDevice>(&gatekeeperChannel);
51 
52     const std::string instance = std::string() + RemoteGateKeeperDevice::descriptor + "/default";
53     binder_status_t status =
54         AServiceManager_addService(gatekeeper->asBinder().get(), instance.c_str());
55     CHECK_EQ(status, STATUS_OK);
56 
57     ABinderProcess_joinThreadPool();
58     return -1;  // Should never get here.
59 }
60