• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "host/libs/command_util/util.h"
18 
19 #include "sys/time.h"
20 #include "sys/types.h"
21 
22 #include <string>
23 
24 #include "common/libs/fs/shared_buf.h"
25 #include "common/libs/fs/shared_fd.h"
26 #include "common/libs/fs/shared_select.h"
27 #include "common/libs/utils/result.h"
28 #include "host/commands/run_cvd/runner_defs.h"
29 #include "host/libs/config/cuttlefish_config.h"
30 
31 namespace cuttlefish {
32 namespace {
33 
34 template <typename T>
ReadFromMonitor(const SharedFD & monitor_socket)35 Result<T> ReadFromMonitor(const SharedFD& monitor_socket) {
36   T response;
37   ssize_t bytes_recv = ReadExactBinary(monitor_socket, &response);
38   CF_EXPECT(bytes_recv != 0, "Launcher socket closed unexpectedly");
39   CF_EXPECT(bytes_recv > 0, "Error receiving response from launcher monitor: "
40                                 << monitor_socket->StrError());
41   CF_EXPECT(bytes_recv == sizeof(response),
42             "Launcher response not correct number of bytes");
43   return response;
44 }
45 
46 }  // namespace
47 
ReadLauncherResponse(const SharedFD & monitor_socket)48 Result<LauncherResponse> ReadLauncherResponse(const SharedFD& monitor_socket) {
49   return ReadFromMonitor<LauncherResponse>(monitor_socket);
50 }
51 
ReadExitCode(const SharedFD & monitor_socket)52 Result<RunnerExitCodes> ReadExitCode(const SharedFD& monitor_socket) {
53   return ReadFromMonitor<RunnerExitCodes>(monitor_socket);
54 }
55 
GetLauncherMonitorFromInstance(const CuttlefishConfig::InstanceSpecific & instance_config,const int timeout_seconds)56 Result<SharedFD> GetLauncherMonitorFromInstance(
57     const CuttlefishConfig::InstanceSpecific& instance_config,
58     const int timeout_seconds) {
59   std::string monitor_path = instance_config.launcher_monitor_socket_path();
60   CF_EXPECT(!monitor_path.empty(), "No path to launcher monitor found");
61 
62   SharedFD monitor_socket = SharedFD::SocketLocalClient(
63       monitor_path.c_str(), false, SOCK_STREAM, timeout_seconds);
64   CF_EXPECT(monitor_socket->IsOpen(),
65             "Unable to connect to launcher monitor at "
66                 << monitor_path << ":" << monitor_socket->StrError());
67   return monitor_socket;
68 }
69 
GetLauncherMonitor(const CuttlefishConfig & config,const int instance_num,const int timeout_seconds)70 Result<SharedFD> GetLauncherMonitor(const CuttlefishConfig& config,
71                                     const int instance_num,
72                                     const int timeout_seconds) {
73   auto instance_config = config.ForInstance(instance_num);
74   return GetLauncherMonitorFromInstance(instance_config, timeout_seconds);
75 }
76 
WriteLauncherAction(const SharedFD & monitor_socket,const LauncherAction request)77 Result<void> WriteLauncherAction(const SharedFD& monitor_socket,
78                                  const LauncherAction request) {
79   ssize_t bytes_sent = WriteAllBinary(monitor_socket, &request);
80   CF_EXPECT(bytes_sent > 0, "Error sending launcher monitor the command: "
81                                 << monitor_socket->StrError());
82   CF_EXPECT(bytes_sent == sizeof(request),
83             "Launcher did not send correct number of bytes");
84   return {};
85 }
86 
WaitForRead(const SharedFD & monitor_socket,const int timeout_seconds)87 Result<void> WaitForRead(const SharedFD& monitor_socket,
88                          const int timeout_seconds) {
89   // Perform a select with a timeout to guard against launcher hanging
90   SharedFDSet read_set;
91   read_set.Set(monitor_socket);
92   struct timeval timeout = {timeout_seconds, 0};
93   int select_result = Select(&read_set, nullptr, nullptr,
94                              timeout_seconds <= 0 ? nullptr : &timeout);
95   CF_EXPECT(select_result != 0,
96             "Timeout expired waiting for launcher monitor to respond");
97   CF_EXPECT(
98       select_result > 0,
99       "Failed communication with the launcher monitor: " << strerror(errno));
100   return {};
101 }
102 
103 }  // namespace cuttlefish
104