diff --git a/src/windows-emulator/handles.hpp b/src/windows-emulator/handles.hpp index c88f5cc3..2c26db94 100644 --- a/src/windows-emulator/handles.hpp +++ b/src/windows-emulator/handles.hpp @@ -312,11 +312,6 @@ private: constexpr auto KNOWN_DLLS_DIRECTORY = make_pseudo_handle(0x1337, handle_types::directory); constexpr auto KNOWN_DLLS_SYMLINK = make_pseudo_handle(0x1337, handle_types::symlink); constexpr auto SHARED_SECTION = make_pseudo_handle(0x1337, handle_types::section); -constexpr auto CONSOLE_SERVER = make_pseudo_handle(0x1338, handle_types::section); -constexpr auto CM_API = make_pseudo_handle(0x1338, handle_types::file); -constexpr auto KSEC_DD = make_pseudo_handle(0x1339, handle_types::file); -constexpr auto AFD_ENDPOINT = make_pseudo_handle(0x133A, handle_types::file); -constexpr auto CNG = make_pseudo_handle(0x133B, handle_types::file); constexpr auto CONSOLE_HANDLE = make_pseudo_handle(0x1, handle_types::file); constexpr auto STDOUT_HANDLE = make_pseudo_handle(0x2, handle_types::file); diff --git a/src/windows-emulator/io_device.cpp b/src/windows-emulator/io_device.cpp new file mode 100644 index 00000000..17f62640 --- /dev/null +++ b/src/windows-emulator/io_device.cpp @@ -0,0 +1,29 @@ +#include "io_device.hpp" + +namespace +{ + struct dummy_device : stateless_device + { + void read() override + { + } + + void write() override + { + } + }; +} + +std::unique_ptr create_device(const std::wstring_view device) +{ + if (device == L"CNG" + || device == L"KsecDD" + || device == L"DeviceApi\\CMApi" + || device == L"ConDrv\\Server" + || device == L"Afd\\Endpoint") + { + return std::make_unique(); + } + + throw std::runtime_error("Unsupported device: " + std::string(device.begin(), device.end())); +} diff --git a/src/windows-emulator/io_device.hpp b/src/windows-emulator/io_device.hpp index 11c79d15..f58ad970 100644 --- a/src/windows-emulator/io_device.hpp +++ b/src/windows-emulator/io_device.hpp @@ -8,6 +8,12 @@ struct io_device io_device() = default; virtual ~io_device() = default; + io_device(io_device&&) = default; + io_device& operator=(io_device&&) = default; + + io_device(const io_device&) = delete; + io_device& operator=(const io_device&) = delete; + // TODO virtual void read() = 0; virtual void write() = 0; @@ -16,12 +22,18 @@ struct io_device virtual void deserialize(utils::buffer_deserializer& buffer) = 0; }; -// TODO -inline std::unique_ptr create_device(const std::wstring_view device) +struct stateless_device : io_device { - (void)device; - return {}; -} + void serialize(utils::buffer_serializer&) const override + { + } + + void deserialize(utils::buffer_deserializer&) override + { + } +}; + +std::unique_ptr create_device(const std::wstring_view device); class io_device_container : public io_device { diff --git a/src/windows-emulator/syscalls.cpp b/src/windows-emulator/syscalls.cpp index 7b055694..2044c3a4 100644 --- a/src/windows-emulator/syscalls.cpp +++ b/src/windows-emulator/syscalls.cpp @@ -366,6 +366,11 @@ namespace return STATUS_SUCCESS; } + if (value.type == handle_types::device && c.proc.devices.erase(handle)) + { + return STATUS_SUCCESS; + } + if (value.type == handle_types::semaphore && c.proc.semaphores.erase(handle)) { return STATUS_SUCCESS; @@ -2040,41 +2045,18 @@ namespace c.win_emu.logger.print(color::dark_gray, "--> Opening file: %S\n", filename.c_str()); }); - if (filename == L"\\Device\\ConDrv\\Server") + constexpr std::wstring_view device_prefix = L"\\Device\\"; + if (filename.starts_with(device_prefix)) { - file_handle.write(CONSOLE_SERVER.bits); - return STATUS_SUCCESS; - } + auto device_name = filename.substr(device_prefix.size()); + io_device_container container{std::move(device_name)}; - if (filename == L"\\Device\\DeviceApi\\CMApi") - { - file_handle.write(CM_API.bits); - return STATUS_SUCCESS; - } + const auto handle = c.proc.devices.store(std::move(container)); + file_handle.write(handle.bits); - if (filename == L"\\Device\\KsecDD") - { - file_handle.write(KSEC_DD.bits); return STATUS_SUCCESS; } - if (filename == L"\\Device\\CNG") - { - file_handle.write(CNG.bits); - return STATUS_SUCCESS; - } - - if (filename.starts_with(L"\\Device\\Afd\\Endpoint")) - { - file_handle.write(AFD_ENDPOINT.bits); - return STATUS_SUCCESS; - } - - if (filename.starts_with(L"\\Device\\")) - { - return STATUS_NOT_SUPPORTED; - } - handle root_handle{}; root_handle.bits = reinterpret_cast(attributes.RootDirectory); if (root_handle.value.is_pseudo && (filename == L"\\Reference" || filename == L"\\Connect"))