ReactOS  0.4.15-dev-1070-ge1a01de
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 (IN OUT PCONSOLE_PROCESS_DATA ProcessData, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle, IN OUT PCONSOLE_INIT_INFO ConsoleInitInfo)
 
NTSTATUS ConSrvInheritConsole (IN OUT PCONSOLE_PROCESS_DATA ProcessData, IN HANDLE ConsoleHandle, IN BOOLEAN CreateNewHandleTable, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle, IN OUT PCONSOLE_START_INFO ConsoleStartInfo)
 
NTSTATUS ConSrvRemoveConsole (IN OUT PCONSOLE_PROCESS_DATA ProcessData)
 

Function Documentation

◆ ConSrvAllocateConsole()

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

Definition at line 968 of file console.c.

974 {
976  HANDLE ConsoleHandle;
978 
979  /*
980  * We are about to create a new console. However when ConSrvNewProcess()
981  * was called, we didn't know that we wanted to create a new console and
982  * therefore, we by default inherited the handle table from our parent
983  * process. It's only now that we notice that in fact we do not need
984  * them, because we've created a new console and thus we must use it.
985  *
986  * Therefore, free the handle table so that we can recreate
987  * a new one later on.
988  */
989  ConSrvFreeHandlesTable(ProcessData);
990 
991  /* Initialize a new Console owned by this process */
992  Status = ConSrvInitConsole(&ConsoleHandle,
993  &Console,
994  ConsoleInitInfo,
995  ProcessData->Process);
996  if (!NT_SUCCESS(Status))
997  {
998  DPRINT1("Console initialization failed\n");
999  return Status;
1000  }
1001 
1002  /* Assign the new console handle */
1003  ProcessData->ConsoleHandle = ConsoleHandle;
1004 
1005  /* Initialize the process handles */
1006  Status = ConSrvInitProcessHandles(ProcessData,
1007  Console,
1008  pInputHandle,
1009  pOutputHandle,
1010  pErrorHandle);
1011  if (!NT_SUCCESS(Status))
1012  {
1013  DPRINT1("Failed to initialize the process handles\n");
1015  ProcessData->ConsoleHandle = NULL;
1016  return Status;
1017  }
1018 
1019  /* Duplicate the Initialization Events */
1021  Console->InitEvents[INIT_SUCCESS],
1022  ProcessData->Process->ProcessHandle,
1023  &ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_SUCCESS],
1024  EVENT_ALL_ACCESS, 0, 0);
1025  if (!NT_SUCCESS(Status))
1026  {
1027  DPRINT1("NtDuplicateObject(InitEvents[INIT_SUCCESS]) failed: %lu\n", Status);
1028  ConSrvFreeHandlesTable(ProcessData);
1030  ProcessData->ConsoleHandle = NULL;
1031  return Status;
1032  }
1033 
1035  Console->InitEvents[INIT_FAILURE],
1036  ProcessData->Process->ProcessHandle,
1037  &ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_FAILURE],
1038  EVENT_ALL_ACCESS, 0, 0);
1039  if (!NT_SUCCESS(Status))
1040  {
1041  DPRINT1("NtDuplicateObject(InitEvents[INIT_FAILURE]) failed: %lu\n", Status);
1042  NtDuplicateObject(ProcessData->Process->ProcessHandle,
1043  ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_SUCCESS],
1044  NULL, NULL, 0, 0, DUPLICATE_CLOSE_SOURCE);
1045  ConSrvFreeHandlesTable(ProcessData);
1047  ProcessData->ConsoleHandle = NULL;
1048  return Status;
1049  }
1050 
1051  /* Duplicate the Input Event */
1053  Console->InputBuffer.ActiveEvent,
1054  ProcessData->Process->ProcessHandle,
1055  &ConsoleInitInfo->ConsoleStartInfo->InputWaitHandle,
1056  EVENT_ALL_ACCESS, 0, 0);
1057  if (!NT_SUCCESS(Status))
1058  {
1059  DPRINT1("NtDuplicateObject(InputWaitHandle) failed: %lu\n", Status);
1060  NtDuplicateObject(ProcessData->Process->ProcessHandle,
1061  ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_FAILURE],
1062  NULL, NULL, 0, 0, DUPLICATE_CLOSE_SOURCE);
1063  NtDuplicateObject(ProcessData->Process->ProcessHandle,
1064  ConsoleInitInfo->ConsoleStartInfo->InitEvents[INIT_SUCCESS],
1065  NULL, NULL, 0, 0, DUPLICATE_CLOSE_SOURCE);
1066  ConSrvFreeHandlesTable(ProcessData);
1068  ProcessData->ConsoleHandle = NULL;
1069  return Status;
1070  }
1071 
1072  /* Mark the process as having a console */
1073  ProcessData->ConsoleApp = TRUE;
1074  ProcessData->Process->Flags |= CsrProcessIsConsoleApp;
1075 
1076  /* Return the console handle to the caller */
1077  ConsoleInitInfo->ConsoleStartInfo->ConsoleHandle = ProcessData->ConsoleHandle;
1078 
1079  /*
1080  * Insert the process into the processes list of the console,
1081  * and set its foreground priority.
1082  */
1083  InsertHeadList(&Console->ProcessList, &ProcessData->ConsoleLink);
1084  ConSrvSetProcessFocus(ProcessData->Process, Console->HasFocus);
1085 
1086  /* Add a reference count because the process is tied to the console */
1087  _InterlockedIncrement(&Console->ReferenceCount);
1088 
1089  /* Update the internal info of the terminal */
1091 
1092  return STATUS_SUCCESS;
1093 }
#define DUPLICATE_CLOSE_SOURCE
#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
VOID ConSrvFreeHandlesTable(IN PCONSOLE_PROCESS_DATA ProcessData)
Definition: handle.c:172
long __cdecl _InterlockedIncrement(_Interlocked_operand_ long volatile *_Addend)
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
smooth NULL
Definition: ftsmooth.c:416
#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:532
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Status
Definition: gdiplustypes.h:24
static NTSTATUS ConSrvInitProcessHandles(IN OUT PCONSOLE_PROCESS_DATA ProcessData, IN PCONSRV_CONSOLE Console, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle)
Definition: console.c:890
VOID NTAPI ConSrvDeleteConsole(PCONSRV_CONSOLE Console)
Definition: console.c:822
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
#define TermRefreshInternalInfo(Console)
Definition: term.h:44
return STATUS_SUCCESS
Definition: btrfs.c:3014
VOID ConSrvSetProcessFocus(IN PCSR_PROCESS CsrProcess, IN BOOLEAN SetForeground)
Definition: console.c:1440

