| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WslCoreInstance.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains WSL Core Instance function declarations. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | |
| 17 | #include "precomp.h" |
| 18 | #include "hcs.hpp" |
| 19 | #include "LxssPort.h" |
| 20 | #include "LxssCreateProcess.h" |
| 21 | #include "LxssConsoleManager.h" |
| 22 | #include "WslPluginApi.h" |
| 23 | #include "DistributionRegistration.h" |
| 24 | |
| 25 | class WslCoreInstance : public LxssRunningInstance |
| 26 | { |
| 27 | public: |
| 28 | class WslCorePort final : public LxssPort |
| 29 | { |
| 30 | public: |
| 31 | WslCorePort(_In_ SOCKET Socket, _In_ const GUID& RuntimeId, _In_ DWORD SocketTimeout); |
| 32 | |
| 33 | WslCorePort(const WslCorePort&) = delete; |
| 34 | WslCorePort& operator=(const WslCorePort&) = delete; |
| 35 | WslCorePort(WslCorePort&& Source) = delete; |
| 36 | WslCorePort& operator=(WslCorePort&& Source) = delete; |
| 37 | |
| 38 | std::shared_ptr<LxssPort> CreateSessionLeader(_In_ HANDLE ClientProcess) override; |
| 39 | void DisconnectConsole(_In_ HANDLE ClientProcess) override; |
| 40 | wsl::shared::SocketChannel& GetChannel(); |
| 41 | wil::cs_leave_scope_exit Lock() override; |
| 42 | void Receive(_Out_writes_bytes_(Length) PVOID Buffer, _In_ ULONG Length, _In_opt_ HANDLE ClientProcess = nullptr, _In_ DWORD Timeout = INFINITE) override; |
| 43 | void Send(_In_reads_bytes_(Length) PVOID Buffer, _In_ ULONG Length) override; |
| 44 | |
| 45 | private: |
| 46 | wil::critical_section m_lock; |
| 47 | wsl::shared::SocketChannel m_channel; |
| 48 | GUID m_runtimeId{}; |
| 49 | DWORD m_socketTimeout{}; |
| 50 | }; |
| 51 | |
| 52 | WslCoreInstance(const WslCoreInstance&) = delete; |
| 53 | WslCoreInstance(WslCoreInstance&&) = delete; |
| 54 | WslCoreInstance& operator=(const WslCoreInstance&) = delete; |
| 55 | WslCoreInstance& operator=(WslCoreInstance&&) = delete; |
| 56 | |
| 57 | WslCoreInstance( |
| 58 | _In_ HANDLE UserToken, |
| 59 | _In_ wil::unique_socket& InitSocket, |
| 60 | _In_ wil::unique_socket& SystemDistroSocket, |
| 61 | _In_ const GUID& InstanceId, |
| 62 | _In_ const GUID& RuntimeId, |
| 63 | _In_ const LXSS_DISTRO_CONFIGURATION& Configuration, |
| 64 | _In_ ULONG DefaultUid, |
| 65 | _In_ ULONG64 ClientLifetimeId, |
| 66 | _In_ const std::function<LX_INIT_DRVFS_MOUNT(HANDLE)>& DrvFsCallback, |
| 67 | _In_ ULONG FeatureFlags, |
| 68 | _In_ DWORD SocketTimeout, |
| 69 | _In_ int IdleTimeout, |
| 70 | _Out_opt_ ULONG* ConnectPort = nullptr, |
| 71 | _In_opt_ HANDLE JobObject = nullptr); |
| 72 | |
| 73 | virtual ~WslCoreInstance(); |
| 74 | |
| 75 | void CreateLxProcess( |
| 76 | _In_ const CreateLxProcessData& CreateProcessData, |
| 77 | _In_ const CreateLxProcessContext& CreateProcessContext, |
| 78 | _In_ const CreateLxProcessConsoleData& ConsoleData, |
| 79 | _In_ SHORT Columns, |
| 80 | _In_ SHORT Rows, |
| 81 | _In_ PLXSS_STD_HANDLES StdHandles, |
| 82 | _Out_ GUID* InstanceId, |
| 83 | _Out_ HANDLE* ProcessHandle, |
| 84 | _Out_ HANDLE* ServerHandle, |
| 85 | _Out_ HANDLE* StandardIn, |
| 86 | _Out_ HANDLE* StandardOut, |
| 87 | _Out_ HANDLE* StandardErr, |
| 88 | _Out_ HANDLE* CommunicationChannel, |
| 89 | _Out_ HANDLE* InteropSocket) override; |
| 90 | |
| 91 | ULONG GetClientId() const override; |
| 92 | |
| 93 | GUID GetDistributionId() const override; |
| 94 | |
| 95 | std::shared_ptr<LxssPort> GetInitPort() override; |
| 96 | |
| 97 | std::shared_ptr<LxssRunningInstance> GetSystemDistro(); |
| 98 | |
| 99 | void UpdateTimezone() override; |
| 100 | |
| 101 | ULONG64 GetLifetimeManagerId() const override; |
| 102 | |
| 103 | void Initialize() override; |
| 104 | |
| 105 | bool RequestStop(_In_ bool Force) override; |
| 106 | |
| 107 | void Stop() override; |
| 108 | |
| 109 | void RegisterPlan9ConnectionTarget(_In_ HANDLE userToken) override; |
| 110 | |
| 111 | const WSLDistributionInformation* DistributionInformation() const noexcept override; |
| 112 | |
| 113 | wil::unique_socket CreateLinuxProcess(_In_ LPCSTR Path, _In_ LPCSTR* Arguments); |
| 114 | |
| 115 | private: |
| 116 | void MountDrvfs(bool Admin) const; |
| 117 | |
| 118 | void ReadOOBEResult(wil::unique_socket&& Socket, wsl::windows::service::DistributionRegistration&& registration); |
| 119 | |
| 120 | std::recursive_mutex m_lock; |
| 121 | wil::unique_handle m_userToken; |
| 122 | bool m_initialized = false; |
| 123 | bool m_adminMountNamespaceCreated = false; |
| 124 | bool m_nonAdminMountNamespaceCreated = false; |
| 125 | ULONG m_featureFlags{}; |
| 126 | GUID m_instanceId{}; |
| 127 | GUID m_runtimeId{}; |
| 128 | LXSS_DISTRO_CONFIGURATION m_configuration{}; |
| 129 | ULONG m_clientId{}; |
| 130 | ULONG m_defaultUid{}; |
| 131 | std::function<LX_INIT_DRVFS_MOUNT(HANDLE)> m_initializeDrvFs; |
| 132 | std::shared_ptr<WslCorePort> m_initChannel; |
| 133 | std::shared_ptr<ConsoleManager> m_consoleManager; |
| 134 | ULONG64 m_ntClientLifetimeId{}; |
| 135 | wsl::windows::common::redirector::ConnectionTargetManager m_redirectorConnectionTargets; |
| 136 | ULONG m_plan9Port{LX_INIT_UTILITY_VM_INVALID_PORT}; |
| 137 | std::shared_ptr<WslCoreInstance> m_systemDistro; |
| 138 | WSLDistributionInformation m_distributionInfo{}; |
| 139 | DWORD m_socketTimeout{}; |
| 140 | HANDLE m_jobObject{}; |
| 141 | std::thread m_oobeThread; |
| 142 | wil::unique_event m_destroyingEvent{wil::EventOptions::ManualReset}; |
| 143 | wil::unique_event m_oobeCompleteEvent; |
| 144 | }; |