codeblog code is freedom — patching my itch

March 9, 2008

using select on a fifo

Filed under: Ubuntu — kees @ 5:00 pm

The right way to handle on-going input from file descriptors is to use select(). All readable events are flagged (one such event is “end of file”, which is indicated by a 0-sized read()). For example, if we’re reading from file descriptor fd:

  fd_set rfds;
  int rc;

  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);

  tv.tv_sec = 1;
  tv.tv_usec = 0;

  rc = select(fd + 1, &rfds, NULL, NULL, &tv);
  if (rc > 0) {
    char buf[80];
    ssize_t got = read(fd, buf, sizeof(buf));
    if (got < 0) {
      perror("read");
      return 1;
    }
    else if (got == 0) {
      printf("EOF\\n");
      return 1;
    }
    else {
      printf("read bytes: %d\\n", got);
    }
  }

When dealing with sockets, the above loop is sane — EOF means the other end hung up and you’ll never get more data from the file descriptor. In the case of a FIFO, however, “0 length read” means there are no more FIFO writers — but more could attach later and continue feeding in data! The problem with this is that select misbehaves and marks the file descriptor as “EOF” forever. Only the initial select() call blocks until there is something to read — once it hits EOF, select will always immediately return, defeating the purpose of select().

One solution is to re-open the FIFO, but you might miss writers between your 0-length read() and the next open().

The seemingly correct solution is rather simple: the FIFO reader should open the FIFO as a writer also. In this case, select() never thinks all the writers have vanished, so there is never an EOF condition, and the reader never misses any writers. So, instead of O_RDONLY, use:

fd = open(FIFO_PATHNAME, O_RDWR | O_NONBLOCK);

© 2008, Kees Cook. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 License.
CC BY-SA 4.0

6 Comments

  1. Very informative post Kees, keep it up.

    Comment by Jeff Schroeder — March 10, 2008 @ 5:36 am

  2. You would need to use clearerr. Actually that is a major problem with GIOChannel is that there is no way to clear the EOF (that I can find). Standard IO has clearerr which clears both errors and EOF status. This is an idle reading function from one of my programs (sanitized a little):

    static gboolean parser_read_icb( SimExec *sim ) {
      SimExec *sim;
      char buf[64] = "", *str;
      gsize bytes_read = 0, curpos, endpos;
      ScenarioStream *ss = NULL;
      
      g_return_val_if_fail( IS_SIM_EXEC(sim), FALSE );
      
      ss = sim->priv->parser;
      
      g_return_val_if_fail( sim->priv->output_fp, FALSE );
      
      clearerr( sim->priv->output_fp );
      if( sim->priv->status == SIM_EXEC_RUNNING ) {
        curpos = ftell( sim->priv->output_fp );
        fseek( sim->priv->output_fp, 0, SEEK_END );
        endpos = ftell( sim->priv->output_fp );
        fseek( sim->priv->output_fp, curpos, SEEK_SET );
        if( endpos - curpos priv->output_fp );
      bytes_read = fread( buf, 1, sizeof(buf), sim->priv->output_fp );
      if( bytes_read priv->output_fp ) ||
    			  ferror( sim->priv->output_fp )) ) {
        if( sim->priv->status != SIM_EXEC_RUNNING ) {
          fclose( sim->priv->output_fp );
          sim->priv->output_fp = NULL;
          return( FALSE );
        }
      } else if( bytes_read > 0 ) {
        for( str = buf; *str; str++ )
          if( *str == '\\n' ) 
    	ss->lineno++;
        ss->pspool =
          sxml_parse( ss->pspool, &ss->pslen, buf, bytes_read,
                      (SXMLStartTagFunc)stream_start,
                      (SXMLEndTagFunc)stream_end,
                      (SXMLCharsFunc)scenario_chars, ss );
      }
      return( TRUE );
    }
    

    Comment by Aron Rubin — March 11, 2008 @ 5:11 pm

  3. Thanks for the tip, it’s just what I was looking for!

    Comment by Schnouki — September 14, 2008 @ 7:57 am

  4. Thanks for the hint, this helped me out of misery :)

    Comment by cfchris6 — October 15, 2008 @ 2:38 am

  5. Again, this look like the answer I needed – thanks!

    Comment by TOMi — September 20, 2010 @ 1:21 pm

  6. I was bashing my head against this problem today, and you were the first Google search result for “select fifo” and this solves my problem entirely, thanks! This (and the clearerr() reference in the comments) is useful information. And coincidental—I think the last time I talked to you was like 14 years ago at UIUC. :)

    Comment by Andrew H. — November 2, 2012 @ 6:02 pm

Powered by WordPress