Re: pidfd and O_NONBLOCK

From: Josh Triplett
Date: Tue Aug 11 2020 - 17:14:06 EST


On Tue, Aug 11, 2020 at 10:10:45PM +0200, Christian Brauner wrote:
> On Tue, Aug 11, 2020 at 11:12:36AM -0700, Josh Triplett wrote:
> > As far as I can tell, O_NONBLOCK has no effect on a pidfd. When calling
> > waitid on a pidfd for a running process, it always blocks unless you
> > provide WNOHANG.
> >
> > I don't think anything depends on that behavior. Would it be possible to
> > make O_NONBLOCK on a pidfd cause waitid on a running process to return
> > EWOULDBLOCK?
> >
> > This would make it easier to use pidfd in some non-blocking event loops.
>
> Hey Josh,
>
> Just to see I did a _horrible_ draft (cf. [1]) and it seems doable to me
> and if you can provide a good rationale and a use-case then I think that
> would be ok.

Rationale and use case: there are some non-blocking event loop
libraries, such as the Rust async-io library, that help build
epoll-based event loops around file descriptors. Those libraries
automatically set O_NONBLOCK on the file descriptors they manage, and
they treat EWOULDBLOCK errno codes specially, with semantics like "call
this function, if it returns EWOULDBLOCK then don't call it again until
epoll says the fd is ready". If setting O_NONBLOCK on pidfd caused
waitid to return EWOULDBLOCK, such libraries would Just Work with very
little effort.

Also, pidfd_open should accept O_NONBLOCK as a flag, which in addition
to saving a call to fcntl would allow userspace to detect if this works.
(Even if you want to use fcntl to set it later, you can always just open
your own PID with pidfd_open and check if you get EINVAL to know that
your kernel doesn't support this.)

Thanks,
Josh Triplett