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

From: Enache Adrian <enache_at_rdslink.ro>
Date: Sun, 23 Nov 2003 16:14:08 +0200
$ 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
_at__at_ -87,10 +87,12 _at__at_ _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
Received on Sun Nov 23 2003 - 09:54:11 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:30 UTC