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
;
188 /* Don't do anything if the VDM has been disabled in the registry */
189 if (!BaseSrvIsVdmAllowed()) return STATUS_ACCESS_DENIED
;
191 /* Validate the message buffers */
192 if (!CsrValidateMessageBuffer(ApiMessage
,
193 (PVOID
*)&CheckVdmRequest
->CmdLine
,
194 CheckVdmRequest
->CmdLen
,
195 sizeof(*CheckVdmRequest
->CmdLine
))
196 || !CsrValidateMessageBuffer(ApiMessage
,
197 (PVOID
*)&CheckVdmRequest
->AppName
,
198 CheckVdmRequest
->AppLen
,
199 sizeof(*CheckVdmRequest
->AppName
))
200 || !CsrValidateMessageBuffer(ApiMessage
,
201 (PVOID
*)&CheckVdmRequest
->PifFile
,
202 CheckVdmRequest
->PifLen
,
203 sizeof(*CheckVdmRequest
->PifFile
))
204 || !CsrValidateMessageBuffer(ApiMessage
,
205 (PVOID
*)&CheckVdmRequest
->CurDirectory
,
206 CheckVdmRequest
->CurDirectoryLen
,
207 sizeof(*CheckVdmRequest
->CurDirectory
))
208 || !CsrValidateMessageBuffer(ApiMessage
,
209 (PVOID
*)&CheckVdmRequest
->Desktop
,
210 CheckVdmRequest
->DesktopLen
,
211 sizeof(*CheckVdmRequest
->Desktop
))
212 || !CsrValidateMessageBuffer(ApiMessage
,
213 (PVOID
*)&CheckVdmRequest
->Title
,
214 CheckVdmRequest
->TitleLen
,
215 sizeof(*CheckVdmRequest
->Title
))
216 || !CsrValidateMessageBuffer(ApiMessage
,
217 (PVOID
*)&CheckVdmRequest
->Reserved
,
218 CheckVdmRequest
->ReservedLen
,
219 sizeof(*CheckVdmRequest
->Reserved
)))
221 return STATUS_INVALID_PARAMETER
;
224 CriticalSection
= (CheckVdmRequest
->BinaryType
!= BINARY_TYPE_SEPARATE_WOW
)
225 ? &DosCriticalSection
226 : &WowCriticalSection
;
228 /* Enter the critical section */
229 RtlEnterCriticalSection(CriticalSection
);
231 /* Check if this is a DOS or WOW VDM */
232 if (CheckVdmRequest
->BinaryType
!= BINARY_TYPE_SEPARATE_WOW
)
234 /* Get the console record */
235 Status
= BaseSrvGetConsoleRecord(CheckVdmRequest
->ConsoleHandle
,
238 if (!NT_SUCCESS(Status
))
240 /* Allocate a new console record */
241 ConsoleRecord
= (PVDM_CONSOLE_RECORD
)RtlAllocateHeap(BaseSrvHeap
,
243 sizeof(VDM_CONSOLE_RECORD
));
244 if (ConsoleRecord
== NULL
)
246 Status
= STATUS_NO_MEMORY
;
250 /* Initialize the console record */
251 ConsoleRecord
->ConsoleHandle
= CheckVdmRequest
->ConsoleHandle
;
252 ConsoleRecord
->CurrentDirs
= NULL
;
253 ConsoleRecord
->CurDirsLength
= 0;
254 ConsoleRecord
->SessionId
= GetNextDosSesId();
255 InitializeListHead(&ConsoleRecord
->DosListHead
);
257 /* Add the console record */
258 InsertTailList(&VDMConsoleListHead
, &ConsoleRecord
->Entry
);
261 // TODO: NOT IMPLEMENTED
263 return STATUS_NOT_IMPLEMENTED
;
267 // TODO: NOT IMPLEMENTED
269 return STATUS_NOT_IMPLEMENTED
;
273 /* Leave the critical section */
274 RtlLeaveCriticalSection(CriticalSection
);
279 CSR_API(BaseSrvUpdateVDMEntry
)
281 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
282 return STATUS_NOT_IMPLEMENTED
;
285 CSR_API(BaseSrvGetNextVDMCommand
)
287 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
288 return STATUS_NOT_IMPLEMENTED
;
291 CSR_API(BaseSrvExitVDM
)
294 PBASE_EXIT_VDM ExitVdmRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.ExitVDMRequest
;
295 PRTL_CRITICAL_SECTION CriticalSection
= NULL
;
296 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
297 PVDM_DOS_RECORD DosRecord
;
299 CriticalSection
= (ExitVdmRequest
->iWowTask
== 0)
300 ? &DosCriticalSection
301 : &WowCriticalSection
;
303 /* Enter the critical section */
304 RtlEnterCriticalSection(CriticalSection
);
306 if (ExitVdmRequest
->iWowTask
== 0)
308 /* Get the console record */
309 Status
= BaseSrvGetConsoleRecord(ExitVdmRequest
->ConsoleHandle
, &ConsoleRecord
);
310 if (!NT_SUCCESS(Status
)) goto Cleanup
;
312 /* Cleanup the DOS records */
313 while (ConsoleRecord
->DosListHead
.Flink
!= &ConsoleRecord
->DosListHead
)
315 DosRecord
= CONTAINING_RECORD(ConsoleRecord
->DosListHead
.Flink
,
319 /* Remove the DOS entry */
320 RemoveEntryList(&DosRecord
->Entry
);
321 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
324 if (ConsoleRecord
->CurrentDirs
!= NULL
)
326 /* Free the current directories */
327 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
->CurrentDirs
);
328 ConsoleRecord
->CurrentDirs
= NULL
;
329 ConsoleRecord
->CurDirsLength
= 0;
332 /* Remove the console record */
333 RemoveEntryList(&ConsoleRecord
->Entry
);
334 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
);
338 // TODO: NOT IMPLEMENTED
340 return STATUS_NOT_IMPLEMENTED
;
344 /* Leave the critical section */
345 RtlLeaveCriticalSection(CriticalSection
);
350 CSR_API(BaseSrvIsFirstVDM
)
352 PBASE_IS_FIRST_VDM IsFirstVDMRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.IsFirstVDMRequest
;
354 /* Return the result */
355 IsFirstVDMRequest
->FirstVDM
= FirstVDM
;
357 /* Clear the first VDM flag */
360 return STATUS_SUCCESS
;
363 CSR_API(BaseSrvGetVDMExitCode
)
366 PBASE_GET_VDM_EXIT_CODE GetVDMExitCodeRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.GetVDMExitCodeRequest
;
367 PLIST_ENTRY i
= NULL
;
368 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
369 PVDM_DOS_RECORD DosRecord
= NULL
;
371 /* Enter the critical section */
372 RtlEnterCriticalSection(&DosCriticalSection
);
374 /* Get the console record */
375 Status
= BaseSrvGetConsoleRecord(GetVDMExitCodeRequest
->ConsoleHandle
, &ConsoleRecord
);
376 if (!NT_SUCCESS(Status
)) goto Cleanup
;
378 /* Search for a DOS record that has the same parent process handle */
379 for (i
= ConsoleRecord
->DosListHead
.Flink
; i
!= &ConsoleRecord
->DosListHead
; i
= i
->Flink
)
381 DosRecord
= CONTAINING_RECORD(i
, VDM_DOS_RECORD
, Entry
);
382 if (DosRecord
->ParentProcess
== GetVDMExitCodeRequest
->hParent
) break;
385 /* Check if no DOS record was found */
386 if (i
== &ConsoleRecord
->DosListHead
)
388 Status
= STATUS_NOT_FOUND
;
392 /* Check if this task is still running */
393 if (DosRecord
->State
== VDM_READY
)
395 GetVDMExitCodeRequest
->ExitCode
= STATUS_PENDING
;
399 /* Return the exit code */
400 GetVDMExitCodeRequest
->ExitCode
= DosRecord
->ExitCode
;
402 /* Since this is a zombie task record, remove it */
403 RemoveEntryList(&DosRecord
->Entry
);
404 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
407 /* Leave the critical section */
408 RtlLeaveCriticalSection(&DosCriticalSection
);
413 CSR_API(BaseSrvSetReenterCount
)
415 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
416 return STATUS_NOT_IMPLEMENTED
;
419 CSR_API(BaseSrvSetVDMCurDirs
)
422 PBASE_GETSET_VDM_CURDIRS VDMCurrentDirsRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.VDMCurrentDirsRequest
;
423 PVDM_CONSOLE_RECORD ConsoleRecord
;
426 /* Validate the input buffer */
427 if (!CsrValidateMessageBuffer(ApiMessage
,
428 (PVOID
*)&VDMCurrentDirsRequest
->lpszzCurDirs
,
429 VDMCurrentDirsRequest
->cchCurDirs
,
430 sizeof(*VDMCurrentDirsRequest
->lpszzCurDirs
)))
432 return STATUS_INVALID_PARAMETER
;
435 /* Enter the critical section */
436 RtlEnterCriticalSection(&DosCriticalSection
);
438 /* Find the console record */
439 Status
= BaseSrvGetConsoleRecord(VDMCurrentDirsRequest
->ConsoleHandle
, &ConsoleRecord
);
440 if (!NT_SUCCESS(Status
)) goto Cleanup
;
442 if (ConsoleRecord
->CurrentDirs
== NULL
)
444 /* Allocate memory for the current directory information */
445 Buffer
= RtlAllocateHeap(BaseSrvHeap
,
447 VDMCurrentDirsRequest
->cchCurDirs
);
451 /* Resize the amount of allocated memory */
452 Buffer
= RtlReAllocateHeap(BaseSrvHeap
,
454 ConsoleRecord
->CurrentDirs
,
455 VDMCurrentDirsRequest
->cchCurDirs
);
460 /* Allocation failed */
461 Status
= STATUS_NO_MEMORY
;
465 /* Update the console record */
466 ConsoleRecord
->CurrentDirs
= Buffer
;
467 ConsoleRecord
->CurDirsLength
= VDMCurrentDirsRequest
->cchCurDirs
;
470 RtlMoveMemory(ConsoleRecord
->CurrentDirs
,
471 VDMCurrentDirsRequest
->lpszzCurDirs
,
472 VDMCurrentDirsRequest
->cchCurDirs
);
475 /* Leave the critical section */
476 RtlLeaveCriticalSection(&DosCriticalSection
);
481 CSR_API(BaseSrvGetVDMCurDirs
)
484 PBASE_GETSET_VDM_CURDIRS VDMCurrentDirsRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.VDMCurrentDirsRequest
;
485 PVDM_CONSOLE_RECORD ConsoleRecord
;
487 /* Validate the output buffer */
488 if (!CsrValidateMessageBuffer(ApiMessage
,
489 (PVOID
*)&VDMCurrentDirsRequest
->lpszzCurDirs
,
490 VDMCurrentDirsRequest
->cchCurDirs
,
491 sizeof(*VDMCurrentDirsRequest
->lpszzCurDirs
)))
493 return STATUS_INVALID_PARAMETER
;
496 /* Enter the critical section */
497 RtlEnterCriticalSection(&DosCriticalSection
);
499 /* Find the console record */
500 Status
= BaseSrvGetConsoleRecord(VDMCurrentDirsRequest
->ConsoleHandle
, &ConsoleRecord
);
501 if (!NT_SUCCESS(Status
)) goto Cleanup
;
503 /* Return the actual size of the current directory information */
504 VDMCurrentDirsRequest
->cchCurDirs
= ConsoleRecord
->CurDirsLength
;
506 /* Check if the buffer is large enough */
507 if (VDMCurrentDirsRequest
->cchCurDirs
< ConsoleRecord
->CurDirsLength
)
509 Status
= STATUS_BUFFER_TOO_SMALL
;
514 RtlMoveMemory(VDMCurrentDirsRequest
->lpszzCurDirs
,
515 ConsoleRecord
->CurrentDirs
,
516 ConsoleRecord
->CurDirsLength
);
519 /* Leave the critical section */
520 RtlLeaveCriticalSection(&DosCriticalSection
);
525 CSR_API(BaseSrvBatNotification
)
527 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
528 return STATUS_NOT_IMPLEMENTED
;
531 CSR_API(BaseSrvRegisterWowExec
)
533 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
534 return STATUS_NOT_IMPLEMENTED
;
537 CSR_API(BaseSrvRefreshIniFileMapping
)
539 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
540 return STATUS_NOT_IMPLEMENTED
;