SystemWindows.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com>
  6. * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
  7. * Copyright (c) 2024, stasoid <stasoid@yahoo.com>
  8. *
  9. * SPDX-License-Identifier: BSD-2-Clause
  10. */
  11. #include <AK/ByteString.h>
  12. #include <AK/ScopeGuard.h>
  13. #include <LibCore/System.h>
  14. #include <direct.h>
  15. #include <sys/mman.h>
  16. #include <AK/Windows.h>
  17. namespace Core::System {
  18. static void invalid_parameter_handler(wchar_t const*, wchar_t const*, wchar_t const*, unsigned int, uintptr_t)
  19. {
  20. }
  21. static int init_crt_and_wsa()
  22. {
  23. WSADATA wsa;
  24. WORD version = MAKEWORD(2, 2);
  25. int rc = WSAStartup(version, &wsa);
  26. VERIFY(!rc && wsa.wVersion == version);
  27. // Make _get_osfhandle return -1 instead of crashing on invalid fd in release (debug still __debugbreak's)
  28. _set_invalid_parameter_handler(invalid_parameter_handler);
  29. return 0;
  30. }
  31. static auto dummy = init_crt_and_wsa();
  32. int handle_to_fd(HANDLE handle, HandleType type)
  33. {
  34. return handle_to_fd((intptr_t)handle, type);
  35. }
  36. int handle_to_fd(intptr_t handle, HandleType type)
  37. {
  38. if (type != SocketHandle && type != FileMappingHandle)
  39. return _open_osfhandle(handle, 0);
  40. // Special treatment for socket and file mapping handles because:
  41. // * _open_osfhandle doesn't support file mapping handles
  42. // * _close doesn't properly support socket handles (it calls CloseHandle instead of closesocket)
  43. // Handle value is held in lower 31 bits, and sign bit is set to indicate this is not a regular fd.
  44. VERIFY((handle >> 31) == 0); // must be 0 ⩽ handle ⩽ 0x7FFFFFFF
  45. return (1 << 31) | handle;
  46. }
  47. HANDLE fd_to_handle(int fd)
  48. {
  49. if (fd >= 0)
  50. return (HANDLE)_get_osfhandle(fd);
  51. if (fd == -1)
  52. return INVALID_HANDLE_VALUE;
  53. return (HANDLE)(intptr_t)(fd & ~(1 << 31));
  54. }
  55. ErrorOr<int> open(StringView path, int options, mode_t mode)
  56. {
  57. ByteString string_path = path;
  58. auto sz_path = string_path.characters();
  59. int rc = _open(sz_path, options | O_BINARY, mode);
  60. if (rc < 0) {
  61. int error = errno;
  62. struct stat st = {};
  63. if (::stat(sz_path, &st) == 0 && (st.st_mode & S_IFDIR)) {
  64. HANDLE dir_handle = CreateFile(sz_path, GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  65. if (dir_handle == INVALID_HANDLE_VALUE)
  66. return Error::from_windows_error();
  67. return handle_to_fd(dir_handle, DirectoryHandle);
  68. }
  69. return Error::from_syscall("open"sv, -error);
  70. }
  71. return rc;
  72. }
  73. ErrorOr<void> close(int fd)
  74. {
  75. if (fd < 0) {
  76. HANDLE handle = fd_to_handle(fd);
  77. if (handle == INVALID_HANDLE_VALUE)
  78. return Error::from_string_literal("Invalid file descriptor");
  79. if (is_socket(fd)) {
  80. if (closesocket((SOCKET)handle))
  81. return Error::from_windows_error();
  82. } else {
  83. if (!CloseHandle(handle))
  84. return Error::from_windows_error();
  85. }
  86. return {};
  87. }
  88. if (_close(fd) < 0)
  89. return Error::from_syscall("close"sv, -errno);
  90. return {};
  91. }
  92. ErrorOr<ssize_t> read(int fd, Bytes buffer)
  93. {
  94. int rc = _read(fd, buffer.data(), buffer.size());
  95. if (rc < 0)
  96. return Error::from_syscall("read"sv, -errno);
  97. return rc;
  98. }
  99. ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer)
  100. {
  101. int rc = _write(fd, buffer.data(), buffer.size());
  102. if (rc < 0)
  103. return Error::from_syscall("write"sv, -errno);
  104. return rc;
  105. }
  106. ErrorOr<off_t> lseek(int fd, off_t offset, int whence)
  107. {
  108. long rc = _lseek(fd, offset, whence);
  109. if (rc < 0)
  110. return Error::from_syscall("lseek"sv, -errno);
  111. return rc;
  112. }
  113. ErrorOr<void> ftruncate(int fd, off_t length)
  114. {
  115. long position = _tell(fd);
  116. if (position == -1)
  117. return Error::from_errno(errno);
  118. ScopeGuard restore_position { [&] { _lseek(fd, position, SEEK_SET); } };
  119. auto result = lseek(fd, length, SEEK_SET);
  120. if (result.is_error())
  121. return result.release_error();
  122. if (SetEndOfFile(fd_to_handle(fd)) == 0)
  123. return Error::from_windows_error();
  124. return {};
  125. }
  126. ErrorOr<struct stat> fstat(int fd)
  127. {
  128. struct stat st = {};
  129. if (::fstat(fd, &st) < 0)
  130. return Error::from_syscall("fstat"sv, -errno);
  131. return st;
  132. }
  133. ErrorOr<void> ioctl(int, unsigned, ...)
  134. {
  135. dbgln("Core::System::ioctl() is not implemented");
  136. VERIFY_NOT_REACHED();
  137. }
  138. ErrorOr<ByteString> getcwd()
  139. {
  140. auto* cwd = _getcwd(nullptr, 0);
  141. if (!cwd)
  142. return Error::from_syscall("getcwd"sv, -errno);
  143. ByteString string_cwd(cwd);
  144. free(cwd);
  145. return string_cwd;
  146. }
  147. ErrorOr<struct stat> stat(StringView path)
  148. {
  149. if (path.is_null())
  150. return Error::from_syscall("stat"sv, -EFAULT);
  151. struct stat st = {};
  152. ByteString path_string = path;
  153. if (::stat(path_string.characters(), &st) < 0)
  154. return Error::from_syscall("stat"sv, -errno);
  155. return st;
  156. }
  157. ErrorOr<void> rmdir(StringView path)
  158. {
  159. if (path.is_null())
  160. return Error::from_errno(EFAULT);
  161. ByteString path_string = path;
  162. if (_rmdir(path_string.characters()) < 0)
  163. return Error::from_syscall("rmdir"sv, -errno);
  164. return {};
  165. }
  166. ErrorOr<void> unlink(StringView path)
  167. {
  168. if (path.is_null())
  169. return Error::from_errno(EFAULT);
  170. ByteString path_string = path;
  171. if (_unlink(path_string.characters()) < 0)
  172. return Error::from_syscall("unlink"sv, -errno);
  173. return {};
  174. }
  175. ErrorOr<void> mkdir(StringView path, mode_t)
  176. {
  177. ByteString str = path;
  178. if (_mkdir(str.characters()) < 0)
  179. return Error::from_syscall("mkdir"sv, -errno);
  180. return {};
  181. }
  182. ErrorOr<int> openat(int, StringView, int, mode_t)
  183. {
  184. dbgln("Core::System::openat() is not implemented");
  185. VERIFY_NOT_REACHED();
  186. }
  187. ErrorOr<struct stat> fstatat(int, StringView, int)
  188. {
  189. dbgln("Core::System::fstatat() is not implemented");
  190. VERIFY_NOT_REACHED();
  191. }
  192. ErrorOr<void*> mmap(void* address, size_t size, int protection, int flags, int fd, off_t offset, size_t alignment, StringView)
  193. {
  194. // custom alignment is not supported
  195. VERIFY(!alignment);
  196. void* ptr = ::mmap(address, size, protection, flags, fd, offset);
  197. if (ptr == MAP_FAILED)
  198. return Error::from_syscall("mmap"sv, -errno);
  199. return ptr;
  200. }
  201. ErrorOr<void> munmap(void* address, size_t size)
  202. {
  203. if (::munmap(address, size) < 0)
  204. return Error::from_syscall("munmap"sv, -errno);
  205. return {};
  206. }
  207. int getpid()
  208. {
  209. return GetCurrentProcessId();
  210. }
  211. ErrorOr<int> dup(int fd)
  212. {
  213. if (fd < 0) {
  214. HANDLE handle = fd_to_handle(fd);
  215. if (handle == INVALID_HANDLE_VALUE)
  216. return Error::from_string_literal("Invalid file descriptor");
  217. if (is_socket(fd)) {
  218. WSAPROTOCOL_INFO pi = {};
  219. if (WSADuplicateSocket((SOCKET)handle, GetCurrentProcessId(), &pi))
  220. return Error::from_windows_error();
  221. SOCKET socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, &pi, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
  222. if (socket == INVALID_SOCKET)
  223. return Error::from_windows_error();
  224. return handle_to_fd(socket, SocketHandle);
  225. } else {
  226. if (!DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0, FALSE, DUPLICATE_SAME_ACCESS))
  227. return Error::from_windows_error();
  228. return handle_to_fd(handle, FileMappingHandle);
  229. }
  230. }
  231. int new_fd = _dup(fd);
  232. if (new_fd < 0)
  233. return Error::from_syscall("dup"sv, -errno);
  234. return new_fd;
  235. }
  236. bool is_socket(int fd)
  237. {
  238. int val, len = sizeof(val);
  239. return !::getsockopt((SOCKET)fd_to_handle(fd), SOL_SOCKET, SO_TYPE, (char*)&val, &len);
  240. }
  241. }