From owner-freebsd-bugs@FreeBSD.ORG Wed Jun 18 10:00:29 2003 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2292C37B401 for ; Wed, 18 Jun 2003 10:00:29 -0700 (PDT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 607DE43FB1 for ; Wed, 18 Jun 2003 10:00:28 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.9/8.12.9) with ESMTP id h5IH0SUp044556 for ; Wed, 18 Jun 2003 10:00:28 -0700 (PDT) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.9/8.12.9/Submit) id h5IH0Ruw044555; Wed, 18 Jun 2003 10:00:27 -0700 (PDT) Date: Wed, 18 Jun 2003 10:00:27 -0700 (PDT) Message-Id: <200306181700.h5IH0Ruw044555@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org From: "Artem 'Zazoobr' Ignatjev" Subject: Re: kern/53447: poll(2) semantics differ from susV3/POSIX X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Artem 'Zazoobr' Ignatjev List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Jun 2003 17:00:29 -0000 The following reply was made to PR kern/53447; it has been noted by GNATS. From: "Artem 'Zazoobr' Ignatjev" To: freebsd-gnats-submit@freebsd.org Cc: Subject: Re: kern/53447: poll(2) semantics differ from susV3/POSIX Date: 18 Jun 2003 20:54:29 +0400 clemens fischer wrote: > a colleague and i independantly made the same observation: we are > running a wiki on a small HTTP server. every page served by it had an > error message on the bottom: "Looks like the CGI crashed.". we could > track this down to the code in the server where data is read from the > CGI through a pipe. this is done using poll(2) and read(2). the same > code runs without problems on linux, and we can patch fnord to work > around the problem, which is otherwise reproducable. > > this is part of the discussion thread on the mailinglist: > > > i had the same problem on my freebsd-4.8-stable. every page had > > "looks like your CGI crashed" at the bottom, but they actually > > worked fine. after applying the patch the problem has > > disappeared. > > Mhh, then this is apparently a problem with BSD poll() semantics. > > poll is expected to set the POLLHUP bit on EOF, but FreeBSD > apparently does not, but signals POLLIN and then returns 0 on > read(). Is someone involved with the FreeBSD crowd and can post a > bug report for this? > FreeBSD DOES set POLLHUP bit; but, also, EOF on pipe or disconnected socket can be caught by reading 0 bytes from ready-to-read descriptor. See the code below (it's /sys/kern/sys_pipe.c 1.60.2.13, used in FreeBSD 4.8-RELEASE): int pipe_poll(fp, events, cred, p) struct file *fp; int events; struct ucred *cred; struct proc *p; { struct pipe *rpipe = (struct pipe *)fp->f_data; struct pipe *wpipe; int revents = 0; wpipe = rpipe->pipe_peer; if (events & (POLLIN | POLLRDNORM)) if ((rpipe->pipe_state & PIPE_DIRECTW) || (rpipe->pipe_buffer.cnt > 0) || > (rpipe->pipe_state & PIPE_EOF)) > revents |= events & (POLLIN | POLLRDNORM); if (events & (POLLOUT | POLLWRNORM)) if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) || (((wpipe->pipe_state & PIPE_DIRECTW) == 0) && (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) revents |= events & (POLLOUT | POLLWRNORM); > if ((rpipe->pipe_state & PIPE_EOF) || > (wpipe == NULL) || > (wpipe->pipe_state & PIPE_EOF)) > revents |= POLLHUP; if (revents == 0) { if (events & (POLLIN | POLLRDNORM)) { selrecord(p, &rpipe->pipe_sel); rpipe->pipe_state |= PIPE_SEL; } if (events & (POLLOUT | POLLWRNORM)) { selrecord(p, &wpipe->pipe_sel); wpipe->pipe_state |= PIPE_SEL; } } return (revents); }