1 /*
2  * Copyright (C) 2020 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 "DumpstateGrpcServer.h"
18 #include "ServiceSupplier.h"
19 #include "config/XmlServiceSupplier.h"
20 
21 #include <getopt.h>
22 
23 #include <iostream>
24 #include <string>
25 
26 static ServiceDescriptor kDmesgService("dmesg", "/bin/dmesg -kuPT");
27 
SystemdService(const std::string & name)28 static ServiceDescriptor SystemdService(const std::string& name) {
29     return ServiceDescriptor{name, std::string("/bin/journalctl --no-pager -t ") + name};
30 }
31 
32 // clang-format off
33 static const std::vector<ServiceDescriptor> kAvailableServices {
34         SystemdService("coqos-virtio-blk"),
35         SystemdService("coqos-virtio-net"),
36         SystemdService("coqos-virtio-video"),
37         SystemdService("coqos-virtio-console"),
38         SystemdService("coqos-virtio-rng"),
39         SystemdService("coqos-virtio-vsock"),
40         SystemdService("coqos-virtio-gpu-virgl"),
41         SystemdService("coqos-virtio-scmi"),
42         SystemdService("coqos-virtio-input"),
43         SystemdService("coqos-virtio-snd"),
44         SystemdService("dumpstate_grpc_server"),
45         SystemdService("systemd"),
46         SystemdService("vehicle_hal_grpc_server"),
47 };
48 // clang-format on
49 
50 // TODO(egranata): this is a default configuration that we can remove once we land the proper BSP
51 class CoqosLvSystemdServices : public ServiceSupplier {
52   public:
GetSystemLogsService() const53     std::optional<ServiceDescriptor> GetSystemLogsService() const override { return kDmesgService; }
54 
GetServices() const55     std::vector<ServiceDescriptor> GetServices() const override { return kAvailableServices; }
56 };
57 
58 static constexpr auto SERVER_CONFIG_FILE = "/etc/aaos.dumpstate.xml";
59 
main(int argc,char ** argv)60 int main(int argc, char** argv) {
61     // TODO(egranata): move address info to config file?
62     std::string serverAddr;
63     std::string serverConfig = SERVER_CONFIG_FILE;
64 
65     // unique values to identify the options
66     constexpr int OPT_SERVER_ADDR = 1001;
67     constexpr int OPT_CONFIG_FILE = 1002;
68 
69     struct option longOptions[] = {
70             {"server_addr", 1, 0, OPT_SERVER_ADDR},
71             {"config_file", 1, 0, OPT_CONFIG_FILE},
72             {},
73     };
74 
75     int optValue;
76     while ((optValue = getopt_long_only(argc, argv, ":", longOptions, 0)) != -1) {
77         switch (optValue) {
78             case OPT_SERVER_ADDR:
79                 serverAddr = optarg;
80                 break;
81             case OPT_CONFIG_FILE:
82                 serverConfig = optarg;
83                 break;
84             default:
85                 // ignore other options
86                 break;
87         }
88     }
89 
90     if (serverAddr.empty()) {
91         std::cerr << "Dumpstate server addreess is missing" << std::endl;
92         return 1;
93     } else {
94         std::cerr << "Dumpstate server addreess: " << serverAddr << std::endl
95                   << "Dumpstate server config: " << serverConfig << std::endl;
96     }
97 
98     std::unique_ptr<DumpstateGrpcServer> server;
99     if (auto xmlServices = XmlServiceSupplier::fromFile(serverConfig)) {
100         server.reset(new DumpstateGrpcServer{serverAddr, *xmlServices});
101     } else {
102         server.reset(new DumpstateGrpcServer{serverAddr, CoqosLvSystemdServices()});
103         std::cerr << "Server configuration not found; defaulting to built-in configuration which"
104                   << " may not work for all environments" << std::endl;
105     }
106     server->Start();
107 
108     return 0;
109 }
110