3 * reactos/subsys/csrss/api/wapi.c
5 * CSRSS port message processing
7 * ReactOS Operating System
11 /* INCLUDES ******************************************************************/
13 #define NTOS_MODE_USER
15 #include <csrss/csrss.h>
16 #include <ddk/ntddk.h>
25 /* GLOBALS *******************************************************************/
27 HANDLE CsrssApiHeap
= (HANDLE
) 0;
29 static unsigned ApiDefinitionsCount
= 0;
30 static PCSRSS_API_DEFINITION ApiDefinitions
= NULL
;
32 /* FUNCTIONS *****************************************************************/
35 CsrApiRegisterDefinitions(PCSRSS_API_DEFINITION NewDefinitions
)
38 PCSRSS_API_DEFINITION Scan
;
39 PCSRSS_API_DEFINITION New
;
41 DPRINT("CSR: %s called", __FUNCTION__
);
44 for (Scan
= NewDefinitions
; 0 != Scan
->Handler
; Scan
++)
49 New
= RtlAllocateHeap(CsrssApiHeap
, 0,
50 (ApiDefinitionsCount
+ NewCount
)
51 * sizeof(CSRSS_API_DEFINITION
));
54 DPRINT1("Unable to allocate memory\n");
55 return STATUS_NO_MEMORY
;
57 if (0 != ApiDefinitionsCount
)
59 RtlCopyMemory(New
, ApiDefinitions
,
60 ApiDefinitionsCount
* sizeof(CSRSS_API_DEFINITION
));
61 RtlFreeHeap(CsrssApiHeap
, 0, ApiDefinitions
);
63 RtlCopyMemory(New
+ ApiDefinitionsCount
, NewDefinitions
,
64 NewCount
* sizeof(CSRSS_API_DEFINITION
));
66 ApiDefinitionsCount
+= NewCount
;
68 return STATUS_SUCCESS
;
72 CsrApiCallHandler(PCSRSS_PROCESS_DATA ProcessData
,
73 PCSRSS_API_REQUEST Request
,
74 PCSRSS_API_REPLY Reply
)
80 for (DefIndex
= 0; ! Found
&& DefIndex
< ApiDefinitionsCount
; DefIndex
++)
82 if (ApiDefinitions
[DefIndex
].Type
== Request
->Type
)
84 if (Request
->Header
.DataSize
< ApiDefinitions
[DefIndex
].MinRequestSize
)
86 DPRINT1("Request type %d min request size %d actual %d\n",
87 Request
->Type
, ApiDefinitions
[DefIndex
].MinRequestSize
,
88 Request
->Header
.DataSize
);
89 Reply
->Status
= STATUS_INVALID_PARAMETER
;
93 (ApiDefinitions
[DefIndex
].Handler
)(ProcessData
, Request
, Reply
);
100 DPRINT1("CSR: Unknown request type 0x%x\n", Request
->Type
);
101 Reply
->Header
.MessageSize
= sizeof(CSRSS_API_REPLY
);
102 Reply
->Header
.DataSize
= sizeof(CSRSS_API_REPLY
) - LPC_MESSAGE_BASE_SIZE
;
103 Reply
->Status
= STATUS_INVALID_SYSTEM_SERVICE
;
108 ClientConnectionThread(HANDLE ServerPort
)
111 LPC_MAX_MESSAGE LpcReply
;
112 LPC_MAX_MESSAGE LpcRequest
;
113 PCSRSS_API_REQUEST Request
;
114 PCSRSS_PROCESS_DATA ProcessData
;
115 PCSRSS_API_REPLY Reply
;
117 DPRINT("CSR: %s called", __FUNCTION__
);
123 Status
= NtReplyWaitReceivePort(ServerPort
,
127 if (! NT_SUCCESS(Status
))
129 DPRINT1("CSR: NtReplyWaitReceivePort failed\n");
133 if (LpcRequest
.Header
.MessageType
== LPC_PORT_CLOSED
)
135 CsrFreeProcessData( LpcRequest
.Header
.ClientId
.UniqueProcess
);
139 Request
= (PCSRSS_API_REQUEST
)&LpcRequest
;
140 Reply
= (PCSRSS_API_REPLY
)&LpcReply
;
142 ProcessData
= CsrGetProcessData(LpcRequest
.Header
.ClientId
.UniqueProcess
);
143 if (ProcessData
== NULL
)
145 DPRINT1("CSR: Message %d: Unable to find data for process 0x%x\n",
146 LpcRequest
.Header
.MessageType
, LpcRequest
.Header
.ClientId
.UniqueProcess
);
151 CsrApiCallHandler(ProcessData
, Request
, Reply
);
154 RtlRosExitUserThread(STATUS_SUCCESS
);
157 /**********************************************************************
159 * ServerApiPortThread/1
162 * Handle connection requests from clients to the port
163 * "\Windows\ApiPort".
166 ServerApiPortThread (PVOID PortHandle
)
168 NTSTATUS Status
= STATUS_SUCCESS
;
169 LPC_MAX_MESSAGE Request
;
170 HANDLE hApiListenPort
= * (PHANDLE
) PortHandle
;
171 HANDLE ServerPort
= (HANDLE
) 0;
172 HANDLE ServerThread
= (HANDLE
) 0;
173 PCSRSS_PROCESS_DATA ProcessData
= NULL
;
175 CsrInitProcessData();
177 DPRINT("CSR: %s called", __FUNCTION__
);
181 LPC_SECTION_READ LpcRead
;
184 Status
= NtListenPort (hApiListenPort
, & Request
.Header
);
185 if (!NT_SUCCESS(Status
))
187 DPRINT1("CSR: NtListenPort() failed\n");
190 Status
= NtAcceptConnectPort(& ServerPort
,
196 if (!NT_SUCCESS(Status
))
198 DPRINT1("CSR: NtAcceptConnectPort() failed\n");
202 ProcessData
= CsrCreateProcessData(Request
.Header
.ClientId
.UniqueProcess
);
203 if (ProcessData
== NULL
)
205 DPRINT1("Unable to allocate or find data for process 0x%x\n",
206 Request
.Header
.ClientId
.UniqueProcess
);
207 Status
= STATUS_UNSUCCESSFUL
;
212 ProcessData
->CsrSectionViewBase
= LpcRead
.ViewBase
;
213 ProcessData
->CsrSectionViewSize
= LpcRead
.ViewSize
;
215 Status
= NtCompleteConnectPort(ServerPort
);
216 if (!NT_SUCCESS(Status
))
218 DPRINT1("CSR: NtCompleteConnectPort() failed\n");
222 Status
= RtlCreateUserThread(NtCurrentProcess(),
228 (PTHREAD_START_ROUTINE
)ClientConnectionThread
,
232 if (!NT_SUCCESS(Status
))
234 DPRINT1("CSR: Unable to create server thread\n");
237 NtClose(ServerThread
);
244 NtTerminateThread(NtCurrentThread(), Status
);
248 /**********************************************************************
250 * ServerSbApiPortThread/1
253 * Handle connection requests from SM to the port
254 * "\Windows\SbApiPort". We will accept only one
255 * connection request (from the SM).
258 ServerSbApiPortThread (PVOID PortHandle
)
260 HANDLE hSbApiPortListen
= * (PHANDLE
) PortHandle
;
261 HANDLE hConnectedPort
= (HANDLE
) 0;
262 LPC_MAX_MESSAGE Request
= {{0}};
263 PVOID Context
= NULL
;
264 NTSTATUS Status
= STATUS_SUCCESS
;
266 DPRINT("CSR: %s called\n", __FUNCTION__
);
268 Status
= NtListenPort (hSbApiPortListen
, & Request
.Header
);
269 if (!NT_SUCCESS(Status
))
271 DPRINT1("CSR: %s: NtListenPort(SB) failed (Status=0x%08lx)\n",
272 __FUNCTION__
, Status
);
275 Status
= NtAcceptConnectPort (& hConnectedPort
,
281 if(!NT_SUCCESS(Status
))
283 DPRINT1("CSR: %s: NtAcceptConnectPort() failed (Status=0x%08lx)\n",
284 __FUNCTION__
, Status
);
287 Status
= NtCompleteConnectPort (hConnectedPort
);
288 if(!NT_SUCCESS(Status
))
290 DPRINT1("CSR: %s: NtCompleteConnectPort() failed (Status=0x%08lx)\n",
291 __FUNCTION__
, Status
);
294 PLPC_MESSAGE Reply
= NULL
;
296 * Tell the init thread the SM gave the
297 * green light for boostrapping.
299 Status
= NtSetEvent (hBootstrapOk
, NULL
);
300 if(!NT_SUCCESS(Status
))
302 DPRINT1("CSR: %s: NtSetEvent failed (Status=0x%08lx)\n",
303 __FUNCTION__
, Status
);
305 /* Wait for messages from the SM */
309 Status
= NtReplyWaitReceivePort(hConnectedPort
,
313 if(!NT_SUCCESS(Status
))
315 DPRINT1("CSR: %s: NtReplyWaitReceivePort failed (Status=0x%08lx)\n",
316 __FUNCTION__
, Status
);
319 switch (Request
.Header
.MessageType
)//fix .h PORT_MESSAGE_TYPE(Request))
323 DPRINT1("CSR: %s received message (type=%d)\n",
324 __FUNCTION__
, Request
.Header
.MessageType
);
331 DPRINT1("CSR: %s: terminating!\n", __FUNCTION__
);
332 if(hConnectedPort
) NtClose (hConnectedPort
);
333 NtClose (hSbApiPortListen
);
334 NtTerminateThread (NtCurrentThread(), Status
);