Skip to content

Commit 08e9f68

Browse files
authored
Update container.rs
1 parent 7c14f3b commit 08e9f68

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

source-code/src/container.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fs::{self, File};
22
use std::io::Read;
3-
use std::os::unix::io::AsRawFd;
3+
use std::os::fd::{AsRawFd, FromRawFd};
44
use std::path::{Path, PathBuf};
55
use std::thread;
66
use std::time::Duration;
@@ -11,15 +11,14 @@ use nix::sys::wait::waitpid;
1111
use nix::unistd::Pid;
1212
use owo_colors::OwoColorize;
1313
use serde::{Deserialize, Serialize};
14-
use miette::{IntoDiagnostic, WrapErr, Result, Context};
14+
use miette::{IntoDiagnostic, WrapErr, Result};
1515

1616
use crate::config::HkConfig;
1717
use crate::image::ImageManager;
1818
use crate::sandbox::{setup_overlayfs, setup_cgroups, setup_namespaces, seccomp_setup, child_entrypoint, ChildConfig};
1919
use crate::network::{setup_bridge, create_veth_pair, setup_port_forwarding, cleanup_port_forwarding, setup_cni};
2020
use crate::logging::ContainerLogger;
2121
use crate::metrics::record_container_metrics;
22-
use crate::utils::parse_bytes;
2322

2423
pub const HACKEROS_LIB: &str = "/var/lib/hackeros";
2524
pub const HACKEROS_RUN: &str = "/var/run/hackeros";
@@ -98,10 +97,8 @@ pub fn start_container(config: HkConfig, detached: bool) -> Result<()> {
9897
}
9998

10099
let stack = &mut [0; 2 * 1024 * 1024];
101-
// Use a raw pointer to pass config (avoid closure move issues)
102100
let cfg_ptr = Box::into_raw(Box::new(child_cfg));
103101
let cb = Box::new(move || {
104-
// Reconstruct ChildConfig from raw pointer
105102
let cfg = unsafe { Box::from_raw(cfg_ptr) };
106103
if let Err(e) = setup_namespaces(cfg.rootless) {
107104
eprintln!("Namespace setup failed: {}", e);
@@ -248,7 +245,7 @@ pub fn enter_container_pty(state: &ContainerState) -> Result<()> {
248245
match unsafe { fork() } {
249246
Ok(ForkResult::Parent { child: _ }) => {
250247
let mut raw_stdout = std::io::stdout().into_raw_mode().into_diagnostic()?;
251-
let mut master_file = unsafe { fs::File::from_raw_fd(master.as_raw_fd()) };
248+
let mut master_file = unsafe { File::from_raw_fd(master.as_raw_fd()) };
252249
let mut master_reader = master_file.try_clone().unwrap();
253250

254251
thread::spawn(move || {
@@ -270,14 +267,13 @@ pub fn enter_container_pty(state: &ContainerState) -> Result<()> {
270267
Ok(ForkResult::Child) => {
271268
attach_namespaces(state.pid)?;
272269
setsid().into_diagnostic()?;
273-
unsafe {
274-
for i in 0..3 {
275-
dup2(slave.as_raw_fd(), i).into_diagnostic()?;
276-
}
270+
// dup2 is safe – no need for unsafe block
271+
for i in 0..3 {
272+
dup2(slave.as_raw_fd(), i).into_diagnostic()?;
277273
}
278274
let cmd = std::ffi::CString::new("/bin/sh").unwrap();
279275
let args = [cmd.clone()];
280-
let env = [std::ffi::CString::new("PATH=/bin:/usr/bin:/sbin").unwrap()];
276+
let _env = [std::ffi::CString::new("PATH=/bin:/usr/bin:/sbin").unwrap()];
281277
let _ = nix::unistd::execvp(&cmd, &args);
282278
std::process::exit(1);
283279
}
@@ -290,7 +286,7 @@ fn attach_namespaces(pid: i32) -> Result<()> {
290286
let pid_fd = nix::unistd::Pid::from_raw(pid);
291287
for ns in &["ipc", "uts", "net", "pid", "mnt"] {
292288
let p = format!("/proc/{}/ns/{}", pid_fd, ns);
293-
let f = fs::File::open(p).into_diagnostic().context("ns open")?;
289+
let f = fs::File::open(p).into_diagnostic()?;
294290
nix::sched::setns(f, nix::sched::CloneFlags::empty()).into_diagnostic()?;
295291
}
296292
Ok(())

0 commit comments

Comments
 (0)