Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move wait logic from intermediate process to main process #116

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions crates/libcontainer/src/process/container_intermediate_process.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{namespaces::Namespaces, process::channel, process::fork};
use anyhow::{Context, Error, Result};
use libcgroups::common::CgroupManager;
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::{Gid, Pid, Uid};
use oci_spec::runtime::{LinuxNamespaceType, LinuxResources};
use procfs::process::Process;
Expand All @@ -15,8 +14,7 @@ pub fn container_intermediate_process(
intermediate_chan: &mut (channel::IntermediateSender, channel::IntermediateReceiver),
init_chan: &mut (channel::InitSender, channel::InitReceiver),
main_sender: &mut channel::MainSender,
wait: bool,
) -> Result<WaitStatus> {
) -> Result<Pid> {
let (inter_sender, inter_receiver) = intermediate_chan;
let (init_sender, init_receiver) = init_chan;
let command = &args.syscall;
Expand Down Expand Up @@ -118,13 +116,7 @@ pub fn container_intermediate_process(
.close()
.context("failed to close unused init sender")?;

if wait {
Ok(waitpid(pid, None)?)
} else {
// we don't actually want to wait, so
// pid and status doesn't really matter
Ok(WaitStatus::Exited(Pid::from_raw(0), 0))
}
return Ok(pid);
}

fn apply_cgroups<C: CgroupManager + ?Sized>(
Expand Down
19 changes: 12 additions & 7 deletions crates/libcontainer/src/process/container_main_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anyhow::{Context, Result};
use nix::{
sys::{
socket::{self, UnixAddr},
wait::WaitStatus,
wait::{waitpid, WaitStatus},
},
unistd::{self, Pid},
};
Expand All @@ -26,16 +26,21 @@ pub fn container_main_process(container_args: &ContainerArgs, wait: bool) -> Res
let init_chan = &mut channel::init_channel()?;

let intermediate_pid = fork::container_fork(|| {
match container_intermediate_process::container_intermediate_process(
let container_pid = container_intermediate_process::container_intermediate_process(
container_args,
inter_chan,
init_chan,
main_sender,
wait,
)? {
WaitStatus::Exited(_, s) => Ok(s),
WaitStatus::Signaled(_, sig, _) => Ok(sig as i32),
_ => Ok(0),
)?;

if wait {
match waitpid(container_pid, None)? {
WaitStatus::Exited(_, s) => Ok(s),
WaitStatus::Signaled(_, sig, _) => Ok(sig as i32),
_ => Ok(0),
}
} else {
Ok(0)
}
})?;
// Close down unused fds. The corresponding fds are duplicated to the
Expand Down