From owner-freebsd-bugs Tue Jul 20 16:34:33 1999 Delivered-To: freebsd-bugs@freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (Postfix) with ESMTP id 37A02153F6 for ; Tue, 20 Jul 1999 16:34:25 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.9.3/8.9.2) id QAA88583; Tue, 20 Jul 1999 16:30:01 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from alcanet.com.au (border.alcanet.com.au [203.62.196.10]) by hub.freebsd.org (Postfix) with ESMTP id 4E103153DF for ; Tue, 20 Jul 1999 16:25:44 -0700 (PDT) (envelope-from jeremyp@gsmx07.alcatel.com.au) Received: by border.alcanet.com.au id <40353>; Wed, 21 Jul 1999 09:07:07 +1000 Message-Id: <99Jul21.090707est.40353@border.alcanet.com.au> Date: Wed, 21 Jul 1999 09:25:26 +1000 From: Peter Jeremy Reply-To: peter.jeremy@alcatel.com.au To: FreeBSD-gnats-submit@FreeBSD.ORG X-Send-Pr-Version: 3.2 Subject: kern/12723: Unnecessary use of magic numbers in F_[SG]ETFD code Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >Number: 12723 >Category: kern >Synopsis: Unnecessary use of magic numbers in F_[SG]ETFD code >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Tue Jul 20 16:30:00 PDT 1999 >Closed-Date: >Last-Modified: >Originator: Peter Jeremy >Release: FreeBSD 4.0-CURRENT i386 >Organization: Alcatel Australia Limited >Environment: -CURRENT from cvs-cur 5494 >Description: The code implementing the F_[SG]ETFD fcntl's uses the magic number `1' to represent both the user mode and kernel mode close on exec flags. These flags both have macros associated with them (FD_CLOEXEC and UF_EXCLOSE, respectively). Neither macro mentions the existence of the other, or the requirement that the macro value not change from 1. >How-To-Repeat: Code inspection >Fix: The following patch removes the reference to a magic value and dissociates the values of FD_CLOEXEC and UF_EXCLOSE. The fcntl(2) man page is also updated to document FD_CLOEXEC. Index: kern_descrip.c =================================================================== RCS file: /home/CVSROOT/./src/sys/kern/kern_descrip.c,v retrieving revision 1.64 diff -u -r1.64 kern_descrip.c --- kern_descrip.c 1999/06/07 20:37:27 1.64 +++ kern_descrip.c 1999/07/20 23:06:23 @@ -247,11 +247,12 @@ return (finishdup(fdp, uap->fd, i, p->p_retval)); case F_GETFD: - p->p_retval[0] = *pop & 1; + p->p_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0; return (0); case F_SETFD: - *pop = (*pop &~ 1) | (uap->arg & 1); + *pop = (*pop &~ UF_EXCLOSE) | + (uap->arg & FD_CLOEXEC ? UF_EXCLOSE : 0); return (0); case F_GETFL: Index: /3.0/cvs/src/lib/libc/sys/fcntl.2 =================================================================== RCS file: /home/CVSROOT/./src/lib/libc/sys/fcntl.2,v retrieving revision 1.14 diff -u -r1.14 fcntl.2 --- fcntl.2 1999/07/12 20:48:21 1.14 +++ fcntl.2 1999/07/20 23:20:54 @@ -80,8 +80,12 @@ .El .It Dv F_GETFD Get the close-on-exec flag associated with the file descriptor -.Fa fd . -If the low-order bit of the returned value is 0, +.Fa fd +as +.Dv FD_CLOEXEC . +If the returned value anded with +.Dv FD_CLOEXEC +is 0, the file will remain open across .Fn exec , otherwise the file will be closed upon execution of @@ -91,9 +95,13 @@ .It Dv F_SETFD Set the close-on-exec flag associated with .Fa fd -to the low order bit of +to +.Fa arg , +where .Fa arg -(0 or 1 as above). +is either 0 or +.Dv FD_CLOEXEC , +as described above. .It Dv F_GETFL Get descriptor status flags, as described below .Fa ( arg >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message