<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    Am 08.05.24 um 13:51 schrieb David Laight:<br>
    <blockquote type="cite" cite="mid:c0fe95949d4f41449f17add8300270b9@AcuMS.aculab.com">
      <pre class="moz-quote-pre" wrap="">From: Christian König
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">Sent: 07 May 2024 15:05
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">...
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">I actually have been telling people to (ab)use the epoll behavior to
check if two file descriptors point to the same underlying file when
KCMP isn't available.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
In what way?</pre>
    </blockquote>
    <br>
    Something like this:<br>
    <span><span class="ui-provider eb blt avv blu blv blw blx bly blz bma bmb bmc bmd bme bmf bmg bmh bmi bmj bmk bml bmm bmn bmo bmp bmq bmr bms bmt bmu bmv bmw bmx bmy bmz" dir="ltr">
        <p>fd_e = epoll_create1(EPOLL_CLOEXEC);</p>
        <p>tmp = dup(fd_A)<br>
          epoll_ctl(fd_e, EPOLL_CTL_ADD, tmp, ....);<br>
          dup2(fd_B, tmp);</p>
        <p>/* If this return -EEXISTS then the fd_A and fd_B are
          pointing to the same struct file */<br>
          epoll_ctl(fd_e, EPOLL_CTL_ADD, tmp, ....);</p>
        <p>close (tmp);<br>
          close (fd_e</p>
      </span></span><br>
    <blockquote type="cite" cite="mid:c0fe95949d4f41449f17add8300270b9@AcuMS.aculab.com">
      <pre class="moz-quote-pre" wrap="">
You can add both fd to the same epoll fd.
Relying on the implicit EPOLL_CTL_DEL not happening until both fd are
closed is a recipe for disaster.
(And I can't see an obvious way of testing it.)

Q6/A6 on epoll(7) should always have had a caveat that it is an
'implementation detail' and shouldn't be relied on.
(it is written as a 'beware of' ...)

The other point is that there are two ways to get multiple fd that
reference the same underlying file.
dup() fork() etc share the file offset, but open("/dev/fd/n") adds
a reference count later and has a separate file offset.</pre>
    </blockquote>
    <br>
    No it doesn't.<br>
    <br>
    Accessing /dev/fd/n or /proc/*/fd/n ideally accesses the same inode,
    but gives you a new struct file.<br>
    <br>
    dup(), fork() etc.. make you actually reference the same struct file
    inside the kernel.<br>
    <br>
    That turned out to be a rather important distinction when working
    with device drivers and DMA-buf.<br>
    <br>
    Regards,<br>
    Christian.<br>
    <br>
    <blockquote type="cite" cite="mid:c0fe95949d4f41449f17add8300270b9@AcuMS.aculab.com">
      <pre class="moz-quote-pre" wrap="">

I don't know which structure epoll is using, but I suspect it is
the former.
So it may not tell you what you want to know.

        David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
</pre>
    </blockquote>
    <br>
  </body>
</html>