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 
17 #include <getopt.h>
18 
19 #include <iostream>
20 #include <string>
21 
22 #include <android-base/logging.h>
23 #include <binder/BinderService.h>
24 #include <binder/IPCThreadState.h>
25 #include <binder/ProcessState.h>
26 #include <libgsi/libgsi.h>
27 #include <libgsi/libgsid.h>
28 
29 #include "gsi_service.h"
30 
31 using android::ProcessState;
32 using android::sp;
33 using namespace std::literals;
34 
35 static int DumpDeviceMapper() {
36     auto service = android::gsi::GetGsiService();
37     if (!service) {
38         std::cerr << "Could not start IGsiService.\n";
39         return 1;
40     }
41     std::string output;
42     auto status = service->dumpDeviceMapperDevices(&output);
43     if (!status.isOk()) {
44         std::cerr << "Could not dump device-mapper devices: " << status.exceptionMessage().c_str()
45                   << "\n";
46         return 1;
47     }
48     std::cout << output;
49     return 0;
50 }
51 
52 int main(int argc, char** argv) {
53     android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
54 
55     if (argc > 1) {
56         if (argv[1] == "run-startup-tasks"s) {
57             android::gsi::GsiService::RunStartupTasks();
58             exit(0);
59         } else if (argv[1] == "dump-device-mapper"s) {
60             int rc = DumpDeviceMapper();
61             exit(rc);
62         }
63     }
64 
65     android::gsi::GsiService::Register();
66     {
67         sp<ProcessState> ps(ProcessState::self());
68         ps->startThreadPool();
69         ps->giveThreadPoolName();
70     }
71     android::IPCThreadState::self()->joinThreadPool();
72 
73     exit(0);
74 }
75