From acfd712efcf4fbd07bed5bcc7c38e64fd389816d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 6 Aug 2019 14:11:49 -0700 Subject: [PATCH] pty: fix passing env to windows child processes --- pty/src/cmdbuilder.rs | 36 ++++++++++++++++++++++++++++-------- pty/src/win/conpty.rs | 6 +++--- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/pty/src/cmdbuilder.rs b/pty/src/cmdbuilder.rs index c61568155..e5ef5714b 100644 --- a/pty/src/cmdbuilder.rs +++ b/pty/src/cmdbuilder.rs @@ -1,7 +1,5 @@ #[cfg(windows)] use failure::{ensure, Error}; -#[cfg(windows)] -use log::error; #[cfg(feature = "serde_support")] use serde_derive::*; use std::ffi::{OsStr, OsString}; @@ -51,12 +49,6 @@ impl CommandBuilder { { self.envs .push((key.as_ref().to_owned(), val.as_ref().to_owned())); - #[cfg(windows)] - error!( - "ignoring env {:?}={:?} for child; FIXME: implement this!", - key.as_ref(), - val.as_ref() - ); } #[cfg(feature = "ssh")] @@ -125,6 +117,34 @@ impl CommandBuilder { exe.to_owned() } + /// Constructs an environment block for this spawn attempt. + /// Uses the current process environment as the base and then + /// adds/replaces the environment that was specified via the + /// `env` methods. + pub(crate) fn environment_block(&self) -> Vec { + // Take the current environment as the base + let mut env_hash: std::collections::HashMap<_, _> = std::env::vars_os().collect(); + + // override with the specified values + for (k, v) in &self.envs { + env_hash.insert(k.to_owned(), v.to_owned()); + } + + // and now encode it as wide characters + let mut block = vec![]; + + for (k, v) in env_hash { + block.extend(k.encode_wide()); + block.push(b'=' as u16); + block.extend(v.encode_wide()); + block.push(0); + } + // and a final terminator for CreateProcessW + block.push(0); + + block + } + pub(crate) fn cmdline(&self) -> Result<(Vec, Vec), Error> { let mut cmdline = Vec::::new(); diff --git a/pty/src/win/conpty.rs b/pty/src/win/conpty.rs index ffdccba35..c713d5878 100644 --- a/pty/src/win/conpty.rs +++ b/pty/src/win/conpty.rs @@ -18,8 +18,8 @@ use winapi::shared::minwindef::DWORD; use winapi::shared::winerror::{HRESULT, S_OK}; use winapi::um::handleapi::*; use winapi::um::processthreadsapi::*; -use winapi::um::winbase::EXTENDED_STARTUPINFO_PRESENT; use winapi::um::winbase::STARTUPINFOEXW; +use winapi::um::winbase::{CREATE_UNICODE_ENVIRONMENT, EXTENDED_STARTUPINFO_PRESENT}; use winapi::um::wincon::COORD; const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE: usize = 0x00020016; @@ -270,8 +270,8 @@ impl SlavePty for ConPtySlavePty { ptr::null_mut(), ptr::null_mut(), 0, - EXTENDED_STARTUPINFO_PRESENT, - ptr::null_mut(), // FIXME: env + EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, + cmd.environment_block().as_mut_slice().as_mut_ptr() as *mut _, ptr::null_mut(), &mut si.StartupInfo, &mut pi,