Skip to content

Commit 16ff3c6

Browse files
committed
refactor(simulator): use safe descriptor flag wrappers
1 parent ba398bd commit 16ff3c6

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

dstack/tee-simulator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dstack-types.workspace = true
2121
dstack-mr.workspace = true
2222
fuser.workspace = true
2323
libc.workspace = true
24-
nix = { workspace = true, features = ["mount"] }
24+
nix = { workspace = true, features = ["fs", "mount"] }
2525
sd-notify.workspace = true
2626
sha2.workspace = true
2727
tracing.workspace = true

dstack/tee-simulator/src/tpm.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,15 @@ pub fn run_nitro_vtpm(runtime_dir: &Path, config: &TeeSimulatorConfig) -> Result
293293
let generator = NsmGenerator::from_seed(parse_seed(seed)?)?;
294294
let (mut simulator, swtpm_stream) = UnixStream::pair()?;
295295
let swtpm_fd = swtpm_stream.as_raw_fd();
296-
let flags = unsafe { libc::fcntl(swtpm_fd, libc::F_GETFD) };
297-
anyhow::ensure!(flags >= 0, "failed to get swtpm socket flags");
298-
anyhow::ensure!(
299-
unsafe { libc::fcntl(swtpm_fd, libc::F_SETFD, flags & !libc::FD_CLOEXEC) } >= 0,
300-
"failed to make swtpm socket inheritable"
296+
let flags = nix::fcntl::FdFlag::from_bits_truncate(
297+
nix::fcntl::fcntl(swtpm_fd, nix::fcntl::FcntlArg::F_GETFD)
298+
.context("failed to get swtpm socket flags")?,
301299
);
300+
nix::fcntl::fcntl(
301+
swtpm_fd,
302+
nix::fcntl::FcntlArg::F_SETFD(flags - nix::fcntl::FdFlag::FD_CLOEXEC),
303+
)
304+
.context("failed to make swtpm socket inheritable")?;
302305

303306
let state_dir = std::env::temp_dir().join(format!("dstack-nitro-swtpm-{}", std::process::id()));
304307
fs_err::create_dir_all(&state_dir)?;

0 commit comments

Comments
 (0)