From d2b1161c72d36d8ea3068620100e86518bcb0d61 Mon Sep 17 00:00:00 2001 From: Stefan Schindler Date: Tue, 25 Aug 2020 18:21:59 +0200 Subject: [PATCH 1/4] CI for Windows --- .gitlab-ci.yml | 14 +++++++++ Cargo.toml | 2 +- src/main.rs | 80 -------------------------------------------------- src/windows.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 81 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2625019..47560c7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -22,3 +22,17 @@ test:cargo: script: - rustc --version && cargo --version # Print version info for debugging - cargo test --all + +test:windows: + script: + - rustup target add x86_64-pc-windows-gnu + - rustc --version && cargo --version + - | + set -x + apt update -qq + apt install -yqq mingw-w64-x86-64-dev + cat <<-EOF >> $CARGO_HOME/config + [target.x86_64-pc-windows-gnu] + linker = "$GCC-gcc" + EOF + - cargo check --target=x86_64-pc-windows-gnu diff --git a/Cargo.toml b/Cargo.toml index f1ee8da..662cbca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] log = "0.4" simple_logger = "1.3" -sudo = "0.3.1" +sudo = "0.5" #num_cpus = "1.13.0" diff --git a/src/main.rs b/src/main.rs index 77ab460..d82d2f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,83 +101,3 @@ pub struct CoreId(u8); #[derive(Debug, Clone, PartialEq)] pub struct Pid(u32); - -#[cfg(windows)] -impl Pid { - //use super::{Pid, K32EnumProcesses, DWORD, size_of}; - - const N_PIDS: usize = 4096; - - pub fn get_system_pids() -> Result, &'static str> { - let mut pids = vec![Pid(0); Self::N_PIDS]; - let mut found_processes: DWORD = 0; - - // https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocesses - if 0 == unsafe { - K32EnumProcesses( - pids.as_mut_ptr() as *mut u32, - (Self::N_PIDS * size_of::()) as u32, - &mut found_processes, - ) - } { - return Err("unable to EnumProcesses"); - } - let n_pids = found_processes as usize / size_of::(); - - pids.truncate(n_pids); - - Ok(pids) - } - - pub fn process_handle(self) -> Result { - use winapi::um::winnt::{ - PROCESS_QUERY_INFORMATION, PROCESS_SET_INFORMATION, PROCESS_VM_READ, PROCESS_VM_WRITE, - }; - // https://docs.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights - let desired_access = PROCESS_VM_READ | PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION; - let inherit_handle = 0; - - // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess - let handle: HANDLE = unsafe { OpenProcess(desired_access, inherit_handle, self.0) }; - if handle.is_null() { - return Err("unable to OpenProcess"); - } - - let mut module_ptr: HMODULE = null_mut(); - let mut module_dyn_needed: DWORD = 0; - let filter_flag = winapi::um::psapi::LIST_MODULES_ALL; - if 0 == unsafe { - K32EnumProcessModulesEx( - handle, - &mut module_ptr as *mut _ as *mut _, - size_of::() as u32, - &mut module_dyn_needed, - filter_flag, - ) - } { - return Err("unable to EnumProcessModulesEx"); - } - - let mut name: Vec = vec![0; winapi::shared::minwindef::MAX_PATH]; - // https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulebasenamew - let len_name = unsafe { - K32GetModuleBaseNameW( - handle, - module_ptr, - name.as_mut_ptr(), - (name.len() / size_of::()) as u32, - ) - }; - if len_name == 0 { - return Err("unable to K32GetModuleBaseNameW"); - } - - let name = String::from_utf16_lossy(&name[0..len_name as usize]); - - Ok(ProcessHandle { - pid: self, - handle, - name, - }) - } -} diff --git a/src/windows.rs b/src/windows.rs index a8795a3..ab2ade2 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -27,6 +27,86 @@ impl Drop for ProcessHandle { } } +#[cfg(windows)] +impl Pid { + //use super::{Pid, K32EnumProcesses, DWORD, size_of}; + + const N_PIDS: usize = 4096; + + pub fn get_system_pids() -> Result, &'static str> { + let mut pids = vec![Pid(0); Self::N_PIDS]; + let mut found_processes: DWORD = 0; + + // https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocesses + if 0 == unsafe { + K32EnumProcesses( + pids.as_mut_ptr() as *mut u32, + (Self::N_PIDS * size_of::()) as u32, + &mut found_processes, + ) + } { + return Err("unable to EnumProcesses"); + } + let n_pids = found_processes as usize / size_of::(); + + pids.truncate(n_pids); + + Ok(pids) + } + + pub fn process_handle(self) -> Result { + use winapi::um::winnt::{ + PROCESS_QUERY_INFORMATION, PROCESS_SET_INFORMATION, PROCESS_VM_READ, PROCESS_VM_WRITE, + }; + // https://docs.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights + let desired_access = PROCESS_VM_READ | PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION; + let inherit_handle = 0; + + // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess + let handle: HANDLE = unsafe { OpenProcess(desired_access, inherit_handle, self.0) }; + if handle.is_null() { + return Err("unable to OpenProcess"); + } + + let mut module_ptr: HMODULE = null_mut(); + let mut module_dyn_needed: DWORD = 0; + let filter_flag = winapi::um::psapi::LIST_MODULES_ALL; + if 0 == unsafe { + K32EnumProcessModulesEx( + handle, + &mut module_ptr as *mut _ as *mut _, + size_of::() as u32, + &mut module_dyn_needed, + filter_flag, + ) + } { + return Err("unable to EnumProcessModulesEx"); + } + + let mut name: Vec = vec![0; winapi::shared::minwindef::MAX_PATH]; + // https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulebasenamew + let len_name = unsafe { + K32GetModuleBaseNameW( + handle, + module_ptr, + name.as_mut_ptr(), + (name.len() / size_of::()) as u32, + ) + }; + if len_name == 0 { + return Err("unable to K32GetModuleBaseNameW"); + } + + let name = String::from_utf16_lossy(&name[0..len_name as usize]); + + Ok(ProcessHandle { + pid: self, + handle, + name, + }) + } +} + pub fn get_cpu_info() -> Result { let mut numa_node_count: ULONG = 0xffffffff; // https://docs.microsoft.com/en-us/windows/win32/api/systemtopologyapi/nf-systemtopologyapi-getnumahighestnodenumber -- GitLab From a2a632925eed389781399d2920beb2428357e99d Mon Sep 17 00:00:00 2001 From: Stefan Schindler Date: Tue, 25 Aug 2020 18:28:29 +0200 Subject: [PATCH 2/4] typo --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index d82d2f1..af23687 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ mod unix; use unix::{get_cpu_info, set_process_affinity}; */ -#[cfg(winows)] +#[cfg(windows)] #[path = "windows.rs"] mod plattform; -- GitLab From 7e0a73a62ed87a9ade110b6b2f050c55e7aebff2 Mon Sep 17 00:00:00 2001 From: Stefan Schindler Date: Tue, 25 Aug 2020 18:39:20 +0200 Subject: [PATCH 3/4] Generic Param --- .gitlab-ci.yml | 2 +- src/windows.rs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 47560c7..c6b275a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -35,4 +35,4 @@ test:windows: [target.x86_64-pc-windows-gnu] linker = "$GCC-gcc" EOF - - cargo check --target=x86_64-pc-windows-gnu + - cargo test --target=x86_64-pc-windows-gnu diff --git a/src/windows.rs b/src/windows.rs index ab2ade2..9d23e67 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,3 +1,5 @@ +use crate::{CoreId, CpuInfo, Pid, ProcessHandle}; + //use winapi::um::winnt::SystemPowerInformation; use std::mem::size_of; // buffer/struct size use std::ptr::null_mut; // NULL @@ -21,7 +23,7 @@ pub struct ProcessHandle { } */ -impl Drop for ProcessHandle { +impl Drop for ProcessHandle { fn drop(&mut self) { unsafe { CloseHandle(self.handle) }; } @@ -54,7 +56,7 @@ impl Pid { Ok(pids) } - pub fn process_handle(self) -> Result { + pub fn process_handle(self) -> Result, &'static str> { use winapi::um::winnt::{ PROCESS_QUERY_INFORMATION, PROCESS_SET_INFORMATION, PROCESS_VM_READ, PROCESS_VM_WRITE, }; @@ -169,7 +171,7 @@ pub fn set_process_affinity(name: &str, cores: &Vec, all_cores: &Vec> = Box::new( + let mut handles: Box>> = Box::new( pids.unwrap() .into_iter() .map(Pid::process_handle) -- GitLab From 36552e0509f765a7739abd2340b60914f360a630 Mon Sep 17 00:00:00 2001 From: Stefan Schindler Date: Tue, 25 Aug 2020 18:51:46 +0200 Subject: [PATCH 4/4] Move Drop fro ProcessHandle --- src/main.rs | 7 +++++++ src/windows.rs | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index af23687..549c9e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,6 +85,13 @@ pub struct ProcessHandle { name: String, } +#[cfg(windows)] +impl Drop for ProcessHandle { + fn drop(&mut self) { + unsafe { winapi::um::handleapi::CloseHandle(self.handle) }; + } +} + #[derive(Debug)] pub struct CpuInfo { all_system_cores: Vec, diff --git a/src/windows.rs b/src/windows.rs index 9d23e67..6b36c19 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -21,15 +21,14 @@ pub struct ProcessHandle { handle: HANDLE, name: String, } -*/ impl Drop for ProcessHandle { fn drop(&mut self) { unsafe { CloseHandle(self.handle) }; } } +*/ -#[cfg(windows)] impl Pid { //use super::{Pid, K32EnumProcesses, DWORD, size_of}; -- GitLab