You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add an implementation of sigaction for libc backend #628
I wanted to try to reimplement signal-hook using rustix primitives; see vorner/signal-hook#142. However, to do this we would need sigaction and other signal manipulating tools to exist on the libc backend as well.
Ah, interesting. This is tricky, because sigaction is not a normal syscall. libc implementations of sigaction on Linux do more than just call Linux's rt_sigaction syscall. They have access to libc internals and do special things for threads.
Also, everything in rustix's public (non-runtime) interface can coexist with a libc, but libc's generally assume they're in full control of sigaction kinds of syscalls. Invoking the rt_sigaction syscall without going through libc could break libc assumptions.
It's possible we can figure something out here, but it'll take some investigation.
At a quick glance, it seems musl is doing these things:
If the signal number is one of the signals libc internally claims for its own purposes, fail
The signal-hook crate's signal constants don't include signal numbers that glibc or musl reserve, so rustix could have a public sigaction API could that refuses to accept any signal not in that set. It could also refuse to handle the signals that signal-hook panics on.
If the signal number is SIGABRT, take a lock that is shared with abort.
Record all the signals that have custom handlers, so that posix_spawn can avoid "unpredictable and dangerous results"
Call SYS_rt_sigprocmask for internal signals before any custom handlers are installed.
Set __eintr_valid_flag, which is used to work around a kernel bug.
According to this commit the bug is in Linux < 2.6.22, which is older than the oldest Linux version Rust supports today, so we don't need to worry about it.
I've done some more investigation and updated the comment above with some notes. Some of the issues can be addressed, but I don't see a way to address the others. They are fairly obscure, and it's not clear why libc implementations originally chose to do some of the things they're doing, but ultimately, they are there. If we register signal handlers without going through libc, we'll evoke non-local behavior changes in programs, which I'm not comfortable doing.
If we register signal handlers without going through libc, we'll evoke non-local behavior changes in programs, which I'm not comfortable doing.
I am assuming that's specific to a Rust program using libc, but not any interactions between programs? (which could use different libc, such as glibc vs musl)
If so, is this something that has been improved upon since the 2023 discussion here? Perhaps via eyra? (or one of the crates it builds upon to provide a libc alternative)
I'm not too familiar with the lower-level crates used by eyra, but I understand for eyra itself there are caveats that limit adoption (none of which seem likely to change):
Not all of libc implemented
Only supports static linking
Requires nightly
NOTE: The linked sigaction implementation in c-scape is dated Sep 2023 in Git blame, other than later change (Mar 2025) to migrate to Rustix 1.0.
Activity
sunfishcode commentedon Apr 24, 2023
Ah, interesting. This is tricky, because
sigactionis not a normal syscall. libc implementations ofsigactionon Linux do more than just call Linux'srt_sigactionsyscall. They have access to libc internals and do special things for threads.Also, everything in rustix's public (non-
runtime) interface can coexist with a libc, but libc's generally assume they're in full control ofsigactionkinds of syscalls. Invoking thert_sigactionsyscall without going through libc could break libc assumptions.It's possible we can figure something out here, but it'll take some investigation.
notgull commentedon Apr 24, 2023
Interesting! I took a look as
musl's implementation and it looked alright to me from this point of view...sunfishcode commentedon Apr 24, 2023
At a quick glance, it seems musl is doing these things:
sigactionAPI could that refuses to accept any signal not in that set. It could also refuse to handle the signals that signal-hook panics on.SIGABRT, take a lock that is shared withabort.posix_spawncan avoid "unpredictable and dangerous results"SYS_rt_sigprocmaskfor internal signals before any custom handlers are installed.__eintr_valid_flag, which is used to work around a kernel bug.I'd want to understand those better.
sunfishcode commentedon Apr 26, 2023
I've done some more investigation and updated the comment above with some notes. Some of the issues can be addressed, but I don't see a way to address the others. They are fairly obscure, and it's not clear why libc implementations originally chose to do some of the things they're doing, but ultimately, they are there. If we register signal handlers without going through libc, we'll evoke non-local behavior changes in programs, which I'm not comfortable doing.
notgull commentedon Apr 27, 2023
Yeah, sounds like a bad idea. Thanks for investigating, I'll close this now.
polarathene commentedon Jun 11, 2026
I am assuming that's specific to a Rust program using libc, but not any interactions between programs? (which could use different libc, such as glibc vs musl)
If so, is this something that has been improved upon since the 2023 discussion here? Perhaps via
eyra? (or one of the crates it builds upon to provide a libc alternative)sigaction()has been implemented withinc-scape](https://github.com/sunfishcode/c-ward/tree/main/c-scape)eyra, but I understand foreyraitself there are caveats that limit adoption (none of which seem likely to change):libcimplementedNOTE: The linked
sigactionimplementation inc-scapeis dated Sep 2023 in Git blame, other than later change (Mar 2025) to migrate to Rustix 1.0.