Skip to content

Commit

Permalink
Move chdir to post start hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Yashodhan Joshi <[email protected]>
  • Loading branch information
YJDoc2 committed May 10, 2024
1 parent 524ae7a commit eb855ce
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions crates/libcontainer/src/container/container_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ impl Container {
err
})?;
if let Some(hooks) = config.hooks.as_ref() {
let original_dir = env::current_dir().map_err(|err| {
tracing::error!("failed to get current directory: {}", err);
LibcontainerError::Other(format!("failed to get current directory: {}", err))
})?;

unistd::chdir(self.root.as_os_str()).map_err(|err| {
tracing::error!("failed to change directory to container root: {}", err);
LibcontainerError::OtherSyscall(err)
})?;

// While prestart is marked as deprecated in the OCI spec, the docker and integration test still
// uses it.
#[allow(deprecated)]
Expand All @@ -69,11 +59,6 @@ impl Container {

err
})?;

unistd::chdir(original_dir.as_path()).map_err(|err| {
tracing::error!("failed to change directory to container root: {}", err);
LibcontainerError::OtherSyscall(err)
})?;
}

let mut notify_socket = NotifySocket::new(self.root.join(NOTIFY_FILE));
Expand All @@ -88,10 +73,25 @@ impl Container {
// Run post start hooks. It runs after the container process is started.
// It is called in the runtime namespace.
if let Some(hooks) = config.hooks.as_ref() {
let original_dir = env::current_dir().map_err(|err| {
tracing::error!("failed to get current directory: {}", err);
LibcontainerError::Other(format!("failed to get current directory: {}", err))
})?;
// before https://github.com/containers/youki/pull/2780 we used to do a chdir outside
// and to not break the behaviour, we do it here
unistd::chdir(self.root.as_os_str()).map_err(|err| {
tracing::error!("failed to change directory to container root: {}", err);
LibcontainerError::OtherSyscall(err)
})?;

hooks::run_hooks(hooks.poststart().as_ref(), Some(self)).map_err(|err| {
tracing::error!("failed to run post start hooks: {}", err);
err
})?;
unistd::chdir(original_dir.as_path()).map_err(|err| {
tracing::error!("failed to change directory to container root: {}", err);
LibcontainerError::OtherSyscall(err)
})?;
}

Ok(())
Expand Down

0 comments on commit eb855ce

Please sign in to comment.