Bug 120233

Summary: [libc] [patch] [request] FreeBSD is missing fcntl() operation F_DUP2FD
Product: Base System Reporter: jau
Component: kernAssignee: Antoine Brodin <antoine>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 6.3-STABLE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description jau 2008-02-03 07:20:00 UTC
This is a compatibility issue.

At least SunOS/Solaris and AIX both have an fcntl() operation F_DUP2FD which is
to dup2() as F_DUPFD is to dup().
Having support to this fcntl() operation would improve software portability
between FreeBSD and other UNIX style systems.
Additionally it would allow both dup() and dup2() be implemented as library
functions narrowing down the system call API.

This PR supersedes the 3.5 years old kern/70798 which provided the same feature
for FreeBSD-4.10. The old ticket is still in the state open and nobody has
cared as much as to say a simple "no" since its introduction.

This PR contains an upgraded patch against FreeBSD-6.3 sources implementing
the F_DUP2FD operation and a test program to show F_DUP2FD in action,
if it is supported.

I have had this modification in my own environments for some 4 years starting
from FreeBSD-4.x with no ill effects.
In fact in my own C library I have both dup() and dup2() only as wrappers for
fcntl() and everything works just fine.

Fix: Apply the attached patch.


Patch attached with submission follows:
How-To-Repeat: Try the test program shown below before and after applying the patch and notice
the difference in its output.


#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

static void
dumpstat (st)
    struct stat	*st;
{
    printf ("st_dev   = %ld\n", (long) st->st_dev);
    printf ("st_ino   = %ld\n", (long) st->st_ino);
    printf ("st_nlink = %ld\n", (long) st->st_nlink);
    printf ("st_uid   = %ld\n", (long) st->st_uid);
    printf ("st_gid   = %ld\n", (long) st->st_gid);
    printf ("st_rdev  = %ld\n", (long) st->st_rdev);
}

static void
viewfdstat (fd)
    int	    fd;
{
    struct stat	st1;

    if (fstat (fd, &st1) < 0) {
	fprintf (stderr, "fstat(%d): %s\n", fd, strerror (errno));
    }
    else {
	printf ("fstat(%d):\n", fd);

	dumpstat (&st1);
    }
}

int
main (ac, av)
    int	    ac;
    char    *av[];
{
    int	    fd;

    viewfdstat (0);

    fd = open ("/dev/null", O_RDWR, 0);

    viewfdstat (fd);

#ifndef	F_DUP2FD
#define F_DUP2FD    F_DUPFD
#warning This system does not support F_DUP2FD.
#endif

    if (fcntl (fd, F_DUP2FD, 0) < 0) {
	fprintf (stderr,
		 "fcntl(%d, F_DUP2FD, 0): %s\n",
		 fd, strerror (errno));
    }

    viewfdstat (0);

    return (0);
}
Comment 1 Robert Watson freebsd_committer freebsd_triage 2008-02-03 19:17:25 UTC
On Sun, 3 Feb 2008, Jukka Ukkonen wrote:

> I have had this modification in my own environments for some 4 years 
> starting from FreeBSD-4.x with no ill effects. In fact in my own C library I 
> have both dup() and dup2() only as wrappers for fcntl() and everything works 
> just fine.

No opinion on the remainder of the patch, but just wanted to chime in on this 
one point: once a system call is in the ABI, we generally won't ever remove 
it.  The reason is that this would break backward compatibility for older 
binaries and libraries that encode the old system call.  There may be an 
argument for adding the new fcntl() interface, but there isn't really an 
argument to remove the old system calls that overrides the need for backward 
compatibility.  I will generally comment that it's unfortunate that we end up 
with many APIs for the same operation, though, but that's generally a concern 
overriden by the need for portability.

Robert N M Watson
Computer Laboratory
University of Cambridge
Comment 2 Antoine Brodin freebsd_committer freebsd_triage 2008-02-23 10:05:42 UTC
Responsible Changed
From-To: freebsd-bugs->antoine

Take this.
Comment 3 dfilter service freebsd_committer freebsd_triage 2008-03-08 22:02:28 UTC
antoine     2008-03-08 22:02:21 UTC

  FreeBSD src repository

  Modified files:
    lib/libc/sys         fcntl.2 
    sys/kern             kern_descrip.c 
    sys/sys              fcntl.h 
    tools/regression/file/dup dup.c 
  Log:
  Introduce a new F_DUP2FD command to fcntl(2), for compatibility with
  Solaris and AIX.
  fcntl(fd, F_DUP2FD, arg) and dup2(fd, arg) are functionnaly equivalent.
  Document it.
  Add some regression tests (identical to the dup2(2) regression tests).
  
  PR:             120233
  Submitted by:   Jukka Ukkonen
  Approved by:    rwaston (mentor)
  MFC after:      1 month
  
  Revision  Changes    Path
  1.46      +37 -1     src/lib/libc/sys/fcntl.2
  1.325     +6 -1      src/sys/kern/kern_descrip.c
  1.18      +1 -0      src/sys/sys/fcntl.h
  1.3       +71 -2     src/tools/regression/file/dup/dup.c
_______________________________________________
cvs-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/cvs-all
To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
Comment 4 Antoine Brodin freebsd_committer freebsd_triage 2008-03-08 22:13:35 UTC
State Changed
From-To: open->patched

patched in HEAD.
Comment 5 dfilter service freebsd_committer freebsd_triage 2008-04-20 20:32:51 UTC
antoine     2008-04-20 19:32:46 UTC

  FreeBSD src repository

  Modified files:        (Branch: RELENG_7)
    lib/libc/sys         fcntl.2 
    sys/kern             kern_descrip.c 
    sys/sys              fcntl.h param.h 
    tools/regression/file/dup dup.c 
  Log:
  MFC to RELENG_7:
    Introduce a new F_DUP2FD command to fcntl(2), for compatibility with
    Solaris and AIX.
    fcntl(fd, F_DUP2FD, arg) and dup2(fd, arg) are functionnaly equivalent.
    Document it.
    Add some regression tests (identical to the dup2(2) regression tests).
  
    PR:             120233
    Submitted by:   Jukka Ukkonen
    Approved by:    rwaston (mentor)
    MFC after:      1 month
  
  Revision    Changes    Path
  1.45.2.2    +37 -1     src/lib/libc/sys/fcntl.2
  1.313.2.5   +6 -1      src/sys/kern/kern_descrip.c
  1.16.18.2   +1 -1      src/sys/sys/fcntl.h
  1.308.2.11  +1 -1      src/sys/sys/param.h
  1.2.2.1     +71 -2     src/tools/regression/file/dup/dup.c
_______________________________________________
cvs-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/cvs-all
To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
Comment 6 Antoine Brodin freebsd_committer freebsd_triage 2008-04-20 21:05:07 UTC
State Changed
From-To: patched->closed

Close: committed in HEAD and RELENG_7. 
Thanks!