[KERNEL32/CONSRV]
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 17 Nov 2012 22:42:04 +0000 (22:42 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 17 Nov 2012 22:42:04 +0000 (22:42 +0000)
- Make OpenConsole call only one server api, namely SrvOpenConsole, which was known before under the name CsrGetHandle (this latter function was removed in r57689 by error).
- Merge CSRSS_GET_INPUT_HANDLE and CSRSS_GET_OUTPUT_HANDLE structures into CSRSS_OPEN_CONSOLE (used by the functions named above).

[CONSRV]
- Add SrvOpenConsole (copied from CsrGetHandle), and add DPRINT1s to it.
- Load the localized string "Command Prompt" using the Server DLL's instance handle instead of calling GetModuleHandleW.
- Make use of CONSOLE_PROCESS_DATA structures.

svn path=/branches/ros-csrss/; revision=57723

dll/win32/kernel32/client/console/console.c
include/reactos/subsys/win/conmsg.h
win32ss/user/consrv/console.c

index e4250c5..07d882d 100644 (file)
@@ -389,52 +389,53 @@ OpenConsoleW(LPCWSTR wsName,
              BOOL bInheritHandle,
              DWORD dwShareMode)
 {
              BOOL bInheritHandle,
              DWORD dwShareMode)
 {
-    CSR_API_MESSAGE Request;
-    ULONG CsrRequest;
     NTSTATUS Status = STATUS_SUCCESS;
     NTSTATUS Status = STATUS_SUCCESS;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCSRSS_OPEN_CONSOLE OpenConsoleRequest = &ApiMessage.Data.OpenConsoleRequest;
+    HANDLE_TYPE HandleType;
 
     if (wsName && 0 == _wcsicmp(wsName, L"CONIN$"))
     {
 
     if (wsName && 0 == _wcsicmp(wsName, L"CONIN$"))
     {
-        CsrRequest = CSR_CREATE_API_NUMBER(CSR_NATIVE, GET_INPUT_HANDLE);
+        HandleType = HANDLE_INPUT;
     }
     else if (wsName && 0 == _wcsicmp(wsName, L"CONOUT$"))
     {
     }
     else if (wsName && 0 == _wcsicmp(wsName, L"CONOUT$"))
     {
-        CsrRequest = CSR_CREATE_API_NUMBER(CSR_NATIVE, GET_OUTPUT_HANDLE);
+        HandleType = HANDLE_OUTPUT;
     }
     else
     {
         SetLastError(ERROR_INVALID_PARAMETER);
     }
     else
     {
         SetLastError(ERROR_INVALID_PARAMETER);
-        return(INVALID_HANDLE_VALUE);
+        return INVALID_HANDLE_VALUE;
     }
 
     }
 
-    if (dwDesiredAccess & ~(GENERIC_READ|GENERIC_WRITE))
+    if (dwDesiredAccess & ~(GENERIC_READ | GENERIC_WRITE))
     {
         SetLastError(ERROR_INVALID_PARAMETER);
     {
         SetLastError(ERROR_INVALID_PARAMETER);
-        return(INVALID_HANDLE_VALUE);
+        return INVALID_HANDLE_VALUE;
     }
 
     }
 
-    if (dwShareMode & ~(FILE_SHARE_READ|FILE_SHARE_WRITE))
+    if (dwShareMode & ~(FILE_SHARE_READ | FILE_SHARE_WRITE))
     {
         SetLastError(ERROR_INVALID_PARAMETER);
     {
         SetLastError(ERROR_INVALID_PARAMETER);
-        return(INVALID_HANDLE_VALUE);
+        return INVALID_HANDLE_VALUE;
     }
 
     }
 
-    /* Structures for GET_INPUT_HANDLE and GET_OUTPUT_HANDLE requests are identical */
-    Request.Data.GetInputHandleRequest.Access = dwDesiredAccess;
-    Request.Data.GetInputHandleRequest.Inheritable = bInheritHandle;
-    Request.Data.GetInputHandleRequest.ShareMode = dwShareMode;
+    OpenConsoleRequest->HandleType = HandleType;
+    OpenConsoleRequest->Access = dwDesiredAccess;
+    OpenConsoleRequest->Inheritable = bInheritHandle;
+    OpenConsoleRequest->ShareMode = dwShareMode;
 
 
-    Status = CsrClientCallServer(&Request,
+    Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                                  NULL,
                                  NULL,
-                                 CsrRequest,
-                                 sizeof(CSR_API_MESSAGE));
-    if (!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status))
+                                 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepOpenConsole),
+                                 sizeof(CSRSS_OPEN_CONSOLE));
+    if (!NT_SUCCESS(Status) || !NT_SUCCESS(Status = ApiMessage.Status))
     {
         BaseSetLastNTError(Status);
         return INVALID_HANDLE_VALUE;
     }
 
     {
         BaseSetLastNTError(Status);
         return INVALID_HANDLE_VALUE;
     }
 
