tools/nolibc: fix return type of getpagesize()
[linux-block.git] / tools / include / nolibc / sys.h
index 5464f93e863ef4d560f2c5190e4605ca74fb2460..c151533ba8e9128d03c7796e9574ce8000a288b1 100644 (file)
@@ -21,7 +21,6 @@
 #include <linux/auxvec.h>
 #include <linux/fcntl.h> /* for O_* and AT_* */
 #include <linux/stat.h>  /* for statx() */
-#include <linux/reboot.h> /* for LINUX_REBOOT_* */
 #include <linux/prctl.h>
 
 #include "arch.h"
 #include "types.h"
 
 
+/* Syscall return helper for library routines, set errno as -ret when ret is in
+ * range of [-MAX_ERRNO, -1]
+ *
+ * Note, No official reference states the errno range here aligns with musl
+ * (src/internal/syscall_ret.c) and glibc (sysdeps/unix/sysv/linux/sysdep.h)
+ */
+
+static __inline__ __attribute__((unused, always_inline))
+long __sysret(unsigned long ret)
+{
+       if (ret >= (unsigned long)-MAX_ERRNO) {
+               SET_ERRNO(-(long)ret);
+               return -1;
+       }
+       return ret;
+}
+
 /* Functions in this file only describe syscalls. They're declared static so
  * that the compiler usually decides to inline them while still being allowed
  * to pass a pointer to one of their instances. Each syscall exists in two
@@ -66,26 +82,19 @@ void *sys_brk(void *addr)
 static __attribute__((unused))
 int brk(void *addr)
 {
-       void *ret = sys_brk(addr);
-
-       if (!ret) {
-               SET_ERRNO(ENOMEM);
-               return -1;
-       }
-       return 0;
+       return __sysret(sys_brk(addr) ? 0 : -ENOMEM);
 }
 
 static __attribute__((unused))
 void *sbrk(intptr_t inc)
 {
-       void *ret;
-
        /* first call to find current end */
-       if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc))
+       void *ret = sys_brk(0);
+
+       if (ret && sys_brk(ret + inc) == ret + inc)
                return ret + inc;
 
-       SET_ERRNO(ENOMEM);
-       return (void *)-1;
+       return (void *)__sysret(-ENOMEM);
 }
 
 
@@ -102,13 +111,7 @@ int sys_chdir(const char *path)
 static __attribute__((unused))
 int chdir(const char *path)
 {
-       int ret = sys_chdir(path);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_chdir(path));
 }
 
 
@@ -131,13 +134,7 @@ int sys_chmod(const char *path, mode_t mode)
 static __attribute__((unused))
 int chmod(const char *path, mode_t mode)
 {
-       int ret = sys_chmod(path, mode);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_chmod(path, mode));
 }
 
 
@@ -160,13 +157,7 @@ int sys_chown(const char *path, uid_t owner, gid_t group)
 static __attribute__((unused))
 int chown(const char *path, uid_t owner, gid_t group)
 {
-       int ret = sys_chown(path, owner, group);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_chown(path, owner, group));
 }
 
 
@@ -183,13 +174,7 @@ int sys_chroot(const char *path)
 static __attribute__((unused))
 int chroot(const char *path)
 {
-       int ret = sys_chroot(path);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_chroot(path));
 }
 
 
@@ -206,13 +191,7 @@ int sys_close(int fd)
 static __attribute__((unused))
 int close(int fd)
 {
-       int ret = sys_close(fd);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_close(fd));
 }
 
 
@@ -229,13 +208,7 @@ int sys_dup(int fd)
 static __attribute__((unused))
 int dup(int fd)
 {
-       int ret = sys_dup(fd);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_dup(fd));
 }
 
 
@@ -258,13 +231,7 @@ int sys_dup2(int old, int new)
 static __attribute__((unused))
 int dup2(int old, int new)
 {
-       int ret = sys_dup2(old, new);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_dup2(old, new));
 }
 
 
@@ -282,13 +249,7 @@ int sys_dup3(int old, int new, int flags)
 static __attribute__((unused))
 int dup3(int old, int new, int flags)
 {
-       int ret = sys_dup3(old, new, flags);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_dup3(old, new, flags));
 }
 #endif
 
@@ -306,13 +267,7 @@ int sys_execve(const char *filename, char *const argv[], char *const envp[])
 static __attribute__((unused))
 int execve(const char *filename, char *const argv[], char *const envp[])
 {
-       int ret = sys_execve(filename, argv, envp);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_execve(filename, argv, envp));
 }
 
 
@@ -359,13 +314,7 @@ pid_t sys_fork(void)
 static __attribute__((unused))
 pid_t fork(void)
 {
-       pid_t ret = sys_fork();
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_fork());
 }
 
 
@@ -382,13 +331,7 @@ int sys_fsync(int fd)
 static __attribute__((unused))
 int fsync(int fd)
 {
-       int ret = sys_fsync(fd);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_fsync(fd));
 }
 
 
@@ -405,13 +348,7 @@ int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
 static __attribute__((unused))
 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
 {
-       int ret = sys_getdents64(fd, dirp, count);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_getdents64(fd, dirp, count));
 }
 
 
@@ -449,13 +386,7 @@ pid_t sys_getpgid(pid_t pid)
 static __attribute__((unused))
 pid_t getpgid(pid_t pid)
 {
-       pid_t ret = sys_getpgid(pid);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_getpgid(pid));
 }
 
 
@@ -529,21 +460,13 @@ pid_t gettid(void)
 static unsigned long getauxval(unsigned long key);
 
 /*
- * long getpagesize(void);
+ * int getpagesize(void);
  */
 
 static __attribute__((unused))
-long getpagesize(void)
+int getpagesize(void)
 {
-       long ret;
-
-       ret = getauxval(AT_PAGESZ);
-       if (!ret) {
-               SET_ERRNO(ENOENT);
-               return -1;
-       }
-
-       return ret;
+       return __sysret(getauxval(AT_PAGESZ) ?: -ENOENT);
 }
 
 
@@ -564,13 +487,7 @@ int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
 static __attribute__((unused))
 int gettimeofday(struct timeval *tv, struct timezone *tz)
 {
-       int ret = sys_gettimeofday(tv, tz);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_gettimeofday(tv, tz));
 }
 
 
@@ -608,13 +525,7 @@ int sys_ioctl(int fd, unsigned long req, void *value)
 static __attribute__((unused))
 int ioctl(int fd, unsigned long req, void *value)
 {
-       int ret = sys_ioctl(fd, req, value);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_ioctl(fd, req, value));
 }
 
 /*
@@ -630,13 +541,7 @@ int sys_kill(pid_t pid, int signal)
 static __attribute__((unused))
 int kill(pid_t pid, int signal)
 {
-       int ret = sys_kill(pid, signal);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_kill(pid, signal));
 }
 
 
@@ -659,13 +564,7 @@ int sys_link(const char *old, const char *new)
 static __attribute__((unused))
 int link(const char *old, const char *new)
 {
-       int ret = sys_link(old, new);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_link(old, new));
 }
 
 
@@ -686,13 +585,7 @@ off_t sys_lseek(int fd, off_t offset, int whence)
 static __attribute__((unused))
 off_t lseek(int fd, off_t offset, int whence)
 {
-       off_t ret = sys_lseek(fd, offset, whence);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_lseek(fd, offset, whence));
 }
 
 
@@ -715,13 +608,29 @@ int sys_mkdir(const char *path, mode_t mode)
 static __attribute__((unused))
 int mkdir(const char *path, mode_t mode)
 {
-       int ret = sys_mkdir(path, mode);
+       return __sysret(sys_mkdir(path, mode));
+}
 
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+/*
+ * int rmdir(const char *path);
+ */
+
+static __attribute__((unused))
+int sys_rmdir(const char *path)
+{
+#ifdef __NR_rmdir
+       return my_syscall1(__NR_rmdir, path);
+#elif defined(__NR_unlinkat)
+       return my_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR);
+#else
+       return -ENOSYS;
+#endif
+}
+
+static __attribute__((unused))
+int rmdir(const char *path)
+{
+       return __sysret(sys_rmdir(path));
 }
 
 
@@ -744,35 +653,14 @@ long sys_mknod(const char *path, mode_t mode, dev_t dev)
 static __attribute__((unused))
 int mknod(const char *path, mode_t mode, dev_t dev)
 {
-       int ret = sys_mknod(path, mode, dev);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_mknod(path, mode, dev));
 }
 
-#ifndef MAP_SHARED
-#define MAP_SHARED             0x01    /* Share changes */
-#define MAP_PRIVATE            0x02    /* Changes are private */
-#define MAP_SHARED_VALIDATE    0x03    /* share + validate extension flags */
-#endif
-
-#ifndef MAP_FAILED
-#define MAP_FAILED ((void *)-1)
-#endif
-
 #ifndef sys_mmap
 static __attribute__((unused))
 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
               off_t offset)
 {
-#ifndef my_syscall6
-       /* Function not implemented. */
-       return (void *)-ENOSYS;
-#else
-
        int n;
 
 #if defined(__NR_mmap2)
@@ -783,20 +671,18 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
 #endif
 
        return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset);
-#endif
 }
 #endif
 
+/* Note that on Linux, MAP_FAILED is -1 so we can use the generic __sysret()
+ * which returns -1 upon error and still satisfy user land that checks for
+ * MAP_FAILED.
+ */
+
 static __attribute__((unused))
 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
 {
-       void *ret = sys_mmap(addr, length, prot, flags, fd, offset);
-
-       if ((unsigned long)ret >= -4095UL) {
-               SET_ERRNO(-(long)ret);
-               ret = MAP_FAILED;
-       }
-       return ret;
+       return (void *)__sysret((unsigned long)sys_mmap(addr, length, prot, flags, fd, offset));
 }
 
 static __attribute__((unused))
@@ -808,13 +694,7 @@ int sys_munmap(void *addr, size_t length)
 static __attribute__((unused))
 int munmap(void *addr, size_t length)
 {
-       int ret = sys_munmap(addr, length);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_munmap(addr, length));
 }
 
 /*
@@ -834,13 +714,7 @@ int mount(const char *src, const char *tgt,
           const char *fst, unsigned long flags,
           const void *data)
 {
-       int ret = sys_mount(src, tgt, fst, flags, data);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_mount(src, tgt, fst, flags, data));
 }
 
 
@@ -864,7 +738,6 @@ static __attribute__((unused))
 int open(const char *path, int flags, ...)
 {
        mode_t mode = 0;
-       int ret;
 
        if (flags & O_CREAT) {
                va_list args;
@@ -874,13 +747,31 @@ int open(const char *path, int flags, ...)
                va_end(args);
        }
 
-       ret = sys_open(path, flags, mode);
+       return __sysret(sys_open(path, flags, mode));
+}
 
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+
+/*
+ * int pipe2(int pipefd[2], int flags);
+ * int pipe(int pipefd[2]);
+ */
+
+static __attribute__((unused))
+int sys_pipe2(int pipefd[2], int flags)
+{
+       return my_syscall2(__NR_pipe2, pipefd, flags);
+}
+
+static __attribute__((unused))
+int pipe2(int pipefd[2], int flags)
+{
+       return __sysret(sys_pipe2(pipefd, flags));
+}
+
+static __attribute__((unused))
+int pipe(int pipefd[2])
+{
+       return pipe2(pipefd, 0);
 }
 
 
@@ -900,13 +791,7 @@ static __attribute__((unused))
 int prctl(int option, unsigned long arg2, unsigned long arg3,
                      unsigned long arg4, unsigned long arg5)
 {
-       int ret = sys_prctl(option, arg2, arg3, arg4, arg5);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5));
 }
 
 
@@ -923,13 +808,7 @@ int sys_pivot_root(const char *new, const char *old)
 static __attribute__((unused))
 int pivot_root(const char *new, const char *old)
 {
-       int ret = sys_pivot_root(new, old);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_pivot_root(new, old));
 }
 
 
@@ -958,13 +837,7 @@ int sys_poll(struct pollfd *fds, int nfds, int timeout)
 static __attribute__((unused))
 int poll(struct pollfd *fds, int nfds, int timeout)
 {
-       int ret = sys_poll(fds, nfds, timeout);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_poll(fds, nfds, timeout));
 }
 
 
@@ -981,13 +854,7 @@ ssize_t sys_read(int fd, void *buf, size_t count)
 static __attribute__((unused))
 ssize_t read(int fd, void *buf, size_t count)
 {
-       ssize_t ret = sys_read(fd, buf, count);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_read(fd, buf, count));
 }
 
 
@@ -1005,13 +872,7 @@ ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
 static __attribute__((unused))
 int reboot(int cmd)
 {
-       int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0));
 }
 
 
@@ -1028,13 +889,7 @@ int sys_sched_yield(void)
 static __attribute__((unused))
 int sched_yield(void)
 {
-       int ret = sys_sched_yield();
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_sched_yield());
 }
 
 
@@ -1074,13 +929,7 @@ int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeva
 static __attribute__((unused))
 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
 {
-       int ret = sys_select(nfds, rfds, wfds, efds, timeout);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_select(nfds, rfds, wfds, efds, timeout));
 }
 
 
@@ -1097,13 +946,7 @@ int sys_setpgid(pid_t pid, pid_t pgid)
 static __attribute__((unused))
 int setpgid(pid_t pid, pid_t pgid)
 {
-       int ret = sys_setpgid(pid, pgid);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_setpgid(pid, pgid));
 }
 
 
@@ -1120,55 +963,41 @@ pid_t sys_setsid(void)
 static __attribute__((unused))
 pid_t setsid(void)
 {
-       pid_t ret = sys_setsid();
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_setsid());
 }
 
-#if defined(__NR_statx)
 /*
  * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
+ * int stat(const char *path, struct stat *buf);
  */
 
 static __attribute__((unused))
 int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
 {
+#ifdef __NR_statx
        return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
+#else
+       return -ENOSYS;
+#endif
 }
 
 static __attribute__((unused))
 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
 {
-       int ret = sys_statx(fd, path, flags, mask, buf);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_statx(fd, path, flags, mask, buf));
 }
-#endif
 
-/*
- * int stat(const char *path, struct stat *buf);
- * Warning: the struct stat's layout is arch-dependent.
- */
 
-#if defined(__NR_statx) && !defined(__NR_newfstatat) && !defined(__NR_stat)
-/*
- * Maybe we can just use statx() when available for all architectures?
- */
 static __attribute__((unused))
-int sys_stat(const char *path, struct stat *buf)
+int stat(const char *path, struct stat *buf)
 {
        struct statx statx;
        long ret;
 
-       ret = sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx);
+       ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
+       if (ret == -1)
+               return ret;
+
        buf->st_dev          = ((statx.stx_dev_minor & 0xff)
                               | (statx.stx_dev_major << 8)
                               | ((statx.stx_dev_minor & ~0xff) << 12));
@@ -1189,53 +1018,8 @@ int sys_stat(const char *path, struct stat *buf)
        buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec;
        buf->st_ctim.tv_sec  = statx.stx_ctime.tv_sec;
        buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
-       return ret;
-}
-#else
-static __attribute__((unused))
-int sys_stat(const char *path, struct stat *buf)
-{
-       struct sys_stat_struct stat;
-       long ret;
-
-#ifdef __NR_newfstatat
-       /* only solution for arm64 */
-       ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0);
-#elif defined(__NR_stat)
-       ret = my_syscall2(__NR_stat, path, &stat);
-#else
-       return -ENOSYS;
-#endif
-       buf->st_dev          = stat.st_dev;
-       buf->st_ino          = stat.st_ino;
-       buf->st_mode         = stat.st_mode;
-       buf->st_nlink        = stat.st_nlink;
-       buf->st_uid          = stat.st_uid;
-       buf->st_gid          = stat.st_gid;
-       buf->st_rdev         = stat.st_rdev;
-       buf->st_size         = stat.st_size;
-       buf->st_blksize      = stat.st_blksize;
-       buf->st_blocks       = stat.st_blocks;
-       buf->st_atim.tv_sec  = stat.st_atime;
-       buf->st_atim.tv_nsec = stat.st_atime_nsec;
-       buf->st_mtim.tv_sec  = stat.st_mtime;
-       buf->st_mtim.tv_nsec = stat.st_mtime_nsec;
-       buf->st_ctim.tv_sec  = stat.st_ctime;
-       buf->st_ctim.tv_nsec = stat.st_ctime_nsec;
-       return ret;
-}
-#endif
-
-static __attribute__((unused))
-int stat(const char *path, struct stat *buf)
-{
-       int ret = sys_stat(path, buf);
 
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return 0;
 }
 
 
@@ -1258,13 +1042,7 @@ int sys_symlink(const char *old, const char *new)
 static __attribute__((unused))
 int symlink(const char *old, const char *new)
 {
-       int ret = sys_symlink(old, new);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_symlink(old, new));
 }
 
 
@@ -1298,13 +1076,7 @@ int sys_umount2(const char *path, int flags)
 static __attribute__((unused))
 int umount2(const char *path, int flags)
 {
-       int ret = sys_umount2(path, flags);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_umount2(path, flags));
 }
 
 
@@ -1327,13 +1099,7 @@ int sys_unlink(const char *path)
 static __attribute__((unused))
 int unlink(const char *path)
 {
-       int ret = sys_unlink(path);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_unlink(path));
 }
 
 
@@ -1356,38 +1122,20 @@ pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
 static __attribute__((unused))
 pid_t wait(int *status)
 {
-       pid_t ret = sys_wait4(-1, status, 0, NULL);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_wait4(-1, status, 0, NULL));
 }
 
 static __attribute__((unused))
 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
 {
-       pid_t ret = sys_wait4(pid, status, options, rusage);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_wait4(pid, status, options, rusage));
 }
 
 
 static __attribute__((unused))
 pid_t waitpid(pid_t pid, int *status, int options)
 {
-       pid_t ret = sys_wait4(pid, status, options, NULL);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_wait4(pid, status, options, NULL));
 }
 
 
@@ -1404,13 +1152,7 @@ ssize_t sys_write(int fd, const void *buf, size_t count)
 static __attribute__((unused))
 ssize_t write(int fd, const void *buf, size_t count)
 {
-       ssize_t ret = sys_write(fd, buf, count);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_write(fd, buf, count));
 }
 
 
@@ -1427,13 +1169,7 @@ int sys_memfd_create(const char *name, unsigned int flags)
 static __attribute__((unused))
 int memfd_create(const char *name, unsigned int flags)
 {
-       ssize_t ret = sys_memfd_create(name, flags);
-
-       if (ret < 0) {
-               SET_ERRNO(-ret);
-               ret = -1;
-       }
-       return ret;
+       return __sysret(sys_memfd_create(name, flags));
 }
 
 /* make sure to include all global symbols */