Referenced by CON_API_NOCONSOLE(), and ConSrvConnect().

◆ ConSrvInheritConsole()

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

Definition at line 1096 of file console.c.

1104 {
1107 
1108  /* Validate and lock the console */
1110  ConsoleHandle,
1112  {
1113  // FIXME: Find another status code
1114  return STATUS_UNSUCCESSFUL;
1115  }
1116 
1117  /* Inherit the console */
1118  ProcessData->ConsoleHandle = ConsoleHandle;
1119 
1120  if (CreateNewHandleTable)
1121  {
1122  /*
1123  * We are about to create a new console. However when ConSrvNewProcess()
1124  * was called, we didn't know that we wanted to create a new console and
1125  * therefore, we by default inherited the handle table from our parent
1126  * process. It's only now that we notice that in fact we do not need
1127  * them, because we've created a new console and thus we must use it.
1128  *
1129  * Therefore, free the handle table so that we can recreate
1130  * a new one later on.
1131  */
1132  ConSrvFreeHandlesTable(ProcessData);
1133 
1134  /* Initialize the process handles */
1135  Status = ConSrvInitProcessHandles(ProcessData,
1136  Console,
1137  pInputHandle,
1138  pOutputHandle,
1139  pErrorHandle);
1140  if (!NT_SUCCESS(Status))
1141  {
1142  DPRINT1("Failed to initialize the process handles\n");
1143  ProcessData->ConsoleHandle = NULL;
1144  goto Quit;
1145  }
1146  }
1147 
1148  /* Duplicate the Initialization Events */
1150  Console->InitEvents[INIT_SUCCESS],
1151  ProcessData->Process->ProcessHandle,
1152  &ConsoleStartInfo->InitEvents[INIT_SUCCESS],
1153  EVENT_ALL_ACCESS, 0, 0);
1154  if (!NT_SUCCESS(Status))
1155  {
1156  DPRINT1("NtDuplicateObject(InitEvents[INIT_SUCCESS]) failed: %lu\n", Status);
1157  ConSrvFreeHandlesTable(ProcessData);
1158  ProcessData->ConsoleHandle = NULL;
1159  goto Quit;
1160  }
1161 
1163  Console->InitEvents[INIT_FAILURE],
1164  ProcessData->Process->ProcessHandle,
1165  &ConsoleStartInfo->InitEvents[INIT_FAILURE],
1166  EVENT_ALL_ACCESS, 0, 0);
1167  if (!NT_SUCCESS(Status))
1168  {
1169  DPRINT1("NtDuplicateObject(InitEvents[INIT_FAILURE]) failed: %lu\n", Status);
1170  NtDuplicateObject(ProcessData->Process->ProcessHandle,
1171  ConsoleStartInfo->InitEvents[INIT_SUCCESS],
1172  NULL, NULL, 0, 0, DUPLICATE_CLOSE_SOURCE);
1173  ConSrvFreeHandlesTable(ProcessData);
1174  ProcessData->ConsoleHandle = NULL;
1175  goto Quit;
1176  }
1177 
1178  /* Duplicate the Input Event */
1180  Console->InputBuffer.ActiveEvent,
1181  ProcessData->Process->ProcessHandle,
1182  &ConsoleStartInfo->InputWaitHandle,
1183  EVENT_ALL_ACCESS, 0, 0);
1184  if (!NT_SUCCESS(Status))
1185  {
1186  DPRINT1("NtDuplicateObject(InputWaitHandle) failed: %lu\n", Status);
1187  NtDuplicateObject(ProcessData->Process->ProcessHandle,
1188  ConsoleStartInfo->InitEvents[INIT_FAILURE],
1189  NULL, NULL, 0, 0, DUPLICATE_CLOSE_SOURCE);
1190  NtDuplicateObject(ProcessData->Process->ProcessHandle,
1191  ConsoleStartInfo->InitEvents[INIT_SUCCESS],
1192  NULL, NULL, 0, 0, DUPLICATE_CLOSE_SOURCE);
1193  ConSrvFreeHandlesTable(ProcessData); // NOTE: Always free the handle table.
1194  ProcessData->ConsoleHandle = NULL;
1195  goto Quit;
1196  }
1197 
1198  /* Mark the process as having a console */
1199  ProcessData->ConsoleApp = TRUE;
1200  ProcessData->Process->Flags |= CsrProcessIsConsoleApp;
1201 
1202  /* Return the console handle to the caller */
1203  ConsoleStartInfo->ConsoleHandle = ProcessData->ConsoleHandle;
1204 
1205  /*
1206  * Insert the process into the processes list of the console,
1207  * and set its foreground priority.
1208  */
1209  InsertHeadList(&Console->ProcessList, &ProcessData->ConsoleLink);
1210  ConSrvSetProcessFocus(ProcessData->Process, Console->HasFocus);
1211 
1212  /* Add a reference count because the process is tied to the console */
1213  _InterlockedIncrement(&Console->ReferenceCount);
1214 
1215  /* Update the internal info of the terminal */
1217 
1219 
1220 Quit:
1221  /* Unlock the console and return */
1222  LeaveCriticalSection(&Console->Lock);
1223  return Status;
1224 }
#define DUPLICATE_CLOSE_SOURCE
#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
VOID ConSrvFreeHandlesTable(IN PCONSOLE_PROCESS_DATA ProcessData)
Definition: handle.c:172
long __cdecl _InterlockedIncrement(_Interlocked_operand_ long volatile *_Addend)
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
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:247
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
Status
Definition: gdiplustypes.h:24
static NTSTATUS ConSrvInitProcessHandles(IN OUT PCONSOLE_PROCESS_DATA ProcessData, IN PCONSRV_CONSOLE Console, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle)
Definition: console.c:890
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)
#define TermRefreshInternalInfo(Console)
Definition: term.h:44
return STATUS_SUCCESS
Definition: btrfs.c:3014
VOID ConSrvSetProcessFocus(IN PCSR_PROCESS CsrProcess, IN BOOLEAN SetForeground)
Definition: console.c:1440

