Skip to content

Commit

Permalink
feat(rsjudge-runner): 🚩 add setgroups flag, allow using groups method…
Browse files Browse the repository at this point in the history
… from std
  • Loading branch information
Jisu-Woniu committed Mar 24, 2024
1 parent ac60529 commit 0a04c95
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/rsjudge-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ caps = "0.5.5"
nix = { version = "0.28.0", features = ["user"] }
once_cell = "1.19.0"
uzers = "0.11.3"

[build-dependencies]
rustc_version = "0.4.0"
41 changes: 28 additions & 13 deletions crates/rsjudge-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,38 @@ impl RunAs for Command {

self.uid(uid).gid(gid);

let groups: Vec<_> = user
.groups()
.unwrap_or_default()
.into_iter()
.map(|g| Gid::from_raw(g.gid()))
.collect();

// SAFETY: `group` is moved into the closure,
// and no longer accessible outside it.
//
// Replace with `CommandExt::groups` once it's stable.
unsafe {
self.pre_exec(move || {
setgroups(&groups)?;
Ok(())
})
};
#[cfg(not(setgroups))]
{
let groups: Vec<_> = user
.groups()
.unwrap_or_default()
.into_iter()
.map(|g| Gid::from_raw(g.gid()))
.collect();
unsafe {
self.pre_exec(move || {
setgroups(&groups)?;
Ok(())
})
};
}

#[cfg(setgroups)]
{
let groups: Vec<_> = user
.groups()
.unwrap_or_default()
.into_iter()
.map(|g| g.gid())
.collect();

self.groups(groups);
}

self
}
}

0 comments on commit 0a04c95

Please sign in to comment.