xref: /aosp_15_r20/system/gsid/daemon.cpp (revision 4e2b41f188908a2ae9d9a2089f1f10779d080021)
1*4e2b41f1SAndroid Build Coastguard Worker //
2*4e2b41f1SAndroid Build Coastguard Worker // Copyright (C) 2019 The Android Open Source Project
3*4e2b41f1SAndroid Build Coastguard Worker //
4*4e2b41f1SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*4e2b41f1SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*4e2b41f1SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*4e2b41f1SAndroid Build Coastguard Worker //
8*4e2b41f1SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*4e2b41f1SAndroid Build Coastguard Worker //
10*4e2b41f1SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*4e2b41f1SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*4e2b41f1SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4e2b41f1SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*4e2b41f1SAndroid Build Coastguard Worker // limitations under the License.
15*4e2b41f1SAndroid Build Coastguard Worker //
16*4e2b41f1SAndroid Build Coastguard Worker 
17*4e2b41f1SAndroid Build Coastguard Worker #include <iostream>
18*4e2b41f1SAndroid Build Coastguard Worker #include <string>
19*4e2b41f1SAndroid Build Coastguard Worker 
20*4e2b41f1SAndroid Build Coastguard Worker #include <android-base/logging.h>
21*4e2b41f1SAndroid Build Coastguard Worker #include <binder/BinderService.h>
22*4e2b41f1SAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
23*4e2b41f1SAndroid Build Coastguard Worker #include <binder/ProcessState.h>
24*4e2b41f1SAndroid Build Coastguard Worker #include <libgsi/libgsid.h>
25*4e2b41f1SAndroid Build Coastguard Worker 
26*4e2b41f1SAndroid Build Coastguard Worker #include "gsi_service.h"
27*4e2b41f1SAndroid Build Coastguard Worker 
28*4e2b41f1SAndroid Build Coastguard Worker using android::ProcessState;
29*4e2b41f1SAndroid Build Coastguard Worker using android::sp;
30*4e2b41f1SAndroid Build Coastguard Worker using namespace std::literals;
31*4e2b41f1SAndroid Build Coastguard Worker 
DumpDeviceMapper()32*4e2b41f1SAndroid Build Coastguard Worker static int DumpDeviceMapper() {
33*4e2b41f1SAndroid Build Coastguard Worker     auto service = android::gsi::GetGsiService();
34*4e2b41f1SAndroid Build Coastguard Worker     if (!service) {
35*4e2b41f1SAndroid Build Coastguard Worker         std::cerr << "Could not start IGsiService.\n";
36*4e2b41f1SAndroid Build Coastguard Worker         return 1;
37*4e2b41f1SAndroid Build Coastguard Worker     }
38*4e2b41f1SAndroid Build Coastguard Worker     std::string output;
39*4e2b41f1SAndroid Build Coastguard Worker     auto status = service->dumpDeviceMapperDevices(&output);
40*4e2b41f1SAndroid Build Coastguard Worker     if (!status.isOk()) {
41*4e2b41f1SAndroid Build Coastguard Worker         std::cerr << "Could not dump device-mapper devices: " << status.exceptionMessage().c_str()
42*4e2b41f1SAndroid Build Coastguard Worker                   << "\n";
43*4e2b41f1SAndroid Build Coastguard Worker         return 1;
44*4e2b41f1SAndroid Build Coastguard Worker     }
45*4e2b41f1SAndroid Build Coastguard Worker     std::cout << output;
46*4e2b41f1SAndroid Build Coastguard Worker     return 0;
47*4e2b41f1SAndroid Build Coastguard Worker }
48*4e2b41f1SAndroid Build Coastguard Worker 
main(int argc,char ** argv)49*4e2b41f1SAndroid Build Coastguard Worker int main(int argc, char** argv) {
50*4e2b41f1SAndroid Build Coastguard Worker     android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
51*4e2b41f1SAndroid Build Coastguard Worker 
52*4e2b41f1SAndroid Build Coastguard Worker     // Create globally readable files.
53*4e2b41f1SAndroid Build Coastguard Worker     umask(0022);
54*4e2b41f1SAndroid Build Coastguard Worker 
55*4e2b41f1SAndroid Build Coastguard Worker     if (argc > 1) {
56*4e2b41f1SAndroid Build Coastguard Worker         if (argv[1] == "run-startup-tasks"s) {
57*4e2b41f1SAndroid Build Coastguard Worker             android::gsi::GsiService::RunStartupTasks();
58*4e2b41f1SAndroid Build Coastguard Worker             exit(0);
59*4e2b41f1SAndroid Build Coastguard Worker         } else if (argv[1] == "dump-device-mapper"s) {
60*4e2b41f1SAndroid Build Coastguard Worker             int rc = DumpDeviceMapper();
61*4e2b41f1SAndroid Build Coastguard Worker             exit(rc);
62*4e2b41f1SAndroid Build Coastguard Worker         } else if (argv[1] == "verify-image-maps"s) {
63*4e2b41f1SAndroid Build Coastguard Worker             android::gsi::GsiService::VerifyImageMaps();
64*4e2b41f1SAndroid Build Coastguard Worker             exit(0);
65*4e2b41f1SAndroid Build Coastguard Worker         }
66*4e2b41f1SAndroid Build Coastguard Worker     }
67*4e2b41f1SAndroid Build Coastguard Worker 
68*4e2b41f1SAndroid Build Coastguard Worker     android::gsi::GsiService::Register(android::gsi::kGsiServiceName);
69*4e2b41f1SAndroid Build Coastguard Worker     {
70*4e2b41f1SAndroid Build Coastguard Worker         sp<ProcessState> ps(ProcessState::self());
71*4e2b41f1SAndroid Build Coastguard Worker         ps->startThreadPool();
72*4e2b41f1SAndroid Build Coastguard Worker         ps->giveThreadPoolName();
73*4e2b41f1SAndroid Build Coastguard Worker     }
74*4e2b41f1SAndroid Build Coastguard Worker     android::IPCThreadState::self()->joinThreadPool();
75*4e2b41f1SAndroid Build Coastguard Worker 
76*4e2b41f1SAndroid Build Coastguard Worker     exit(0);
77*4e2b41f1SAndroid Build Coastguard Worker }
78