| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WslcSDKPrivate.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains the private WSL Container SDK definitions. |
| 12 | |
| 13 | --*/ |
| 14 | #pragma once |
| 15 | #include <windows.h> |
| 16 | #include "wslcsdk.h" |
| 17 | #include "WSLCCompat.h" |
| 18 | #include "IOCallback.h" |
| 19 | #include <stdint.h> |
| 20 | #include <wil/com.h> // COM helpers |
| 21 | // #include <wil/resource.h> // handle wrappers |
| 22 | // #include <wil/result.h> // error handling |
| 23 | |
| 24 | // SESSION DEFINITIONS |
| 25 | typedef struct WslcSessionOptionsInternal |
| 26 | { |
| 27 | PCWSTR displayName; |
| 28 | PCWSTR storagePath; |
| 29 | |
| 30 | uint32_t cpuCount; |
| 31 | uint32_t memoryMb; |
| 32 | uint32_t timeoutMS; |
| 33 | |
| 34 | WslcVhdRequirements vhdRequirements; |
| 35 | WslcSessionFeatureFlags featureFlags; |
| 36 | } WslcSessionOptionsInternal; |
| 37 | |
| 38 | static_assert(sizeof(WslcSessionOptionsInternal) == WSLC_SESSION_OPTIONS_SIZE, "WSLC_SESSION_OPTIONS_INTERNAL size mismatch"); |
| 39 | |
| 40 | static_assert( |
| 41 | __alignof(WslcSessionOptionsInternal) == WSLC_SESSION_OPTIONS_ALIGNMENT, "WSLC_SESSION_OPTIONS_INTERNAL alignment mismatch"); |
| 42 | |
| 43 | static_assert(std::is_trivial_v<WslcSessionOptionsInternal>, "WSLC_SESSION_OPTIONS_INTERNAL must be trivial"); |
| 44 | |
| 45 | WslcSessionOptionsInternal* GetInternalType(WslcSessionSettings* settings); |
| 46 | |
| 47 | struct WslcContainerProcessIOCallbackOptions : public WslcProcessCallbacks |
| 48 | { |
| 49 | PVOID callbackContext; |
| 50 | }; |
| 51 | |
| 52 | // PROCESS DEFINITIONS |
| 53 | typedef struct WslcContainerProcessOptionsInternal |
| 54 | { |
| 55 | PCSTR const* commandLine; |
| 56 | uint32_t commandLineCount; |
| 57 | PCSTR const* environment; |
| 58 | uint32_t environmentCount; |
| 59 | PCSTR workingDirectory; |
| 60 | WslcContainerProcessIOCallbackOptions ioCallbacks; |
| 61 | } WslcContainerProcessOptionsInternal; |
| 62 | |
| 63 | static_assert( |
| 64 | sizeof(WslcContainerProcessOptionsInternal) == WSLC_CONTAINER_PROCESS_OPTIONS_SIZE, |
| 65 | "WSLC_CONTAINER_PROCESS_OPTIONS_INTERNAL size mismatch"); |
| 66 | static_assert( |
| 67 | __alignof(WslcContainerProcessOptionsInternal) == WSLC_CONTAINER_PROCESS_OPTIONS_ALIGNMENT, |
| 68 | "WSLC_CONTAINER_PROCESS_OPTIONS_INTERNAL alignment mismatch"); |
| 69 | |
| 70 | static_assert(std::is_trivial_v<WslcContainerProcessOptionsInternal>, "WSLC_CONTAINER_PROCESS_OPTIONS_INTERNAL must be trivial"); |
| 71 | |
| 72 | WslcContainerProcessOptionsInternal* GetInternalType(WslcProcessSettings* settings); |
| 73 | |
| 74 | // CONTAINER DEFINITIONS |
| 75 | typedef struct WslcContainerOptionsInternal |
| 76 | { |
| 77 | PCSTR image; // Image name (repository:tag) |
| 78 | PCSTR runtimeName; // Container runtime name (expected to allow DNS resolution between containers) |
| 79 | PCSTR HostName; |
| 80 | PCSTR DomainName; |
| 81 | const WslcContainerPortMapping* ports; |
| 82 | uint32_t portsCount; |
| 83 | const WslcContainerVolume* volumes; |
| 84 | uint32_t volumesCount; |
| 85 | const WslcContainerNamedVolume* namedVolumes; |
| 86 | uint32_t namedVolumesCount; |
| 87 | const WslcContainerProcessOptionsInternal* initProcessOptions; |
| 88 | PCSTR networkMode; |
| 89 | WslcContainerFlags containerFlags; |
| 90 | |
| 91 | } WslcContainerOptionsInternal; |
| 92 | |
| 93 | static_assert( |
| 94 | sizeof(WslcContainerOptionsInternal) == WSLC_CONTAINER_OPTIONS_SIZE, "WSLC_CONTAINER_OPTIONS_INTERNAL size mismatch"); |
| 95 | static_assert( |
| 96 | __alignof(WslcContainerOptionsInternal) == WSLC_CONTAINER_OPTIONS_ALIGNMENT, |
| 97 | "WSLC_CONTAINER_OPTIONS_INTERNAL alignment mismatch"); |
| 98 | |
| 99 | static_assert(std::is_trivial_v<WslcContainerOptionsInternal>, "WSLC_CONTAINER_OPTIONS_INTERNAL must be trivial"); |
| 100 | |
| 101 | WslcContainerOptionsInternal* GetInternalType(WslcContainerSettings* settings); |
| 102 | const WslcContainerOptionsInternal* GetInternalType(const WslcContainerSettings* settings); |
| 103 | |
| 104 | // Use to allocate the actual objects on the heap to keep it alive. |
| 105 | struct WslcSessionImpl |
| 106 | { |
| 107 | wil::com_ptr<IWSLCCompatSession> session; |
| 108 | }; |
| 109 | |
| 110 | WslcSessionImpl* GetInternalType(WslcSession handle); |
| 111 | |
| 112 | // Backs a WslcCrashDumpSubscription handle. Keeps the COM shim alive and holds the service-side |
| 113 | // subscription whose release unregisters the callback. |
| 114 | struct WslcCrashDumpSubscriptionImpl |
| 115 | { |
| 116 | wil::com_ptr<IWSLCCompatCrashDumpCallback> callback; |
| 117 | wil::com_ptr<IUnknown> subscription; |
| 118 | }; |
| 119 | |
| 120 | WslcCrashDumpSubscriptionImpl* GetInternalType(WslcCrashDumpSubscription handle); |
| 121 | |
| 122 | struct WslcContainerImpl |
| 123 | { |
| 124 | wil::com_ptr<IWSLCCompatContainer> container; |
| 125 | WslcContainerProcessIOCallbackOptions ioCallbackOptions{}; |
| 126 | std::atomic<std::shared_ptr<IOCallback>> ioCallbacks; |
| 127 | }; |
| 128 | |
| 129 | WslcContainerImpl* GetInternalType(WslcContainer handle); |
| 130 | |
| 131 | struct WslcProcessImpl |
| 132 | { |
| 133 | wil::com_ptr<IWSLCCompatProcess> process; |
| 134 | std::shared_ptr<IOCallback> ioCallbacks; |
| 135 | }; |
| 136 | |
| 137 | WslcProcessImpl* GetInternalType(WslcProcess handle); |
| 138 | |
| 139 | // Converts to the internal type and throws an error on null input. |
| 140 | template <typename T> |
| 141 | auto CheckAndGetInternalType(T* value) |
| 142 | { |
| 143 | THROW_HR_IF_NULL(E_POINTER, value); |
| 144 | return GetInternalType(value); |
| 145 | } |
| 146 | |
| 147 | // Converts to the internal type and throws an error on null input. |
| 148 | template <typename T> |
| 149 | auto CheckAndGetInternalTypeUniquePointer(T* value) |
| 150 | { |
| 151 | THROW_HR_IF_NULL(E_POINTER, value); |
| 152 | return std::unique_ptr<std::remove_pointer_t<decltype(GetInternalType(value))>>{GetInternalType(value)}; |
| 153 | } |