/src/FreeRDP/channels/serial/client/serial_main.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Serial Port Device Service Virtual Channel |
4 | | * |
5 | | * Copyright 2011 O.S. Systems Software Ltda. |
6 | | * Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br> |
7 | | * Copyright 2014 Hewlett-Packard Development Company, L.P. |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include <errno.h> |
25 | | #include <stdio.h> |
26 | | #include <stdint.h> |
27 | | #include <stdlib.h> |
28 | | #include <string.h> |
29 | | |
30 | | #include <winpr/collections.h> |
31 | | #include <winpr/comm.h> |
32 | | #include <winpr/crt.h> |
33 | | #include <winpr/stream.h> |
34 | | #include <winpr/synch.h> |
35 | | #include <winpr/thread.h> |
36 | | #include <winpr/wlog.h> |
37 | | #include <winpr/assert.h> |
38 | | |
39 | | #include <freerdp/freerdp.h> |
40 | | #include <freerdp/channels/rdpdr.h> |
41 | | #include <freerdp/channels/log.h> |
42 | | #include <freerdp/utils/rdpdr_utils.h> |
43 | | |
44 | 0 | #define TAG CHANNELS_TAG("serial.client") |
45 | | |
46 | 0 | #define MAX_IRP_THREADS 5 |
47 | | |
48 | | typedef struct |
49 | | { |
50 | | DEVICE device; |
51 | | BOOL permissive; |
52 | | SERIAL_DRIVER_ID ServerSerialDriverId; |
53 | | HANDLE hComm; |
54 | | |
55 | | wLog* log; |
56 | | HANDLE MainThread; |
57 | | wMessageQueue* MainIrpQueue; |
58 | | |
59 | | /* one thread per pending IRP and indexed according their CompletionId */ |
60 | | wListDictionary* IrpThreads; |
61 | | CRITICAL_SECTION TerminatingIrpThreadsLock; |
62 | | rdpContext* rdpcontext; |
63 | | } SERIAL_DEVICE; |
64 | | |
65 | | typedef struct |
66 | | { |
67 | | SERIAL_DEVICE* serial; |
68 | | IRP* irp; |
69 | | } IRP_THREAD_DATA; |
70 | | |
71 | | static void close_terminated_irp_thread_handles(SERIAL_DEVICE* serial, BOOL forceClose); |
72 | | static NTSTATUS GetLastErrorToIoStatus(SERIAL_DEVICE* serial) |
73 | 0 | { |
74 | | /* http://msdn.microsoft.com/en-us/library/ff547466%28v=vs.85%29.aspx#generic_status_values_for_serial_device_control_requests |
75 | | */ |
76 | 0 | switch (GetLastError()) |
77 | 0 | { |
78 | 0 | case ERROR_BAD_DEVICE: |
79 | 0 | return STATUS_INVALID_DEVICE_REQUEST; |
80 | | |
81 | 0 | case ERROR_CALL_NOT_IMPLEMENTED: |
82 | 0 | return STATUS_NOT_IMPLEMENTED; |
83 | | |
84 | 0 | case ERROR_CANCELLED: |
85 | 0 | return STATUS_CANCELLED; |
86 | | |
87 | 0 | case ERROR_INSUFFICIENT_BUFFER: |
88 | 0 | return STATUS_BUFFER_TOO_SMALL; /* NB: STATUS_BUFFER_SIZE_TOO_SMALL not defined */ |
89 | | |
90 | 0 | case ERROR_INVALID_DEVICE_OBJECT_PARAMETER: /* eg: SerCx2.sys' _purge() */ |
91 | 0 | return STATUS_INVALID_DEVICE_STATE; |
92 | | |
93 | 0 | case ERROR_INVALID_HANDLE: |
94 | 0 | return STATUS_INVALID_DEVICE_REQUEST; |
95 | | |
96 | 0 | case ERROR_INVALID_PARAMETER: |
97 | 0 | return STATUS_INVALID_PARAMETER; |
98 | | |
99 | 0 | case ERROR_IO_DEVICE: |
100 | 0 | return STATUS_IO_DEVICE_ERROR; |
101 | | |
102 | 0 | case ERROR_IO_PENDING: |
103 | 0 | return STATUS_PENDING; |
104 | | |
105 | 0 | case ERROR_NOT_SUPPORTED: |
106 | 0 | return STATUS_NOT_SUPPORTED; |
107 | | |
108 | 0 | case ERROR_TIMEOUT: |
109 | 0 | return STATUS_TIMEOUT; |
110 | 0 | default: |
111 | 0 | break; |
112 | 0 | } |
113 | | |
114 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "unexpected last-error: 0x%08" PRIX32 "", GetLastError()); |
115 | 0 | return STATUS_UNSUCCESSFUL; |
116 | 0 | } |
117 | | |
118 | | WINPR_ATTR_NODISCARD |
119 | | static UINT serial_process_irp_create(SERIAL_DEVICE* serial, IRP* irp) |
120 | 0 | { |
121 | 0 | DWORD DesiredAccess = 0; |
122 | 0 | DWORD SharedAccess = 0; |
123 | 0 | DWORD CreateDisposition = 0; |
124 | 0 | UINT32 PathLength = 0; |
125 | |
|
126 | 0 | WINPR_ASSERT(serial); |
127 | 0 | WINPR_ASSERT(irp); |
128 | |
|
129 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32)) |
130 | 0 | return ERROR_INVALID_DATA; |
131 | | |
132 | 0 | Stream_Read_UINT32(irp->input, DesiredAccess); /* DesiredAccess (4 bytes) */ |
133 | 0 | Stream_Seek_UINT64(irp->input); /* AllocationSize (8 bytes) */ |
134 | 0 | Stream_Seek_UINT32(irp->input); /* FileAttributes (4 bytes) */ |
135 | 0 | Stream_Read_UINT32(irp->input, SharedAccess); /* SharedAccess (4 bytes) */ |
136 | 0 | Stream_Read_UINT32(irp->input, CreateDisposition); /* CreateDisposition (4 bytes) */ |
137 | 0 | Stream_Seek_UINT32(irp->input); /* CreateOptions (4 bytes) */ |
138 | 0 | Stream_Read_UINT32(irp->input, PathLength); /* PathLength (4 bytes) */ |
139 | |
|
140 | 0 | if (!Stream_SafeSeek(irp->input, PathLength)) /* Path (variable) */ |
141 | 0 | return ERROR_INVALID_DATA; |
142 | | |
143 | 0 | WINPR_ASSERT(PathLength == 0); /* MS-RDPESP 2.2.2.2 */ |
144 | 0 | #ifndef _WIN32 |
145 | | /* Windows 2012 server sends on a first call : |
146 | | * DesiredAccess = 0x00100080: SYNCHRONIZE | FILE_READ_ATTRIBUTES |
147 | | * SharedAccess = 0x00000007: FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ |
148 | | * CreateDisposition = 0x00000001: CREATE_NEW |
149 | | * |
150 | | * then Windows 2012 sends : |
151 | | * DesiredAccess = 0x00120089: SYNCHRONIZE | READ_CONTROL | FILE_READ_ATTRIBUTES | |
152 | | * FILE_READ_EA | FILE_READ_DATA SharedAccess = 0x00000007: FILE_SHARE_DELETE | |
153 | | * FILE_SHARE_WRITE | FILE_SHARE_READ CreateDisposition = 0x00000001: CREATE_NEW |
154 | | * |
155 | | * WINPR_ASSERT(DesiredAccess == (GENERIC_READ | GENERIC_WRITE)); |
156 | | * WINPR_ASSERT(SharedAccess == 0); |
157 | | * WINPR_ASSERT(CreateDisposition == OPEN_EXISTING); |
158 | | * |
159 | | */ |
160 | 0 | WLog_Print(serial->log, WLOG_DEBUG, |
161 | 0 | "DesiredAccess: 0x%" PRIX32 ", SharedAccess: 0x%" PRIX32 |
162 | 0 | ", CreateDisposition: 0x%" PRIX32 "", |
163 | 0 | DesiredAccess, SharedAccess, CreateDisposition); |
164 | | /* FIXME: As of today only the flags below are supported by CommCreateFileA: */ |
165 | 0 | DesiredAccess = GENERIC_READ | GENERIC_WRITE; |
166 | 0 | SharedAccess = 0; |
167 | 0 | CreateDisposition = OPEN_EXISTING; |
168 | 0 | #endif |
169 | 0 | if (serial->hComm) |
170 | 0 | { |
171 | 0 | WLog_Print(serial->log, WLOG_ERROR, "Device %s already open, cancel request", |
172 | 0 | serial->device.name); |
173 | 0 | return ERROR_INVALID_DATA; |
174 | 0 | } |
175 | | |
176 | 0 | serial->hComm = winpr_CreateFile(serial->device.name, DesiredAccess, SharedAccess, |
177 | 0 | nullptr, /* SecurityAttributes */ |
178 | 0 | CreateDisposition, 0, /* FlagsAndAttributes */ |
179 | 0 | nullptr); /* TemplateFile */ |
180 | |
|
181 | 0 | if (!serial->hComm || (serial->hComm == INVALID_HANDLE_VALUE)) |
182 | 0 | { |
183 | 0 | WLog_Print(serial->log, WLOG_WARN, "CreateFile failure: %s last-error: 0x%08" PRIX32 "", |
184 | 0 | serial->device.name, GetLastError()); |
185 | 0 | irp->IoStatus = STATUS_UNSUCCESSFUL; |
186 | 0 | goto error_handle; |
187 | 0 | } |
188 | | |
189 | 0 | _comm_setServerSerialDriver(serial->hComm, serial->ServerSerialDriverId); |
190 | 0 | _comm_set_permissive(serial->hComm, serial->permissive); |
191 | | /* NOTE: binary mode/raw mode required for the redirection. On |
192 | | * Linux, CommCreateFileA forces this setting. |
193 | | */ |
194 | | /* ZeroMemory(&dcb, sizeof(DCB)); */ |
195 | | /* dcb.DCBlength = sizeof(DCB); */ |
196 | | /* GetCommState(serial->hComm, &dcb); */ |
197 | | /* dcb.fBinary = TRUE; */ |
198 | | /* SetCommState(serial->hComm, &dcb); */ |
199 | 0 | WINPR_ASSERT(irp->FileId == 0); |
200 | 0 | irp->FileId = irp->devman->id_sequence++; /* FIXME: why not ((WINPR_COMM*)hComm)->fd? */ |
201 | 0 | irp->IoStatus = STATUS_SUCCESS; |
202 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "%s (DeviceId: %" PRIu32 ", FileId: %" PRIu32 ") created.", |
203 | 0 | serial->device.name, irp->device->id, irp->FileId); |
204 | |
|
205 | 0 | { |
206 | 0 | DWORD BytesReturned = 0; |
207 | 0 | if (!CommDeviceIoControl(serial->hComm, IOCTL_SERIAL_RESET_DEVICE, nullptr, 0, nullptr, 0, |
208 | 0 | &BytesReturned, nullptr)) |
209 | 0 | goto error_handle; |
210 | 0 | } |
211 | | |
212 | 0 | error_handle: |
213 | 0 | Stream_Write_UINT32(irp->output, irp->FileId); /* FileId (4 bytes) */ |
214 | 0 | Stream_Write_UINT8(irp->output, 0); /* Information (1 byte) */ |
215 | 0 | return CHANNEL_RC_OK; |
216 | 0 | } |
217 | | |
218 | | static UINT serial_process_irp_close(SERIAL_DEVICE* serial, IRP* irp) |
219 | 0 | { |
220 | 0 | WINPR_ASSERT(serial); |
221 | 0 | WINPR_ASSERT(irp); |
222 | |
|
223 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32)) |
224 | 0 | return ERROR_INVALID_DATA; |
225 | | |
226 | 0 | Stream_Seek(irp->input, 32); /* Padding (32 bytes) */ |
227 | |
|
228 | 0 | if (!serial->hComm) |
229 | 0 | { |
230 | 0 | WLog_Print(serial->log, WLOG_ERROR, "serial device %s is not open, aborting close", |
231 | 0 | serial->device.name); |
232 | 0 | return ERROR_INVALID_DATA; |
233 | 0 | } |
234 | | |
235 | 0 | close_terminated_irp_thread_handles(serial, TRUE); |
236 | |
|
237 | 0 | if (!CloseHandle(serial->hComm)) |
238 | 0 | { |
239 | 0 | WLog_Print(serial->log, WLOG_WARN, "CloseHandle failure: %s (%" PRIu32 ") closed.", |
240 | 0 | serial->device.name, irp->device->id); |
241 | 0 | irp->IoStatus = STATUS_UNSUCCESSFUL; |
242 | 0 | goto error_handle; |
243 | 0 | } |
244 | | |
245 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "%s (DeviceId: %" PRIu32 ", FileId: %" PRIu32 ") closed.", |
246 | 0 | serial->device.name, irp->device->id, irp->FileId); |
247 | 0 | irp->IoStatus = STATUS_SUCCESS; |
248 | 0 | error_handle: |
249 | 0 | serial->hComm = nullptr; |
250 | 0 | Stream_Zero(irp->output, 5); /* Padding (5 bytes) */ |
251 | 0 | return CHANNEL_RC_OK; |
252 | 0 | } |
253 | | |
254 | | /** |
255 | | * Function description |
256 | | * |
257 | | * @return 0 on success, otherwise a Win32 error code |
258 | | */ |
259 | | WINPR_ATTR_NODISCARD |
260 | | static UINT serial_process_irp_read(SERIAL_DEVICE* serial, IRP* irp) |
261 | 0 | { |
262 | 0 | UINT32 Length = 0; |
263 | 0 | UINT64 Offset = 0; |
264 | 0 | DWORD nbRead = 0; |
265 | |
|
266 | 0 | WINPR_ASSERT(serial); |
267 | 0 | WINPR_ASSERT(irp); |
268 | |
|
269 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32)) |
270 | 0 | return ERROR_INVALID_DATA; |
271 | | |
272 | 0 | Stream_Read_UINT32(irp->input, Length); /* Length (4 bytes) */ |
273 | 0 | Stream_Read_UINT64(irp->input, Offset); /* Offset (8 bytes) */ |
274 | 0 | (void)Offset; /* [MS-RDPESP] 3.2.5.1.4 Processing a Server Read Request Message |
275 | | * ignored */ |
276 | 0 | Stream_Seek(irp->input, 20); /* Padding (20 bytes) */ |
277 | |
|
278 | 0 | if (!serial->hComm) |
279 | 0 | { |
280 | 0 | WLog_Print(serial->log, WLOG_ERROR, "serial device %s is not open, aborting read", |
281 | 0 | serial->device.name); |
282 | 0 | return ERROR_INVALID_DATA; |
283 | 0 | } |
284 | | |
285 | 0 | BYTE* buffer = (BYTE*)calloc(Length, sizeof(BYTE)); |
286 | |
|
287 | 0 | if (buffer == nullptr) |
288 | 0 | { |
289 | 0 | irp->IoStatus = STATUS_NO_MEMORY; |
290 | 0 | goto error_handle; |
291 | 0 | } |
292 | | |
293 | | /* MS-RDPESP 3.2.5.1.4: If the Offset field is not set to 0, the value MUST be ignored |
294 | | * WINPR_ASSERT(Offset == 0); |
295 | | */ |
296 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "reading %" PRIu32 " bytes from %s", Length, |
297 | 0 | serial->device.name); |
298 | | |
299 | | /* FIXME: CommReadFile to be replaced by ReadFile */ |
300 | 0 | if (CommReadFile(serial->hComm, buffer, Length, &nbRead, nullptr)) |
301 | 0 | { |
302 | 0 | irp->IoStatus = STATUS_SUCCESS; |
303 | 0 | } |
304 | 0 | else |
305 | 0 | { |
306 | 0 | WLog_Print(serial->log, WLOG_DEBUG, |
307 | 0 | "read failure to %s, nbRead=%" PRIu32 ", last-error: 0x%08" PRIX32 "", |
308 | 0 | serial->device.name, nbRead, GetLastError()); |
309 | 0 | irp->IoStatus = GetLastErrorToIoStatus(serial); |
310 | 0 | } |
311 | |
|
312 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "%" PRIu32 " bytes read from %s", nbRead, |
313 | 0 | serial->device.name); |
314 | 0 | error_handle: |
315 | 0 | Stream_Write_UINT32(irp->output, nbRead); /* Length (4 bytes) */ |
316 | |
|
317 | 0 | if (nbRead > 0) |
318 | 0 | { |
319 | 0 | if (!Stream_EnsureRemainingCapacity(irp->output, nbRead)) |
320 | 0 | { |
321 | 0 | WLog_Print(serial->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!"); |
322 | 0 | free(buffer); |
323 | 0 | return CHANNEL_RC_NO_MEMORY; |
324 | 0 | } |
325 | | |
326 | 0 | Stream_Write(irp->output, buffer, nbRead); /* ReadData */ |
327 | 0 | } |
328 | | |
329 | 0 | free(buffer); |
330 | 0 | return CHANNEL_RC_OK; |
331 | 0 | } |
332 | | |
333 | | WINPR_ATTR_NODISCARD |
334 | | static UINT serial_process_irp_write(SERIAL_DEVICE* serial, IRP* irp) |
335 | 0 | { |
336 | 0 | UINT32 Length = 0; |
337 | 0 | UINT64 Offset = 0; |
338 | 0 | DWORD nbWritten = 0; |
339 | |
|
340 | 0 | WINPR_ASSERT(serial); |
341 | 0 | WINPR_ASSERT(irp); |
342 | |
|
343 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32)) |
344 | 0 | return ERROR_INVALID_DATA; |
345 | | |
346 | 0 | Stream_Read_UINT32(irp->input, Length); /* Length (4 bytes) */ |
347 | 0 | Stream_Read_UINT64(irp->input, Offset); /* Offset (8 bytes) */ |
348 | 0 | (void)Offset; /* [MS-RDPESP] 3.2.5.1.4 Processing a Server Read Request Message |
349 | | * ignored */ |
350 | 0 | if (!Stream_SafeSeek(irp->input, 20)) /* Padding (20 bytes) */ |
351 | 0 | return ERROR_INVALID_DATA; |
352 | | |
353 | | /* MS-RDPESP 3.2.5.1.5: The Offset field is ignored |
354 | | * WINPR_ASSERT(Offset == 0); |
355 | | * |
356 | | * Using a serial printer, noticed though this field could be |
357 | | * set. |
358 | | */ |
359 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "writing %" PRIu32 " bytes to %s", Length, |
360 | 0 | serial->device.name); |
361 | |
|
362 | 0 | const void* ptr = Stream_ConstPointer(irp->input); |
363 | 0 | if (!Stream_SafeSeek(irp->input, Length)) |
364 | 0 | return ERROR_INVALID_DATA; |
365 | | |
366 | 0 | if (!serial->hComm) |
367 | 0 | { |
368 | 0 | WLog_Print(serial->log, WLOG_ERROR, "serial device %s is not open, aborting write", |
369 | 0 | serial->device.name); |
370 | 0 | return ERROR_INVALID_DATA; |
371 | 0 | } |
372 | | |
373 | | /* FIXME: CommWriteFile to be replaced by WriteFile */ |
374 | 0 | if (CommWriteFile(serial->hComm, ptr, Length, &nbWritten, nullptr)) |
375 | 0 | { |
376 | 0 | irp->IoStatus = STATUS_SUCCESS; |
377 | 0 | } |
378 | 0 | else |
379 | 0 | { |
380 | 0 | WLog_Print(serial->log, WLOG_DEBUG, |
381 | 0 | "write failure to %s, nbWritten=%" PRIu32 ", last-error: 0x%08" PRIX32 "", |
382 | 0 | serial->device.name, nbWritten, GetLastError()); |
383 | 0 | irp->IoStatus = GetLastErrorToIoStatus(serial); |
384 | 0 | } |
385 | |
|
386 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "%" PRIu32 " bytes written to %s", nbWritten, |
387 | 0 | serial->device.name); |
388 | 0 | Stream_Write_UINT32(irp->output, nbWritten); /* Length (4 bytes) */ |
389 | 0 | Stream_Write_UINT8(irp->output, 0); /* Padding (1 byte) */ |
390 | 0 | return CHANNEL_RC_OK; |
391 | 0 | } |
392 | | |
393 | | /** |
394 | | * Function description |
395 | | * |
396 | | * @return 0 on success, otherwise a Win32 error code |
397 | | */ |
398 | | WINPR_ATTR_NODISCARD |
399 | | static UINT serial_process_irp_device_control(SERIAL_DEVICE* serial, IRP* irp) |
400 | 0 | { |
401 | 0 | DWORD BytesReturned = 0; |
402 | |
|
403 | 0 | WINPR_ASSERT(serial); |
404 | 0 | WINPR_ASSERT(irp); |
405 | |
|
406 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32)) |
407 | 0 | return ERROR_INVALID_DATA; |
408 | | |
409 | 0 | const UINT32 OutputBufferLength = |
410 | 0 | Stream_Get_UINT32(irp->input); /* OutputBufferLength (4 bytes) */ |
411 | 0 | const UINT32 InputBufferLength = |
412 | 0 | Stream_Get_UINT32(irp->input); /* InputBufferLength (4 bytes) */ |
413 | 0 | const UINT32 IoControlCode = Stream_Get_UINT32(irp->input); /* IoControlCode (4 bytes) */ |
414 | 0 | Stream_Seek(irp->input, 20); /* Padding (20 bytes) */ |
415 | |
|
416 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, InputBufferLength)) |
417 | 0 | return ERROR_INVALID_DATA; |
418 | | |
419 | 0 | if (!serial->hComm) |
420 | 0 | { |
421 | 0 | WLog_Print(serial->log, WLOG_ERROR, |
422 | 0 | "serial device %s is not open, aborting device IO control", serial->device.name); |
423 | 0 | return ERROR_INVALID_DATA; |
424 | 0 | } |
425 | | |
426 | 0 | const BYTE* InputBuffer = Stream_PointerAs(irp->input, BYTE); |
427 | 0 | if (!Stream_SafeSeek(irp->input, InputBufferLength)) |
428 | 0 | return ERROR_INVALID_DATA; |
429 | | |
430 | 0 | WLog_Print(serial->log, WLOG_DEBUG, |
431 | 0 | "CommDeviceIoControl: CompletionId=%" PRIu32 ", IoControlCode=[0x%" PRIX32 "] %s", |
432 | 0 | irp->CompletionId, IoControlCode, _comm_serial_ioctl_name(IoControlCode)); |
433 | |
|
434 | 0 | BYTE* OutputBuffer = nullptr; |
435 | 0 | if (OutputBufferLength > 0) |
436 | 0 | { |
437 | 0 | OutputBuffer = (BYTE*)calloc(OutputBufferLength, sizeof(BYTE)); |
438 | 0 | if (!OutputBuffer) |
439 | 0 | { |
440 | 0 | irp->IoStatus = STATUS_NO_MEMORY; |
441 | 0 | goto error_handle; |
442 | 0 | } |
443 | 0 | } |
444 | | |
445 | | /* FIXME: CommDeviceIoControl to be replaced by DeviceIoControl() */ |
446 | 0 | if (CommDeviceIoControl(serial->hComm, IoControlCode, InputBuffer, InputBufferLength, |
447 | 0 | OutputBuffer, OutputBufferLength, &BytesReturned, nullptr)) |
448 | 0 | { |
449 | | /* WLog_Print(serial->log, WLOG_DEBUG, "CommDeviceIoControl: CompletionId=%"PRIu32", |
450 | | * IoControlCode=[0x%"PRIX32"] %s done", irp->CompletionId, IoControlCode, |
451 | | * _comm_serial_ioctl_name(IoControlCode)); */ |
452 | 0 | irp->IoStatus = STATUS_SUCCESS; |
453 | 0 | } |
454 | 0 | else |
455 | 0 | { |
456 | 0 | WLog_Print(serial->log, WLOG_DEBUG, |
457 | 0 | "CommDeviceIoControl failure: IoControlCode=[0x%" PRIX32 |
458 | 0 | "] %s, last-error: 0x%08" PRIX32 "", |
459 | 0 | IoControlCode, _comm_serial_ioctl_name(IoControlCode), GetLastError()); |
460 | 0 | irp->IoStatus = GetLastErrorToIoStatus(serial); |
461 | 0 | BytesReturned = 0; |
462 | 0 | } |
463 | |
|
464 | 0 | error_handle: |
465 | 0 | Stream_Write_UINT32(irp->output, BytesReturned); /* OutputBufferLength (4 bytes) */ |
466 | |
|
467 | 0 | if (BytesReturned > 0) |
468 | 0 | { |
469 | 0 | if (!Stream_EnsureRemainingCapacity(irp->output, BytesReturned)) |
470 | 0 | { |
471 | 0 | WLog_Print(serial->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!"); |
472 | 0 | free(OutputBuffer); |
473 | 0 | return CHANNEL_RC_NO_MEMORY; |
474 | 0 | } |
475 | | |
476 | 0 | Stream_Write(irp->output, OutputBuffer, BytesReturned); /* OutputBuffer */ |
477 | 0 | } |
478 | | |
479 | | /* FIXME: Why at least Windows 2008R2 gets lost with this |
480 | | * extra byte and likely on a IOCTL_SERIAL_SET_BAUD_RATE? The |
481 | | * extra byte is well required according MS-RDPEFS |
482 | | * 2.2.1.5.5 */ |
483 | | /* else */ |
484 | | /* { */ |
485 | | /* Stream_Write_UINT8(irp->output, 0); /\* Padding (1 byte) *\/ */ |
486 | | /* } */ |
487 | 0 | free(OutputBuffer); |
488 | 0 | return CHANNEL_RC_OK; |
489 | 0 | } |
490 | | |
491 | | /** |
492 | | * Function description |
493 | | * |
494 | | * @return 0 on success, otherwise a Win32 error code |
495 | | */ |
496 | | static UINT serial_process_irp(SERIAL_DEVICE* serial, IRP* irp) |
497 | 0 | { |
498 | 0 | UINT error = CHANNEL_RC_OK; |
499 | |
|
500 | 0 | WINPR_ASSERT(serial); |
501 | 0 | WINPR_ASSERT(irp); |
502 | |
|
503 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "IRP MajorFunction: %s, MinorFunction: 0x%08" PRIX32 "\n", |
504 | 0 | rdpdr_irp_string(irp->MajorFunction), irp->MinorFunction); |
505 | |
|
506 | 0 | switch (irp->MajorFunction) |
507 | 0 | { |
508 | 0 | case IRP_MJ_CREATE: |
509 | 0 | error = serial_process_irp_create(serial, irp); |
510 | 0 | break; |
511 | | |
512 | 0 | case IRP_MJ_CLOSE: |
513 | 0 | error = serial_process_irp_close(serial, irp); |
514 | 0 | break; |
515 | | |
516 | 0 | case IRP_MJ_READ: |
517 | 0 | error = serial_process_irp_read(serial, irp); |
518 | 0 | break; |
519 | | |
520 | 0 | case IRP_MJ_WRITE: |
521 | 0 | error = serial_process_irp_write(serial, irp); |
522 | 0 | break; |
523 | | |
524 | 0 | case IRP_MJ_DEVICE_CONTROL: |
525 | 0 | error = serial_process_irp_device_control(serial, irp); |
526 | 0 | break; |
527 | | |
528 | 0 | default: |
529 | 0 | irp->IoStatus = STATUS_NOT_SUPPORTED; |
530 | 0 | break; |
531 | 0 | } |
532 | | |
533 | 0 | DWORD level = WLOG_TRACE; |
534 | 0 | if (error) |
535 | 0 | level = WLOG_WARN; |
536 | |
|
537 | 0 | WLog_Print(serial->log, level, |
538 | 0 | "[%s|0x%08" PRIx32 "] completed with %s [0x%08" PRIx32 "] (IoStatus %s [0x%08" PRIx32 |
539 | 0 | "])", |
540 | 0 | rdpdr_irp_string(irp->MajorFunction), irp->MajorFunction, WTSErrorToString(error), |
541 | 0 | error, NtStatus2Tag(irp->IoStatus), WINPR_CXX_COMPAT_CAST(UINT32, irp->IoStatus)); |
542 | |
|
543 | 0 | return error; |
544 | 0 | } |
545 | | |
546 | | static DWORD WINAPI irp_thread_func(LPVOID arg) |
547 | 0 | { |
548 | 0 | IRP_THREAD_DATA* data = (IRP_THREAD_DATA*)arg; |
549 | |
|
550 | 0 | WINPR_ASSERT(data); |
551 | 0 | WINPR_ASSERT(data->serial); |
552 | 0 | WINPR_ASSERT(data->irp); |
553 | | |
554 | | /* blocks until the end of the request */ |
555 | 0 | UINT error = serial_process_irp(data->serial, data->irp); |
556 | 0 | if (error) |
557 | 0 | { |
558 | 0 | WLog_Print(data->serial->log, WLOG_ERROR, |
559 | 0 | "serial_process_irp failed with error %" PRIu32 "", error); |
560 | 0 | data->irp->Discard(data->irp); |
561 | 0 | goto error_out; |
562 | 0 | } |
563 | | |
564 | 0 | EnterCriticalSection(&data->serial->TerminatingIrpThreadsLock); |
565 | 0 | WINPR_ASSERT(data->irp->Complete); |
566 | 0 | error = data->irp->Complete(data->irp); |
567 | 0 | LeaveCriticalSection(&data->serial->TerminatingIrpThreadsLock); |
568 | 0 | error_out: |
569 | |
|
570 | 0 | if (error && data->serial->rdpcontext) |
571 | 0 | setChannelError(data->serial->rdpcontext, error, "irp_thread_func reported an error"); |
572 | | |
573 | | /* NB: At this point, the server might already being reusing |
574 | | * the CompletionId whereas the thread is not yet |
575 | | * terminated */ |
576 | 0 | free(data); |
577 | 0 | ExitThread(error); |
578 | 0 | return error; |
579 | 0 | } |
580 | | |
581 | | static void close_unterminated_irp_thread(wListDictionary* list, wLog* log, ULONG_PTR id) |
582 | 0 | { |
583 | 0 | WINPR_ASSERT(list); |
584 | 0 | HANDLE self = _GetCurrentThread(); |
585 | 0 | HANDLE cirpThread = ListDictionary_GetItemValue(list, (void*)id); |
586 | 0 | if (self == cirpThread) |
587 | 0 | WLog_Print(log, WLOG_DEBUG, "Skipping termination of own IRP thread"); |
588 | 0 | else |
589 | 0 | ListDictionary_Remove(list, (void*)id); |
590 | 0 | } |
591 | | |
592 | | static void close_terminated_irp_thread(wListDictionary* list, wLog* log, ULONG_PTR id) |
593 | 0 | { |
594 | 0 | WINPR_ASSERT(list); |
595 | |
|
596 | 0 | HANDLE cirpThread = ListDictionary_GetItemValue(list, (void*)id); |
597 | | /* FIXME: not quite sure a zero timeout is a good thing to check whether a thread is |
598 | | * still alive or not */ |
599 | 0 | const DWORD waitResult = WaitForSingleObject(cirpThread, 0); |
600 | |
|
601 | 0 | if (waitResult == WAIT_OBJECT_0) |
602 | 0 | ListDictionary_Remove(list, (void*)id); |
603 | 0 | else if (waitResult != WAIT_TIMEOUT) |
604 | 0 | { |
605 | | /* unexpected thread state */ |
606 | 0 | WLog_Print(log, WLOG_WARN, "WaitForSingleObject, got an unexpected result=0x%" PRIX32 "\n", |
607 | 0 | waitResult); |
608 | 0 | } |
609 | 0 | } |
610 | | |
611 | | void close_terminated_irp_thread_handles(SERIAL_DEVICE* serial, BOOL forceClose) |
612 | 0 | { |
613 | 0 | WINPR_ASSERT(serial); |
614 | |
|
615 | 0 | EnterCriticalSection(&serial->TerminatingIrpThreadsLock); |
616 | |
|
617 | 0 | ListDictionary_Lock(serial->IrpThreads); |
618 | 0 | ULONG_PTR* ids = nullptr; |
619 | 0 | const size_t nbIds = ListDictionary_GetKeys(serial->IrpThreads, &ids); |
620 | |
|
621 | 0 | for (size_t i = 0; i < nbIds; i++) |
622 | 0 | { |
623 | 0 | ULONG_PTR id = ids[i]; |
624 | 0 | if (forceClose) |
625 | 0 | close_unterminated_irp_thread(serial->IrpThreads, serial->log, id); |
626 | 0 | else |
627 | 0 | close_terminated_irp_thread(serial->IrpThreads, serial->log, id); |
628 | 0 | } |
629 | |
|
630 | 0 | free(ids); |
631 | 0 | ListDictionary_Unlock(serial->IrpThreads); |
632 | |
|
633 | 0 | LeaveCriticalSection(&serial->TerminatingIrpThreadsLock); |
634 | 0 | } |
635 | | |
636 | | static void create_irp_thread(SERIAL_DEVICE* serial, IRP* irp) |
637 | 0 | { |
638 | 0 | IRP_THREAD_DATA* data = nullptr; |
639 | 0 | HANDLE irpThread = nullptr; |
640 | 0 | HANDLE previousIrpThread = nullptr; |
641 | 0 | uintptr_t key = 0; |
642 | |
|
643 | 0 | WINPR_ASSERT(serial); |
644 | 0 | WINPR_ASSERT(irp); |
645 | |
|
646 | 0 | close_terminated_irp_thread_handles(serial, FALSE); |
647 | | |
648 | | /* NB: At this point and thanks to the synchronization we're |
649 | | * sure that the incoming IRP uses well a recycled |
650 | | * CompletionId or the server sent again an IRP already posted |
651 | | * which didn't get yet a response (this later server behavior |
652 | | * at least observed with IOCTL_SERIAL_WAIT_ON_MASK and |
653 | | * mstsc.exe). |
654 | | * |
655 | | * FIXME: behavior documented somewhere? behavior not yet |
656 | | * observed with FreeRDP). |
657 | | */ |
658 | 0 | key = irp->CompletionId + 1ull; |
659 | |
|
660 | 0 | ListDictionary_Lock(serial->IrpThreads); |
661 | 0 | previousIrpThread = ListDictionary_GetItemValue(serial->IrpThreads, (void*)key); |
662 | 0 | ListDictionary_Unlock(serial->IrpThreads); |
663 | |
|
664 | 0 | if (previousIrpThread) |
665 | 0 | { |
666 | | /* Thread still alived <=> Request still pending */ |
667 | 0 | WLog_Print(serial->log, WLOG_DEBUG, |
668 | 0 | "IRP recall: IRP with the CompletionId=%" PRIu32 " not yet completed!", |
669 | 0 | irp->CompletionId); |
670 | 0 | WINPR_ASSERT(FALSE); /* unimplemented */ |
671 | | /* TODO: WINPR_ASSERTs that previousIrpThread handles well |
672 | | * the same request by checking more details. Need an |
673 | | * access to the IRP object used by previousIrpThread |
674 | | */ |
675 | | /* TODO: taking over the pending IRP or sending a kind |
676 | | * of wake up signal to accelerate the pending |
677 | | * request |
678 | | * |
679 | | * To be considered: |
680 | | * if (IoControlCode == IOCTL_SERIAL_WAIT_ON_MASK) { |
681 | | * pComm->PendingEvents |= SERIAL_EV_FREERDP_*; |
682 | | * } |
683 | | */ |
684 | 0 | irp->Discard(irp); |
685 | 0 | return; |
686 | 0 | } |
687 | | |
688 | | /* error_handle to be used ... */ |
689 | 0 | data = (IRP_THREAD_DATA*)calloc(1, sizeof(IRP_THREAD_DATA)); |
690 | |
|
691 | 0 | if (data == nullptr) |
692 | 0 | { |
693 | 0 | WLog_Print(serial->log, WLOG_WARN, "Could not allocate a new IRP_THREAD_DATA."); |
694 | 0 | goto error_handle; |
695 | 0 | } |
696 | | |
697 | 0 | data->serial = serial; |
698 | 0 | data->irp = irp; |
699 | | /* data freed by irp_thread_func */ |
700 | 0 | irpThread = CreateThread(nullptr, 0, irp_thread_func, (void*)data, CREATE_SUSPENDED, nullptr); |
701 | |
|
702 | 0 | if (irpThread == INVALID_HANDLE_VALUE) |
703 | 0 | { |
704 | 0 | WLog_Print(serial->log, WLOG_WARN, "Could not allocate a new IRP thread."); |
705 | 0 | goto error_handle; |
706 | 0 | } |
707 | | |
708 | 0 | key = irp->CompletionId + 1ull; |
709 | |
|
710 | 0 | ListDictionary_Lock(serial->IrpThreads); |
711 | 0 | if (ListDictionary_Count(serial->IrpThreads) >= MAX_IRP_THREADS) |
712 | 0 | { |
713 | 0 | WLog_Print(serial->log, WLOG_WARN, |
714 | 0 | "Number of IRP threads threshold reached: %" PRIuz ", keep on anyway", |
715 | 0 | ListDictionary_Count(serial->IrpThreads)); |
716 | 0 | WINPR_ASSERT(FALSE); /* unimplemented */ |
717 | | /* TODO: MAX_IRP_THREADS has been thought to avoid a |
718 | | * flooding of pending requests. Use |
719 | | * WaitForMultipleObjects() when available in winpr |
720 | | * for threads. |
721 | | */ |
722 | 0 | } |
723 | |
|
724 | 0 | { |
725 | 0 | const BOOL added = ListDictionary_Add(serial->IrpThreads, (void*)key, irpThread); |
726 | 0 | ListDictionary_Unlock(serial->IrpThreads); |
727 | |
|
728 | 0 | if (!added) |
729 | 0 | { |
730 | 0 | WLog_Print(serial->log, WLOG_ERROR, "ListDictionary_Add failed!"); |
731 | 0 | goto error_handle; |
732 | 0 | } |
733 | 0 | } |
734 | | |
735 | 0 | ResumeThread(irpThread); |
736 | |
|
737 | 0 | return; |
738 | 0 | error_handle: |
739 | 0 | if (irpThread) |
740 | 0 | (void)CloseHandle(irpThread); |
741 | 0 | irp->IoStatus = STATUS_NO_MEMORY; |
742 | 0 | WINPR_ASSERT(irp->Complete); |
743 | 0 | const UINT rc = irp->Complete(irp); |
744 | 0 | if (rc != CHANNEL_RC_OK) |
745 | 0 | WLog_Print(serial->log, WLOG_WARN, "irp->Complete failed with %" PRIu32, rc); |
746 | 0 | free(data); |
747 | 0 | } |
748 | | |
749 | | static DWORD WINAPI serial_thread_func(LPVOID arg) |
750 | 0 | { |
751 | 0 | IRP* irp = nullptr; |
752 | 0 | wMessage message = WINPR_C_ARRAY_INIT; |
753 | 0 | SERIAL_DEVICE* serial = (SERIAL_DEVICE*)arg; |
754 | 0 | UINT error = CHANNEL_RC_OK; |
755 | |
|
756 | 0 | WINPR_ASSERT(serial); |
757 | |
|
758 | 0 | while (1) |
759 | 0 | { |
760 | 0 | if (!MessageQueue_Wait(serial->MainIrpQueue)) |
761 | 0 | { |
762 | 0 | WLog_Print(serial->log, WLOG_ERROR, "MessageQueue_Wait failed!"); |
763 | 0 | error = ERROR_INTERNAL_ERROR; |
764 | 0 | break; |
765 | 0 | } |
766 | | |
767 | 0 | if (!MessageQueue_Peek(serial->MainIrpQueue, &message, TRUE)) |
768 | 0 | { |
769 | 0 | WLog_Print(serial->log, WLOG_ERROR, "MessageQueue_Peek failed!"); |
770 | 0 | error = ERROR_INTERNAL_ERROR; |
771 | 0 | break; |
772 | 0 | } |
773 | | |
774 | 0 | if (message.id == WMQ_QUIT) |
775 | 0 | break; |
776 | | |
777 | 0 | irp = (IRP*)message.wParam; |
778 | |
|
779 | 0 | if (irp) |
780 | 0 | create_irp_thread(serial, irp); |
781 | 0 | } |
782 | |
|
783 | 0 | ListDictionary_Lock(serial->IrpThreads); |
784 | 0 | ListDictionary_Clear(serial->IrpThreads); |
785 | 0 | ListDictionary_Unlock(serial->IrpThreads); |
786 | |
|
787 | 0 | if (error && serial->rdpcontext) |
788 | 0 | setChannelError(serial->rdpcontext, error, "serial_thread_func reported an error"); |
789 | |
|
790 | 0 | ExitThread(error); |
791 | 0 | return error; |
792 | 0 | } |
793 | | |
794 | | /** |
795 | | * Function description |
796 | | * |
797 | | * @return 0 on success, otherwise a Win32 error code |
798 | | */ |
799 | | static UINT serial_irp_request(DEVICE* device, IRP* irp) |
800 | 0 | { |
801 | 0 | SERIAL_DEVICE* serial = (SERIAL_DEVICE*)device; |
802 | 0 | WINPR_ASSERT(irp != nullptr); |
803 | 0 | WINPR_ASSERT(serial); |
804 | | |
805 | | /* NB: ENABLE_ASYNCIO is set, (MS-RDPEFS 2.2.2.7.2) this |
806 | | * allows the server to send multiple simultaneous read or |
807 | | * write requests. |
808 | | */ |
809 | |
|
810 | 0 | if (!MessageQueue_Post(serial->MainIrpQueue, nullptr, 0, (void*)irp, nullptr)) |
811 | 0 | { |
812 | 0 | WLog_Print(serial->log, WLOG_ERROR, "MessageQueue_Post failed!"); |
813 | 0 | irp->Discard(irp); |
814 | 0 | return ERROR_INTERNAL_ERROR; |
815 | 0 | } |
816 | | |
817 | 0 | return CHANNEL_RC_OK; |
818 | 0 | } |
819 | | |
820 | | /** |
821 | | * Function description |
822 | | * |
823 | | * @return 0 on success, otherwise a Win32 error code |
824 | | */ |
825 | | static UINT serial_free(DEVICE* device) |
826 | 0 | { |
827 | 0 | UINT error = 0; |
828 | 0 | SERIAL_DEVICE* serial = (SERIAL_DEVICE*)device; |
829 | 0 | if (!serial) |
830 | 0 | return CHANNEL_RC_OK; |
831 | | |
832 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "freeing"); |
833 | 0 | if (serial->MainIrpQueue) |
834 | 0 | MessageQueue_PostQuit(serial->MainIrpQueue, 0); |
835 | |
|
836 | 0 | if (serial->MainThread) |
837 | 0 | { |
838 | 0 | if (WaitForSingleObject(serial->MainThread, INFINITE) == WAIT_FAILED) |
839 | 0 | { |
840 | 0 | error = GetLastError(); |
841 | 0 | WLog_Print(serial->log, WLOG_ERROR, |
842 | 0 | "WaitForSingleObject failed with error %" PRIu32 "!", error); |
843 | 0 | } |
844 | 0 | (void)CloseHandle(serial->MainThread); |
845 | 0 | } |
846 | |
|
847 | 0 | if (serial->hComm) |
848 | 0 | (void)CloseHandle(serial->hComm); |
849 | | |
850 | | /* Clean up resources */ |
851 | 0 | Stream_Free(serial->device.data, TRUE); |
852 | 0 | MessageQueue_Free(serial->MainIrpQueue); |
853 | 0 | ListDictionary_Free(serial->IrpThreads); |
854 | 0 | DeleteCriticalSection(&serial->TerminatingIrpThreadsLock); |
855 | 0 | free(serial); |
856 | 0 | return CHANNEL_RC_OK; |
857 | 0 | } |
858 | | |
859 | | static void serial_message_free(void* obj) |
860 | 0 | { |
861 | 0 | wMessage* msg = obj; |
862 | 0 | if (!msg) |
863 | 0 | return; |
864 | 0 | if (msg->id != 0) |
865 | 0 | return; |
866 | | |
867 | 0 | IRP* irp = (IRP*)msg->wParam; |
868 | 0 | if (!irp) |
869 | 0 | return; |
870 | 0 | WINPR_ASSERT(irp->Discard); |
871 | 0 | irp->Discard(irp); |
872 | 0 | } |
873 | | |
874 | | static void irp_thread_close(void* arg) |
875 | 0 | { |
876 | 0 | HANDLE hdl = arg; |
877 | 0 | if (hdl) |
878 | 0 | { |
879 | 0 | HANDLE thz = _GetCurrentThread(); |
880 | 0 | if (thz == hdl) |
881 | 0 | WLog_WARN(TAG, "closing self, ignoring..."); |
882 | 0 | else |
883 | 0 | { |
884 | 0 | (void)TerminateThread(hdl, 0); |
885 | 0 | (void)WaitForSingleObject(hdl, INFINITE); |
886 | 0 | (void)CloseHandle(hdl); |
887 | 0 | } |
888 | 0 | } |
889 | 0 | } |
890 | | |
891 | | /** |
892 | | * Function description |
893 | | * |
894 | | * @return 0 on success, otherwise a Win32 error code |
895 | | */ |
896 | | FREERDP_ENTRY_POINT( |
897 | | UINT VCAPITYPE serial_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)) |
898 | 0 | { |
899 | 0 | size_t len = 0; |
900 | 0 | SERIAL_DEVICE* serial = nullptr; |
901 | 0 | UINT error = CHANNEL_RC_OK; |
902 | |
|
903 | 0 | WINPR_ASSERT(pEntryPoints); |
904 | |
|
905 | 0 | RDPDR_SERIAL* device = (RDPDR_SERIAL*)pEntryPoints->device; |
906 | 0 | WINPR_ASSERT(device); |
907 | |
|
908 | 0 | wLog* log = WLog_Get(TAG); |
909 | 0 | const char* name = device->device.Name; |
910 | 0 | const char* path = device->Path; |
911 | 0 | const char* driver = device->Driver; |
912 | |
|
913 | 0 | if (!name || (name[0] == '*')) |
914 | 0 | { |
915 | | /* TODO: implement auto detection of serial ports */ |
916 | 0 | WLog_Print(log, WLOG_WARN, |
917 | 0 | "Serial port autodetection not implemented, nothing will be redirected!"); |
918 | 0 | return CHANNEL_RC_OK; |
919 | 0 | } |
920 | | |
921 | 0 | if ((name && name[0]) && (path && path[0])) |
922 | 0 | { |
923 | 0 | WLog_Print(log, WLOG_DEBUG, "Defining %s as %s", name, path); |
924 | |
|
925 | 0 | if (!DefineCommDevice(name /* eg: COM1 */, path /* eg: /dev/ttyS0 */)) |
926 | 0 | { |
927 | 0 | DWORD status = GetLastError(); |
928 | 0 | WLog_Print(log, WLOG_ERROR, "DefineCommDevice failed with %08" PRIx32, status); |
929 | 0 | return ERROR_INTERNAL_ERROR; |
930 | 0 | } |
931 | | |
932 | 0 | serial = (SERIAL_DEVICE*)calloc(1, sizeof(SERIAL_DEVICE)); |
933 | |
|
934 | 0 | if (!serial) |
935 | 0 | { |
936 | 0 | WLog_Print(log, WLOG_ERROR, "calloc failed!"); |
937 | 0 | return CHANNEL_RC_NO_MEMORY; |
938 | 0 | } |
939 | | |
940 | 0 | serial->log = log; |
941 | 0 | serial->device.type = RDPDR_DTYP_SERIAL; |
942 | 0 | serial->device.name = name; |
943 | 0 | serial->device.IRPRequest = serial_irp_request; |
944 | 0 | serial->device.Free = serial_free; |
945 | 0 | serial->rdpcontext = pEntryPoints->rdpcontext; |
946 | 0 | len = strlen(name); |
947 | 0 | serial->device.data = Stream_New(nullptr, len + 1); |
948 | |
|
949 | 0 | if (!serial->device.data) |
950 | 0 | { |
951 | 0 | WLog_Print(serial->log, WLOG_ERROR, "calloc failed!"); |
952 | 0 | error = CHANNEL_RC_NO_MEMORY; |
953 | 0 | goto error_out; |
954 | 0 | } |
955 | | |
956 | 0 | for (size_t i = 0; i <= len; i++) |
957 | 0 | Stream_Write_INT8(serial->device.data, name[i] < 0 ? '_' : name[i]); |
958 | |
|
959 | 0 | if (driver != nullptr) |
960 | 0 | { |
961 | 0 | if (_stricmp(driver, "Serial") == 0) |
962 | 0 | serial->ServerSerialDriverId = SerialDriverSerialSys; |
963 | 0 | else if (_stricmp(driver, "SerCx") == 0) |
964 | 0 | serial->ServerSerialDriverId = SerialDriverSerCxSys; |
965 | 0 | else if (_stricmp(driver, "SerCx2") == 0) |
966 | 0 | serial->ServerSerialDriverId = SerialDriverSerCx2Sys; |
967 | 0 | else |
968 | 0 | { |
969 | 0 | WLog_Print(serial->log, WLOG_WARN, "Unknown server's serial driver: %s.", driver); |
970 | 0 | WLog_Print(serial->log, WLOG_WARN, |
971 | 0 | "Valid options are: 'Serial' (default), 'SerCx' and 'SerCx2'"); |
972 | 0 | goto error_out; |
973 | 0 | } |
974 | 0 | } |
975 | 0 | else |
976 | 0 | { |
977 | | /* default driver */ |
978 | 0 | serial->ServerSerialDriverId = SerialDriverSerialSys; |
979 | 0 | } |
980 | | |
981 | 0 | if (device->Permissive != nullptr) |
982 | 0 | { |
983 | 0 | if (_stricmp(device->Permissive, "permissive") == 0) |
984 | 0 | { |
985 | 0 | serial->permissive = TRUE; |
986 | 0 | } |
987 | 0 | else |
988 | 0 | { |
989 | 0 | WLog_Print(serial->log, WLOG_WARN, "Unknown flag: %s", device->Permissive); |
990 | 0 | goto error_out; |
991 | 0 | } |
992 | 0 | } |
993 | | |
994 | 0 | WLog_Print(serial->log, WLOG_DEBUG, "Server's serial driver: %s (id: %u)", driver, |
995 | 0 | serial->ServerSerialDriverId); |
996 | |
|
997 | 0 | serial->MainIrpQueue = MessageQueue_New(nullptr); |
998 | |
|
999 | 0 | if (!serial->MainIrpQueue) |
1000 | 0 | { |
1001 | 0 | WLog_Print(serial->log, WLOG_ERROR, "MessageQueue_New failed!"); |
1002 | 0 | error = CHANNEL_RC_NO_MEMORY; |
1003 | 0 | goto error_out; |
1004 | 0 | } |
1005 | | |
1006 | 0 | { |
1007 | 0 | wObject* obj = MessageQueue_Object(serial->MainIrpQueue); |
1008 | 0 | WINPR_ASSERT(obj); |
1009 | 0 | obj->fnObjectFree = serial_message_free; |
1010 | 0 | } |
1011 | | |
1012 | | /* IrpThreads content only modified by create_irp_thread() */ |
1013 | 0 | serial->IrpThreads = ListDictionary_New(FALSE); |
1014 | |
|
1015 | 0 | if (!serial->IrpThreads) |
1016 | 0 | { |
1017 | 0 | WLog_Print(serial->log, WLOG_ERROR, "ListDictionary_New failed!"); |
1018 | 0 | error = CHANNEL_RC_NO_MEMORY; |
1019 | 0 | goto error_out; |
1020 | 0 | } |
1021 | | |
1022 | 0 | { |
1023 | 0 | wObject* obj = ListDictionary_ValueObject(serial->IrpThreads); |
1024 | 0 | WINPR_ASSERT(obj); |
1025 | 0 | obj->fnObjectFree = irp_thread_close; |
1026 | 0 | } |
1027 | |
|
1028 | 0 | InitializeCriticalSection(&serial->TerminatingIrpThreadsLock); |
1029 | |
|
1030 | 0 | error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &serial->device); |
1031 | 0 | if (error != CHANNEL_RC_OK) |
1032 | 0 | { |
1033 | 0 | WLog_Print(serial->log, WLOG_ERROR, |
1034 | 0 | "EntryPoints->RegisterDevice failed with error %" PRIu32 "!", error); |
1035 | 0 | goto error_out; |
1036 | 0 | } |
1037 | | |
1038 | 0 | serial->MainThread = CreateThread(nullptr, 0, serial_thread_func, serial, 0, nullptr); |
1039 | 0 | if (!serial->MainThread) |
1040 | 0 | { |
1041 | 0 | WLog_Print(serial->log, WLOG_ERROR, "CreateThread failed!"); |
1042 | 0 | error = ERROR_INTERNAL_ERROR; |
1043 | 0 | goto error_out; |
1044 | 0 | } |
1045 | 0 | } |
1046 | | |
1047 | 0 | return error; |
1048 | 0 | error_out: |
1049 | 0 | if (serial) |
1050 | 0 | serial_free(&serial->device); |
1051 | 0 | return error; |
1052 | 0 | } |