Extract device creation

This commit is contained in:
momo5502
2024-11-06 16:31:30 +01:00
parent 4c0c1bf0c6
commit 8cf451fd1f
4 changed files with 57 additions and 39 deletions

View File

@@ -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);

View File

@@ -0,0 +1,29 @@
#include "io_device.hpp"
namespace
{
struct dummy_device : stateless_device
{
void read() override
{
}
void write() override
{
}
};
}
std::unique_ptr<io_device> 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<dummy_device>();
}
throw std::runtime_error("Unsupported device: " + std::string(device.begin(), device.end()));
}

View File

@@ -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<io_device> 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<io_device> create_device(const std::wstring_view device);
class io_device_container : public io_device
{

View File

@@ -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<uint64_t>(attributes.RootDirectory);
if (root_handle.value.is_pseudo && (filename == L"\\Reference" || filename == L"\\Connect"))