| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | main.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | Entry point for wslcsession.exe - the per-user COM server for WSLC sessions. |
| 12 | |
| 13 | This runs under the user's identity and hosts WSLCSessionFactory COM objects. |
| 14 | The SYSTEM service creates sessions via IWSLCSessionFactory::CreateSession, |
| 15 | which returns both the session and a service reference for lifetime tracking. |
| 16 | |
| 17 | --*/ |
| 18 | |
| 19 | #include "precomp.h" |
| 20 | #include "WSLCSessionFactory.h" |
| 21 | |
| 22 | using namespace wsl::windows::common; |
| 23 | using namespace wsl::windows::common::wslutil; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // Event used to signal that the COM server should exit. |
| 28 | wil::unique_event g_exitEvent{wil::EventOptions::ManualReset}; |
| 29 | |
| 30 | class WSLCSessionFactoryClassFactory : public winrt::implements<WSLCSessionFactoryClassFactory, IClassFactory> |
| 31 | { |
| 32 | public: |
| 33 | STDMETHODIMP CreateInstance(_In_ IUnknown* outer, REFIID iid, _COM_Outptr_ void** result) noexcept override |
| 34 | try |
| 35 | { |
| 36 | *result = nullptr; |
| 37 | THROW_HR_IF(CLASS_E_NOAGGREGATION, outer != nullptr); |
| 38 | |
| 39 | auto factory = Microsoft::WRL::Make<wsl::windows::service::wslc::WSLCSessionFactory>(); |
| 40 | factory->SetDestructionCallback([]() { g_exitEvent.SetEvent(); }); |
| 41 | return factory->QueryInterface(iid, result); |
| 42 | } |
| 43 | CATCH_RETURN() |
| 44 | |
| 45 | STDMETHODIMP LockServer(BOOL) noexcept override |
| 46 | { |
| 47 | return S_OK; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | } // namespace |
| 52 | |
| 53 | int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) |
| 54 | try |
| 55 | { |
| 56 | ConfigureCrt(); |
| 57 | |
| 58 | // Enable contextualized errors |
| 59 | wsl::windows::common::EnableContextualizedErrors(true); |
| 60 | |
| 61 | // Initialize telemetry |
| 62 | WslTraceLoggingInitialize(WslcTelemetryProvider, !wsl::shared::OfficialBuild); |
| 63 | auto cleanup = wil::scope_exit([] { WslTraceLoggingUninitialize(); }); |
| 64 | |
| 65 | // Don't kill the process on unknown C++ exceptions |
| 66 | wil::g_fResultFailFastUnknownExceptions = false; |
| 67 | |
| 68 | wsl::windows::common::security::ApplyProcessMitigationPolicies(); |
| 69 | |
| 70 | // Initialize Winsock |
| 71 | WSADATA data; |
| 72 | THROW_IF_WIN32_ERROR(WSAStartup(MAKEWORD(2, 2), &data)); |
| 73 | |
| 74 | WSL_LOG("Per-user session server starting", TraceLoggingLevel(WINEVENT_LEVEL_INFO)); |
| 75 | |
| 76 | // Initialize COM |
| 77 | auto coInit = wil::CoInitializeEx(COINIT_MULTITHREADED); |
| 78 | wsl::windows::common::wslutil::CoInitializeSecurity(); |
| 79 | auto cleanupWinrt = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, []() { winrt::clear_factory_cache(); }); |
| 80 | |
| 81 | // Register the class factory (single-use: one factory per process) |
| 82 | auto factory = winrt::make<WSLCSessionFactoryClassFactory>(); |
| 83 | wil::unique_com_class_object_cookie cookie; |
| 84 | THROW_IF_FAILED(::CoRegisterClassObject( |
| 85 | __uuidof(wsl::windows::service::wslc::WSLCSessionFactory), factory.get(), CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie)); |
| 86 | |
| 87 | WSL_LOG("Per-user session server registered, waiting for activations", TraceLoggingLevel(WINEVENT_LEVEL_INFO)); |
| 88 | |
| 89 | // Wait until all objects have been released |
| 90 | g_exitEvent.wait(); |
| 91 | |
| 92 | WSL_LOG("Per-user session server exiting", TraceLoggingLevel(WINEVENT_LEVEL_INFO)); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | catch (...) |
| 97 | { |
| 98 | LOG_CAUGHT_EXCEPTION(); |
| 99 | return 1; |
| 100 | } |