From owner-freebsd-current@FreeBSD.ORG Sun Nov 23 10:54:11 2003 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7832C16A4CE for ; Sun, 23 Nov 2003 10:54:11 -0800 (PST) Received: from mail.rdslink.ro (mail.rdslink.ro [193.231.236.20]) by mx1.FreeBSD.org (Postfix) with ESMTP id 94FB943F75 for ; Sun, 23 Nov 2003 10:54:09 -0800 (PST) (envelope-from enache@rdslink.ro) Received: (qmail 2227 invoked from network); 23 Nov 2003 21:42:56 -0000 Received: from unknown (HELO ratsnest.hole) (81.196.245.141) by mail.rdslink.ro with SMTP; 23 Nov 2003 21:42:56 -0000 Date: Sun, 23 Nov 2003 16:14:08 +0200 From: Enache Adrian To: current@freebsd.org Message-ID: <20031123141408.GA1426@ratsnest.hole> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4i Subject: [PATCH] libc_r bug: successful close(2) sets errno to ENOTTY X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 Nov 2003 18:54:11 -0000 $ cc close.c -o close && ./close 0 0 $ cc close.c -lc_r -o close && ./close 0 25 $ cat close.c #include 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