| 1 | # wslservice.exe |
| 2 | |
| 3 | WslService is a session 0 service, running as SYSTEM. Its job is to manage WSL sessions, communicate with the WSL2 virtual machine and configure WSL distributions. |
| 4 | |
| 5 | ## COM Interface |
| 6 | |
| 7 | Clients can connect to WslService via its COM interface, ILxssUserSession. Its definition can be found in `src/windows/service/inc/wslservice.idl`. |
| 8 | |
| 9 | When a COM client calls [CoCreateInstance()](https://learn.microsoft.com/windows/win32/api/combaseapi/nf-combaseapi-cocreateinstance) on this interface, the service receives the requests via `LxssUserSessionFactory` (see `src/windows/service/exe/LxssUserSessionFactory.cpp`) and returns an instance of `LxssUserSession` (see `src/windows/service/exe/LxssUserSession.cpp`) per Windows user (calling CoCreateInstance() multiple times from the same Windows user accounts returns the same instance). |
| 10 | |
| 11 | The client can then use its `ILxssUserSession` instance to call methods into the service, such as: |
| 12 | |
| 13 | - `CreateInstance()`: Launch a WSL distribution |
| 14 | - `CreateLxProcess()`: Launch a process inside a distribution |
| 15 | - `RegisterDistribution()`: Register a new WSL distribution |
| 16 | - `Shutdown()`: Terminate all WSL distributions |
| 17 | |
| 18 | ## WSL2 Virtual machine |
| 19 | |
| 20 | WslService manages the WSL2 Virtual Machine. The virtual machine management logic can be found in `src/windows/service/exe/WslCoreVm.cpp`. |
| 21 | |
| 22 | Once booted, WslService maintains an [hvsocket](https://learn.microsoft.com/virtualization/hyper-v-on-windows/user-guide/make-integration-service) with the Virtual Machine which it uses to send various commands to Linux processes (see [mini_init](mini_init.md) for more details). |
| 23 | |
| 24 | ## WSL2 Distributions |
| 25 | |
| 26 | Once the virtual machine is running, WSL distributions can be started by calling `WslCoreVm::CreateInstance`. Each running distribution is represented by a `WslCoreInstance` (see `src/windows/service/exe/WslCoreInstance.cpp`). |
| 27 | |
| 28 | Each `WslCoreInstance` maintains an hvsocket connection to [init](init.md) which allows WslService to perform various tasks such as: |
| 29 | |
| 30 | - Launching processes inside the distribution |
| 31 | - Be notified when the distribution exits |
| 32 | - Mount drvfs shares (/mnt/*) |
| 33 | - Stop the distribution |