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.trusty"
17
18 #include <android-base/logging.h>
19 #include <android/binder_manager.h>
20 #include <android/binder_process.h>
21 #include <getopt.h>
22
23 #include "trusty_gatekeeper.h"
24 #include "trusty_gatekeeper_ipc.h"
25
26 using aidl::android::hardware::gatekeeper::TrustyGateKeeperDevice;
27
28 static const char* _sopts = "hD:";
29 static const struct option _lopts[] = {
30 {"help", no_argument, 0, 'h'},
31 {"dev", required_argument, 0, 'D'},
32 {0, 0, 0, 0},
33 };
34
35 static const char* usage =
36 "Usage: %s [options]\n"
37 "\n"
38 "options:\n"
39 " -h, --help prints this message and exit\n"
40 " -D, --dev name Trusty device name\n"
41 "\n";
42
43 static const char* usage_long = "\n";
44
print_usage_and_exit(const char * prog,int code,bool verbose)45 static void print_usage_and_exit(const char* prog, int code, bool verbose) {
46 fprintf(stderr, usage, prog);
47 if (verbose) {
48 fprintf(stderr, "%s", usage_long);
49 }
50 exit(code);
51 }
52
parse_options(int argc,char ** argv)53 static void parse_options(int argc, char** argv) {
54 int c;
55 int oidx = 0;
56
57 while (1) {
58 c = getopt_long(argc, argv, _sopts, _lopts, &oidx);
59 if (c == -1) {
60 break; /* done */
61 }
62
63 switch (c) {
64 case 'D':
65 trusty_gatekeeper_set_dev_name(optarg);
66 break;
67
68 case 'h':
69 print_usage_and_exit(argv[0], EXIT_SUCCESS, true);
70 break;
71
72 default:
73 print_usage_and_exit(argv[0], EXIT_FAILURE, false);
74 }
75 }
76 }
77
main(int argc,char ** argv)78 int main(int argc, char** argv) {
79 parse_options(argc, argv);
80
81 ABinderProcess_setThreadPoolMaxThreadCount(0);
82
83 std::shared_ptr<TrustyGateKeeperDevice> gatekeeper =
84 ndk::SharedRefBase::make<TrustyGateKeeperDevice>();
85
86 const std::string instance = std::string() + TrustyGateKeeperDevice::descriptor + "/default";
87 binder_status_t status =
88 AServiceManager_addService(gatekeeper->asBinder().get(), instance.c_str());
89 CHECK_EQ(status, STATUS_OK);
90
91 ABinderProcess_joinThreadPool();
92
93 return -1; // Should never get here.
94 }
95