From 661f76699fe78fa4ae7087e6da5c7b88c6c505a8 Mon Sep 17 00:00:00 2001 From: kleink Date: Sat, 25 Oct 2003 01:18:01 +0000 Subject: [PATCH] POSIX: when attempting to fdatasync(2) a file which is not open for writing, fail with EBADF. --- lib/libc/sys/fdatasync.2 | 6 +++--- sys/kern/vfs_syscalls.c | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/libc/sys/fdatasync.2 b/lib/libc/sys/fdatasync.2 index d8dd19536db6..5c3a33673397 100644 --- a/lib/libc/sys/fdatasync.2 +++ b/lib/libc/sys/fdatasync.2 @@ -1,4 +1,4 @@ -.\" $NetBSD: fdatasync.2,v 1.13 2003/04/16 13:34:51 wiz Exp $ +.\" $NetBSD: fdatasync.2,v 1.14 2003/10/25 01:18:01 kleink Exp $ .\" .\" Copyright (c) 1998 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -34,7 +34,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd May 30, 1998 +.Dd October 25, 2003 .Dt FDATASYNC 2 .Os .Sh NAME @@ -75,7 +75,7 @@ function will fail if: .It Bq Er EBADF The .Fa fd -argument is not a valid file descriptor. +argument is not a valid file descriptor open for writing. .It Bq Er EINVAL This implementation does not support synchronized I/O for this file. .It Bq Er ENOSYS diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index 22316ec33859..eb9cb777b947 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -1,4 +1,4 @@ -/* $NetBSD: vfs_syscalls.c,v 1.198 2003/10/15 17:26:38 thorpej Exp $ */ +/* $NetBSD: vfs_syscalls.c,v 1.199 2003/10/25 01:18:01 kleink Exp $ */ /* * Copyright (c) 1989, 1993 @@ -37,7 +37,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.198 2003/10/15 17:26:38 thorpej Exp $"); +__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.199 2003/10/25 01:18:01 kleink Exp $"); #include "opt_compat_netbsd.h" #include "opt_compat_43.h" @@ -3025,6 +3025,10 @@ sys_fdatasync(l, v, retval) /* getvnode() will use the descriptor for us */ if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) return (error); + if ((fp->f_flag & FWRITE) == 0) { + FILE_UNUSE(fp, p); + return (EBADF); + } vp = (struct vnode *)fp->f_data; vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p);