Bug 70798

Summary: Compatibility: Sun/Solaris has an fcntl() F_DUP2FD. FreeBSD could support it too.
Product: Base System Reporter: jau
Component: kernAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.10-STABLE   
Hardware: Any   
OS: Any   

Description jau 2004-08-21 20:20:23 UTC
      This is taken from SunOS/Solaris fcntl() manual page...

F_DUP2FD
	Similar to  F_DUPFD, but always returns arg. F_DUP2FD closes
	arg if  it is open and not equal to fildes. F_DUP2FD is
	equivalent to dup2(fildes, arg).

It seems to be very easy to implement this functionality in FreeBSD.

1st step:
	Add a definition for F_DUP2FD in fcntl.h ...

#define F_DUP2FD	10		/* Parallel to dup2() like F_DUPFD is for dup(). */
	
2nd step:
	Add this change to kern_descrip.c ...

--- ../kern_descrip.c.old	Sat May 17 12:10:06 2003
+++ ../kern_descrip.c	Sat May 17 12:10:13 2003
@@ -249,24 +249,35 @@
 	switch (cmd) {
 	case F_DUPFD:
 		FILEDESC_UNLOCK(fdp);
 		newmin = arg;
 		if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
 		    newmin >= maxfilesperproc) {
 			error = EINVAL;
 			break;
 		}
 		error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
 		break;
 
+	case F_DUP2FD:
+		FILEDESC_UNLOCK(fdp);
+		newmin = arg;
+		if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
+		    newmin >= maxfilesperproc) {
+			error = EINVAL;
+			break;
+		}
+		error = do_dup(td, DUP_FIXED, fd, newmin, td->td_retval);
+		break;
+
 	case F_GETFD:
 		td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
 		FILEDESC_UNLOCK(fdp);
 		break;
 
 	case F_SETFD:
 		*pop = (*pop &~ UF_EXCLOSE) |
 		    (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
 		FILEDESC_UNLOCK(fdp);
 		break;
 
 	case F_GETFL:

Fix: 

See "Full Description" above.
How-To-Repeat:       Not an actual problem per se unless a lacking portability feature
is counted as one.
Comment 1 Antoine Brodin freebsd_committer freebsd_triage 2008-02-23 00:02:50 UTC
State Changed
From-To: open->closed

Close: superseded by PR 120233 that has an updated patch.