Skip to content

Commit e8503af

Browse files
34056915820405ysj
authored andcommitted
Wire cvdalloc up to run_cvd.
The instance id is passed to cvdalloc here so that cvdalloc can just simply trust that it is the appropriate instance ID and let that be managed elsewhere. Recall that we need run_cvd to invoke socketpair(2) for acting on key events with the cvdalloc process: when allocation has completed, when the instance is stopping and resources should be torn down, and when teardown is complete. We must wait on allocation completion so that we know we can start up the vmm without issue. To do that we need to override the WaitForAvailability method provided through VmmDependencyCommand; all classes implementing VmmDependencyCommand will have their WaitForAvailability methods called synchronously before the vmm is started, which achieves the intended result. When run_cvd is shutting down cleanly, we could potentially just have run_cvd close its end of the socket, let cvdalloc invoke its teardown sequence, and have run_cvd abandon the cvdalloc process (letting init(1) or equivalent adopt it), but this loses the ability to see cvdalloc's log messages. Instead, we wait explicitly on teardown completion for a clean shutdown.
1 parent b474d59 commit e8503af

File tree

10 files changed

+177
-8
lines changed

10 files changed

+177
-8
lines changed

base/cvd/cuttlefish/host/commands/cvdalloc/cvdalloc.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,13 @@ Result<int> CvdallocMain(int argc, char *argv[]) {
125125
};
126126

127127
CF_EXPECT(Allocate(id, bridge_name));
128-
CF_EXPECT(Post(sock));
128+
CF_EXPECT(cvdalloc::Post(sock));
129129

130130
LOG(INFO) << "cvdalloc: waiting to teardown";
131131

132-
CF_EXPECT(Wait(sock, kSemNoTimeout));
132+
CF_EXPECT(cvdalloc::Wait(sock, cvdalloc::kSemNoTimeout));
133133
std::move(teardown).Invoke();
134-
CF_EXPECT(Post(sock));
135-
134+
CF_EXPECT(cvdalloc::Post(sock));
136135

137136
return 0;
138137
}

base/cvd/cuttlefish/host/commands/cvdalloc/sem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include "cuttlefish/host/libs/command_util/util.h"
1919

20-
namespace cuttlefish {
20+
namespace cuttlefish::cvdalloc {
2121

2222
Result<void> Post(const SharedFD socket) {
2323
char i = 0;
@@ -35,4 +35,4 @@ Result<void> Wait(const SharedFD socket, std::chrono::seconds timeout) {
3535
return {};
3636
}
3737

38-
} // namespace
38+
} // namespace cuttlefish::cvdalloc

base/cvd/cuttlefish/host/commands/cvdalloc/sem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "cuttlefish/common/libs/fs/shared_fd.h"
2121
#include "cuttlefish/common/libs/utils/result.h"
2222

23-
namespace cuttlefish {
23+
namespace cuttlefish::cvdalloc {
2424

2525
constexpr std::chrono::seconds kSemNoTimeout = std::chrono::seconds(0);
2626

@@ -40,4 +40,4 @@ Result<void> Post(const SharedFD socket);
4040
*/
4141
Result<void> Wait(const SharedFD socket, std::chrono::seconds timeout);
4242

43-
} // namespace
43+
} // namespace cuttlefish::cvdalloc

base/cvd/cuttlefish/host/commands/run_cvd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ cf_cc_binary(
6060
"//cuttlefish/host/commands/run_cvd/launch:casimir_control_server",
6161
"//cuttlefish/host/commands/run_cvd/launch:console_forwarder",
6262
"//cuttlefish/host/commands/run_cvd/launch:control_env_proxy_server",
63+
"//cuttlefish/host/commands/run_cvd/launch:cvdalloc",
6364
"//cuttlefish/host/commands/run_cvd/launch:echo_server",
6465
"//cuttlefish/host/commands/run_cvd/launch:gnss_grpc_proxy",
6566
"//cuttlefish/host/commands/run_cvd/launch:kernel_log_monitor",

base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ cf_cc_library(
101101
],
102102
)
103103

104+
cf_cc_library(
105+
name = "cvdalloc",
106+
srcs = ["cvdalloc.cpp"],
107+
hdrs = ["cvdalloc.h"],
108+
deps = [
109+
"//cuttlefish/common/libs/fs",
110+
"//cuttlefish/common/libs/utils:result",
111+
"//cuttlefish/common/libs/utils:subprocess",
112+
"//cuttlefish/host/commands/cvdalloc:sem",
113+
"//cuttlefish/host/libs/command_util",
114+
"//cuttlefish/host/libs/config:cuttlefish_config",
115+
"//cuttlefish/host/libs/config:known_paths",
116+
"//cuttlefish/host/libs/feature",
117+
"//cuttlefish/host/libs/vm_manager",
118+
"//libbase",
119+
"@fruit",
120+
],
121+
)
122+
104123
cf_cc_library(
105124
name = "echo_server",
106125
srcs = ["echo_server.cpp"],
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#pragma once
17+
18+
#include <fruit/fruit.h>
19+
20+
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
21+
22+
namespace cuttlefish {
23+
24+
fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific>>
25+
CvdallocComponent();
26+
27+
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/run_cvd/main.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "cuttlefish/host/commands/run_cvd/launch/casimir_control_server.h"
4343
#include "cuttlefish/host/commands/run_cvd/launch/console_forwarder.h"
4444
#include "cuttlefish/host/commands/run_cvd/launch/control_env_proxy_server.h"
45+
#include "cuttlefish/host/commands/run_cvd/launch/cvdalloc.h"
4546
#include "cuttlefish/host/commands/run_cvd/launch/echo_server.h"
4647
#include "cuttlefish/host/commands/run_cvd/launch/gnss_grpc_proxy.h"
4748
#include "cuttlefish/host/commands/run_cvd/launch/input_connections_provider.h"
@@ -174,6 +175,7 @@ fruit::Component<> runCvdComponent(
174175
.install(AutoCmd<VhalProxyServer>::Component)
175176
.install(Ti50EmulatorComponent)
176177
#endif
178+
.install(CvdallocComponent)
177179
.install(AdbConfigComponent)
178180
.install(AdbConfigFragmentComponent)
179181
.install(FastbootConfigComponent)

base/cvd/cuttlefish/host/libs/config/known_paths.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ std::string ControlEnvProxyServerBinary() {
4646

4747
std::string CpioBinary() { return HostBinaryPath("cpio"); }
4848

49+
std::string CvdallocBinary() { return HostBinaryPath("cvdalloc"); }
50+
4951
std::string DefaultKeyboardSpec() {
5052
return DefaultHostArtifactsPath("etc/default_input_devices/keyboard.json");
5153
}

base/cvd/cuttlefish/host/libs/config/known_paths.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ std::string CasimirControlServerBinary();
2727
std::string ConsoleForwarderBinary();
2828
std::string ControlEnvProxyServerBinary();
2929
std::string CpioBinary();
30+
std::string CvdallocBinary();
3031
std::string DefaultKeyboardSpec();
3132
std::string DefaultMouseSpec();
3233
std::string DefaultGamepadSpec();

0 commit comments

Comments
 (0)