-    return Request.Data.GetInputHandleRequest.Handle;
+    return OpenConsoleRequest->Handle;
 }
 
 
 }
 
 
index daa864d..e245a6b 100644 (file)
@@ -20,8 +20,8 @@
 // Some names are also deduced from the subsystems/win32/csrss/csrsrv/server.c ones.
 typedef enum _CONSRV_API_NUMBER
 {
 // Some names are also deduced from the subsystems/win32/csrss/csrsrv/server.c ones.
 typedef enum _CONSRV_API_NUMBER
 {
-    // ConsolepOpenConsole = CONSRV_FIRST_API_NUMBER,
-    ConsolepGetConsoleInput = CONSRV_FIRST_API_NUMBER,
+    ConsolepOpenConsole = CONSRV_FIRST_API_NUMBER,
+    ConsolepGetConsoleInput,
     ConsolepWriteConsoleInput,
     ConsolepReadConsoleOutput,
     ConsolepWriteConsoleOutput,
     ConsolepWriteConsoleInput,
     ConsolepReadConsoleOutput,
     ConsolepWriteConsoleOutput,
@@ -392,14 +392,23 @@ typedef struct
     DWORD Options;
 } CSRSS_DUPLICATE_HANDLE, *PCSRSS_DUPLICATE_HANDLE;
 
     DWORD Options;
 } CSRSS_DUPLICATE_HANDLE, *PCSRSS_DUPLICATE_HANDLE;
 
