bin/75767: WANTED: "fdclose" function in libc

Ronald F.Guilmette rfg at monkeys.com
Mon Jan 3 22:00:53 GMT 2005


>Number:         75767
>Category:       bin
>Synopsis:       WANTED: "fdclose" function in libc
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jan 03 22:00:51 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator:     Ronald F. Guilmette
>Release:        FreeBSD 4.10-RELEASE i386
>Organization:
Infinite Monkeys & Co.
>Environment:
System: FreeBSD segfault.monkeys.com 4.10-RELEASE FreeBSD 4.10-RELEASE #0: Wed Oct 27 13:02:03 PDT 2004 rfg at segfault.monkeys.com:/usr/src/sys/compile/rfg20041027 i386

>Description:

In certain contexts, and when using certain programming styles, it is
important to be able to symmetrically "undo" the effects of various calls
to various standard library (libc) routines.

At present, due to the lack of an "fdclose" primitive in libc, it is not
easily possible to symmetrically undo the effects of a call to the "fdopen"
function.  One may call fclose on an open stdio file pointer, however the
fclose function actually has two effects, i.e.:

   1)  recycling the stdio file descriptor that was allocated by the prior
	call to fdopen() into the pool of available stdio file descriptors
	and...

   2)  calling close(2) on the underlying UNIX file descriptor.

Unfortunately, only the first of these two effects actually constitutes
a symmetric undo-ing of the actions performed by a call to fdopen.  The
second effect is an unfortunate (and undesirable) side-effect of fclose
in some instances.

Thus, it would be useful to have a new function in libc, i.e. an "fdclose"
function whose only effect would be to recycle a stdio file descriptor
into the pool of free/available stdio file descriptors *without* also
closing the underlying UNIX file descriptor.

>How-To-Repeat:

	int fd;
	FILE *fp;

	fd = open ("foo", O_RDWR | O_CREAT, 0666);
	fp = fdopen (fd, "r+");
	...
	fclose (fp);
	write (fd, buf, size);  /* A problematic error occurs here! */

>Fix:

Add a new "fdclose" function to libc.  This should have the same effects
as fclose, except that it should *not* close the underlying UNIX file
descriptor.
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list