Fix syscall(2) returning -errno instead of using POSIX errno scheme (#830)

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.
This commit is contained in:
Gabriel Ravier 2023-06-11 19:33:28 +02:00 committed by GitHub
parent e47c0cc929
commit 50d8d953ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;
}
}
}