1 /*
2  * Copyright (C) 2017 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 #pragma once
18 
19 #include <atomic>
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include <fruit/fruit.h>
26 
27 #include "common/libs/fs/shared_fd.h"
28 #include "common/libs/utils/json.h"
29 #include "common/libs/utils/result.h"
30 #include "host/commands/run_cvd/launch/webrtc_controller.h"
31 #include "host/commands/run_cvd/server_loop.h"
32 #include "host/libs/command_util/runner/defs.h"
33 #include "host/libs/command_util/util.h"
34 #include "host/libs/config/command_source.h"
35 #include "host/libs/config/cuttlefish_config.h"
36 #include "host/libs/config/feature.h"
37 #include "host/libs/config/inject.h"
38 #include "host/libs/process_monitor/process_monitor.h"
39 
40 namespace cuttlefish {
41 namespace run_cvd_impl {
42 
43 class ServerLoopImpl : public ServerLoop,
44                        public SetupFeature,
45                        public LateInjected {
46  public:
47   INJECT(ServerLoopImpl(const CuttlefishConfig& config,
48                         const CuttlefishConfig::InstanceSpecific& instance,
49                         AutoSnapshotControlFiles::Type& snapshot_control_files,
50                         WebRtcController& webrtc_controller));
51 
52   Result<void> LateInject(fruit::Injector<>& injector) override;
53 
54   // ServerLoop
55   Result<void> Run() override;
56 
57   // SetupFeature
Name()58   std::string Name() const override { return "ServerLoop"; }
59 
60   enum class DeviceStatus : int {
61     kUnknown = 0,
62     kActive = 1,
63     kSuspended = 2,
64   };
65 
66  private:
Dependencies()67   std::unordered_set<SetupFeature*> Dependencies() const override {
68     return {&snapshot_control_files_};
69   }
70   Result<void> ResultSetup() override;
71   Result<void> HandleExtended(const LauncherActionInfo& action_info,
72                               ProcessMonitor& process_monitor);
73   Result<void> HandleSuspend(ProcessMonitor& process_monitor);
74   Result<void> HandleResume(ProcessMonitor& process_monitor);
75   Result<void> HandleSnapshotTake(const run_cvd::SnapshotTake& snapshot_take);
76   Result<void> HandleStartScreenRecording();
77   Result<void> HandleStopScreenRecording();
78   Result<void> HandleScreenshotDisplay(
79       const run_cvd::ScreenshotDisplay& request);
80 
81   void HandleActionWithNoData(const LauncherAction action,
82                               const SharedFD& client,
83                               ProcessMonitor& process_monitor);
84 
85   void DeleteFifos();
86   bool PowerwashFiles();
87   void RestartRunCvd(int notification_fd);
88   static bool CreateQcowOverlay(const std::string& crosvm_path,
89                                 const std::string& backing_file,
90                                 const std::string& output_overlay_path);
91   Result<void> SuspendGuest();
92   Result<void> ResumeGuest();
93 
94   static std::unordered_map<std::string, std::string>
95   InitializeVmToControlSockPath(const CuttlefishConfig::InstanceSpecific&);
96   Result<std::string> VmControlSocket() const;
97   Result<void> TakeGuestSnapshot(VmmMode, const std::string&);
98   Result<void> TakeCrosvmGuestSnapshot(const Json::Value&);
99 
100   const CuttlefishConfig& config_;
101   const CuttlefishConfig::InstanceSpecific& instance_;
102 
103   /*
104    * This is needed to get the run_cvd side socket pair connected to
105    * secure_env. The socket pairs are used to send suspend/resume to
106    * secure_env, and get the responses.
107    */
108   AutoSnapshotControlFiles::Type& snapshot_control_files_;
109   WebRtcController& webrtc_controller_;
110   std::vector<CommandSource*> command_sources_;
111   SharedFD server_;
112   // mapping from the name of vm_manager to control_sock path
113   std::unordered_map<std::string, std::string> vm_name_to_control_sock_;
114   std::atomic<DeviceStatus> device_status_;
115 };
116 
117 }  // namespace run_cvd_impl
118 }  // namespace cuttlefish
119