Re: [PATCH 2/2] gpiolib: cdev: export the consumer's PID

Kent Gibson
2022-09-13T02:12:54Z

On Mon, Sep 12, 2022 at 11:56:17AM +0200, Bartosz Golaszewski wrote:
> On Mon, Sep 12, 2022 at 11:53 AM Kent Gibson <warthog618@gmail.com> wrote:
> >
>
> [snip]
>
> > >
> > > Using -1 sounds good but I've just realized there's a different
> > > problem. A process holding a file descriptor may fork and both the
> > > parent and the child will keep the same file descriptors open. Now
> > > we'll have two processes (with different PIDs) holding the same GPIO
> > > lines (specifically holding a file descriptor to the same anonymous
> > > inode).
> > >
> > > This already poses a problem for this patch as we'd need to return an
> > > array of PIDs which we don't have the space for but also is a
> > > situation which we haven't discussed previously IIRC - two processes
> > > keeping the same GPIO lines requested.
> > >
> > > I don't have any good idea on how to address this yet. One thing off
> > > the top of my head is: close the parent's file descriptor from kernel
> > > space (is it even possible?) on fork() (kind of like the close() on
> > > exec flag).
> > >
> > > I need to think about it more.
> > >
> >
> > I thought the O_CLOEXEC was set on the request fds exactly to prevent this
> > case - only one process can hold the request fd.
> >
>
> O_CLOEXEC means "close on exec" not "close on fork". When you fork,
> you inherit all file descriptors from your parent. Only once you call
> execve() are the fds with this flag closed *in the child*.
>

Ah, ok.
You want to pass request fd ownership from parent to child??
Why not lock ownership to the parent, so O_CLOFORK, were that
available?

Cheers,
Kent.