Skip to content

Commit 30a118e

Browse files
committed
Check user groups in ConstructCommand and fallback to cvd_refresh_groups if necessary
This change introduces a check in ConstructCommand to ensure the user is a member of cvdnetwork, kvm, and render groups. If not, it executes the command via cvd_refresh_groups. Assisted-by: Jetski <jetski@google.com> Bug: b/510930737
1 parent b2f98cc commit 30a118e

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ cf_cc_library(
149149
"//cuttlefish/common/libs/utils:contains",
150150
"//cuttlefish/common/libs/utils:files",
151151
"//cuttlefish/common/libs/utils:subprocess",
152+
"//cuttlefish/common/libs/utils:users",
152153
"//cuttlefish/host/commands/cvd/instances:config_path",
153154
"//cuttlefish/host/commands/cvd/utils",
154155
"//cuttlefish/host/libs/config:config_constants",
155156
"//cuttlefish/result",
157+
"//libbase",
156158
"@abseil-cpp//absl/strings",
157159
"@fmt",
158160
],

base/cvd/cuttlefish/host/commands/cvd/cli/utils.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
#include <vector>
2727

2828
#include "absl/strings/str_cat.h"
29+
#include "android-base/file.h"
2930
#include "fmt/format.h"
3031
#include "fmt/ranges.h" // NOLINT(misc-include-cleaner): version difference
3132

3233
#include "cuttlefish/common/libs/fs/shared_fd.h"
3334
#include "cuttlefish/common/libs/utils/contains.h"
3435
#include "cuttlefish/common/libs/utils/files.h"
36+
#include "cuttlefish/common/libs/utils/users.h"
3537
#include "cuttlefish/host/commands/cvd/instances/config_path.h"
3638
#include "cuttlefish/host/commands/cvd/utils/common.h"
3739
#include "cuttlefish/host/libs/config/config_constants.h"
@@ -54,9 +56,24 @@ Result<void> CheckProcessExitedNormally(siginfo_t infop,
5456
}
5557
}
5658

59+
static Command FixGroupsIfNecessary(const std::string& command_name,
60+
const std::string& bin_path) {
61+
bool in_required_groups =
62+
InGroup("cvdnetwork") && InGroup("kvm") && InGroup("render");
63+
if (in_required_groups) {
64+
return Command(command_name).SetExecutable(bin_path);
65+
} else {
66+
const std::string refresh_groups =
67+
android::base::GetExecutableDirectory() + "/cvd_refresh_groups";
68+
return Command("cvd_refresh_groups")
69+
.SetExecutable(refresh_groups)
70+
.AddParameter(bin_path)
71+
.AddParameter(command_name);
72+
}
73+
}
74+
5775
Result<Command> ConstructCommand(const ConstructCommandParam& param) {
58-
Command command(param.command_name);
59-
command.SetExecutable(param.bin_path);
76+
Command command = FixGroupsIfNecessary(param.command_name, param.bin_path);
6077
for (const std::string& arg : param.args) {
6178
command.AddParameter(arg);
6279
}

0 commit comments

Comments
 (0)