#include "prx/libkernel/include/Api.hpp" #include "prx/libc/include/Api.hpp" #include "prx/libkernel/File/include/DescriptorControl.hpp" #include "prx/libkernel/Equeue/Equeue.hpp" #include "prx/libkernel/File/include/DirectoryDescriptor.hpp" #include "prx/libkernel/File/include/File.hpp" #include "prx/libkernel/File/include/RandomDevice.hpp" #include "prx/libkernel/Equeue/Equeue.hpp" #include "prx/libkernel/File/include/ArchivedFile.hpp" #include "prx/libkernel/Semaphore/include/Ksem.hpp" #include "prx/libkernel/Socket/include/SocketRuntime.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/FileLock.hpp" #include "prx/libc/include/general/VabiMacros.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef _WIN32 #include #include #include #include "prx/libc/include/General.hpp" #include "prx/libc/include/WindowsDescriptor.hpp" #else #include #include #include #include #endif namespace { constexpr int GuestEintr = 4; constexpr int GuestEbadf = 9; constexpr int GuestEdeadlk = 11; constexpr int GuestEfault = 14; constexpr int GuestEinval = 22; constexpr int GuestEnotty = 25; constexpr int GuestEagain = 35; constexpr int GuestEopnotsupp = 45; constexpr int GuestEnolck = 77; constexpr int GuestEoverflow = 84; constexpr int DirectoryReadableBytes = 65536; constexpr int DuplicateCommand = 0; constexpr int GetDescriptorFlags = 1; constexpr int SetDescriptorFlags = 2; constexpr int GetStatusFlags = 3; constexpr int SetStatusFlags = 4; constexpr int GetOwner = 5; constexpr int SetOwner = 6; constexpr int OldGetLock = 7; constexpr int OldSetLock = 8; constexpr int OldSetLockWait = 9; constexpr int DuplicateToCommand = 10; constexpr int GetLock = 11; constexpr int SetLock = 12; constexpr int SetLockWait = 13; constexpr int SetLockRemote = 14; constexpr int ReadAhead = 15; constexpr int DarwinReadAhead = 16; constexpr int DuplicateCloseOnExec = 17; constexpr int DuplicateToCloseOnExec = 18; constexpr int CloseOnExec = 1; constexpr int OpenCloseOnExec = 0x00100000; constexpr int ReadOnly = 0; constexpr int WriteOnly = 1; constexpr int ReadWrite = 2; constexpr int Nonblocking = 0x0004; constexpr int Append = 0x0008; constexpr int Asynchronous = 0x0040; constexpr int Synchronous = 0x0080; constexpr int ReadAheadFlag = 0x0200; constexpr int Direct = 0x00010000; constexpr int OpenStatusFlags = Nonblocking | Append | Asynchronous | Synchronous | Direct; constexpr int SettableStatusFlags = OpenStatusFlags | ReadAheadFlag; constexpr std::int16_t ReadLock = 1; constexpr std::int16_t Unlock = 2; constexpr std::int16_t WriteLock = 3; constexpr std::int16_t FromStart = 0; constexpr std::int16_t FromCurrent = 1; constexpr std::int16_t FromEnd = 2; constexpr std::uint32_t CloseOnExecRequest = 0x20006601; constexpr std::uint32_t KeepOnExecRequest = 0x20006602; constexpr std::uint32_t NonblockingRequest = 0x8004667e; constexpr std::uint32_t AsynchronousRequest = 0x8004667d; constexpr std::uint32_t SetOwnerRequest = 0x8004667c; constexpr std::uint32_t GetOwnerRequest = 0x4004667b; constexpr std::uint32_t DeviceTypeRequest = 0x4004667a; constexpr std::uint32_t DeviceNameRequest = 0x80106678; constexpr std::uint32_t ReadableRequest = 0x4004667f; constexpr std::uint32_t RequestIn = 0x80000000u; constexpr std::uint32_t RequestOut = 0x40000000u; constexpr std::uint32_t RequestVoid = 0x20000000u; constexpr std::uint32_t TerminalGroup = 't'; struct GuestLock { std::int64_t start; std::int64_t length; std::int32_t pid; std::int16_t type; std::int16_t whence; std::int32_t system; }; static_assert(sizeof(GuestLock) == 32); static_assert(offsetof(GuestLock, type) == 20); constexpr std::size_t OldGuestLockSize = 24; enum class Kind { File, Directory, Pipe, Socket, Device, RandomDevice, ArchivedFile }; struct Entry { bool closeOnExec = false; std::shared_ptr status; std::shared_ptr owner; bool pipe = false; bool fixedSizeWrites = false; }; std::mutex g_mutex; std::map g_entries; int Fail(int error) { *__error_nid_postfix() = error; return -1; } const char* KindName(Kind kind) { switch (kind) { case Kind::File: return "file"; case Kind::Directory: return "directory"; case Kind::Pipe: return "pipe"; case Kind::Socket: return "socket"; case Kind::Device: return "character device"; case Kind::RandomDevice: return "random device"; case Kind::ArchivedFile: return "file read from app0.zar"; } return "descriptor"; } [[noreturn]] void Unsupported(const char* function, const std::string& what, Kind kind, int fd) { throw std::runtime_error(std::string(function) + ": " + what + " on a " + KindName(kind) + " is not implemented, fd=" + std::to_string(fd)); } #ifdef _WIN32 std::optional Classify(int fd, const char* function) { if (fd < 0 || (!GuestSockets::IsOpen(fd) && !DescriptorIsOpen_nid_no_patch(fd))) return std::nullopt; if (GuestSockets::IsOpen(fd)) return Kind::Socket; if (File::IsDirectoryDescriptor(fd)) return Kind::Directory; if (File::IsRandomDevice(fd)) return Kind::RandomDevice; if (File::IsArchivedFile(fd)) return Kind::ArchivedFile; switch (::GetFileType(WindowsDescriptorHandle_nid_no_patch(fd))) { case FILE_TYPE_DISK: return Kind::File; case FILE_TYPE_PIPE: return Kind::Pipe; case FILE_TYPE_CHAR: return Kind::Device; default: throw std::runtime_error(std::string(function) + ": descriptor " + std::to_string(fd) + " has an unknown Windows file type, error=" + std::to_string(::GetLastError())); } } int AccessMode(int fd, const char* function) { if (GuestSockets::IsOpen(fd)) return ReadWrite; return WindowsDescriptorAccess_nid_no_patch(WindowsDescriptorHandle_nid_no_patch(fd), fd, function); } DWORD PipeState(int fd, const char* function) { DWORD state = 0; if (!::GetNamedPipeHandleStateW(WindowsDescriptorHandle_nid_no_patch(fd), &state, nullptr, nullptr, nullptr, nullptr, 0)) throw std::runtime_error(std::string(function) + ": cannot read the mode of pipe descriptor " + std::to_string(fd) + ", error=" + std::to_string(::GetLastError())); return state; } void SetPipeNonblocking(int fd, bool nonblocking, const char* function) { DWORD mode = (PipeState(fd, function) & PIPE_READMODE_MESSAGE) | (nonblocking ? PIPE_NOWAIT : PIPE_WAIT); if (!::SetNamedPipeHandleState(WindowsDescriptorHandle_nid_no_patch(fd), &mode, nullptr, nullptr)) throw std::runtime_error(std::string(function) + ": cannot change the mode of pipe descriptor " + std::to_string(fd) + ", error=" + std::to_string(::GetLastError())); } Entry InitialEntry(int fd) { int status = 0; if (::GetFileType(WindowsDescriptorHandle_nid_no_patch(fd)) == FILE_TYPE_PIPE && (PipeState(fd, "fcntl") & PIPE_NOWAIT) != 0) status |= Nonblocking; return {false, std::make_shared(status)}; } void SetHostAppend(int fd, bool append, const char* function) { const int translation = ::_setmode(fd, _O_BINARY); if (translation == -1 || ::_setmode(fd, translation) == -1) throw std::runtime_error(std::string(function) + ": cannot read the translation mode of descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); HANDLE copy = nullptr; if (!::DuplicateHandle(::GetCurrentProcess(), WindowsDescriptorHandle_nid_no_patch(fd), ::GetCurrentProcess(), ©, 0, TRUE, DUPLICATE_SAME_ACCESS)) throw std::runtime_error(std::string(function) + ": cannot duplicate the handle of descriptor " + std::to_string(fd) + ", error=" + std::to_string(::GetLastError())); const int replacement = ::_open_osfhandle(reinterpret_cast(copy), (append ? _O_APPEND : 0) | translation); if (replacement < 0) { ::CloseHandle(copy); throw std::runtime_error(std::string(function) + ": cannot open a replacement for descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); } auto* const standard = HostStandardHandle_nid_no_patch(fd); const int moved = ::_dup2(replacement, fd); const int error = errno; ::_close(replacement); KeepStandardHandle_nid_no_patch(fd, standard); if (moved != 0) throw std::runtime_error(std::string(function) + ": cannot replace descriptor " + std::to_string(fd) + ", errno=" + std::to_string(error)); } void ApplyHostStatus(int fd, Kind kind, int changed, int next, const char* function) { if ((changed & Append) && kind == Kind::File) { const auto status = g_entries.at(fd).status; for (const auto& [descriptor, entry] : g_entries) if (entry.status == status && DescriptorIsOpen_nid_no_patch(descriptor)) SetHostAppend(descriptor, (next & Append) != 0, function); } if ((changed & Nonblocking) && kind == Kind::Device) Unsupported(function, "changing O_NONBLOCK", kind, fd); if ((changed & Nonblocking) && kind == Kind::Pipe) SetPipeNonblocking(fd, (next & Nonblocking) != 0, function); } int Readable(int fd, Kind kind, const char* function) { if (kind == Kind::File) { const auto size = ::_filelengthi64(fd); const auto offset = ::_telli64(fd); if (size < 0 || offset < 0) throw std::runtime_error(std::string(function) + ": cannot read the size and offset of descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); return static_cast(size - offset); } if (kind == Kind::Socket) { std::int64_t data = 0; bool eof = false; GuestSockets::Ready(fd, false, &data, &eof); return static_cast(data); } if (AccessMode(fd, function) == WriteOnly) return 0; DWORD available = 0; if (!::PeekNamedPipe(WindowsDescriptorHandle_nid_no_patch(fd), nullptr, 0, nullptr, &available, nullptr)) { const DWORD error = ::GetLastError(); if (error == ERROR_BROKEN_PIPE) return 0; throw std::runtime_error(std::string(function) + ": cannot peek pipe descriptor " + std::to_string(fd) + ", error=" + std::to_string(error)); } return static_cast(available); } std::int64_t LockOffset(int fd, Kind kind) { if (kind == Kind::Directory) { const auto cursor = File::SeekDirectoryDescriptor(fd, 0, FromCurrent); if (!cursor) throw std::runtime_error("fcntl: cannot read the offset of directory descriptor " + std::to_string(fd)); return *cursor; } const auto offset = ::_telli64(fd); if (offset < 0) throw std::runtime_error("fcntl: cannot read the offset of descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); return offset; } std::int64_t LockFileSize(int fd, Kind kind) { if (kind == Kind::Directory) Unsupported("fcntl", "a record lock from the end", kind, fd); const auto size = ::_filelengthi64(fd); if (size < 0) throw std::runtime_error("fcntl: cannot read the size of descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); return size; } int LockRangeError(int fd, Kind kind, const GuestLock& lock) { constexpr auto Largest = std::numeric_limits::max(); std::int64_t start = lock.start; if (lock.whence == FromEnd) { const auto size = LockFileSize(fd, kind); if (lock.start > 0 && size > Largest - lock.start) return GuestEoverflow; start = size + lock.start; } else if (lock.whence != FromStart && lock.whence != FromCurrent) { return GuestEinval; } if (start < 0) return GuestEinval; if (lock.length < 0 && (start == 0 || start + lock.length < 0)) return GuestEinval; if (lock.length > 0 && lock.length - 1 > Largest - start) return GuestEoverflow; return 0; } int ProcessRecordLock(int fd, Kind kind, int command, GuestLock& lock, void* pointer, std::size_t size) { const bool known = lock.type == ReadLock || lock.type == WriteLock || lock.type == Unlock; if (command == GetLock && !known) return Fail(GuestEinval); if (lock.whence == FromCurrent) { const auto offset = LockOffset(fd, kind); if (lock.start > 0 && offset > std::numeric_limits::max() - lock.start) return Fail(GuestEoverflow); lock.start += offset; } if (command != GetLock) { if (!known) return Fail(GuestEinval); const int access = lock.type == Unlock ? ReadWrite : AccessMode(fd, "fcntl"); if ((lock.type == ReadLock && access == WriteOnly) || (lock.type == WriteLock && access == ReadOnly)) return Fail(GuestEbadf); } if (const int error = LockRangeError(fd, kind, lock); error != 0) return Fail(error); if (command != GetLock) return 0; lock.type = Unlock; const GuestArena::HostWrite destination(pointer, size); if (!destination.Open()) return Fail(GuestEfault); std::memcpy(pointer, &lock, size); return 0; } #else std::optional Classify(int fd, const char* function) { if (fd < 0) return std::nullopt; struct stat status{}; if (::fstat(fd, &status) != 0) { if (errno == EBADF) return std::nullopt; throw std::runtime_error(std::string(function) + ": fstat failed, fd=" + std::to_string(fd) + ", errno=" + std::to_string(errno)); } if (File::IsRandomDevice(fd)) return Kind::RandomDevice; if (File::IsArchivedFile(fd)) return Kind::ArchivedFile; if (S_ISREG(status.st_mode)) return Kind::File; if (S_ISDIR(status.st_mode)) return Kind::Directory; if (S_ISFIFO(status.st_mode)) return Kind::Pipe; if (S_ISCHR(status.st_mode)) return Kind::Device; if (S_ISSOCK(status.st_mode)) return Kind::Socket; #ifdef __linux__ return std::nullopt; #else throw std::runtime_error(std::string(function) + ": descriptor " + std::to_string(fd) + " is neither a file, a directory, a pipe, a socket nor a character device"); #endif } int HostStatus(int fd, const char* function) { const int flags = ::fcntl(fd, F_GETFL); if (flags < 0) throw std::runtime_error(std::string(function) + ": host F_GETFL failed, fd=" + std::to_string(fd) + ", errno=" + std::to_string(errno)); return flags; } int AccessMode(int fd, const char* function) { return HostStatus(fd, function) & O_ACCMODE; } Entry InitialEntry(int fd) { const int descriptorFlags = ::fcntl(fd, F_GETFD); if (descriptorFlags < 0) throw std::runtime_error("fcntl: host F_GETFD failed, fd=" + std::to_string(fd) + ", errno=" + std::to_string(errno)); const int host = HostStatus(fd, "fcntl"); int status = 0; if (host & O_NONBLOCK) status |= Nonblocking; if (host & O_APPEND) status |= Append; if (host & O_ASYNC) status |= Asynchronous; if ((host & O_SYNC) == O_SYNC) status |= Synchronous; #ifdef O_DIRECT if (host & O_DIRECT) status |= Direct; #endif return {(descriptorFlags & FD_CLOEXEC) != 0, std::make_shared(status)}; } void ApplyHostStatus(int fd, Kind, int changed, int next, const char* function) { if ((changed & (Nonblocking | Append)) == 0) return; const int host = HostStatus(fd, function); const int wanted = (host & ~(O_NONBLOCK | O_APPEND)) | ((next & Nonblocking) ? O_NONBLOCK : 0) | ((next & Append) ? O_APPEND : 0); if (::fcntl(fd, F_SETFL, wanted) != 0) throw std::runtime_error(std::string(function) + ": host F_SETFL failed, fd=" + std::to_string(fd) + ", errno=" + std::to_string(errno)); } int Readable(int fd, Kind kind, const char* function) { if (kind == Kind::File) { struct stat status{}; const auto offset = ::lseek(fd, 0, SEEK_CUR); if (::fstat(fd, &status) != 0 || offset < 0) throw std::runtime_error(std::string(function) + ": cannot read the size and offset of descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); return static_cast(status.st_size - offset); } if (AccessMode(fd, function) == WriteOnly) return 0; int available = 0; if (::ioctl(fd, FIONREAD, &available) != 0) throw std::runtime_error(std::string(function) + ": host FIONREAD failed, fd=" + std::to_string(fd) + ", errno=" + std::to_string(errno)); return available; } int LockError(int error) { switch (error) { case EACCES: case EAGAIN: return GuestEagain; case EDEADLK: return GuestEdeadlk; case EINTR: return GuestEintr; case ENOLCK: return GuestEnolck; case EBADF: return GuestEbadf; case EINVAL: return GuestEinval; case EOVERFLOW: return GuestEoverflow; default: throw std::runtime_error("fcntl: host record lock failed, errno=" + std::to_string(error)); } } #endif Entry& Materialize(int fd) { const auto found = g_entries.find(fd); if (found != g_entries.end()) return found->second; return g_entries.emplace(fd, InitialEntry(fd)).first->second; } int ChangeStatus(int fd, Kind kind, int mask, int value, const char* function) { std::lock_guard lock(g_mutex); auto& status = *Materialize(fd).status; const int next = (status & ~mask) | (value & mask); const int changed = status ^ next; if ((changed & Asynchronous) && (kind == Kind::Pipe || kind == Kind::Socket || kind == Kind::Device)) Unsupported(function, "O_ASYNC", kind, fd); ApplyHostStatus(fd, kind, changed, next, function); status = next; return 0; } int CurrentStatus(int fd) { std::lock_guard lock(g_mutex); return *Materialize(fd).status; } bool CreatedByPipe(int fd) { std::lock_guard lock(g_mutex); const auto found = g_entries.find(fd); return found != g_entries.end() && found->second.pipe; } void SetCloseOnExec(int fd, bool value) { std::lock_guard lock(g_mutex); Materialize(fd).closeOnExec = value; } int RecordLock(int fd, int command, std::intptr_t argument) { const bool old = command <= OldSetLockWait; const int current = old ? command + (GetLock - OldGetLock) : command; const std::size_t size = old ? OldGuestLockSize : sizeof(GuestLock); auto* pointer = reinterpret_cast(argument); GuestLock lock{}; if (!GuestCopyIn_nid_no_patch(&lock, pointer, size)) return Fail(GuestEfault); if (File::IsVirtualDirectoryDescriptor(fd)) return Fail(GuestEinval); const auto kind = Classify(fd, "fcntl"); if (!kind || *kind == Kind::Pipe || *kind == Kind::Socket) return Fail(GuestEbadf); if (*kind != Kind::File && *kind != Kind::Directory) Unsupported("fcntl", "a record lock", *kind, fd); #ifdef _WIN32 return ProcessRecordLock(fd, *kind, current, lock, pointer, size); #else struct flock host{}; if (lock.type == ReadLock) host.l_type = F_RDLCK; else if (lock.type == WriteLock) host.l_type = F_WRLCK; else if (lock.type == Unlock) host.l_type = current == GetLock ? F_RDLCK : F_UNLCK; else return Fail(GuestEinval); host.l_whence = lock.whence; host.l_start = static_cast(lock.start); host.l_len = static_cast(lock.length); const int hostCommand = current == GetLock ? F_GETLK : current == SetLock ? F_SETLK : F_SETLKW; if (::fcntl(fd, hostCommand, &host) != 0) return Fail(LockError(errno)); if (current != GetLock) return 0; if (host.l_type == F_UNLCK) { if (lock.whence == FromCurrent) { const auto offset = ::lseek(fd, 0, SEEK_CUR); if (offset < 0) throw std::runtime_error("fcntl: cannot read the offset of descriptor " + std::to_string(fd) + ", errno=" + std::to_string(errno)); lock.start += offset; } lock.type = Unlock; } else { lock.type = host.l_type == F_RDLCK ? ReadLock : WriteLock; lock.whence = SEEK_SET; lock.start = host.l_start; lock.length = host.l_len; lock.pid = host.l_pid; lock.system = 0; } const GuestArena::HostWrite destination(pointer, size); if (!destination.Open()) return Fail(GuestEfault); std::memcpy(pointer, &lock, size); return 0; #endif } int ReadAheadControl(int fd, int command, std::intptr_t argument) { const auto kind = Classify(fd, "fcntl"); if (!kind || *kind == Kind::Pipe || *kind == Kind::Socket) return Fail(GuestEbadf); if (*kind != Kind::File && *kind != Kind::ArchivedFile) return Fail(GuestEnotty); std::lock_guard lock(g_mutex); auto& status = *Materialize(fd).status; if (command == DarwinReadAhead || argument >= 0) status |= ReadAheadFlag; else status &= ~ReadAheadFlag; return 0; } } namespace File { FcntlArgument FcntlArgumentOf(int command) { switch (command) { case DuplicateCommand: case SetDescriptorFlags: case SetStatusFlags: case SetOwner: case DuplicateToCommand: case ReadAhead: case DarwinReadAhead: case DuplicateCloseOnExec: case DuplicateToCloseOnExec: return FcntlArgument::Int; case OldGetLock: case OldSetLock: case OldSetLockWait: case GetLock: case SetLock: case SetLockWait: case SetLockRemote: return FcntlArgument::Pointer; default: return FcntlArgument::None; } } int Fcntl(int fd, int command, std::intptr_t argument) { switch (command) { case DuplicateCommand: { if (fd < 0) return Fail(GuestEbadf); if (static_cast(argument) < 0) return Fail(GuestEinval); if (!Classify(fd, "fcntl")) return Fail(GuestEbadf); return DuplicateFrom(fd, static_cast(argument)); } case DuplicateCloseOnExec: if (!Classify(fd, "fcntl")) return Fail(GuestEbadf); return Fail(GuestEopnotsupp); case DuplicateToCommand: return dup2_nid_postfix(fd, static_cast(argument)); case DuplicateToCloseOnExec: if (fd < 0 || !Classify(fd, "fcntl")) return Fail(GuestEbadf); return DuplicateFrom(fd, 0); case GetDescriptorFlags: { if (!Classify(fd, "fcntl")) return Fail(GuestEbadf); std::lock_guard lock(g_mutex); return Materialize(fd).closeOnExec ? CloseOnExec : 0; } case SetDescriptorFlags: if (!Classify(fd, "fcntl")) return Fail(GuestEbadf); SetCloseOnExec(fd, (argument & CloseOnExec) != 0); return 0; case GetStatusFlags: { if (!Classify(fd, "fcntl")) return Fail(GuestEbadf); const int access = CreatedByPipe(fd) ? ReadWrite : AccessMode(fd, "fcntl"); return access | CurrentStatus(fd); } case SetStatusFlags: { const auto kind = Classify(fd, "fcntl"); if (!kind) return Fail(GuestEbadf); return ChangeStatus(fd, *kind, SettableStatusFlags, static_cast(argument), "fcntl"); } case GetOwner: case SetOwner: { const auto kind = Classify(fd, "fcntl"); if (!kind) return Fail(GuestEbadf); if (*kind != Kind::Pipe) Unsupported("fcntl", command == GetOwner ? "F_GETOWN" : "F_SETOWN", *kind, fd); std::lock_guard lock(g_mutex); auto& owner = Materialize(fd).owner; if (!owner) owner = std::make_shared(0); if (command == GetOwner) return *owner; *owner = static_cast(argument); return 0; } case OldGetLock: case OldSetLock: case OldSetLockWait: case GetLock: case SetLock: case SetLockWait: return RecordLock(fd, command, argument); case SetLockRemote: throw std::runtime_error("fcntl: F_SETLK_REMOTE needs a privilege whose state on the console is not known, fd=" + std::to_string(fd)); case ReadAhead: case DarwinReadAhead: return ReadAheadControl(fd, command, argument); default: return Fail(GuestEinval); } } bool IoctlRequestMalformed(std::uint64_t request) { const auto command = static_cast(request); const std::uint32_t length = (command >> 16) & 0x1fff; if ((command & (RequestIn | RequestOut | RequestVoid)) == 0) return true; if ((command & (RequestIn | RequestOut)) && length == 0) return true; return (command & RequestVoid) && length != 0 && length != sizeof(int); } bool CopyIoctlInput(std::uint64_t request, const void* argument, std::vector& input) { const auto command = static_cast(request); const std::uint32_t length = (command >> 16) & 0x1fff; input.clear(); if ((command & RequestIn) == 0 || (command & RequestVoid) != 0 || length == 0) return true; if (argument == nullptr) return false; input.resize(length); return GuestCopyIn_nid_no_patch(input.data(), argument, length); } int Ioctl(int fd, std::uint64_t request, void* argument) { if (IoctlRequestMalformed(request)) return Fail(GuestEnotty); std::vector input; if (!CopyIoctlInput(request, argument, input)) return Fail(GuestEfault); if (File::IsVirtualDirectoryDescriptor(fd)) return Fail(GuestEinval); const auto command = static_cast(request); const auto kind = Classify(fd, "ioctl"); if (!kind) return Fail(GuestEbadf); if (command == CloseOnExecRequest || command == KeepOnExecRequest) { SetCloseOnExec(fd, command == CloseOnExecRequest); return 0; } if (IsKqueueDescriptor_nid_no_patch(fd)) return Fail(GuestEnotty); if (command == NonblockingRequest || command == AsynchronousRequest) { int enable = 0; std::memcpy(&enable, input.data(), sizeof(enable)); const int flag = command == NonblockingRequest ? Nonblocking : Asynchronous; return ChangeStatus(fd, *kind, flag, enable != 0 ? flag : 0, "ioctl"); } if (command == ReadableRequest) { if (*kind == Kind::RandomDevice) return Fail(GuestEnotty); if (*kind != Kind::File && *kind != Kind::Directory && *kind != Kind::Pipe && *kind != Kind::Socket && *kind != Kind::ArchivedFile) Unsupported("ioctl", "FIONREAD", *kind, fd); const auto archived = File::ArchivedFileRemaining(fd); const int available = *kind == Kind::Directory ? DirectoryReadableBytes : archived ? static_cast(std::min(*archived, std::numeric_limits::max())) : Readable(fd, *kind, "ioctl"); if (!GuestCopyOut_nid_no_patch(argument, &available, sizeof(available))) return Fail(GuestEfault); return 0; } if (command == DeviceTypeRequest && *kind == Kind::Device && !IsKsemDescriptor(fd)) { const int type = 0; if (!GuestCopyOut_nid_no_patch(argument, &type, sizeof(type))) return Fail(GuestEfault); return 0; } if (((command >> 8) & 0xff) == TerminalGroup) return Fail(GuestEnotty); if (*kind == Kind::Pipe && fd > 2 && command != SetOwnerRequest && command != GetOwnerRequest) return Fail(GuestEnotty); if (*kind == Kind::RandomDevice && command != DeviceTypeRequest && command != DeviceNameRequest) return Fail(GuestEnotty); char hex[11]; std::snprintf(hex, sizeof(hex), "0x%08X", command); Unsupported("ioctl", std::string("request ") + hex, *kind, fd); } void RememberDescriptor(int fd, int openFlags, bool fixedSizeWrites) { std::lock_guard lock(g_mutex); g_entries[fd] = Entry{.closeOnExec = (openFlags & OpenCloseOnExec) != 0, .status = std::make_shared(openFlags & OpenStatusFlags), .fixedSizeWrites = fixedSizeWrites}; #ifdef _WIN32 GuestFileLockForget_nid_no_patch(fd); #endif } void RememberPipe(int fd, int openFlags) { RememberDescriptor(fd, openFlags); std::lock_guard lock(g_mutex); g_entries[fd].pipe = true; } void ShareDescriptor(int from, int to) { File::ShareArchivedFile(from, to); std::lock_guard lock(g_mutex); auto& source = Materialize(from); if (source.pipe && !source.owner) source.owner = std::make_shared(0); auto copy = source; copy.closeOnExec = false; g_entries[to] = std::move(copy); #if defined(_WIN32) || defined(__APPLE__) File::SharePositionedTransfer(from, to); #endif #ifdef _WIN32 GuestFileLockShare_nid_no_patch(from, to); #endif } void ForgetDescriptor(int fd) { File::ForgetArchivedFile(fd); std::lock_guard lock(g_mutex); g_entries.erase(fd); } bool HasFixedSizeWrites(int fd) { std::lock_guard lock(g_mutex); const auto found = g_entries.find(fd); return found != g_entries.end() && found->second.fixedSizeWrites; } std::uint64_t g_synchronousWrites = 0; void SyncWrites(int fd) { { std::lock_guard lock(g_mutex); const auto found = g_entries.find(fd); if (found == g_entries.end() || !found->second.status || (*found->second.status & Synchronous) == 0) return; } if (Classify(fd, "write") != Kind::File) return; if (const int result = sceKernelFsync(fd); result != 0) throw std::runtime_error("write: syncing an O_FSYNC file failed, fd=" + std::to_string(fd) + ", error=" + std::to_string(result & 0xffff)); std::lock_guard lock(g_mutex); ++g_synchronousWrites; } bool Appends(int fd) { std::lock_guard lock(g_mutex); const auto found = g_entries.find(fd); return found != g_entries.end() && found->second.status && (*found->second.status & Append) != 0; } bool IsCharacterDevice(int fd) { if (fd < 0) return false; #ifdef _WIN32 return DescriptorIsOpen_nid_no_patch(fd) && !GuestSockets::IsOpen(fd) && !File::DirectoryDescriptorPath(fd) && ::GetFileType(WindowsDescriptorHandle_nid_no_patch(fd)) == FILE_TYPE_CHAR; #else struct stat status{}; return ::fstat(fd, &status) == 0 && S_ISCHR(status.st_mode); #endif } bool GrantsAccess(int fd, bool write) { const int access = AccessMode(fd, write ? "pwrite" : "pread"); return access == ReadWrite || access == (write ? WriteOnly : ReadOnly); } bool IsVnode(int fd) { if (fd < 0 || File::IsRandomDevice(fd) || GuestSockets::IsOpen(fd)) return false; if (File::IsArchivedFile(fd)) return true; #ifdef _WIN32 if (!DescriptorIsOpen_nid_no_patch(fd)) return false; return File::DirectoryDescriptorPath(fd) || ::GetFileType(WindowsDescriptorHandle_nid_no_patch(fd)) == FILE_TYPE_DISK; #else struct stat status{}; return ::fstat(fd, &status) == 0 && (S_ISREG(status.st_mode) || S_ISDIR(status.st_mode)); #endif } } extern "C" bool DescriptorHasNoVnode_nid_no_patch(int fd) { return IsKqueueDescriptor_nid_no_patch(fd) || IsKsemDescriptor(fd) || (!File::IsVnode(fd) && !File::IsCharacterDevice(fd) && !File::IsRandomDevice(fd)); } extern "C" std::uint64_t FileSynchronousWritesForTest_nid_no_patch() { std::lock_guard lock(g_mutex); return File::g_synchronousWrites; }