2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/lpc/send.c
5 * PURPOSE: Local Procedure Call: Sending (Requests)
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
15 /* PUBLIC FUNCTIONS **********************************************************/
22 LpcRequestPort(IN PVOID PortObject
,
23 IN PPORT_MESSAGE LpcMessage
)
25 PLPCP_PORT_OBJECT Port
= PortObject
, QueuePort
, ConnectionPort
= NULL
;
27 PLPCP_MESSAGE Message
;
28 KPROCESSOR_MODE PreviousMode
= KeGetPreviousMode();
30 LPCTRACE(LPC_SEND_DEBUG
, "Port: %p. Message: %p\n", Port
, LpcMessage
);
32 /* Check if this is a non-datagram message */
33 if (LpcMessage
->u2
.s2
.Type
)
35 /* Get the message type */
36 MessageType
= LpcpGetMessageType(LpcMessage
);
39 if ((MessageType
< LPC_DATAGRAM
) || (MessageType
> LPC_CLIENT_DIED
))
42 return STATUS_INVALID_PARAMETER
;
45 /* Mark this as a kernel-mode message only if we really came from it */
46 if ((PreviousMode
== KernelMode
) &&
47 (LpcMessage
->u2
.s2
.Type
& LPC_KERNELMODE_MESSAGE
))
49 /* We did, this is a kernel mode message */
50 MessageType
|= LPC_KERNELMODE_MESSAGE
;
55 /* This is a datagram */
56 MessageType
= LPC_DATAGRAM
;
59 /* Can't have data information on this type of call */
60 if (LpcMessage
->u2
.s2
.DataInfoOffset
) return STATUS_INVALID_PARAMETER
;
62 /* Validate message sizes */
63 if (((ULONG
)LpcMessage
->u1
.s1
.TotalLength
> Port
->MaxMessageLength
) ||
64 ((ULONG
)LpcMessage
->u1
.s1
.TotalLength
<= (ULONG
)LpcMessage
->u1
.s1
.DataLength
))
67 return STATUS_PORT_MESSAGE_TOO_LONG
;
70 /* Allocate a new message */
71 Message
= LpcpAllocateFromPortZone();
72 if (!Message
) return STATUS_NO_MEMORY
;
74 /* Clear the context */
75 Message
->RepliedToThread
= NULL
;
76 Message
->PortContext
= NULL
;
78 /* Copy the message */
79 LpcpMoveMessage(&Message
->Request
,
83 &PsGetCurrentThread()->Cid
);
85 /* Acquire the LPC lock */
86 KeAcquireGuardedMutex(&LpcpLock
);
88 /* Check if this is anything but a connection port */
89 if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) != LPCP_CONNECTION_PORT
)
91 /* The queue port is the connected port */
92 QueuePort
= Port
->ConnectedPort
;
95 /* Check if this is a client port */
96 if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) == LPCP_CLIENT_PORT
)
98 /* Then copy the context */
99 Message
->PortContext
= QueuePort
->PortContext
;
100 ConnectionPort
= QueuePort
= Port
->ConnectionPort
;
104 LpcpFreeToPortZone(Message
, 3);
105 return STATUS_PORT_DISCONNECTED
;
108 else if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) != LPCP_COMMUNICATION_PORT
)
110 /* Any other kind of port, use the connection port */
111 ConnectionPort
= QueuePort
= Port
->ConnectionPort
;
115 LpcpFreeToPortZone(Message
, 3);
116 return STATUS_PORT_DISCONNECTED
;
120 /* If we have a connection port, reference it */
121 if (ConnectionPort
) ObReferenceObject(ConnectionPort
);
126 /* For connection ports, use the port itself */
127 QueuePort
= PortObject
;
130 /* Make sure we have a port */
133 /* Generate the Message ID and set it */
134 Message
->Request
.MessageId
= LpcpNextMessageId
++;
135 if (!LpcpNextMessageId
) LpcpNextMessageId
= 1;
136 Message
->Request
.CallbackId
= 0;
138 /* No Message ID for the thread */
139 PsGetCurrentThread()->LpcReplyMessageId
= 0;
141 /* Insert the message in our chain */
142 InsertTailList(&QueuePort
->MsgQueue
.ReceiveHead
, &Message
->Entry
);
144 /* Release the lock and release the semaphore */
145 KeEnterCriticalRegion();
146 KeReleaseGuardedMutex(&LpcpLock
);
147 LpcpCompleteWait(QueuePort
->MsgQueue
.Semaphore
);
149 /* If this is a waitable port, wake it up */
150 if (QueuePort
->Flags
& LPCP_WAITABLE_PORT
)
153 KeSetEvent(&QueuePort
->WaitEvent
, IO_NO_INCREMENT
, FALSE
);
157 KeLeaveCriticalRegion();
158 if (ConnectionPort
) ObDereferenceObject(ConnectionPort
);
159 LPCTRACE(LPC_SEND_DEBUG
, "Port: %p. Message: %p\n", QueuePort
, Message
);
160 return STATUS_SUCCESS
;
163 /* If we got here, then free the message and fail */
164 LpcpFreeToPortZone(Message
, 3);
165 if (ConnectionPort
) ObDereferenceObject(ConnectionPort
);
166 return STATUS_PORT_DISCONNECTED
;
174 LpcRequestWaitReplyPort(IN PVOID PortObject
,
175 IN PPORT_MESSAGE LpcRequest
,
176 OUT PPORT_MESSAGE LpcReply
)
178 PLPCP_PORT_OBJECT Port
, QueuePort
, ReplyPort
, ConnectionPort
= NULL
;
179 KPROCESSOR_MODE PreviousMode
= KeGetPreviousMode();
180 NTSTATUS Status
= STATUS_SUCCESS
;
181 PLPCP_MESSAGE Message
;
182 PETHREAD Thread
= PsGetCurrentThread();
183 BOOLEAN Callback
= FALSE
;
184 PKSEMAPHORE Semaphore
;
188 Port
= (PLPCP_PORT_OBJECT
)PortObject
;
190 LPCTRACE(LPC_SEND_DEBUG
,
191 "Port: %p. Messages: %p/%p. Type: %lx\n",
195 LpcpGetMessageType(LpcRequest
));
197 /* Check if the thread is dying */
198 if (Thread
->LpcExitThreadCalled
) return STATUS_THREAD_IS_TERMINATING
;
200 /* Check if this is an LPC Request */
201 MessageType
= LpcpGetMessageType(LpcRequest
);
207 /* Assume LPC request */
208 MessageType
= LPC_REQUEST
;
211 /* LPC request callback */
214 /* This is a callback */
219 case LPC_CLIENT_DIED
:
220 case LPC_PORT_CLOSED
:
222 case LPC_DEBUG_EVENT
:
223 case LPC_ERROR_EVENT
:
230 /* Invalid message type */
231 return STATUS_INVALID_PARAMETER
;
234 /* Set the request type */
235 LpcRequest
->u2
.s2
.Type
= MessageType
;
237 /* Validate the message length */
238 if (((ULONG
)LpcRequest
->u1
.s1
.TotalLength
> Port
->MaxMessageLength
) ||
239 ((ULONG
)LpcRequest
->u1
.s1
.TotalLength
<= (ULONG
)LpcRequest
->u1
.s1
.DataLength
))
242 return STATUS_PORT_MESSAGE_TOO_LONG
;
245 /* Allocate a message from the port zone */
246 Message
= LpcpAllocateFromPortZone();
249 /* Fail if we couldn't allocate a message */
250 return STATUS_NO_MEMORY
;
253 /* Check if this is a callback */
257 Semaphore
= NULL
; // we'd use the Thread Semaphore here
259 return STATUS_NOT_IMPLEMENTED
;
263 /* No callback, just copy the message */
264 LpcpMoveMessage(&Message
->Request
,
270 /* Acquire the LPC lock */
271 KeAcquireGuardedMutex(&LpcpLock
);
273 /* Right now clear the port context */
274 Message
->PortContext
= NULL
;
276 /* Check if this is a not connection port */
277 if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) != LPCP_CONNECTION_PORT
)
279 /* We want the connected port */
280 QueuePort
= Port
->ConnectedPort
;
283 /* We have no connected port, fail */
284 LpcpFreeToPortZone(Message
, 3);
285 return STATUS_PORT_DISCONNECTED
;
288 /* This will be the rundown port */
289 ReplyPort
= QueuePort
;
291 /* Check if this is a communication port */
292 if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) == LPCP_CLIENT_PORT
)
294 /* Copy the port context and use the connection port */
295 Message
->PortContext
= QueuePort
->PortContext
;
296 ConnectionPort
= QueuePort
= Port
->ConnectionPort
;
300 LpcpFreeToPortZone(Message
, 3);
301 return STATUS_PORT_DISCONNECTED
;
304 else if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) !=
305 LPCP_COMMUNICATION_PORT
)
307 /* Use the connection port for anything but communication ports */
308 ConnectionPort
= QueuePort
= Port
->ConnectionPort
;
312 LpcpFreeToPortZone(Message
, 3);
313 return STATUS_PORT_DISCONNECTED
;
317 /* Reference the connection port if it exists */
318 if (ConnectionPort
) ObReferenceObject(ConnectionPort
);
322 /* Otherwise, for a connection port, use the same port object */
323 QueuePort
= ReplyPort
= Port
;
326 /* No reply thread */
327 Message
->RepliedToThread
= NULL
;
328 Message
->SenderPort
= Port
;
330 /* Generate the Message ID and set it */
331 Message
->Request
.MessageId
= LpcpNextMessageId
++;
332 if (!LpcpNextMessageId
) LpcpNextMessageId
= 1;
333 Message
->Request
.CallbackId
= 0;
335 /* Set the message ID for our thread now */
336 Thread
->LpcReplyMessageId
= Message
->Request
.MessageId
;
337 Thread
->LpcReplyMessage
= NULL
;
339 /* Insert the message in our chain */
340 InsertTailList(&QueuePort
->MsgQueue
.ReceiveHead
, &Message
->Entry
);
341 InsertTailList(&ReplyPort
->LpcReplyChainHead
, &Thread
->LpcReplyChain
);
342 LpcpSetPortToThread(Thread
, Port
);
344 /* Release the lock and get the semaphore we'll use later */
345 KeEnterCriticalRegion();
346 KeReleaseGuardedMutex(&LpcpLock
);
347 Semaphore
= QueuePort
->MsgQueue
.Semaphore
;
349 /* If this is a waitable port, wake it up */
350 if (QueuePort
->Flags
& LPCP_WAITABLE_PORT
)
353 KeSetEvent(&QueuePort
->WaitEvent
, IO_NO_INCREMENT
, FALSE
);
357 /* Now release the semaphore */
358 LpcpCompleteWait(Semaphore
);
359 KeLeaveCriticalRegion();
361 /* And let's wait for the reply */
362 LpcpReplyWait(&Thread
->LpcReplySemaphore
, PreviousMode
);
364 /* Acquire the LPC lock */
365 KeAcquireGuardedMutex(&LpcpLock
);
367 /* Get the LPC Message and clear our thread's reply data */
368 Message
= LpcpGetMessageFromThread(Thread
);
369 Thread
->LpcReplyMessage
= NULL
;
370 Thread
->LpcReplyMessageId
= 0;
372 /* Check if we have anything on the reply chain*/
373 if (!IsListEmpty(&Thread
->LpcReplyChain
))
375 /* Remove this thread and reinitialize the list */
376 RemoveEntryList(&Thread
->LpcReplyChain
);
377 InitializeListHead(&Thread
->LpcReplyChain
);
380 /* Release the lock */
381 KeReleaseGuardedMutex(&LpcpLock
);
383 /* Check if we got a reply */
384 if (Status
== STATUS_SUCCESS
)
386 /* Check if we have a valid message */
389 LPCTRACE(LPC_SEND_DEBUG
,
390 "Reply Messages: %p/%p\n",
392 (&Message
->Request
) + 1);
394 /* Move the message */
395 LpcpMoveMessage(LpcReply
,
397 (&Message
->Request
) + 1,
401 /* Acquire the lock */
402 KeAcquireGuardedMutex(&LpcpLock
);
404 /* Check if we replied to a thread */
405 if (Message
->RepliedToThread
)
408 ObDereferenceObject(Message
->RepliedToThread
);
409 Message
->RepliedToThread
= NULL
;
413 /* Free the message */
414 LpcpFreeToPortZone(Message
, 3);
418 /* We don't have a reply */
419 Status
= STATUS_LPC_REPLY_LOST
;
424 /* The wait failed, free the message */
425 if (Message
) LpcpFreeToPortZone(Message
, 0);
429 LPCTRACE(LPC_SEND_DEBUG
,
430 "Port: %p. Status: %p\n",
434 /* Dereference the connection port */
435 if (ConnectionPort
) ObDereferenceObject(ConnectionPort
);
444 NtRequestPort(IN HANDLE PortHandle
,
445 IN PPORT_MESSAGE LpcMessage
)
448 return STATUS_NOT_IMPLEMENTED
;
456 NtRequestWaitReplyPort(IN HANDLE PortHandle
,
457 IN PPORT_MESSAGE LpcRequest
,
458 IN OUT PPORT_MESSAGE LpcReply
)
460 PLPCP_PORT_OBJECT Port
, QueuePort
, ReplyPort
, ConnectionPort
= NULL
;
461 KPROCESSOR_MODE PreviousMode
= KeGetPreviousMode();
463 PLPCP_MESSAGE Message
;
464 PETHREAD Thread
= PsGetCurrentThread();
466 PKSEMAPHORE Semaphore
;
469 LPCTRACE(LPC_SEND_DEBUG
,
470 "Handle: %lx. Messages: %p/%p. Type: %lx\n",
474 LpcpGetMessageType(LpcRequest
));
476 /* Check if the thread is dying */
477 if (Thread
->LpcExitThreadCalled
) return STATUS_THREAD_IS_TERMINATING
;
479 /* Check if this is an LPC Request */
480 if (LpcpGetMessageType(LpcRequest
) == LPC_REQUEST
)
482 /* Then it's a callback */
485 else if (LpcpGetMessageType(LpcRequest
))
487 /* This is a not kernel-mode message */
488 return STATUS_INVALID_PARAMETER
;
492 /* This is a kernel-mode message without a callback */
493 LpcRequest
->u2
.s2
.Type
|= LPC_REQUEST
;
497 /* Get the message type */
498 MessageType
= LpcRequest
->u2
.s2
.Type
;
500 /* Validate the length */
501 if (((ULONG
)LpcRequest
->u1
.s1
.DataLength
+ sizeof(PORT_MESSAGE
)) >
502 (ULONG
)LpcRequest
->u1
.s1
.TotalLength
)
505 return STATUS_INVALID_PARAMETER
;
508 /* Reference the object */
509 Status
= ObReferenceObjectByHandle(PortHandle
,
515 if (!NT_SUCCESS(Status
)) return Status
;
517 /* Validate the message length */
518 if (((ULONG
)LpcRequest
->u1
.s1
.TotalLength
> Port
->MaxMessageLength
) ||
519 ((ULONG
)LpcRequest
->u1
.s1
.TotalLength
<= (ULONG
)LpcRequest
->u1
.s1
.DataLength
))
522 ObDereferenceObject(Port
);
523 return STATUS_PORT_MESSAGE_TOO_LONG
;
526 /* Allocate a message from the port zone */
527 Message
= LpcpAllocateFromPortZone();
530 /* Fail if we couldn't allocate a message */
531 ObDereferenceObject(Port
);
532 return STATUS_NO_MEMORY
;
535 /* Check if this is a callback */
539 Semaphore
= NULL
; // we'd use the Thread Semaphore here
544 /* No callback, just copy the message */
548 LpcpMoveMessage(&Message
->Request
,
554 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
557 LpcpFreeToPortZone(Message
, 0);
558 ObDereferenceObject(Port
);
559 _SEH2_YIELD(return _SEH2_GetExceptionCode());
563 /* Acquire the LPC lock */
564 KeAcquireGuardedMutex(&LpcpLock
);
566 /* Right now clear the port context */
567 Message
->PortContext
= NULL
;
569 /* Check if this is a not connection port */
570 if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) != LPCP_CONNECTION_PORT
)
572 /* We want the connected port */
573 QueuePort
= Port
->ConnectedPort
;
576 /* We have no connected port, fail */
577 LpcpFreeToPortZone(Message
, 3);
578 ObDereferenceObject(Port
);
579 return STATUS_PORT_DISCONNECTED
;
582 /* This will be the rundown port */
583 ReplyPort
= QueuePort
;
585 /* Check if this is a communication port */
586 if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) == LPCP_CLIENT_PORT
)
588 /* Copy the port context and use the connection port */
589 Message
->PortContext
= QueuePort
->PortContext
;
590 ConnectionPort
= QueuePort
= Port
->ConnectionPort
;
594 LpcpFreeToPortZone(Message
, 3);
595 ObDereferenceObject(Port
);
596 return STATUS_PORT_DISCONNECTED
;
599 else if ((Port
->Flags
& LPCP_PORT_TYPE_MASK
) !=
600 LPCP_COMMUNICATION_PORT
)
602 /* Use the connection port for anything but communication ports */
603 ConnectionPort
= QueuePort
= Port
->ConnectionPort
;
607 LpcpFreeToPortZone(Message
, 3);
608 ObDereferenceObject(Port
);
609 return STATUS_PORT_DISCONNECTED
;
613 /* Reference the connection port if it exists */
614 if (ConnectionPort
) ObReferenceObject(ConnectionPort
);
618 /* Otherwise, for a connection port, use the same port object */
619 QueuePort
= ReplyPort
= Port
;
622 /* No reply thread */
623 Message
->RepliedToThread
= NULL
;
624 Message
->SenderPort
= Port
;
626 /* Generate the Message ID and set it */
627 Message
->Request
.MessageId
= LpcpNextMessageId
++;
628 if (!LpcpNextMessageId
) LpcpNextMessageId
= 1;
629 Message
->Request
.CallbackId
= 0;
631 /* Set the message ID for our thread now */
632 Thread
->LpcReplyMessageId
= Message
->Request
.MessageId
;
633 Thread
->LpcReplyMessage
= NULL
;
635 /* Insert the message in our chain */
636 InsertTailList(&QueuePort
->MsgQueue
.ReceiveHead
, &Message
->Entry
);
637 InsertTailList(&ReplyPort
->LpcReplyChainHead
, &Thread
->LpcReplyChain
);
638 LpcpSetPortToThread(Thread
, Port
);
640 /* Release the lock and get the semaphore we'll use later */
641 KeEnterCriticalRegion();
642 KeReleaseGuardedMutex(&LpcpLock
);
643 Semaphore
= QueuePort
->MsgQueue
.Semaphore
;
645 /* If this is a waitable port, wake it up */
646 if (QueuePort
->Flags
& LPCP_WAITABLE_PORT
)
649 KeSetEvent(&QueuePort
->WaitEvent
, IO_NO_INCREMENT
, FALSE
);
653 /* Now release the semaphore */
654 LpcpCompleteWait(Semaphore
);
655 KeLeaveCriticalRegion();
657 /* And let's wait for the reply */
658 LpcpReplyWait(&Thread
->LpcReplySemaphore
, PreviousMode
);
660 /* Acquire the LPC lock */
661 KeAcquireGuardedMutex(&LpcpLock
);
663 /* Get the LPC Message and clear our thread's reply data */
664 Message
= LpcpGetMessageFromThread(Thread
);
665 Thread
->LpcReplyMessage
= NULL
;
666 Thread
->LpcReplyMessageId
= 0;
668 /* Check if we have anything on the reply chain*/
669 if (!IsListEmpty(&Thread
->LpcReplyChain
))
671 /* Remove this thread and reinitialize the list */
672 RemoveEntryList(&Thread
->LpcReplyChain
);
673 InitializeListHead(&Thread
->LpcReplyChain
);
676 /* Release the lock */
677 KeReleaseGuardedMutex(&LpcpLock
);
679 /* Check if we got a reply */
680 if (Status
== STATUS_SUCCESS
)
682 /* Check if we have a valid message */
685 LPCTRACE(LPC_SEND_DEBUG
,
686 "Reply Messages: %p/%p\n",
688 (&Message
->Request
) + 1);
690 /* Move the message */
693 LpcpMoveMessage(LpcReply
,
695 (&Message
->Request
) + 1,
699 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
701 Status
= _SEH2_GetExceptionCode();
705 /* Check if this is an LPC request with data information */
706 if ((LpcpGetMessageType(&Message
->Request
) == LPC_REQUEST
) &&
707 (Message
->Request
.u2
.s2
.DataInfoOffset
))
709 /* Save the data information */
710 LpcpSaveDataInfoMessage(Port
, Message
, 0);
714 /* Otherwise, just free it */
715 LpcpFreeToPortZone(Message
, 0);
720 /* We don't have a reply */
721 Status
= STATUS_LPC_REPLY_LOST
;
726 /* The wait failed, free the message */
727 if (Message
) LpcpFreeToPortZone(Message
, 0);
731 LPCTRACE(LPC_SEND_DEBUG
,
732 "Port: %p. Status: %p\n",
735 ObDereferenceObject(Port
);
736 if (ConnectionPort
) ObDereferenceObject(ConnectionPort
);