2007-01-10 04:27:40 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2000-06-04 17:27:39 +00:00
|
|
|
* FILE: ntoskrnl/lpc/create.c
|
2007-01-10 04:27:40 +00:00
|
|
|
* PURPOSE: Local Procedure Call: Port/Queue/Message Creation
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
2000-06-04 17:27:39 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* INCLUDES ******************************************************************/
|
2000-06-04 17:27:39 +00:00
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2000-06-04 17:27:39 +00:00
|
|
|
#define NDEBUG
|
2007-01-10 06:09:57 +00:00
|
|
|
#include <debug.h>
|
2000-06-04 17:27:39 +00:00
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
LpcpInitializePortQueue(IN PLPCP_PORT_OBJECT Port)
|
2001-01-29 00:13:22 +00:00
|
|
|
{
|
2007-01-10 04:27:40 +00:00
|
|
|
PLPCP_NONPAGED_PORT_QUEUE MessageQueue;
|
2016-11-07 00:55:34 +00:00
|
|
|
|
2007-01-21 17:21:42 +00:00
|
|
|
PAGED_CODE();
|
2007-01-10 04:27:40 +00:00
|
|
|
|
|
|
|
/* Allocate the queue */
|
|
|
|
MessageQueue = ExAllocatePoolWithTag(NonPagedPool,
|
2016-11-07 00:55:34 +00:00
|
|
|
sizeof(*MessageQueue),
|
2009-08-24 18:19:53 +00:00
|
|
|
'troP');
|
2007-01-10 04:27:40 +00:00
|
|
|
if (!MessageQueue) return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
|
|
|
/* Set it up */
|
|
|
|
KeInitializeSemaphore(&MessageQueue->Semaphore, 0, MAXLONG);
|
|
|
|
MessageQueue->BackPointer = Port;
|
|
|
|
|
|
|
|
/* And link it with the Paged Pool part */
|
|
|
|
Port->MsgQueue.Semaphore = &MessageQueue->Semaphore;
|
|
|
|
InitializeListHead(&Port->MsgQueue.ReceiveHead);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
LpcpCreatePort(OUT PHANDLE PortHandle,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
IN ULONG MaxConnectionInfoLength,
|
|
|
|
IN ULONG MaxMessageLength,
|
|
|
|
IN ULONG MaxPoolUsage,
|
|
|
|
IN BOOLEAN Waitable)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
2016-11-07 00:55:34 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
|
2016-11-07 01:24:24 +00:00
|
|
|
UNICODE_STRING CapturedObjectName, *ObjectName;
|
2007-01-10 04:27:40 +00:00
|
|
|
PLPCP_PORT_OBJECT Port;
|
2014-05-25 10:21:08 +00:00
|
|
|
HANDLE Handle;
|
2016-11-07 00:55:34 +00:00
|
|
|
|
2007-01-21 17:21:42 +00:00
|
|
|
PAGED_CODE();
|
2007-01-10 04:27:40 +00:00
|
|
|
LPCTRACE(LPC_CREATE_DEBUG, "Name: %wZ\n", ObjectAttributes->ObjectName);
|
|
|
|
|
2016-11-07 01:24:24 +00:00
|
|
|
RtlInitEmptyUnicodeString(&CapturedObjectName, NULL, 0);
|
|
|
|
|
2014-05-25 10:21:08 +00:00
|
|
|
/* Check if the call comes from user mode */
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Probe the PortHandle */
|
|
|
|
ProbeForWriteHandle(PortHandle);
|
|
|
|
|
2016-11-07 01:24:24 +00:00
|
|
|
/* Probe the ObjectAttributes and its object name (not the buffer) */
|
|
|
|
ProbeForRead(ObjectAttributes, sizeof(*ObjectAttributes), sizeof(ULONG));
|
|
|
|
ObjectName = ((volatile OBJECT_ATTRIBUTES*)ObjectAttributes)->ObjectName;
|
|
|
|
if (ObjectName)
|
|
|
|
{
|
|
|
|
ProbeForRead(ObjectName, sizeof(*ObjectName), 1);
|
|
|
|
CapturedObjectName = *(volatile UNICODE_STRING*)ObjectName;
|
|
|
|
}
|
2014-05-25 10:21:08 +00:00
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
/* Return the exception code */
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-07 01:24:24 +00:00
|
|
|
if (ObjectAttributes->ObjectName)
|
|
|
|
CapturedObjectName = *(ObjectAttributes->ObjectName);
|
2014-05-25 10:21:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 01:24:24 +00:00
|
|
|
/* Normalize the buffer pointer in case we don't have a name */
|
|
|
|
if (CapturedObjectName.Length == 0)
|
|
|
|
CapturedObjectName.Buffer = NULL;
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Create the Object */
|
|
|
|
Status = ObCreateObject(PreviousMode,
|
|
|
|
LpcPortObjectType,
|
|
|
|
ObjectAttributes,
|
|
|
|
PreviousMode,
|
|
|
|
NULL,
|
|
|
|
sizeof(LPCP_PORT_OBJECT),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(PVOID*)&Port);
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
|
|
|
|
/* Set up the Object */
|
|
|
|
RtlZeroMemory(Port, sizeof(LPCP_PORT_OBJECT));
|
|
|
|
Port->ConnectionPort = Port;
|
|
|
|
Port->Creator = PsGetCurrentThread()->Cid;
|
|
|
|
InitializeListHead(&Port->LpcDataInfoChainHead);
|
|
|
|
InitializeListHead(&Port->LpcReplyChainHead);
|
|
|
|
|
|
|
|
/* Check if we don't have a name */
|
2016-11-07 01:24:24 +00:00
|
|
|
if (CapturedObjectName.Buffer == NULL)
|
2001-12-02 23:34:43 +00:00
|
|
|
{
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Set up for an unconnected port */
|
|
|
|
Port->Flags = LPCP_UNCONNECTED_PORT;
|
|
|
|
Port->ConnectedPort = Port;
|
|
|
|
Port->ServerProcess = NULL;
|
2001-12-02 23:34:43 +00:00
|
|
|
}
|
2007-01-10 04:27:40 +00:00
|
|
|
else
|
2001-12-02 23:34:43 +00:00
|
|
|
{
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Set up for a named connection port */
|
|
|
|
Port->Flags = LPCP_CONNECTION_PORT;
|
|
|
|
Port->ServerProcess = PsGetCurrentProcess();
|
|
|
|
|
|
|
|
/* Don't let the process die on us */
|
|
|
|
ObReferenceObject(Port->ServerProcess);
|
2001-12-02 23:34:43 +00:00
|
|
|
}
|
2007-01-10 04:27:40 +00:00
|
|
|
|
|
|
|
/* Check if this is a waitable port */
|
|
|
|
if (Waitable) Port->Flags |= LPCP_WAITABLE_PORT;
|
|
|
|
|
|
|
|
/* Setup the port queue */
|
|
|
|
Status = LpcpInitializePortQueue(Port);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2001-12-02 23:34:43 +00:00
|
|
|
{
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Fail */
|
|
|
|
ObDereferenceObject(Port);
|
|
|
|
return Status;
|
2001-12-02 23:34:43 +00:00
|
|
|
}
|
2001-01-29 00:13:22 +00:00
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Check if this is a waitable port */
|
|
|
|
if (Port->Flags & LPCP_WAITABLE_PORT)
|
2001-12-02 23:34:43 +00:00
|
|
|
{
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Setup the wait event */
|
|
|
|
KeInitializeEvent(&Port->WaitEvent, NotificationEvent, FALSE);
|
2001-12-02 23:34:43 +00:00
|
|
|
}
|
2003-09-25 20:09:56 +00:00
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Set the maximum message size allowed */
|
|
|
|
Port->MaxMessageLength = LpcpMaxMessageSize -
|
|
|
|
FIELD_OFFSET(LPCP_MESSAGE, Request);
|
|
|
|
|
|
|
|
/* Now subtract the actual message structures and get the data size */
|
|
|
|
Port->MaxConnectionInfoLength = Port->MaxMessageLength -
|
|
|
|
sizeof(PORT_MESSAGE) -
|
|
|
|
sizeof(LPCP_CONNECTION_MESSAGE);
|
|
|
|
|
|
|
|
/* Validate the sizes */
|
|
|
|
if (Port->MaxConnectionInfoLength < MaxConnectionInfoLength)
|
2001-12-02 23:34:43 +00:00
|
|
|
{
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Not enough space for your request */
|
|
|
|
ObDereferenceObject(Port);
|
|
|
|
return STATUS_INVALID_PARAMETER_3;
|
2001-12-02 23:34:43 +00:00
|
|
|
}
|
2007-01-10 04:27:40 +00:00
|
|
|
else if (Port->MaxMessageLength < MaxMessageLength)
|
2003-09-25 20:09:56 +00:00
|
|
|
{
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Not enough space for your request */
|
|
|
|
ObDereferenceObject(Port);
|
|
|
|
return STATUS_INVALID_PARAMETER_4;
|
2003-09-25 20:09:56 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Now set the custom setting */
|
|
|
|
Port->MaxMessageLength = MaxMessageLength;
|
|
|
|
|
|
|
|
/* Insert it now */
|
2016-11-07 00:55:34 +00:00
|
|
|
Status = ObInsertObject(Port,
|
2007-01-10 04:27:40 +00:00
|
|
|
NULL,
|
|
|
|
PORT_ALL_ACCESS,
|
|
|
|
0,
|
|
|
|
NULL,
|
2014-05-25 10:21:08 +00:00
|
|
|
&Handle);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Write back the handle, pointer was already probed */
|
|
|
|
*PortHandle = Handle;
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
2016-11-07 01:24:24 +00:00
|
|
|
/* An exception happened, close the opened handle */
|
|
|
|
ObCloseHandle(Handle, PreviousMode);
|
2014-05-25 10:21:08 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
2007-01-10 04:27:40 +00:00
|
|
|
|
|
|
|
/* Return success or the error */
|
2014-05-25 10:21:08 +00:00
|
|
|
LPCTRACE(LPC_CREATE_DEBUG, "Port: %p. Handle: %p\n", Port, Handle);
|
2007-01-10 04:27:40 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PUBLIC FUNCTIONS **********************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtCreatePort(OUT PHANDLE PortHandle,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
IN ULONG MaxConnectInfoLength,
|
|
|
|
IN ULONG MaxDataLength,
|
|
|
|
IN ULONG MaxPoolUsage)
|
|
|
|
{
|
|
|
|
PAGED_CODE();
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Call the internal API */
|
|
|
|
return LpcpCreatePort(PortHandle,
|
|
|
|
ObjectAttributes,
|
|
|
|
MaxConnectInfoLength,
|
|
|
|
MaxDataLength,
|
|
|
|
MaxPoolUsage,
|
|
|
|
FALSE);
|
2000-06-04 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
2001-01-29 00:13:22 +00:00
|
|
|
*/
|
2007-01-10 04:27:40 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtCreateWaitablePort(OUT PHANDLE PortHandle,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
IN ULONG MaxConnectInfoLength,
|
|
|
|
IN ULONG MaxDataLength,
|
|
|
|
IN ULONG MaxPoolUsage)
|
2001-01-29 00:13:22 +00:00
|
|
|
{
|
2007-01-10 04:27:40 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Call the internal API */
|
|
|
|
return LpcpCreatePort(PortHandle,
|
|
|
|
ObjectAttributes,
|
|
|
|
MaxConnectInfoLength,
|
|
|
|
MaxDataLength,
|
|
|
|
MaxPoolUsage,
|
|
|
|
TRUE);
|
2001-01-29 00:13:22 +00:00
|
|
|
}
|
2000-06-04 17:27:39 +00:00
|
|
|
|
|
|
|
/* EOF */
|