2021-11-22 14:43:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2022-06-30 14:19:03 +00:00
|
|
|
* Copyright (c) 2021-2022, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2021-12-25 12:52:19 +00:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2021-11-22 14:43:57 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
2022-12-07 23:47:25 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2022-12-16 03:51:55 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2022-01-14 09:20:29 +00:00
|
|
|
#include <AK/StringView.h>
|
2022-12-07 23:47:25 +00:00
|
|
|
#include <AK/Vector.h>
|
2022-04-10 16:25:22 +00:00
|
|
|
#include <dirent.h>
|
2021-12-25 12:52:19 +00:00
|
|
|
#include <fcntl.h>
|
2021-12-06 20:24:18 +00:00
|
|
|
#include <grp.h>
|
2022-12-07 23:47:25 +00:00
|
|
|
#include <netdb.h>
|
2022-12-11 22:40:25 +00:00
|
|
|
#include <poll.h>
|
2021-11-29 21:03:19 +00:00
|
|
|
#include <pwd.h>
|
2021-11-22 15:11:03 +00:00
|
|
|
#include <signal.h>
|
2021-12-13 17:38:48 +00:00
|
|
|
#include <spawn.h>
|
2021-12-25 12:52:19 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
2021-11-23 10:10:19 +00:00
|
|
|
#include <sys/stat.h>
|
2022-01-17 00:25:26 +00:00
|
|
|
#include <sys/time.h>
|
2022-01-04 02:56:28 +00:00
|
|
|
#include <sys/utsname.h>
|
2021-12-13 17:43:39 +00:00
|
|
|
#include <sys/wait.h>
|
2021-11-29 22:28:10 +00:00
|
|
|
#include <termios.h>
|
2021-11-27 21:01:40 +00:00
|
|
|
#include <time.h>
|
2021-12-21 00:27:51 +00:00
|
|
|
#include <utime.h>
|
2021-11-22 14:43:57 +00:00
|
|
|
|
2022-07-11 07:06:29 +00:00
|
|
|
#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID)
|
2022-01-01 17:26:17 +00:00
|
|
|
# include <shadow.h>
|
|
|
|
#endif
|
|
|
|
|
2021-11-23 09:59:50 +00:00
|
|
|
namespace Core::System {
|
2021-11-22 14:43:57 +00:00
|
|
|
|
2022-10-09 21:23:23 +00:00
|
|
|
#ifdef AK_OS_SERENITY
|
2022-01-09 05:14:43 +00:00
|
|
|
ErrorOr<void> beep();
|
2021-11-27 22:23:58 +00:00
|
|
|
ErrorOr<void> pledge(StringView promises, StringView execpromises = {});
|
2021-11-22 14:43:57 +00:00
|
|
|
ErrorOr<void> unveil(StringView path, StringView permissions);
|
2022-11-04 17:20:11 +00:00
|
|
|
ErrorOr<void> unveil_after_exec(StringView path, StringView permissions);
|
2021-11-28 08:12:29 +00:00
|
|
|
ErrorOr<void> sendfd(int sockfd, int fd);
|
|
|
|
ErrorOr<int> recvfd(int sockfd, int options);
|
2021-11-25 22:04:52 +00:00
|
|
|
ErrorOr<void> ptrace_peekbuf(pid_t tid, void const* tracee_addr, Bytes destination_buf);
|
2021-12-16 19:23:21 +00:00
|
|
|
ErrorOr<void> mount(int source_fd, StringView target, StringView fs_type, int flags);
|
2022-01-21 22:10:59 +00:00
|
|
|
ErrorOr<void> umount(StringView mount_point);
|
2021-12-25 10:04:40 +00:00
|
|
|
ErrorOr<long> ptrace(int request, pid_t tid, void* address, void* data);
|
2021-12-29 15:22:22 +00:00
|
|
|
ErrorOr<void> disown(pid_t pid);
|
2022-02-12 22:07:51 +00:00
|
|
|
ErrorOr<void> profiling_enable(pid_t, u64 event_mask);
|
2022-02-12 22:08:16 +00:00
|
|
|
ErrorOr<void> profiling_disable(pid_t);
|
2022-02-12 22:08:37 +00:00
|
|
|
ErrorOr<void> profiling_free_buffer(pid_t);
|
2022-07-04 16:03:31 +00:00
|
|
|
#else
|
|
|
|
inline ErrorOr<void> unveil(StringView, StringView)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
inline ErrorOr<void> pledge(StringView, StringView = {}) { return {}; }
|
2021-11-23 09:59:50 +00:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 20:49:07 +00:00
|
|
|
template<size_t N>
|
|
|
|
ALWAYS_INLINE ErrorOr<void> pledge(char const (&promises)[N])
|
|
|
|
{
|
|
|
|
return pledge(StringView { promises, N - 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t NPromises, size_t NExecPromises>
|
|
|
|
ALWAYS_INLINE ErrorOr<void> pledge(char const (&promises)[NPromises], char const (&execpromises)[NExecPromises])
|
|
|
|
{
|
|
|
|
return pledge(StringView { promises, NPromises - 1 }, StringView { execpromises, NExecPromises - 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t NPath, size_t NPermissions>
|
|
|
|
ALWAYS_INLINE ErrorOr<void> unveil(char const (&path)[NPath], char const (&permissions)[NPermissions])
|
|
|
|
{
|
|
|
|
return unveil(StringView { path, NPath - 1 }, StringView { permissions, NPermissions - 1 });
|
|
|
|
}
|
|
|
|
|
2022-12-13 06:59:30 +00:00
|
|
|
ALWAYS_INLINE ErrorOr<void> unveil(nullptr_t, nullptr_t)
|
2022-07-11 20:49:07 +00:00
|
|
|
{
|
|
|
|
return unveil(StringView {}, StringView {});
|
|
|
|
}
|
|
|
|
|
2022-07-11 07:06:29 +00:00
|
|
|
#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID)
|
2022-01-01 17:26:17 +00:00
|
|
|
ErrorOr<Optional<struct spwd>> getspent();
|
|
|
|
ErrorOr<Optional<struct spwd>> getspnam(StringView name);
|
|
|
|
#endif
|
|
|
|
|
2021-12-25 12:52:19 +00:00
|
|
|
#ifndef AK_OS_MACOS
|
|
|
|
ErrorOr<int> accept4(int sockfd, struct sockaddr*, socklen_t*, int flags);
|
|
|
|
#endif
|
|
|
|
|
2021-11-22 15:11:03 +00:00
|
|
|
ErrorOr<void> sigaction(int signal, struct sigaction const* action, struct sigaction* old_action);
|
2022-10-09 22:45:59 +00:00
|
|
|
#if defined(AK_OS_MACOS) || defined(AK_OS_OPENBSD) || defined(AK_OS_FREEBSD)
|
2021-11-23 20:03:53 +00:00
|
|
|
ErrorOr<sig_t> signal(int signal, sig_t handler);
|
|
|
|
#else
|
|
|
|
ErrorOr<sighandler_t> signal(int signal, sighandler_t handler);
|
|
|
|
#endif
|
2021-11-23 10:10:19 +00:00
|
|
|
ErrorOr<struct stat> fstat(int fd);
|
|
|
|
ErrorOr<int> fcntl(int fd, int command, ...);
|
2021-11-23 10:47:32 +00:00
|
|
|
ErrorOr<void*> mmap(void* address, size_t, int protection, int flags, int fd, off_t, size_t alignment = 0, StringView name = {});
|
2021-11-23 10:51:11 +00:00
|
|
|
ErrorOr<void> munmap(void* address, size_t);
|
2022-01-23 22:03:09 +00:00
|
|
|
ErrorOr<int> anon_create(size_t size, int options);
|
2022-04-10 16:25:22 +00:00
|
|
|
ErrorOr<int> open(StringView path, int options, mode_t mode = 0);
|
|
|
|
ErrorOr<int> openat(int fd, StringView path, int options, mode_t mode = 0);
|
2021-11-23 11:05:56 +00:00
|
|
|
ErrorOr<void> close(int fd);
|
2021-11-23 11:16:07 +00:00
|
|
|
ErrorOr<void> ftruncate(int fd, off_t length);
|
2021-11-23 11:22:21 +00:00
|
|
|
ErrorOr<struct stat> stat(StringView path);
|
2021-12-02 21:41:40 +00:00
|
|
|
ErrorOr<struct stat> lstat(StringView path);
|
2021-11-27 21:02:33 +00:00
|
|
|
ErrorOr<ssize_t> read(int fd, Bytes buffer);
|
|
|
|
ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer);
|
2021-11-23 15:06:56 +00:00
|
|
|
ErrorOr<void> kill(pid_t, int signal);
|
2022-03-01 19:04:41 +00:00
|
|
|
ErrorOr<void> killpg(int pgrp, int signal);
|
2021-11-28 11:08:28 +00:00
|
|
|
ErrorOr<int> dup(int source_fd);
|
2021-11-24 21:36:28 +00:00
|
|
|
ErrorOr<int> dup2(int source_fd, int destination_fd);
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<DeprecatedString> ptsname(int fd);
|
|
|
|
ErrorOr<DeprecatedString> gethostname();
|
2022-01-14 09:20:29 +00:00
|
|
|
ErrorOr<void> sethostname(StringView);
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<DeprecatedString> getcwd();
|
2021-11-29 22:07:33 +00:00
|
|
|
ErrorOr<void> ioctl(int fd, unsigned request, ...);
|
2021-11-29 22:28:10 +00:00
|
|
|
ErrorOr<struct termios> tcgetattr(int fd);
|
|
|
|
ErrorOr<void> tcsetattr(int fd, int optional_actions, struct termios const&);
|
2022-03-01 19:05:14 +00:00
|
|
|
ErrorOr<int> tcsetpgrp(int fd, pid_t pgrp);
|
2021-11-27 14:46:11 +00:00
|
|
|
ErrorOr<void> chmod(StringView pathname, mode_t mode);
|
2021-12-31 18:20:17 +00:00
|
|
|
ErrorOr<void> lchown(StringView pathname, uid_t uid, gid_t gid);
|
2021-11-27 14:23:16 +00:00
|
|
|
ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid);
|
2022-12-13 19:49:32 +00:00
|
|
|
ErrorOr<Optional<struct passwd>> getpwent(Span<char> buffer);
|
2021-12-23 08:41:00 +00:00
|
|
|
ErrorOr<Optional<struct passwd>> getpwnam(StringView name);
|
|
|
|
ErrorOr<Optional<struct group>> getgrnam(StringView name);
|
2022-01-01 16:53:57 +00:00
|
|
|
ErrorOr<Optional<struct passwd>> getpwuid(uid_t);
|
2022-12-13 20:08:58 +00:00
|
|
|
ErrorOr<Optional<struct group>> getgrent(Span<char> buffer);
|
2022-01-01 16:53:57 +00:00
|
|
|
ErrorOr<Optional<struct group>> getgrgid(gid_t);
|
2021-11-27 21:01:40 +00:00
|
|
|
ErrorOr<void> clock_settime(clockid_t clock_id, struct timespec* ts);
|
2022-05-11 19:24:39 +00:00
|
|
|
ErrorOr<pid_t> posix_spawn(StringView path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const arguments[], char* const envp[]);
|
|
|
|
ErrorOr<pid_t> posix_spawnp(StringView path, posix_spawn_file_actions_t* const file_actions, posix_spawnattr_t* const attr, char* const arguments[], char* const envp[]);
|
2022-01-18 14:42:45 +00:00
|
|
|
ErrorOr<off_t> lseek(int fd, off_t, int whence);
|
2022-06-08 12:39:59 +00:00
|
|
|
ErrorOr<void> endgrent();
|
2022-01-10 13:34:09 +00:00
|
|
|
|
|
|
|
struct WaitPidResult {
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
};
|
|
|
|
ErrorOr<WaitPidResult> waitpid(pid_t waitee, int options = 0);
|
2021-12-16 18:17:44 +00:00
|
|
|
ErrorOr<void> setuid(uid_t);
|
|
|
|
ErrorOr<void> seteuid(uid_t);
|
|
|
|
ErrorOr<void> setgid(gid_t);
|
|
|
|
ErrorOr<void> setegid(gid_t);
|
2021-12-21 02:47:50 +00:00
|
|
|
ErrorOr<void> setpgid(pid_t pid, pid_t pgid);
|
2022-01-03 00:58:58 +00:00
|
|
|
ErrorOr<pid_t> setsid();
|
2022-09-06 04:12:58 +00:00
|
|
|
ErrorOr<pid_t> getsid(pid_t pid = 0);
|
2022-03-21 07:05:42 +00:00
|
|
|
ErrorOr<void> drop_privileges();
|
2021-12-16 18:24:58 +00:00
|
|
|
ErrorOr<bool> isatty(int fd);
|
2022-07-25 14:03:51 +00:00
|
|
|
ErrorOr<void> link(StringView old_path, StringView new_path);
|
2021-12-16 19:23:21 +00:00
|
|
|
ErrorOr<void> symlink(StringView target, StringView link_path);
|
2021-12-16 19:27:25 +00:00
|
|
|
ErrorOr<void> mkdir(StringView path, mode_t);
|
2022-01-03 09:07:26 +00:00
|
|
|
ErrorOr<void> chdir(StringView path);
|
2022-02-13 01:35:38 +00:00
|
|
|
ErrorOr<void> rmdir(StringView path);
|
2021-12-16 19:28:54 +00:00
|
|
|
ErrorOr<pid_t> fork();
|
2021-12-16 20:10:59 +00:00
|
|
|
ErrorOr<int> mkstemp(Span<char> pattern);
|
2021-12-16 20:12:23 +00:00
|
|
|
ErrorOr<void> fchmod(int fd, mode_t mode);
|
2022-02-06 17:54:15 +00:00
|
|
|
ErrorOr<void> fchown(int fd, uid_t, gid_t);
|
2021-12-16 20:40:42 +00:00
|
|
|
ErrorOr<void> rename(StringView old_path, StringView new_path);
|
2021-12-29 20:47:38 +00:00
|
|
|
ErrorOr<void> unlink(StringView path);
|
2021-12-20 18:48:30 +00:00
|
|
|
ErrorOr<void> utime(StringView path, Optional<struct utimbuf>);
|
2022-01-04 02:56:28 +00:00
|
|
|
ErrorOr<struct utsname> uname();
|
2021-12-21 02:46:46 +00:00
|
|
|
ErrorOr<Array<int, 2>> pipe2(int flags);
|
2022-07-11 07:06:29 +00:00
|
|
|
#ifndef AK_OS_ANDROID
|
2022-01-17 00:25:26 +00:00
|
|
|
ErrorOr<void> adjtime(const struct timeval* delta, struct timeval* old_delta);
|
2022-07-11 07:06:29 +00:00
|
|
|
#endif
|
2022-03-12 20:23:43 +00:00
|
|
|
enum class SearchInPath {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
2022-11-04 09:50:40 +00:00
|
|
|
|
|
|
|
#ifdef AK_OS_SERENITY
|
|
|
|
ErrorOr<void> exec_command(Vector<StringView>& command, bool preserve_env);
|
|
|
|
#endif
|
|
|
|
|
2022-03-12 20:23:43 +00:00
|
|
|
ErrorOr<void> exec(StringView filename, Span<StringView> arguments, SearchInPath, Optional<Span<StringView>> environment = {});
|
2021-11-22 14:43:57 +00:00
|
|
|
|
2022-11-02 20:28:58 +00:00
|
|
|
#ifdef AK_OS_SERENITY
|
|
|
|
ErrorOr<void> join_jail(u64 jail_index);
|
|
|
|
ErrorOr<u64> create_jail(StringView jail_name);
|
|
|
|
#endif
|
|
|
|
|
2021-12-25 12:52:19 +00:00
|
|
|
ErrorOr<int> socket(int domain, int type, int protocol);
|
|
|
|
ErrorOr<void> bind(int sockfd, struct sockaddr const*, socklen_t);
|
|
|
|
ErrorOr<void> listen(int sockfd, int backlog);
|
|
|
|
ErrorOr<int> accept(int sockfd, struct sockaddr*, socklen_t*);
|
|
|
|
ErrorOr<void> connect(int sockfd, struct sockaddr const*, socklen_t);
|
|
|
|
ErrorOr<void> shutdown(int sockfd, int how);
|
|
|
|
ErrorOr<ssize_t> send(int sockfd, void const*, size_t, int flags);
|
|
|
|
ErrorOr<ssize_t> sendmsg(int sockfd, const struct msghdr*, int flags);
|
|
|
|
ErrorOr<ssize_t> sendto(int sockfd, void const*, size_t, int flags, struct sockaddr const*, socklen_t);
|
|
|
|
ErrorOr<ssize_t> recv(int sockfd, void*, size_t, int flags);
|
|
|
|
ErrorOr<ssize_t> recvmsg(int sockfd, struct msghdr*, int flags);
|
|
|
|
ErrorOr<ssize_t> recvfrom(int sockfd, void*, size_t, int flags, struct sockaddr*, socklen_t*);
|
|
|
|
ErrorOr<void> getsockopt(int sockfd, int level, int option, void* value, socklen_t* value_size);
|
|
|
|
ErrorOr<void> setsockopt(int sockfd, int level, int option, void const* value, socklen_t value_size);
|
|
|
|
ErrorOr<void> getsockname(int sockfd, struct sockaddr*, socklen_t*);
|
|
|
|
ErrorOr<void> getpeername(int sockfd, struct sockaddr*, socklen_t*);
|
|
|
|
ErrorOr<void> socketpair(int domain, int type, int protocol, int sv[2]);
|
2022-01-01 15:19:51 +00:00
|
|
|
ErrorOr<Vector<gid_t>> getgroups();
|
2022-07-09 12:49:49 +00:00
|
|
|
ErrorOr<void> setgroups(Span<gid_t const>);
|
2022-01-14 16:08:46 +00:00
|
|
|
ErrorOr<void> mknod(StringView pathname, mode_t mode, dev_t dev);
|
2022-01-17 09:04:00 +00:00
|
|
|
ErrorOr<void> mkfifo(StringView pathname, mode_t mode);
|
2022-03-01 22:43:55 +00:00
|
|
|
ErrorOr<void> setenv(StringView, StringView, bool);
|
2022-03-01 22:44:53 +00:00
|
|
|
ErrorOr<int> posix_openpt(int flags);
|
|
|
|
ErrorOr<void> grantpt(int fildes);
|
|
|
|
ErrorOr<void> unlockpt(int fildes);
|
2022-03-29 17:24:06 +00:00
|
|
|
ErrorOr<void> access(StringView pathname, int mode);
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<DeprecatedString> readlink(StringView pathname);
|
2022-12-11 22:40:25 +00:00
|
|
|
ErrorOr<int> poll(Span<struct pollfd>, int timeout);
|
2022-03-01 22:43:55 +00:00
|
|
|
|
2022-12-07 23:47:25 +00:00
|
|
|
class AddressInfoVector {
|
|
|
|
AK_MAKE_NONCOPYABLE(AddressInfoVector);
|
|
|
|
|
|
|
|
public:
|
|
|
|
AddressInfoVector(AddressInfoVector&&) = default;
|
|
|
|
~AddressInfoVector() = default;
|
|
|
|
|
|
|
|
Span<struct addrinfo const> addresses() const { return m_addresses; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
|
|
|
|
|
2022-12-16 03:51:55 +00:00
|
|
|
AddressInfoVector(Vector<struct addrinfo>&& addresses, struct addrinfo* ptr)
|
2022-12-07 23:47:25 +00:00
|
|
|
: m_addresses(move(addresses))
|
2022-12-16 03:51:55 +00:00
|
|
|
, m_ptr(adopt_own_if_nonnull(ptr))
|
2022-12-07 23:47:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-16 03:51:55 +00:00
|
|
|
struct AddrInfoDeleter {
|
|
|
|
void operator()(struct addrinfo* ptr) { ::freeaddrinfo(ptr); }
|
|
|
|
};
|
|
|
|
|
2022-12-07 23:47:25 +00:00
|
|
|
Vector<struct addrinfo> m_addresses {};
|
2022-12-16 03:51:55 +00:00
|
|
|
OwnPtr<struct addrinfo, AddrInfoDeleter> m_ptr {};
|
2022-12-07 23:47:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
|
|
|
|
|
2022-11-27 18:46:03 +00:00
|
|
|
#ifdef AK_OS_SERENITY
|
|
|
|
ErrorOr<void> posix_fallocate(int fd, off_t offset, off_t length);
|
|
|
|
#endif
|
|
|
|
|
2021-11-22 14:43:57 +00:00
|
|
|
}
|