| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:59:16 +01:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-23 10:59:50 +01:00
										 |  |  | #include <LibCore/System.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | #include <LibSystem/syscall.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:10:19 +01:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <stdarg.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:47:32 +01:00
										 |  |  | #include <sys/mman.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 10:48:22 +01:00
										 |  |  | #define HANDLE_SYSCALL_RETURN_VALUE(syscall_name, rc, success_value) \
 | 
					
						
							|  |  |  |     if ((rc) < 0) {                                                  \ | 
					
						
							|  |  |  |         return Error::from_syscall(syscall_name, rc);                \ | 
					
						
							|  |  |  |     }                                                                \ | 
					
						
							|  |  |  |     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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 10:59:50 +01:00
										 |  |  | #ifdef __serenity__
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2021-11-23 10:48:22 +01:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("pledge"sv, rc, {}); | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> unveil(StringView path, StringView permissions) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Syscall::SC_unveil_params params { | 
					
						
							|  |  |  |         { path.characters_without_null_termination(), path.length() }, | 
					
						
							|  |  |  |         { permissions.characters_without_null_termination(), permissions.length() }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int rc = syscall(SC_unveil, ¶ms); | 
					
						
							| 
									
										
										
										
											2021-11-23 10:48:22 +01:00
										 |  |  |     HANDLE_SYSCALL_RETURN_VALUE("unveil"sv, rc, {}); | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-23 10:59:50 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     u32 extra_arg = va_arg(ap, u32); | 
					
						
							|  |  |  |     int rc = ::fcntl(fd, command, extra_arg); | 
					
						
							|  |  |  |     va_end(ap); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("fcntl"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef __serenity__
 | 
					
						
							|  |  |  |     Syscall::SC_mmap_params params { (uintptr_t)address, size, alignment, protection, flags, fd, offset, { name.characters_without_null_termination(), name.length() } }; | 
					
						
							|  |  |  |     ptrdiff_t rc = syscall(SC_mmap, ¶ms); | 
					
						
							|  |  |  |     if (rc < 0 && rc > -EMAXERRNO) | 
					
						
							|  |  |  |         return Error::from_syscall("mmap"sv, rc); | 
					
						
							|  |  |  |     return reinterpret_cast<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 {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:59:16 +01:00
										 |  |  | ErrorOr<int> open(StringView path, int options, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!path.characters_without_null_termination()) | 
					
						
							|  |  |  |         return Error::from_syscall("open"sv, -EFAULT); | 
					
						
							|  |  |  |     va_list ap; | 
					
						
							|  |  |  |     va_start(ap, options); | 
					
						
							|  |  |  |     auto mode = (mode_t)va_arg(ap, unsigned); | 
					
						
							|  |  |  |     va_end(ap); | 
					
						
							|  |  |  | #ifdef __serenity__
 | 
					
						
							|  |  |  |     Syscall::SC_open_params params { AT_FDCWD, { path.characters_without_null_termination(), path.length() }, options, mode }; | 
					
						
							|  |  |  |     int rc = syscall(SC_open, ¶ms); | 
					
						
							|  |  |  |     HANDLE_SYSCALL_RETURN_VALUE("open"sv, rc, rc); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     // NOTE: We have to ensure that the path is null-terminated.
 | 
					
						
							| 
									
										
										
										
											2021-11-23 11:59:54 +01:00
										 |  |  |     String path_string = path; | 
					
						
							| 
									
										
										
										
											2021-11-23 11:59:16 +01:00
										 |  |  |     int rc = ::open(path_string.characters(), options, mode); | 
					
						
							|  |  |  |     if (rc < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("open"sv, -errno); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 15:43:57 +01:00
										 |  |  | } |