Skip to content

Commit

Permalink
Wait for the terminated of the command ran by exec command.
Browse files Browse the repository at this point in the history
Signed-off-by: utam0k <k0ma@utam0k.jp>
  • Loading branch information
utam0k committed Jun 29, 2022
1 parent 6eefb16 commit 0d1ae45
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
21 changes: 11 additions & 10 deletions crates/libcontainer/src/container/builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
utils,
};
use anyhow::{bail, Context, Result};
use nix::unistd::Pid;
use oci_spec::runtime::Spec;
use std::{fs, io::Write, os::unix::prelude::RawFd, path::PathBuf};

Expand Down Expand Up @@ -40,19 +41,19 @@ pub(super) struct ContainerBuilderImpl<'a> {
}

impl<'a> ContainerBuilderImpl<'a> {
pub(super) fn create(&mut self) -> Result<()> {
if let Err(outer) = self.run_container().context("failed to create container") {
if let Err(inner) = self.cleanup_container() {
return Err(outer.context(inner));
pub(super) fn create(&mut self) -> Result<Pid> {
match self.run_container().context("failed to create container") {
Ok(pid) => Ok(pid),
Err(outer) => {
if let Err(inner) = self.cleanup_container() {
return Err(outer.context(inner));
}
Err(outer)
}

return Err(outer);
}

Ok(())
}

fn run_container(&mut self) -> Result<()> {
fn run_container(&mut self) -> Result<Pid> {
let linux = self.spec.linux().as_ref().context("no linux in spec")?;
let cgroups_path = utils::get_cgroup_path(
linux.cgroups_path(),
Expand Down Expand Up @@ -138,7 +139,7 @@ impl<'a> ContainerBuilderImpl<'a> {
.context("Failed to save container state")?;
}

Ok(())
Ok(init_pid)
}

fn cleanup_container(&self) -> Result<()> {
Expand Down
9 changes: 5 additions & 4 deletions crates/libcontainer/src/container/tenant_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{bail, Context, Result};
use caps::Capability;
use nix::unistd;
use nix::unistd::{self, Pid};
use oci_spec::runtime::{
Capabilities as SpecCapabilities, Capability as SpecCapability, LinuxBuilder,
LinuxCapabilities, LinuxCapabilitiesBuilder, LinuxNamespace, LinuxNamespaceBuilder,
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<'a> TenantContainerBuilder<'a> {
}

/// Joins an existing container
pub fn build(self) -> Result<()> {
pub fn build(self) -> Result<Pid> {
let container_dir = self
.lookup_container_dir()
.context("failed to look up container dir")?;
Expand Down Expand Up @@ -130,11 +130,12 @@ impl<'a> TenantContainerBuilder<'a> {
preserve_fds: self.base.preserve_fds,
};

builder_impl.create()?;
let pid = builder_impl.create()?;

let mut notify_socket = NotifySocket::new(notify_path);
notify_socket.notify_container_start()?;
Ok(())

Ok(pid)
}

fn lookup_container_dir(&self) -> Result<PathBuf> {
Expand Down
30 changes: 25 additions & 5 deletions crates/youki/src/commands/exec.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use anyhow::Result;
use std::path::PathBuf;
use anyhow::{bail, Context, Result};
use nix::{
libc,
poll::{PollFd, PollFlags},
};
use std::{os::unix::prelude::RawFd, path::PathBuf};

use libcontainer::{container::builder::ContainerBuilder, syscall::syscall::create_syscall};
use liboci_cli::Exec;

pub fn exec(args: Exec, root_path: PathBuf) -> Result<()> {
pub fn exec(args: Exec, root_path: PathBuf) -> Result<i32> {
let syscall = create_syscall();
ContainerBuilder::new(args.container_id.clone(), syscall.as_ref())
let pid = ContainerBuilder::new(args.container_id.clone(), syscall.as_ref())
.with_root_path(root_path)?
.with_console_socket(args.console_socket.as_ref())
.with_pid_file(args.pid_file.as_ref())?
Expand All @@ -16,5 +20,21 @@ pub fn exec(args: Exec, root_path: PathBuf) -> Result<()> {
.with_process(args.process.as_ref())
.with_no_new_privs(args.no_new_privs)
.with_container_args(args.command.clone())
.build()
.build()?;

let pidfd = pidfd_open(pid.as_raw(), 0)?;
let poll_fd = PollFd::new(pidfd, PollFlags::POLLIN);
nix::poll::poll(&mut [poll_fd], -1).context("failed to wait for the container id")?;

// TODO
Ok(0)
}

fn pidfd_open(pid: libc::pid_t, flags: libc::c_uint) -> Result<RawFd> {
let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, pid, flags) };
if fd == -1 {
bail!("faild to pifd_open syscall")
} else {
Ok(fd as RawFd)
}
}
5 changes: 4 additions & 1 deletion crates/youki/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ fn main() -> Result<()> {
commands::checkpoint::checkpoint(checkpoint, root_path)
}
CommonCmd::Events(events) => commands::events::events(events, root_path),
CommonCmd::Exec(exec) => commands::exec::exec(exec, root_path),
CommonCmd::Exec(exec) => {
let exit_code = commands::exec::exec(exec, root_path)?;
std::process::exit(exit_code)
}
CommonCmd::List(list) => commands::list::list(list, root_path),
CommonCmd::Pause(pause) => commands::pause::pause(pause, root_path),
CommonCmd::Ps(ps) => commands::ps::ps(ps, root_path),
Expand Down

0 comments on commit 0d1ae45

Please sign in to comment.