<div dir="ltr">I&#39;m using it as a fake &quot;always non-blocking&quot; file descriptor.<div><br></div><div style>My main libevent-like poll loop looks like:</div><div style><br></div><div style><font face="courier new, monospace">    poll(fds)</font></div>
<div style><font face="courier new, monospace">    for fd in fds:</font></div><div style><font face="courier new, monospace">       if fd.revents | POLLIN:</font></div><div style><font face="courier new, monospace">           fd.read_callback()</font></div>
<div style><font face="courier new, monospace">       if fd.revents | POLLOUT:</font></div><div style><font face="courier new, monospace">           fd.write_callback()</font></div><div style><br></div><div style>Now let&#39;s say I want a fake filedescriptor that always reads &#39;z&#39;s (a sleepy fd).</div>
<div style>A simple way to implement that is to have a callback function associated with &quot;/dev/zero&quot; (so that poll will always say it&#39;s available to read from), and in the write callback we&#39;ll simply ignore the filedescriptor and write &#39;z&#39;s to the given buffer.</div>
<div style><br></div><div style>Something like:</div><div style><br></div><div style><font face="courier new, monospace">    register_with_poll(devzero, write_z_cb);</font></div><div style><font face="courier new, monospace">    </font></div>
<div style><font face="courier new, monospace">    int write_z_cb(int fd, char *buf, int len) {</font></div><div style><font face="courier new, monospace">        memset(buf, &#39;z&#39;, len);</font></div><div style><font face="courier new, monospace">        return len;</font></div>
<div style><font face="courier new, monospace">    }</font></div><div style><br></div><div style>I can still do that with non-blocking pipe I&#39;ll never write to (and since its buffer is empty it&#39;ll never block), but somehow I found /dev/zero to be more elegant.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jun 18, 2013 at 8:42 PM, Shachar Shemesh <span dir="ltr">&lt;<a href="mailto:shachar@shemesh.biz" target="_blank">shachar@shemesh.biz</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
    
  
  <div style="direction:ltr" bgcolor="#FFFFFF" text="#000000"><div class="im">
    <div>On 18/06/13 17:43, Elazar Leibovich
      wrote:<br>
    </div>
    <blockquote type="cite">
      <div dir="ltr">
        <div>Try to open /dev/null, and then to poll the file
          descriptor. Neither in the man page nor in the standard I see
          anything preventing you to poll on /dev/null, yet, it does not
          work on Mac OS X. You get a POLLNVAL.</div>
        <div><br>
        </div>
      </div>
    </blockquote></div>
    Under Linux, whether you can poll (epoll, select) a character device
    depends on the device&#39;s implementation. If the device implements it,
    then you can. If not, you can&#39;t.<br>
    <br>
    Under Linux, the device driver&#39;s implementer implements one
    callback, and the kernel uses that to allow select, poll and epoll
    (plus the &quot;p&quot; variants of the above) all at once. On Mac OS X,
    however, the BUGS line Oleg pointed to does not appear in the select
    syscall interface, which means that it might be possible to select a
    device file descriptor.<br>
    <br>
    Our of curiosity, however, why would you want to poll /dev/null?<br>
    <br>
    Shachar<br>
    <blockquote type="cite"><div class="im">
      <div dir="ltr">Run the following:
        <div><br>
        </div>
        <div><a href="https://gist.github.com/elazarl/5805848" target="_blank">https://gist.github.com/elazarl/5805848</a><br>
          <div><br>
          </div>
          <div>
            <pre style="line-height:16px;width:744px;font-size:12px;margin-bottom:0px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;margin-top:0px;padding:0px"><div><span style="color:rgb(153,153,153);font-weight:bold">#include &lt;stdio.h&gt;</span></div>
<div><span style="color:rgb(153,153,153);font-weight:bold">#include &lt;poll.h&gt;</span></div>
<div> </div><div><span style="color:rgb(153,153,153);font-weight:bold">#include &lt;fcntl.h&gt;</span></div><div>
 </div><div><span style="color:rgb(68,85,136);font-weight:bold">int</span> <span style="color:rgb(153,0,0);font-weight:bold">main</span><span>()</span> <span>{</span></div>
<div>    <span style="color:rgb(68,85,136);font-weight:bold">int</span> <span style="color:rgb(51,51,51)">fd</span> <span style="font-weight:bold">=</span> <span style="color:rgb(51,51,51)">open</span><span>(</span><span style="color:rgb(221,17,68)">&quot;/dev/null&quot;</span><span>,</span> <span style="color:rgb(51,51,51)">O_WRONLY</span><span>);</span></div>

<div>    <span style="font-weight:bold">struct</span> <span style="color:rgb(51,51,51)">pollfd</span> <span style="color:rgb(51,51,51)">pollfds</span> <span style="font-weight:bold">=</span> <span>{</span> <span style="color:rgb(51,51,51)">fd</span><span>,</span> <span style="color:rgb(51,51,51)">POLLOUT</span><span>,</span> <span style="color:rgb(0,153,153)">0</span> <span>};</span></div>

<div>    <span style="font-weight:bold">if</span> <span>(</span><span style="color:rgb(51,51,51)">fd</span> <span style="font-weight:bold">&lt;</span> <span style="color:rgb(0,153,153)">0</span><span>)</span> <span>{</span></div>

<div>        <span style="color:rgb(51,51,51)">perror</span><span>(</span><span style="color:rgb(221,17,68)">&quot;open&quot;</span><span>);</span></div>
<div>        <span style="font-weight:bold">return</span> <span style="color:rgb(0,153,153)">1</span><span>;</span></div><div>
    <span>}</span></div><div>    <span style="font-weight:bold">if</span> <span>(</span><span style="color:rgb(51,51,51)">poll</span><span>(</span><span style="font-weight:bold">&amp;</span><span style="color:rgb(51,51,51)">pollfds</span><span>,</span> <span style="color:rgb(0,153,153)">1</span><span>,</span> <span style="font-weight:bold">-</span><span style="color:rgb(0,153,153)">1</span><span>)</span> <span style="font-weight:bold">&lt;</span> <span style="color:rgb(0,153,153)">0</span><span>)</span> <span>{</span></div>

<div>        <span style="color:rgb(51,51,51)">perror</span><span>(</span><span style="color:rgb(221,17,68)">&quot;poll&quot;</span><span>);</span></div>
<div>        <span style="font-weight:bold">return</span> <span style="color:rgb(0,153,153)">2</span><span>;</span></div><div>
    <span>}</span></div><div>    <span style="font-weight:bold">if</span> <span>(</span><span style="color:rgb(51,51,51)">pollfds</span><span>.</span><span style="color:rgb(51,51,51)">revents</span> <span style="font-weight:bold">==</span> <span style="color:rgb(51,51,51)">POLLNVAL</span><span>)</span> <span>{</span></div>

<div>        <span style="color:rgb(51,51,51)">puts</span><span>(</span><span style="color:rgb(221,17,68)">&quot;huh? why poll({/dev/null, POLLOUT, 0}, 1) returns POLLNVAL?&quot;</span><span>);</span></div>
<div>    <span>}</span></div><div>    <span style="font-weight:bold">if</span> <span>(</span><span style="color:rgb(51,51,51)">pollfds</span><span>.</span><span style="color:rgb(51,51,51)">revents</span> <span style="font-weight:bold">==</span> <span style="color:rgb(51,51,51)">POLLOUT</span><span>)</span> <span>{</span></div>

<div>        <span style="color:rgb(51,51,51)">puts</span><span>(</span><span style="color:rgb(221,17,68)">&quot;working as expected&quot;</span><span>);</span></div>
<div>    <span>}</span></div><div>    <span style="font-weight:bold">return</span> <span style="color:rgb(0,153,153)">0</span><span>;</span></div>
<div><span>}</span></div><div><span>
</span></div><div><span style="color:rgb(34,34,34);font-family:arial;font-size:small;line-height:normal;white-space:normal">Is there anything I&#39;m missing? Or is it a real vaguely implemented corner of the standard.</span>

</div><div><span style="color:rgb(34,34,34);font-family:arial;font-size:small;line-height:normal;white-space:normal">If it is, I&#39;ll be glad to see a list of supported/unsupported platforms,</span></div>
<div><span style="color:rgb(34,34,34);font-family:arial;font-size:small;line-height:normal;white-space:normal">as well as list of other corner cases (for instance, POLLHUP may or may be received on EOF).</span></div>
<div><span style="color:rgb(34,34,34);font-family:arial;font-size:small;line-height:normal;white-space:normal">
</span></div></pre>
          </div>
        </div>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      </div><pre>_______________________________________________
Linux-il mailing list
<a href="mailto:Linux-il@cs.huji.ac.il" target="_blank">Linux-il@cs.huji.ac.il</a>
<a href="http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il" target="_blank">http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il</a>
</pre>
    </blockquote>
    <br>
  </div>

</blockquote></div><br></div>