ReactOS  0.4.14-dev-999-g61c8d34
procinit.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

NTSTATUS ConSrvAllocateConsole (PCONSOLE_PROCESS_DATA ProcessData, PHANDLE pInputHandle, PHANDLE pOutputHandle, PHANDLE pErrorHandle, PCONSOLE_INIT_INFO ConsoleInitInfo)
 
NTSTATUS ConSrvInheritConsole (PCONSOLE_PROCESS_DATA ProcessData, HANDLE ConsoleHandle, BOOLEAN CreateNewHandlesTable, PHANDLE pInputHandle, PHANDLE pOutputHandle, PHANDLE pErrorHandle, PCONSOLE_START_INFO ConsoleStartInfo)
 
NTSTATUS ConSrvRemoveConsole (PCONSOLE_PROCESS_DATA ProcessData)
 

Function Documentation

◆ ConSrvAllocateConsole()

NTSTATUS ConSrvAllocateConsole ( PCONSOLE_PROCESS_DATA  ProcessData,
PHANDLE  pInputHandle,
PHANDLE  pOutputHandle,
PHANDLE  pErrorHandle,
PCONSOLE_INIT_INFO  ConsoleInitInfo 
)

Definition at line 474 of file handle.c.

479 {
481  HANDLE ConsoleHandle;
483 
484  /*
485  * We are about to create a new console. However when ConSrvNewProcess
486  * was called, we didn't know that we wanted to create a new console and
487  * therefore, we by default inherited the handles table from our parent
488  * process. It's only now that we notice that in fact we do not need
489  * them, because we've created a new console and thus we must use it.
490  *
491  * Therefore, free the handles table so that we can recreate
492  * a new one later on.
493  */
494  ConSrvFreeHandlesTable(ProcessData);
495 
496  /* Initialize a new Console owned by this process */
497  DPRINT("Initialization of console '%S' for process '%S' on desktop '%S'\n",
498  ConsoleInitInfo->ConsoleTitle ? ConsoleInitInfo->ConsoleTitle : L"n/a",
499  ConsoleInitInfo->AppName ? ConsoleInitInfo->AppName : L"n/a",
500  ConsoleInitInfo->Desktop ? ConsoleInitInfo->Desktop : L"n/a");
501  Status = ConSrvInitConsole(&ConsoleHandle,
502  &Console,
503  ConsoleInitInfo,
504  ProcessData->Process);
505  if (!NT_SUCCESS(Status))
506  {
507  DPRINT1("Console initialization failed\n");
508  return Status;
509  }
510 
511  /* Assign the new console handle */
512  ProcessData->ConsoleHandle = ConsoleHandle;
513 
514  /* Initialize the handles table */
515  Status = ConSrvInitHandlesTable(ProcessData,
516  Console,
517  pInputHandle,
518  pOutputHandle,
519  pErrorHandle);
520  if (!NT_SUCCESS(Status))
521  {
522  DPRINT1("Failed to initialize the handles table\n");
524  ProcessData->ConsoleHandle = NULL;
525  return Status;
526  }
527 
528  /* Duplicate the Initialization Events */
530  Console->InitEvents[INIT_SUCCESS],
531  ProcessData->Process->ProcessHandle,
532  &ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_SUCCESS],
533  EVENT_ALL_ACCESS, 0, 0);
534  if (!NT_SUCCESS(Status))
535  {
536  DPRINT1("NtDuplicateObject(InitEvents[INIT_SUCCESS]) failed: %lu\n", Status);
537  ConSrvFreeHandlesTable(ProcessData);
539  ProcessData->ConsoleHandle = NULL;
540  return Status;
541  }
542 
544  Console->InitEvents[INIT_FAILURE],
545  ProcessData->Process->ProcessHandle,
546  &ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_FAILURE],
547  EVENT_ALL_ACCESS, 0, 0);
548  if (!NT_SUCCESS(Status))
549  {
550  DPRINT1("NtDuplicateObject(InitEvents[INIT_FAILURE]) failed: %lu\n", Status);
552  ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_SUCCESS],
554  ConSrvFreeHandlesTable(ProcessData);
556  ProcessData->ConsoleHandle = NULL;
557  return Status;
558  }
559 
560  /* Duplicate the Input Event */
562  Console->InputBuffer.ActiveEvent,
563  ProcessData->Process->ProcessHandle,
564  &ConsoleInitInfo->ConsoleStartInfo->InputWaitHandle,
565  EVENT_ALL_ACCESS, 0, 0);
566  if (!NT_SUCCESS(Status))
567  {
568  DPRINT1("NtDuplicateObject(InputWaitHandle) failed: %lu\n", Status);
570  ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_FAILURE],
573  ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_SUCCESS],
575  ConSrvFreeHandlesTable(ProcessData);
577  ProcessData->ConsoleHandle = NULL;
578  return Status;
579  }
580 
581  /* Mark the process as having a console */
582  ProcessData->ConsoleApp = TRUE;
583  ProcessData->Process->Flags |= CsrProcessIsConsoleApp;
584 
585  /* Return the console handle to the caller */
586  ConsoleInitInfo->ConsoleStartInfo->ConsoleHandle = ProcessData->ConsoleHandle;
587 
588  /*
589  * Insert the process into the processes list of the console,
590  * and set its foreground priority.
591  */
592  InsertHeadList(&Console->ProcessList, &ProcessData->ConsoleLink);
593  ConSrvSetProcessFocus(ProcessData->Process, Console->HasFocus);
594 
595  /* Add a reference count because the process is tied to the console */
596  _InterlockedIncrement(&Console->ReferenceCount);
597 
598  /* Update the internal info of the terminal */
600 
601  return STATUS_SUCCESS;
602 }
HANDLE ConsoleHandle
Definition: consrv.h:45
PWCHAR Desktop
Definition: console.h:19
PCONSOLE_START_INFO ConsoleStartInfo
Definition: console.h:13
#define DUPLICATE_CLOSE_SOURCE
#define TRUE
Definition: types.h:120
static NTSTATUS ConSrvInitHandlesTable(IN OUT PCONSOLE_PROCESS_DATA ProcessData, IN PCONSOLE Console, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle)
Definition: handle.c:127
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
LONG NTSTATUS
Definition: precomp.h:26
HANDLE InitEvents[MAX_INIT_EVENTS]
Definition: conmsg.h:175
#define PCONSRV_CONSOLE
Definition: conio.h:27
PWCHAR AppName
Definition: console.h:21
LIST_ENTRY ConsoleLink
Definition: consrv.h:42
long __cdecl _InterlockedIncrement(_Interlocked_operand_ long volatile *_Addend)
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
HANDLE ProcessHandle
Definition: csrsrv.h:46
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define NtCurrentProcess()
Definition: nt_native.h:1657
PWCHAR ConsoleTitle
Definition: console.h:17
NTSTATUS NTAPI ConSrvInitConsole(OUT PHANDLE NewConsoleHandle, OUT PCONSRV_CONSOLE *NewConsole, IN OUT PCONSOLE_INIT_INFO ConsoleInitInfo, IN PCSR_PROCESS ConsoleLeaderProcess)
Definition: console.c:511
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
BOOLEAN ConsoleApp
Definition: consrv.h:46
HANDLE ConsoleHandle
Definition: conmsg.h:170
PCSR_PROCESS Process
Definition: consrv.h:43
static const WCHAR L[]
Definition: oid.c:1250
Status
Definition: gdiplustypes.h:24
HANDLE InputWaitHandle
Definition: conmsg.h:171
VOID NTAPI ConSrvDeleteConsole(PCONSRV_CONSOLE Console)
Definition: console.c:771
CConsole Console
#define DPRINT1
Definition: precomp.h:8
NTSTATUS NTAPI NtDuplicateObject(IN HANDLE SourceProcessHandle, IN HANDLE SourceHandle, IN HANDLE TargetProcessHandle OPTIONAL, OUT PHANDLE TargetHandle OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG HandleAttributes, IN ULONG Options)
Definition: obhandle.c:3407
ULONG Flags
Definition: csrsrv.h:48
#define TermRefreshInternalInfo(Console)
Definition: term.h:44
return STATUS_SUCCESS
Definition: btrfs.c:2938
static VOID ConSrvFreeHandlesTable(PCONSOLE_PROCESS_DATA ProcessData)
VOID ConSrvSetProcessFocus(IN PCSR_PROCESS CsrProcess, IN BOOLEAN SetForeground)
Definition: console.c:944

Referenced by ConSrvConnect(), and CSR_API().

◆ ConSrvInheritConsole()

NTSTATUS ConSrvInheritConsole ( PCONSOLE_PROCESS_DATA  ProcessData,
HANDLE  ConsoleHandle,
BOOLEAN  CreateNewHandlesTable,
PHANDLE  pInputHandle,
PHANDLE  pOutputHandle,
PHANDLE  pErrorHandle,
PCONSOLE_START_INFO  ConsoleStartInfo 
)

Definition at line 605 of file handle.c.

612 {
615 
616  /* Validate and lock the console */
618  ConsoleHandle,
620  {
621  // FIXME: Find another status code
622  return STATUS_UNSUCCESSFUL;
623  }
624 
625  /* Inherit the console */
626  ProcessData->ConsoleHandle = ConsoleHandle;
627 
628  if (CreateNewHandlesTable)
629  {
630  /*
631  * We are about to create a new console. However when ConSrvNewProcess
632  * was called, we didn't know that we wanted to create a new console and
633  * therefore, we by default inherited the handles table from our parent
634  * process. It's only now that we notice that in fact we do not need
635  * them, because we've created a new console and thus we must use it.
636  *
637  * Therefore, free the handles table so that we can recreate
638  * a new one later on.
639  */
640  ConSrvFreeHandlesTable(ProcessData);
641 
642  /* Initialize the handles table */
643  Status = ConSrvInitHandlesTable(ProcessData,
644  Console,
645  pInputHandle,
646  pOutputHandle,
647  pErrorHandle);
648  if (!NT_SUCCESS(Status))
649  {
650  DPRINT1("Failed to initialize the handles table\n");
651  ProcessData->ConsoleHandle = NULL;
652  goto Quit;
653  }
654  }
655 
656  /* Duplicate the Initialization Events */
658  Console->InitEvents[INIT_SUCCESS],
659  ProcessData->Process->ProcessHandle,
660  &ConsoleStartInfo->InitEvents[INIT_SUCCESS],
661  EVENT_ALL_ACCESS, 0, 0);
662  if (!NT_SUCCESS(Status))
663  {
664  DPRINT1("NtDuplicateObject(InitEvents[INIT_SUCCESS]) failed: %lu\n", Status);
665  ConSrvFreeHandlesTable(ProcessData);
666  ProcessData->ConsoleHandle = NULL;
667  goto Quit;
668  }
669 
671  Console->InitEvents[INIT_FAILURE],
672  ProcessData->Process->ProcessHandle,
673  &ConsoleStartInfo->InitEvents[INIT_FAILURE],
674  EVENT_ALL_ACCESS, 0, 0);
675  if (!NT_SUCCESS(Status))
676  {
677  DPRINT1("NtDuplicateObject(InitEvents[INIT_FAILURE]) failed: %lu\n", Status);
679  ConsoleStartInfo->InitEvents[INIT_SUCCESS],
681  ConSrvFreeHandlesTable(ProcessData);
682  ProcessData->ConsoleHandle = NULL;
683  goto Quit;
684  }
685 
686  /* Duplicate the Input Event */
688  Console->InputBuffer.ActiveEvent,
689  ProcessData->Process->ProcessHandle,
690  &ConsoleStartInfo->InputWaitHandle,
691  EVENT_ALL_ACCESS, 0, 0);
692  if (!NT_SUCCESS(Status))
693  {
694  DPRINT1("NtDuplicateObject(InputWaitHandle) failed: %lu\n", Status);
696  ConsoleStartInfo->InitEvents[INIT_FAILURE],
699  ConsoleStartInfo->InitEvents[INIT_SUCCESS],
701  ConSrvFreeHandlesTable(ProcessData); // NOTE: Always free the handles table.
702  ProcessData->ConsoleHandle = NULL;
703  goto Quit;
704  }
705 
706  /* Mark the process as having a console */
707  ProcessData->ConsoleApp = TRUE;
708  ProcessData->Process->Flags |= CsrProcessIsConsoleApp;
709 
710  /* Return the console handle to the caller */
711  ConsoleStartInfo->ConsoleHandle = ProcessData->ConsoleHandle;
712 
713  /*
714  * Insert the process into the processes list of the console,
715  * and set its foreground priority.
716  */
717  InsertHeadList(&Console->ProcessList, &ProcessData->ConsoleLink);
718  ConSrvSetProcessFocus(ProcessData->Process, Console->HasFocus);
719 
720  /* Add a reference count because the process is tied to the console */
721  _InterlockedIncrement(&Console->ReferenceCount);
722 
723  /* Update the internal info of the terminal */
725 
727 
728 Quit:
729  /* Unlock the console and return */
731  return Status;
732 }
HANDLE ConsoleHandle
Definition: consrv.h:45
#define DUPLICATE_CLOSE_SOURCE
#define TRUE
Definition: types.h:120
static NTSTATUS ConSrvInitHandlesTable(IN OUT PCONSOLE_PROCESS_DATA ProcessData, IN PCONSOLE Console, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle)
Definition: handle.c:127
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
LONG NTSTATUS
Definition: precomp.h:26
HANDLE InitEvents[MAX_INIT_EVENTS]
Definition: conmsg.h:175
LIST_ENTRY ConsoleLink
Definition: consrv.h:42
long __cdecl _InterlockedIncrement(_Interlocked_operand_ long volatile *_Addend)
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
HANDLE ProcessHandle
Definition: csrsrv.h:46
smooth NULL
Definition: ftsmooth.c:416
#define NtCurrentProcess()
Definition: nt_native.h:1657
BOOLEAN NTAPI ConSrvValidateConsole(OUT PCONSRV_CONSOLE *Console, IN HANDLE ConsoleHandle, IN CONSOLE_STATE ExpectedState, IN BOOLEAN LockConsole)
Definition: console.c:167
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
BOOLEAN ConsoleApp
Definition: consrv.h:46
HANDLE ConsoleHandle
Definition: conmsg.h:170
PCSR_PROCESS Process
Definition: consrv.h:43
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
Status
Definition: gdiplustypes.h:24
HANDLE InputWaitHandle
Definition: conmsg.h:171
CConsole Console
#define DPRINT1
Definition: precomp.h:8
NTSTATUS NTAPI NtDuplicateObject(IN HANDLE SourceProcessHandle, IN HANDLE SourceHandle, IN HANDLE TargetProcessHandle OPTIONAL, OUT PHANDLE TargetHandle OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG HandleAttributes, IN ULONG Options)
Definition: obhandle.c:3407
ULONG Flags
Definition: csrsrv.h:48
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
#define TermRefreshInternalInfo(Console)
Definition: term.h:44
return STATUS_SUCCESS
Definition: btrfs.c:2938
static VOID ConSrvFreeHandlesTable(PCONSOLE_PROCESS_DATA ProcessData)
VOID ConSrvSetProcessFocus(IN PCSR_PROCESS CsrProcess, IN BOOLEAN SetForeground)
Definition: console.c:944

Referenced by ConSrvConnect(), and CSR_API().

◆ ConSrvRemoveConsole()

NTSTATUS ConSrvRemoveConsole ( PCONSOLE_PROCESS_DATA  ProcessData)

Definition at line 735 of file handle.c.

736 {
738  PCONSOLE_PROCESS_DATA ConsoleLeaderProcess;
739 
740  DPRINT("ConSrvRemoveConsole\n");
741 
742  /* Mark the process as not having a console anymore */
743  ProcessData->ConsoleApp = FALSE;
744  ProcessData->Process->Flags &= ~CsrProcessIsConsoleApp;
745 
746  /* Validate and lock the console */
748  ProcessData->ConsoleHandle,
750  {
751  // FIXME: Find another status code
752  return STATUS_UNSUCCESSFUL;
753  }
754 
755  DPRINT("ConSrvRemoveConsole - Locking OK\n");
756 
757  /* Retrieve the console leader process */
758  ConsoleLeaderProcess = ConSrvGetConsoleLeaderProcess(Console);
759 
760  /* Close all console handles and free the handles table */
761  ConSrvFreeHandlesTable(ProcessData);
762 
763  /* Detach the process from the console */
764  ProcessData->ConsoleHandle = NULL;
765 
766  /* Remove the process from the console's list of processes */
767  RemoveEntryList(&ProcessData->ConsoleLink);
768 
769  /* Check whether the console should send a last close notification */
770  if (Console->NotifyLastClose)
771  {
772  /* If we are removing the process which wants the last close notification... */
773  if (ProcessData == Console->NotifiedLastCloseProcess)
774  {
775  /* ... just reset the flag and the pointer... */
776  Console->NotifyLastClose = FALSE;
777  Console->NotifiedLastCloseProcess = NULL;
778  }
779  /*
780  * ... otherwise, if we are removing the console leader process
781  * (that cannot be the process wanting the notification, because
782  * the previous case already dealt with it)...
783  */
784  else if (ProcessData == ConsoleLeaderProcess)
785  {
786  /*
787  * ... reset the flag first (so that we avoid multiple notifications)
788  * and then send the last close notification.
789  */
790  Console->NotifyLastClose = FALSE;
791  ConSrvConsoleCtrlEvent(CTRL_LAST_CLOSE_EVENT, Console->NotifiedLastCloseProcess);
792 
793  /* Only now, reset the pointer */
794  Console->NotifiedLastCloseProcess = NULL;
795  }
796  }
797 
798  /* Update the internal info of the terminal */
800 
801  /* Release the console */
802  DPRINT("ConSrvRemoveConsole - Decrement Console->ReferenceCount = %lu\n", Console->ReferenceCount);
804 
805  return STATUS_SUCCESS;
806 }
PCONSOLE_PROCESS_DATA NTAPI ConSrvGetConsoleLeaderProcess(IN PCONSRV_CONSOLE Console)
Definition: console.c:867
HANDLE ConsoleHandle
Definition: consrv.h:45
#define TRUE
Definition: types.h:120
NTSTATUS ConSrvConsoleCtrlEvent(IN ULONG CtrlEvent, IN PCONSOLE_PROCESS_DATA ProcessData)
Definition: console.c:860
VOID ConSrvReleaseConsole(IN PCONSRV_CONSOLE Console, IN BOOLEAN IsConsoleLocked)
Definition: console.c:296
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
LIST_ENTRY ConsoleLink
Definition: consrv.h:42
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
BOOLEAN NTAPI ConSrvValidateConsole(OUT PCONSRV_CONSOLE *Console, IN HANDLE ConsoleHandle, IN CONSOLE_STATE ExpectedState, IN BOOLEAN LockConsole)
Definition: console.c:167
BOOLEAN ConsoleApp
Definition: consrv.h:46
PCSR_PROCESS Process
Definition: consrv.h:43
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define CTRL_LAST_CLOSE_EVENT
Definition: wincon.h:71
CConsole Console
ULONG Flags
Definition: csrsrv.h:48
#define TermRefreshInternalInfo(Console)
Definition: term.h:44
return STATUS_SUCCESS
Definition: btrfs.c:2938
static VOID ConSrvFreeHandlesTable(PCONSOLE_PROCESS_DATA ProcessData)