standards/186028: incorrect return values for posix_fallocate()

Konstantin Belousov kostikbel at gmail.com
Fri Jan 24 18:12:54 UTC 2014


On Thu, Jan 23, 2014 at 09:20:11PM +0000, Bob Bishop wrote:
> Hi,
> 
> On 23 Jan 2014, at 09:40, Konstantin Belousov <kostikbel at gmail.com> wrote:
> 
> > On Thu, Jan 23, 2014 at 08:58:58AM +0000, Gennady Proskurin wrote:
> >> 
> >>> Number:         186028
> >>> Category:       standards
> >>> Synopsis:       incorrect return values for posix_fallocate()
> >>> Confidential:   no
> >>> Severity:       non-critical
> >>> Priority:       low
> >>> Responsible:    freebsd-standards
> >>> State:          open
> >>> Quarter:        
> >>> Keywords:       
> >>> Date-Required:
> >>> Class:          sw-bug
> >>> Submitter-Id:   current-users
> >>> Arrival-Date:   Thu Jan 23 09:00:00 UTC 2014
> >>> Closed-Date:
> >>> Last-Modified:
> >>> Originator:     Gennady Proskurin
> >>> Release:        FreeBSD 11.0-CURRENT
> >>> Organization:
> >>> Environment:
> >> FreeBSD gpr.nnz-home.ru 11.0-CURRENT FreeBSD 11.0-CURRENT #0 r260472+743aa78(svn_head): Fri Jan 10 05:28:05 MSK 2014     gpr at gpr.nnz-home.ru:/usr/obj/usr/src/freebsd-head/sys/GPR  amd64
> >>> Description:
> >> In case of error, posix_fallocate() should return error code itself, but in FreeBSD it returns -1 and sets errno.
> 
> FWIW at least posix_madvise() and posix_fadvise() have the same problem.

Indeed, I suppose the following should also fix two syscalls noted.
I hope that some doc committer would do the pass over man pages
following Bruce' suggestions.

diff --git a/lib/libc/gen/pmadvise.c b/lib/libc/gen/pmadvise.c
index 60cef63..68b1181 100644
--- a/lib/libc/gen/pmadvise.c
+++ b/lib/libc/gen/pmadvise.c
@@ -12,5 +12,14 @@ __FBSDID("$FreeBSD$");
 int
 posix_madvise(void *address, size_t size, int how)
 {
-	return madvise(address, size, how);
+	int ret, saved_errno;
+
+	saved_errno = errno;
+	if (madvise(address, size, how) == -1) {
+		ret = errno;
+		errno = saved_errno;
+	} else {
+		ret = 0;
+	}
+	return (ret);
 }
diff --git a/lib/libc/sys/madvise.2 b/lib/libc/sys/madvise.2
index b5ea6b2..755ecc2 100644
--- a/lib/libc/sys/madvise.2
+++ b/lib/libc/sys/madvise.2
@@ -50,7 +50,10 @@ allows a process that has knowledge of its memory behavior
 to describe it to the system.
 The
 .Fn posix_madvise
-interface is identical and is provided for standards conformance.
+interface is identical, except it returns an error number on error and does
+not modify
+.Va errno ,
+and is provided for standards conformance.
 .Pp
 The known behaviors are:
 .Bl -tag -width MADV_SEQUENTIAL
diff --git a/lib/libc/sys/posix_fadvise.2 b/lib/libc/sys/posix_fadvise.2
index 7a9a648..96c99d2 100644
--- a/lib/libc/sys/posix_fadvise.2
+++ b/lib/libc/sys/posix_fadvise.2
@@ -93,7 +93,7 @@ Future access to this data may require a read operation.
 .Sh ERRORS
 The
 .Fn posix_fadvise
-system call will fail if:
+system call returns zero on success, and an error on failure:
 .Bl -tag -width Er
 .It Bq Er EBADF
 The
diff --git a/lib/libc/sys/posix_fallocate.2 b/lib/libc/sys/posix_fallocate.2
index 1460764..7501777 100644
--- a/lib/libc/sys/posix_fallocate.2
+++ b/lib/libc/sys/posix_fallocate.2
@@ -83,9 +83,8 @@ that reduces the file size to a size smaller than
 If successful,
 .Fn posix_fallocate
 returns zero.
-It returns error number on failure, without setting
-.Va errno
-variable.
+It returns an error on failure, without setting
+.Va errno .
 .Sh ERRORS
 Possible failure conditions:
 .Bl -tag -width Er
diff --git a/sys/compat/freebsd32/freebsd32_misc.c b/sys/compat/freebsd32/freebsd32_misc.c
index e4ffbe4..c7b677f 100644
--- a/sys/compat/freebsd32/freebsd32_misc.c
+++ b/sys/compat/freebsd32/freebsd32_misc.c
@@ -3005,8 +3005,10 @@ freebsd32_posix_fadvise(struct thread *td,
     struct freebsd32_posix_fadvise_args *uap)
 {
 
-	return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
-	    PAIR32TO64(off_t, uap->len), uap->advice));
+	td->td_retval[0] = kern_posix_fadvise(td, uap->fd,
+	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len),
+	    uap->advice);
+	return (0);
 }
 
 int
diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index b864c90..4be9738 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -4725,6 +4725,7 @@ int
 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
 {
 
-	return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
-	    uap->advice));
+	td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
+	    uap->len, uap->advice);
+	return (0);
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 834 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-standards/attachments/20140124/9e197b8d/attachment.sig>


More information about the freebsd-standards mailing list