Skip to content

Commit 834e9f2

Browse files
committed
winapi functions
1 parent 4107217 commit 834e9f2

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

crates/vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ features = [
128128
"Win32_System_Environment",
129129
"Win32_System_IO",
130130
"Win32_System_Ioctl",
131+
"Win32_System_JobObjects",
131132
"Win32_System_Kernel",
132133
"Win32_System_LibraryLoader",
133134
"Win32_System_Memory",

crates/vm/src/stdlib/_winapi.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,79 @@ mod _winapi {
615615
})
616616
}
617617

618+
#[pyfunction]
619+
fn CreateJobObject(
620+
_security_attributes: PyObjectRef,
621+
name: OptionalArg<Option<PyStrRef>>,
622+
vm: &VirtualMachine,
623+
) -> PyResult<WinHandle> {
624+
let handle = unsafe {
625+
match name.flatten() {
626+
Some(name) => {
627+
let name_wide = name.as_wtf8().to_wide_with_nul();
628+
windows_sys::Win32::System::JobObjects::CreateJobObjectW(
629+
null(),
630+
name_wide.as_ptr(),
631+
)
632+
}
633+
None => windows_sys::Win32::System::JobObjects::CreateJobObjectW(null(), null()),
634+
}
635+
};
636+
if handle.is_null() {
637+
return Err(vm.new_last_os_error());
638+
}
639+
Ok(WinHandle(handle))
640+
}
641+
642+
#[pyfunction]
643+
fn AssignProcessToJobObject(
644+
job: WinHandle,
645+
process: WinHandle,
646+
vm: &VirtualMachine,
647+
) -> PyResult<()> {
648+
let ret = unsafe {
649+
windows_sys::Win32::System::JobObjects::AssignProcessToJobObject(job.0, process.0)
650+
};
651+
if ret == 0 {
652+
return Err(vm.new_last_os_error());
653+
}
654+
Ok(())
655+
}
656+
657+
#[pyfunction]
658+
fn TerminateJobObject(job: WinHandle, exit_code: u32, vm: &VirtualMachine) -> PyResult<()> {
659+
let ret = unsafe {
660+
windows_sys::Win32::System::JobObjects::TerminateJobObject(job.0, exit_code)
661+
};
662+
if ret == 0 {
663+
return Err(vm.new_last_os_error());
664+
}
665+
Ok(())
666+
}
667+
668+
#[pyfunction]
669+
fn SetJobObjectKillOnClose(job: WinHandle, vm: &VirtualMachine) -> PyResult<()> {
670+
use windows_sys::Win32::System::JobObjects::{
671+
JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation,
672+
SetInformationJobObject,
673+
};
674+
let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { core::mem::zeroed() };
675+
info.BasicLimitInformation.LimitFlags =
676+
windows_sys::Win32::System::JobObjects::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
677+
let ret = unsafe {
678+
SetInformationJobObject(
679+
job.0,
680+
JobObjectExtendedLimitInformation,
681+
&info as *const _ as *const core::ffi::c_void,
682+
core::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
683+
)
684+
};
685+
if ret == 0 {
686+
return Err(vm.new_last_os_error());
687+
}
688+
Ok(())
689+
}
690+
618691
#[pyfunction]
619692
fn GetModuleFileName(handle: isize, vm: &VirtualMachine) -> PyResult<String> {
620693
let mut path: Vec<u16> = vec![0; MAX_PATH as usize];

0 commit comments

Comments
 (0)