title: How can I use inotify to tell when a named pipe is opened?
body: <p>The overall goal is I am trying to make a named pipe that when read is a PNG file of the current framebuffer.  With various snippets cobbled together from online like the PNG creator from Andrew Duncan I have something that can create and write the PNG OK but the trick is I need to not write the pipe until someone reads it so that the image is current and not back when the pipe was opened.</p>
  <p>It seems like I should be able to use <code>inotify_add_watch( fd, filename, IN_OPEN | IN_ACCESS )</code> to tell when someone opens the file to start reading and then I open it for writing and send my PNG file data and then close the file.</p>
  <p>The pipe is getting created but I am not getting the watch event when I try to read from the file (cat fbpipe.png &gt; pipefile.png).  It is blocking at the first read() of watch events.</p>
  <p>Relevant code snippets:</p>
  <pre><code>// Creating the named file(FIFO)
  // mkfifo(&lt;pathname&gt;, &lt;permission&gt;)
  mkfifo(filename, 0666);
  
  /*creating the INOTIFY instance*/
  fd = inotify_init();
  
  wd = inotify_add_watch( fd, filename, IN_OPEN | IN_ACCESS );
  
  /*read to determine the event change happens. Actually this read blocks until the change event occurs*/ 
  
  length = read( fd, buffer, EVENT_BUF_LEN ); 
  
  /*checking for error*/
  if ( length &lt; 0 ) {
     perror( &quot;read&quot; );
     return -1;
   }
  
    //  READ MY FRAMEBUFFER AND THEN WRITE THE DATA:
  
    FILE* file;
    file = fopen(filename, &quot;wb&quot; );
  
    fwrite(buffer, 1, buffersize, file);
    fclose(file);
  </code></pre>