|
| 1 | +// |
| 2 | +// Copyright (C) 2025 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 | +#include "cuttlefish/host/commands/run_cvd/launch/cvdalloc.h" |
| 17 | + |
| 18 | +#include <sys/socket.h> |
| 19 | +#include <chrono> |
| 20 | +#include <string> |
| 21 | +#include <unordered_set> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include <android-base/logging.h> |
| 26 | +#include <fruit/component.h> |
| 27 | +#include <fruit/fruit_forward_decls.h> |
| 28 | +#include <fruit/macro.h> |
| 29 | + |
| 30 | +#include "cuttlefish/common/libs/fs/shared_fd.h" |
| 31 | +#include "cuttlefish/common/libs/utils/result.h" |
| 32 | +#include "cuttlefish/common/libs/utils/subprocess.h" |
| 33 | +#include "cuttlefish/host/commands/cvdalloc/sem.h" |
| 34 | +#include "cuttlefish/host/libs/config/cuttlefish_config.h" |
| 35 | +#include "cuttlefish/host/libs/config/known_paths.h" |
| 36 | +#include "cuttlefish/host/libs/feature/command_source.h" |
| 37 | +#include "cuttlefish/host/libs/feature/feature.h" |
| 38 | +#include "cuttlefish/host/libs/vm_manager/vm_manager.h" |
| 39 | + |
| 40 | +namespace cuttlefish { |
| 41 | +namespace { |
| 42 | + |
| 43 | +constexpr std::chrono::seconds kCvdAllocateTimeout = std::chrono::seconds(30); |
| 44 | +constexpr std::chrono::seconds kCvdTeardownTimeout = std::chrono::seconds(2); |
| 45 | + |
| 46 | +class Cvdalloc : public vm_manager::VmmDependencyCommand { |
| 47 | + public: |
| 48 | + INJECT(Cvdalloc(const CuttlefishConfig::InstanceSpecific &instance)) |
| 49 | + : instance_(instance) {} |
| 50 | + |
| 51 | + // CommandSource |
| 52 | + Result<std::vector<MonitorCommand>> Commands() override { |
| 53 | + auto nice_stop = [this]() { return Stop(); }; |
| 54 | + |
| 55 | + Command cmd(CvdallocBinary(), KillSubprocessFallback(nice_stop)); |
| 56 | + cmd.AddParameter("--id=", instance_.id()); |
| 57 | + cmd.AddParameter("--socket=", their_socket_); |
| 58 | + std::vector<MonitorCommand> commands; |
| 59 | + commands.emplace_back(std::move(cmd)); |
| 60 | + return commands; |
| 61 | + } |
| 62 | + |
| 63 | + std::string Name() const override { return "Cvdalloc"; } |
| 64 | + bool Enabled() const override { return instance_.use_cvdalloc(); } |
| 65 | + std::unordered_set<SetupFeature *> Dependencies() const override { |
| 66 | + return {}; |
| 67 | + } |
| 68 | + |
| 69 | + // StatusCheckCommandSource |
| 70 | + Result<void> WaitForAvailability() const override { |
| 71 | + LOG(INFO) << "cvdalloc (run_cvd): waiting to finish allocation."; |
| 72 | + CF_EXPECT(cvdalloc::Wait(socket_, kCvdAllocateTimeout), |
| 73 | + "cvdalloc (run_cvd): Wait failed"); |
| 74 | + LOG(INFO) << "cvdalloc (run_cvd): allocation is done."; |
| 75 | + |
| 76 | + return {}; |
| 77 | + } |
| 78 | + |
| 79 | + private: |
| 80 | + Result<void> ResultSetup() override { |
| 81 | + std::pair<SharedFD, SharedFD> p = |
| 82 | + CF_EXPECT(SharedFD::SocketPair(AF_LOCAL, SOCK_STREAM, 0)); |
| 83 | + socket_ = std::move(p.first); |
| 84 | + their_socket_ = std::move(p.second); |
| 85 | + return {}; |
| 86 | + } |
| 87 | + |
| 88 | + StopperResult Stop() { |
| 89 | + LOG(INFO) << "cvdalloc (run_cvd): stop requested; teardown started"; |
| 90 | + if (!cvdalloc::Post(socket_).ok()) { |
| 91 | + LOG(INFO) << "cvdalloc (run_cvd): stop failed: couldn't Post"; |
| 92 | + return StopperResult::kStopFailure; |
| 93 | + } |
| 94 | + |
| 95 | + if (!cvdalloc::Wait(socket_, kCvdTeardownTimeout).ok()) { |
| 96 | + LOG(INFO) << "cvdalloc (run_cvd): stop failed: couldn't Wait"; |
| 97 | + return StopperResult::kStopFailure; |
| 98 | + } |
| 99 | + |
| 100 | + LOG(INFO) << "cvdalloc (run_cvd): teardown completed"; |
| 101 | + return StopperResult::kStopSuccess; |
| 102 | + } |
| 103 | + |
| 104 | + const CuttlefishConfig::InstanceSpecific &instance_; |
| 105 | + SharedFD socket_, their_socket_; |
| 106 | +}; |
| 107 | + |
| 108 | +} // namespace |
| 109 | + |
| 110 | +fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific>> |
| 111 | +CvdallocComponent() { |
| 112 | + return fruit::createComponent() |
| 113 | + .addMultibinding<vm_manager::VmmDependencyCommand, Cvdalloc>() |
| 114 | + .addMultibinding<CommandSource, Cvdalloc>() |
| 115 | + .addMultibinding<SetupFeature, Cvdalloc>(); |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace cuttlefish |
0 commit comments