/* * Copyright (C) 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include "common/libs/utils/result.h" #include "host/commands/cvd/types.h" namespace cuttlefish { class RunCvdProcessManager { struct RunCvdProcInfo { pid_t pid_; std::string home_; std::string exec_path_; cvd_common::Envs envs_; cvd_common::Args cmd_args_; std::string stop_cvd_path_; bool is_cvd_server_started_; std::optional android_host_out_; unsigned id_; }; struct GroupProcInfo { std::string home_; std::string exec_path_; std::string stop_cvd_path_; bool is_cvd_server_started_; std::optional android_host_out_; struct InstanceInfo { std::set pids_; cvd_common::Envs envs_; cvd_common::Args cmd_args_; unsigned id_; }; // instance id to instance info mapping std::unordered_map instances_; }; public: static Result Get(); RunCvdProcessManager(const RunCvdProcessManager&) = delete; RunCvdProcessManager(RunCvdProcessManager&&) = default; Result KillAllCuttlefishInstances(const bool cvd_server_children_only, const bool clear_runtime_dirs) { auto stop_cvd_result = RunStopCvdAll(cvd_server_children_only, clear_runtime_dirs); if (!stop_cvd_result.ok()) { LOG(ERROR) << stop_cvd_result.error().Message(); } auto send_signals_result = SendSignals(cvd_server_children_only); if (!send_signals_result.ok()) { LOG(ERROR) << send_signals_result.error().Message(); } DeleteLockFiles(cvd_server_children_only); cf_groups_.clear(); auto recollect_info_result = CollectInfo(); if (!recollect_info_result.ok()) { LOG(ERROR) << "Recollecting run_cvd processes information failed."; LOG(ERROR) << recollect_info_result.error().Trace(); } return {}; } private: RunCvdProcessManager() = default; static Result RunStopCvd(const GroupProcInfo& run_cvd_info, const bool clear_runtime_dirs); Result RunStopCvdAll(const bool cvd_server_children_only, const bool clear_runtime_dirs); Result SendSignals(const bool cvd_server_children_only); Result AnalyzeRunCvdProcess(const pid_t pid); void DeleteLockFiles(const bool cvd_server_children_only); Result> CollectInfo(); std::vector cf_groups_; }; struct DeviceClearOptions { bool cvd_server_children_only; bool clear_instance_dirs; }; /* * Runs stop_cvd for all cuttlefish instances found based on run_cvd processes, * and send SIGKILL to the run_cvd processes. * * If cvd_server_children_only is set, it kills the run_cvd processes that were * started by a cvd server process. */ Result KillAllCuttlefishInstances(const DeviceClearOptions& options); Result KillCvdServerProcess(); } // namespace cuttlefish