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(wait_for_launcher, 5,
49 "How many seconds to wait for the launcher to respond to the status "
50 "command. A value of zero means wait indefinetly");
51
main(int argc,char ** argv)52 int main(int argc, char** argv) {
53 ::android::base::InitLogging(argv, android::base::StderrLogger);
54 google::ParseCommandLineFlags(&argc, &argv, true);
55
56 auto config = cuttlefish::CuttlefishConfig::Get();
57 if (!config) {
58 LOG(ERROR) << "Failed to obtain config object";
59 return 1;
60 }
61
62 auto instance = config->ForDefaultInstance();
63 auto monitor_path = instance.launcher_monitor_socket_path();
64 if (monitor_path.empty()) {
65 LOG(ERROR) << "No path to launcher monitor found";
66 return 2;
67 }
68 auto monitor_socket = cuttlefish::SharedFD::SocketLocalClient(monitor_path.c_str(),
69 false, SOCK_STREAM);
70 if (!monitor_socket->IsOpen()) {
71 LOG(ERROR) << "Unable to connect to launcher monitor at " << monitor_path
72 << ": " << monitor_socket->StrError();
73 return 3;
74 }
75 auto request = cuttlefish::LauncherAction::kStatus;
76 auto bytes_sent = monitor_socket->Send(&request, sizeof(request), 0);
77 if (bytes_sent < 0) {
78 LOG(ERROR) << "Error sending launcher monitor the status command: "
79 << monitor_socket->StrError();
80 return 4;
81 }
82 // Perform a select with a timeout to guard against launcher hanging
83 cuttlefish::SharedFDSet read_set;
84 read_set.Set(monitor_socket);
85 struct timeval timeout = {FLAGS_wait_for_launcher, 0};
86 int selected = cuttlefish::Select(&read_set, nullptr, nullptr,
87 FLAGS_wait_for_launcher <= 0 ? nullptr : &timeout);
88 if (selected < 0){
89 LOG(ERROR) << "Failed communication with the launcher monitor: "
90 << strerror(errno);
91 return 5;
92 }
93 if (selected == 0) {
94 LOG(ERROR) << "Timeout expired waiting for launcher monitor to respond";
95 return 6;
96 }
97 cuttlefish::LauncherResponse response;
98 auto bytes_recv = monitor_socket->Recv(&response, sizeof(response), 0);
99 if (bytes_recv < 0) {
100 LOG(ERROR) << "Error receiving response from launcher monitor: "
101 << monitor_socket->StrError();
102 return 7;
103 }
104 if (response != cuttlefish::LauncherResponse::kSuccess) {
105 LOG(ERROR) << "Received '" << static_cast<char>(response)
106 << "' response from launcher monitor";
107 return 8;
108 }
109 LOG(INFO) << "run_cvd is active.";
110 return 0;
111 }
112