From 46fb4ba41418d116eaaba03d8265f34b60531ab1 Mon Sep 17 00:00:00 2001 From: Kotaro Inoue Date: Mon, 17 Jun 2024 07:37:47 +0900 Subject: [PATCH] Use HashMap for envs in container_init_process (#2817) Signed-off-by: Kotaro Inoue --- .../src/process/container_init_process.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/crates/libcontainer/src/process/container_init_process.rs b/crates/libcontainer/src/process/container_init_process.rs index 912cf3f9..8f4494c1 100644 --- a/crates/libcontainer/src/process/container_init_process.rs +++ b/crates/libcontainer/src/process/container_init_process.rs @@ -279,7 +279,8 @@ pub fn container_init_process( let spec = &args.spec; let linux = spec.linux().as_ref().ok_or(MissingSpecError::Linux)?; let proc = spec.process().as_ref().ok_or(MissingSpecError::Process)?; - let mut envs: Vec = proc.env().as_ref().unwrap_or(&vec![]).clone(); + let mut envs: HashMap = + utils::parse_env(proc.env().as_ref().unwrap_or(&vec![])); let rootfs_path = &args.rootfs; let hooks = spec.hooks().as_ref(); let container = args.container.as_ref(); @@ -482,10 +483,8 @@ pub fn container_init_process( // LISTEN_FDS is 0, the variable should be unset, so we just ignore // it here, if it is 0. if listen_fds > 0 { - envs.append(&mut vec![ - format!("LISTEN_FDS={listen_fds}"), - "LISTEN_PID=1".to_string(), - ]); + envs.insert("LISTEN_FDS".to_owned(), listen_fds.to_string()); + envs.insert("LISTEN_PID".to_owned(), 1.to_string()); } args.preserve_fds + listen_fds @@ -560,17 +559,15 @@ pub fn container_init_process( })?; // add HOME into envs if not exists - let home_in_envs = envs.iter().any(|x| x.starts_with("HOME=")); - if !home_in_envs { + if !envs.contains_key("HOME") { if let Some(dir_home) = utils::get_user_home(proc.user().uid()) { - envs.push(format!("HOME={}", dir_home.to_string_lossy())); + envs.insert("HOME".to_owned(), dir_home.to_string_lossy().to_string()); } } // Reset the process env based on oci spec. env::vars().for_each(|(key, _value)| env::remove_var(key)); - utils::parse_env(&envs) - .iter() + envs.iter() .for_each(|(key, value)| env::set_var(key, value)); // Initialize seccomp profile right before we are ready to execute the