From 2be8661ffa4f626d5cdbf2cf1a8a5c384b9d1fa9 Mon Sep 17 00:00:00 2001 From: "fupan.lfp" Date: Mon, 20 Jan 2020 10:17:19 +0800 Subject: [PATCH] agent: fix the issue of missing restore process's cwd It should restore to it's previous cwd after it create container in which it would change it's cwd to container's bundle path. Fixes: #126 Signed-off-by: fupan.lfp --- src/agent/Cargo.toml | 1 + src/agent/src/grpc.rs | 9 ++++++--- src/agent/src/main.rs | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/agent/Cargo.toml b/src/agent/Cargo.toml index a13d9a94e..6f97b8042 100644 --- a/src/agent/Cargo.toml +++ b/src/agent/Cargo.toml @@ -20,6 +20,7 @@ prctl = "1.0.0" serde_json = "1.0.39" signal-hook = "0.1.9" scan_fmt = "0.2.3" +scopeguard = "1.0.0" regex = "1" # slog: # - Dynamic keys required to allow HashMap keys to be slog::Serialized. diff --git a/src/agent/src/grpc.rs b/src/agent/src/grpc.rs index 441b7120c..6c0ff713a 100644 --- a/src/agent/src/grpc.rs +++ b/src/agent/src/grpc.rs @@ -113,7 +113,9 @@ impl agentService { // write spec to bundle path, hooks might // read ocispec - setup_bundle(oci)?; + let olddir = setup_bundle(oci)?; + // restore the cwd for kata-agent process. + defer!(unistd::chdir(&olddir).unwrap()); let opts = CreateOpts { cgroup_name: "".to_string(), @@ -1738,7 +1740,7 @@ fn do_copy_file(req: &CopyFileRequest) -> Result<()> { Ok(()) } -fn setup_bundle(gspec: &Spec) -> Result<()> { +fn setup_bundle(gspec: &Spec) -> Result { if gspec.Root.is_none() { return Err(nix::Error::Sys(Errno::EINVAL).into()); } @@ -1757,7 +1759,8 @@ fn setup_bundle(gspec: &Spec) -> Result<()> { ); let _ = oci.save(config.as_str()); + let olddir = unistd::getcwd().chain_err(|| "cannot getcwd")?; unistd::chdir(bundle_path)?; - Ok(()) + Ok(olddir) } diff --git a/src/agent/src/main.rs b/src/agent/src/main.rs index a40892249..b8a54698f 100644 --- a/src/agent/src/main.rs +++ b/src/agent/src/main.rs @@ -20,6 +20,9 @@ extern crate signal_hook; extern crate scan_fmt; extern crate oci; +#[macro_use] +extern crate scopeguard; + #[macro_use] extern crate slog; extern crate slog_async;