+/*
+ * Type of handles.
+ */
+typedef enum _HANDLE_TYPE
+{
+    HANDLE_INPUT    = 0x01,
+    HANDLE_OUTPUT   = 0x02
+} HANDLE_TYPE;
+
 typedef struct
 {
 typedef struct
 {
+    HANDLE Handle;
+    HANDLE_TYPE HandleType;
     DWORD Access;
     BOOL Inheritable;
     DWORD Access;
     BOOL Inheritable;
-    HANDLE Handle;
     DWORD ShareMode;
     DWORD ShareMode;
-} CSRSS_GET_INPUT_HANDLE, *PCSRSS_GET_INPUT_HANDLE,
-  CSRSS_GET_OUTPUT_HANDLE, *PCSRSS_GET_OUTPUT_HANDLE;
+} CSRSS_OPEN_CONSOLE, *PCSRSS_OPEN_CONSOLE;
 
 typedef struct
 {
 
 typedef struct
 {
@@ -575,8 +584,7 @@ typedef struct _CONSOLE_API_MESSAGE
         CSRSS_FREE_CONSOLE FreeConsoleRequest;
 
         /* Handles */
         CSRSS_FREE_CONSOLE FreeConsoleRequest;
 
         /* Handles */
-        CSRSS_GET_INPUT_HANDLE GetInputHandleRequest;
-        CSRSS_GET_OUTPUT_HANDLE GetOutputHandleRequest;
+        CSRSS_OPEN_CONSOLE OpenConsoleRequest;
         CSRSS_CLOSE_HANDLE CloseHandleRequest;
         CSRSS_VERIFY_HANDLE VerifyHandleRequest;
         CSRSS_DUPLICATE_HANDLE DuplicateHandleRequest;
         CSRSS_CLOSE_HANDLE CloseHandleRequest;
         CSRSS_VERIFY_HANDLE VerifyHandleRequest;
         CSRSS_DUPLICATE_HANDLE DuplicateHandleRequest;
index 7605b61..1db0eb1 100644 (file)
@@ -17,7 +17,6 @@
 
 /* FUNCTIONS *****************************************************************/
 
 
 /* FUNCTIONS *****************************************************************/
 
-/*** Taken from win32ss/user/win32csr/desktopbg.c ***/
 BOOL FASTCALL
 DtbgIsDesktopVisible(VOID)
 {
 BOOL FASTCALL
 DtbgIsDesktopVisible(VOID)
 {
@@ -31,10 +30,10 @@ DtbgIsDesktopVisible(VOID)
 
     return VisibleDesktopWindow != NULL;
 }
 
     return VisibleDesktopWindow != NULL;
 }
-/****************************************************/
 
 NTSTATUS FASTCALL
 
 NTSTATUS FASTCALL
-ConioConsoleFromProcessData(PCSR_PROCESS ProcessData, PCSRSS_CONSOLE *Console)
+ConioConsoleFromProcessData(PCONSOLE_PROCESS_DATA ProcessData,
+                            PCSRSS_CONSOLE *Console)
 {
     PCSRSS_CONSOLE ProcessConsole;
 
 {
     PCSRSS_CONSOLE ProcessConsole;
 
@@ -57,30 +56,32 @@ ConioConsoleFromProcessData(PCSR_PROCESS ProcessData, PCSRSS_CONSOLE *Console)
 }
 
 VOID FASTCALL
 }
 
 VOID FASTCALL
-ConioConsoleCtrlEventTimeout(DWORD Event, PCSR_PROCESS ProcessData, DWORD Timeout)
+ConioConsoleCtrlEventTimeout(DWORD Event,
+                             PCONSOLE_PROCESS_DATA ProcessData,
+                             DWORD Timeout)
 {
     HANDLE Thread;
 
 {
     HANDLE Thread;
 
-    DPRINT("ConioConsoleCtrlEvent Parent ProcessId = %x\n", ProcessData->ClientId.UniqueProcess);
+    DPRINT("ConioConsoleCtrlEvent Parent ProcessId = %x\n", ProcessData->Process->ClientId.UniqueProcess);
 
     if (ProcessData->CtrlDispatcher)
     {
 
     if (ProcessData->CtrlDispatcher)
     {
-
-        Thread = CreateRemoteThread(ProcessData->ProcessHandle, NULL, 0,
-                                    (LPTHREAD_START_ROUTINE) ProcessData->CtrlDispatcher,
+        Thread = CreateRemoteThread(ProcessData->Process->ProcessHandle, NULL, 0,
+                                    ProcessData->CtrlDispatcher,
                                     UlongToPtr(Event), 0, NULL);
         if (NULL == Thread)
         {
             DPRINT1("Failed thread creation (Error: 0x%x)\n", GetLastError());
             return;
         }
                                     UlongToPtr(Event), 0, NULL);
         if (NULL == Thread)
         {
             DPRINT1("Failed thread creation (Error: 0x%x)\n", GetLastError());
             return;
         }
+
         WaitForSingleObject(Thread, Timeout);
         CloseHandle(Thread);
     }
 }
 
 VOID FASTCALL
         WaitForSingleObject(Thread, Timeout);
         CloseHandle(Thread);
     }
 }
 
 VOID FASTCALL
-ConioConsoleCtrlEvent(DWORD Event, PCSR_PROCESS ProcessData)
+ConioConsoleCtrlEvent(DWORD Event, PCONSOLE_PROCESS_DATA ProcessData)
 {
     ConioConsoleCtrlEventTimeout(Event, ProcessData, 0);
 }
 {
     ConioConsoleCtrlEventTimeout(Event, ProcessData, 0);
 }
@@ -93,13 +94,11 @@ CsrInitConsole(PCSRSS_CONSOLE Console, int ShowCmd)
     PCSRSS_SCREEN_BUFFER NewBuffer;
     BOOL GuiMode;
     WCHAR Title[255];
     PCSRSS_SCREEN_BUFFER NewBuffer;
     BOOL GuiMode;
     WCHAR Title[255];
-    HINSTANCE hInst;
 
     Console->Title.MaximumLength = Console->Title.Length = 0;
     Console->Title.Buffer = NULL;
 
 
     Console->Title.MaximumLength = Console->Title.Length = 0;
     Console->Title.Buffer = NULL;
 
-    hInst = GetModuleHandleW(L"win32csr");
-    if (LoadStringW(hInst,IDS_COMMAND_PROMPT,Title,sizeof(Title)/sizeof(Title[0])))
+    if (LoadStringW(ConSrvDllInstance, IDS_COMMAND_PROMPT, Title, sizeof(Title) / sizeof(Title[0])))
     {
         RtlCreateUnicodeString(&Console->Title, Title);
     }
     {
         RtlCreateUnicodeString(&Console->Title, Title);
     }
@@ -150,10 +149,10 @@ CsrInitConsole(PCSRSS_CONSOLE Console, int ShowCmd)
     /* make console active, and insert into console list */
     Console->ActiveBuffer = (PCSRSS_SCREEN_BUFFER) NewBuffer;
 
     /* make console active, and insert into console list */
     Console->ActiveBuffer = (PCSRSS_SCREEN_BUFFER) NewBuffer;
 
-    if (! GuiMode)
+    if (!GuiMode)
     {
         Status = TuiInitConsole(Console);
     {
         Status = TuiInitConsole(Console);
-        if (! NT_SUCCESS(Status))
+        if (!NT_SUCCESS(Status))
         {
             DPRINT1("Failed to open text-mode console, switching to gui-mode\n");
             GuiMode = TRUE;
         {
             DPRINT1("Failed to open text-mode console, switching to gui-mode\n");
             GuiMode = TRUE;
@@ -162,7 +161,7 @@ CsrInitConsole(PCSRSS_CONSOLE Console, int ShowCmd)
     else /* GuiMode */
     {
         Status = GuiInitConsole(Console, ShowCmd);
     else /* GuiMode */
     {
         Status = GuiInitConsole(Console, ShowCmd);
-        if (! NT_SUCCESS(Status))
+        if (!NT_SUCCESS(Status))
         {
             HeapFree(ConSrvHeap,0, NewBuffer);
             RtlFreeUnicodeString(&Console->Title);
         {
             HeapFree(ConSrvHeap,0, NewBuffer);
             RtlFreeUnicodeString(&Console->Title);
@@ -191,6 +190,64 @@ CsrInitConsole(PCSRSS_CONSOLE Console, int ShowCmd)
     return STATUS_SUCCESS;
 }
 
     return STATUS_SUCCESS;
 }
 
+CSR_API(SrvOpenConsole)
+{
+    NTSTATUS Status = STATUS_SUCCESS;
+    PCSRSS_OPEN_CONSOLE OpenConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.OpenConsoleRequest;
+    PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
+
+    DPRINT("SrvOpenConsole\n");
+
+    OpenConsoleRequest->Handle = INVALID_HANDLE_VALUE;
+
+    RtlEnterCriticalSection(&ProcessData->HandleTableLock);
+
+    DPRINT1("SrvOpenConsole - Checkpoint 1\n");
+    DPRINT1("ProcessData = 0x%p ; ProcessData->Console = 0x%p\n", ProcessData, ProcessData->Console);
+
+    if (ProcessData->Console)
+    {
+        DWORD DesiredAccess = OpenConsoleRequest->Access;
+        DWORD ShareMode = OpenConsoleRequest->ShareMode;
+
+        PCSRSS_CONSOLE Console = ProcessData->Console;
+        Object_t *Object;
+
+        DPRINT1("SrvOpenConsole - Checkpoint 2\n");
+        EnterCriticalSection(&Console->Lock);
+        DPRINT1("SrvOpenConsole - Checkpoint 3\n");
+
+        if (OpenConsoleRequest->HandleType == HANDLE_OUTPUT)
+            Object = &Console->ActiveBuffer->Header;
+        else // HANDLE_INPUT
+            Object = &Console->Header;
+
+        if (((DesiredAccess & GENERIC_READ)  && Object->ExclusiveRead  != 0) ||
+            ((DesiredAccess & GENERIC_WRITE) && Object->ExclusiveWrite != 0) ||
+            (!(ShareMode & FILE_SHARE_READ)  && Object->AccessRead     != 0) ||
+            (!(ShareMode & FILE_SHARE_WRITE) && Object->AccessWrite    != 0))
+        {
+            DPRINT1("Sharing violation\n");
+            Status = STATUS_SHARING_VIOLATION;
+        }
+        else
+        {
+            Status = Win32CsrInsertObject(ProcessData,
+                                          &OpenConsoleRequest->Handle,
+                                          Object,
+                                          DesiredAccess,
+                                          OpenConsoleRequest->Inheritable,
+                                          ShareMode);
+        }
+
+        LeaveCriticalSection(&Console->Lock);
+    }
+
+    RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+
+    return Status;
+}
+
 CSR_API(SrvAllocConsole)
 {
     PCSRSS_ALLOC_CONSOLE AllocConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.AllocConsoleRequest;
 CSR_API(SrvAllocConsole)
 {
     PCSRSS_ALLOC_CONSOLE AllocConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.AllocConsoleRequest;