Referenced by CON_API_NOCONSOLE(), and ConSrvConnect().

◆ ConSrvRemoveConsole()

NTSTATUS ConSrvRemoveConsole ( IN OUT PCONSOLE_PROCESS_DATA  ProcessData)

Definition at line 1227 of file console.c.

1229 {
1231  PCONSOLE_PROCESS_DATA ConsoleLeaderProcess;
1232 
1233  DPRINT("ConSrvRemoveConsole\n");
1234 
1235  /* Mark the process as not having a console anymore */
1236  ProcessData->ConsoleApp = FALSE;
1237  ProcessData->Process->Flags &= ~CsrProcessIsConsoleApp;
1238 
1239  /* Validate and lock the console */
1241  ProcessData->ConsoleHandle,
1243  {
1244  // FIXME: Find another status code
1245  return STATUS_UNSUCCESSFUL;
1246  }
1247 
1248  DPRINT("ConSrvRemoveConsole - Locking OK\n");
1249 
1250  /* Retrieve the console leader process */
1251  ConsoleLeaderProcess = ConSrvGetConsoleLeaderProcess(Console);
1252 
1253  /* Close all console handles and free the handle table */
1254  ConSrvFreeHandlesTable(ProcessData);
1255 
1256  /* Detach the process from the console */
1257  ProcessData->ConsoleHandle = NULL;
1258 
1259  /* Remove the process from the console's list of processes */
1260  RemoveEntryList(&ProcessData->ConsoleLink);
1261 
1262  /* Check whether the console should send a last close notification */
1263  if (Console->NotifyLastClose)
1264  {
1265  /* If we are removing the process which wants the last close notification... */
1266  if (ProcessData == Console->NotifiedLastCloseProcess)
1267  {
1268  /* ... just reset the flag and the pointer... */
1269  Console->NotifyLastClose = FALSE;
1270  Console->NotifiedLastCloseProcess = NULL;
1271  }
1272  /*
1273  * ... otherwise, if we are removing the console leader process
1274  * (that cannot be the process wanting the notification, because
1275  * the previous case already dealt with it)...
1276  */
1277  else if (ProcessData == ConsoleLeaderProcess)
1278  {
1279  /*
1280  * ... reset the flag first (so that we avoid multiple notifications)
1281  * and then send the last close notification.
1282  */
1283  Console->NotifyLastClose = FALSE;
1284  ConSrvConsoleCtrlEvent(CTRL_LAST_CLOSE_EVENT, Console->NotifiedLastCloseProcess);
1285 
1286  /* Only now, reset the pointer */
1287  Console->NotifiedLastCloseProcess = NULL;
1288  }
1289  }
1290 
1291  /* Update the internal info of the terminal */
1293 
1294  /* Release the console */
1295  DPRINT("ConSrvRemoveConsole - Decrement Console->ReferenceCount = %lu\n", Console->ReferenceCount);
1297 
1298  return STATUS_SUCCESS;
1299 }
PCONSOLE_PROCESS_DATA NTAPI ConSrvGetConsoleLeaderProcess(IN PCONSRV_CONSOLE Console)
Definition: console.c:1363
#define TRUE
Definition: types.h:120
NTSTATUS ConSrvConsoleCtrlEvent(IN ULONG CtrlEvent, IN PCONSOLE_PROCESS_DATA ProcessData)
Definition: console.c:1356
VOID ConSrvReleaseConsole(IN PCONSRV_CONSOLE Console, IN BOOLEAN IsConsoleLocked)
Definition: console.c:316
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
VOID ConSrvFreeHandlesTable(IN PCONSOLE_PROCESS_DATA ProcessData)
Definition: handle.c:172
#define FALSE
Definition: types.h:117
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:247
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define CTRL_LAST_CLOSE_EVENT
Definition: wincon.h:71
CConsole Console
#define TermRefreshInternalInfo(Console)
Definition: term.h:44
return STATUS_SUCCESS
Definition: btrfs.c:3014

Referenced by CON_API_NOCONSOLE(), and ConSrvDisconnect().