char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
f4a0f5b
main
1use std:: io; 2use std:: os:: windows:: io::{ AsRawHandle , FromRawHandle , OwnedHandle , RawHandle }; 3use windows_sys:: Win32 :: System :: JobObjects :: * ; 4 5pub ( crate ) struct Job ( OwnedHandle ); 6 7impl Job { 8pub fn new ( flags : u32 ) -> io:: Result < Self > { 9let handle =unsafe { CreateJobObjectW ( std:: ptr:: null (), std:: ptr:: null ()) }; 10if handle. is_null () { 11return Err ( io:: Error :: last_os_error ()); 12} 13let job =Self ( unsafe { OwnedHandle :: from_raw_handle ( handle) }); 14let mut limits: JOBOBJECT_EXTENDED_LIMIT_INFORMATION =unsafe { std:: mem:: zeroed () }; 15 limits. BasicLimitInformation . LimitFlags =JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | flags; 16if unsafe { 17SetInformationJobObject ( 18 job. as_raw_handle (), 19JobObjectExtendedLimitInformation , 20( & raw const limits). cast (), 21 std:: mem:: size_of_val ( & limits) as u32 , 22) 23} ==0 24{ 25return Err ( io:: Error :: last_os_error ()); 26} 27Ok ( job) 28} 29 30pub fn alive ( & self ) -> io:: Result < bool > { 31let mut accounting: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION =unsafe { std:: mem:: zeroed () }; 32if unsafe { 33QueryInformationJobObject ( 34self . as_raw_handle (), 35JobObjectBasicAccountingInformation , 36( & raw mut accounting). cast (), 37 std:: mem:: size_of_val ( & accounting) as u32 , 38 std:: ptr:: null_mut (), 39) 40} ==0 41{ 42return Err ( io:: Error :: last_os_error ()); 43} 44Ok ( accounting. ActiveProcesses >0 ) 45} 46 47pub fn terminate ( & self ) -> io:: Result < bool > { 48if !self . alive () ?{ 49return Ok ( false ); 50} 51if unsafe { TerminateJobObject ( self . as_raw_handle (), 1 ) } ==0 { 52return Err ( io:: Error :: last_os_error ()); 53} 54Ok ( true ) 55} 56} 57 58impl AsRawHandle for Job { 59fn as_raw_handle ( & self ) ->RawHandle { 60self . 0 . as_raw_handle () 61} 62}