/* * Copyright (c) 2021, Andreas Kling * Copyright (c) 2021, Kenneth Myhra * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #define HANDLE_SYSCALL_RETURN_VALUE(syscall_name, rc, success_value) \ if ((rc) < 0) { \ return Error::from_syscall(syscall_name, rc); \ } \ return success_value; namespace Core::System { #ifdef __serenity__ ErrorOr pledge(StringView promises, StringView execpromises) { Syscall::SC_pledge_params params { { promises.characters_without_null_termination(), promises.length() }, { execpromises.characters_without_null_termination(), execpromises.length() }, }; int rc = syscall(SC_pledge, ¶ms); HANDLE_SYSCALL_RETURN_VALUE("pledge"sv, rc, {}); } ErrorOr unveil(StringView path, StringView permissions) { Syscall::SC_unveil_params params { { path.characters_without_null_termination(), path.length() }, { permissions.characters_without_null_termination(), permissions.length() }, }; int rc = syscall(SC_unveil, ¶ms); HANDLE_SYSCALL_RETURN_VALUE("unveil"sv, rc, {}); } ErrorOr> pipe2(int flags) { Array fds; if (::pipe2(fds.data(), flags) < 0) return Error::from_syscall("pipe2"sv, -errno); return fds; } #endif ErrorOr sigaction(int signal, struct sigaction const* action, struct sigaction* old_action) { if (::sigaction(signal, action, old_action) < 0) return Error::from_syscall("sigaction"sv, -errno); return {}; } ErrorOr fstat(int fd) { struct stat st = {}; if (::fstat(fd, &st) < 0) return Error::from_syscall("fstat"sv, -errno); return st; } ErrorOr fcntl(int fd, int command, ...) { va_list ap; va_start(ap, command); u32 extra_arg = va_arg(ap, u32); int rc = ::fcntl(fd, command, extra_arg); va_end(ap); if (rc < 0) return Error::from_syscall("fcntl"sv, -errno); return rc; } ErrorOr mmap(void* address, size_t size, int protection, int flags, int fd, off_t offset, [[maybe_unused]] size_t alignment, [[maybe_unused]] StringView name) { #ifdef __serenity__ Syscall::SC_mmap_params params { (uintptr_t)address, size, alignment, protection, flags, fd, offset, { name.characters_without_null_termination(), name.length() } }; ptrdiff_t rc = syscall(SC_mmap, ¶ms); if (rc < 0 && rc > -EMAXERRNO) return Error::from_syscall("mmap"sv, rc); return reinterpret_cast(rc); #else // NOTE: Regular POSIX mmap() doesn't support custom alignment requests. VERIFY(!alignment); auto* ptr = ::mmap(address, size, protection, flags, fd, offset); if (ptr == MAP_FAILED) return Error::from_syscall("mmap"sv, -errno); return ptr; #endif } ErrorOr munmap(void* address, size_t size) { if (::munmap(address, size) < 0) return Error::from_syscall("munmap"sv, -errno); return {}; } ErrorOr open(StringView path, int options, ...) { if (!path.characters_without_null_termination()) return Error::from_syscall("open"sv, -EFAULT); va_list ap; va_start(ap, options); auto mode = (mode_t)va_arg(ap, unsigned); va_end(ap); #ifdef __serenity__ Syscall::SC_open_params params { AT_FDCWD, { path.characters_without_null_termination(), path.length() }, options, mode }; int rc = syscall(SC_open, ¶ms); HANDLE_SYSCALL_RETURN_VALUE("open"sv, rc, rc); #else // NOTE: We have to ensure that the path is null-terminated. String path_string = path; int rc = ::open(path_string.characters(), options, mode); if (rc < 0) return Error::from_syscall("open"sv, -errno); return rc; #endif } ErrorOr close(int fd) { if (::close(fd) < 0) return Error::from_syscall("close"sv, -errno); return {}; } ErrorOr ftruncate(int fd, off_t length) { if (::ftruncate(fd, length) < 0) return Error::from_syscall("ftruncate"sv, -errno); return {}; } ErrorOr stat(StringView path) { if (!path.characters_without_null_termination()) return Error::from_syscall("stat"sv, -EFAULT); struct stat st = {}; #ifdef __serenity__ Syscall::SC_stat_params params { { path.characters_without_null_termination(), path.length() }, &st, AT_FDCWD, true }; int rc = syscall(SC_stat, ¶ms); HANDLE_SYSCALL_RETURN_VALUE("stat"sv, rc, st); #else String path_string = path; if (::stat(path_string.characters(), &st) < 0) return Error::from_syscall("stat"sv, -errno); return st; #endif } ErrorOr read(int fd, void* buffer, size_t buffer_size) { ssize_t rc = ::read(fd, buffer, buffer_size); if (rc < 0) return Error::from_syscall("read"sv, -errno); return rc; } ErrorOr write(int fd, void const* data, size_t data_size) { ssize_t rc = ::write(fd, data, data_size); if (rc < 0) return Error::from_syscall("write"sv, -errno); return rc; } ErrorOr kill(pid_t pid, int signal) { if (::kill(pid, signal) < 0) return Error::from_syscall("kill"sv, -errno); return {}; } ErrorOr dup2(int source_fd, int destination_fd) { int fd = ::dup2(source_fd, destination_fd); if (fd < 0) return Error::from_syscall("dup2"sv, -errno); return fd; } ErrorOr ptsname(int fd) { auto* name = ::ptsname(fd); if (!name) return Error::from_syscall("ptsname"sv, -errno); return String(name); } ErrorOr gethostname() { char hostname[256]; int rc = ::gethostname(hostname, sizeof(hostname)); if (rc < 0) return Error::from_syscall("gethostname"sv, -errno); return String(&hostname[0]); } }