From 0013abb1c9483a88a836bb77bdb39c5e6b05ad16 Mon Sep 17 00:00:00 2001 From: Cherry <13651622+MolotovCherry@users.noreply.github.com> Date: Wed, 18 Sep 2024 07:53:56 -0700 Subject: [PATCH] Fixup comments + use lpApplicationName for simplification --- pueue/src/daemon/service.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/pueue/src/daemon/service.rs b/pueue/src/daemon/service.rs index e1c4aa08..f43dd404 100644 --- a/pueue/src/daemon/service.rs +++ b/pueue/src/daemon/service.rs @@ -593,16 +593,29 @@ impl Spawner { let arguments = arguments.join(" "); // Try to get the path to the current binary - let current_exe = env::current_exe()?.to_string_lossy().to_string(); + let mut current_exe = env::current_exe()? + .to_string_lossy() + .to_string() + .encode_utf16() + .chain(iter::once(0)) + .collect::>(); - let mut command = format!(r#""{current_exe}" {arguments}"#) + let mut arguments = arguments .encode_utf16() .chain(iter::once(0)) .collect::>(); - // lpcommandline may modify the the cmd vec. Max valid size is 1024, so - // I think it best to ensure 1024 bytes of capacity total exist just in case. - command.reserve(1024 - (command.len() / 2)); + // CreateProcessAsUserW's lpcommandline arg may modify the the cmd vec, so we need to account for this. + // Does "modify" mean we need extra room in the string? Not sure. But we can't afford to get this wrong, + // since it'd be UB otherwise. + // + // As per original docs (below), this may only add 1 extra character. But still not entirely sure. + // The system adds a null character to the command line string to separate the file name from the arguments. + // This divides the original string into two strings for internal processing. + // + // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessasuserw + // https://devblogs.microsoft.com/oldnewthing/20090601-00/?p=18083 + arguments.reserve(10); let env_block = EnvBlock::new(token.0)?; @@ -610,13 +623,13 @@ impl Spawner { unsafe { CreateProcessAsUserW( token.0, - None, - PWSTR(command.as_mut_ptr()), + PWSTR(current_exe.as_mut_ptr()), + PWSTR(arguments.as_mut_ptr()), None, None, false, - // unicode is required if we pass env block - // create_no_window causes all child processes to not show a visible console window + // CREATE_UNICODE_ENVIRONMENT is required if we pass env block. + // CREATE_NO_WINDOW causes all child processes to not show a visible console window. CREATE_UNICODE_ENVIRONMENT | CREATE_NO_WINDOW, Some(env_block.0), None,