Browse Source

libc: posix_fallocate must return an error number on failure

posix_fallocate implementation in uClibc relies on fallocate
system call - it just returns what fallocate returns. However
fallocate returns -1 on failure and assigns an error number
to errno variable. In the same time posix_fallocate must
return an error number but not -1.

What does this patch: if fallocate returns -1 then posix_fallocate
returns errno. Otherwise posix_fallocate returns 0 on success.

However there is a side effect - posix_fallocate sets errno on
failure because fallocate does it. But POSIX does not forbid it
thus it's not a problem.

Signed-off-by: Yuriy Kolerov <yuriy.kolerov@synopsys.com>
Yuriy Kolerov 9 years ago
parent
commit
9fae2ad993

+ 4 - 1
libc/sysdeps/linux/common/posix_fallocate.c

@@ -12,12 +12,15 @@
 #include <fcntl.h>
 #include <fcntl.h>
 #include <bits/kernel-features.h>
 #include <bits/kernel-features.h>
 #include <stdint.h>
 #include <stdint.h>
+#include <errno.h>
 
 
 #if defined __NR_fallocate
 #if defined __NR_fallocate
 extern __typeof(fallocate) __libc_fallocate attribute_hidden;
 extern __typeof(fallocate) __libc_fallocate attribute_hidden;
 int posix_fallocate(int fd, __off_t offset, __off_t len)
 int posix_fallocate(int fd, __off_t offset, __off_t len)
 {
 {
-	return __libc_fallocate(fd, 0, offset, len);
+	if (__libc_fallocate(fd, 0, offset, len))
+		return errno;
+	return 0;
 }
 }
 # if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
 # if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
 strong_alias(posix_fallocate,posix_fallocate64)
 strong_alias(posix_fallocate,posix_fallocate64)

+ 4 - 1
libc/sysdeps/linux/common/posix_fallocate64.c

@@ -12,6 +12,7 @@
 #include <fcntl.h>
 #include <fcntl.h>
 #include <bits/kernel-features.h>
 #include <bits/kernel-features.h>
 #include <stdint.h>
 #include <stdint.h>
+#include <errno.h>
 
 
 #if defined __NR_fallocate
 #if defined __NR_fallocate
 # if __WORDSIZE == 64
 # if __WORDSIZE == 64
@@ -20,7 +21,9 @@
 extern __typeof(fallocate64) __libc_fallocate64 attribute_hidden;
 extern __typeof(fallocate64) __libc_fallocate64 attribute_hidden;
 int posix_fallocate64(int fd, __off64_t offset, __off64_t len)
 int posix_fallocate64(int fd, __off64_t offset, __off64_t len)
 {
 {
-	return __libc_fallocate64(fd, 0, offset, len);
+	if (__libc_fallocate64(fd, 0, offset, len))
+		return errno;
+	return 0;
 }
 }
 # else
 # else
 #  error your machine is neither 32 bit or 64 bit ... it must be magical
 #  error your machine is neither 32 bit or 64 bit ... it must be magical