[PATCH] libc_r bug: successful close(2) sets errno to ENOTTY

Enache Adrian enache at rdslink.ro
Sun Nov 23 10:54:11 PST 2003


$ cc close.c -o close && ./close
0
0

$ cc close.c -lc_r -o close && ./close
0
25

$ cat close.c
#include <errno.h>
main()
{
        int fd = open("/dev/null", 1);
        printf("%d\n", errno);
        close(fd);
        printf("%d\n", errno);
}

This confuses rather badly applications which assume errno is meaningful.
It could be fixed easily by repeating the trick from uthread/uthread_fd.c:

--- /arc/freebsd/src/lib/libc_r/uthread/uthread_close.c	Tue Jun 10 23:42:27 2003
+++ lib/libc_r/uthread/uthread_close.c	Sun Nov 23 00:50:30 2003
@@ -87,10 +87,12 @@ _close(int fd)
 		 */
 		if ((S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode))
 		    && (_thread_fd_getflags(fd) & O_NONBLOCK) == 0) {
+			int saved_errno = errno;
 			/* Get the current flags: */
 			flags = __sys_fcntl(fd, F_GETFL, NULL);
 			/* Clear the nonblocking file descriptor flag: */
 			__sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
+			errno = saved_errno;
 		}
 
 		/* XXX: Assumes well behaved threads. */

Regards,
Adi


More information about the freebsd-current mailing list