Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
fix(toolchain): fix task manager compilation for windows (#530)
Browse files Browse the repository at this point in the history
Fixes RVTEE-657
  • Loading branch information
NathanFlurry committed Sep 30, 2024
1 parent e072ac6 commit e0d1325
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
3 changes: 2 additions & 1 deletion packages/backend-embed/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::*;
use include_dir::{include_dir, Dir, DirEntry};
use include_dir::{include_dir, Dir};
use std::path::PathBuf;
use tokio::fs;

Expand Down Expand Up @@ -29,6 +29,7 @@ pub async fn backend_dir(data_dir: &PathBuf) -> Result<PathBuf> {
/// bundling the vendored folders strips permissions, so executables can't be ran.
#[cfg(unix)]
async fn set_executables(dir: &Dir<'_>, fs_path: &PathBuf) -> Result<()> {
use include_dir::DirEntry;
use std::os::unix::fs::PermissionsExt;

for entry in dir.entries() {
Expand Down
2 changes: 1 addition & 1 deletion packages/toolchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ nix = { version = "0.27", default-features = false, features = ["user", "signal"
libproc = "0.14.8"

[target.'cfg(windows)'.dependencies]
windows = { version = "0.48", features = ["Win32_Foundation", "Win32_Security", "Win32_System_Diagnostics", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Threading", "Win32_System_Console"] }
windows = { version = "0.48", features = ["Win32_Foundation", "Win32_System_Diagnostics", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Threading", "Win32_System_Console", "Win32_System_ProcessStatus"] }

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/toolchain/src/util/process_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl ProcessManager {

#[cfg(windows)]
{
use windows::Win32::System::Threading::CREATE_NEW_PROCESS_GROUP;
use ::windows::Win32::System::Threading::CREATE_NEW_PROCESS_GROUP;
cmd.creation_flags(CREATE_NEW_PROCESS_GROUP.0);
}

Expand Down
42 changes: 21 additions & 21 deletions packages/toolchain/src/util/process_manager/windows.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use anyhow::*;
use windows::Win32::{
Foundation::{CloseHandle, HANDLE},
Foundation::{CloseHandle},
System::{
Console::{GenerateConsoleCtrlEvent, CTRL_BREAK_EVENT},
Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32,
TH32CS_SNAPPROCESS,
},
ProcessStatus::{EnumProcesses, K32EnumProcessModules, K32GetModuleFileNameExW},
Threading::{OpenProcess, TerminateProcess, PROCESS_TERMINATE},
},
};
Expand All @@ -24,20 +23,15 @@ pub fn send_terminate_signal(pid: u32) {
pub fn kill_process_tree(pid: u32) {
unsafe {
// Kill children
let snapshot = match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) {
Result::Ok(handle) => {
match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) {
Result::Ok(snapshot) => {
let mut process_entry = PROCESSENTRY32::default();
process_entry.dwSize = std::mem::size_of::<PROCESSENTRY32>() as u32;

if Process32First(snapshot, &mut process_entry).as_bool() {
loop {
if process_entry.th32ParentProcessID == pid {
if let Err(e) = kill_process_tree(process_entry.th32ProcessID) {
eprintln!(
"Failed to kill child process {}: {}",
process_entry.th32ProcessID, e
);
}
kill_process_tree(process_entry.th32ProcessID);
}
if !Process32Next(snapshot, &mut process_entry).as_bool() {
break;
Expand All @@ -50,20 +44,26 @@ pub fn kill_process_tree(pid: u32) {
Err(e) => {
eprintln!("Failed to create process snapshot: {}", e);
}
};
}

// Kill parent
let process_handle = OpenProcess(PROCESS_TERMINATE, false, pid);
if process_handle.is_invalid() {
if TerminateProcess(process_handle, 1).as_bool() {
CloseHandle(process_handle);
} else {
let error = std::io::Error::last_os_error();
CloseHandle(process_handle);
eprintln!("Failed to terminate process {}: {}", pid, error);
match OpenProcess(PROCESS_TERMINATE, false, pid) {
Result::Ok(process_handle) => {
if process_handle.is_invalid() {
if TerminateProcess(process_handle, 1).as_bool() {
CloseHandle(process_handle);
} else {
let error = std::io::Error::last_os_error();
CloseHandle(process_handle);
eprintln!("Failed to terminate process {}: {}", pid, error);
}
} else {
eprintln!("Failed to open process {}: Process may not exist", pid);
}
}
Err(err) => {
eprintln!("Failed to open process {}: {}", pid, err);
}
} else {
eprintln!("Failed to open process {}: Process may not exist", pid);
}
}
}

0 comments on commit e0d1325

Please sign in to comment.