- get csrss, ntvdm, smss compiling on msvc.
[reactos.git] / reactos / subsys / csrss / api / wapi.c
index efb189c..92a726e 100644 (file)
 
 /* INCLUDES ******************************************************************/
 
-#define NTOS_MODE_USER
-#include <ntos.h>
-#include <csrss/csrss.h>
-#include <ddk/ntddk.h>
-
+#include <csrss.h>
 
+#define NDEBUG
 
 #define NDEBUG
 #include <debug.h>
 
-#include "api.h"
-
 /* GLOBALS *******************************************************************/
 
 HANDLE CsrssApiHeap = (HANDLE) 0;
@@ -68,29 +63,34 @@ CsrApiRegisterDefinitions(PCSRSS_API_DEFINITION NewDefinitions)
   return STATUS_SUCCESS;
 }
 
-VOID FASTCALL
+VOID 
+FASTCALL
 CsrApiCallHandler(PCSRSS_PROCESS_DATA ProcessData,
-                  PCSRSS_API_REQUEST Request,
-                  PCSRSS_API_REPLY Reply)
+                  PCSR_API_MESSAGE Request)
 {
-  BOOL Found;
+  BOOL Found = FALSE;
   unsigned DefIndex;
+  ULONG Type;
+  
+  DPRINT("CSR: Calling handler for type: %x.\n", Request->Type);
+  Type = Request->Type & 0xFFFF; /* FIXME: USE MACRO */
+  DPRINT("CSR: API Number: %x ServerID: %x\n",Type, Request->Type >> 16);
 
-  Found = FALSE;
+  /* FIXME: Extract DefIndex instead of looping */
   for (DefIndex = 0; ! Found && DefIndex < ApiDefinitionsCount; DefIndex++)
     {
-      if (ApiDefinitions[DefIndex].Type == Request->Type)
+      if (ApiDefinitions[DefIndex].Type == Type)
         {
-          if (Request->Header.DataSize < ApiDefinitions[DefIndex].MinRequestSize)
+          if (Request->Header.u1.s1.DataLength < ApiDefinitions[DefIndex].MinRequestSize)
             {
               DPRINT1("Request type %d min request size %d actual %d\n",
-                      Request->Type, ApiDefinitions[DefIndex].MinRequestSize,
-                      Request->Header.DataSize);
-              Reply->Status = STATUS_INVALID_PARAMETER;
+                      Type, ApiDefinitions[DefIndex].MinRequestSize,
+                      Request->Header.u1.s1.DataLength);
+              Request->Status = STATUS_INVALID_PARAMETER;
             }
           else
             {
-              (ApiDefinitions[DefIndex].Handler)(ProcessData, Request, Reply);
+              (ApiDefinitions[DefIndex].Handler)(ProcessData, Request);
               Found = TRUE;
             }
         }
@@ -98,60 +98,79 @@ CsrApiCallHandler(PCSRSS_PROCESS_DATA ProcessData,
   if (! Found)
     {
       DPRINT1("CSR: Unknown request type 0x%x\n", Request->Type);
-      Reply->Header.MessageSize = sizeof(CSRSS_API_REPLY);
-      Reply->Header.DataSize = sizeof(CSRSS_API_REPLY) - LPC_MESSAGE_BASE_SIZE;
-      Reply->Status = STATUS_INVALID_SYSTEM_SERVICE;
+      Request->Header.u1.s1.TotalLength = sizeof(CSR_API_MESSAGE);
+      Request->Header.u1.s1.DataLength = sizeof(CSR_API_MESSAGE) - sizeof(PORT_MESSAGE);
+      Request->Status = STATUS_INVALID_SYSTEM_SERVICE;
     }
 }
 
-static void STDCALL
+static
+VOID
+STDCALL
 ClientConnectionThread(HANDLE ServerPort)
 {
-  NTSTATUS Status;
-  LPC_MAX_MESSAGE LpcReply;
-  LPC_MAX_MESSAGE LpcRequest;
-  PCSRSS_API_REQUEST Request;
-  PCSRSS_PROCESS_DATA ProcessData;
-  PCSRSS_API_REPLY Reply;
-
-       DPRINT("CSR: %s called", __FUNCTION__);
-
-  Reply = NULL;
-
-  for (;;)
+    NTSTATUS Status;
+    BYTE RawRequest[LPC_MAX_DATA_LENGTH];
+    PCSR_API_MESSAGE Request = (PCSR_API_MESSAGE)RawRequest;
+    PCSR_API_MESSAGE Reply;
+    PCSRSS_PROCESS_DATA ProcessData;
+  
+    DPRINT("CSR: %s called", __FUNCTION__);
+
+    /* Reply must be NULL at the first call to NtReplyWaitReceivePort */
+    Reply = NULL; 
+
+    /* Loop and reply/wait for a new message */
+    for (;;)
     {
-      Status = NtReplyWaitReceivePort(ServerPort,
-                                      0,
-                                      &Reply->Header,
-                                      &LpcRequest.Header);
-      if (! NT_SUCCESS(Status))
+        /* Send the reply and wait for a new request */
+        Status = NtReplyWaitReceivePort(ServerPort,
+                                        0,
+                                        &Reply->Header,
+                                        &Request->Header);
+        if (!NT_SUCCESS(Status))
         {
-          DPRINT1("CSR: NtReplyWaitReceivePort failed\n");
+          DPRINT1("NtReplyWaitReceivePort failed\n");
           break;
         }
-
-      if (LpcRequest.Header.MessageType == LPC_PORT_CLOSED)
+        
+        /* If the connection was closed, handle that */
+        if (Request->Header.u2.s2.Type == LPC_PORT_CLOSED)
         {
-          CsrFreeProcessData( LpcRequest.Header.ClientId.UniqueProcess );
-          break;
+            CsrFreeProcessData( Request->Header.ClientId.UniqueProcess );
+            break;
         }
 
-      Request = (PCSRSS_API_REQUEST)&LpcRequest;
-      Reply = (PCSRSS_API_REPLY)&LpcReply;
+        DPRINT("CSR: Got CSR API: %x [Message Origin: %x]\n", 
+               Request->Type, 
+               Request->Header.ClientId.UniqueProcess);
 
-      ProcessData = CsrGetProcessData(LpcRequest.Header.ClientId.UniqueProcess);
-      if (ProcessData == NULL)
+        /* Get the Process Data */
+        ProcessData = CsrGetProcessData(Request->Header.ClientId.UniqueProcess);
+        if (ProcessData == NULL)
         {
-          DPRINT1("CSR: Message %d: Unable to find data for process 0x%x\n",
-                 LpcRequest.Header.MessageType, LpcRequest.Header.ClientId.UniqueProcess);
-         break;
+            DPRINT1("Message %d: Unable to find data for process 0x%x\n",
+                    Request->Header.u2.s2.Type,
+                    Request->Header.ClientId.UniqueProcess);
+            break;
+        }
+        if (ProcessData->Terminated)
+        {
+            DPRINT1("Message %d: process %d already terminated\n",
+                   Request->Type, (ULONG)Request->Header.ClientId.UniqueProcess);
+            continue;
         }
 
-
-      CsrApiCallHandler(ProcessData, Request, Reply);
+        /* Call the Handler */
+        CsrApiCallHandler(ProcessData, Request);
+        
+        /* Send back the reply */
+        Reply = Request;
     }
-  NtClose(ServerPort);
-  RtlRosExitUserThread(STATUS_SUCCESS);
+    
+    /* Close the port and exit the thread */
+    NtClose(ServerPort);
+    RtlExitUserThread(STATUS_SUCCESS);
 }
 
 /**********************************************************************
@@ -166,7 +185,8 @@ DWORD STDCALL
 ServerApiPortThread (PVOID PortHandle)
 {
    NTSTATUS Status = STATUS_SUCCESS;
-   LPC_MAX_MESSAGE Request;
+   BYTE RawRequest[sizeof(PORT_MESSAGE) + sizeof(CSR_CONNECTION_INFO)];
+   PPORT_MESSAGE Request = (PPORT_MESSAGE)RawRequest;
    HANDLE hApiListenPort = * (PHANDLE) PortHandle;
    HANDLE ServerPort = (HANDLE) 0;
    HANDLE ServerThread = (HANDLE) 0;
@@ -178,13 +198,13 @@ ServerApiPortThread (PVOID PortHandle)
 
    for (;;)
      {
-        LPC_SECTION_READ LpcRead;
+        REMOTE_PORT_VIEW LpcRead;
         ServerPort = NULL;
 
-       Status = NtListenPort (hApiListenPort, & Request.Header);
+       Status = NtListenPort (hApiListenPort, Request);
        if (!NT_SUCCESS(Status))
          {
-            DPRINT1("CSR: NtListenPort() failed\n");
+            DPRINT1("CSR: NtListenPort() failed, status=%x\n", Status);
             break;
          }
        Status = NtAcceptConnectPort(& ServerPort,
@@ -199,11 +219,11 @@ ServerApiPortThread (PVOID PortHandle)
             break;
          }
 
-       ProcessData = CsrCreateProcessData(Request.Header.ClientId.UniqueProcess);
+       ProcessData = CsrCreateProcessData(Request->ClientId.UniqueProcess);
        if (ProcessData == NULL)
          {
             DPRINT1("Unable to allocate or find data for process 0x%x\n",
-                    Request.Header.ClientId.UniqueProcess);
+                    Request->ClientId.UniqueProcess);
             Status = STATUS_UNSUCCESSFUL;
             break;
          }
@@ -223,8 +243,8 @@ ServerApiPortThread (PVOID PortHandle)
                                     NULL,
                                     FALSE,
                                     0,
-                                    NULL,
-                                    NULL,
+                                    0,
+                                    0,
                                     (PTHREAD_START_ROUTINE)ClientConnectionThread,
                                     ServerPort,
                                     & ServerThread,
@@ -259,13 +279,15 @@ ServerSbApiPortThread (PVOID PortHandle)
 {
        HANDLE          hSbApiPortListen = * (PHANDLE) PortHandle;
        HANDLE          hConnectedPort = (HANDLE) 0;
-       LPC_MAX_MESSAGE Request = {{0}};
+       PORT_MESSAGE    Request;
        PVOID           Context = NULL;
        NTSTATUS        Status = STATUS_SUCCESS;
+    PPORT_MESSAGE Reply = NULL;
 
        DPRINT("CSR: %s called\n", __FUNCTION__);
 
-       Status = NtListenPort (hSbApiPortListen, & Request.Header);
+    RtlZeroMemory(&Request, sizeof(PORT_MESSAGE));
+       Status = NtListenPort (hSbApiPortListen, & Request);
        if (!NT_SUCCESS(Status))
        {
                DPRINT1("CSR: %s: NtListenPort(SB) failed (Status=0x%08lx)\n",
@@ -291,7 +313,7 @@ DPRINT("-- 2\n");
                                        __FUNCTION__, Status);
                        } else {
 DPRINT("-- 3\n");
-                               PLPC_MESSAGE Reply = NULL;
+                               
                                /*
                                 * Tell the init thread the SM gave the
                                 * green light for boostrapping.
@@ -309,19 +331,19 @@ DPRINT("-- 4\n");
                                        Status = NtReplyWaitReceivePort(hConnectedPort,
                                                                        Context,
                                                                        Reply,
-                                                                       & Request.Header);
+                                                                       & Request);
                                        if(!NT_SUCCESS(Status))
                                        {
                                                DPRINT1("CSR: %s: NtReplyWaitReceivePort failed (Status=0x%08lx)\n",
                                                        __FUNCTION__, Status);
                                                break;
                                        }
-                                       switch (Request.Header.MessageType)//fix .h PORT_MESSAGE_TYPE(Request))
+                                       switch (Request.u2.s2.Type)//fix .h PORT_MESSAGE_TYPE(Request))
                                        {
                                                /* TODO */
                                        default:
                                                DPRINT1("CSR: %s received message (type=%d)\n",
-                                                       __FUNCTION__, Request.Header.MessageType);
+                                                       __FUNCTION__, Request.u2.s2.Type);
                                        }
 DPRINT("-- 5\n");
                                }