[ros-kernel] CSRSS problems

Thomas Weidenmueller thomas at reactsoft.com
Mon Jun 28 22:10:22 CEST 2004


Hartmut Birr wrote:

>Hi,
>
>I can fix the problem that the LPC listener port isn't setup at the time
>where win32csr.dll is loaded. It is possible to change the initialisation
>order. But I run in an other problem. CsrClientConnectToServer is called
>from DllMain while the process is attaching. Csrss creates a new thread for
>the client. Attaching of the new thread is not possible, because the loader
>lock is held while the process attaching is in progress. I can fix this
>deadlock by calling CsrClientConnectToServer previous win32csr.dll is loaded
>and add a check that CsrClientConnectToServer doesn't initialized the
>connection again. But I think it is also a dirty fix like CsrIsCsrss(). Any
>other ideas? 
>
>- Hartmut
>  
>
I attached a patch that makes the client request handling look a little 
more correct (i think). I don't know if it's a good solution but at 
least it doesn't crash in csrss anymore.
But it raises an exception in <ntdll.dll: 3acd (csr/lpc.c:197 
(CsrClientConnectToServer)) - so if we fixed the initialization it might 
work. Hartmut, would you do that? I'm stuck with my work on win32k 
because of this problem.

Regards
Thomas
-------------- next part --------------
Index: subsys/csrss/init.c
===================================================================
RCS file: /CVS/ReactOS/reactos/subsys/csrss/init.c,v
retrieving revision 1.27
diff -u -r1.27 init.c
--- subsys/csrss/init.c	28 May 2004 21:33:41 -0000	1.27
+++ subsys/csrss/init.c	28 Jun 2004 18:15:25 -0000
@@ -300,7 +300,7 @@
                                0,
                                NULL,
                                NULL,
-                               (PTHREAD_START_ROUTINE)Thread_Api,
+                               (PTHREAD_START_ROUTINE)ServerApiPortThead,
                                ApiPortHandle,
                                NULL,
                                NULL);
Index: subsys/csrss/api/wapi.c
===================================================================
RCS file: /CVS/ReactOS/reactos/subsys/csrss/api/wapi.c,v
retrieving revision 1.36
diff -u -r1.36 wapi.c
--- subsys/csrss/api/wapi.c	27 Jun 2004 12:21:32 -0000	1.36
+++ subsys/csrss/api/wapi.c	28 Jun 2004 19:02:49 -0000
@@ -98,7 +98,7 @@
 }
 
 static void
