From 06aa7725a1334c6fa82ec2ac67e1fe20c97cdbdb Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 2 Sep 2023 14:27:04 +0200 Subject: [PATCH] libos: Stop using syscalls directly and proxy to libc --- libos/src/Directory.cpp | 5 ++--- libos/src/File.cpp | 5 ++--- libos/src/FileSystem.cpp | 39 ++++++++++++++++++++------------------- libos/src/Process.cpp | 17 ++++++++++------- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/libos/src/Directory.cpp b/libos/src/Directory.cpp index 876f2324..c8518710 100644 --- a/libos/src/Directory.cpp +++ b/libos/src/Directory.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include static bool should_skip_entry(const char* name, os::Directory::Filter filter) @@ -33,8 +32,8 @@ namespace os { auto dir = TRY(adopt_shared_if_nonnull(new (std::nothrow) Directory({}))); - long rc = syscall(SYS_openat, path.dirfd(), path.name().chars(), O_RDONLY | O_DIRECTORY, 0); - int fd = TRY(Result::from_syscall(rc)); + int fd = openat(path.dirfd(), path.name().chars(), O_RDONLY | O_DIRECTORY, 0); + if (fd < 0) return err(errno); DIR* dp = fdopendir(fd); if (!dp) diff --git a/libos/src/File.cpp b/libos/src/File.cpp index 04557337..4b97c0b3 100644 --- a/libos/src/File.cpp +++ b/libos/src/File.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include static SharedPtr g_stdin = {}; @@ -83,8 +82,8 @@ namespace os { auto file = TRY(adopt_shared_if_nonnull(new (std::nothrow) File({}))); - long rc = syscall(SYS_openat, path.dirfd(), path.name().chars(), flags, mode); - int fd = TRY(Result::from_syscall(rc)); + int fd = openat(path.dirfd(), path.name().chars(), flags, mode); + if (fd < 0) return err(errno); file->m_file = fdopen(fd, stdio_mode_from_openmode(flags)); if (!file->m_file) return err(errno); diff --git a/libos/src/FileSystem.cpp b/libos/src/FileSystem.cpp index 1c2e4a9d..33964db8 100644 --- a/libos/src/FileSystem.cpp +++ b/libos/src/FileSystem.cpp @@ -10,13 +10,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include namespace os::FileSystem @@ -41,24 +41,26 @@ namespace os::FileSystem Result stat(const Path& path, struct stat& st, bool follow_symlinks) { - long rc = syscall(SYS_fstatat, path.dirfd(), path.name().chars(), &st, - (int)(path.is_empty_path() | (follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW))); + int rc = fstatat(path.dirfd(), path.name().chars(), &st, + (int)(path.is_empty_path() | (follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW))); - return Result::from_syscall(rc); + if (rc < 0) return err(errno); + return {}; } Result create_directory(StringView path, mode_t mode) { - long rc = syscall(SYS_mkdir, path.chars(), mode); - - return Result::from_syscall(rc); + int rc = mkdir(path.chars(), mode); + if (rc < 0) return err(errno); + return {}; } Result remove(const Path& path) { - long rc = syscall(SYS_unlinkat, path.dirfd(), path.name().chars(), 0); - - return Result::from_syscall(rc); + // FIXME: This will not work on many operating systems that require rmdir() for directories. + int rc = unlinkat(path.dirfd(), path.name().chars(), 0); + if (rc < 0) return err(errno); + return {}; } Result remove_tree(const Path& path) @@ -82,14 +84,13 @@ namespace os::FileSystem TRY(stat(path, st, false)); if (!S_ISLNK(st.st_mode)) return String {}; - char* buf = (char*)TRY(calloc_impl(st.st_size + 1, 1)); - auto guard = make_scope_guard([buf] { free_impl(buf); }); - usize nread = TRY( - Result::from_syscall(syscall(SYS_readlinkat, path.dirfd(), path.name().chars(), buf, st.st_size))); + Buffer buf = TRY(Buffer::create_sized(st.st_size + 1)); + memset(buf.data(), 0, buf.size()); - guard.deactivate(); + ssize_t nread = readlinkat(path.dirfd(), path.name().chars(), (char*)buf.data(), st.st_size); + if (nread < 0) return err(errno); - return String { buf, nread }; + return String { (char*)buf.release_data(), (usize)nread }; } Result working_directory() @@ -112,8 +113,8 @@ namespace os::FileSystem Result change_directory(StringView path) { - long rc = syscall(SYS_chdir, path.chars()); - - return Result::from_syscall(rc); + int rc = chdir(path.chars()); + if (rc < 0) return err(errno); + return {}; } } diff --git a/libos/src/Process.cpp b/libos/src/Process.cpp index b533908d..7d297c77 100644 --- a/libos/src/Process.cpp +++ b/libos/src/Process.cpp @@ -9,8 +9,8 @@ #include #include +#include #include -#include #include extern char** environ; @@ -19,8 +19,9 @@ namespace os { Result Process::fork() { - long rc = syscall(SYS_fork); - return Result::from_syscall(rc); + pid_t pid = ::fork(); + if (pid < 0) return err(errno); + return pid; } Result Process::exec(StringView path, Slice args, bool search_in_path) @@ -121,14 +122,16 @@ namespace os Result Process::wait(pid_t child, int* status, int options) { - long rc = syscall(SYS_waitpid, child, status, options); - return Result::from_syscall(rc); + pid_t pid = waitpid(child, status, options); + if (pid < 0) return err(errno); + return pid; } Result Process::kill(pid_t pid, int signo) { - long rc = syscall(SYS_kill, pid, signo); - return Result::from_syscall(rc); + int rc = ::kill(pid, signo); + if (rc < 0) return err(errno); + return {}; } [[noreturn]] void Process::exit(int status)