| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | ConsoleManager.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains function declarations around console management. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | #include "precomp.h" |
| 17 | #include "LxssCreateProcess.h" |
| 18 | #include "Lifetime.h" |
| 19 | |
| 20 | class ConsoleManager : public std::enable_shared_from_this<ConsoleManager> |
| 21 | { |
| 22 | public: |
| 23 | static std::shared_ptr<ConsoleManager> CreateConsoleManager(_In_ const std::shared_ptr<LxssPort>& Port); |
| 24 | |
| 25 | ~ConsoleManager(); |
| 26 | |
| 27 | std::shared_ptr<LxssPort> GetSessionLeader(_In_ const CreateLxProcessConsoleData& ConsoleData, _In_ bool Elevated, _Out_opt_ bool* Created = nullptr); |
| 28 | |
| 29 | private: |
| 30 | ConsoleManager() = delete; |
| 31 | ConsoleManager(_In_ const std::shared_ptr<LxssPort>& Port); |
| 32 | |
| 33 | std::shared_ptr<LxssPort> _RegisterProcess(_In_ const CreateLxProcessConsoleData& ConsoleData, _In_ bool Elevated, _Out_ ULONG* ConsoleId); |
| 34 | |
| 35 | void _SetPort(_In_ ULONG ConsoleId, _In_ bool Elevated, _In_ std::shared_ptr<LxssPort>& Port); |
| 36 | |
| 37 | void _UnregisterProcess(_In_ const wil::unique_handle& ConsoleHandle, _In_ bool Elevated); |
| 38 | |
| 39 | struct SessionLeaderKey |
| 40 | { |
| 41 | ULONG ConsoleId; |
| 42 | bool Elevated; |
| 43 | bool operator<(const SessionLeaderKey& other) const |
| 44 | { |
| 45 | return std::tie(ConsoleId, Elevated) < std::tie(other.ConsoleId, other.Elevated); |
| 46 | } |
| 47 | |
| 48 | bool operator==(const SessionLeaderKey& other) const |
| 49 | { |
| 50 | return ConsoleId == other.ConsoleId && Elevated == other.Elevated; |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | struct SessionLeaderMapping |
| 55 | { |
| 56 | wil::unique_handle console; |
| 57 | wil::unique_handle firstClient; |
| 58 | std::shared_ptr<LxssPort> port; |
| 59 | ULONG64 clientCallbackId; |
| 60 | }; |
| 61 | |
| 62 | static bool s_OnProcessTerminated(_In_ ConsoleManager* Self, _In_ ULONG ConsoleId, _In_ bool Elevated); |
| 63 | |
| 64 | void _OnProcessDisconnect(_In_ ULONG ConsoleId, _In_ bool Elevated); |
| 65 | |
| 66 | static void _GetConsoleInfo(_In_ const wil::unique_handle& ConsoleHandle, _Out_ ULONG& ConsoleId, _Out_ wil::unique_handle& ConhostHandle); |
| 67 | |
| 68 | static ULONG s_GetConhostServerId(_In_ HANDLE ConsoleHandle); |
| 69 | |
| 70 | std::mutex m_mappingListLock; |
| 71 | |
| 72 | _Guarded_by_(m_mappingListLock) std::map<SessionLeaderKey, SessionLeaderMapping> m_mappings; |
| 73 | std::shared_ptr<LxssPort> m_initPort; |
| 74 | LifetimeManager m_lifetimeManager; |
| 75 | }; |