1 /*
2  * Copyright (C) 2018 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 <inttypes.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <signal.h>
28 
29 #include <algorithm>
30 #include <cstdlib>
31 #include <fstream>
32 #include <iomanip>
33 #include <memory>
34 #include <sstream>
35 #include <string>
36 #include <vector>
37 
38 #include <gflags/gflags.h>
39 #include <android-base/logging.h>
40 
41 #include "common/libs/fs/shared_fd.h"
42 #include "common/libs/fs/shared_select.h"
43 #include "common/libs/utils/environment.h"
44 #include "host/commands/run_cvd/runner_defs.h"
45 #include "host/libs/config/cuttlefish_config.h"
46 #include "host/libs/vm_manager/vm_manager.h"
47 
48 DEFINE_int32(instance_num, cuttlefish::GetInstance(),
49              "Which instance to powerwash");
50 
51 DEFINE_int32(wait_for_launcher, 5,
52              "How many seconds to wait for the launcher to respond to the status "
53              "command. A value of zero means wait indefinetly");
54 
main(int argc,char ** argv)55 int main(int argc, char** argv) {
56   ::android::base::InitLogging(argv, android::base::StderrLogger);
57   google::ParseCommandLineFlags(&argc, &argv, true);
58 
59   auto config = cuttlefish::CuttlefishConfig::Get();
60   if (!config) {
61     LOG(ERROR) << "Failed to obtain config object";
62     return 1;
63   }
64 
65   auto instance = config->ForInstance(FLAGS_instance_num);
66   auto monitor_path = instance.launcher_monitor_socket_path();
67   if (monitor_path.empty()) {
68     LOG(ERROR) << "No path to launcher monitor found";
69     return 2;
70   }
71   auto monitor_socket = cuttlefish::SharedFD::SocketLocalClient(monitor_path.c_str(),
72                                                          false, SOCK_STREAM);
73   if (!monitor_socket->IsOpen()) {
74     LOG(ERROR) << "Unable to connect to launcher monitor at " << monitor_path
75                << ": " << monitor_socket->StrError();
76     return 3;
77   }
78   auto request = cuttlefish::LauncherAction::kPowerwash;
79   auto bytes_sent = monitor_socket->Send(&request, sizeof(request), 0);
80   if (bytes_sent < 0) {
81     LOG(ERROR) << "Error sending launcher monitor the status command: "
82                << monitor_socket->StrError();
83     return 4;
84   }
85   // Perform a select with a timeout to guard against launcher hanging
86   cuttlefish::SharedFDSet read_set;
87   read_set.Set(monitor_socket);
88   struct timeval timeout = {FLAGS_wait_for_launcher, 0};
89   int selected = cuttlefish::Select(&read_set, nullptr, nullptr,
90                              FLAGS_wait_for_launcher <= 0 ? nullptr : &timeout);
91   if (selected < 0){
92     LOG(ERROR) << "Failed communication with the launcher monitor: "
93                << strerror(errno);
94     return 5;
95   }
96   if (selected == 0) {
97     LOG(ERROR) << "Timeout expired waiting for launcher monitor to respond";
98     return 6;
99   }
100   cuttlefish::LauncherResponse response;
101   auto bytes_recv = monitor_socket->Recv(&response, sizeof(response), 0);
102   if (bytes_recv < 0) {
103     LOG(ERROR) << "Error receiving response from launcher monitor: "
104                << monitor_socket->StrError();
105     return 7;
106   }
107   if (response != cuttlefish::LauncherResponse::kSuccess) {
108     LOG(ERROR) << "Received '" << static_cast<char>(response)
109                << "' response from launcher monitor";
110     return 8;
111   }
112   bytes_recv = monitor_socket->Recv(&response, sizeof(response), 0);
113   if (bytes_recv != 0) {
114     LOG(ERROR) << "execv must have failed in the launcher.";
115     if (bytes_recv < 0) {
116       LOG(ERROR) << monitor_socket->StrError();
117     }
118     return 9;
119   }
120   LOG(INFO) << "Powerwash successful";
121   return 0;
122 }
123