Skip to content

Commit ec1c9f9

Browse files
coolreader18youknowone
authored andcommitted
Add os.kill
1 parent 26bcb1f commit ec1c9f9

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ schannel = "0.1"
122122

123123
[target.'cfg(windows)'.dependencies.winapi]
124124
version = "0.3"
125-
features = ["winsock2", "handleapi", "ws2def", "std", "winbase", "wincrypt", "fileapi"]
125+
features = ["winsock2", "handleapi", "ws2def", "std", "winbase", "wincrypt", "fileapi", "wincon"]
126126

127127
[target.'cfg(target_arch = "wasm32")'.dependencies]
128128
wasm-bindgen = "0.2"

vm/src/stdlib/os.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,43 @@ fn os_wait(vm: &VirtualMachine) -> PyResult<(libc::pid_t, i32)> {
18231823
os_waitpid(-1, 0, vm)
18241824
}
18251825

1826+
fn os_kill(pid: i32, sig: isize, vm: &VirtualMachine) -> PyResult<()> {
1827+
#[cfg(unix)]
1828+
{
1829+
let ret = unsafe { libc::kill(pid, sig as i32) };
1830+
if ret == -1 {
1831+
Err(errno_err(vm))
1832+
} else {
1833+
Ok(())
1834+
}
1835+
}
1836+
#[cfg(windows)]
1837+
{
1838+
use winapi::um::{handleapi, processthreadsapi, wincon, winnt};
1839+
let sig = sig as u32;
1840+
let pid = pid as u32;
1841+
1842+
if sig == wincon::CTRL_C_EVENT || sig == wincon::CTRL_BREAK_EVENT {
1843+
let ret = unsafe { wincon::GenerateConsoleCtrlEvent(sig, pid) };
1844+
let res = if ret == 0 { Err(errno_err(vm)) } else { Ok(()) };
1845+
return res;
1846+
}
1847+
1848+
let h = unsafe { processthreadsapi::OpenProcess(winnt::PROCESS_ALL_ACCESS, 0, pid) };
1849+
if h.is_null() {
1850+
return Err(errno_err(vm));
1851+
}
1852+
let ret = unsafe { processthreadsapi::TerminateProcess(h, sig) };
1853+
let res = if ret == 0 { Err(errno_err(vm)) } else { Ok(()) };
1854+
unsafe { handleapi::CloseHandle(h) };
1855+
res
1856+
}
1857+
#[cfg(not(any(unix, windows)))]
1858+
{
1859+
unimplemented!()
1860+
}
1861+
}
1862+
18261863
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
18271864
let ctx = &vm.ctx;
18281865

@@ -1932,6 +1969,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
19321969
"lseek" => ctx.new_function(os_lseek),
19331970
"set_inheritable" => ctx.new_function(os_set_inheritable),
19341971
"link" => ctx.new_function(os_link),
1972+
"kill" => ctx.new_function(os_kill),
19351973

19361974
"O_RDONLY" => ctx.new_int(libc::O_RDONLY),
19371975
"O_WRONLY" => ctx.new_int(libc::O_WRONLY),

0 commit comments

Comments
 (0)