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 NTSTATUS NTAPI
GetConsoleRecordBySessionId(ULONG TaskId
, PVDM_CONSOLE_RECORD
*Record
)
46 PVDM_CONSOLE_RECORD CurrentRecord
= NULL
;
48 /* Search for a record that has the same console handle */
49 for (i
= VDMConsoleListHead
.Flink
; i
!= &VDMConsoleListHead
; i
= i
->Flink
)
51 CurrentRecord
= CONTAINING_RECORD(i
, VDM_CONSOLE_RECORD
, Entry
);
52 if (CurrentRecord
->SessionId
== TaskId
) break;
55 *Record
= CurrentRecord
;
56 return CurrentRecord
? STATUS_SUCCESS
: STATUS_NOT_FOUND
;
59 ULONG NTAPI
GetNextDosSesId(VOID
)
63 PVDM_CONSOLE_RECORD CurrentRecord
= NULL
;
66 /* Search for an available session ID */
67 for (SessionId
= 1; SessionId
!= 0; SessionId
++)
71 /* Check if the ID is already in use */
72 for (i
= VDMConsoleListHead
.Flink
; i
!= &VDMConsoleListHead
; i
= i
->Flink
)
74 CurrentRecord
= CONTAINING_RECORD(i
, VDM_CONSOLE_RECORD
, Entry
);
75 if (CurrentRecord
->SessionId
== SessionId
) Found
= TRUE
;
78 /* If not, we found one */
82 ASSERT(SessionId
!= 0);
84 /* Return the session ID */
88 BOOLEAN NTAPI
BaseSrvIsVdmAllowed(VOID
)
91 BOOLEAN VdmAllowed
= TRUE
;
92 HANDLE RootKey
, KeyHandle
;
93 UNICODE_STRING KeyName
, ValueName
, MachineKeyName
;
94 OBJECT_ATTRIBUTES Attributes
;
95 UCHAR ValueBuffer
[sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
)];
96 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo
= (PKEY_VALUE_PARTIAL_INFORMATION
)ValueBuffer
;
99 /* Initialize the unicode strings */
100 RtlInitUnicodeString(&MachineKeyName
, L
"\\Registry\\Machine");
101 RtlInitUnicodeString(&KeyName
, VDM_POLICY_KEY_NAME
);
102 RtlInitUnicodeString(&ValueName
, VDM_DISALLOWED_VALUE_NAME
);
104 InitializeObjectAttributes(&Attributes
,
106 OBJ_CASE_INSENSITIVE
,
110 /* Open the local machine key */
111 Status
= NtOpenKey(&RootKey
, KEY_READ
, &Attributes
);
112 if (!NT_SUCCESS(Status
)) return FALSE
;
114 InitializeObjectAttributes(&Attributes
,
116 OBJ_CASE_INSENSITIVE
,
120 /* Open the policy key in the local machine hive, if it exists */
121 if (NT_SUCCESS(NtOpenKey(&KeyHandle
, KEY_READ
, &Attributes
)))
123 /* Read the value, if it's set */
124 if (NT_SUCCESS(NtQueryValueKey(KeyHandle
,
126 KeyValuePartialInformation
,
131 if (*((PULONG
)ValueInfo
->Data
))
133 /* The VDM has been disabled in the registry */
141 /* Close the local machine key */
144 /* If it's disabled system-wide, there's no need to check the user key */
145 if (!VdmAllowed
) return FALSE
;
147 /* Open the current user key of the client */
148 if (!CsrImpersonateClient(NULL
)) return VdmAllowed
;
149 Status
= RtlOpenCurrentUser(KEY_READ
, &RootKey
);
152 /* If that fails, return the system-wide setting */
153 if (!NT_SUCCESS(Status
)) return VdmAllowed
;
155 InitializeObjectAttributes(&Attributes
,
157 OBJ_CASE_INSENSITIVE
,
161 /* Open the policy key in the current user hive, if it exists */
162 if (NT_SUCCESS(NtOpenKey(&KeyHandle
, KEY_READ
, &Attributes
)))
164 /* Read the value, if it's set */
165 if (NT_SUCCESS(NtQueryValueKey(KeyHandle
,
167 KeyValuePartialInformation
,
172 if (*((PULONG
)ValueInfo
->Data
))
174 /* The VDM has been disabled in the registry */
185 NTSTATUS NTAPI
BaseSrvCreatePairWaitHandles(PHANDLE ServerEvent
, PHANDLE ClientEvent
)
189 /* Create the event */
190 Status
= NtCreateEvent(ServerEvent
, EVENT_ALL_ACCESS
, NULL
, NotificationEvent
, FALSE
);
191 if (!NT_SUCCESS(Status
)) return Status
;
193 /* Duplicate the event into the client process */
194 Status
= NtDuplicateObject(NtCurrentProcess(),
196 CsrGetClientThread()->Process
->ProcessHandle
,
200 DUPLICATE_SAME_ATTRIBUTES
| DUPLICATE_SAME_ACCESS
);
202 if (!NT_SUCCESS(Status
)) NtClose(*ServerEvent
);
206 VOID
BaseSrvFreeVDMInfo(PVDM_COMMAND_INFO CommandInfo
)
208 /* Free the allocated structure members */
209 if (CommandInfo
->CmdLine
!= NULL
) RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
->CmdLine
);
210 if (CommandInfo
->AppName
!= NULL
) RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
->AppName
);
211 if (CommandInfo
->PifFile
!= NULL
) RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
->PifFile
);
212 if (CommandInfo
->CurDirectory
!= NULL
) RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
->CurDirectory
);
213 if (CommandInfo
->Env
!= NULL
) RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
->Env
);
214 if (CommandInfo
->Desktop
!= NULL
) RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
->Desktop
);
215 if (CommandInfo
->Title
!= NULL
) RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
->Title
);
216 if (CommandInfo
->Reserved
!= NULL
) RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
->Reserved
);
218 /* Free the structure itself */
219 RtlFreeHeap(BaseSrvHeap
, 0, CommandInfo
);
222 BOOLEAN NTAPI
BaseSrvCopyCommand(PBASE_CHECK_VDM CheckVdmRequest
, PVDM_DOS_RECORD DosRecord
)
224 BOOLEAN Success
= FALSE
;
225 PVDM_COMMAND_INFO CommandInfo
= NULL
;
227 /* Allocate the command information structure */
228 CommandInfo
= (PVDM_COMMAND_INFO
)RtlAllocateHeap(BaseSrvHeap
,
230 sizeof(VDM_COMMAND_INFO
));
231 if (CommandInfo
== NULL
) return FALSE
;
233 /* Fill the structure */
234 CommandInfo
->TaskId
= CheckVdmRequest
->iTask
;
235 CommandInfo
->ExitCode
= DosRecord
->ExitCode
;
236 CommandInfo
->CodePage
= CheckVdmRequest
->CodePage
;
237 CommandInfo
->StdIn
= CheckVdmRequest
->StdIn
;
238 CommandInfo
->StdOut
= CheckVdmRequest
->StdOut
;
239 CommandInfo
->StdErr
= CheckVdmRequest
->StdErr
;
241 /* Allocate memory for the command line */
242 CommandInfo
->CmdLine
= RtlAllocateHeap(BaseSrvHeap
,
244 CheckVdmRequest
->CmdLen
);
245 if (CommandInfo
->CmdLine
== NULL
) goto Cleanup
;
247 /* Copy the command line */
248 RtlMoveMemory(CommandInfo
->CmdLine
, CheckVdmRequest
->CmdLine
, CheckVdmRequest
->CmdLen
);
250 /* Allocate memory for the application name */
251 CommandInfo
->AppName
= RtlAllocateHeap(BaseSrvHeap
,
253 CheckVdmRequest
->AppLen
);
254 if (CommandInfo
->AppName
== NULL
) goto Cleanup
;
256 /* Copy the application name */
257 RtlMoveMemory(CommandInfo
->AppName
, CheckVdmRequest
->AppName
, CheckVdmRequest
->AppLen
);
259 /* Allocate memory for the PIF file name */
260 if (CheckVdmRequest
->PifLen
!= 0)
262 CommandInfo
->PifFile
= RtlAllocateHeap(BaseSrvHeap
,
264 CheckVdmRequest
->PifLen
);
265 if (CommandInfo
->PifFile
== NULL
) goto Cleanup
;
267 /* Copy the PIF file name */
268 RtlMoveMemory(CommandInfo
->PifFile
, CheckVdmRequest
->PifFile
, CheckVdmRequest
->PifLen
);
270 else CommandInfo
->PifFile
= NULL
;
272 /* Allocate memory for the current directory */
273 if (CheckVdmRequest
->CurDirectoryLen
!= 0)
275 CommandInfo
->CurDirectory
= RtlAllocateHeap(BaseSrvHeap
,
277 CheckVdmRequest
->CurDirectoryLen
);
278 if (CommandInfo
->CurDirectory
== NULL
) goto Cleanup
;
280 /* Copy the current directory */
281 RtlMoveMemory(CommandInfo
->CurDirectory
,
282 CheckVdmRequest
->CurDirectory
,
283 CheckVdmRequest
->CurDirectoryLen
);
285 else CommandInfo
->CurDirectory
= NULL
;
287 /* Allocate memory for the environment block */
288 CommandInfo
->Env
= RtlAllocateHeap(BaseSrvHeap
,
290 CheckVdmRequest
->EnvLen
);
291 if (CommandInfo
->Env
== NULL
) goto Cleanup
;
293 /* Copy the environment block */
294 RtlMoveMemory(CommandInfo
->Env
, CheckVdmRequest
->Env
, CheckVdmRequest
->EnvLen
);
296 CommandInfo
->EnvLen
= CheckVdmRequest
->EnvLen
;
297 RtlMoveMemory(&CommandInfo
->StartupInfo
,
298 CheckVdmRequest
->StartupInfo
,
299 sizeof(STARTUPINFOA
));
301 /* Allocate memory for the desktop */
302 if (CheckVdmRequest
->DesktopLen
!= 0)
304 CommandInfo
->Desktop
= RtlAllocateHeap(BaseSrvHeap
,
306 CheckVdmRequest
->DesktopLen
);
307 if (CommandInfo
->Desktop
== NULL
) goto Cleanup
;
309 /* Copy the desktop name */
310 RtlMoveMemory(CommandInfo
->Desktop
, CheckVdmRequest
->Desktop
, CheckVdmRequest
->DesktopLen
);
312 else CommandInfo
->Desktop
= NULL
;
314 CommandInfo
->DesktopLen
= CheckVdmRequest
->DesktopLen
;
316 /* Allocate memory for the title */
317 if (CheckVdmRequest
->TitleLen
!= 0)
319 CommandInfo
->Title
= RtlAllocateHeap(BaseSrvHeap
,
321 CheckVdmRequest
->TitleLen
);
322 if (CommandInfo
->Title
== NULL
) goto Cleanup
;
325 RtlMoveMemory(CommandInfo
->Title
, CheckVdmRequest
->Title
, CheckVdmRequest
->TitleLen
);
327 else CommandInfo
->Title
= NULL
;
329 CommandInfo
->TitleLen
= CheckVdmRequest
->TitleLen
;
331 /* Allocate memory for the reserved field */
332 if (CheckVdmRequest
->ReservedLen
!= 0)
334 CommandInfo
->Reserved
= RtlAllocateHeap(BaseSrvHeap
,
336 CheckVdmRequest
->ReservedLen
);
337 if (CommandInfo
->Reserved
== NULL
) goto Cleanup
;
339 /* Copy the reserved field */
340 RtlMoveMemory(CommandInfo
->Reserved
,
341 CheckVdmRequest
->Reserved
,
342 CheckVdmRequest
->ReservedLen
);
344 else CommandInfo
->Reserved
= NULL
;
346 CommandInfo
->ReservedLen
= CheckVdmRequest
->ReservedLen
;
348 CommandInfo
->CmdLen
= CheckVdmRequest
->CmdLen
;
349 CommandInfo
->AppLen
= CheckVdmRequest
->AppLen
;
350 CommandInfo
->PifLen
= CheckVdmRequest
->PifLen
;
351 CommandInfo
->CurDirectoryLen
= CheckVdmRequest
->CurDirectoryLen
;
352 CommandInfo
->VDMState
= DosRecord
->State
;
353 // TODO: Set CommandInfo->CurrentDrive
354 // TODO: Set CommandInfo->ComingFromBat
356 /* Set the DOS record's command structure */
357 DosRecord
->CommandInfo
= CommandInfo
;
359 /* The operation was successful */
363 /* If it wasn't successful, free the memory */
364 if (!Success
) BaseSrvFreeVDMInfo(CommandInfo
);
369 VOID NTAPI
BaseInitializeVDM(VOID
)
371 /* Initialize the list head */
372 InitializeListHead(&VDMConsoleListHead
);
374 /* Initialize the critical section */
375 RtlInitializeCriticalSection(&DosCriticalSection
);
376 RtlInitializeCriticalSection(&WowCriticalSection
);
379 /* PUBLIC SERVER APIS *********************************************************/
381 CSR_API(BaseSrvCheckVDM
)
384 PBASE_CHECK_VDM CheckVdmRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.CheckVDMRequest
;
385 PCSR_PROCESS ClientProcess
;
386 PRTL_CRITICAL_SECTION CriticalSection
= NULL
;
387 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
388 PVDM_DOS_RECORD DosRecord
= NULL
;
389 BOOLEAN NewConsoleRecord
= FALSE
;
391 /* Don't do anything if the VDM has been disabled in the registry */
392 if (!BaseSrvIsVdmAllowed()) return STATUS_ACCESS_DENIED
;
394 /* Validate the message buffers */
395 if (!CsrValidateMessageBuffer(ApiMessage
,
396 (PVOID
*)&CheckVdmRequest
->CmdLine
,
397 CheckVdmRequest
->CmdLen
,
398 sizeof(*CheckVdmRequest
->CmdLine
))
399 || !CsrValidateMessageBuffer(ApiMessage
,
400 (PVOID
*)&CheckVdmRequest
->AppName
,
401 CheckVdmRequest
->AppLen
,
402 sizeof(*CheckVdmRequest
->AppName
))
403 || !CsrValidateMessageBuffer(ApiMessage
,
404 (PVOID
*)&CheckVdmRequest
->PifFile
,
405 CheckVdmRequest
->PifLen
,
406 sizeof(*CheckVdmRequest
->PifFile
))
407 || !CsrValidateMessageBuffer(ApiMessage
,
408 (PVOID
*)&CheckVdmRequest
->CurDirectory
,
409 CheckVdmRequest
->CurDirectoryLen
,
410 sizeof(*CheckVdmRequest
->CurDirectory
))
411 || !CsrValidateMessageBuffer(ApiMessage
,
412 (PVOID
*)&CheckVdmRequest
->Desktop
,
413 CheckVdmRequest
->DesktopLen
,
414 sizeof(*CheckVdmRequest
->Desktop
))
415 || !CsrValidateMessageBuffer(ApiMessage
,
416 (PVOID
*)&CheckVdmRequest
->Title
,
417 CheckVdmRequest
->TitleLen
,
418 sizeof(*CheckVdmRequest
->Title
))
419 || !CsrValidateMessageBuffer(ApiMessage
,
420 (PVOID
*)&CheckVdmRequest
->Reserved
,
421 CheckVdmRequest
->ReservedLen
,
422 sizeof(*CheckVdmRequest
->Reserved
)))
424 return STATUS_INVALID_PARAMETER
;
427 /* Lock the process */
428 Status
= CsrLockProcessByClientId(ApiMessage
->Header
.ClientId
.UniqueProcess
,
430 if (!NT_SUCCESS(Status
)) return Status
;
432 CriticalSection
= (CheckVdmRequest
->BinaryType
!= BINARY_TYPE_SEPARATE_WOW
)
433 ? &DosCriticalSection
434 : &WowCriticalSection
;
436 /* Enter the critical section */
437 RtlEnterCriticalSection(CriticalSection
);
439 /* Check if this is a DOS or WOW VDM */
440 if (CheckVdmRequest
->BinaryType
!= BINARY_TYPE_SEPARATE_WOW
)
442 /* Get the console record */
443 Status
= BaseSrvGetConsoleRecord(CheckVdmRequest
->ConsoleHandle
,
446 if (!NT_SUCCESS(Status
))
448 /* Allocate a new console record */
449 ConsoleRecord
= (PVDM_CONSOLE_RECORD
)RtlAllocateHeap(BaseSrvHeap
,
451 sizeof(VDM_CONSOLE_RECORD
));
452 if (ConsoleRecord
== NULL
)
454 Status
= STATUS_NO_MEMORY
;
458 /* Initialize the console record */
459 ConsoleRecord
->ConsoleHandle
= CheckVdmRequest
->ConsoleHandle
;
460 ConsoleRecord
->ProcessHandle
= ClientProcess
->ProcessHandle
;
461 ConsoleRecord
->ServerEvent
= ConsoleRecord
->ClientEvent
= NULL
;
462 ConsoleRecord
->ReenterCount
= 0;
463 ConsoleRecord
->CurrentDirs
= NULL
;
464 ConsoleRecord
->CurDirsLength
= 0;
465 ConsoleRecord
->SessionId
= GetNextDosSesId();
466 InitializeListHead(&ConsoleRecord
->DosListHead
);
467 // TODO: The console record structure is incomplete
469 /* Remember that the console record was allocated here */
470 NewConsoleRecord
= TRUE
;
473 /* Allocate a new DOS record */
474 DosRecord
= (PVDM_DOS_RECORD
)RtlAllocateHeap(BaseSrvHeap
,
476 sizeof(VDM_DOS_RECORD
));
477 if (DosRecord
== NULL
)
479 Status
= STATUS_NO_MEMORY
;
483 /* Initialize the DOS record */
484 DosRecord
->State
= NewConsoleRecord
? VDM_NOT_LOADED
: VDM_READY
;
485 DosRecord
->ExitCode
= 0;
486 // TODO: The DOS record structure is incomplete
488 Status
= BaseSrvCreatePairWaitHandles(&DosRecord
->ServerEvent
, &DosRecord
->ClientEvent
);
489 if (!NT_SUCCESS(Status
)) goto Cleanup
;
491 /* Return the client event handle */
492 CheckVdmRequest
->WaitObjectForParent
= DosRecord
->ClientEvent
;
494 /* Translate the input structure into a VDM command structure and set it in the DOS record */
495 if (!BaseSrvCopyCommand(CheckVdmRequest
, DosRecord
))
497 /* The only possibility is that an allocation failure occurred */
498 Status
= STATUS_NO_MEMORY
;
502 /* Add the DOS record */
503 InsertHeadList(&ConsoleRecord
->DosListHead
, &DosRecord
->Entry
);
505 if (NewConsoleRecord
)
507 /* Add the console record */
508 InsertTailList(&VDMConsoleListHead
, &ConsoleRecord
->Entry
);
511 CheckVdmRequest
->VDMState
= DosRecord
->State
;
512 Status
= STATUS_SUCCESS
;
516 // TODO: NOT IMPLEMENTED
518 Status
= STATUS_NOT_IMPLEMENTED
;
522 /* Check if it failed */
523 if (!NT_SUCCESS(Status
))
525 /* Free the DOS record */
526 if (DosRecord
!= NULL
)
528 if (DosRecord
->ServerEvent
) NtClose(DosRecord
->ServerEvent
);
529 if (DosRecord
->ClientEvent
)
531 /* Close the remote handle */
532 NtDuplicateObject(CsrGetClientThread()->Process
->ProcessHandle
,
533 DosRecord
->ClientEvent
,
538 DUPLICATE_CLOSE_SOURCE
);
541 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
545 /* Free the console record if it was allocated here */
546 if (NewConsoleRecord
)
548 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
);
549 ConsoleRecord
= NULL
;
553 /* Leave the critical section */
554 RtlLeaveCriticalSection(CriticalSection
);
556 /* Unlock the process */
557 CsrUnlockProcess(ClientProcess
);
562 CSR_API(BaseSrvUpdateVDMEntry
)
565 PBASE_UPDATE_VDM_ENTRY UpdateVdmEntryRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.UpdateVDMEntryRequest
;
566 PRTL_CRITICAL_SECTION CriticalSection
= NULL
;
567 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
568 PVDM_DOS_RECORD DosRecord
= NULL
;
570 CriticalSection
= (UpdateVdmEntryRequest
->BinaryType
!= BINARY_TYPE_SEPARATE_WOW
)
571 ? &DosCriticalSection
572 : &WowCriticalSection
;
574 /* Enter the critical section */
575 RtlEnterCriticalSection(CriticalSection
);
577 /* Check if this is a DOS or WOW VDM */
578 if (UpdateVdmEntryRequest
->BinaryType
!= BINARY_TYPE_SEPARATE_WOW
)
580 if (UpdateVdmEntryRequest
->iTask
!= 0)
582 /* Get the console record using the task ID */
583 Status
= GetConsoleRecordBySessionId(UpdateVdmEntryRequest
->iTask
,
588 /* Get the console record using the console handle */
589 Status
= BaseSrvGetConsoleRecord(UpdateVdmEntryRequest
->ConsoleHandle
,
593 if (!NT_SUCCESS(Status
)) goto Cleanup
;
595 /* Get the primary DOS record */
596 DosRecord
= (PVDM_DOS_RECORD
)CONTAINING_RECORD(ConsoleRecord
->DosListHead
.Flink
,
600 switch (UpdateVdmEntryRequest
->EntryIndex
)
604 /* Close the server event handle, the client will close the client handle */
605 NtClose(DosRecord
->ServerEvent
);
606 DosRecord
->ServerEvent
= DosRecord
->ClientEvent
= NULL
;
608 if (UpdateVdmEntryRequest
->VDMCreationState
& (VDM_UNDO_PARTIAL
| VDM_UNDO_FULL
))
610 /* Remove the DOS record */
611 if (DosRecord
->CommandInfo
) BaseSrvFreeVDMInfo(DosRecord
->CommandInfo
);
612 RemoveEntryList(&DosRecord
->Entry
);
613 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
616 * Since this is an undo, if that was the only DOS record the VDM
617 * won't even start, so the console record should be removed too.
619 if (ConsoleRecord
->DosListHead
.Flink
== &ConsoleRecord
->DosListHead
)
621 RemoveEntryList(&ConsoleRecord
->Entry
);
622 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
);
626 /* It was successful */
627 Status
= STATUS_SUCCESS
;
632 case VdmEntryUpdateProcess
:
634 /* Duplicate the VDM process handle */
635 Status
= NtDuplicateObject(CsrGetClientThread()->Process
->ProcessHandle
,
636 UpdateVdmEntryRequest
->VDMProcessHandle
,
638 &ConsoleRecord
->ProcessHandle
,
641 DUPLICATE_SAME_ATTRIBUTES
| DUPLICATE_SAME_ACCESS
);
642 if (!NT_SUCCESS(Status
)) goto Cleanup
;
644 /* Create a pair of handles to one event object */
645 Status
= BaseSrvCreatePairWaitHandles(&DosRecord
->ServerEvent
,
646 &DosRecord
->ClientEvent
);
647 if (!NT_SUCCESS(Status
)) goto Cleanup
;
649 /* Return the client event handle */
650 UpdateVdmEntryRequest
->WaitObjectForParent
= DosRecord
->ClientEvent
;
655 case VdmEntryUpdateControlCHandler
:
657 // TODO: NOT IMPLEMENTED
658 DPRINT1("BaseSrvUpdateVDMEntry: VdmEntryUpdateControlCHandler not implemented!");
659 Status
= STATUS_NOT_IMPLEMENTED
;
667 Status
= STATUS_INVALID_PARAMETER
;
673 // TODO: NOT IMPLEMENTED
675 Status
= STATUS_NOT_IMPLEMENTED
;
679 /* Leave the critical section */
680 RtlLeaveCriticalSection(CriticalSection
);
685 CSR_API(BaseSrvGetNextVDMCommand
)
687 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
688 return STATUS_NOT_IMPLEMENTED
;
691 CSR_API(BaseSrvExitVDM
)
694 PBASE_EXIT_VDM ExitVdmRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.ExitVDMRequest
;
695 PRTL_CRITICAL_SECTION CriticalSection
= NULL
;
696 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
697 PVDM_DOS_RECORD DosRecord
;
699 CriticalSection
= (ExitVdmRequest
->iWowTask
== 0)
700 ? &DosCriticalSection
701 : &WowCriticalSection
;
703 /* Enter the critical section */
704 RtlEnterCriticalSection(CriticalSection
);
706 if (ExitVdmRequest
->iWowTask
== 0)
708 /* Get the console record */
709 Status
= BaseSrvGetConsoleRecord(ExitVdmRequest
->ConsoleHandle
, &ConsoleRecord
);
710 if (!NT_SUCCESS(Status
)) goto Cleanup
;
712 /* Cleanup the DOS records */
713 while (ConsoleRecord
->DosListHead
.Flink
!= &ConsoleRecord
->DosListHead
)
715 DosRecord
= CONTAINING_RECORD(ConsoleRecord
->DosListHead
.Flink
,
719 /* Set the event and close it */
720 NtSetEvent(DosRecord
->ServerEvent
, NULL
);
721 NtClose(DosRecord
->ServerEvent
);
723 /* Remove the DOS entry */
724 if (DosRecord
->CommandInfo
) BaseSrvFreeVDMInfo(DosRecord
->CommandInfo
);
725 RemoveEntryList(&DosRecord
->Entry
);
726 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
729 if (ConsoleRecord
->CurrentDirs
!= NULL
)
731 /* Free the current directories */
732 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
->CurrentDirs
);
733 ConsoleRecord
->CurrentDirs
= NULL
;
734 ConsoleRecord
->CurDirsLength
= 0;
737 /* Remove the console record */
738 RemoveEntryList(&ConsoleRecord
->Entry
);
739 RtlFreeHeap(BaseSrvHeap
, 0, ConsoleRecord
);
743 // TODO: NOT IMPLEMENTED
745 Status
= STATUS_NOT_IMPLEMENTED
;
749 /* Leave the critical section */
750 RtlLeaveCriticalSection(CriticalSection
);
755 CSR_API(BaseSrvIsFirstVDM
)
757 PBASE_IS_FIRST_VDM IsFirstVDMRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.IsFirstVDMRequest
;
759 /* Return the result */
760 IsFirstVDMRequest
->FirstVDM
= FirstVDM
;
762 /* Clear the first VDM flag */
765 return STATUS_SUCCESS
;
768 CSR_API(BaseSrvGetVDMExitCode
)
771 PBASE_GET_VDM_EXIT_CODE GetVDMExitCodeRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.GetVDMExitCodeRequest
;
772 PLIST_ENTRY i
= NULL
;
773 PVDM_CONSOLE_RECORD ConsoleRecord
= NULL
;
774 PVDM_DOS_RECORD DosRecord
= NULL
;
776 /* Enter the critical section */
777 RtlEnterCriticalSection(&DosCriticalSection
);
779 /* Get the console record */
780 Status
= BaseSrvGetConsoleRecord(GetVDMExitCodeRequest
->ConsoleHandle
, &ConsoleRecord
);
781 if (!NT_SUCCESS(Status
)) goto Cleanup
;
783 /* Search for a DOS record that has the same parent process handle */
784 for (i
= ConsoleRecord
->DosListHead
.Flink
; i
!= &ConsoleRecord
->DosListHead
; i
= i
->Flink
)
786 DosRecord
= CONTAINING_RECORD(i
, VDM_DOS_RECORD
, Entry
);
787 if (DosRecord
->ClientEvent
== GetVDMExitCodeRequest
->hParent
) break;
790 /* Check if no DOS record was found */
791 if (i
== &ConsoleRecord
->DosListHead
)
793 Status
= STATUS_NOT_FOUND
;
797 /* Check if this task is still running */
798 if (DosRecord
->State
== VDM_READY
)
800 GetVDMExitCodeRequest
->ExitCode
= STATUS_PENDING
;
804 /* Return the exit code */
805 GetVDMExitCodeRequest
->ExitCode
= DosRecord
->ExitCode
;
807 /* Since this is a zombie task record, remove it */
808 if (DosRecord
->CommandInfo
) BaseSrvFreeVDMInfo(DosRecord
->CommandInfo
);
809 RemoveEntryList(&DosRecord
->Entry
);
810 RtlFreeHeap(BaseSrvHeap
, 0, DosRecord
);
813 /* Leave the critical section */
814 RtlLeaveCriticalSection(&DosCriticalSection
);
819 CSR_API(BaseSrvSetReenterCount
)
821 NTSTATUS Status
= STATUS_SUCCESS
;
822 PBASE_SET_REENTER_COUNT SetReenterCountRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.SetReenterCountRequest
;
823 PVDM_CONSOLE_RECORD ConsoleRecord
;
825 /* Enter the critical section */
826 RtlEnterCriticalSection(&DosCriticalSection
);
828 /* Get the console record */
829 Status
= BaseSrvGetConsoleRecord(SetReenterCountRequest
->ConsoleHandle
, &ConsoleRecord
);
830 if (!NT_SUCCESS(Status
)) goto Cleanup
;
832 if (SetReenterCountRequest
->fIncDec
== VDM_INC_REENTER_COUNT
) ConsoleRecord
->ReenterCount
++;
833 else if (SetReenterCountRequest
->fIncDec
== VDM_DEC_REENTER_COUNT
)
835 ConsoleRecord
->ReenterCount
--;
836 if (ConsoleRecord
->ServerEvent
!= NULL
) NtSetEvent(ConsoleRecord
->ServerEvent
, NULL
);
838 else Status
= STATUS_INVALID_PARAMETER
;
841 /* Leave the critical section */
842 RtlLeaveCriticalSection(&DosCriticalSection
);
847 CSR_API(BaseSrvSetVDMCurDirs
)
850 PBASE_GETSET_VDM_CURDIRS VDMCurrentDirsRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.VDMCurrentDirsRequest
;
851 PVDM_CONSOLE_RECORD ConsoleRecord
;
854 /* Validate the input buffer */
855 if (!CsrValidateMessageBuffer(ApiMessage
,
856 (PVOID
*)&VDMCurrentDirsRequest
->lpszzCurDirs
,
857 VDMCurrentDirsRequest
->cchCurDirs
,
858 sizeof(*VDMCurrentDirsRequest
->lpszzCurDirs
)))
860 return STATUS_INVALID_PARAMETER
;
863 /* Enter the critical section */
864 RtlEnterCriticalSection(&DosCriticalSection
);
866 /* Find the console record */
867 Status
= BaseSrvGetConsoleRecord(VDMCurrentDirsRequest
->ConsoleHandle
, &ConsoleRecord
);
868 if (!NT_SUCCESS(Status
)) goto Cleanup
;
870 if (ConsoleRecord
->CurrentDirs
== NULL
)
872 /* Allocate memory for the current directory information */
873 Buffer
= RtlAllocateHeap(BaseSrvHeap
,
875 VDMCurrentDirsRequest
->cchCurDirs
);
879 /* Resize the amount of allocated memory */
880 Buffer
= RtlReAllocateHeap(BaseSrvHeap
,
882 ConsoleRecord
->CurrentDirs
,
883 VDMCurrentDirsRequest
->cchCurDirs
);
888 /* Allocation failed */
889 Status
= STATUS_NO_MEMORY
;
893 /* Update the console record */
894 ConsoleRecord
->CurrentDirs
= Buffer
;
895 ConsoleRecord
->CurDirsLength
= VDMCurrentDirsRequest
->cchCurDirs
;
898 RtlMoveMemory(ConsoleRecord
->CurrentDirs
,
899 VDMCurrentDirsRequest
->lpszzCurDirs
,
900 VDMCurrentDirsRequest
->cchCurDirs
);
903 /* Leave the critical section */
904 RtlLeaveCriticalSection(&DosCriticalSection
);
909 CSR_API(BaseSrvGetVDMCurDirs
)
912 PBASE_GETSET_VDM_CURDIRS VDMCurrentDirsRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.VDMCurrentDirsRequest
;
913 PVDM_CONSOLE_RECORD ConsoleRecord
;
915 /* Validate the output buffer */
916 if (!CsrValidateMessageBuffer(ApiMessage
,
917 (PVOID
*)&VDMCurrentDirsRequest
->lpszzCurDirs
,
918 VDMCurrentDirsRequest
->cchCurDirs
,
919 sizeof(*VDMCurrentDirsRequest
->lpszzCurDirs
)))
921 return STATUS_INVALID_PARAMETER
;
924 /* Enter the critical section */
925 RtlEnterCriticalSection(&DosCriticalSection
);
927 /* Find the console record */
928 Status
= BaseSrvGetConsoleRecord(VDMCurrentDirsRequest
->ConsoleHandle
, &ConsoleRecord
);
929 if (!NT_SUCCESS(Status
)) goto Cleanup
;
931 /* Return the actual size of the current directory information */
932 VDMCurrentDirsRequest
->cchCurDirs
= ConsoleRecord
->CurDirsLength
;
934 /* Check if the buffer is large enough */
935 if (VDMCurrentDirsRequest
->cchCurDirs
< ConsoleRecord
->CurDirsLength
)
937 Status
= STATUS_BUFFER_TOO_SMALL
;
942 RtlMoveMemory(VDMCurrentDirsRequest
->lpszzCurDirs
,
943 ConsoleRecord
->CurrentDirs
,
944 ConsoleRecord
->CurDirsLength
);
947 /* Leave the critical section */
948 RtlLeaveCriticalSection(&DosCriticalSection
);
953 CSR_API(BaseSrvBatNotification
)
955 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
956 return STATUS_NOT_IMPLEMENTED
;
959 CSR_API(BaseSrvRegisterWowExec
)
961 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
962 return STATUS_NOT_IMPLEMENTED
;
965 CSR_API(BaseSrvRefreshIniFileMapping
)
967 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
968 return STATUS_NOT_IMPLEMENTED
;