implemented sweeping of handle tables
[reactos.git] / reactos / ntoskrnl / lpc / reply.c
index 7ebda68..e67db08 100644 (file)
@@ -37,19 +37,30 @@ EiReplyOrRequestPort (IN    PEPORT          Port,
 {
    KIRQL oldIrql;
    PQUEUEDMESSAGE MessageReply;
+   ULONG Size;
 
    if (Port == NULL)
      {
        KEBUGCHECK(0);
      }
 
-   MessageReply = ExAllocatePoolWithTag(NonPagedPool, sizeof(QUEUEDMESSAGE),
+   Size = sizeof(QUEUEDMESSAGE);
+   if (LpcReply && LpcReply->u1.s1.TotalLength > (CSHORT)sizeof(PORT_MESSAGE))
+     {
+       Size += LpcReply->u1.s1.TotalLength - sizeof(PORT_MESSAGE);
+     }
+   MessageReply = ExAllocatePoolWithTag(NonPagedPool, Size, 
                                        TAG_LPC_MESSAGE);
    MessageReply->Sender = Sender;
 
    if (LpcReply != NULL)
      {
-       memcpy(&MessageReply->Message, LpcReply, LpcReply->u1.s1.TotalLength);
+       memcpy(&MessageReply->Message, LpcReply, LpcReply->u1.s1.TotalLength);
+     }
+   else
+     {
+       MessageReply->Message.u1.s1.TotalLength = sizeof(PORT_MESSAGE);
+       MessageReply->Message.u1.s1.DataLength = 0;
      }
 
    MessageReply->Message.ClientId.UniqueProcess = PsGetCurrentProcessId();
@@ -135,11 +146,11 @@ NtReplyPort (IN   HANDLE          PortHandle,
  * REVISIONS
  */
 NTSTATUS STDCALL
-NtReplyWaitReceivePortEx(IN  HANDLE            PortHandle,
-                        OUT PULONG             PortId,
-                        IN  PPORT_MESSAGE      LpcReply,
-                        OUT PPORT_MESSAGE      LpcMessage,
-                        IN  PLARGE_INTEGER     Timeout)
+NtReplyWaitReceivePortEx(IN HANDLE PortHandle,
+                         OUT PVOID *PortContext OPTIONAL,
+                         IN PPORT_MESSAGE ReplyMessage OPTIONAL,
+                         OUT PPORT_MESSAGE ReceiveMessage,
+                                    IN PLARGE_INTEGER Timeout OPTIONAL)
 {
    PEPORT Port;
    KIRQL oldIrql;
@@ -152,13 +163,13 @@ NtReplyWaitReceivePortEx(IN  HANDLE               PortHandle,
    PreviousMode = ExGetPreviousMode();
 
    DPRINT("NtReplyWaitReceivePortEx(PortHandle %x, LpcReply %x, "
-         "LpcMessage %x)\n", PortHandle, LpcReply, LpcMessage);
+         "LpcMessage %x)\n", PortHandle, ReplyMessage, ReceiveMessage);
 
    if (PreviousMode != KernelMode)
      {
        _SEH_TRY
          {
-           ProbeForWrite(LpcMessage,
+           ProbeForWrite(ReceiveMessage,
                          sizeof(PORT_MESSAGE),
                          1);
          }
@@ -200,10 +211,10 @@ NtReplyWaitReceivePortEx(IN  HANDLE               PortHandle,
    /*
     * Send the reply, only if port is connected
     */
-   if (LpcReply != NULL && !Disconnected)
+   if (ReplyMessage != NULL && !Disconnected)
      {
        Status = EiReplyOrRequestPort(Port->OtherPort,
-                                     LpcReply,
+                                     ReplyMessage,
                                      LPC_REPLY,
                                      Port);
        KeReleaseSemaphore(&Port->OtherPort->Semaphore, IO_NO_INCREMENT, 1,
@@ -252,6 +263,12 @@ NtReplyWaitReceivePortEx(IN  HANDLE                PortHandle,
    Request = EiDequeueMessagePort(Port);
    KeReleaseSpinLock(&Port->Lock, oldIrql);
 
+   if (Request == NULL)
+     {
+       ObDereferenceObject(Port);
+       return STATUS_UNSUCCESSFUL;
+     }
+
    if (Request->Message.u2.s2.Type == LPC_CONNECTION_REQUEST)
      {
        PORT_MESSAGE Header;
@@ -266,14 +283,14 @@ NtReplyWaitReceivePortEx(IN  HANDLE               PortHandle,
          {
            _SEH_TRY
              {
-               ProbeForWrite((PVOID)(LpcMessage + 1),
+               ProbeForWrite((PVOID)(ReceiveMessage + 1),
                              CRequest->ConnectDataLength,
                              1);
 
-               RtlCopyMemory(LpcMessage,
+               RtlCopyMemory(ReceiveMessage,
                              &Header,
                              sizeof(PORT_MESSAGE));
-               RtlCopyMemory((PVOID)(LpcMessage + 1),
+               RtlCopyMemory((PVOID)(ReceiveMessage + 1),
                              CRequest->ConnectData,
                              CRequest->ConnectDataLength);
              }
@@ -285,10 +302,10 @@ NtReplyWaitReceivePortEx(IN  HANDLE               PortHandle,
          }
        else
          {
-           RtlCopyMemory(LpcMessage,
+           RtlCopyMemory(ReceiveMessage,
                          &Header,
                          sizeof(PORT_MESSAGE));
-           RtlCopyMemory((PVOID)(LpcMessage + 1),
+           RtlCopyMemory((PVOID)(ReceiveMessage + 1),
                          CRequest->ConnectData,
                          CRequest->ConnectDataLength);
          }
@@ -299,11 +316,11 @@ NtReplyWaitReceivePortEx(IN  HANDLE               PortHandle,
          {
            _SEH_TRY
              {
-               ProbeForWrite(LpcMessage,
+               ProbeForWrite(ReceiveMessage,
                              Request->Message.u1.s1.TotalLength,
                              1);
 
-               RtlCopyMemory(LpcMessage,
+               RtlCopyMemory(ReceiveMessage,
                              &Request->Message,
                              Request->Message.u1.s1.TotalLength);
              }
@@ -315,7 +332,7 @@ NtReplyWaitReceivePortEx(IN  HANDLE         PortHandle,
          }
        else
          {
-           RtlCopyMemory(LpcMessage,
+           RtlCopyMemory(ReceiveMessage,
                          &Request->Message,
                          Request->Message.u1.s1.TotalLength);
          }
@@ -370,16 +387,16 @@ NtReplyWaitReceivePortEx(IN  HANDLE               PortHandle,
  * REVISIONS
  */
 NTSTATUS STDCALL
-NtReplyWaitReceivePort (IN  HANDLE             PortHandle,
-                       OUT PULONG              PortId,
-                       IN  PPORT_MESSAGE       LpcReply,
-                       OUT PPORT_MESSAGE       LpcMessage)
+NtReplyWaitReceivePort(IN HANDLE PortHandle,
+                       OUT PVOID *PortContext OPTIONAL,
+                       IN PPORT_MESSAGE ReplyMessage OPTIONAL,
+                       OUT PPORT_MESSAGE ReceiveMessage)
 {
-  return(NtReplyWaitReceivePortEx (PortHandle,
-                                  PortId,
-                                  LpcReply,
-                                  LpcMessage,
-                                  NULL));
+    return NtReplyWaitReceivePortEx(PortHandle,
+                                                   PortContext,
+                                                   ReplyMessage,
+                                                   ReceiveMessage,
+                                                   NULL);
 }
 
 /**********************************************************************