From 6445a706cf9a6be799b9fc8ae7b724ebc450b55b Mon Sep 17 00:00:00 2001 From: sin-ack Date: Sat, 1 Oct 2022 11:20:08 +0000 Subject: [PATCH] Kernel+LibC: Implement `readlinkat(2)` Co-Authored-By: Daniel Bertalan --- Kernel/API/Syscall.h | 1 + Kernel/Syscalls/readlink.cpp | 2 +- Userland/Libraries/LibC/serenity.cpp | 3 ++- Userland/Libraries/LibC/unistd.cpp | 10 ++++++++-- Userland/Libraries/LibC/unistd.h | 1 + Userland/Libraries/LibCore/System.cpp | 5 +++-- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Kernel/API/Syscall.h b/Kernel/API/Syscall.h index cd58e676bb..cabc347576 100644 --- a/Kernel/API/Syscall.h +++ b/Kernel/API/Syscall.h @@ -389,6 +389,7 @@ struct SC_execve_params { struct SC_readlink_params { StringArgument path; MutableBufferArgument buffer; + int dirfd; }; struct SC_link_params { diff --git a/Kernel/Syscalls/readlink.cpp b/Kernel/Syscalls/readlink.cpp index 593eb862d3..0d2d187456 100644 --- a/Kernel/Syscalls/readlink.cpp +++ b/Kernel/Syscalls/readlink.cpp @@ -17,7 +17,7 @@ ErrorOr Process::sys$readlink(Userspaceview(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, current_directory())); + auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, TRY(custody_for_dirfd(params.dirfd)))); if (!description->metadata().is_symlink()) return EINVAL; diff --git a/Userland/Libraries/LibC/serenity.cpp b/Userland/Libraries/LibC/serenity.cpp index 11c9524d4e..45f1bab522 100644 --- a/Userland/Libraries/LibC/serenity.cpp +++ b/Userland/Libraries/LibC/serenity.cpp @@ -105,7 +105,8 @@ int serenity_readlink(char const* path, size_t path_length, char* buffer, size_t { Syscall::SC_readlink_params small_params { { path, path_length }, - { buffer, buffer_size } + { buffer, buffer_size }, + AT_FDCWD }; int rc = syscall(SC_readlink, &small_params); __RETURN_WITH_ERRNO(rc, rc, -1); diff --git a/Userland/Libraries/LibC/unistd.cpp b/Userland/Libraries/LibC/unistd.cpp index 8e732f197a..b5dc377770 100644 --- a/Userland/Libraries/LibC/unistd.cpp +++ b/Userland/Libraries/LibC/unistd.cpp @@ -630,10 +630,16 @@ int sethostname(char const* hostname, ssize_t size) __RETURN_WITH_ERRNO(rc, rc, -1); } -// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html +// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlinkat.html ssize_t readlink(char const* path, char* buffer, size_t size) { - Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } }; + return readlinkat(AT_FDCWD, path, buffer, size); +} + +// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlinkat.html +ssize_t readlinkat(int dirfd, char const* path, char* buffer, size_t size) +{ + Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size }, dirfd }; int rc = syscall(SC_readlink, ¶ms); // Return the number of bytes placed in the buffer, not the full path size. __RETURN_WITH_ERRNO(rc, min((size_t)rc, size), -1); diff --git a/Userland/Libraries/LibC/unistd.h b/Userland/Libraries/LibC/unistd.h index ef8c28ad47..02d4bc672a 100644 --- a/Userland/Libraries/LibC/unistd.h +++ b/Userland/Libraries/LibC/unistd.h @@ -88,6 +88,7 @@ int usleep(useconds_t); int gethostname(char*, size_t); int sethostname(char const*, ssize_t); ssize_t readlink(char const* path, char* buffer, size_t); +ssize_t readlinkat(int dirfd, char const* path, char* buffer, size_t); char* ttyname(int fd); int ttyname_r(int fd, char* buffer, size_t); off_t lseek(int fd, off_t, int whence); diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index f58b4952b7..e4576c4f63 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -1386,8 +1386,9 @@ ErrorOr readlink(StringView pathname) char data[PATH_MAX]; #ifdef AK_OS_SERENITY Syscall::SC_readlink_params small_params { - { pathname.characters_without_null_termination(), pathname.length() }, - { data, sizeof(data) } + .path = { pathname.characters_without_null_termination(), pathname.length() }, + .buffer = { data, sizeof(data) }, + .dirfd = AT_FDCWD, }; int rc = syscall(SC_readlink, &small_params); HANDLE_SYSCALL_RETURN_VALUE("readlink", rc, DeprecatedString(data, rc));