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_recorder.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 WebRtcRecorder& webrtc_recorder)); 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: Enabled()67 bool Enabled() const override { return true; } Dependencies()68 std::unordered_set<SetupFeature*> Dependencies() const override { 69 return {&snapshot_control_files_}; 70 } 71 Result<void> ResultSetup() override; 72 Result<void> HandleExtended(const LauncherActionInfo& action_info, 73 ProcessMonitor& process_monitor); 74 Result<void> HandleSuspend(ProcessMonitor& process_monitor); 75 Result<void> HandleResume(ProcessMonitor& process_monitor); 76 Result<void> HandleSnapshotTake(const run_cvd::SnapshotTake& snapshot_take); 77 Result<void> HandleStartScreenRecording(); 78 Result<void> HandleStopScreenRecording(); 79 80 void HandleActionWithNoData(const LauncherAction action, 81 const SharedFD& client, 82 ProcessMonitor& process_monitor); 83 84 void DeleteFifos(); 85 bool PowerwashFiles(); 86 void RestartRunCvd(int notification_fd); 87 static bool CreateQcowOverlay(const std::string& crosvm_path, 88 const std::string& backing_file, 89 const std::string& output_overlay_path); 90 Result<void> SuspendGuest(); 91 Result<void> ResumeGuest(); 92 93 static std::unordered_map<std::string, std::string> 94 InitializeVmToControlSockPath(const CuttlefishConfig::InstanceSpecific&); 95 Result<std::string> VmControlSocket() const; 96 Result<void> TakeGuestSnapshot(VmmMode, const std::string&); 97 Result<void> TakeCrosvmGuestSnapshot(const Json::Value&); 98 99 const CuttlefishConfig& config_; 100 const CuttlefishConfig::InstanceSpecific& instance_; 101 102 /* 103 * This is needed to get the run_cvd side socket pair connected to 104 * secure_env. The socket pairs are used to send suspend/resume to 105 * secure_env, and get the responses. 106 */ 107 AutoSnapshotControlFiles::Type& snapshot_control_files_; 108 WebRtcRecorder& webrtc_recorder_; 109 std::vector<CommandSource*> command_sources_; 110 SharedFD server_; 111 // mapping from the name of vm_manager to control_sock path 112 std::unordered_map<std::string, std::string> vm_name_to_control_sock_; 113 std::atomic<DeviceStatus> device_status_; 114 }; 115 116 } // namespace run_cvd_impl 117 } // namespace cuttlefish 118