Skip to content

Add an implementation of sigaction for libc backend #628

Closed as not planned
Closed as not planned

Description

@notgull

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.

Activity

  1. sunfishcode commented on Apr 24, 2023

    @sunfishcode
    Member

    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.

  2. notgull commented on Apr 24, 2023

    @notgull
    ContributorAuthor

    Interesting! I took a look as musl's implementation and it looked alright to me from this point of view...

  3. sunfishcode commented on Apr 24, 2023

    @sunfishcode
    Member

    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'd want to understand those better.

  4. sunfishcode commented on Apr 26, 2023

    @sunfishcode
    Member

    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.

  5. notgull commented on Apr 27, 2023

    @notgull
    ContributorAuthor

    Yeah, sounds like a bad idea. Thanks for investigating, I'll close this now.

  6. polarathene commented on Jun 11, 2026

    @polarathene
    Contributor

    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)

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions