Skip to content

Commit cdfc413

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 add4696 commit cdfc413

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ 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",

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727

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

3334
#include "cuttlefish/common/libs/fs/shared_fd.h"
3435
#include "cuttlefish/common/libs/utils/contains.h"
3536
#include "cuttlefish/common/libs/utils/files.h"
37+
#include "cuttlefish/common/libs/utils/users.h"
3638
#include "cuttlefish/host/commands/cvd/instances/config_path.h"
3739
#include "cuttlefish/host/commands/cvd/utils/common.h"
3840
#include "cuttlefish/host/libs/config/config_constants.h"
@@ -55,9 +57,24 @@ Result<void> CheckProcessExitedNormally(siginfo_t infop,
5557
}
5658
}
5759

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

0 commit comments

Comments
 (0)