Skip to content

Commit

Permalink
Fixup comments + use lpApplicationName for simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Sep 18, 2024
1 parent bd9cc34 commit 0013abb
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions pueue/src/daemon/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,30 +593,43 @@ 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::<Vec<_>>();

let mut command = format!(r#""{current_exe}" {arguments}"#)
let mut arguments = arguments
.encode_utf16()
.chain(iter::once(0))
.collect::<Vec<_>>();

// 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)?;

let mut process_info = PROCESS_INFORMATION::default();
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,
Expand Down

0 comments on commit 0013abb

Please sign in to comment.