2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Base API Server DLL
4 * FILE: subsystems/win/basesrv/vdm.c
5 * PURPOSE: Virtual DOS Machines (VDM) Support
6 * PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
7 * Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
10 /* INCLUDES *******************************************************************/
18 /* GLOBALS ********************************************************************/
20 BOOLEAN FirstVDM
= TRUE
;
21 LIST_ENTRY VDMConsoleListHead
;
22 RTL_CRITICAL_SECTION DosCriticalSection
;
23 RTL_CRITICAL_SECTION WowCriticalSection
;
25 /* FUNCTIONS ******************************************************************/
27 NTSTATUS NTAPI
BaseSrvGetConsoleRecord(HANDLE ConsoleHandle
, PVDM_CONSOLE_RECORD
*Record
)
30 PVDM_CONSOLE_RECORD CurrentRecord
= NULL
;
32 /* Search for a record that has the same console handle */
33 for (i
= VDMConsoleListHead
.Flink
; i
!= &VDMConsoleListHead
; i
= i
->Flink
)
35 CurrentRecord
= CONTAINING_RECORD(i
, VDM_CONSOLE_RECORD
, Entry
);
36 if (CurrentRecord
->ConsoleHandle
== ConsoleHandle
) break;
39 *Record
= CurrentRecord
;
40 return CurrentRecord
? STATUS_SUCCESS
: STATUS_NOT_FOUND
;
43 ULONG NTAPI
GetNextDosSesId(VOID
)
47 PVDM_CONSOLE_RECORD CurrentRecord
= NULL
;
50 /* Search for an available session ID */
51 for (SessionId
= 1; SessionId
!= 0; SessionId
++)
55 /* Check if the ID is already in use */
56 for (i
= VDMConsoleListHead
.Flink
; i
!= &VDMConsoleListHead
; i
= i
->Flink
)
58 CurrentRecord
= CONTAINING_RECORD(i
, VDM_CONSOLE_RECORD
, Entry
);
59 if (CurrentRecord
->SessionId
== SessionId
) Found
= TRUE
;
62 /* If not, we found one */
66 ASSERT(SessionId
!= 0);
68 /* Return the session ID */
72 BOOLEAN NTAPI
BaseSrvIsVdmAllowed(VOID
)
75 BOOLEAN VdmAllowed
= TRUE
;
76 HANDLE RootKey
, KeyHandle
;
77 UNICODE_STRING KeyName
, ValueName
, MachineKeyName
;
78 OBJECT_ATTRIBUTES Attributes
;
79 UCHAR ValueBuffer
[sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
)];
80 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo
= (PKEY_VALUE_PARTIAL_INFORMATION
)ValueBuffer
;
83 /* Initialize the unicode strings */
84 RtlInitUnicodeString(&MachineKeyName
, L
"\\Registry\\Machine");
85 RtlInitUnicodeString(&KeyName
, VDM_POLICY_KEY_NAME
);
86 RtlInitUnicodeString(&ValueName
, VDM_DISALLOWED_VALUE_NAME
);
88 InitializeObjectAttributes(&Attributes
,
94 /* Open the local machine key */
95 Status
= NtOpenKey(&RootKey
, KEY_READ
, &Attributes
);
96 if (!NT_SUCCESS(Status
)) return FALSE
;
98 InitializeObjectAttributes(&Attributes
,
100 OBJ_CASE_INSENSITIVE
,
104 /* Open the policy key in the local machine hive, if it exists */
105 if (NT_SUCCESS(NtOpenKey(&KeyHandle
, KEY_READ
, &Attributes
)))
107 /* Read the value, if it's set */
108 if (NT_SUCCESS(NtQueryValueKey(KeyHandle
,
110 KeyValuePartialInformation
,
115 if (*((PULONG
)ValueInfo
->Data
))
117 /* The VDM has been disabled in the registry */
125 /* Close the local machine key */
128 /* If it's disabled system-wide, there's no need to check the user key */
129 if (!VdmAllowed
) return FALSE
;
131 /* Open the current user key of the client */
132 if (!CsrImpersonateClient(NULL
)) return VdmAllowed
;
133 Status
= RtlOpenCurrentUser(KEY_READ
, &RootKey
);
136 /* If that fails, return the system-wide setting */
137 if (!NT_SUCCESS(Status
)) return VdmAllowed
;
139 InitializeObjectAttributes(&Attributes
,
141 OBJ_CASE_INSENSITIVE
,
145 /* Open the policy key in the current user hive, if it exists */
146 if (NT_SUCCESS(NtOpenKey(&KeyHandle
, KEY_READ
, &Attributes
)))
148 /* Read the value, if it's set */
149 if (NT_SUCCESS(NtQueryValueKey(KeyHandle
,
151 KeyValuePartialInformation
,
156 if (*((PULONG
)ValueInfo
->Data
))
158 /* The VDM has been disabled in the registry */
169 VOID NTAPI
BaseInitializeVDM(VOID
)
171 /* Initialize the list head */
172 InitializeListHead(&VDMConsoleListHead
);
174 /* Initialize the critical section */
175 RtlInitializeCriticalSection(&DosCriticalSection
);
176 RtlInitializeCriticalSection(&WowCriticalSection
);
179 /* PUBLIC SERVER APIS *********************************************************/
181 CSR_API(BaseSrvCheckVDM
)
184 PBASE_CHECK_VDM CheckVdmRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.CheckVDMRequest
;
185 PRTL_CRITICAL_SECTION CriticalSection
= NULL
;
186 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
187 PVDM_DOS_RECORD DosRecord
= NULL
;
188 BOOLEAN NewConsoleRecord
= FALSE
;
190 /* Don't do anything if the VDM has been disabled in the registry */
191 if (!BaseSrvIsVdmAllowed()) return STATUS_ACCESS_DENIED
;
193 /* Validate the message buffers */
194 if (!CsrValidateMessageBuffer(ApiMessage
,
195 (PVOID
*)&CheckVdmRequest
->CmdLine
,
196 CheckVdmRequest
->CmdLen
,
197 sizeof(*CheckVdmRequest
->CmdLine
))
198 || !CsrValidateMessageBuffer(ApiMessage
,
199 (PVOID
*)&CheckVdmRequest
->AppName
,
200 CheckVdmRequest
->AppLen
,
201 sizeof(*CheckVdmRequest
->AppName
))
202 || !CsrValidateMessageBuffer(ApiMessage
,
203 (PVOID
*)&CheckVdmRequest
->PifFile
,
204 CheckVdmRequest
->PifLen
,
205 sizeof(*CheckVdmRequest
->PifFile
))
206 || !CsrValidateMessageBuffer(ApiMessage
,
207 (PVOID
*)&CheckVdmRequest
->CurDirectory
,
208 CheckVdmRequest
->CurDirectoryLen
,
209 sizeof(*CheckVdmRequest
->CurDirectory
))
210 || !CsrValidateMessageBuffer(ApiMessage
,
211 (PVOID
*)&CheckVdmRequest
->Desktop
,
212 CheckVdmRequest
->DesktopLen
,
213 sizeof(*CheckVdmRequest
->Desktop
))
214 || !CsrValidateMessageBuffer(ApiMessage
,
215 (PVOID
*)&CheckVdmRequest
->Title
,
216 CheckVdmRequest
->TitleLen
,
217 sizeof(*CheckVdmRequest
->Title
))
218 || !CsrValidateMessageBuffer(ApiMessage
,
219 (PVOID
*)&CheckVdmRequest
->Reserved
,
220 CheckVdmRequest
->ReservedLen
,
221 sizeof(*CheckVdmRequest
->Reserved
)))
223 return STATUS_INVALID_PARAMETER
;
226 CriticalSection
= (CheckVdmRequest
->BinaryType
!= BINARY_TYPE_SEPARATE_WOW
)
227 ? &DosCriticalSection
228 : &WowCriticalSection
;
230 /* Enter the critical section */
231 RtlEnterCriticalSection(CriticalSection
);
233 /* Check if this is a DOS or WOW VDM */
234 if (CheckVdmRequest
->BinaryType
!= BINARY_TYPE_SEPARATE_WOW
)
236 /* Get the console record */
237 Status
= BaseSrvGetConsoleRecord(CheckVdmRequest
->ConsoleHandle
,
240 if (!NT_SUCCESS(Status
))
242 /* Allocate a new console record */
243 ConsoleRecord
= (PVDM_CONSOLE_RECORD
)RtlAllocateHeap(BaseSrvHeap
,
245 sizeof(VDM_CONSOLE_RECORD
));
246 if (ConsoleRecord
== NULL
)
248 Status
= STATUS_NO_MEMORY
;
252 /* Initialize the console record */
253 ConsoleRecord
->ConsoleHandle
= CheckVdmRequest
->ConsoleHandle
;
254 ConsoleRecord
->CurrentDirs
= NULL
;
255 ConsoleRecord
->CurDirsLength
= 0;
256 ConsoleRecord
->SessionId
= GetNextDosSesId();
257 InitializeListHead(&ConsoleRecord
->DosListHead
);
258 // TODO: The console record structure is incomplete
260 /* Remember that the console record was allocated here */
261 NewConsoleRecord
= TRUE
;
264 /* Allocate a new DOS record */
265 DosRecord
= (PVDM_DOS_RECORD
)RtlAllocateHeap(BaseSrvHeap
,
267 sizeof(VDM_DOS_RECORD
));
268 if (DosRecord
== NULL
)
270 Status
= STATUS_NO_MEMORY
;
274 /* Initialize the DOS record */
275 DosRecord
->State
= NewConsoleRecord
? VDM_NOT_LOADED
: VDM_READY
;
276 DosRecord
->ExitCode
= 0;
277 // TODO: The DOS record structure is incomplete
279 /* Add the DOS record */
280 InsertHeadList(&ConsoleRecord
->DosListHead
, &DosRecord
->Entry
);
282 if (NewConsoleRecord
)
284 /* Add the console record */
285 InsertTailList(&VDMConsoleListHead
, &ConsoleRecord
->Entry
);
288 CheckVdmRequest
->VDMState
= DosRecord
->State
;
289 Status
= STATUS_SUCCESS
;
293 // TODO: NOT IMPLEMENTED
295 return STATUS_NOT_IMPLEMENTED
;
299 /* Check if it failed */
300 if (!NT_SUCCESS(Status
))
302 /* Free the DOS record */
303 if (DosRecord
!= NULL
)
305 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
309 /* Free the console record if it was allocated here */
310 if (NewConsoleRecord
)
312 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
);
313 ConsoleRecord
= NULL
;
317 /* Leave the critical section */
318 RtlLeaveCriticalSection(CriticalSection
);
323 CSR_API(BaseSrvUpdateVDMEntry
)
325 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
326 return STATUS_NOT_IMPLEMENTED
;
329 CSR_API(BaseSrvGetNextVDMCommand
)
331 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
332 return STATUS_NOT_IMPLEMENTED
;
335 CSR_API(BaseSrvExitVDM
)
338 PBASE_EXIT_VDM ExitVdmRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.ExitVDMRequest
;
339 PRTL_CRITICAL_SECTION CriticalSection
= NULL
;
340 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
341 PVDM_DOS_RECORD DosRecord
;
343 CriticalSection
= (ExitVdmRequest
->iWowTask
== 0)
344 ? &DosCriticalSection
345 : &WowCriticalSection
;
347 /* Enter the critical section */
348 RtlEnterCriticalSection(CriticalSection
);
350 if (ExitVdmRequest
->iWowTask
== 0)
352 /* Get the console record */
353 Status
= BaseSrvGetConsoleRecord(ExitVdmRequest
->ConsoleHandle
, &ConsoleRecord
);
354 if (!NT_SUCCESS(Status
)) goto Cleanup
;
356 /* Cleanup the DOS records */
357 while (ConsoleRecord
->DosListHead
.Flink
!= &ConsoleRecord
->DosListHead
)
359 DosRecord
= CONTAINING_RECORD(ConsoleRecord
->DosListHead
.Flink
,
363 /* Remove the DOS entry */
364 RemoveEntryList(&DosRecord
->Entry
);
365 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
368 if (ConsoleRecord
->CurrentDirs
!= NULL
)
370 /* Free the current directories */
371 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
->CurrentDirs
);
372 ConsoleRecord
->CurrentDirs
= NULL
;
373 ConsoleRecord
->CurDirsLength
= 0;
376 /* Remove the console record */
377 RemoveEntryList(&ConsoleRecord
->Entry
);
378 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
);
382 // TODO: NOT IMPLEMENTED
384 return STATUS_NOT_IMPLEMENTED
;
388 /* Leave the critical section */
389 RtlLeaveCriticalSection(CriticalSection
);
394 CSR_API(BaseSrvIsFirstVDM
)
396 PBASE_IS_FIRST_VDM IsFirstVDMRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.IsFirstVDMRequest
;
398 /* Return the result */
399 IsFirstVDMRequest
->FirstVDM
= FirstVDM
;
401 /* Clear the first VDM flag */
404 return STATUS_SUCCESS
;
407 CSR_API(BaseSrvGetVDMExitCode
)
410 PBASE_GET_VDM_EXIT_CODE GetVDMExitCodeRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.GetVDMExitCodeRequest
;
411 PLIST_ENTRY i
= NULL
;
412 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
413 PVDM_DOS_RECORD DosRecord
= NULL
;
415 /* Enter the critical section */
416 RtlEnterCriticalSection(&DosCriticalSection
);
418 /* Get the console record */
419 Status
= BaseSrvGetConsoleRecord(GetVDMExitCodeRequest
->ConsoleHandle
, &ConsoleRecord
);
420 if (!NT_SUCCESS(Status
)) goto Cleanup
;
422 /* Search for a DOS record that has the same parent process handle */
423 for (i
= ConsoleRecord
->DosListHead
.Flink
; i
!= &ConsoleRecord
->DosListHead
; i
= i
->Flink
)
425 DosRecord
= CONTAINING_RECORD(i
, VDM_DOS_RECORD
, Entry
);
426 if (DosRecord
->ParentProcess
== GetVDMExitCodeRequest
->hParent
) break;
429 /* Check if no DOS record was found */
430 if (i
== &ConsoleRecord
->DosListHead
)
432 Status
= STATUS_NOT_FOUND
;
436 /* Check if this task is still running */
437 if (DosRecord
->State
== VDM_READY
)
439 GetVDMExitCodeRequest
->ExitCode
= STATUS_PENDING
;
443 /* Return the exit code */
444 GetVDMExitCodeRequest
->ExitCode
= DosRecord
->ExitCode
;
446 /* Since this is a zombie task record, remove it */
447 RemoveEntryList(&DosRecord
->Entry
);
448 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
451 /* Leave the critical section */
452 RtlLeaveCriticalSection(&DosCriticalSection
);
457 CSR_API(BaseSrvSetReenterCount
)
459 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
460 return STATUS_NOT_IMPLEMENTED
;
463 CSR_API(BaseSrvSetVDMCurDirs
)
466 PBASE_GETSET_VDM_CURDIRS VDMCurrentDirsRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.VDMCurrentDirsRequest
;
467 PVDM_CONSOLE_RECORD ConsoleRecord
;
470 /* Validate the input buffer */
471 if (!CsrValidateMessageBuffer(ApiMessage
,
472 (PVOID
*)&VDMCurrentDirsRequest
->lpszzCurDirs
,
473 VDMCurrentDirsRequest
->cchCurDirs
,
474 sizeof(*VDMCurrentDirsRequest
->lpszzCurDirs
)))
476 return STATUS_INVALID_PARAMETER
;
479 /* Enter the critical section */
480 RtlEnterCriticalSection(&DosCriticalSection
);
482 /* Find the console record */
483 Status
= BaseSrvGetConsoleRecord(VDMCurrentDirsRequest
->ConsoleHandle
, &ConsoleRecord
);
484 if (!NT_SUCCESS(Status
)) goto Cleanup
;
486 if (ConsoleRecord
->CurrentDirs
== NULL
)
488 /* Allocate memory for the current directory information */
489 Buffer
= RtlAllocateHeap(BaseSrvHeap
,
491 VDMCurrentDirsRequest
->cchCurDirs
);
495 /* Resize the amount of allocated memory */
496 Buffer
= RtlReAllocateHeap(BaseSrvHeap
,
498 ConsoleRecord
->CurrentDirs
,
499 VDMCurrentDirsRequest
->cchCurDirs
);
504 /* Allocation failed */
505 Status
= STATUS_NO_MEMORY
;
509 /* Update the console record */
510 ConsoleRecord
->CurrentDirs
= Buffer
;
511 ConsoleRecord
->CurDirsLength
= VDMCurrentDirsRequest
->cchCurDirs
;
514 RtlMoveMemory(ConsoleRecord
->CurrentDirs
,
515 VDMCurrentDirsRequest
->lpszzCurDirs
,
516 VDMCurrentDirsRequest
->cchCurDirs
);
519 /* Leave the critical section */
520 RtlLeaveCriticalSection(&DosCriticalSection
);
525 CSR_API(BaseSrvGetVDMCurDirs
)
528 PBASE_GETSET_VDM_CURDIRS VDMCurrentDirsRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.VDMCurrentDirsRequest
;
529 PVDM_CONSOLE_RECORD ConsoleRecord
;
531 /* Validate the output buffer */
532 if (!CsrValidateMessageBuffer(ApiMessage
,
533 (PVOID
*)&VDMCurrentDirsRequest
->lpszzCurDirs
,
534 VDMCurrentDirsRequest
->cchCurDirs
,
535 sizeof(*VDMCurrentDirsRequest
->lpszzCurDirs
)))
537 return STATUS_INVALID_PARAMETER
;
540 /* Enter the critical section */
541 RtlEnterCriticalSection(&DosCriticalSection
);
543 /* Find the console record */
544 Status
= BaseSrvGetConsoleRecord(VDMCurrentDirsRequest
->ConsoleHandle
, &ConsoleRecord
);
545 if (!NT_SUCCESS(Status
)) goto Cleanup
;
547 /* Return the actual size of the current directory information */
548 VDMCurrentDirsRequest
->cchCurDirs
= ConsoleRecord
->CurDirsLength
;
550 /* Check if the buffer is large enough */
551 if (VDMCurrentDirsRequest
->cchCurDirs
< ConsoleRecord
->CurDirsLength
)
553 Status
= STATUS_BUFFER_TOO_SMALL
;
558 RtlMoveMemory(VDMCurrentDirsRequest
->lpszzCurDirs
,
559 ConsoleRecord
->CurrentDirs
,
560 ConsoleRecord
->CurDirsLength
);
563 /* Leave the critical section */
564 RtlLeaveCriticalSection(&DosCriticalSection
);
569 CSR_API(BaseSrvBatNotification
)
571 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
572 return STATUS_NOT_IMPLEMENTED
;
575 CSR_API(BaseSrvRegisterWowExec
)
577 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
578 return STATUS_NOT_IMPLEMENTED
;
581 CSR_API(BaseSrvRefreshIniFileMapping
)
583 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
584 return STATUS_NOT_IMPLEMENTED
;