Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 23 Nov 2003 16:14:08 +0200
From:      Enache Adrian <enache@rdslink.ro>
To:        current@freebsd.org
Subject:   [PATCH] libc_r bug: successful close(2) sets errno to ENOTTY
Message-ID:  <20031123141408.GA1426@ratsnest.hole>

next in thread | raw e-mail | index | archive | help
$ 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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20031123141408.GA1426>