Skip to content

Conversation

@SteveLauC
Copy link
Member

@SteveLauC SteveLauC commented Dec 7, 2022

What this PR does:

  1. Adds I/O safety for sys/wait

Actually, I am not sure about which type to use here:

pub enum Id<'fd> {
    /// Wait for the child referred to by the given PID file descriptor
    #[cfg(any(target_os = "android", target_os = "linux"))]
    PIDFd(RawFd),
    PIDFd(BorrowedFd<'fd>),
}

If we use Fd: AsFd

pub enum Id<'fd, Fd: AsFd> {
    /// Wait for the child referred to by the given PID file descriptor
    #[cfg(any(target_os = "android", target_os = "linux"))]
    PIDFd(RawFd),
    PIDFd(&'fd Fd),
}

then the user has to specify that generic type when using this interface, which is kinda user-unfriendly...


The typical usage of this interface will be something like:

// Thought currently we don't have pidfd_open(2) in `Nix`
let fd_referring_to_a_process: OwnedFd  = pidfd_open().unwrap();
let status = waitid(Id::PIDFd(fd_referring_to_a_process), WaitPidFlag::XXXX).unwrap();

UPDATE: pidfd_open(2) will be added in #1859 or #1868 .

@SteveLauC SteveLauC mentioned this pull request Dec 7, 2022
29 tasks
@SteveLauC
Copy link
Member Author

The reason why CI failed is that enum Id<'fd> is available on:

  1. Linux (glibc/musl)
  2. Android
  3. FreeBSD
  4. Haiku

And that 'fd parameter is used only on Android and Linux(glibc/musl) as the cfg here:

    /// Wait for the child referred to by the given PID file descriptor
    #[cfg(any(target_os = "android", target_os = "linux"))]
    PIDFd(BorrowedFd<'fd>),

So 'fd becomes unused on FreeBSD and haiku, and Rust does not like this.

Honestly, I don't have a good solution to this problem, perhaps then BorrowedFd<'_> is not an appropriate option for this PR...


Similar Problem: Type parameter not used on enums

@asomers
Copy link
Member

asomers commented Dec 9, 2022

I think the easiest way to fix the tests on non-Linux is to add a PhantomData<'fd> field. For non-Linux systems, the lifetime will always end up being 'static, so it shouldn't be much of an inconvenience to the users.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

bors r+

@bors bors bot merged commit b7b0a65 into nix-rust:master Dec 11, 2022
@SteveLauC SteveLauC deleted the io-safety-sys/wait branch December 11, 2022 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants