From owner-freebsd-bugs Mon Nov 25 14:40: 7 2002 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 DE52D37B401 for ; Mon, 25 Nov 2002 14:40:04 -0800 (PST) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id A8A6043E88 for ; Mon, 25 Nov 2002 14:40:01 -0800 (PST) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.6/8.12.6) with ESMTP id gAPMe1x3080545 for ; Mon, 25 Nov 2002 14:40:01 -0800 (PST) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.6/8.12.6/Submit) id gAPMe1Wj080544; Mon, 25 Nov 2002 14:40:01 -0800 (PST) Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 25CC137B401 for ; Mon, 25 Nov 2002 14:35:04 -0800 (PST) Received: from mrout3.yahoo.com (mrout3.yahoo.com [216.145.54.173]) by mx1.FreeBSD.org (Postfix) with ESMTP id A28E343EB2 for ; Mon, 25 Nov 2002 14:35:03 -0800 (PST) (envelope-from jayanth@yahoo-inc.com) Received: from milk.yahoo.com (milk.yahoo.com [216.145.52.137]) by mrout3.yahoo.com (8.11.6/8.11.6/y.out) with ESMTP id gAPMYl941320 for ; Mon, 25 Nov 2002 14:34:47 -0800 (PST) Received: (from jayanth@localhost) by milk.yahoo.com (8.11.0/8.11.0) id gAPMYlV19610; Mon, 25 Nov 2002 14:34:47 -0800 (PST) (envelope-from jayanth) Message-Id: <200211252234.gAPMYlV19610@milk.yahoo.com> Date: Mon, 25 Nov 2002 14:34:47 -0800 (PST) From: jayanth@yahoo-inc.com Reply-To: jayanth@yahoo-inc.com To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.2 Subject: kern/45733: file descriptor flags and socket flags out of sync Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org >Number: 45733 >Category: kern >Synopsis: file descriptor flags and socket flags out of sync >Confidential: no >Severity: serious >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Mon Nov 25 14:40:01 PST 2002 >Closed-Date: >Last-Modified: >Originator: Jayanth Vijayaraghavan >Release: All FreeBSD releases. >Organization: Yahoo! Inc. >Environment: >Description: Some developers here have encountered a scenario where the file descriptor flags and the socket flags seem to be out of sync. if an application does: listen(listenfd) while (!done) { select() <-------------------- new connection arrives before fcntl() fcntl(listenfd,O_NONBLOCK) newfd = accept(listenfd,...) fnctl(listenfd,0) /* make socket blocking */ flags = fcntl(newfd,GETFL) if (flags & O_NONBLOCK) /* fd is O_NONBLOCK, but socket is blocking */ } At this point socket is blocking because the state of the new socket = state of the listen socket only during the connection setup phase, not during the accept phase. However, the filedescriptor flags are copied during the accept phase. So at this point the filedescriptor flags are nonblocking but the socket is actually blocking. Agreed, that the solution is to have the application set NONBLOCK before the listen() call, but it seems incorrect to have the newfd's flags and socket state be out of sync. Copying the state of the socket during the accept might lead to a slightly different behaviour, but will solve this particular problem. >How-To-Repeat: Code snippet shown above. >Fix: One of the ways is to have the state of the socket consistent with the listening(parent) socket's state. So repeat the copy of the state of the socket in file uipc_syscalls.c and function accept1() as shown below. static int accept1(p, uap, compat) struct proc *p; register struct accept_args /* { int s; ....... /* connection has been removed from the listen queue */ KNOTE(&head->so_rcv.sb_sel.si_note, 0); so->so_state |= head->so_state; /* Add this line */ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ so->so_state &= ~SS_COMP; so->so_head = NULL; >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message