Date: Mon, 25 Nov 2002 14:34:47 -0800 (PST) From: jayanth@yahoo-inc.com To: FreeBSD-gnats-submit@FreeBSD.org Subject: kern/45733: file descriptor flags and socket flags out of sync Message-ID: <200211252234.gAPMYlV19610@milk.yahoo.com>
next in thread | raw e-mail | index | archive | help
>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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200211252234.gAPMYlV19610>