From 69250cf85646e621450badccae33d256301907d9 Mon Sep 17 00:00:00 2001 From: xavo95 Date: Sun, 3 Nov 2024 14:21:23 +0100 Subject: [PATCH] Added support for auto process exit when child ends --- injector/src/lib.rs | 37 +++++++++++++++++++++++++++---------- launcher/src/main.rs | 19 +++++++++++-------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/injector/src/lib.rs b/injector/src/lib.rs index 6675f0f..c3ae874 100644 --- a/injector/src/lib.rs +++ b/injector/src/lib.rs @@ -8,14 +8,12 @@ use log::{debug, error, info, trace}; use path_clean::PathClean; use serde::{Deserialize, Serialize}; use windows::core::{PSTR, s}; -use windows::Win32::Foundation::{CloseHandle, GetLastError, HANDLE}; +use windows::Win32::Foundation::{CloseHandle, GetLastError, HANDLE, NTSTATUS}; use windows::Win32::System::Diagnostics::Debug::WriteProcessMemory; use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress}; -use windows::Win32::System::Memory::{MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, +use windows::Win32::System::Memory::{MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAllocEx, VirtualFreeEx}; -use windows::Win32::System::Threading::{CREATE_SUSPENDED, CreateProcessA, CreateRemoteThread, - PROCESS_INFORMATION, ResumeThread, STARTUPINFOA, - TerminateProcess, TerminateThread, WaitForSingleObject}; +use windows::Win32::System::Threading::{CREATE_SUSPENDED, CreateProcessA, CreateRemoteThread, GetExitCodeProcess, PROCESS_INFORMATION, ResumeThread, STARTUPINFOA, TerminateProcess, TerminateThread, WaitForSingleObject}; type FarProcUnwrapped = unsafe extern "system" fn() -> isize; type LPThreadStartRoutine = unsafe extern "system" fn(param: *mut c_void) -> u32; @@ -258,11 +256,30 @@ pub fn spawn_process<'a>(launcher: Launcher, env: Environment) -> Result bool { + let mut exit_code = 0u32; + let result = unsafe { GetExitCodeProcess(proc_info.hProcess, &mut exit_code) }; + match result { + Ok(_) => { + match NTSTATUS(exit_code as i32) { + windows::Win32::Foundation::STILL_ACTIVE => true, + _ => { + unsafe { + CloseHandle(proc_info.hThread).unwrap(); + CloseHandle(proc_info.hProcess).unwrap(); + } + false + } + } + } + Err(err) => { + error!("Error while getting exit result: {err:?}"); + true + } + } } \ No newline at end of file diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 91144b3..e9f7d64 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -1,5 +1,5 @@ use clap::Parser; -use log::{error, trace}; +use log::{error, info, trace}; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] @@ -72,8 +72,8 @@ fn run(config_file: String) -> Result { trace!("{:?}", configuration); Ok(injector::spawn_process( - configuration.launcher, - configuration.environment.unwrap_or_default() + configuration.launcher, + configuration.environment.unwrap_or_default(), )?) } @@ -82,13 +82,16 @@ fn main() { colog::default_builder() .filter_level(args.log_level.unwrap_or_default().into_level_filter()) .init(); - + match run(args.config_file) { - Ok(_context) => { - // Since environment pointer has to outlive the process, here we can do several things, + Ok(context) => { + // Since environment pointer has to outlive the process, here we can do several things, // either we join the thread of the process, or we just wait - std::thread::sleep(std::time::Duration::from_secs(u64::MAX)) - }, + while injector::is_process_running(context.proc_info) { + std::thread::sleep(std::time::Duration::from_secs(1)) + } + info!("Application exited"); + } Err(err) => error!("{}", err.to_string()) } } \ No newline at end of file