3 * reactos/subsys/csrss/api/wapi.c
5 * CSRSS port message processing
7 * ReactOS Operating System
11 /* INCLUDES ******************************************************************/
20 /* GLOBALS *******************************************************************/
22 extern HANDLE hApiPort
;
24 HANDLE CsrssApiHeap
= (HANDLE
) 0;
26 static unsigned ApiDefinitionsCount
= 0;
27 static PCSRSS_API_DEFINITION ApiDefinitions
= NULL
;
29 /* FUNCTIONS *****************************************************************/
32 CsrApiRegisterDefinitions(PCSRSS_API_DEFINITION NewDefinitions
)
35 PCSRSS_API_DEFINITION Scan
;
36 PCSRSS_API_DEFINITION New
;
38 DPRINT("CSR: %s called\n", __FUNCTION__
);
41 for (Scan
= NewDefinitions
; 0 != Scan
->Handler
; Scan
++)
46 New
= RtlAllocateHeap(CsrssApiHeap
, 0,
47 (ApiDefinitionsCount
+ NewCount
)
48 * sizeof(CSRSS_API_DEFINITION
));
51 DPRINT1("Unable to allocate memory\n");
52 return STATUS_NO_MEMORY
;
54 if (0 != ApiDefinitionsCount
)
56 RtlCopyMemory(New
, ApiDefinitions
,
57 ApiDefinitionsCount
* sizeof(CSRSS_API_DEFINITION
));
58 RtlFreeHeap(CsrssApiHeap
, 0, ApiDefinitions
);
60 RtlCopyMemory(New
+ ApiDefinitionsCount
, NewDefinitions
,
61 NewCount
* sizeof(CSRSS_API_DEFINITION
));
63 ApiDefinitionsCount
+= NewCount
;
65 return STATUS_SUCCESS
;
70 CsrApiCallHandler(PCSRSS_PROCESS_DATA ProcessData
,
71 PCSR_API_MESSAGE Request
)
76 DPRINT("CSR: Calling handler for type: %x.\n", Request
->Type
);
77 Type
= Request
->Type
& 0xFFFF; /* FIXME: USE MACRO */
78 DPRINT("CSR: API Number: %x ServerID: %x\n",Type
, Request
->Type
>> 16);
80 /* FIXME: Extract DefIndex instead of looping */
81 for (DefIndex
= 0; DefIndex
< ApiDefinitionsCount
; DefIndex
++)
83 if (ApiDefinitions
[DefIndex
].Type
== Type
)
85 if (Request
->Header
.u1
.s1
.DataLength
< ApiDefinitions
[DefIndex
].MinRequestSize
)
87 DPRINT1("Request type %d min request size %d actual %d\n",
88 Type
, ApiDefinitions
[DefIndex
].MinRequestSize
,
89 Request
->Header
.u1
.s1
.DataLength
);
90 Request
->Status
= STATUS_INVALID_PARAMETER
;
94 Request
->Status
= (ApiDefinitions
[DefIndex
].Handler
)(ProcessData
, Request
);
99 DPRINT1("CSR: Unknown request type 0x%x\n", Request
->Type
);
100 Request
->Header
.u1
.s1
.TotalLength
= sizeof(CSR_API_MESSAGE
);
101 Request
->Header
.u1
.s1
.DataLength
= sizeof(CSR_API_MESSAGE
) - sizeof(PORT_MESSAGE
);
102 Request
->Status
= STATUS_INVALID_SYSTEM_SERVICE
;
106 CallHardError(IN PCSRSS_PROCESS_DATA ProcessData
,
107 IN PHARDERROR_MSG HardErrorMessage
);
112 CsrHandleHardError(IN PCSRSS_PROCESS_DATA ProcessData
,
113 IN OUT PHARDERROR_MSG Message
)
115 DPRINT1("CSR: received hard error %lx\n", Message
->Status
);
117 /* Call the hard error handler in win32csr */
118 (VOID
)CallHardError(ProcessData
, Message
);
122 CsrpHandleConnectionRequest (PPORT_MESSAGE Request
,
123 IN HANDLE hApiListenPort
)
126 HANDLE ServerPort
= (HANDLE
) 0;
127 PCSRSS_PROCESS_DATA ProcessData
= NULL
;
128 REMOTE_PORT_VIEW LpcRead
;
129 LpcRead
.Length
= sizeof(LpcRead
);
132 DPRINT("CSR: %s: Handling: %p\n", __FUNCTION__
, Request
);
134 Status
= NtAcceptConnectPort(&ServerPort
,
140 if (!NT_SUCCESS(Status
))
142 DPRINT1("CSR: NtAcceptConnectPort() failed\n");
146 ProcessData
= CsrGetProcessData(Request
->ClientId
.UniqueProcess
);
147 if (ProcessData
== NULL
)
149 ProcessData
= CsrCreateProcessData(Request
->ClientId
.UniqueProcess
);
150 if (ProcessData
== NULL
)
152 DPRINT1("Unable to allocate or find data for process 0x%x\n",
153 Request
->ClientId
.UniqueProcess
);
154 Status
= STATUS_UNSUCCESSFUL
;
159 ProcessData
->CsrSectionViewBase
= LpcRead
.ViewBase
;
160 ProcessData
->CsrSectionViewSize
= LpcRead
.ViewSize
;
161 ProcessData
->ServerCommunicationPort
= ServerPort
;
163 Status
= NtCompleteConnectPort(ServerPort
);
164 if (!NT_SUCCESS(Status
))
166 DPRINT1("CSR: NtCompleteConnectPort() failed\n");
170 HANDLE ServerThread
= (HANDLE
) 0;
171 Status
= RtlCreateUserThread(NtCurrentProcess(),
177 (PTHREAD_START_ROUTINE
)ClientConnectionThread
,
181 if (!NT_SUCCESS(Status
))
183 DPRINT1("CSR: Unable to create server thread\n");
187 NtClose(ServerThread
);
189 Status
= STATUS_SUCCESS
;
190 DPRINT("CSR: %s done\n", __FUNCTION__
);
196 ClientConnectionThread(HANDLE ServerPort
)
199 BYTE RawRequest
[LPC_MAX_DATA_LENGTH
];
200 PCSR_API_MESSAGE Request
= (PCSR_API_MESSAGE
)RawRequest
;
201 PCSR_API_MESSAGE Reply
;
202 PCSRSS_PROCESS_DATA ProcessData
;
204 DPRINT("CSR: %s called\n", __FUNCTION__
);
206 /* Reply must be NULL at the first call to NtReplyWaitReceivePort */
209 /* Loop and reply/wait for a new message */
212 /* Send the reply and wait for a new request */
213 Status
= NtReplyWaitReceivePort(hApiPort
,
217 /* Client died, continue */
218 if (Status
== STATUS_INVALID_CID
)
224 if (!NT_SUCCESS(Status
))
226 DPRINT1("NtReplyWaitReceivePort failed: %lx\n", Status
);
230 /* If the connection was closed, handle that */
231 if (Request
->Header
.u2
.s2
.Type
== LPC_PORT_CLOSED
)
233 DPRINT("Port died, oh well\n");
234 CsrFreeProcessData( Request
->Header
.ClientId
.UniqueProcess
);
238 if (Request
->Header
.u2
.s2
.Type
== LPC_CONNECTION_REQUEST
)
240 CsrpHandleConnectionRequest((PPORT_MESSAGE
)Request
, ServerPort
);
245 if (Request
->Header
.u2
.s2
.Type
== LPC_CLIENT_DIED
)
247 DPRINT("Client died, oh well\n");
252 if ((Request
->Header
.u2
.s2
.Type
!= LPC_ERROR_EVENT
) &&
253 (Request
->Header
.u2
.s2
.Type
!= LPC_REQUEST
))
255 DPRINT1("CSR: received message %d\n", Request
->Header
.u2
.s2
.Type
);
260 DPRINT("CSR: Got CSR API: %x [Message Origin: %x]\n",
262 Request
->Header
.ClientId
.UniqueThread
);
264 /* Get the Process Data */
265 ProcessData
= CsrGetProcessData(Request
->Header
.ClientId
.UniqueProcess
);
266 if (ProcessData
== NULL
)
268 DPRINT1("Message %d: Unable to find data for process 0x%x\n",
269 Request
->Header
.u2
.s2
.Type
,
270 Request
->Header
.ClientId
.UniqueProcess
);
273 if (ProcessData
->Terminated
)
275 DPRINT1("Message %d: process %p already terminated\n",
276 Request
->Type
, Request
->Header
.ClientId
.UniqueProcess
);
280 /* Check if we got a hard error */
281 if (Request
->Header
.u2
.s2
.Type
== LPC_ERROR_EVENT
)
283 /* Call the Handler */
284 CsrHandleHardError(ProcessData
, (PHARDERROR_MSG
)Request
);
288 /* Call the Handler */
289 CsrApiCallHandler(ProcessData
, Request
);
292 /* Send back the reply */
296 /* Close the port and exit the thread */
297 // NtClose(ServerPort);
299 DPRINT("CSR: %s done\n", __FUNCTION__
);
300 RtlExitUserThread(STATUS_SUCCESS
);
303 /**********************************************************************
305 * ServerApiPortThread/1
308 * Handle connection requests from clients to the port
309 * "\Windows\ApiPort".
313 ServerApiPortThread (HANDLE hApiListenPort
)
315 NTSTATUS Status
= STATUS_SUCCESS
;
316 BYTE RawRequest
[sizeof(PORT_MESSAGE
) + sizeof(CSR_CONNECTION_INFO
)];
317 PPORT_MESSAGE Request
= (PPORT_MESSAGE
)RawRequest
;
319 DPRINT("CSR: %s called", __FUNCTION__
);
323 REMOTE_PORT_VIEW LpcRead
;
324 LpcRead
.Length
= sizeof(LpcRead
);
326 Status
= NtListenPort (hApiListenPort
, Request
);
327 if (!NT_SUCCESS(Status
))
329 DPRINT1("CSR: NtListenPort() failed, status=%x\n", Status
);
333 Status
= CsrpHandleConnectionRequest(Request
, hApiListenPort
);
334 if(!NT_SUCCESS(Status
))
336 DPRINT1("CSR: %s: SmpHandleConnectionRequest failed (Status=0x%08lx)\n",
337 __FUNCTION__
, Status
);
342 NtClose(hApiListenPort
);
343 NtTerminateThread(NtCurrentThread(), Status
);
348 /**********************************************************************
350 * ServerSbApiPortThread/1
353 * Handle connection requests from SM to the port
354 * "\Windows\SbApiPort". We will accept only one
355 * connection request (from the SM).
358 ServerSbApiPortThread (HANDLE hSbApiPortListen
)
360 HANDLE hConnectedPort
= (HANDLE
) 0;
361 PORT_MESSAGE Request
;
362 PVOID Context
= NULL
;
363 NTSTATUS Status
= STATUS_SUCCESS
;
364 PPORT_MESSAGE Reply
= NULL
;
366 DPRINT("CSR: %s called\n", __FUNCTION__
);
368 RtlZeroMemory(&Request
, sizeof(PORT_MESSAGE
));
369 Status
= NtListenPort (hSbApiPortListen
, & Request
);
371 if (!NT_SUCCESS(Status
))
373 DPRINT1("CSR: %s: NtListenPort(SB) failed (Status=0x%08lx)\n",
374 __FUNCTION__
, Status
);
377 Status
= NtAcceptConnectPort(&hConnectedPort
,
383 if(!NT_SUCCESS(Status
))
385 DPRINT1("CSR: %s: NtAcceptConnectPort() failed (Status=0x%08lx)\n",
386 __FUNCTION__
, Status
);
389 Status
= NtCompleteConnectPort (hConnectedPort
);
390 if(!NT_SUCCESS(Status
))
392 DPRINT1("CSR: %s: NtCompleteConnectPort() failed (Status=0x%08lx)\n",
393 __FUNCTION__
, Status
);
397 * Tell the init thread the SM gave the
398 * green light for boostrapping.
400 Status
= NtSetEvent (hBootstrapOk
, NULL
);
401 if(!NT_SUCCESS(Status
))
403 DPRINT1("CSR: %s: NtSetEvent failed (Status=0x%08lx)\n",
404 __FUNCTION__
, Status
);
406 /* Wait for messages from the SM */
410 Status
= NtReplyWaitReceivePort(hConnectedPort
,
414 if(!NT_SUCCESS(Status
))
416 DPRINT1("CSR: %s: NtReplyWaitReceivePort failed (Status=0x%08lx)\n",
417 __FUNCTION__
, Status
);
421 switch (Request
.u2
.s2
.Type
) //fix .h PORT_MESSAGE_TYPE(Request))
425 DPRINT1("CSR: %s received message (type=%d)\n",
426 __FUNCTION__
, Request
.u2
.s2
.Type
);
434 DPRINT("CSR: %s: terminating!\n", __FUNCTION__
);
435 if(hConnectedPort
) NtClose (hConnectedPort
);
436 NtClose (hSbApiPortListen
);
437 NtTerminateThread (NtCurrentThread(), Status
);