FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: signal.SIGINT and signal() raise NotImplementedError on wasi by jiwahn · Pull Request #8986 · RustPython/RustPython · GitHub

Repository navigation

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .rs  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Group WASI signal definitions in mod wasm
Move WASI-only types, constants, FFI, and handlers into
`mod wasm` and re-export them with `#[cfg(target_os = "wasi")]`.

Assisted-by: Grok 4.6
  • Loading branch information
youknowone committed Oct 7, 2026
commit 26eb85b15f2007bc96b77762351e1c6f44ab9140
118 changes: 58 additions & 60 deletions crates/host_env/src/signal.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(any(unix, windows))]
use std::io;
#[cfg(windows)]
use std::sync::Once;
Expand Down Expand Up @@ -27,36 +28,69 @@ pub const SIG_ERR: libc::sighandler_t = -1 as _;
/// process's own code calls `raise()`, never from an outside interrupt.
/// Numbering matches wasi-libc's `bits/signal.h`, the same values CPython's
/// WASI build gets by linking the same library.
#[allow(non_camel_case_types)]
#[cfg(target_os = "wasi")]
pub type sighandler_t = usize;
mod wasm {
use std::io;

#[cfg(target_os = "wasi")]
pub const SIG_DFL: sighandler_t = 0;
#[cfg(target_os = "wasi")]
pub const SIG_IGN: sighandler_t = 1;
#[cfg(target_os = "wasi")]
pub const SIG_ERR: sighandler_t = -1isize as usize;
#[allow(non_camel_case_types)]
pub type sighandler_t = usize;

#[cfg(target_os = "wasi")]
pub const SIGINT: i32 = 2;
#[cfg(target_os = "wasi")]
pub const SIGILL: i32 = 4;
#[cfg(target_os = "wasi")]
pub const SIGABRT: i32 = 6;
#[cfg(target_os = "wasi")]
pub const SIGFPE: i32 = 8;
#[cfg(target_os = "wasi")]
pub const SIGSEGV: i32 = 11;
#[cfg(target_os = "wasi")]
pub const SIGTERM: i32 = 15;
pub const SIG_DFL: sighandler_t = 0;
pub const SIG_IGN: sighandler_t = 1;
pub const SIG_ERR: sighandler_t = -1isize as usize;

#[cfg(target_os = "wasi")]
unsafe extern "C" {
fn signal(signum: i32, handler: sighandler_t) -> sighandler_t;
fn raise(signum: i32) -> i32;
pub const SIGINT: i32 = 2;
pub const SIGILL: i32 = 4;
pub const SIGABRT: i32 = 6;
pub const SIGFPE: i32 = 8;
pub const SIGSEGV: i32 = 11;
pub const SIGTERM: i32 = 15;

unsafe extern "C" {
fn signal(signum: i32, handler: sighandler_t) -> sighandler_t;
fn raise(signum: i32) -> i32;
}

/// # Safety
///
/// The caller must ensure `signalnum` is a valid platform signal number.
pub unsafe fn probe_handler(signalnum: i32) -> Option<sighandler_t> {
let handler = unsafe { signal(signalnum, SIG_IGN) };
if handler == SIG_ERR {
None
} else {
unsafe { signal(signalnum, handler) };
Some(handler)
}
}

/// # Safety
///
/// The caller must ensure `signalnum` is a valid platform signal number and
/// `handler` is accepted by the platform signal ABI.
pub unsafe fn install_handler(
signalnum: i32,
handler: sighandler_t,
) -> io::Result<sighandler_t> {
let old = unsafe { signal(signalnum, handler) };
if old == SIG_ERR {
return Err(io::Error::last_os_error());
}
Ok(old)
}

pub fn raise_signal(signalnum: i32) -> io::Result<()> {
if unsafe { raise(signalnum) } == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}
}

#[cfg(target_os = "wasi")]
pub use wasm::*;

#[cfg(unix)]
pub use libc::{SIG_BLOCK, SIG_SETMASK, SIG_UNBLOCK};

Expand Down Expand Up @@ -170,42 +204,6 @@ pub fn raise_signal(signalnum: i32) -> io::Result<()> {
unsafe { libc::raise(signalnum) }.check_libc_zero()
}

/// # Safety
///
/// The caller must ensure `signalnum` is a valid platform signal number.
#[cfg(target_os = "wasi")]
pub unsafe fn probe_handler(signalnum: i32) -> Option<sighandler_t> {
let handler = unsafe { signal(signalnum, SIG_IGN) };
if handler == SIG_ERR {
None
} else {
unsafe { signal(signalnum, handler) };
Some(handler)
}
}

/// # Safety
///
/// The caller must ensure `signalnum` is a valid platform signal number and
/// `handler` is accepted by the platform signal ABI.
#[cfg(target_os = "wasi")]
pub unsafe fn install_handler(signalnum: i32, handler: sighandler_t) -> io::Result<sighandler_t> {
let old = unsafe { signal(signalnum, handler) };
if old == SIG_ERR {
return Err(io::Error::last_os_error());
}
Ok(old)
}

#[cfg(target_os = "wasi")]
pub fn raise_signal(signalnum: i32) -> io::Result<()> {
if unsafe { raise(signalnum) } == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}

#[cfg(unix)]
pub fn alarm(seconds: u32) -> u32 {
unsafe { libc::alarm(seconds) }
Expand Down
Loading

Back | FazBrowse Home | New Git URL