-Thread_Api2(HANDLE ServerPort)
+ClientConnectionThread(PCSRSS_CLIENT_REQ Req)
 {
   NTSTATUS Status;
   LPC_MAX_MESSAGE LpcReply;
@@ -106,38 +106,53 @@
   PCSRSS_API_REQUEST Request;
   PCSRSS_PROCESS_DATA ProcessData;
   PCSRSS_API_REPLY Reply;
+  
+  ASSERT(ClientReq);
    
   Reply = NULL;
    
   for (;;)
     {
-      Status = NtReplyWaitReceivePort(ServerPort,
+      Status = NtReplyWaitReceivePort(Req->ServerPort,
                                       0,
                                       &Reply->Header,
                                       &LpcRequest.Header);
       if (! NT_SUCCESS(Status))
         {
           DPRINT1("CSR: NtReplyWaitReceivePort failed\n");
-          NtClose(ServerPort);
-          RtlRosExitUserThread(Status);
-          continue;
+          break;
         }
 	
       if (LpcRequest.Header.MessageType == LPC_PORT_CLOSED)
         {
           CsrFreeProcessData( (ULONG)LpcRequest.Header.ClientId.UniqueProcess );
-          NtClose(ServerPort);
-          RtlRosExitUserThread(STATUS_SUCCESS);
-          continue;
+          Status = STATUS_SUCCESS;
+          break;
         }
 
       Request = (PCSRSS_API_REQUEST)&LpcRequest;
       Reply = (PCSRSS_API_REPLY)&LpcReply;
 	
       ProcessData = CsrGetProcessData((ULONG)LpcRequest.Header.ClientId.UniqueProcess);
+      if (ProcessData == NULL)
+      {
+        DPRINT1("CSR: Message %d: Unable to find data for process %d\n",
+	        LpcRequest.Header.MessageType, (ULONG)LpcRequest.Header.ClientId.UniqueProcess);
+	Reply->Status = STATUS_INVALID_PARAMETER;
+	/* wait for the next request... */
+	continue;
+      }
+      ProcessData->CsrSectionViewBase = Req->LpcRead.ViewBase;
+      ProcessData->CsrSectionViewSize = Req->LpcRead.ViewSize;
 
       CsrApiCallHandler(ProcessData, Request, Reply);
     }
+  
+  NtClose(Req->ServerPort);
+  
+  /* Free the Req structure */
+  RtlFreeHeap(CsrssApiHeap, 0, Req);
+  RtlRosExitUserThread(Status);
 }
 
 /**********************************************************************
@@ -148,47 +163,54 @@
  * 	Handle connection requests from clients to the port
  * 	"\Windows\ApiPort".
  */
-void Thread_Api(PVOID PortHandle)
+void ServerApiPortThead(PVOID PortHandle)
 {
    NTSTATUS Status;
    LPC_MAX_MESSAGE Request;
-   HANDLE ServerPort;
    HANDLE ServerThread;
-   PCSRSS_PROCESS_DATA ProcessData;
    
    CsrInitProcessData();
    
    for (;;)
      {
-        LPC_SECTION_READ LpcRead;
+        PCSRSS_CLIENT_REQ Req;
+
+	if (!(Req = RtlAllocateHeap(CsrssApiHeap, 0, sizeof(CSRSS_CLIENT_REQ))))
+	  {
+	     DPRINT1("CSR: Not enough memory to allocate a CSRSS_CLIENT_REQ structure!\n");
+	     NtTerminateThread(NtCurrentThread(), Status);
+	  }
 
 	Status = NtListenPort(PortHandle, &Request.Header);
 	if (!NT_SUCCESS(Status))
 	  {
 	     DPRINT1("CSR: NtListenPort() failed\n");
+	     RtlFreeHeap(CsrssApiHeap, 0, Req);
+	     NtClose(PortHandle);
 	     NtTerminateThread(NtCurrentThread(), Status);
 	  }
 	
-	Status = NtAcceptConnectPort(&ServerPort,
+	Status = NtAcceptConnectPort(&Req->ServerPort,
 				     PortHandle,
 				     NULL,
 				     1,
 				     0,
-				     &LpcRead);
+				     &Req->LpcRead);
 	if (!NT_SUCCESS(Status))
 	  {
 	     DPRINT1("CSR: NtAcceptConnectPort() failed\n");
+	     RtlFreeHeap(CsrssApiHeap, 0, Req);
+	     NtClose(PortHandle);
 	     NtTerminateThread(NtCurrentThread(), Status);
 	  }
-
-	ProcessData = CsrGetProcessData((ULONG)Request.Header.ClientId.UniqueProcess);
-	ProcessData->CsrSectionViewBase = LpcRead.ViewBase;
-	ProcessData->CsrSectionViewSize = LpcRead.ViewSize;
 	
-	Status = NtCompleteConnectPort(ServerPort);
+	Status = NtCompleteConnectPort(Req->ServerPort);
 	if (!NT_SUCCESS(Status))
 	  {
 	     DPRINT1("CSR: NtCompleteConnectPort() failed\n");
+	     RtlFreeHeap(CsrssApiHeap, 0, Req);
+	     NtClose(Req->ServerPort);
+	     NtClose(PortHandle);
 	     NtTerminateThread(NtCurrentThread(), Status);
 	  }
 	
@@ -198,18 +220,25 @@
 				     0,
 				     NULL,
 				     NULL,
-				     (PTHREAD_START_ROUTINE)Thread_Api2,
-				     ServerPort,
+				     (PTHREAD_START_ROUTINE)ClientConnectionThread,
+				     Req,
 				     &ServerThread,
 				     NULL);
 	if (!NT_SUCCESS(Status))
 	  {
 	     DPRINT1("CSR: Unable to create server thread\n");
-	     NtClose(ServerPort);
+	     RtlFreeHeap(CsrssApiHeap, 0, Req);
+	     NtClose(Req->ServerPort);
+	     NtClose(PortHandle);
 	     NtTerminateThread(NtCurrentThread(), Status);
 	  }
 	NtClose(ServerThread);
+	/* Req is freed by the thread we created */
      }
+   
+   /* we should never get here, maybe when doing a shutdown? */
+   NtClose(PortHandle);
+   NtTerminateThread(NtCurrentThread(), Status);
 }
 
 /* EOF */
Index: subsys/csrss/include/api.h
===================================================================
RCS file: /CVS/ReactOS/reactos/subsys/csrss/include/api.h,v
retrieving revision 1.4
diff -u -r1.4 api.h
--- subsys/csrss/include/api.h	11 Jan 2004 17:31:15 -0000	1.4
+++ subsys/csrss/include/api.h	28 Jun 2004 18:38:08 -0000
@@ -77,6 +77,12 @@
   PCSRSS_API_DEFINITION *ApiDefinitions;
 } CSRSS_LISTEN_DATA, *PCSRSS_LISTEN_DATA;
 
+typedef struct _CSRSS_CLIENT_REQ
+{
+  LPC_SECTION_READ LpcRead;
+  HANDLE ServerPort;
+} CSRSS_CLIENT_REQ, *PCSRSS_CLIENT_REQ;
+
 #define CSR_API(n) NTSTATUS STDCALL n (\
 PCSRSS_PROCESS_DATA ProcessData,\
 PCSRSS_API_REQUEST Request,\
@@ -96,7 +102,7 @@
 VOID FASTCALL CsrApiCallHandler(PCSRSS_PROCESS_DATA ProcessData,
                                 PCSRSS_API_REQUEST Request,
                                 PCSRSS_API_REPLY Reply);
-VOID Thread_Api(PVOID PortHandle);
+VOID ServerApiPortThead(PVOID PortHandle);
 VOID Console_Api( DWORD Ignored );
 
 extern HANDLE CsrssApiHeap;


More information about the Ros-kernel mailing list