Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Jul 1999 09:25:26 +1000
From:      Peter Jeremy <jeremyp@gsmx07.alcatel.com.au>
To:        FreeBSD-gnats-submit@FreeBSD.ORG
Subject:   kern/12723: Unnecessary use of magic numbers in F_[SG]ETFD code
Message-ID:  <99Jul21.090707est.40353@border.alcanet.com.au>

next in thread | raw e-mail | index | archive | help

>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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?99Jul21.090707est.40353>