| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WSLCProcessControl.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | Contains the different WSLCProcessControl definitions for process control logic. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | #include "wslc.h" |
| 17 | #include "DockerHTTPClient.h" |
| 18 | #include "DockerEventTracker.h" |
| 19 | |
| 20 | namespace wsl::windows::service::wslc { |
| 21 | |
| 22 | class WSLCVirtualMachine; |
| 23 | class WSLCContainerImpl; |
| 24 | |
| 25 | class WSLCProcessControl |
| 26 | { |
| 27 | public: |
| 28 | WSLCProcessControl() = default; |
| 29 | virtual ~WSLCProcessControl() = default; |
| 30 | |
| 31 | virtual void Signal(int Signal) = 0; |
| 32 | virtual void ResizeTty(ULONG Rows, ULONG Columns) = 0; |
| 33 | virtual int GetPid() const = 0; |
| 34 | std::pair<WSLCProcessState, int> GetState() const; |
| 35 | const wil::unique_event& GetExitEvent() const; |
| 36 | |
| 37 | protected: |
| 38 | wil::unique_event m_exitEvent{wil::EventOptions::ManualReset}; |
| 39 | std::optional<int> m_exitedCode{}; |
| 40 | }; |
| 41 | |
| 42 | class DockerContainerProcessControl : public WSLCProcessControl |
| 43 | { |
| 44 | public: |
| 45 | DockerContainerProcessControl(WSLCContainerImpl& Container, DockerHTTPClient& DockerClient); |
| 46 | ~DockerContainerProcessControl(); |
| 47 | void Signal(int Signal) override; |
| 48 | void ResizeTty(ULONG Rows, ULONG Columns) override; |
| 49 | int GetPid() const override; |
| 50 | void OnContainerReleased() noexcept; |
| 51 | |
| 52 | // Records the exit code observed from Docker. Idempotent: first call wins. |
| 53 | void SetExitCode(int ExitCode); |
| 54 | void SignalExit(); |
| 55 | |
| 56 | private: |
| 57 | std::mutex m_lock; |
| 58 | DockerHTTPClient& m_client; |
| 59 | WSLCContainerImpl* m_container{}; |
| 60 | }; |
| 61 | |
| 62 | class DockerExecProcessControl : public WSLCProcessControl |
| 63 | { |
| 64 | public: |
| 65 | DockerExecProcessControl(WSLCContainerImpl& Container, const std::string& Id, DockerHTTPClient& DockerClient, DockerEventTracker& EventTracker); |
| 66 | void Signal(int Signal) override; |
| 67 | void ResizeTty(ULONG Rows, ULONG Columns) override; |
| 68 | int GetPid() const override; |
| 69 | void OnContainerReleased() noexcept; |
| 70 | |
| 71 | void SetPid(int Pid); |
| 72 | void SetExitCode(int ExitCode); |
| 73 | |
| 74 | private: |
| 75 | void OnEvent(ContainerEvent Event, std::optional<int> ExitCode, std::int64_t eventTime); |
| 76 | |
| 77 | mutable std::mutex m_lock; |
| 78 | std::string m_id; |
| 79 | std::optional<int> m_pid{}; |
| 80 | DockerHTTPClient& m_client; |
| 81 | WSLCContainerImpl* m_container{}; |
| 82 | DockerEventTracker::EventTrackingReference m_eventTrackingReference; |
| 83 | }; |
| 84 | |
| 85 | class VMProcessControl : public WSLCProcessControl |
| 86 | { |
| 87 | public: |
| 88 | VMProcessControl(WSLCVirtualMachine& VirtualMachine, int Pid, wil::unique_socket&& TtyControl); |
| 89 | ~VMProcessControl(); |
| 90 | |
| 91 | void Signal(int Signal) override; |
| 92 | void ResizeTty(ULONG Rows, ULONG Columns) override; |
| 93 | int GetPid() const override; |
| 94 | |
| 95 | void OnExited(int Code); |
| 96 | void OnVmTerminated(); |
| 97 | |
| 98 | private: |
| 99 | std::mutex m_lock; |
| 100 | int m_pid{}; |
| 101 | wsl::shared::SocketChannel m_ttyControlChannel; |
| 102 | WSLCVirtualMachine* m_vm{}; |
| 103 | }; |
| 104 | |
| 105 | } // namespace wsl::windows::service::wslc |