mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 13:11:31 +00:00
utilize async processing for console and child exit events
This commit is contained in:
@ -25,6 +25,9 @@ features = ["process"]
|
||||
[dependencies.krata]
|
||||
path = "../shared"
|
||||
|
||||
[dependencies.xenevtchn]
|
||||
path = "../libs/xen/xenevtchn"
|
||||
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
|
||||
|
49
container/src/background.rs
Normal file
49
container/src/background.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::childwait::{ChildEvent, ChildWait};
|
||||
use anyhow::Result;
|
||||
use nix::{libc::c_int, unistd::Pid};
|
||||
use tokio::{select, time::sleep};
|
||||
|
||||
pub struct ContainerBackground {
|
||||
child: Pid,
|
||||
wait: ChildWait,
|
||||
}
|
||||
|
||||
impl ContainerBackground {
|
||||
pub async fn new(child: Pid) -> Result<ContainerBackground> {
|
||||
Ok(ContainerBackground {
|
||||
child,
|
||||
wait: ChildWait::new()?,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> Result<()> {
|
||||
loop {
|
||||
select! {
|
||||
event = self.wait.recv() => match event {
|
||||
Some(event) => self.child_event(event).await?,
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn child_event(&mut self, event: ChildEvent) -> Result<()> {
|
||||
if event.pid == self.child {
|
||||
self.death(event.status).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn death(&mut self, code: c_int) -> Result<()> {
|
||||
println!("[krata] container process exited: status = {}", code);
|
||||
println!("[krata] looping forever");
|
||||
loop {
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
}
|
84
container/src/childwait.rs
Normal file
84
container/src/childwait.rs
Normal file
@ -0,0 +1,84 @@
|
||||
use std::{
|
||||
ptr::addr_of_mut,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
thread::{self, JoinHandle},
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use log::warn;
|
||||
use nix::{
|
||||
libc::{c_int, wait},
|
||||
unistd::Pid,
|
||||
};
|
||||
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||
|
||||
const CHILD_WAIT_QUEUE_LEN: usize = 10;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct ChildEvent {
|
||||
pub pid: Pid,
|
||||
pub status: c_int,
|
||||
}
|
||||
|
||||
pub struct ChildWait {
|
||||
receiver: Receiver<ChildEvent>,
|
||||
signal: Arc<AtomicBool>,
|
||||
_task: JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl ChildWait {
|
||||
pub fn new() -> Result<ChildWait> {
|
||||
let (sender, receiver) = channel(CHILD_WAIT_QUEUE_LEN);
|
||||
let signal = Arc::new(AtomicBool::new(false));
|
||||
let mut processor = ChildWaitTask {
|
||||
sender,
|
||||
signal: signal.clone(),
|
||||
};
|
||||
let task = thread::spawn(move || {
|
||||
if let Err(error) = processor.process() {
|
||||
warn!("failed to process child updates: {}", error);
|
||||
}
|
||||
});
|
||||
Ok(ChildWait {
|
||||
receiver,
|
||||
signal,
|
||||
_task: task,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn recv(&mut self) -> Option<ChildEvent> {
|
||||
self.receiver.recv().await
|
||||
}
|
||||
}
|
||||
|
||||
struct ChildWaitTask {
|
||||
sender: Sender<ChildEvent>,
|
||||
signal: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl ChildWaitTask {
|
||||
fn process(&mut self) -> Result<()> {
|
||||
loop {
|
||||
let mut status: c_int = 0;
|
||||
let pid = unsafe { wait(addr_of_mut!(status)) };
|
||||
let event = ChildEvent {
|
||||
pid: Pid::from_raw(pid),
|
||||
status,
|
||||
};
|
||||
let _ = self.sender.try_send(event);
|
||||
|
||||
if self.signal.load(Ordering::Acquire) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ChildWait {
|
||||
fn drop(&mut self) {
|
||||
self.signal.store(true, Ordering::Release);
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ use futures::stream::TryStreamExt;
|
||||
use ipnetwork::IpNetwork;
|
||||
use krata::{LaunchInfo, LaunchNetwork};
|
||||
use log::{trace, warn};
|
||||
use nix::libc::{c_int, dup2, ioctl, wait};
|
||||
use nix::libc::{dup2, ioctl};
|
||||
use nix::unistd::{execve, fork, ForkResult, Pid};
|
||||
use oci_spec::image::{Config, ImageConfiguration};
|
||||
use std::ffi::{CStr, CString};
|
||||
@ -13,14 +13,13 @@ use std::os::fd::AsRawFd;
|
||||
use std::os::linux::fs::MetadataExt;
|
||||
use std::os::unix::fs::{chroot, PermissionsExt};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::ptr::addr_of_mut;
|
||||
use std::str::FromStr;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
use std::{fs, io};
|
||||
use sys_mount::{FilesystemType, Mount, MountFlags};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::background::ContainerBackground;
|
||||
|
||||
const IMAGE_BLOCK_DEVICE_PATH: &str = "/dev/xvda";
|
||||
const CONFIG_BLOCK_DEVICE_PATH: &str = "/dev/xvdb";
|
||||
|
||||
@ -84,7 +83,7 @@ impl ContainerInit {
|
||||
}
|
||||
|
||||
if let Some(cfg) = config.config() {
|
||||
self.run(cfg, &launch)?;
|
||||
self.run(cfg, &launch).await?;
|
||||
} else {
|
||||
return Err(anyhow!(
|
||||
"unable to determine what to execute, image config doesn't tell us"
|
||||
@ -367,7 +366,7 @@ impl ContainerInit {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run(&mut self, config: &Config, launch: &LaunchInfo) -> Result<()> {
|
||||
async fn run(&mut self, config: &Config, launch: &LaunchInfo) -> Result<()> {
|
||||
let mut cmd = match config.cmd() {
|
||||
None => vec![],
|
||||
Some(value) => value.clone(),
|
||||
@ -408,7 +407,7 @@ impl ContainerInit {
|
||||
}
|
||||
|
||||
std::env::set_current_dir(&working_dir)?;
|
||||
self.fork_and_exec(&path_cstr, cmd_cstr, env_cstr)?;
|
||||
self.fork_and_exec(&path_cstr, cmd_cstr, env_cstr).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -420,9 +419,14 @@ impl ContainerInit {
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
fn fork_and_exec(&mut self, path: &CStr, cmd: Vec<CString>, env: Vec<CString>) -> Result<()> {
|
||||
async fn fork_and_exec(
|
||||
&mut self,
|
||||
path: &CStr,
|
||||
cmd: Vec<CString>,
|
||||
env: Vec<CString>,
|
||||
) -> Result<()> {
|
||||
match unsafe { fork()? } {
|
||||
ForkResult::Parent { child } => self.background(child),
|
||||
ForkResult::Parent { child } => self.background(child).await,
|
||||
ForkResult::Child => {
|
||||
unsafe { nix::libc::setsid() };
|
||||
let result = unsafe { ioctl(io::stdin().as_raw_fd(), nix::libc::TIOCSCTTY, 0) };
|
||||
@ -435,21 +439,9 @@ impl ContainerInit {
|
||||
}
|
||||
}
|
||||
|
||||
fn background(&mut self, executed: Pid) -> Result<()> {
|
||||
loop {
|
||||
let mut status: c_int = 0;
|
||||
let pid = unsafe { wait(addr_of_mut!(status)) };
|
||||
if executed.as_raw() == pid {
|
||||
return self.death(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn death(&mut self, code: c_int) -> Result<()> {
|
||||
println!("[krata] container process exited: status = {}", code);
|
||||
println!("[krata] looping forever");
|
||||
loop {
|
||||
sleep(Duration::from_secs(1));
|
||||
}
|
||||
async fn background(&mut self, executed: Pid) -> Result<()> {
|
||||
let mut background = ContainerBackground::new(executed).await?;
|
||||
background.run().await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,3 @@
|
||||
pub mod background;
|
||||
pub mod childwait;
|
||||
pub mod init;
|
||||
|
Reference in New Issue
Block a user