Author: hbelusca
Date: Wed Jan 16 22:25:12 2013
New Revision: 58184
URL:
http://svn.reactos.org/svn/reactos?rev=58184&view=rev
Log:
[CONSRV]
Introduce new helper functions Win32CsrInitHandlesTable and Win32CsrAllocateConsole and
use them in many places (in ConsoleConnect, SrvAllocConsole and SrvAttachConsole).
Modified:
branches/ros-csrss/dll/win32/kernel32/client/console/console.c
branches/ros-csrss/win32ss/user/consrv/coninput.c
branches/ros-csrss/win32ss/user/consrv/console.c
branches/ros-csrss/win32ss/user/consrv/consrv.h
branches/ros-csrss/win32ss/user/consrv/handle.c
Modified: branches/ros-csrss/dll/win32/kernel32/client/console/console.c
URL:
http://svn.reactos.org/svn/reactos/branches/ros-csrss/dll/win32/kernel32/cl…
==============================================================================
--- branches/ros-csrss/dll/win32/kernel32/client/console/console.c [iso-8859-1]
(original)
+++ branches/ros-csrss/dll/win32/kernel32/client/console/console.c [iso-8859-1] Wed Jan 16
22:25:12 2013
@@ -210,7 +210,7 @@
DeviceNameInfo = RtlIsDosDeviceName_U(pszName);
if (DeviceNameInfo != 0)
{
- ConsoleName = (LPCWSTR)((ULONG_PTR)ConsoleName + (ULONG_PTR)((DeviceNameInfo
>> 16) & 0xFFFF));
+ ConsoleName = (LPCWSTR)((ULONG_PTR)ConsoleName + ((DeviceNameInfo >> 16)
& 0xFFFF));
}
/* Return a standard console "file" name according to what we passed in
parameters */
Modified: branches/ros-csrss/win32ss/user/consrv/coninput.c
URL:
http://svn.reactos.org/svn/reactos/branches/ros-csrss/win32ss/user/consrv/c…
==============================================================================
--- branches/ros-csrss/win32ss/user/consrv/coninput.c [iso-8859-1] (original)
+++ branches/ros-csrss/win32ss/user/consrv/coninput.c [iso-8859-1] Wed Jan 16 22:25:12
2013
@@ -255,7 +255,7 @@
{
current = CONTAINING_RECORD(current_entry, CONSOLE_PROCESS_DATA,
ConsoleLink);
current_entry = current_entry->Flink;
- ConioConsoleCtrlEvent((DWORD)CTRL_C_EVENT, current);
+ ConioConsoleCtrlEvent(CTRL_C_EVENT, current);
}
if (Console->LineBuffer && !Console->LineComplete)
{
Modified: branches/ros-csrss/win32ss/user/consrv/console.c
URL:
http://svn.reactos.org/svn/reactos/branches/ros-csrss/win32ss/user/consrv/c…
==============================================================================
--- branches/ros-csrss/win32ss/user/consrv/console.c [iso-8859-1] (original)
+++ branches/ros-csrss/win32ss/user/consrv/console.c [iso-8859-1] Wed Jan 16 22:25:12
2013
@@ -308,7 +308,6 @@
/** This comes from ConsoleConnect!! **/
DPRINT1("SrvAllocConsole - Checkpoint 1\n");
-#if 0000
/*
* We are about to create a new console. However when ConsoleNewProcess
* was called, we didn't know that we wanted to create a new console and
@@ -321,13 +320,17 @@
*/
Win32CsrReleaseConsole(ProcessData);
// Win32CsrFreeHandlesTable(ProcessData);
-#endif
/* Initialize a new Console owned by the Console Leader Process */
- Status = CsrInitConsole(&ProcessData->Console,
AllocConsoleRequest->ShowCmd, ConsoleLeader);
+ Status = Win32CsrAllocateConsole(ProcessData,
+ &AllocConsoleRequest->InputHandle,
+ &AllocConsoleRequest->OutputHandle,
+ &AllocConsoleRequest->ErrorHandle,
+ AllocConsoleRequest->ShowCmd,
+ ConsoleLeader);
if (!NT_SUCCESS(Status))
{
- DPRINT1("Console initialization failed\n");
+ DPRINT1("Console allocation failed\n");
return Status;
}
@@ -336,75 +339,6 @@
/* Insert the process into the processes list of the console */
InsertHeadList(&ProcessData->Console->ProcessList,
&ProcessData->ConsoleLink);
-
- /* Return it to the caller */
- AllocConsoleRequest->Console = ProcessData->Console;
-
-
- /*
- * Create a new handle table - Insert the IO handles
- */
-
- RtlEnterCriticalSection(&ProcessData->HandleTableLock);
-
- /* Insert the Input handle */
- Status = Win32CsrInsertObject(ProcessData,
- &AllocConsoleRequest->InputHandle,
- &ProcessData->Console->Header,
- GENERIC_READ | GENERIC_WRITE,
- TRUE,
- FILE_SHARE_READ | FILE_SHARE_WRITE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Failed to insert the input handle\n");
- RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
- Win32CsrReleaseConsole(ProcessData);
- // ConioDeleteConsole(ProcessData->Console);
- // ProcessData->Console = NULL;
- return Status;
- }
-
- /* Insert the Output handle */
- Status = Win32CsrInsertObject(ProcessData,
- &AllocConsoleRequest->OutputHandle,
-
&ProcessData->Console->ActiveBuffer->Header,
- GENERIC_READ | GENERIC_WRITE,
- TRUE,
- FILE_SHARE_READ | FILE_SHARE_WRITE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Failed to insert the output handle\n");
- RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
- Win32CsrReleaseConsole(ProcessData);
- // Win32CsrReleaseObject(ProcessData,
- // AllocConsoleRequest->InputHandle);
- // ConioDeleteConsole(ProcessData->Console);
- // ProcessData->Console = NULL;
- return Status;
- }
-
- /* Insert the Error handle */
- Status = Win32CsrInsertObject(ProcessData,
- &AllocConsoleRequest->ErrorHandle,
-
&ProcessData->Console->ActiveBuffer->Header,
- GENERIC_READ | GENERIC_WRITE,
- TRUE,
- FILE_SHARE_READ | FILE_SHARE_WRITE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Failed to insert the error handle\n");
- RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
- Win32CsrReleaseConsole(ProcessData);
- // Win32CsrReleaseObject(ProcessData,
- // AllocConsoleRequest->OutputHandle);
- // Win32CsrReleaseObject(ProcessData,
- // AllocConsoleRequest->InputHandle);
- // ConioDeleteConsole(ProcessData->Console);
- // ProcessData->Console = NULL;
- return Status;
- }
-
- RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
/* Duplicate the Event */
Status = NtDuplicateObject(NtCurrentProcess(),
@@ -416,19 +350,12 @@
{
DPRINT1("NtDuplicateObject() failed: %lu\n", Status);
Win32CsrReleaseConsole(ProcessData);
- // if (NewConsole)
- // {
- // Win32CsrReleaseObject(ProcessData,
- // AllocConsoleRequest->ErrorHandle);
- // Win32CsrReleaseObject(ProcessData,
- // AllocConsoleRequest->OutputHandle);
- // Win32CsrReleaseObject(ProcessData,
- // AllocConsoleRequest->InputHandle);
- // }
- // ConioDeleteConsole(ProcessData->Console); // FIXME: Just release the
console ?
- // ProcessData->Console = NULL;
return Status;
}
+
+ /* Return it to the caller */
+ AllocConsoleRequest->Console = ProcessData->Console;
+
/* Input Wait Handle */
AllocConsoleRequest->InputWaitHandle = ProcessData->ConsoleEvent;
@@ -493,6 +420,19 @@
SourceProcessData = ConsoleGetPerProcessData(SourceProcess);
/*
+ * We are about to create a new console. However when ConsoleNewProcess
+ * was called, we didn't know that we wanted to create a new console and
+ * therefore, we by default inherited the handles table from our parent
+ * process. It's only now that we notice that in fact we do not need
+ * them, because we've created a new console and thus we must use it.
+ *
+ * Therefore, free the console we can have and our handles table,
+ * and recreate a new one later on.
+ */
+ Win32CsrReleaseConsole(TargetProcessData);
+ // Win32CsrFreeHandlesTable(TargetProcessData);
+
+ /*
* Inherit the console from the parent,
* if any, otherwise return an error.
*/
@@ -504,11 +444,18 @@
}
TargetProcessData->Console = SourceProcessData->Console;
- DPRINT1("SrvAttachConsole - Copy the handle table (1)\n");
- Status = Win32CsrInheritHandlesTable(SourceProcessData, TargetProcessData);
- DPRINT1("SrvAttachConsole - Copy the handle table (2)\n");
+ /// REMARK: This code comes from Win32CsrAllocateConsole.
+ /* Initialize the handles table */
+ Status = Win32CsrInitHandlesTable(TargetProcessData,
+ &AttachConsoleRequest->InputHandle,
+ &AttachConsoleRequest->OutputHandle,
+ &AttachConsoleRequest->ErrorHandle);
if (!NT_SUCCESS(Status))
{
+ DPRINT1("Failed to initialize the handles table\n");
+
+ // Win32CsrReleaseConsole(TargetProcessData);
+ TargetProcessData->Console = NULL;
goto Quit;
}
@@ -521,9 +468,6 @@
/* Insert the process into the processes list of the console */
InsertHeadList(&TargetProcessData->Console->ProcessList,
&TargetProcessData->ConsoleLink);
-
- /* Return it to the caller */
- AttachConsoleRequest->Console = TargetProcessData->Console;
/** Here, we inherited the console handles from the "source" process,
** so no need to reinitialize the handles table. **/
@@ -540,21 +484,12 @@
{
DPRINT1("NtDuplicateObject() failed: %lu\n", Status);
Win32CsrReleaseConsole(TargetProcessData);
-// #if 0
- // if (NewConsole)
- // {
- // Win32CsrReleaseObject(TargetProcessData,
- // AttachConsoleRequest->ErrorHandle);
- // Win32CsrReleaseObject(TargetProcessData,
- // AttachConsoleRequest->OutputHandle);
- // Win32CsrReleaseObject(TargetProcessData,
- // AttachConsoleRequest->InputHandle);
- // }
-// #endif
- // ConioDeleteConsole(TargetProcessData->Console); // FIXME: Just release the
console ?
- // TargetProcessData->Console = NULL;
goto Quit;
}
+
+ /* Return it to the caller */
+ AttachConsoleRequest->Console = TargetProcessData->Console;
+
/* Input Wait Handle */
AttachConsoleRequest->InputWaitHandle = TargetProcessData->ConsoleEvent;
Modified: branches/ros-csrss/win32ss/user/consrv/consrv.h
URL:
http://svn.reactos.org/svn/reactos/branches/ros-csrss/win32ss/user/consrv/c…
==============================================================================
--- branches/ros-csrss/win32ss/user/consrv/consrv.h [iso-8859-1] (original)
+++ branches/ros-csrss/win32ss/user/consrv/consrv.h [iso-8859-1] Wed Jan 16 22:25:12 2013
@@ -140,6 +140,10 @@
CSR_API(SrvDuplicateHandle);
/// CSR_API(CsrGetInputWaitHandle);
+NTSTATUS FASTCALL Win32CsrInitHandlesTable(IN OUT PCONSOLE_PROCESS_DATA ProcessData,
+ OUT PHANDLE pInputHandle,
+ OUT PHANDLE pOutputHandle,
+ OUT PHANDLE pErrorHandle);
NTSTATUS FASTCALL Win32CsrInheritHandlesTable(IN PCONSOLE_PROCESS_DATA
SourceProcessData,
IN PCONSOLE_PROCESS_DATA
TargetProcessData);
VOID FASTCALL Win32CsrFreeHandlesTable(PCONSOLE_PROCESS_DATA ProcessData);
@@ -157,6 +161,13 @@
VOID FASTCALL Win32CsrUnlockObject(Object_t *Object);
NTSTATUS FASTCALL Win32CsrReleaseObject(PCONSOLE_PROCESS_DATA ProcessData,
HANDLE Handle);
+
+NTSTATUS FASTCALL Win32CsrAllocateConsole(PCONSOLE_PROCESS_DATA ProcessData,
+ PHANDLE pInputHandle,
+ PHANDLE pOutputHandle,
+ PHANDLE pErrorHandle,
+ int ShowCmd,
+ PCSR_PROCESS CsrProcess);
VOID FASTCALL Win32CsrReleaseConsole(PCONSOLE_PROCESS_DATA ProcessData);
NTSTATUS NTAPI ConsoleNewProcess(PCSR_PROCESS SourceProcess,
Modified: branches/ros-csrss/win32ss/user/consrv/handle.c
URL:
http://svn.reactos.org/svn/reactos/branches/ros-csrss/win32ss/user/consrv/h…
==============================================================================
--- branches/ros-csrss/win32ss/user/consrv/handle.c [iso-8859-1] (original)
+++ branches/ros-csrss/win32ss/user/consrv/handle.c [iso-8859-1] Wed Jan 16 22:25:12 2013
@@ -79,6 +79,82 @@
/* FUNCTIONS *****************************************************************/
+/* static */ NTSTATUS
+FASTCALL
+Win32CsrInitHandlesTable(IN OUT PCONSOLE_PROCESS_DATA ProcessData,
+ OUT PHANDLE pInputHandle,
+ OUT PHANDLE pOutputHandle,
+ OUT PHANDLE pErrorHandle)
+{
+ NTSTATUS Status;
+ HANDLE InputHandle = INVALID_HANDLE_VALUE,
+ OutputHandle = INVALID_HANDLE_VALUE,
+ ErrorHandle = INVALID_HANDLE_VALUE;
+
+ /*
+ * Initialize the handles table. Use temporary variables to store
+ * the handles values in such a way that, if we fail, we don't
+ * return to the caller invalid handle values.
+ *
+ * Insert the IO handles.
+ */
+
+ RtlEnterCriticalSection(&ProcessData->HandleTableLock);
+
+ /* Insert the Input handle */
+ Status = Win32CsrInsertObject(ProcessData,
+ &InputHandle,
+ &ProcessData->Console->Header,
+ GENERIC_READ | GENERIC_WRITE,
+ TRUE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Failed to insert the input handle\n");
+ RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+ Win32CsrFreeHandlesTable(ProcessData);
+ return Status;
+ }
+
+ /* Insert the Output handle */
+ Status = Win32CsrInsertObject(ProcessData,
+ &OutputHandle,
+
&ProcessData->Console->ActiveBuffer->Header,
+ GENERIC_READ | GENERIC_WRITE,
+ TRUE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Failed to insert the output handle\n");
+ RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+ Win32CsrFreeHandlesTable(ProcessData);
+ return Status;
+ }
+
+ /* Insert the Error handle */
+ Status = Win32CsrInsertObject(ProcessData,
+ &ErrorHandle,
+
&ProcessData->Console->ActiveBuffer->Header,
+ GENERIC_READ | GENERIC_WRITE,
+ TRUE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Failed to insert the error handle\n");
+ RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+ Win32CsrFreeHandlesTable(ProcessData);
+ return Status;
+ }
+
+ /* Return the newly created handles */
+ *pInputHandle = InputHandle;
+ *pOutputHandle = OutputHandle;
+ *pErrorHandle = ErrorHandle;
+
+ RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+ return STATUS_SUCCESS;
+}
+
NTSTATUS
FASTCALL
Win32CsrInheritHandlesTable(IN PCONSOLE_PROCESS_DATA SourceProcessData,
@@ -282,6 +358,44 @@
Win32CsrUnlockConsole(Object->Console);
}
+NTSTATUS
+FASTCALL
+Win32CsrAllocateConsole(PCONSOLE_PROCESS_DATA ProcessData,
+ PHANDLE pInputHandle,
+ PHANDLE pOutputHandle,
+ PHANDLE pErrorHandle,
+ int ShowCmd,
+ PCSR_PROCESS CsrProcess)
+{
+ NTSTATUS Status = STATUS_SUCCESS;
+
+ /* Initialize a new Console owned by the Console Leader Process */
+ Status = CsrInitConsole(&ProcessData->Console, ShowCmd, CsrProcess);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Console initialization failed\n");
+ return Status;
+ }
+
+ /* Initialize the handles table */
+ Status = Win32CsrInitHandlesTable(ProcessData,
+ pInputHandle,
+ pOutputHandle,
+ pErrorHandle);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Failed to initialize the handles table\n");
+
+ // Win32CsrReleaseConsole(ProcessData);
+ ConioDeleteConsole(ProcessData->Console);
+ ProcessData->Console = NULL;
+
+ return Status;
+ }
+
+ return Status;
+}
+
VOID
FASTCALL
Win32CsrReleaseConsole(PCONSOLE_PROCESS_DATA ProcessData)
@@ -377,7 +491,6 @@
return Status;
}
- // FIXME: Do it before, or after the handles table inheritance ??
/* Temporary "inherit" the console from the parent */
TargetProcessData->ParentConsole = SourceProcessData->Console;
}
@@ -402,7 +515,6 @@
NTSTATUS Status = STATUS_SUCCESS;
PCONSOLE_CONNECTION_INFO ConnectInfo = (PCONSOLE_CONNECTION_INFO)ConnectionInfo;
PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrProcess);
- BOOLEAN NewConsole = FALSE;
DPRINT1("ConsoleConnect\n");
@@ -440,11 +552,15 @@
Win32CsrReleaseConsole(ProcessData);
/* Initialize a new Console owned by the Console Leader Process */
- NewConsole = TRUE;
- Status = CsrInitConsole(&ProcessData->Console, ConnectInfo->ShowCmd,
CsrProcess);
+ Status = Win32CsrAllocateConsole(ProcessData,
+ &ConnectInfo->InputHandle,
+ &ConnectInfo->OutputHandle,
+ &ConnectInfo->ErrorHandle,
+ ConnectInfo->ShowCmd,
+ CsrProcess);
if (!NT_SUCCESS(Status))
{
- DPRINT1("Console initialization failed\n");
+ DPRINT1("Console allocation failed\n");
return Status;
}
}
@@ -453,7 +569,6 @@
DPRINT1("ConsoleConnect - Reuse current (parent's) console\n");
/* Reuse our current console */
- NewConsole = FALSE;
ProcessData->Console = ConnectInfo->Console;
}
@@ -463,77 +578,7 @@
/* Insert the process into the processes list of the console */
InsertHeadList(&ProcessData->Console->ProcessList,
&ProcessData->ConsoleLink);
- /* Return it to the caller */
- ConnectInfo->Console = ProcessData->Console;
-
- if (NewConsole)
- {
- /*
- * Create a new handle table - Insert the IO handles
- */
-
- RtlEnterCriticalSection(&ProcessData->HandleTableLock);
-
- /* Insert the Input handle */
- Status = Win32CsrInsertObject(ProcessData,
- &ConnectInfo->InputHandle,
- &ProcessData->Console->Header,
- GENERIC_READ | GENERIC_WRITE,
- TRUE,
- FILE_SHARE_READ | FILE_SHARE_WRITE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Failed to insert the input handle\n");
- RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
- Win32CsrReleaseConsole(ProcessData);
- // ConioDeleteConsole(ProcessData->Console);
- // ProcessData->Console = NULL;
- return Status;
- }
-
- /* Insert the Output handle */
- Status = Win32CsrInsertObject(ProcessData,
- &ConnectInfo->OutputHandle,
-
&ProcessData->Console->ActiveBuffer->Header,
- GENERIC_READ | GENERIC_WRITE,
- TRUE,
- FILE_SHARE_READ | FILE_SHARE_WRITE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Failed to insert the output handle\n");
- RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
- Win32CsrReleaseConsole(ProcessData);
- // Win32CsrReleaseObject(ProcessData,
- // ConnectInfo->InputHandle);
- // ConioDeleteConsole(ProcessData->Console);
- // ProcessData->Console = NULL;
- return Status;
- }
-
- /* Insert the Error handle */
- Status = Win32CsrInsertObject(ProcessData,
- &ConnectInfo->ErrorHandle,
-
&ProcessData->Console->ActiveBuffer->Header,
- GENERIC_READ | GENERIC_WRITE,
- TRUE,
- FILE_SHARE_READ | FILE_SHARE_WRITE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("Failed to insert the error handle\n");
- RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
- Win32CsrReleaseConsole(ProcessData);
- // Win32CsrReleaseObject(ProcessData,
- // ConnectInfo->OutputHandle);
- // Win32CsrReleaseObject(ProcessData,
- // ConnectInfo->InputHandle);
- // ConioDeleteConsole(ProcessData->Console);
- // ProcessData->Console = NULL;
- return Status;
- }
-
- RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
- }
-
+ /// TODO: Move this up ?
/* Duplicate the Event */
Status = NtDuplicateObject(NtCurrentProcess(),
ProcessData->Console->ActiveEvent,
@@ -544,19 +589,12 @@
{
DPRINT1("NtDuplicateObject() failed: %lu\n", Status);
Win32CsrReleaseConsole(ProcessData);
- // if (NewConsole)
- // {
- // Win32CsrReleaseObject(ProcessData,
- // ConnectInfo->ErrorHandle);
- // Win32CsrReleaseObject(ProcessData,
- // ConnectInfo->OutputHandle);
- // Win32CsrReleaseObject(ProcessData,
- // ConnectInfo->InputHandle);
- // }
- // ConioDeleteConsole(ProcessData->Console); // FIXME: Just release the
console ?
- // ProcessData->Console = NULL;
return Status;
}
+
+ /* Return it to the caller */
+ ConnectInfo->Console = ProcessData->Console;
+
/* Input Wait Handle */
ConnectInfo->InputWaitHandle = ProcessData->ConsoleEvent;