2021-11-22 11:43:57 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2022-06-30 11:19:03 -03:00
|
|
|
* Copyright (c) 2021-2022, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2024-01-30 15:30:22 -03:00
|
|
|
* Copyright (c) 2021-2024, Sam Atkins <atkinssj@serenityos.org>
|
2021-11-22 11:43:57 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
2022-01-14 06:20:29 -03:00
|
|
|
#include <AK/StringView.h>
|
2025-11-30 12:18:43 -03:00
|
|
|
#include <LibCore/AddressInfoVector.h>
|
2025-12-04 14:12:06 -03:00
|
|
|
#include <LibCore/Export.h>
|
2021-12-25 09:52:19 -03:00
|
|
|
#include <fcntl.h>
|
2021-11-22 12:11:03 -03:00
|
|
|
#include <signal.h>
|
2021-11-23 07:10:19 -03:00
|
|
|
#include <sys/stat.h>
|
2021-11-27 18:01:40 -03:00
|
|
|
#include <time.h>
|
2021-11-22 11:43:57 -03:00
|
|
|
|
2024-10-19 22:57:00 -03:00
|
|
|
#if !defined(AK_OS_WINDOWS)
|
|
|
|
|
# include <netdb.h>
|
|
|
|
|
# include <poll.h>
|
|
|
|
|
# include <pwd.h>
|
|
|
|
|
# include <spawn.h>
|
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
|
# include <sys/resource.h>
|
|
|
|
|
# include <sys/socket.h>
|
|
|
|
|
# include <sys/time.h>
|
|
|
|
|
# include <sys/wait.h>
|
|
|
|
|
# include <termios.h>
|
|
|
|
|
# include <utime.h>
|
|
|
|
|
#else
|
2025-02-15 10:37:38 -03:00
|
|
|
# include <io.h>
|
2025-11-30 12:21:31 -03:00
|
|
|
|
2024-10-19 22:57:00 -03:00
|
|
|
# define O_CLOEXEC O_NOINHERIT
|
2026-01-13 07:18:05 -03:00
|
|
|
# define STDIN_FILENO to_fd(reinterpret_cast<void*>(_get_osfhandle(_fileno(stdin))))
|
|
|
|
|
# define STDOUT_FILENO to_fd(reinterpret_cast<void*>(_get_osfhandle(_fileno(stdout))))
|
|
|
|
|
# define STDERR_FILENO to_fd(reinterpret_cast<void*>(_get_osfhandle(_fileno(stderr))))
|
2024-12-21 02:04:11 -03:00
|
|
|
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
|
|
|
|
|
# define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
2025-11-30 12:21:31 -03:00
|
|
|
|
2024-10-19 22:57:00 -03:00
|
|
|
using sighandler_t = void (*)(int);
|
2025-11-30 12:21:31 -03:00
|
|
|
using socklen_t = int;
|
|
|
|
|
|
|
|
|
|
struct addrinfo;
|
|
|
|
|
struct sockaddr;
|
2024-10-19 22:57:00 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID) && !defined(AK_OS_WINDOWS)
|
2022-01-01 14:26:17 -03:00
|
|
|
# include <shadow.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-04-25 12:00:10 -03:00
|
|
|
#ifdef AK_OS_FREEBSD
|
|
|
|
|
# include <sys/ucred.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-02-25 14:53:46 -03:00
|
|
|
#ifdef AK_OS_SOLARIS
|
|
|
|
|
# include <sys/filio.h>
|
2023-02-25 14:58:53 -03:00
|
|
|
# include <ucred.h>
|
2023-02-25 14:53:46 -03:00
|
|
|
#endif
|
|
|
|
|
|
2021-11-23 06:59:50 -03:00
|
|
|
namespace Core::System {
|
2021-11-22 11:43:57 -03:00
|
|
|
|
2023-08-27 16:11:12 -03:00
|
|
|
#if !defined(AK_OS_MACOS) && !defined(AK_OS_HAIKU)
|
2021-12-25 09:52:19 -03:00
|
|
|
ErrorOr<int> accept4(int sockfd, struct sockaddr*, socklen_t*, int flags);
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-11-22 12:11:03 -03:00
|
|
|
ErrorOr<void> sigaction(int signal, struct sigaction const* action, struct sigaction* old_action);
|
2023-02-25 14:50:03 -03:00
|
|
|
#if defined(AK_OS_SOLARIS)
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<SIG_TYP> signal(int signal, SIG_TYP handler);
|
2023-02-25 14:50:03 -03:00
|
|
|
#elif defined(AK_OS_BSD_GENERIC)
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<sig_t> signal(int signal, sig_t handler);
|
2021-11-23 17:03:53 -03:00
|
|
|
#else
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<sighandler_t> signal(int signal, sighandler_t handler);
|
2021-11-23 17:03:53 -03:00
|
|
|
#endif
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<struct stat> fstat(int fd);
|
2023-03-17 12:23:24 -03:00
|
|
|
ErrorOr<struct stat> fstatat(int fd, StringView path, int flags);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<int> fcntl(int fd, int command, ...);
|
2021-11-23 07:47:32 -03: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 07:51:11 -03:00
|
|
|
ErrorOr<void> munmap(void* address, size_t);
|
2026-04-22 06:45:08 -03:00
|
|
|
CORE_API ErrorOr<void*> reserve_address_space(size_t size);
|
|
|
|
|
CORE_API ErrorOr<void> commit_memory(void* address, size_t size);
|
|
|
|
|
CORE_API ErrorOr<void> release_address_space(void* address, size_t size);
|
2022-01-23 19:03:09 -03:00
|
|
|
ErrorOr<int> anon_create(size_t size, int options);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<int> open(StringView path, int options, mode_t mode = 0);
|
2022-04-10 13:25:22 -03:00
|
|
|
ErrorOr<int> openat(int fd, StringView path, int options, mode_t mode = 0);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<void> close(int fd);
|
2021-11-23 08:16:07 -03:00
|
|
|
ErrorOr<void> ftruncate(int fd, off_t length);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<struct stat> stat(StringView path);
|
|
|
|
|
CORE_API ErrorOr<struct stat> lstat(StringView path);
|
|
|
|
|
CORE_API ErrorOr<size_t> read(int fd, Bytes buffer);
|
|
|
|
|
CORE_API ErrorOr<size_t> write(int fd, ReadonlyBytes buffer);
|
|
|
|
|
CORE_API ErrorOr<int> dup(int source_fd);
|
2021-11-24 18:36:28 -03:00
|
|
|
ErrorOr<int> dup2(int source_fd, int destination_fd);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<ByteString> getcwd();
|
|
|
|
|
CORE_API ErrorOr<void> ioctl(int fd, unsigned request, ...);
|
2021-11-29 19:28:10 -03:00
|
|
|
ErrorOr<struct termios> tcgetattr(int fd);
|
|
|
|
|
ErrorOr<void> tcsetattr(int fd, int optional_actions, struct termios const&);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<void> chmod(StringView pathname, mode_t mode);
|
2022-01-18 11:42:45 -03:00
|
|
|
ErrorOr<off_t> lseek(int fd, off_t, int whence);
|
2022-01-10 10:34:09 -03:00
|
|
|
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<bool> isatty(int fd);
|
|
|
|
|
CORE_API ErrorOr<void> link(StringView old_path, StringView new_path);
|
|
|
|
|
CORE_API ErrorOr<void> symlink(StringView target, StringView link_path);
|
|
|
|
|
CORE_API ErrorOr<void> mkdir(StringView path, mode_t);
|
|
|
|
|
CORE_API ErrorOr<void> chdir(StringView path);
|
|
|
|
|
CORE_API ErrorOr<void> rmdir(StringView path);
|
|
|
|
|
CORE_API ErrorOr<int> mkstemp(Span<char> pattern);
|
|
|
|
|
CORE_API ErrorOr<void> fchmod(int fd, mode_t mode);
|
|
|
|
|
CORE_API ErrorOr<void> rename(StringView old_path, StringView new_path);
|
|
|
|
|
CORE_API ErrorOr<void> unlink(StringView path);
|
|
|
|
|
CORE_API ErrorOr<void> utimensat(int fd, StringView path, struct timespec const times[2], int flag);
|
|
|
|
|
CORE_API ErrorOr<Array<int, 2>> pipe2(int flags);
|
|
|
|
|
|
|
|
|
|
CORE_API ErrorOr<int> socket(int domain, int type, int protocol);
|
|
|
|
|
CORE_API ErrorOr<void> bind(int sockfd, struct sockaddr const*, socklen_t);
|
|
|
|
|
CORE_API ErrorOr<void> listen(int sockfd, int backlog);
|
2021-12-25 09:52:19 -03:00
|
|
|
ErrorOr<int> accept(int sockfd, struct sockaddr*, socklen_t*);
|
|
|
|
|
ErrorOr<void> connect(int sockfd, struct sockaddr const*, socklen_t);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<size_t> send(int sockfd, ReadonlyBytes, int flags);
|
2025-12-01 11:03:25 -03:00
|
|
|
ErrorOr<size_t> sendmsg(int sockfd, const struct msghdr*, int flags);
|
|
|
|
|
ErrorOr<size_t> sendto(int sockfd, ReadonlyBytes, int flags, struct sockaddr const*, socklen_t);
|
|
|
|
|
ErrorOr<size_t> recv(int sockfd, Bytes, int flags);
|
|
|
|
|
ErrorOr<size_t> recvmsg(int sockfd, struct msghdr*, int flags);
|
|
|
|
|
ErrorOr<size_t> recvfrom(int sockfd, Bytes, int flags, struct sockaddr*, socklen_t*);
|
2021-12-25 09:52:19 -03:00
|
|
|
ErrorOr<void> getsockopt(int sockfd, int level, int option, void* value, socklen_t* value_size);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<void> setsockopt(int sockfd, int level, int option, void const* value, socklen_t value_size);
|
2021-12-25 09:52:19 -03:00
|
|
|
ErrorOr<void> getsockname(int sockfd, struct sockaddr*, socklen_t*);
|
|
|
|
|
ErrorOr<void> getpeername(int sockfd, struct sockaddr*, socklen_t*);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<void> socketpair(int domain, int type, int protocol, int sv[2]);
|
2024-10-18 11:36:03 -03:00
|
|
|
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<void> access(StringView pathname, int mode, int flags = 0);
|
2023-12-16 11:19:34 -03:00
|
|
|
ErrorOr<ByteString> readlink(StringView pathname);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<int> poll(Span<struct pollfd>, int timeout);
|
2022-03-01 19:43:55 -03:00
|
|
|
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<void> kill(pid_t, int signal);
|
2025-08-24 00:58:43 -03:00
|
|
|
#if !defined(AK_OS_WINDOWS)
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid);
|
2024-10-19 22:57:00 -03: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[]);
|
|
|
|
|
|
|
|
|
|
struct WaitPidResult {
|
|
|
|
|
pid_t pid;
|
|
|
|
|
int status;
|
|
|
|
|
};
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<WaitPidResult> waitpid(pid_t waitee, int options = 0);
|
|
|
|
|
CORE_API ErrorOr<void> fchown(int fd, uid_t, gid_t);
|
2025-06-13 13:56:20 -03:00
|
|
|
#endif
|
2024-10-19 22:57:00 -03:00
|
|
|
|
2022-12-07 20:47:25 -03:00
|
|
|
ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
|
|
|
|
|
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API unsigned hardware_concurrency();
|
|
|
|
|
CORE_API u64 physical_memory_bytes();
|
2024-04-05 15:43:45 -03:00
|
|
|
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<ByteString> current_executable_path();
|
2023-08-02 19:56:53 -03:00
|
|
|
|
2024-10-19 22:57:00 -03:00
|
|
|
#if !defined(AK_OS_WINDOWS)
|
2024-07-22 11:11:18 -03:00
|
|
|
ErrorOr<rlimit> get_resource_limits(int resource);
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<void> set_resource_limits(int resource, rlim_t limit);
|
2024-10-19 22:57:00 -03:00
|
|
|
#endif
|
2024-07-22 11:11:18 -03:00
|
|
|
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API int getpid();
|
|
|
|
|
CORE_API bool is_socket(int fd);
|
|
|
|
|
CORE_API ErrorOr<void> sleep_ms(u32 milliseconds);
|
|
|
|
|
CORE_API ErrorOr<void> set_close_on_exec(int fd, bool enabled);
|
2024-12-14 21:39:12 -03:00
|
|
|
|
2025-12-04 14:12:06 -03:00
|
|
|
CORE_API ErrorOr<size_t> transfer_file_through_socket(int source_fd, int target_fd, size_t source_offset, size_t source_length);
|
2025-10-08 09:01:14 -03:00
|
|
|
|
2021-11-22 11:43:57 -03:00
|
|
|
}
|