| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | lxssclient.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains the LXSS client library implementation. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include "precomp.h" |
| 16 | #include "lxssbusclient.h" |
| 17 | |
| 18 | HANDLE LxssRootHandle = NULL; |
| 19 | |
| 20 | NTSTATUS |
| 21 | LxssClientInitialize(VOID) |
| 22 | |
| 23 | /*++ |
| 24 | |
| 25 | Routine Description: |
| 26 | |
| 27 | Initializes a new LXSS client by connecting to the LXSS driver. |
| 28 | |
| 29 | Arguments: |
| 30 | |
| 31 | None. |
| 32 | |
| 33 | Return Value: |
| 34 | |
| 35 | NTSTATUS. |
| 36 | |
| 37 | --*/ |
| 38 | |
| 39 | { |
| 40 | UNICODE_STRING ControlDevicePath; |
| 41 | IO_STATUS_BLOCK IoStatus; |
| 42 | OBJECT_ATTRIBUTES ObjectAttributes; |
| 43 | NTSTATUS Status; |
| 44 | |
| 45 | WI_ASSERT(LxssRootHandle == NULL); |
| 46 | |
| 47 | // |
| 48 | // Create the device string and connect to the device. |
| 49 | // |
| 50 | |
| 51 | RtlInitUnicodeString(&ControlDevicePath, LX_CONTROL_DEVICE_ROOT); |
| 52 | InitializeObjectAttributes(&ObjectAttributes, &ControlDevicePath, OBJ_CASE_INSENSITIVE, NULL, NULL); |
| 53 | |
| 54 | Status = NtOpenFile(&LxssRootHandle, FILE_WRITE_DATA, &ObjectAttributes, &IoStatus, 0, 0); |
| 55 | |
| 56 | WI_ASSERT(Status != STATUS_PENDING); |
| 57 | |
| 58 | if (!NT_SUCCESS(Status)) |
| 59 | { |
| 60 | if (LxssRootHandle != NULL) |
| 61 | { |
| 62 | WI_VERIFY(NT_SUCCESS(NtClose(LxssRootHandle))); |
| 63 | } |
| 64 | |
| 65 | LxssRootHandle = NULL; |
| 66 | } |
| 67 | |
| 68 | return Status; |
| 69 | } |
| 70 | |
| 71 | NTSTATUS |
| 72 | LxssClientInstanceCreate(_In_ PLX_KINSTANCECREATESTART Parameters, _Out_ PHANDLE InstanceHandle) |
| 73 | |
| 74 | /*++ |
| 75 | |
| 76 | Routine Description: |
| 77 | |
| 78 | This routine sends an instance create request to the LXSS driver. |
| 79 | |
| 80 | Arguments: |
| 81 | |
| 82 | Parameters - Supplies a pointer to the input parameters. |
| 83 | |
| 84 | InstanceHandle - Supplies a buffer to receive a handle to the instance. |
| 85 | |
| 86 | Return Value: |
| 87 | |
| 88 | NTSTATUS. |
| 89 | |
| 90 | --*/ |
| 91 | |
| 92 | { |
| 93 | // |
| 94 | // Create the instance. |
| 95 | // |
| 96 | |
| 97 | return LxBusClientpIoctl( |
| 98 | LxssRootHandle, LXBUS_ROOT_IOCTL_CREATE_INSTANCE, Parameters, sizeof(*Parameters), InstanceHandle, sizeof(*InstanceHandle)); |
| 99 | } |
| 100 | |
| 101 | NTSTATUS |
| 102 | LxssClientInstanceDestroy(_In_ HANDLE InstanceHandle) |
| 103 | |
| 104 | /*++ |
| 105 | |
| 106 | Routine Description: |
| 107 | |
| 108 | This routine sends an instance destroy request to the LXSS driver. |
| 109 | |
| 110 | Arguments: |
| 111 | |
| 112 | InstanceHandle - Supplies a handle to an instance. |
| 113 | |
| 114 | Return Value: |
| 115 | |
| 116 | NTSTATUS. |
| 117 | |
| 118 | --*/ |
| 119 | |
| 120 | { |
| 121 | LX_KINSTANCESETSTATE InstanceSetState; |
| 122 | NTSTATUS Status; |
| 123 | |
| 124 | // |
| 125 | // Destroy the instance. |
| 126 | // |
| 127 | |
| 128 | RtlZeroMemory(&InstanceSetState, sizeof(InstanceSetState)); |
| 129 | InstanceSetState.Type = LxKInstanceSetStateTypeDestroy; |
| 130 | Status = LxBusClientpIoctl(InstanceHandle, LXBUS_IOCTL_SET_INSTANCE_STATE, &InstanceSetState, sizeof(InstanceSetState), NULL, 0); |
| 131 | |
| 132 | return Status; |
| 133 | } |
| 134 | |
| 135 | NTSTATUS |
| 136 | LxssClientInstanceGetExitStatus(_In_ HANDLE InstanceHandle, _Out_ PLONG ExitStatus) |
| 137 | |
| 138 | /*++ |
| 139 | |
| 140 | Routine Description: |
| 141 | |
| 142 | This routine sends a instance get exit status request to the LXSS driver. |
| 143 | |
| 144 | Arguments: |
| 145 | |
| 146 | InstanceHandle - Supplies a handle to an instance. |
| 147 | |
| 148 | ExitStatus - Supplies a buffer to receive the instance exit status. |
| 149 | |
| 150 | Return Value: |
| 151 | |
| 152 | NTSTATUS. |
| 153 | |
| 154 | --*/ |
| 155 | |
| 156 | { |
| 157 | NTSTATUS ExitStatusLocal; |
| 158 | NTSTATUS Status; |
| 159 | |
| 160 | Status = LxBusClientpIoctl(InstanceHandle, LXBUS_INSTANCE_IOCTL_GET_INIT_EXIT_STATUS, NULL, 0, &ExitStatusLocal, sizeof(ExitStatusLocal)); |
| 161 | |
| 162 | if (!NT_SUCCESS(Status)) |
| 163 | { |
| 164 | goto ClientInstanceGetExitStatusEnd; |
| 165 | } |
| 166 | |
| 167 | *ExitStatus = ExitStatusLocal; |
| 168 | |
| 169 | ClientInstanceGetExitStatusEnd: |
| 170 | return Status; |
| 171 | } |
| 172 | |
| 173 | NTSTATUS |
| 174 | LxssClientInstanceStart(_In_ HANDLE InstanceHandle, _In_ HANDLE ParentProcessHandle) |
| 175 | |
| 176 | /*++ |
| 177 | |
| 178 | Routine Description: |
| 179 | |
| 180 | This routine sends an instance start request to the LXSS driver. |
| 181 | |
| 182 | Arguments: |
| 183 | |
| 184 | InstanceHandle - Supplies a handle to an instance. |
| 185 | |
| 186 | ParentProcessHandle - Supplies a handle to the parent process for the |
| 187 | instance. |
| 188 | |
| 189 | Return Value: |
| 190 | |
| 191 | NTSTATUS. |
| 192 | |
| 193 | --*/ |
| 194 | |
| 195 | { |
| 196 | LX_KINSTANCESETSTATE InstanceSetState; |
| 197 | NTSTATUS Status; |
| 198 | |
| 199 | // |
| 200 | // Start the instance. |
| 201 | // |
| 202 | |
| 203 | RtlZeroMemory(&InstanceSetState, sizeof(InstanceSetState)); |
| 204 | InstanceSetState.Type = LxKInstanceSetStateTypeStart; |
| 205 | InstanceSetState.TypeData.StartParentProcessHandle = HandleToUlong(ParentProcessHandle); |
| 206 | |
| 207 | Status = LxBusClientpIoctl(InstanceHandle, LXBUS_IOCTL_SET_INSTANCE_STATE, &InstanceSetState, sizeof(InstanceSetState), NULL, 0); |
| 208 | |
| 209 | return Status; |
| 210 | } |
| 211 | |
| 212 | NTSTATUS |
| 213 | LxssClientInstanceStop(_In_ HANDLE InstanceHandle) |
| 214 | |
| 215 | /*++ |
| 216 | |
| 217 | Routine Description: |
| 218 | |
| 219 | This routine sends an instance stop request to the LXSS driver. |
| 220 | |
| 221 | Arguments: |
| 222 | |
| 223 | InstanceHandle - Supplies a handle to an instance. |
| 224 | |
| 225 | Return Value: |
| 226 | |
| 227 | NTSTATUS. |
| 228 | |
| 229 | --*/ |
| 230 | |
| 231 | { |
| 232 | LX_KINSTANCESETSTATE InstanceSetState; |
| 233 | NTSTATUS Status; |
| 234 | |
| 235 | // |
| 236 | // Stop the instance. |
| 237 | // |
| 238 | |
| 239 | RtlZeroMemory(&InstanceSetState, sizeof(InstanceSetState)); |
| 240 | InstanceSetState.Type = LxKInstanceSetStateTypeStop; |
| 241 | Status = LxBusClientpIoctl(InstanceHandle, LXBUS_IOCTL_SET_INSTANCE_STATE, &InstanceSetState, sizeof(InstanceSetState), NULL, 0); |
| 242 | |
| 243 | return Status; |
| 244 | } |
| 245 | |
| 246 | VOID LxssClientUninitialize(VOID) |
| 247 | |
| 248 | /*++ |
| 249 | |
| 250 | Routine Description: |
| 251 | |
| 252 | Uninitializes a new LXSS client by disconnecting from LXSS driver. |
| 253 | |
| 254 | Arguments: |
| 255 | |
| 256 | None. |
| 257 | |
| 258 | Return Value: |
| 259 | |
| 260 | None. |
| 261 | |
| 262 | --*/ |
| 263 | |
| 264 | { |
| 265 | if (LxssRootHandle != NULL) |
| 266 | { |
| 267 | WI_VERIFY(NT_SUCCESS(NtClose(LxssRootHandle))); |
| 268 | LxssRootHandle = NULL; |
| 269 | } |
| 270 | } |