From owner-freebsd-bugs@FreeBSD.ORG Fri Feb 10 22:50:12 2012 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5B9F5106564A for ; Fri, 10 Feb 2012 22:50:12 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 310298FC12 for ; Fri, 10 Feb 2012 22:50:12 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.5/8.14.5) with ESMTP id q1AMoCkt074251 for ; Fri, 10 Feb 2012 22:50:12 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.5/8.14.5/Submit) id q1AMoCCB074250; Fri, 10 Feb 2012 22:50:12 GMT (envelope-from gnats) Resent-Date: Fri, 10 Feb 2012 22:50:12 GMT Resent-Message-Id: <201202102250.q1AMoCCB074250@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Peter Jeremy Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id AD159106566B for ; Fri, 10 Feb 2012 22:46:14 +0000 (UTC) (envelope-from peterjeremy@acm.org) Received: from mail35.syd.optusnet.com.au (mail35.syd.optusnet.com.au [211.29.133.51]) by mx1.freebsd.org (Postfix) with ESMTP id 47FED8FC12 for ; Fri, 10 Feb 2012 22:46:13 +0000 (UTC) Received: from server.vk2pj.dyndns.org (c220-239-116-103.belrs4.nsw.optusnet.com.au [220.239.116.103]) by mail35.syd.optusnet.com.au (8.13.1/8.13.1) with ESMTP id q1AMkB85032415 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sat, 11 Feb 2012 09:46:12 +1100 Received: from server.vk2pj.dyndns.org (localhost.vk2pj.dyndns.org [127.0.0.1]) by server.vk2pj.dyndns.org (8.14.5/8.14.4) with ESMTP id q1AMkB6X019308; Sat, 11 Feb 2012 09:46:11 +1100 (EST) (envelope-from peter@server.vk2pj.dyndns.org) Received: (from peter@localhost) by server.vk2pj.dyndns.org (8.14.5/8.14.4/Submit) id q1AMkAhN019307; Sat, 11 Feb 2012 09:46:10 +1100 (EST) (envelope-from peter) Message-Id: <201202102246.q1AMkAhN019307@server.vk2pj.dyndns.org> Date: Sat, 11 Feb 2012 09:46:10 +1100 (EST) From: Peter Jeremy To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Cc: Subject: kern/164970: dup2(2) incorrectly returns EMFILE instead of EBADF X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Peter Jeremy List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Feb 2012 22:50:12 -0000 >Number: 164970 >Category: kern >Synopsis: dup2(2) incorrectly returns EMFILE instead of EBADF >Confidential: no >Severity: serious >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Fri Feb 10 22:50:11 UTC 2012 >Closed-Date: >Last-Modified: >Originator: Peter Jeremy >Release: FreeBSD 8.2-STABLE amd64 >Organization: >Environment: System: FreeBSD server.vk2pj.dyndns.org 8.2-STABLE FreeBSD 8.2-STABLE #15: Thu Feb 2 11:02:29 EST 2012 root@server.vk2pj.dyndns.org:/var/obj/usr/src/sys/server amd64 >Description: http://pubs.opengroup.org/onlinepubs/9699919799/functions/dup.html states that dup2() should return EBADF if the second argumunt is "greater than or equal to {OPEN_MAX}." FreeBSD returns EMFILE in this case (this was broken in r124548). FreeBSD also returns EMFILE for fcntl(F_DUP2FD) but this operation is not defined by The Open Group. Note that GNU autotools disables the use of dup2() on FreeBSD due to this error. >How-To-Repeat: ktrace the execution of: #include int main(int argc, char **argv) { return dup2(1,1000000); } >Fix: Index: sys/kern/kern_descrip.c =================================================================== --- sys/kern/kern_descrip.c (revision 231385) +++ sys/kern/kern_descrip.c (working copy) @@ -813,7 +813,7 @@ maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); PROC_UNLOCK(p); if (new >= maxfd) - return (flags & DUP_FCNTL ? EINVAL : EMFILE); + return (flags & DUP_FCNTL ? EINVAL : EBADF); FILEDESC_XLOCK(fdp); if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) { Index: lib/libc/sys/fcntl.2 =================================================================== --- lib/libc/sys/fcntl.2 (revision 231385) +++ lib/libc/sys/fcntl.2 (working copy) @@ -539,8 +539,6 @@ .Fa cmd is .Dv F_DUPFD -or -.Dv F_DUP2FD and the maximum number of file descriptors permitted for the process are already in use, or no file descriptors greater than or equal to Index: lib/libc/sys/dup.2 =================================================================== --- lib/libc/sys/dup.2 (revision 231385) +++ lib/libc/sys/dup.2 (working copy) @@ -128,20 +128,27 @@ .Sh ERRORS The .Fn dup -and -.Fn dup2 -system calls fail if: +system call fails if: .Bl -tag -width Er .It Bq Er EBADF The .Fa oldd -or -.Fa newd argument is not a valid active descriptor .It Bq Er EMFILE Too many descriptors are active. .El +The +.Fn dup2 +system call fails if: +.Bl -tag -width Er +.It Bq Er EBADF +The +.Fa oldd +argument is not a valid active descriptor or the +.Fa newd +argument is negative or exceeds the maximum allowable descriptor number +.El .Sh SEE ALSO .Xr accept 2 , .Xr cap_new 2 , >Release-Note: >Audit-Trail: >Unformatted: