From a1b9a2693521ea37359aaa173bab8ac5375eb8d9 Mon Sep 17 00:00:00 2001 From: Gabriel Ravier Date: Sat, 10 Jun 2023 16:51:04 +0200 Subject: [PATCH] Fix syscall(2) returning -errno instead of using POSIX errno scheme MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As of now, the syscall function is implemented as alike to how the linux kernel sycall ABI works, returning -errno upon errors without setting the value of errno. However, this does not conform to the expectations of most software, which expect it to return -1 and set errno, which is how it works on other libcs, which document it accordingly: > The return value is defined by the system call being invoked. In > general, a 0 return value indicates success. A -1 return value > indicates an error, and an error number is stored in errno. - Linux man-pages, syscall(2) > The return value is the return value from the system call, unless > the system call failed. In that case, ‘syscall’ returns ‘-1’ and > sets ‘errno’ to an error code that the system call returned. - glibc manual, (libc)System Calls > When the C-bit is set, syscall returns -1 and sets the external > variable errno (see intro(2)). - 4BSD manual, syscall(2) > A -1 return value indicates an error, and an error code is stored in > errno. - 4.4BSD, FreeBSD, OpenBSD and NetBSD manuals (same quote is found in all of them), syscall(2) This patch corrects the syscall function to work in the same way as in other libcs. --- libc/stdio/syscall.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libc/stdio/syscall.c b/libc/stdio/syscall.c index 7bee0f8ff..34d4012da 100644 --- a/libc/stdio/syscall.c +++ b/libc/stdio/syscall.c @@ -32,7 +32,8 @@ long syscall(long number, ...) { switch (number) { default: - return -ENOSYS; + errno = ENOSYS; + return -1; case SYS_gettid: return gettid(); case SYS_getrandom: { @@ -43,7 +44,7 @@ long syscall(long number, ...) { unsigned flags = va_arg(va, unsigned); va_end(va); ssize_t rc = getrandom(buf, buflen, flags); - return rc == -1 ? -errno : rc; + return rc; } } }