diff --git a/crates/shim/src/asynchronous/container.rs b/crates/shim/src/asynchronous/container.rs index 751f19bd..195c7c1c 100644 --- a/crates/shim/src/asynchronous/container.rs +++ b/crates/shim/src/asynchronous/container.rs @@ -49,6 +49,7 @@ pub trait Container { async fn update(&mut self, resources: &LinuxResources) -> Result<()>; async fn stats(&self) -> Result; async fn all_processes(&self) -> Result>; + async fn close_io(&mut self, exec_id: Option<&str>) -> Result<()>; } #[async_trait] @@ -182,6 +183,11 @@ where async fn all_processes(&self) -> Result> { self.init.ps().await } + + async fn close_io(&mut self, exec_id: Option<&str>) -> Result<()> { + let process = self.get_mut_process(exec_id)?; + process.close_io().await + } } impl ContainerTemplate diff --git a/crates/shim/src/asynchronous/processes.rs b/crates/shim/src/asynchronous/processes.rs index 048f78ca..22d771e9 100644 --- a/crates/shim/src/asynchronous/processes.rs +++ b/crates/shim/src/asynchronous/processes.rs @@ -24,6 +24,8 @@ use containerd_shim_protos::{ }; use oci_spec::runtime::LinuxResources; use time::OffsetDateTime; +use tokio::fs::File; +use tokio::sync::Mutex; use tokio::sync::oneshot::{channel, Receiver, Sender}; use crate::{io::Stdio, ioctl_set_winsz, util::asyncify, Console}; @@ -43,6 +45,7 @@ pub trait Process { async fn update(&mut self, resources: &LinuxResources) -> crate::Result<()>; async fn stats(&self) -> crate::Result; async fn ps(&self) -> crate::Result>; + async fn close_io(&mut self) -> crate::Result<()>; } #[async_trait] @@ -65,6 +68,7 @@ pub struct ProcessTemplate { pub wait_chan_tx: Vec>, pub console: Option, pub lifecycle: Arc, + pub stdin: Arc>>, } impl ProcessTemplate { @@ -79,6 +83,7 @@ impl ProcessTemplate { wait_chan_tx: vec![], console: None, lifecycle: Arc::new(lifecycle), + stdin: Arc::new(Mutex::new(None)), } } } @@ -176,4 +181,12 @@ where async fn ps(&self) -> crate::Result> { self.lifecycle.ps(self).await } + + async fn close_io(&mut self) -> crate::Result<()> { + let mut lock_guard = self.stdin.lock().await; + if let Some(stdin_w_file) = lock_guard.take() { + drop(stdin_w_file); + } + Ok(()) + } } diff --git a/crates/shim/src/asynchronous/task.rs b/crates/shim/src/asynchronous/task.rs index f2706911..50f0a2f9 100644 --- a/crates/shim/src/asynchronous/task.rs +++ b/crates/shim/src/asynchronous/task.rs @@ -269,8 +269,9 @@ where Ok(Empty::new()) } - async fn close_io(&self, _ctx: &TtrpcContext, _req: CloseIORequest) -> TtrpcResult { - // TODO call close_io of container + async fn close_io(&self, _ctx: &TtrpcContext, req: CloseIORequest) -> TtrpcResult { + let mut container = self.get_container(req.id()).await?; + container.close_io(req.exec_id().as_option()).await?; Ok(Empty::new()) }