/* * Copyright (c) 2021, Andreas Kling * Copyright (c) 2021-2022, Kenneth Myhra * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID) # include #endif #ifdef AK_OS_SOLARIS # include # include #endif namespace Core::System { #ifdef AK_OS_SERENITY ErrorOr beep(Optional tone); ErrorOr pledge(StringView promises, StringView execpromises = {}); ErrorOr unveil(StringView path, StringView permissions); ErrorOr unveil_after_exec(StringView path, StringView permissions); ErrorOr sendfd(int sockfd, int fd); ErrorOr recvfd(int sockfd, int options); ErrorOr ptrace_peekbuf(pid_t tid, void const* tracee_addr, Bytes destination_buf); ErrorOr mount(int source_fd, StringView target, StringView fs_type, int flags); ErrorOr umount(StringView mount_point); ErrorOr ptrace(int request, pid_t tid, void* address, void* data); ErrorOr disown(pid_t pid); ErrorOr profiling_enable(pid_t, u64 event_mask); ErrorOr profiling_disable(pid_t); ErrorOr profiling_free_buffer(pid_t); #else inline ErrorOr unveil(StringView, StringView) { return {}; } inline ErrorOr pledge(StringView, StringView = {}) { return {}; } #endif template ALWAYS_INLINE ErrorOr pledge(char const (&promises)[N]) { return pledge(StringView { promises, N - 1 }); } template ALWAYS_INLINE ErrorOr pledge(char const (&promises)[NPromises], char const (&execpromises)[NExecPromises]) { return pledge(StringView { promises, NPromises - 1 }, StringView { execpromises, NExecPromises - 1 }); } template ALWAYS_INLINE ErrorOr unveil(char const (&path)[NPath], char const (&permissions)[NPermissions]) { return unveil(StringView { path, NPath - 1 }, StringView { permissions, NPermissions - 1 }); } ALWAYS_INLINE ErrorOr unveil(nullptr_t, nullptr_t) { return unveil(StringView {}, StringView {}); } #if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID) ErrorOr> getspent(); ErrorOr> getspnam(StringView name); #endif #ifndef AK_OS_MACOS ErrorOr accept4(int sockfd, struct sockaddr*, socklen_t*, int flags); #endif ErrorOr sigaction(int signal, struct sigaction const* action, struct sigaction* old_action); #if defined(AK_OS_SOLARIS) ErrorOr signal(int signal, SIG_TYP handler); #elif defined(AK_OS_BSD_GENERIC) ErrorOr signal(int signal, sig_t handler); #else ErrorOr signal(int signal, sighandler_t handler); #endif ErrorOr fstat(int fd); ErrorOr fcntl(int fd, int command, ...); ErrorOr mmap(void* address, size_t, int protection, int flags, int fd, off_t, size_t alignment = 0, StringView name = {}); ErrorOr munmap(void* address, size_t); ErrorOr anon_create(size_t size, int options); ErrorOr open(StringView path, int options, mode_t mode = 0); ErrorOr openat(int fd, StringView path, int options, mode_t mode = 0); ErrorOr close(int fd); ErrorOr ftruncate(int fd, off_t length); ErrorOr stat(StringView path); ErrorOr lstat(StringView path); ErrorOr read(int fd, Bytes buffer); ErrorOr write(int fd, ReadonlyBytes buffer); ErrorOr kill(pid_t, int signal); ErrorOr killpg(int pgrp, int signal); ErrorOr dup(int source_fd); ErrorOr dup2(int source_fd, int destination_fd); ErrorOr ptsname(int fd); ErrorOr gethostname(); ErrorOr sethostname(StringView); ErrorOr getcwd(); ErrorOr ioctl(int fd, unsigned request, ...); ErrorOr tcgetattr(int fd); ErrorOr tcsetattr(int fd, int optional_actions, struct termios const&); ErrorOr tcsetpgrp(int fd, pid_t pgrp); ErrorOr chmod(StringView pathname, mode_t mode); ErrorOr lchown(StringView pathname, uid_t uid, gid_t gid); ErrorOr chown(StringView pathname, uid_t uid, gid_t gid); ErrorOr> getpwent(Span buffer); ErrorOr> getpwnam(StringView name); ErrorOr> getgrnam(StringView name); ErrorOr> getpwuid(uid_t); ErrorOr> getgrent(Span buffer); ErrorOr> getgrgid(gid_t); ErrorOr clock_settime(clockid_t clock_id, struct timespec* ts); ErrorOr posix_spawn(StringView path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const arguments[], char* const envp[]); ErrorOr posix_spawnp(StringView path, posix_spawn_file_actions_t* const file_actions, posix_spawnattr_t* const attr, char* const arguments[], char* const envp[]); ErrorOr lseek(int fd, off_t, int whence); ErrorOr endgrent(); struct WaitPidResult { pid_t pid; int status; }; ErrorOr waitpid(pid_t waitee, int options = 0); ErrorOr setuid(uid_t); ErrorOr seteuid(uid_t); ErrorOr setgid(gid_t); ErrorOr setegid(gid_t); ErrorOr setpgid(pid_t pid, pid_t pgid); ErrorOr setsid(); ErrorOr getsid(pid_t pid = 0); ErrorOr drop_privileges(); ErrorOr isatty(int fd); ErrorOr link(StringView old_path, StringView new_path); ErrorOr symlink(StringView target, StringView link_path); ErrorOr mkdir(StringView path, mode_t); ErrorOr chdir(StringView path); ErrorOr rmdir(StringView path); ErrorOr fork(); ErrorOr mkstemp(Span pattern); ErrorOr mkdtemp(Span pattern); ErrorOr fchmod(int fd, mode_t mode); ErrorOr fchown(int fd, uid_t, gid_t); ErrorOr rename(StringView old_path, StringView new_path); ErrorOr unlink(StringView path); ErrorOr utime(StringView path, Optional); ErrorOr uname(); ErrorOr> pipe2(int flags); #ifndef AK_OS_ANDROID ErrorOr adjtime(const struct timeval* delta, struct timeval* old_delta); #endif enum class SearchInPath { No, Yes, }; #ifdef AK_OS_SERENITY ErrorOr exec_command(Vector& command, bool preserve_env); #endif ErrorOr exec(StringView filename, ReadonlySpan arguments, SearchInPath, Optional> environment = {}); #ifdef AK_OS_SERENITY ErrorOr join_jail(u64 jail_index); ErrorOr create_jail(StringView jail_name); #endif ErrorOr socket(int domain, int type, int protocol); ErrorOr bind(int sockfd, struct sockaddr const*, socklen_t); ErrorOr listen(int sockfd, int backlog); ErrorOr accept(int sockfd, struct sockaddr*, socklen_t*); ErrorOr connect(int sockfd, struct sockaddr const*, socklen_t); ErrorOr shutdown(int sockfd, int how); ErrorOr send(int sockfd, void const*, size_t, int flags); ErrorOr sendmsg(int sockfd, const struct msghdr*, int flags); ErrorOr sendto(int sockfd, void const*, size_t, int flags, struct sockaddr const*, socklen_t); ErrorOr recv(int sockfd, void*, size_t, int flags); ErrorOr recvmsg(int sockfd, struct msghdr*, int flags); ErrorOr recvfrom(int sockfd, void*, size_t, int flags, struct sockaddr*, socklen_t*); ErrorOr getsockopt(int sockfd, int level, int option, void* value, socklen_t* value_size); ErrorOr setsockopt(int sockfd, int level, int option, void const* value, socklen_t value_size); ErrorOr getsockname(int sockfd, struct sockaddr*, socklen_t*); ErrorOr getpeername(int sockfd, struct sockaddr*, socklen_t*); ErrorOr socketpair(int domain, int type, int protocol, int sv[2]); ErrorOr> getgroups(); ErrorOr setgroups(ReadonlySpan); ErrorOr mknod(StringView pathname, mode_t mode, dev_t dev); ErrorOr mkfifo(StringView pathname, mode_t mode); ErrorOr setenv(StringView, StringView, bool); ErrorOr putenv(StringView); ErrorOr posix_openpt(int flags); ErrorOr grantpt(int fildes); ErrorOr unlockpt(int fildes); ErrorOr access(StringView pathname, int mode); ErrorOr readlink(StringView pathname); ErrorOr poll(Span, int timeout); class AddressInfoVector { AK_MAKE_NONCOPYABLE(AddressInfoVector); public: AddressInfoVector(AddressInfoVector&&) = default; ~AddressInfoVector() = default; ReadonlySpan addresses() const { return m_addresses; } private: friend ErrorOr getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints); AddressInfoVector(Vector&& addresses, struct addrinfo* ptr) : m_addresses(move(addresses)) , m_ptr(adopt_own_if_nonnull(ptr)) { } struct AddrInfoDeleter { void operator()(struct addrinfo* ptr) { ::freeaddrinfo(ptr); } }; Vector m_addresses {}; OwnPtr m_ptr {}; }; ErrorOr getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints); #ifdef AK_OS_SERENITY ErrorOr posix_fallocate(int fd, off_t offset, off_t length); #endif }