From jeremyp@gsmx07.alcatel.com.au Tue Jul 20 16:25:48 1999
Return-Path: <jeremyp@gsmx07.alcatel.com.au>
Received: from alcanet.com.au (border.alcanet.com.au [203.62.196.10])
	by hub.freebsd.org (Postfix) with ESMTP id 4E103153DF
	for <FreeBSD-gnats-submit@FreeBSD.ORG>; 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 <jeremyp@gsmx07.alcatel.com.au>
Reply-To: peter.jeremy@alcatel.com.au
To: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Unnecessary use of magic numbers in F_[SG]ETFD code
X-Send-Pr-Version: 3.2

>Number:         12723
>Category:       kern
>Synopsis:       Unnecessary use of magic numbers in F_[SG]ETFD code
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    des
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jul 20 16:30:00 PDT 1999
>Closed-Date:    Mon Apr 29 01:58:47 PDT 2002
>Last-Modified:  Mon Apr 29 01:58:47 PDT 2002
>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:

From: Peter Jeremy <peter.jeremy@alcatel.com.au>
To: imp@village.org
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: kern/12723: Unnecessary use of magic numbers in F_[SG]ETFD code
Date: Thu, 27 Jan 2000 07:27:52 +1100

 Hi Warner,
 
 As the last person to touch /usr/src/sys/kern/kern_descrip.c, would
 you please consider applying the patches in PR kern/12723 to remove
 the unnecessary use of the magic number 1 instead of FD_CLOEXEC and
 UF_EXCLOSE.
 
 Peter
 

From: Warner Losh <imp@village.org>
To: Peter Jeremy <peter.jeremy@alcatel.com.au>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: kern/12723: Unnecessary use of magic numbers in F_[SG]ETFD code 
Date: Wed, 26 Jan 2000 14:28:19 -0700

 In message <00Jan27.072752est.115241@border.alcanet.com.au> Peter Jeremy writes:
 : As the last person to touch /usr/src/sys/kern/kern_descrip.c, would
 : you please consider applying the patches in PR kern/12723 to remove
 : the unnecessary use of the magic number 1 instead of FD_CLOEXEC and
 : UF_EXCLOSE.
 
 Sure.  Go ahead and assign it to me if you can, let me know if you
 can't.
 
 Warner
 
Responsible-Changed-From-To: freebsd-bugs->imp 
Responsible-Changed-By: ru 
Responsible-Changed-When: Thu Jan 27 00:19:41 PST 2000 
Responsible-Changed-Why:  
Warner volunteered for this PR. 
Responsible-Changed-From-To: imp->des 
Responsible-Changed-By: des 
Responsible-Changed-When: Tue Nov 20 16:39:27 PST 2001 
Responsible-Changed-Why:  
Warner asked me to take this. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=12723 
State-Changed-From-To: open->patched 
State-Changed-By: asmodai 
State-Changed-When: Sat Apr 13 03:21:51 PDT 2002 
State-Changed-Why:  
This has been committed to CURRENT. 
Will be MFC'd in about 2 weeks. 

Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=12723 
State-Changed-From-To: patched->closed 
State-Changed-By: asmodai 
State-Changed-When: Mon Apr 29 01:58:31 PDT 2002 
State-Changed-Why:  
Merged from CURRENT. 

Thanks. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=12723 
>Unformatted:
