From 38324cda7d84aac8c5172a4fa46730a6cfbbdb57 Mon Sep 17 00:00:00 2001 From: utam0k Date: Thu, 29 Jul 2021 23:07:33 +0900 Subject: [PATCH] reduce the number of unnecessary clones and arguments by changing the order in which NotifyListeners are generated. --- src/commands/start.rs | 3 ++- src/container/builder_impl.rs | 12 +++++------- src/process/child.rs | 10 +++++++++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/commands/start.rs b/src/commands/start.rs index b327e63e0..08d42b114 100644 --- a/src/commands/start.rs +++ b/src/commands/start.rs @@ -16,8 +16,9 @@ pub struct Start { impl Start { pub fn new(container_id: String) -> Self { - return Self { container_id }; + Self { container_id } } + pub fn exec(&self, root_path: PathBuf) -> Result<()> { let container_root = root_path.join(&self.container_id); if !container_root.exists() { diff --git a/src/container/builder_impl.rs b/src/container/builder_impl.rs index a96588100..51809135b 100644 --- a/src/container/builder_impl.rs +++ b/src/container/builder_impl.rs @@ -66,7 +66,10 @@ impl ContainerBuilderImpl { // create the parent and child process structure so the parent and child process can sync with each other let (mut parent, parent_channel) = parent::ParentProcess::new(self.rootless.clone())?; - let mut child = child::ChildProcess::new(parent_channel)?; + // need to create the notify socket before we pivot root, since the unix + // domain socket used here is outside of the rootfs of container + let notify: NotifyListener = NotifyListener::new(&self.notify_path)?; + let mut child = child::ChildProcess::new(parent_channel, notify)?; let cb = Box::new(|| { if let Err(error) = container_init( @@ -76,7 +79,6 @@ impl ContainerBuilderImpl { self.syscall.clone(), self.rootfs.clone(), self.console_socket.clone(), - self.notify_path.clone(), &mut child, ) { log::debug!("failed to run container_init: {:?}", error); @@ -121,14 +123,10 @@ fn container_init( command: LinuxSyscall, rootfs: PathBuf, console_socket: Option, - notify_name: PathBuf, child: &mut child::ChildProcess, ) -> Result<()> { let linux = &spec.linux; let namespaces: Namespaces = linux.namespaces.clone().into(); - // need to create the notify socket before we pivot root, since the unix - // domain socket used here is outside of the rootfs of container - let mut notify_socket: NotifyListener = NotifyListener::new(¬ify_name)?; let proc = &spec.process; // if Out-of-memory score adjustment is set in specification. set the score @@ -204,7 +202,7 @@ fn container_init( child.notify_parent()?; // listing on the notify socket for container start command - notify_socket.wait_for_container_start()?; + child.wait_for_container_start()?; let args: &Vec = &spec.process.args; let envs: &Vec = &spec.process.env; diff --git a/src/process/child.rs b/src/process/child.rs index eb790226d..094683b81 100644 --- a/src/process/child.rs +++ b/src/process/child.rs @@ -4,6 +4,8 @@ use mio::unix::pipe::Receiver; use mio::unix::pipe::Sender; use mio::{Interest, Poll, Token}; +use crate::notify_socket::NotifyListener; + use super::parent::ParentChannel; // Token is used to identify which socket generated an event @@ -15,6 +17,7 @@ pub struct ChildProcess { parent_channel: ParentChannel, receiver: Option, poll: Option, + notify: NotifyListener, } // Note : The original youki process first forks into 'parent' (P) and 'child' (C1) process @@ -23,11 +26,12 @@ pub struct ChildProcess { // a process point of view, init process is child of child process, which is child of original youki process. impl ChildProcess { /// create a new Child process structure - pub fn new(parent_channel: ParentChannel) -> Result { + pub fn new(parent_channel: ParentChannel, notify: NotifyListener) -> Result { Ok(Self { parent_channel, receiver: None, poll: None, + notify, }) } @@ -62,4 +66,8 @@ impl ChildProcess { self.parent_channel.wait_for_mapping_ack()?; Ok(()) } + + pub fn wait_for_container_start(&mut self) -> Result<()> { + self.notify.wait_for_container_start() + } }