| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-01-01 16:19:51 +01:00
										 |  |  |  * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-06-30 16:19:03 +02:00
										 |  |  |  * Copyright (c) 2021-2022, Kenneth Myhra <kennethmyhra@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |  * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-01-16 17:25:26 -07:00
										 |  |  |  * Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com> | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | #include <AK/DeprecatedString.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  | #include <AK/FixedArray.h>
 | 
					
						
							|  |  |  | #include <AK/ScopedValueRollback.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-23 23:03:09 +01:00
										 |  |  | #include <AK/StdLibExtras.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-01 16:19:51 +01:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-16 16:55:44 +03:00
										 |  |  | #include <LibCore/File.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-06 00:04:06 -06:00
										 |  |  | #include <LibCore/SessionManagement.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-23 10:59:50 +01:00
										 |  |  | #include <LibCore/System.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-14 00:32:55 +01:00
										 |  |  | #include <limits.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:10:19 +01:00
										 |  |  | #include <stdarg.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-01 23:43:55 +01:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:07:33 +01:00
										 |  |  | #include <sys/ioctl.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:47:32 +01:00
										 |  |  | #include <sys/mman.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-16 17:25:26 -07:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:28:10 +01:00
										 |  |  | #include <termios.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:05:56 +01:00
										 |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-11-04 19:20:11 +02:00
										 |  |  | #    include <Kernel/API/Unveil.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-22 07:46:01 -06:00
										 |  |  | #    include <LibCore/Account.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-11 01:06:29 -06:00
										 |  |  | #    include <LibSystem/syscall.h>
 | 
					
						
							| 
									
										
										
										
											2021-12-29 16:22:22 +01:00
										 |  |  | #    include <serenity.h>
 | 
					
						
							| 
									
										
										
										
											2022-11-20 06:53:14 +03:30
										 |  |  | #    include <sys/ptrace.h>
 | 
					
						
							| 
									
										
										
										
											2021-12-29 16:22:22 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 16:45:59 -06:00
										 |  |  | #if defined(AK_OS_LINUX) && !defined(MFD_CLOEXEC)
 | 
					
						
							| 
									
										
										
										
											2022-01-23 23:03:09 +01:00
										 |  |  | #    include <linux/memfd.h>
 | 
					
						
							|  |  |  | #    include <sys/syscall.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | static int memfd_create(char const* name, unsigned int flags) | 
					
						
							| 
									
										
										
										
											2022-01-23 23:03:09 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return syscall(SYS_memfd_create, name, flags); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 16:45:59 -06:00
										 |  |  | #if defined(AK_OS_MACOS)
 | 
					
						
							| 
									
										
										
										
											2022-02-13 00:28:43 +01:00
										 |  |  | #    include <sys/mman.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 10:48:22 +01:00
										 |  |  | #define HANDLE_SYSCALL_RETURN_VALUE(syscall_name, rc, success_value) \
 | 
					
						
							|  |  |  |     if ((rc) < 0) {                                                  \ | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return Error::from_syscall(syscall_name##sv, rc);            \ | 
					
						
							| 
									
										
										
										
											2021-11-23 10:48:22 +01:00
										 |  |  |     }                                                                \ | 
					
						
							|  |  |  |     return success_value; | 
					
						
							| 
									
										
										
										
											2021-11-22 16:00:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 10:59:50 +01:00
										 |  |  | namespace Core::System { | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 00:32:55 +01:00
										 |  |  | #ifndef HOST_NAME_MAX
 | 
					
						
							| 
									
										
										
										
											2022-10-09 16:45:59 -06:00
										 |  |  | #    ifdef AK_OS_MACOS
 | 
					
						
							| 
									
										
										
										
											2022-01-14 00:32:55 +01:00
										 |  |  | #        define HOST_NAME_MAX 255
 | 
					
						
							|  |  |  | #    else
 | 
					
						
							|  |  |  | #        define HOST_NAME_MAX 64
 | 
					
						
							|  |  |  | #    endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-01-09 06:14:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> beep() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto rc = ::sysbeep(); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							| 
									
										
										
										
											2022-01-22 12:38:55 +02:00
										 |  |  |         return Error::from_syscall("beep"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-01-09 06:14:43 +01:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | ErrorOr<void> 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); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("pledge", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-24 17:59:56 +03:00
										 |  |  | static ErrorOr<void> unveil_dynamic_loader() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     static bool dynamic_loader_unveiled { false }; | 
					
						
							|  |  |  |     if (dynamic_loader_unveiled) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     // FIXME: Try to find a way to not hardcode the dynamic loader path.
 | 
					
						
							|  |  |  |     constexpr auto dynamic_loader_path = "/usr/lib/Loader.so"sv; | 
					
						
							|  |  |  |     constexpr auto dynamic_loader_permissions = "x"sv; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Syscall::SC_unveil_params params { | 
					
						
							| 
									
										
										
										
											2022-11-04 19:20:11 +02:00
										 |  |  |         static_cast<int>(UnveilFlags::CurrentProgram), | 
					
						
							| 
									
										
										
										
											2022-10-24 17:59:56 +03:00
										 |  |  |         { dynamic_loader_path.characters_without_null_termination(), dynamic_loader_path.length() }, | 
					
						
							|  |  |  |         { dynamic_loader_permissions.characters_without_null_termination(), dynamic_loader_permissions.length() }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_unveil, ¶ms); | 
					
						
							|  |  |  |     if (rc < 0) { | 
					
						
							|  |  |  |         return Error::from_syscall("unveil (DynamicLoader @ /usr/lib/Loader.so)"sv, rc); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     dynamic_loader_unveiled = true; | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | ErrorOr<void> unveil(StringView path, StringView permissions) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-06 00:04:06 -06:00
										 |  |  |     auto const parsed_path = TRY(Core::SessionManagement::parse_path_with_sid(path)); | 
					
						
							| 
									
										
										
										
											2022-07-24 12:04:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-24 17:59:56 +03:00
										 |  |  |     if (permissions.contains('x')) | 
					
						
							|  |  |  |         TRY(unveil_dynamic_loader()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  |     Syscall::SC_unveil_params params { | 
					
						
							| 
									
										
										
										
											2022-11-04 19:20:11 +02:00
										 |  |  |         static_cast<int>(UnveilFlags::CurrentProgram), | 
					
						
							|  |  |  |         { parsed_path.characters(), parsed_path.length() }, | 
					
						
							|  |  |  |         { permissions.characters_without_null_termination(), permissions.length() }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_unveil, ¶ms); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("unveil", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> unveil_after_exec(StringView path, StringView permissions) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto const parsed_path = TRY(Core::SessionManagement::parse_path_with_sid(path)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Syscall::SC_unveil_params params { | 
					
						
							|  |  |  |         static_cast<int>(UnveilFlags::AfterExec), | 
					
						
							| 
									
										
										
										
											2022-07-24 12:04:31 +02:00
										 |  |  |         { parsed_path.characters(), parsed_path.length() }, | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  |         { permissions.characters_without_null_termination(), permissions.length() }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_unveil, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("unveil", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-24 22:36:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 09:12:29 +01:00
										 |  |  | ErrorOr<void> sendfd(int sockfd, int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::sendfd(sockfd, fd) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("sendfd"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<int> recvfd(int sockfd, int options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto fd = ::recvfd(sockfd, options); | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("recvfd"sv, -errno); | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-25 23:04:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> ptrace_peekbuf(pid_t tid, void const* tracee_addr, Bytes destination_buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Syscall::SC_ptrace_buf_params buf_params { | 
					
						
							|  |  |  |         { destination_buf.data(), destination_buf.size() } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     Syscall::SC_ptrace_params params { | 
					
						
							|  |  |  |         PT_PEEKBUF, | 
					
						
							|  |  |  |         tid, | 
					
						
							|  |  |  |         const_cast<void*>(tracee_addr), | 
					
						
							|  |  |  |         (FlatPtr)&buf_params, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_ptrace, ¶ms); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("ptrace_peekbuf", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-12-16 19:31:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 20:23:21 +01:00
										 |  |  | ErrorOr<void> mount(int source_fd, StringView target, StringView fs_type, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (target.is_null() || fs_type.is_null()) | 
					
						
							|  |  |  |         return Error::from_errno(EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Syscall::SC_mount_params params { | 
					
						
							|  |  |  |         { target.characters_without_null_termination(), target.length() }, | 
					
						
							|  |  |  |         { fs_type.characters_without_null_termination(), fs_type.length() }, | 
					
						
							|  |  |  |         source_fd, | 
					
						
							|  |  |  |         flags | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_mount, ¶ms); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("mount", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 23:10:59 +01:00
										 |  |  | ErrorOr<void> umount(StringView mount_point) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (mount_point.is_null()) | 
					
						
							|  |  |  |         return Error::from_errno(EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int rc = syscall(SC_umount, mount_point.characters_without_null_termination(), mount_point.length()); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("umount", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 11:04:40 +01:00
										 |  |  | ErrorOr<long> ptrace(int request, pid_t tid, void* address, void* data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto rc = ::ptrace(request, tid, address, data); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("ptrace"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-12-29 16:22:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> disown(pid_t pid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::disown(pid); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("disown", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-13 00:07:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> profiling_enable(pid_t pid, u64 event_mask) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::profiling_enable(pid, event_mask); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("profiling_enable", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-13 00:08:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> profiling_disable(pid_t pid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::profiling_disable(pid); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("profiling_disable", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-13 00:08:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> profiling_free_buffer(pid_t pid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::profiling_free_buffer(pid); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("profiling_free_buffer", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-23 10:59:50 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 01:06:29 -06:00
										 |  |  | #if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID)
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:26:17 +01:00
										 |  |  | ErrorOr<Optional<struct spwd>> getspent() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     errno = 0; | 
					
						
							|  |  |  |     if (auto* spwd = ::getspent()) | 
					
						
							|  |  |  |         return *spwd; | 
					
						
							|  |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_syscall("getspent"sv, -errno); | 
					
						
							|  |  |  |     return Optional<struct spwd> {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<Optional<struct spwd>> getspnam(StringView name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     errno = 0; | 
					
						
							|  |  |  |     ::setspent(); | 
					
						
							|  |  |  |     while (auto* spwd = ::getspent()) { | 
					
						
							|  |  |  |         if (spwd->sp_namp == name) | 
					
						
							|  |  |  |             return *spwd; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_syscall("getspnam"sv, -errno); | 
					
						
							|  |  |  |     return Optional<struct spwd> {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 12:52:19 +00:00
										 |  |  | #ifndef AK_OS_MACOS
 | 
					
						
							|  |  |  | ErrorOr<int> accept4(int sockfd, sockaddr* address, socklen_t* address_length, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto fd = ::accept4(sockfd, address, address_length, flags); | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("accept4"sv, -errno); | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 16:11:03 +01:00
										 |  |  | ErrorOr<void> sigaction(int signal, struct sigaction const* action, struct sigaction* old_action) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-23 10:59:50 +01:00
										 |  |  |     if (::sigaction(signal, action, old_action) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("sigaction"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2021-11-22 16:11:03 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 16:45:59 -06:00
										 |  |  | #if defined(AK_OS_MACOS) || defined(AK_OS_OPENBSD) || defined(AK_OS_FREEBSD)
 | 
					
						
							| 
									
										
										
										
											2021-11-23 21:03:53 +01:00
										 |  |  | ErrorOr<sig_t> signal(int signal, sig_t handler) | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | ErrorOr<sighandler_t> signal(int signal, sighandler_t handler) | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto old_handler = ::signal(signal, handler); | 
					
						
							|  |  |  |     if (old_handler == SIG_ERR) | 
					
						
							|  |  |  |         return Error::from_syscall("signal"sv, -errno); | 
					
						
							|  |  |  |     return old_handler; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:10:19 +01:00
										 |  |  | ErrorOr<struct stat> fstat(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct stat st = {}; | 
					
						
							|  |  |  |     if (::fstat(fd, &st) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("fstat"sv, -errno); | 
					
						
							|  |  |  |     return st; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<int> fcntl(int fd, int command, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     va_list ap; | 
					
						
							|  |  |  |     va_start(ap, command); | 
					
						
							| 
									
										
										
										
											2022-07-03 01:02:45 +02:00
										 |  |  |     uintptr_t extra_arg = va_arg(ap, uintptr_t); | 
					
						
							| 
									
										
										
										
											2021-11-23 11:10:19 +01:00
										 |  |  |     int rc = ::fcntl(fd, command, extra_arg); | 
					
						
							|  |  |  |     va_end(ap); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("fcntl"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:47:32 +01:00
										 |  |  | ErrorOr<void*> mmap(void* address, size_t size, int protection, int flags, int fd, off_t offset, [[maybe_unused]] size_t alignment, [[maybe_unused]] StringView name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-12-22 13:20:32 +01:00
										 |  |  |     Syscall::SC_mmap_params params { address, size, alignment, protection, flags, fd, offset, { name.characters_without_null_termination(), name.length() } }; | 
					
						
							| 
									
										
										
										
											2021-11-23 11:47:32 +01:00
										 |  |  |     ptrdiff_t rc = syscall(SC_mmap, ¶ms); | 
					
						
							|  |  |  |     if (rc < 0 && rc > -EMAXERRNO) | 
					
						
							|  |  |  |         return Error::from_syscall("mmap"sv, rc); | 
					
						
							|  |  |  |     return reinterpret_cast<void*>(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
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:51:11 +01:00
										 |  |  | ErrorOr<void> munmap(void* address, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::munmap(address, size) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("munmap"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-23 23:03:09 +01:00
										 |  |  | ErrorOr<int> anon_create([[maybe_unused]] size_t size, [[maybe_unused]] int options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int fd = -1; | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #if defined(AK_OS_SERENITY)
 | 
					
						
							| 
									
										
										
										
											2022-01-23 23:03:09 +01:00
										 |  |  |     fd = ::anon_create(round_up_to_power_of_two(size, PAGE_SIZE), options); | 
					
						
							| 
									
										
										
										
											2022-10-09 16:45:59 -06:00
										 |  |  | #elif defined(AK_OS_LINUX) || defined(AK_OS_FREEBSD)
 | 
					
						
							| 
									
										
										
										
											2022-01-23 23:03:09 +01:00
										 |  |  |     // FIXME: Support more options on Linux.
 | 
					
						
							|  |  |  |     auto linux_options = ((options & O_CLOEXEC) > 0) ? MFD_CLOEXEC : 0; | 
					
						
							|  |  |  |     fd = memfd_create("", linux_options); | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_errno(errno); | 
					
						
							|  |  |  |     if (::ftruncate(fd, size) < 0) { | 
					
						
							|  |  |  |         auto saved_errno = errno; | 
					
						
							|  |  |  |         TRY(close(fd)); | 
					
						
							|  |  |  |         return Error::from_errno(saved_errno); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-20 06:53:14 +03:30
										 |  |  | #elif defined(AK_OS_MACOS) || defined(AK_OS_EMSCRIPTEN)
 | 
					
						
							| 
									
										
										
										
											2022-02-13 00:28:43 +01:00
										 |  |  |     struct timespec time; | 
					
						
							|  |  |  |     clock_gettime(CLOCK_REALTIME, &time); | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     auto name = DeprecatedString::formatted("/shm-{}{}", (unsigned long)time.tv_sec, (unsigned long)time.tv_nsec); | 
					
						
							| 
									
										
										
										
											2022-02-13 00:28:43 +01:00
										 |  |  |     fd = shm_open(name.characters(), O_RDWR | O_CREAT | options, 0600); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (shm_unlink(name.characters()) == -1) { | 
					
						
							|  |  |  |         auto saved_errno = errno; | 
					
						
							|  |  |  |         TRY(close(fd)); | 
					
						
							|  |  |  |         return Error::from_errno(saved_errno); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_errno(errno); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (::ftruncate(fd, size) < 0) { | 
					
						
							|  |  |  |         auto saved_errno = errno; | 
					
						
							|  |  |  |         TRY(close(fd)); | 
					
						
							|  |  |  |         return Error::from_errno(saved_errno); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void* addr = ::mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0); | 
					
						
							|  |  |  |     if (addr == MAP_FAILED) { | 
					
						
							|  |  |  |         auto saved_errno = errno; | 
					
						
							|  |  |  |         TRY(close(fd)); | 
					
						
							|  |  |  |         return Error::from_errno(saved_errno); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-23 23:03:09 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_errno(errno); | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 18:25:22 +02:00
										 |  |  | ErrorOr<int> open(StringView path, int options, mode_t mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return openat(AT_FDCWD, path, options, mode); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<int> openat(int fd, StringView path, int options, mode_t mode) | 
					
						
							| 
									
										
										
										
											2021-11-23 11:59:16 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!path.characters_without_null_termination()) | 
					
						
							|  |  |  |         return Error::from_syscall("open"sv, -EFAULT); | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-04-10 18:25:22 +02:00
										 |  |  |     Syscall::SC_open_params params { fd, { path.characters_without_null_termination(), path.length() }, options, mode }; | 
					
						
							| 
									
										
										
										
											2021-11-23 11:59:16 +01:00
										 |  |  |     int rc = syscall(SC_open, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("open", rc, rc); | 
					
						
							| 
									
										
										
										
											2021-11-23 11:59:16 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // NOTE: We have to ensure that the path is null-terminated.
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = path; | 
					
						
							| 
									
										
										
										
											2022-04-10 18:25:22 +02:00
										 |  |  |     int rc = ::openat(fd, path_string.characters(), options, mode); | 
					
						
							| 
									
										
										
										
											2021-11-23 11:59:16 +01:00
										 |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("open"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:05:56 +01:00
										 |  |  | ErrorOr<void> close(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::close(fd) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("close"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:16:07 +01:00
										 |  |  | ErrorOr<void> ftruncate(int fd, off_t length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::ftruncate(fd, length) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("ftruncate"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:22:21 +01:00
										 |  |  | ErrorOr<struct stat> stat(StringView path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!path.characters_without_null_termination()) | 
					
						
							|  |  |  |         return Error::from_syscall("stat"sv, -EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct stat st = {}; | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:22:21 +01:00
										 |  |  |     Syscall::SC_stat_params params { { path.characters_without_null_termination(), path.length() }, &st, AT_FDCWD, true }; | 
					
						
							|  |  |  |     int rc = syscall(SC_stat, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("stat", rc, st); | 
					
						
							| 
									
										
										
										
											2021-11-23 12:22:21 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = path; | 
					
						
							| 
									
										
										
										
											2021-11-23 12:22:21 +01:00
										 |  |  |     if (::stat(path_string.characters(), &st) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("stat"sv, -errno); | 
					
						
							|  |  |  |     return st; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 22:41:40 +01:00
										 |  |  | ErrorOr<struct stat> lstat(StringView path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!path.characters_without_null_termination()) | 
					
						
							|  |  |  |         return Error::from_syscall("lstat"sv, -EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct stat st = {}; | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-12-02 22:41:40 +01:00
										 |  |  |     Syscall::SC_stat_params params { { path.characters_without_null_termination(), path.length() }, &st, AT_FDCWD, false }; | 
					
						
							|  |  |  |     int rc = syscall(SC_stat, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("lstat", rc, st); | 
					
						
							| 
									
										
										
										
											2021-12-02 22:41:40 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = path; | 
					
						
							| 
									
										
										
										
											2021-12-02 22:41:40 +01:00
										 |  |  |     if (::stat(path_string.characters(), &st) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("lstat"sv, -errno); | 
					
						
							|  |  |  |     return st; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-27 13:02:33 -08:00
										 |  |  | ErrorOr<ssize_t> read(int fd, Bytes buffer) | 
					
						
							| 
									
										
										
										
											2021-11-23 12:40:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-27 13:02:33 -08:00
										 |  |  |     ssize_t rc = ::read(fd, buffer.data(), buffer.size()); | 
					
						
							| 
									
										
										
										
											2021-11-23 12:40:17 +01:00
										 |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("read"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-27 13:02:33 -08:00
										 |  |  | ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer) | 
					
						
							| 
									
										
										
										
											2021-11-23 12:40:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-27 13:02:33 -08:00
										 |  |  |     ssize_t rc = ::write(fd, buffer.data(), buffer.size()); | 
					
						
							| 
									
										
										
										
											2021-11-23 12:40:17 +01:00
										 |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("write"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 16:06:56 +01:00
										 |  |  | ErrorOr<void> kill(pid_t pid, int signal) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::kill(pid, signal) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("kill"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 20:04:41 +01:00
										 |  |  | ErrorOr<void> killpg(int pgrp, int signal) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::killpg(pgrp, signal) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("killpg"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 12:08:28 +01:00
										 |  |  | ErrorOr<int> dup(int source_fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int fd = ::dup(source_fd); | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("dup"sv, -errno); | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 22:36:28 +01:00
										 |  |  | ErrorOr<int> 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | ErrorOr<DeprecatedString> ptsname(int fd) | 
					
						
							| 
									
										
										
										
											2021-11-24 23:24:53 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto* name = ::ptsname(fd); | 
					
						
							|  |  |  |     if (!name) | 
					
						
							|  |  |  |         return Error::from_syscall("ptsname"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     return DeprecatedString(name); | 
					
						
							| 
									
										
										
										
											2021-11-24 23:24:53 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | ErrorOr<DeprecatedString> gethostname() | 
					
						
							| 
									
										
										
										
											2021-11-27 10:13:33 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-14 00:32:55 +01:00
										 |  |  |     char hostname[HOST_NAME_MAX]; | 
					
						
							| 
									
										
										
										
											2021-11-27 10:13:33 +01:00
										 |  |  |     int rc = ::gethostname(hostname, sizeof(hostname)); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("gethostname"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     return DeprecatedString(&hostname[0]); | 
					
						
							| 
									
										
										
										
											2021-11-27 10:13:33 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 10:20:29 +01:00
										 |  |  | ErrorOr<void> sethostname(StringView hostname) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::sethostname(hostname.characters_without_null_termination(), hostname.length()); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("sethostname"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | ErrorOr<DeprecatedString> getcwd() | 
					
						
							| 
									
										
										
										
											2021-12-24 08:20:29 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     auto* cwd = ::getcwd(nullptr, 0); | 
					
						
							|  |  |  |     if (!cwd) | 
					
						
							|  |  |  |         return Error::from_syscall("getcwd"sv, -errno); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString string_cwd(cwd); | 
					
						
							| 
									
										
										
										
											2021-12-24 08:20:29 -08:00
										 |  |  |     free(cwd); | 
					
						
							|  |  |  |     return string_cwd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:07:33 +01:00
										 |  |  | ErrorOr<void> ioctl(int fd, unsigned request, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     va_list ap; | 
					
						
							|  |  |  |     va_start(ap, request); | 
					
						
							|  |  |  |     FlatPtr arg = va_arg(ap, FlatPtr); | 
					
						
							|  |  |  |     va_end(ap); | 
					
						
							|  |  |  |     if (::ioctl(fd, request, arg) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("ioctl"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:28:10 +01:00
										 |  |  | ErrorOr<struct termios> tcgetattr(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct termios ios = {}; | 
					
						
							|  |  |  |     if (::tcgetattr(fd, &ios) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("tcgetattr"sv, -errno); | 
					
						
							|  |  |  |     return ios; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> tcsetattr(int fd, int optional_actions, struct termios const& ios) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::tcsetattr(fd, optional_actions, &ios) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("tcsetattr"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 20:05:14 +01:00
										 |  |  | ErrorOr<int> tcsetpgrp(int fd, pid_t pgrp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::tcsetpgrp(fd, pgrp); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("tcsetpgrp"sv, -errno); | 
					
						
							|  |  |  |     return { rc }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-27 15:46:11 +01:00
										 |  |  | ErrorOr<void> chmod(StringView pathname, mode_t mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!pathname.characters_without_null_termination()) | 
					
						
							|  |  |  |         return Error::from_syscall("chmod"sv, -EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-01-11 16:51:34 +01:00
										 |  |  |     Syscall::SC_chmod_params params { | 
					
						
							|  |  |  |         AT_FDCWD, | 
					
						
							|  |  |  |         { pathname.characters_without_null_termination(), pathname.length() }, | 
					
						
							|  |  |  |         mode, | 
					
						
							|  |  |  |         true | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_chmod, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("chmod", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-11-27 15:46:11 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path = pathname; | 
					
						
							| 
									
										
										
										
											2021-11-27 15:46:11 +01:00
										 |  |  |     if (::chmod(path.characters(), mode) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("chmod"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:12:23 +01:00
										 |  |  | ErrorOr<void> fchmod(int fd, mode_t mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::fchmod(fd, mode) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("fchmod"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 17:54:15 +00:00
										 |  |  | ErrorOr<void> fchown(int fd, uid_t uid, gid_t gid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::fchown(fd, uid, gid) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("fchown"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-31 19:20:17 +01:00
										 |  |  | ErrorOr<void> lchown(StringView pathname, uid_t uid, gid_t gid) | 
					
						
							| 
									
										
										
										
											2021-11-27 15:23:16 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!pathname.characters_without_null_termination()) | 
					
						
							|  |  |  |         return Error::from_syscall("chown"sv, -EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-12-31 19:20:17 +01:00
										 |  |  |     Syscall::SC_chown_params params = { { pathname.characters_without_null_termination(), pathname.length() }, uid, gid, AT_FDCWD, false }; | 
					
						
							| 
									
										
										
										
											2021-11-27 15:23:16 +01:00
										 |  |  |     int rc = syscall(SC_chown, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("chown", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-11-27 15:23:16 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path = pathname; | 
					
						
							| 
									
										
										
										
											2021-11-27 15:23:16 +01:00
										 |  |  |     if (::chown(path.characters(), uid, gid) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("chown"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-31 19:20:17 +01:00
										 |  |  | ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!pathname.characters_without_null_termination()) | 
					
						
							|  |  |  |         return Error::from_syscall("chown"sv, -EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-12-31 19:20:17 +01:00
										 |  |  |     Syscall::SC_chown_params params = { { pathname.characters_without_null_termination(), pathname.length() }, uid, gid, AT_FDCWD, true }; | 
					
						
							|  |  |  |     int rc = syscall(SC_chown, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("chown", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-12-31 19:20:17 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path = pathname; | 
					
						
							| 
									
										
										
										
											2021-12-31 19:20:17 +01:00
										 |  |  |     if (::lchown(path.characters(), uid, gid) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("lchown"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 17:53:57 +01:00
										 |  |  | ErrorOr<Optional<struct passwd>> getpwuid(uid_t uid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     errno = 0; | 
					
						
							|  |  |  |     if (auto* pwd = ::getpwuid(uid)) | 
					
						
							|  |  |  |         return *pwd; | 
					
						
							|  |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_syscall("getpwuid"sv, -errno); | 
					
						
							|  |  |  |     return Optional<struct passwd> {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<Optional<struct group>> getgrgid(gid_t gid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     errno = 0; | 
					
						
							|  |  |  |     if (auto* grp = ::getgrgid(gid)) | 
					
						
							|  |  |  |         return *grp; | 
					
						
							|  |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_syscall("getgrgid"sv, -errno); | 
					
						
							|  |  |  |     return Optional<struct group> {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 09:41:00 +01:00
										 |  |  | ErrorOr<Optional<struct passwd>> getpwnam(StringView name) | 
					
						
							| 
									
										
										
										
											2021-11-29 22:03:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-01 16:05:21 +01:00
										 |  |  |     errno = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 22:03:19 +01:00
										 |  |  |     ::setpwent(); | 
					
						
							|  |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_syscall("getpwnam"sv, -errno); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (auto* pw = ::getpwent()) { | 
					
						
							|  |  |  |         if (errno) | 
					
						
							|  |  |  |             return Error::from_syscall("getpwnam"sv, -errno); | 
					
						
							|  |  |  |         if (pw->pw_name == name) | 
					
						
							|  |  |  |             return *pw; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_syscall("getpwnam"sv, -errno); | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-23 09:41:00 +01:00
										 |  |  |         return Optional<struct passwd> {}; | 
					
						
							| 
									
										
										
										
											2021-11-29 22:03:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 09:41:00 +01:00
										 |  |  | ErrorOr<Optional<struct group>> getgrnam(StringView name) | 
					
						
							| 
									
										
										
										
											2021-12-06 21:24:18 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-01 16:05:21 +01:00
										 |  |  |     errno = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 21:24:18 +01:00
										 |  |  |     ::setgrent(); | 
					
						
							|  |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_syscall("getgrnam"sv, -errno); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (auto* gr = ::getgrent()) { | 
					
						
							|  |  |  |         if (errno) | 
					
						
							|  |  |  |             return Error::from_syscall("getgrnam"sv, -errno); | 
					
						
							|  |  |  |         if (gr->gr_name == name) | 
					
						
							|  |  |  |             return *gr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_syscall("getgrnam"sv, -errno); | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-23 09:41:00 +01:00
										 |  |  |         return Optional<struct group> {}; | 
					
						
							| 
									
										
										
										
											2021-12-06 21:24:18 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-27 22:01:40 +01:00
										 |  |  | ErrorOr<void> clock_settime(clockid_t clock_id, struct timespec* ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-11-27 22:01:40 +01:00
										 |  |  |     int rc = syscall(SC_clock_settime, clock_id, ts); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("clocksettime", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-11-27 22:01:40 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     if (::clock_settime(clock_id, ts) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("clocksettime"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 20:24:39 +01:00
										 |  |  | static ALWAYS_INLINE ErrorOr<pid_t> posix_spawn_wrapper(StringView path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const arguments[], char* const envp[], StringView function_name, decltype(::posix_spawn) spawn_function) | 
					
						
							| 
									
										
										
										
											2021-12-13 18:38:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     pid_t child_pid; | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |     if ((errno = spawn_function(&child_pid, path.to_deprecated_string().characters(), file_actions, attr, arguments, envp))) | 
					
						
							| 
									
										
										
										
											2022-05-11 20:24:39 +01:00
										 |  |  |         return Error::from_syscall(function_name, -errno); | 
					
						
							| 
									
										
										
										
											2021-12-13 18:38:48 +01:00
										 |  |  |     return child_pid; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 20:24:39 +01: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[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return posix_spawn_wrapper(path, file_actions, attr, arguments, envp, "posix_spawn"sv, ::posix_spawn); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return posix_spawn_wrapper(path, file_actions, attr, arguments, envp, "posix_spawnp"sv, ::posix_spawnp); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 14:42:45 +00:00
										 |  |  | ErrorOr<off_t> lseek(int fd, off_t offset, int whence) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     off_t rc = ::lseek(fd, offset, whence); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return Error::from_syscall("lseek"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-01-18 14:42:45 +00:00
										 |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 14:39:59 +02:00
										 |  |  | ErrorOr<void> endgrent() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int old_errno = 0; | 
					
						
							|  |  |  |     swap(old_errno, errno); | 
					
						
							|  |  |  |     ::endgrent(); | 
					
						
							|  |  |  |     if (errno != 0) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return Error::from_syscall("endgrent"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-06-08 14:39:59 +02:00
										 |  |  |     errno = old_errno; | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-10 14:34:09 +01:00
										 |  |  | ErrorOr<WaitPidResult> waitpid(pid_t waitee, int options) | 
					
						
							| 
									
										
										
										
											2021-12-13 18:43:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-10 14:34:09 +01:00
										 |  |  |     int wstatus; | 
					
						
							|  |  |  |     pid_t pid = ::waitpid(waitee, &wstatus, options); | 
					
						
							| 
									
										
										
										
											2021-12-13 18:43:39 +01:00
										 |  |  |     if (pid < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("waitpid"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-01-10 14:34:09 +01:00
										 |  |  |     return WaitPidResult { pid, wstatus }; | 
					
						
							| 
									
										
										
										
											2021-12-13 18:43:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 19:17:44 +01:00
										 |  |  | ErrorOr<void> setuid(uid_t uid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::setuid(uid) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("setuid"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> seteuid(uid_t uid) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-16 23:02:56 +01:00
										 |  |  |     if (::seteuid(uid) < 0) | 
					
						
							| 
									
										
										
										
											2021-12-16 19:17:44 +01:00
										 |  |  |         return Error::from_syscall("seteuid"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> setgid(gid_t gid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::setgid(gid) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("setgid"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> setegid(gid_t gid) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-16 23:02:56 +01:00
										 |  |  |     if (::setegid(gid) < 0) | 
					
						
							| 
									
										
										
										
											2021-12-16 19:17:44 +01:00
										 |  |  |         return Error::from_syscall("setegid"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 06:17:50 +03:30
										 |  |  | ErrorOr<void> setpgid(pid_t pid, pid_t pgid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::setpgid(pid, pgid) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("setpgid"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 01:58:58 +01:00
										 |  |  | ErrorOr<pid_t> setsid() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::setsid(); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("setsid"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-05 22:12:58 -06:00
										 |  |  | ErrorOr<pid_t> getsid(pid_t pid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::getsid(pid); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("getsid"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-21 00:05:42 -07:00
										 |  |  | ErrorOr<void> drop_privileges() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto gid_result = setgid(getgid()); | 
					
						
							|  |  |  |     auto uid_result = setuid(getuid()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (gid_result.is_error() || uid_result.is_error()) | 
					
						
							|  |  |  |         return Error::from_string_literal("Failed to drop privileges"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 19:24:58 +01:00
										 |  |  | ErrorOr<bool> isatty(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::isatty(fd); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("isatty"sv, -errno); | 
					
						
							|  |  |  |     return rc == 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-25 16:03:51 +02:00
										 |  |  | ErrorOr<void> link(StringView old_path, StringView new_path) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-07-25 16:03:51 +02:00
										 |  |  |     Syscall::SC_link_params params { | 
					
						
							|  |  |  |         .old_path = { old_path.characters_without_null_termination(), old_path.length() }, | 
					
						
							|  |  |  |         .new_path = { new_path.characters_without_null_termination(), new_path.length() }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_link, ¶ms); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("link", rc, {}); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString old_path_string = old_path; | 
					
						
							|  |  |  |     DeprecatedString new_path_string = new_path; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:03:51 +02:00
										 |  |  |     if (::link(old_path_string.characters(), new_path_string.characters()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("link"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 20:23:21 +01:00
										 |  |  | ErrorOr<void> symlink(StringView target, StringView link_path) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-12-16 20:23:21 +01:00
										 |  |  |     Syscall::SC_symlink_params params { | 
					
						
							|  |  |  |         .target = { target.characters_without_null_termination(), target.length() }, | 
					
						
							|  |  |  |         .linkpath = { link_path.characters_without_null_termination(), link_path.length() }, | 
					
						
							| 
									
										
										
										
											2022-10-01 11:15:02 +00:00
										 |  |  |         .dirfd = AT_FDCWD, | 
					
						
							| 
									
										
										
										
											2021-12-16 20:23:21 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_symlink, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("symlink", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-12-16 20:23:21 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString target_string = target; | 
					
						
							|  |  |  |     DeprecatedString link_path_string = link_path; | 
					
						
							| 
									
										
										
										
											2021-12-16 20:23:21 +01:00
										 |  |  |     if (::symlink(target_string.characters(), link_path_string.characters()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("symlink"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 20:27:25 +01:00
										 |  |  | ErrorOr<void> mkdir(StringView path, mode_t mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (path.is_null()) | 
					
						
							|  |  |  |         return Error::from_errno(EFAULT); | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-10-01 11:36:24 +00:00
										 |  |  |     int rc = syscall(SC_mkdir, AT_FDCWD, path.characters_without_null_termination(), path.length(), mode); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("mkdir", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-12-16 20:27:25 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = path; | 
					
						
							| 
									
										
										
										
											2021-12-16 20:27:25 +01:00
										 |  |  |     if (::mkdir(path_string.characters(), mode) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("mkdir"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 02:07:26 -07:00
										 |  |  | ErrorOr<void> chdir(StringView path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (path.is_null()) | 
					
						
							|  |  |  |         return Error::from_errno(EFAULT); | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-01-03 02:07:26 -07:00
										 |  |  |     int rc = syscall(SC_chdir, path.characters_without_null_termination(), path.length()); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("chdir", rc, {}); | 
					
						
							| 
									
										
										
										
											2022-01-03 02:07:26 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = path; | 
					
						
							| 
									
										
										
										
											2022-01-03 02:07:26 -07:00
										 |  |  |     if (::chdir(path_string.characters()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("chdir"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 18:35:38 -07:00
										 |  |  | ErrorOr<void> rmdir(StringView path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (path.is_null()) | 
					
						
							|  |  |  |         return Error::from_errno(EFAULT); | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-02-12 18:35:38 -07:00
										 |  |  |     int rc = syscall(SC_rmdir, path.characters_without_null_termination(), path.length()); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("rmdir", rc, {}); | 
					
						
							| 
									
										
										
										
											2022-02-12 18:35:38 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = path; | 
					
						
							| 
									
										
										
										
											2022-02-12 18:35:38 -07:00
										 |  |  |     if (::rmdir(path_string.characters()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("rmdir"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 20:28:54 +01:00
										 |  |  | ErrorOr<pid_t> fork() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     pid_t pid = ::fork(); | 
					
						
							|  |  |  |     if (pid < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("fork"sv, -errno); | 
					
						
							|  |  |  |     return pid; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:10:59 +01:00
										 |  |  | ErrorOr<int> mkstemp(Span<char> pattern) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int fd = ::mkstemp(pattern.data()); | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("mkstemp"sv, -errno); | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:40:42 +01:00
										 |  |  | ErrorOr<void> rename(StringView old_path, StringView new_path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (old_path.is_null() || new_path.is_null()) | 
					
						
							|  |  |  |         return Error::from_errno(EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:40:42 +01:00
										 |  |  |     Syscall::SC_rename_params params { | 
					
						
							| 
									
										
										
										
											2022-10-01 11:42:25 +00:00
										 |  |  |         .olddirfd = AT_FDCWD, | 
					
						
							| 
									
										
										
										
											2021-12-16 21:40:42 +01:00
										 |  |  |         .old_path = { old_path.characters_without_null_termination(), old_path.length() }, | 
					
						
							| 
									
										
										
										
											2022-10-01 11:42:25 +00:00
										 |  |  |         .newdirfd = AT_FDCWD, | 
					
						
							| 
									
										
										
										
											2021-12-16 21:40:42 +01:00
										 |  |  |         .new_path = { new_path.characters_without_null_termination(), new_path.length() }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_rename, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("rename", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-12-16 21:40:42 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString old_path_string = old_path; | 
					
						
							|  |  |  |     DeprecatedString new_path_string = new_path; | 
					
						
							| 
									
										
										
										
											2021-12-16 21:40:42 +01:00
										 |  |  |     if (::rename(old_path_string.characters(), new_path_string.characters()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("rename"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:47:38 +01:00
										 |  |  | ErrorOr<void> unlink(StringView path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (path.is_null()) | 
					
						
							|  |  |  |         return Error::from_errno(EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-02-10 11:30:33 +00:00
										 |  |  |     int rc = syscall(SC_unlink, AT_FDCWD, path.characters_without_null_termination(), path.length(), 0); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("unlink", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-12-29 21:47:38 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = path; | 
					
						
							| 
									
										
										
										
											2021-12-29 21:47:38 +01:00
										 |  |  |     if (::unlink(path_string.characters()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("unlink"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 19:48:30 +01:00
										 |  |  | ErrorOr<void> utime(StringView path, Optional<struct utimbuf> maybe_buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (path.is_null()) | 
					
						
							|  |  |  |         return Error::from_errno(EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct utimbuf* buf = nullptr; | 
					
						
							|  |  |  |     if (maybe_buf.has_value()) | 
					
						
							|  |  |  |         buf = &maybe_buf.value(); | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2021-12-20 19:48:30 +01:00
										 |  |  |     int rc = syscall(SC_utime, path.characters_without_null_termination(), path.length(), buf); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("utime", rc, {}); | 
					
						
							| 
									
										
										
										
											2021-12-20 19:48:30 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = path; | 
					
						
							| 
									
										
										
										
											2021-12-21 01:28:59 +01:00
										 |  |  |     if (::utime(path_string.characters(), buf) < 0) | 
					
						
							| 
									
										
										
										
											2021-12-20 19:48:30 +01:00
										 |  |  |         return Error::from_syscall("utime"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 19:56:28 -07:00
										 |  |  | ErrorOr<struct utsname> uname() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     utsname uts; | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-01-03 19:56:28 -07:00
										 |  |  |     int rc = syscall(SC_uname, &uts); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("uname", rc, uts); | 
					
						
							| 
									
										
										
										
											2022-01-03 19:56:28 -07:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     if (::uname(&uts) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("uname"sv, -errno); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return uts; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 01:06:29 -06:00
										 |  |  | #ifndef AK_OS_ANDROID
 | 
					
						
							| 
									
										
										
										
											2022-01-16 17:25:26 -07:00
										 |  |  | ErrorOr<void> adjtime(const struct timeval* delta, struct timeval* old_delta) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #    ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-01-16 17:25:26 -07:00
										 |  |  |     int rc = syscall(SC_adjtime, delta, old_delta); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("adjtime", rc, {}); | 
					
						
							| 
									
										
										
										
											2022-07-11 01:06:29 -06:00
										 |  |  | #    else
 | 
					
						
							| 
									
										
										
										
											2022-01-16 17:25:26 -07:00
										 |  |  |     if (::adjtime(delta, old_delta) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("adjtime"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2022-07-11 01:06:29 -06:00
										 |  |  | #    endif
 | 
					
						
							| 
									
										
										
										
											2022-01-16 17:25:26 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-11 01:06:29 -06:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-01-16 17:25:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 11:50:40 +02:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							|  |  |  | ErrorOr<void> exec_command(Vector<StringView>& command, bool preserve_env) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Vector<StringView> exec_environment; | 
					
						
							|  |  |  |     for (size_t i = 0; environ[i]; ++i) { | 
					
						
							|  |  |  |         StringView env_view { environ[i], strlen(environ[i]) }; | 
					
						
							|  |  |  |         auto maybe_needle = env_view.find('='); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!maybe_needle.has_value()) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // FIXME: Allow a custom selection of variables once ArgsParser supports options with optional parameters.
 | 
					
						
							|  |  |  |         if (!preserve_env && env_view.substring_view(0, maybe_needle.value()) != "TERM"sv) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         exec_environment.append(env_view); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TRY(Core::System::exec(command.at(0), command, Core::System::SearchInPath::Yes, exec_environment)); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-11-02 22:28:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> join_jail(u64 jail_index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Syscall::SC_jail_attach_params params { jail_index }; | 
					
						
							|  |  |  |     int rc = syscall(SC_jail_attach, ¶ms); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("jail_attach", rc, {}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<u64> create_jail(StringView jail_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Syscall::SC_jail_create_params params { 0, { jail_name.characters_without_null_termination(), jail_name.length() } }; | 
					
						
							|  |  |  |     int rc = syscall(SC_jail_create, ¶ms); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("jail_create", rc, static_cast<u64>(params.index)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-11-04 11:50:40 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  | ErrorOr<void> exec(StringView filename, Span<StringView> arguments, SearchInPath search_in_path, Optional<Span<StringView>> environment) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |     Syscall::SC_execve_params params; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto argument_strings = TRY(FixedArray<Syscall::StringArgument>::try_create(arguments.size())); | 
					
						
							|  |  |  |     for (size_t i = 0; i < arguments.size(); ++i) { | 
					
						
							|  |  |  |         argument_strings[i] = { arguments[i].characters_without_null_termination(), arguments[i].length() }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     params.arguments.strings = argument_strings.data(); | 
					
						
							|  |  |  |     params.arguments.length = argument_strings.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t env_count = 0; | 
					
						
							|  |  |  |     if (environment.has_value()) { | 
					
						
							|  |  |  |         env_count = environment->size(); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         for (size_t i = 0; environ[i]; ++i) | 
					
						
							|  |  |  |             ++env_count; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto environment_strings = TRY(FixedArray<Syscall::StringArgument>::try_create(env_count)); | 
					
						
							|  |  |  |     if (environment.has_value()) { | 
					
						
							|  |  |  |         for (size_t i = 0; i < env_count; ++i) { | 
					
						
							|  |  |  |             environment_strings[i] = { environment->at(i).characters_without_null_termination(), environment->at(i).length() }; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         for (size_t i = 0; i < env_count; ++i) { | 
					
						
							|  |  |  |             environment_strings[i] = { environ[i], strlen(environ[i]) }; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     params.environment.strings = environment_strings.data(); | 
					
						
							|  |  |  |     params.environment.length = environment_strings.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto run_exec = [](Syscall::SC_execve_params& params) -> ErrorOr<void> { | 
					
						
							|  |  |  |         int rc = syscall(Syscall::SC_execve, ¶ms); | 
					
						
							|  |  |  |         if (rc < 0) | 
					
						
							|  |  |  |             return Error::from_syscall("exec"sv, rc); | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString exec_filename; | 
					
						
							| 
									
										
										
										
											2022-08-20 18:31:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (search_in_path == SearchInPath::Yes) { | 
					
						
							|  |  |  |         auto maybe_executable = Core::File::resolve_executable_from_environment(filename); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!maybe_executable.has_value()) | 
					
						
							|  |  |  |             return ENOENT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         exec_filename = maybe_executable.release_value(); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |         exec_filename = filename.to_deprecated_string(); | 
					
						
							| 
									
										
										
										
											2022-08-20 18:31:03 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 16:55:44 +03:00
										 |  |  |     params.path = { exec_filename.characters(), exec_filename.length() }; | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |     TRY(run_exec(params)); | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString filename_string { filename }; | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     auto argument_strings = TRY(FixedArray<DeprecatedString>::try_create(arguments.size())); | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |     auto argv = TRY(FixedArray<char*>::try_create(arguments.size() + 1)); | 
					
						
							|  |  |  |     for (size_t i = 0; i < arguments.size(); ++i) { | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |         argument_strings[i] = arguments[i].to_deprecated_string(); | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |         argv[i] = const_cast<char*>(argument_strings[i].characters()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     argv[arguments.size()] = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int rc = 0; | 
					
						
							|  |  |  |     if (environment.has_value()) { | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         auto environment_strings = TRY(FixedArray<DeprecatedString>::try_create(environment->size())); | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |         auto envp = TRY(FixedArray<char*>::try_create(environment->size() + 1)); | 
					
						
							|  |  |  |         for (size_t i = 0; i < environment->size(); ++i) { | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |             environment_strings[i] = environment->at(i).to_deprecated_string(); | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |             envp[i] = const_cast<char*>(environment_strings[i].characters()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         envp[environment->size()] = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (search_in_path == SearchInPath::Yes && !filename.contains('/')) { | 
					
						
							| 
									
										
										
										
											2022-10-09 16:45:59 -06:00
										 |  |  | #    if defined(AK_OS_MACOS) || defined(AK_OS_FREEBSD)
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |             // These BSDs don't support execvpe(), so we'll have to manually search the PATH.
 | 
					
						
							|  |  |  |             ScopedValueRollback errno_rollback(errno); | 
					
						
							| 
									
										
										
										
											2022-08-20 18:31:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             auto maybe_executable = Core::File::resolve_executable_from_environment(filename_string); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!maybe_executable.has_value()) { | 
					
						
							|  |  |  |                 errno_rollback.set_override_rollback_value(ENOENT); | 
					
						
							|  |  |  |                 return Error::from_errno(ENOENT); | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-08-20 18:31:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             rc = ::execve(maybe_executable.release_value().characters(), argv.data(), envp.data()); | 
					
						
							| 
									
										
										
										
											2022-03-12 20:23:43 +00:00
										 |  |  | #    else
 | 
					
						
							|  |  |  |             rc = ::execvpe(filename_string.characters(), argv.data(), envp.data()); | 
					
						
							|  |  |  | #    endif
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             rc = ::execve(filename_string.characters(), argv.data(), envp.data()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (search_in_path == SearchInPath::Yes) | 
					
						
							|  |  |  |             rc = ::execvp(filename_string.characters(), argv.data()); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             rc = ::execv(filename_string.characters(), argv.data()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("exec"sv, rc); | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 12:52:19 +00:00
										 |  |  | ErrorOr<int> socket(int domain, int type, int protocol) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto fd = ::socket(domain, type, protocol); | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("socket"sv, -errno); | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> bind(int sockfd, struct sockaddr const* address, socklen_t address_length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::bind(sockfd, address, address_length) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("bind"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> listen(int sockfd, int backlog) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::listen(sockfd, backlog) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("listen"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<int> accept(int sockfd, struct sockaddr* address, socklen_t* address_length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto fd = ::accept(sockfd, address, address_length); | 
					
						
							|  |  |  |     if (fd < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("accept"sv, -errno); | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> connect(int sockfd, struct sockaddr const* address, socklen_t address_length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::connect(sockfd, address, address_length) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("connect"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> shutdown(int sockfd, int how) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::shutdown(sockfd, how) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("shutdown"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<ssize_t> send(int sockfd, void const* buffer, size_t buffer_length, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto sent = ::send(sockfd, buffer, buffer_length, flags); | 
					
						
							|  |  |  |     if (sent < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("send"sv, -errno); | 
					
						
							|  |  |  |     return sent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<ssize_t> sendmsg(int sockfd, const struct msghdr* message, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto sent = ::sendmsg(sockfd, message, flags); | 
					
						
							|  |  |  |     if (sent < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("sendmsg"sv, -errno); | 
					
						
							|  |  |  |     return sent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<ssize_t> sendto(int sockfd, void const* source, size_t source_length, int flags, struct sockaddr const* destination, socklen_t destination_length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto sent = ::sendto(sockfd, source, source_length, flags, destination, destination_length); | 
					
						
							|  |  |  |     if (sent < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("sendto"sv, -errno); | 
					
						
							|  |  |  |     return sent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<ssize_t> recv(int sockfd, void* buffer, size_t length, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto received = ::recv(sockfd, buffer, length, flags); | 
					
						
							|  |  |  |     if (received < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("recv"sv, -errno); | 
					
						
							|  |  |  |     return received; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<ssize_t> recvmsg(int sockfd, struct msghdr* message, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto received = ::recvmsg(sockfd, message, flags); | 
					
						
							|  |  |  |     if (received < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("recvmsg"sv, -errno); | 
					
						
							|  |  |  |     return received; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<ssize_t> recvfrom(int sockfd, void* buffer, size_t buffer_length, int flags, struct sockaddr* address, socklen_t* address_length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto received = ::recvfrom(sockfd, buffer, buffer_length, flags, address, address_length); | 
					
						
							|  |  |  |     if (received < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("recvfrom"sv, -errno); | 
					
						
							|  |  |  |     return received; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> getsockopt(int sockfd, int level, int option, void* value, socklen_t* value_size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::getsockopt(sockfd, level, option, value, value_size) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("getsockopt"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> setsockopt(int sockfd, int level, int option, void const* value, socklen_t value_size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::setsockopt(sockfd, level, option, value, value_size) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("setsockopt"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> getsockname(int sockfd, struct sockaddr* address, socklen_t* address_length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::getsockname(sockfd, address, address_length) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("getsockname"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> getpeername(int sockfd, struct sockaddr* address, socklen_t* address_length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::getpeername(sockfd, address, address_length) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("getpeername"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> socketpair(int domain, int type, int protocol, int sv[2]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::socketpair(domain, type, protocol, sv) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("socketpair"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 06:16:46 +03:30
										 |  |  | ErrorOr<Array<int, 2>> pipe2([[maybe_unused]] int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Array<int, 2> fds; | 
					
						
							|  |  |  | #if defined(__unix__)
 | 
					
						
							|  |  |  |     if (::pipe2(fds.data(), flags) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("pipe2"sv, -errno); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     if (::pipe(fds.data()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("pipe2"sv, -errno); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return fds; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 16:19:51 +01:00
										 |  |  | ErrorOr<Vector<gid_t>> getgroups() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int count = ::getgroups(0, nullptr); | 
					
						
							|  |  |  |     if (count < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("getgroups"sv, -errno); | 
					
						
							|  |  |  |     if (count == 0) | 
					
						
							|  |  |  |         return Vector<gid_t> {}; | 
					
						
							|  |  |  |     Vector<gid_t> groups; | 
					
						
							|  |  |  |     TRY(groups.try_resize(count)); | 
					
						
							|  |  |  |     if (::getgroups(count, groups.data()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("getgroups"sv, -errno); | 
					
						
							|  |  |  |     return groups; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-09 14:49:49 +02:00
										 |  |  | ErrorOr<void> setgroups(Span<gid_t const> gids) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (::setgroups(gids.size(), gids.data()) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("setgroups"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 17:08:46 +01:00
										 |  |  | ErrorOr<void> mknod(StringView pathname, mode_t mode, dev_t dev) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (pathname.is_null()) | 
					
						
							|  |  |  |         return Error::from_syscall("mknod"sv, -EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-01-14 17:08:46 +01:00
										 |  |  |     Syscall::SC_mknod_params params { { pathname.characters_without_null_termination(), pathname.length() }, mode, dev }; | 
					
						
							|  |  |  |     int rc = syscall(SC_mknod, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("mknod", rc, {}); | 
					
						
							| 
									
										
										
										
											2022-01-14 17:08:46 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = pathname; | 
					
						
							| 
									
										
										
										
											2022-01-14 17:08:46 +01:00
										 |  |  |     if (::mknod(path_string.characters(), mode, dev) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("mknod"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-17 10:04:00 +01:00
										 |  |  | ErrorOr<void> mkfifo(StringView pathname, mode_t mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return mknod(pathname, mode | S_IFIFO, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 23:43:55 +01:00
										 |  |  | ErrorOr<void> setenv(StringView name, StringView value, bool overwrite) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-03-01 23:43:55 +01:00
										 |  |  |     auto const rc = ::serenity_setenv(name.characters_without_null_termination(), name.length(), value.characters_without_null_termination(), value.length(), overwrite); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString name_string = name; | 
					
						
							|  |  |  |     DeprecatedString value_string = value; | 
					
						
							| 
									
										
										
										
											2022-03-01 23:43:55 +01:00
										 |  |  |     auto const rc = ::setenv(name_string.characters(), value_string.characters(), overwrite); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return Error::from_syscall("setenv"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-03-01 23:43:55 +01:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 23:44:53 +01:00
										 |  |  | ErrorOr<int> posix_openpt(int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int const rc = ::posix_openpt(flags); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return Error::from_syscall("posix_openpt"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-03-01 23:44:53 +01:00
										 |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> grantpt(int fildes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto const rc = ::grantpt(fildes); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return Error::from_syscall("grantpt"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-03-01 23:44:53 +01:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> unlockpt(int fildes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto const rc = ::unlockpt(fildes); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return Error::from_syscall("unlockpt"sv, -errno); | 
					
						
							| 
									
										
										
										
											2022-03-01 23:44:53 +01:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-29 19:24:06 +02:00
										 |  |  | ErrorOr<void> access(StringView pathname, int mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (pathname.is_null()) | 
					
						
							|  |  |  |         return Error::from_syscall("access"sv, -EFAULT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2022-10-01 12:24:56 +00:00
										 |  |  |     Syscall::SC_faccessat_params params { | 
					
						
							|  |  |  |         .dirfd = AT_FDCWD, | 
					
						
							|  |  |  |         .pathname = { pathname.characters_without_null_termination(), pathname.length() }, | 
					
						
							|  |  |  |         .mode = mode, | 
					
						
							|  |  |  |         .flags = 0, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = ::syscall(Syscall::SC_faccessat, ¶ms); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("access", rc, {}); | 
					
						
							| 
									
										
										
										
											2022-03-29 19:24:06 +02:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = pathname; | 
					
						
							| 
									
										
										
										
											2022-03-29 19:24:06 +02:00
										 |  |  |     if (::access(path_string.characters(), mode) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("access"sv, -errno); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | ErrorOr<DeprecatedString> readlink(StringView pathname) | 
					
						
							| 
									
										
										
										
											2022-10-24 22:15:58 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: Try again with a larger buffer.
 | 
					
						
							|  |  |  |     char data[PATH_MAX]; | 
					
						
							|  |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							|  |  |  |     Syscall::SC_readlink_params small_params { | 
					
						
							| 
									
										
										
										
											2022-10-01 11:20:08 +00:00
										 |  |  |         .path = { pathname.characters_without_null_termination(), pathname.length() }, | 
					
						
							|  |  |  |         .buffer = { data, sizeof(data) }, | 
					
						
							|  |  |  |         .dirfd = AT_FDCWD, | 
					
						
							| 
									
										
										
										
											2022-10-24 22:15:58 -06:00
										 |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_readlink, &small_params); | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("readlink", rc, DeprecatedString(data, rc)); | 
					
						
							| 
									
										
										
										
											2022-10-24 22:15:58 -06:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     DeprecatedString path_string = pathname; | 
					
						
							| 
									
										
										
										
											2022-10-24 22:15:58 -06:00
										 |  |  |     int rc = ::readlink(path_string.characters(), data, sizeof(data)); | 
					
						
							|  |  |  |     if (rc == -1) | 
					
						
							|  |  |  |         return Error::from_syscall("readlink"sv, -errno); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     return DeprecatedString(data, rc); | 
					
						
							| 
									
										
										
										
											2022-10-24 22:15:58 -06:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-27 19:46:03 +01:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							|  |  |  | ErrorOr<void> posix_fallocate(int fd, off_t offset, off_t length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = ::posix_fallocate(fd, offset, length); | 
					
						
							|  |  |  |     if (rc != 0) | 
					
						
							|  |  |  |         return Error::from_syscall("posix_fallocate"sv, -rc); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | } |