ReactOS  0.4.14-dev-337-gf981a68
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 FASTCALL ConSrvAllocateConsole (PCONSOLE_PROCESS_DATA ProcessData, PHANDLE pInputHandle, PHANDLE pOutputHandle, PHANDLE pErrorHandle, PCONSOLE_START_INFO ConsoleStartInfo)
 
NTSTATUS FASTCALL ConSrvInheritConsole (PCONSOLE_PROCESS_DATA ProcessData, HANDLE ConsoleHandle, BOOL CreateNewHandlesTable, PHANDLE pInputHandle, PHANDLE pOutputHandle, PHANDLE pErrorHandle)
 
VOID FASTCALL ConSrvRemoveConsole (PCONSOLE_PROCESS_DATA ProcessData)
 

Function Documentation

◆ ConSrvAllocateConsole()

NTSTATUS FASTCALL ConSrvAllocateConsole ( PCONSOLE_PROCESS_DATA  ProcessData,
PHANDLE  pInputHandle,
PHANDLE  pOutputHandle,
PHANDLE  pErrorHandle,
PCONSOLE_START_INFO  ConsoleStartInfo 
)

Definition at line 480 of file handle.c.

485 {
487  HANDLE ConsoleHandle;
489 
490  /*
491  * We are about to create a new console. However when ConSrvNewProcess
492  * was called, we didn't know that we wanted to create a new console and
493  * therefore, we by default inherited the handles table from our parent
494  * process. It's only now that we notice that in fact we do not need
495  * them, because we've created a new console and thus we must use it.
496  *
497  * Therefore, free the handles table so that we can recreate
498  * a new one later on.
499  */
500  ConSrvFreeHandlesTable(ProcessData);
501 
502  /* Initialize a new Console owned by this process */
503  Status = ConSrvInitConsole(&ConsoleHandle,
504  &Console,
505  ConsoleStartInfo,
507  if (!NT_SUCCESS(Status))
508  {
509  DPRINT1("Console initialization failed\n");
510  return Status;
511  }
512 
513  /* Assign the new console handle */
514  ProcessData->ConsoleHandle = ConsoleHandle;
515 
516  /* Initialize the handles table */
517  Status = ConSrvInitHandlesTable(ProcessData,
518  Console,
519  pInputHandle,
520  pOutputHandle,
521  pErrorHandle);
522  if (!NT_SUCCESS(Status))
523  {
524  DPRINT1("Failed to initialize the handles table\n");
526  ProcessData->ConsoleHandle = NULL;
527  return Status;
528  }
529 
530  /* Duplicate the Input Event */
532  Console->InputBuffer.ActiveEvent,
533  ProcessData->Process->ProcessHandle,
534  &ProcessData->ConsoleEvent,
535  EVENT_ALL_ACCESS, 0, 0);
536  if (!NT_SUCCESS(Status))
537  {
538  DPRINT1("NtDuplicateObject() failed: %lu\n", Status);
539  ConSrvFreeHandlesTable(ProcessData);
541  ProcessData->ConsoleHandle = NULL;
542  return Status;
543  }
544 
545  /* Insert the process into the processes list of the console */
546  InsertHeadList(&Console->ProcessList, &ProcessData->ConsoleLink);
547 
548  /* Add a reference count because the process is tied to the console */
549  _InterlockedIncrement(&Console->ReferenceCount);
550 
551  /* Update the internal info of the terminal */
553 
554  return STATUS_SUCCESS;
555 }
HANDLE ConsoleHandle
Definition: consrv.h:45
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
LONG NTSTATUS
Definition: precomp.h:26
#define HandleToUlong(h)
Definition: basetsd.h:79
HANDLE UniqueProcess
Definition: compat.h:482
LIST_ENTRY ConsoleLink
Definition: consrv.h:42
long __cdecl _InterlockedIncrement(_Interlocked_operand_ long volatile *_Addend)
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
static NTSTATUS ConSrvInitHandlesTable(IN OUT PCONSOLE_PROCESS_DATA ProcessData, IN PCONSOLE Console, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle)
Definition: handle.c:137
HANDLE ProcessHandle
Definition: csrsrv.h:46
smooth NULL
Definition: ftsmooth.c:416
CLIENT_ID ClientId
Definition: csrsrv.h:38
#define NtCurrentProcess()
Definition: nt_native.h:1657
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
PCSR_PROCESS Process
Definition: consrv.h:43
Status
Definition: gdiplustypes.h:24
static VOID ConSrvFreeHandlesTable(PCONSOLE_PROCESS_DATA ProcessData)
Definition: handle.c:265
HANDLE ConsoleEvent
Definition: consrv.h:62
#define ConioRefreshInternalInfo(Console)
Definition: conio2.h:26
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
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ ConSrvInheritConsole()

NTSTATUS FASTCALL ConSrvInheritConsole ( PCONSOLE_PROCESS_DATA  ProcessData,
HANDLE  ConsoleHandle,
BOOL  CreateNewHandlesTable,
PHANDLE  pInputHandle,
PHANDLE  pOutputHandle,
PHANDLE  pErrorHandle 
)

Definition at line 559 of file handle.c.

565 {
568 
569  /* Validate and lock the console */
571  ConsoleHandle,
573  {
574  // FIXME: Find another status code
575  return STATUS_UNSUCCESSFUL;
576  }
577 
578  /* Inherit the console */
579  ProcessData->ConsoleHandle = ConsoleHandle;
580 
581  if (CreateNewHandlesTable)
582  {
583  /*
584  * We are about to create a new console. However when ConSrvNewProcess
585  * was called, we didn't know that we wanted to create a new console and
586  * therefore, we by default inherited the handles table from our parent
587  * process. It's only now that we notice that in fact we do not need
588  * them, because we've created a new console and thus we must use it.
589  *
590  * Therefore, free the handles table so that we can recreate
591  * a new one later on.
592  */
593  ConSrvFreeHandlesTable(ProcessData);
594 
595  /* Initialize the handles table */
596  Status = ConSrvInitHandlesTable(ProcessData,
597  Console,
598  pInputHandle,
599  pOutputHandle,
600  pErrorHandle);
601  if (!NT_SUCCESS(Status))
602  {
603  DPRINT1("Failed to initialize the handles table\n");
604  ProcessData->ConsoleHandle = NULL;
605  goto Quit;
606  }
607  }
608 
609  /* Duplicate the Input Event */
611  Console->InputBuffer.ActiveEvent,
612  ProcessData->Process->ProcessHandle,
613  &ProcessData->ConsoleEvent,
614  EVENT_ALL_ACCESS, 0, 0);
615  if (!NT_SUCCESS(Status))
616  {
617  DPRINT1("NtDuplicateObject() failed: %lu\n", Status);
618  ConSrvFreeHandlesTable(ProcessData); // NOTE: Always free the handles table.
619  ProcessData->ConsoleHandle = NULL;
620  goto Quit;
621  }
622 
623  /* Insert the process into the processes list of the console */
624  InsertHeadList(&Console->ProcessList, &ProcessData->ConsoleLink);
625 
626  /* Add a reference count because the process is tied to the console */
627  _InterlockedIncrement(&Console->ReferenceCount);
628 
629  /* Update the internal info of the terminal */
631 
633 
634 Quit:
635  /* Unlock the console and return */
637  return Status;
638 }
HANDLE ConsoleHandle
Definition: consrv.h:45
#define TRUE
Definition: types.h:120
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
LONG NTSTATUS
Definition: precomp.h:26
LIST_ENTRY ConsoleLink
Definition: consrv.h:42
long __cdecl _InterlockedIncrement(_Interlocked_operand_ long volatile *_Addend)
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
static NTSTATUS ConSrvInitHandlesTable(IN OUT PCONSOLE_PROCESS_DATA ProcessData, IN PCONSOLE Console, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle)
Definition: handle.c:137
HANDLE ProcessHandle
Definition: csrsrv.h:46
smooth NULL
Definition: ftsmooth.c:416
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
BOOLEAN NTAPI ConDrvValidateConsole(OUT PCONSOLE *Console, IN HANDLE ConsoleHandle, IN CONSOLE_STATE ExpectedState, IN BOOLEAN LockConsole)
Definition: console.c:336
PCSR_PROCESS Process
Definition: consrv.h:43
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
Status
Definition: gdiplustypes.h:24
static VOID ConSrvFreeHandlesTable(PCONSOLE_PROCESS_DATA ProcessData)
Definition: handle.c:265
HANDLE ConsoleEvent
Definition: consrv.h:62
#define ConioRefreshInternalInfo(Console)
Definition: conio2.h:26
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
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
return STATUS_SUCCESS
Definition: btrfs.c:2938

◆ ConSrvRemoveConsole()

VOID FASTCALL 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:68
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)

Referenced by ConSrvDisconnect(), and CSR_API().