|
| 1 | +use std::ops::Deref; |
| 2 | + |
| 3 | +use sysinfo::Pid; |
| 4 | +use windows::Win32::Foundation::{ |
| 5 | + CloseHandle, |
| 6 | + HANDLE, |
| 7 | +}; |
| 8 | +use windows::Win32::System::Threading::{ |
| 9 | + OpenProcess, |
| 10 | + PROCESS_TERMINATE, |
| 11 | + TerminateProcess, |
| 12 | +}; |
| 13 | + |
| 14 | +/// Terminate a process on Windows using the Windows API |
| 15 | +pub fn terminate_process(pid: Pid) -> Result<(), String> { |
| 16 | + unsafe { |
| 17 | + // Open the process with termination rights |
| 18 | + let handle = OpenProcess(PROCESS_TERMINATE, false, pid.as_u32()) |
| 19 | + .map_err(|e| format!("Failed to open process: {}", e))?; |
| 20 | + |
| 21 | + // Create a safe handle that will be closed automatically when dropped |
| 22 | + let safe_handle = SafeHandle::new(handle).ok_or_else(|| "Invalid process handle".to_string())?; |
| 23 | + |
| 24 | + // Terminate the process with exit code 1 |
| 25 | + TerminateProcess(*safe_handle, 1).map_err(|e| format!("Failed to terminate process: {}", e))?; |
| 26 | + |
| 27 | + Ok(()) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +struct SafeHandle(HANDLE); |
| 32 | + |
| 33 | +impl SafeHandle { |
| 34 | + fn new(handle: HANDLE) -> Option<Self> { |
| 35 | + if !handle.is_invalid() { Some(Self(handle)) } else { None } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl Drop for SafeHandle { |
| 40 | + fn drop(&mut self) { |
| 41 | + unsafe { |
| 42 | + let _ = CloseHandle(self.0); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl Deref for SafeHandle { |
| 48 | + type Target = HANDLE; |
| 49 | + |
| 50 | + fn deref(&self) -> &Self::Target { |
| 51 | + &self.0 |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use std::process::Command; |
| 58 | + use std::time::Duration; |
| 59 | + |
| 60 | + use super::*; |
| 61 | + |
| 62 | + // Helper to create a long-running process for testing |
| 63 | + fn spawn_test_process() -> std::process::Child { |
| 64 | + let mut command = Command::new("cmd"); |
| 65 | + command.args(["/C", "timeout 30 > nul"]); |
| 66 | + command.spawn().expect("Failed to spawn test process") |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_terminate_process() { |
| 71 | + // Spawn a test process |
| 72 | + let mut child = spawn_test_process(); |
| 73 | + let pid = Pid::from_u32(child.id()); |
| 74 | + |
| 75 | + // Terminate the process |
| 76 | + let result = terminate_process(pid); |
| 77 | + |
| 78 | + // Verify termination was successful |
| 79 | + assert!(result.is_ok(), "Process termination failed: {:?}", result.err()); |
| 80 | + |
| 81 | + // Give it a moment to terminate |
| 82 | + std::thread::sleep(Duration::from_millis(100)); |
| 83 | + |
| 84 | + // Verify the process is actually terminated |
| 85 | + match child.try_wait() { |
| 86 | + Ok(Some(_)) => { |
| 87 | + // Process exited, which is what we expect |
| 88 | + }, |
| 89 | + Ok(None) => { |
| 90 | + panic!("Process is still running after termination"); |
| 91 | + }, |
| 92 | + Err(e) => { |
| 93 | + panic!("Error checking process status: {}", e); |
| 94 | + }, |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_terminate_nonexistent_process() { |
| 100 | + // Use a likely invalid PID |
| 101 | + let invalid_pid = Pid::from_u32(u32::MAX - 1); |
| 102 | + |
| 103 | + // Attempt to terminate a non-existent process |
| 104 | + let result = terminate_process(invalid_pid); |
| 105 | + |
| 106 | + // Should return an error |
| 107 | + assert!(result.is_err(), "Terminating non-existent process should fail"); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test_safe_handle() { |
| 112 | + // Test creating a SafeHandle with an invalid handle |
| 113 | + let invalid_handle = HANDLE(std::ptr::null_mut()); |
| 114 | + let safe_handle = SafeHandle::new(invalid_handle); |
| 115 | + assert!(safe_handle.is_none(), "SafeHandle should be None for invalid handle"); |
| 116 | + |
| 117 | + // We can't easily test a valid handle without actually opening a process, |
| 118 | + // which would require additional setup and teardown |
| 119 | + } |
| 120 | +} |
0 commit comments