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 <iostream>
18 #include <string>
19 
20 #include <android-base/logging.h>
21 #include <binder/BinderService.h>
22 #include <binder/IPCThreadState.h>
23 #include <binder/ProcessState.h>
24 #include <libgsi/libgsid.h>
25 
26 #include "gsi_service.h"
27 
28 using android::ProcessState;
29 using android::sp;
30 using namespace std::literals;
31 
DumpDeviceMapper()32 static int DumpDeviceMapper() {
33     auto service = android::gsi::GetGsiService();
34     if (!service) {
35         std::cerr << "Could not start IGsiService.\n";
36         return 1;
37     }
38     std::string output;
39     auto status = service->dumpDeviceMapperDevices(&output);
40     if (!status.isOk()) {
41         std::cerr << "Could not dump device-mapper devices: " << status.exceptionMessage().c_str()
42                   << "\n";
43         return 1;
44     }
45     std::cout << output;
46     return 0;
47 }
48 
main(int argc,char ** argv)49 int main(int argc, char** argv) {
50     android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
51 
52     // Create globally readable files.
53     umask(0022);
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