Skip to content

Fix: Replace sighandler_t with sig_t for Apple and BSDs - #5008

Open
chengr4 wants to merge 1 commit into
rust-lang:mainfrom
chengr4:sig_t
Open

Fix: Replace sighandler_t with sig_t for Apple and BSDs#5008
chengr4 wants to merge 1 commit into
rust-lang:mainfrom
chengr4:sig_t

Conversation

@chengr4

@chengr4 chengr4 commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

Description

This PR tries to fix #1273.

I am not able to evaluate whether #[deprecated] should be added.

Sources

Apple I checked at my local (Aarch MacOS 26.3.1)

Checklist

  • Relevant tests in libc-test/semver have been updated
  • No placeholder or unstable values like *LAST or *MAX are
    included (see #3131)
  • Tested locally (cd libc-test && cargo test --target mytarget);
    especially relevant for platforms that may not be checked in CI

@rustbot

rustbot commented Mar 9, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in a NetBSD-like module

cc @semarie

Some changes occurred in an OpenBSD module

cc @semarie

@chengr4 chengr4 changed the title Sig t Fix: Replace sighandler_t with sig_t for Apple and BSDs Mar 9, 2026
@rustbot

This comment has been minimized.

@dybucc

dybucc commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Not sure if the author intends on finishing this up. If not, I've solved the
merge conflicts and (possibly) tweaked some the patchset at 1.

Note these changes should be enough to close issue #1273, but it doesn't solve
the underlying problem with sigaction (which is prevalent across other
targets.) There's some places where it's used inappropriately.

Footnotes

  1. https://github.com/dybucc/libc/commit/9f9458a9d3446d88d2b5dcbb8a837dbcaf34681e

@chengr4

chengr4 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

This PR was waiting for someone to review it for me. I can take another look after 8/9. If you have already resolved the issue and it is ready to merge, feel free to go ahead!

@dybucc

dybucc commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Nay, just wanted to know if you were still on it. I did make some changes,
though, as I believe the actual definition will change once we settle on the
right bindings for siginfo_t across all targets (see #1359.)

But this PR can probably merged as the required changes once we get siginfo_t
right are minimal. Though I don't think we should deprecate it. siginfo_t
actually exists (with sort of mangled name) in FreeBSD 1 2.

All other relevant targets (BSDs and Apple) can just rely on sig_t, as that's
their definition, while hiding sighandler_t in the docs for anything but
FreeBSD (and every target this PR doesn't change.)

It would be nice to have siginfo_t be an alias to the function and sig_t be
a pointer to this alias. But Rust doesn't allow function pointer decay through
function aliases when typedefining a function without prepending *.

Footnotes

  1. https://github.com/freebsd/freebsd-src/blob/a9305a6f50be89f22c7a469aeee4c3bea2086ace/sys/sys/signal.h#L160

  2. https://github.com/freebsd/freebsd-src/blob/a9305a6f50be89f22c7a469aeee4c3bea2086ace/sys/sys/signal.h#L408

@tgross35 tgross35 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I've missed this, one request then LGTM. Please squash as well.

I think we should deprecate sighandler_t on platforms where it doesn't exist, adding __sighandler_t if that's relevant. But not something to do here.

View changes since this review

Comment thread src/unix/mod.rs Outdated
Comment on lines +21 to +32
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
pub type sig_t = sighandler_t;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can go in src/unix/bsd/mod.rs so we don't need the cfg

@rustbot

rustbot commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@chengr4

chengr4 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot

This comment has been minimized.

@rustbot

rustbot commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • The following commits have merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

    You can start a rebase with the following commands:

    $ # rebase
    $ git pull --rebase https://github.com/rust-lang/libc.git main
    $ git push --force-with-lease
    

@rustbot

This comment has been minimized.

@rustbot

rustbot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

rustbot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • The following commits have merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

    You can start a rebase with the following commands:

    $ # rebase
    $ git pull --rebase https://github.com/rust-lang/libc.git main
    $ git push --force-with-lease
    

Comment thread src/unix/bsd/mod.rs
Comment on lines +27 to +28
// going to need tweaking because raw pointers to functions aren't a thing just
// yet in Rust.

@madsmtm madsmtm Aug 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't Option<unsafe extern "C" fn()> basically a raw pointer to a function?

View changes since the review

@tgross35 tgross35 Aug 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduce new alias `sig_t` to `sighandler_t` for both Apple and BSD
targets.

Replace all uses of `sighandler_t` with `sig_t` in Apple and BSD
targets. These platforms don't use the GNU-specific extension that
`sighandler_t` is. See the following list for details on each system.

- Apple: <https://github.com/apple-oss-distributions/xnu/blob/f6217f891ac0bb64f3d375211650a4c1ff8ca1ea/bsd/sys/signal.h#L501>
- FreeBSD: <https://github.com/freebsd/freebsd-src/blob/77d6c45afdca8a524a88edfb3097d4d9dc90b583/sys/sys/signal.h#L160>
- DragonFlyBSD: <https://github.com/DragonFlyBSD/DragonFlyBSD/blob/e8a62118fe4823bb5982e5d759e5ffaa6b6aae21/sys/sys/signal.h#L257>
- NetBSD: <https://github.com/NetBSD/src/blob/5c8b04300c02b550f331bc0b7357f0fc71909aaa/sys/sys/signal.h#L247>
- OpenBSD: <https://github.com/openbsd/src/blob/f8480ad0276ad54ce9f7ed2b2004c86f9f1a9521/sys/sys/signal.h#L148>

Note this patch doesn't try to fix the `sigaction` situation where most
systems use a specific field of the upstream `union` in its first field.
That is work for another patchset (and further discussion.)

Co-authored-by: Adam Martinez <149513579+dybucc@users.noreply.github.com>
auto-merge was automatically disabled August 19, 2026 21:56

Head branch was pushed to by a user without write access

Comment thread src/unix/bsd/mod.rs
Comment on lines +14 to +29
// FIXME(1.0): once we settle for a definition for `sighandler_t`, ensure it's
// sound for `sig_t` to be a raw pointer to it, as that's the definition in the
// BSDs and Apple targets.
//
// See <https://github.com/freebsd/freebsd-src/blob/77d6c45afdca8a524a88edfb3097d4d9dc90b583/sys/sys/signal.h#L160>
// and <https://github.com/freebsd/freebsd-src/blob/77d6c45afdca8a524a88edfb3097d4d9dc90b583/sys/sys/signal.h#L408>
// for FreeBSD details.
//
// Because `sighandler_t` is defined in FreeBSD as a function alias that
// implicitly decays to the function pointer and not a "straight" alias to a
// function pointer, we can probably keep the same definitions in other BSDs.
//
// They have no `sighandler_t` but do have the same `sig_t`. Of course, this is
// going to need tweaking because raw pointers to functions aren't a thing just
// yet in Rust.
pub type sig_t = *const crate::sighandler_t;

@tgross35 tgross35 Aug 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, reading things closer since Mads pointed it out, I think this should just be a type sig_t = Option<unsafe extern "C" fn(c_int)> and then sighandler_t needs to be deleted. Opsem says that's sound even for non-dereferenceable values, which was one of the questions in the past. The other question is whether or not it's a footgun, which I still think it is, but that applies to all function pointers in this crate.

Sorry for adding another change, but I think that gets us closer to where we want to be. You can also drop this comment since it will only have the problem that all function pointers have, and I don't think there's anything really worth noting about the FreeBSD definition (but do feel free to put any of it into the commit message).

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it looks like you need to recheck the semver files based on the CI failure.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect type name sighandler_t should be sig_t on apple targets

5 participants