kern/162379: When select(2) closed writing pipe, it will sticks.

Gleb Smirnoff glebius at FreeBSD.org
Thu Nov 10 12:30:14 UTC 2011


The following reply was made to PR kern/162379; it has been noted by GNATS.

From: Gleb Smirnoff <glebius at FreeBSD.org>
To: Yui NARUSE <naruse at airemix.jp>
Cc: freebsd-gnats-submit at FreeBSD.org
Subject: Re: kern/162379: When select(2) closed writing pipe, it will sticks.
Date: Thu, 10 Nov 2011 16:27:37 +0400

 On Tue, Nov 08, 2011 at 04:57:50PM +0000, Yui NARUSE wrote:
 Y> >How-To-Repeat:
 Y> Run following program, it will sticks.
 Y> 
 Y> #include <stdio.h>
 Y> #include <stdlib.h>
 Y> #include <errno.h>
 Y> #include <sys/select.h>
 Y> #define max(x,y) ((x > y) ? x : y)
 Y> int
 Y> main(void) {
 Y>     int pipes[2];
 Y>     int res = pipe(pipes);
 Y>     if (res != 0) abort();
 Y>     int r = pipes[0];
 Y>     int w = pipes[1];
 Y>     res = close(w);
 Y>     if (res != 0) abort();
 Y>     fd_set readfds; FD_ZERO(&readfds);
 Y>     fd_set writefds; FD_ZERO(&writefds);
 Y>     fd_set exceptfds; FD_ZERO(&exceptfds);
 Y>     FD_SET(w, &writefds);
 Y>     res = select(max(r,w)+1, &readfds, &writefds, &exceptfds, NULL);
 Y>     if (res) perror("select");
 Y>     return 0;
 Y> }
 Y> >Fix:
 
 According to SUS, issuing close() on a descriptor means deallocating it,
 and the argument value for close() is not a valid descriptor anymore
 after close().
 
 http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html
 
 So, you are passing an invalid descriptor to FD_SET(). Behavior in this
 case is undefined. Quoting SUS:
 
 "The behavior of these macros is undefined if the fd argument is less than 0 or greater than or equal to FD_SETSIZE, or if fd is not a valid file descriptor, or if any of the arguments are expressions with side-effects."
 
 also
 
 "The pselect() and select() functions shall support regular files, terminal and pseudo-terminal devices, STREAMS-based files, FIFOs, pipes, and sockets. The behavior of pselect() and select() on file descriptors that refer to other types of file is unspecified."
 
 More info here:
 
 http://pubs.opengroup.org/onlinepubs/9699919799/functions/select.html
 
 If you don't mind, I am going to close this problem report.
 
 -- 
 Totus tuus, Glebius.


More information about the freebsd-bugs mailing list