libos: Stop using syscalls directly and proxy to libc
This commit is contained in:
parent
64f9e9dcde
commit
06aa7725a1
@ -12,7 +12,6 @@
|
|||||||
#include <luna/CString.h>
|
#include <luna/CString.h>
|
||||||
#include <os/Directory.h>
|
#include <os/Directory.h>
|
||||||
#include <os/FileSystem.h>
|
#include <os/FileSystem.h>
|
||||||
#include <sys/syscall.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static bool should_skip_entry(const char* name, os::Directory::Filter filter)
|
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({})));
|
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 = openat(path.dirfd(), path.name().chars(), O_RDONLY | O_DIRECTORY, 0);
|
||||||
int fd = TRY(Result<int>::from_syscall(rc));
|
if (fd < 0) return err(errno);
|
||||||
|
|
||||||
DIR* dp = fdopendir(fd);
|
DIR* dp = fdopendir(fd);
|
||||||
if (!dp)
|
if (!dp)
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <luna/StringBuilder.h>
|
#include <luna/StringBuilder.h>
|
||||||
#include <os/File.h>
|
#include <os/File.h>
|
||||||
#include <sys/syscall.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static SharedPtr<os::File> g_stdin = {};
|
static SharedPtr<os::File> g_stdin = {};
|
||||||
@ -83,8 +82,8 @@ namespace os
|
|||||||
{
|
{
|
||||||
auto file = TRY(adopt_shared_if_nonnull(new (std::nothrow) File({})));
|
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 = openat(path.dirfd(), path.name().chars(), flags, mode);
|
||||||
int fd = TRY(Result<int>::from_syscall(rc));
|
if (fd < 0) return err(errno);
|
||||||
|
|
||||||
file->m_file = fdopen(fd, stdio_mode_from_openmode(flags));
|
file->m_file = fdopen(fd, stdio_mode_from_openmode(flags));
|
||||||
if (!file->m_file) return err(errno);
|
if (!file->m_file) return err(errno);
|
||||||
|
@ -10,13 +10,13 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <luna/Buffer.h>
|
||||||
#include <luna/PathParser.h>
|
#include <luna/PathParser.h>
|
||||||
#include <luna/String.h>
|
#include <luna/String.h>
|
||||||
#include <os/Directory.h>
|
#include <os/Directory.h>
|
||||||
#include <os/FileSystem.h>
|
#include <os/FileSystem.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/syscall.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
namespace os::FileSystem
|
namespace os::FileSystem
|
||||||
@ -41,24 +41,26 @@ namespace os::FileSystem
|
|||||||
|
|
||||||
Result<void> stat(const Path& path, struct stat& st, bool follow_symlinks)
|
Result<void> stat(const Path& path, struct stat& st, bool follow_symlinks)
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_fstatat, path.dirfd(), path.name().chars(), &st,
|
int rc = fstatat(path.dirfd(), path.name().chars(), &st,
|
||||||
(int)(path.is_empty_path() | (follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW)));
|
(int)(path.is_empty_path() | (follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW)));
|
||||||
|
|
||||||
return Result<void>::from_syscall(rc);
|
if (rc < 0) return err(errno);
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> create_directory(StringView path, mode_t mode)
|
Result<void> create_directory(StringView path, mode_t mode)
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_mkdir, path.chars(), mode);
|
int rc = mkdir(path.chars(), mode);
|
||||||
|
if (rc < 0) return err(errno);
|
||||||
return Result<void>::from_syscall(rc);
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> remove(const Path& path)
|
Result<void> remove(const Path& path)
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_unlinkat, path.dirfd(), path.name().chars(), 0);
|
// FIXME: This will not work on many operating systems that require rmdir() for directories.
|
||||||
|
int rc = unlinkat(path.dirfd(), path.name().chars(), 0);
|
||||||
return Result<void>::from_syscall(rc);
|
if (rc < 0) return err(errno);
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> remove_tree(const Path& path)
|
Result<void> remove_tree(const Path& path)
|
||||||
@ -82,14 +84,13 @@ namespace os::FileSystem
|
|||||||
TRY(stat(path, st, false));
|
TRY(stat(path, st, false));
|
||||||
if (!S_ISLNK(st.st_mode)) return String {};
|
if (!S_ISLNK(st.st_mode)) return String {};
|
||||||
|
|
||||||
char* buf = (char*)TRY(calloc_impl(st.st_size + 1, 1));
|
Buffer buf = TRY(Buffer::create_sized(st.st_size + 1));
|
||||||
auto guard = make_scope_guard([buf] { free_impl(buf); });
|
memset(buf.data(), 0, buf.size());
|
||||||
usize nread = TRY(
|
|
||||||
Result<usize>::from_syscall(syscall(SYS_readlinkat, path.dirfd(), path.name().chars(), buf, st.st_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<String> working_directory()
|
Result<String> working_directory()
|
||||||
@ -112,8 +113,8 @@ namespace os::FileSystem
|
|||||||
|
|
||||||
Result<void> change_directory(StringView path)
|
Result<void> change_directory(StringView path)
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_chdir, path.chars());
|
int rc = chdir(path.chars());
|
||||||
|
if (rc < 0) return err(errno);
|
||||||
return Result<void>::from_syscall(rc);
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <os/Process.h>
|
#include <os/Process.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/syscall.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
extern char** environ;
|
extern char** environ;
|
||||||
@ -19,8 +19,9 @@ namespace os
|
|||||||
{
|
{
|
||||||
Result<pid_t> Process::fork()
|
Result<pid_t> Process::fork()
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_fork);
|
pid_t pid = ::fork();
|
||||||
return Result<pid_t>::from_syscall(rc);
|
if (pid < 0) return err(errno);
|
||||||
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> Process::exec(StringView path, Slice<String> args, bool search_in_path)
|
Result<void> Process::exec(StringView path, Slice<String> args, bool search_in_path)
|
||||||
@ -121,14 +122,16 @@ namespace os
|
|||||||
|
|
||||||
Result<pid_t> Process::wait(pid_t child, int* status, int options)
|
Result<pid_t> Process::wait(pid_t child, int* status, int options)
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_waitpid, child, status, options);
|
pid_t pid = waitpid(child, status, options);
|
||||||
return Result<pid_t>::from_syscall(rc);
|
if (pid < 0) return err(errno);
|
||||||
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> Process::kill(pid_t pid, int signo)
|
Result<void> Process::kill(pid_t pid, int signo)
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_kill, pid, signo);
|
int rc = ::kill(pid, signo);
|
||||||
return Result<void>::from_syscall(rc);
|
if (rc < 0) return err(errno);
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
[[noreturn]] void Process::exit(int status)
|
[[noreturn]] void Process::exit(int status)
|
||||||
|
Loading…
Reference in New Issue
Block a user