3 * reactos/subsys/csrss/api/wapi.c
5 * CSRSS port message processing
7 * ReactOS Operating System
11 /* INCLUDES ******************************************************************/
20 /* GLOBALS *******************************************************************/
22 HANDLE CsrssApiHeap
= (HANDLE
) 0;
24 static unsigned ApiDefinitionsCount
= 0;
25 static PCSRSS_API_DEFINITION ApiDefinitions
= NULL
;
27 /* FUNCTIONS *****************************************************************/
30 CsrApiRegisterDefinitions(PCSRSS_API_DEFINITION NewDefinitions
)
33 PCSRSS_API_DEFINITION Scan
;
34 PCSRSS_API_DEFINITION New
;
36 DPRINT("CSR: %s called", __FUNCTION__
);
39 for (Scan
= NewDefinitions
; 0 != Scan
->Handler
; Scan
++)
44 New
= RtlAllocateHeap(CsrssApiHeap
, 0,
45 (ApiDefinitionsCount
+ NewCount
)
46 * sizeof(CSRSS_API_DEFINITION
));
49 DPRINT1("Unable to allocate memory\n");
50 return STATUS_NO_MEMORY
;
52 if (0 != ApiDefinitionsCount
)
54 RtlCopyMemory(New
, ApiDefinitions
,
55 ApiDefinitionsCount
* sizeof(CSRSS_API_DEFINITION
));
56 RtlFreeHeap(CsrssApiHeap
, 0, ApiDefinitions
);
58 RtlCopyMemory(New
+ ApiDefinitionsCount
, NewDefinitions
,
59 NewCount
* sizeof(CSRSS_API_DEFINITION
));
61 ApiDefinitionsCount
+= NewCount
;
63 return STATUS_SUCCESS
;
68 CsrApiCallHandler(PCSRSS_PROCESS_DATA ProcessData
,
69 PCSR_API_MESSAGE Request
)
75 DPRINT("CSR: Calling handler for type: %x.\n", Request
->Type
);
76 Type
= Request
->Type
& 0xFFFF; /* FIXME: USE MACRO */
77 DPRINT("CSR: API Number: %x ServerID: %x\n",Type
, Request
->Type
>> 16);
79 /* FIXME: Extract DefIndex instead of looping */
80 for (DefIndex
= 0; ! Found
&& DefIndex
< ApiDefinitionsCount
; DefIndex
++)
82 if (ApiDefinitions
[DefIndex
].Type
== Type
)
84 if (Request
->Header
.u1
.s1
.DataLength
< ApiDefinitions
[DefIndex
].MinRequestSize
)
86 DPRINT1("Request type %d min request size %d actual %d\n",
87 Type
, ApiDefinitions
[DefIndex
].MinRequestSize
,
88 Request
->Header
.u1
.s1
.DataLength
);
89 Request
->Status
= STATUS_INVALID_PARAMETER
;
93 (ApiDefinitions
[DefIndex
].Handler
)(ProcessData
, Request
);
100 DPRINT1("CSR: Unknown request type 0x%x\n", Request
->Type
);
101 Request
->Header
.u1
.s1
.TotalLength
= sizeof(CSR_API_MESSAGE
);
102 Request
->Header
.u1
.s1
.DataLength
= sizeof(CSR_API_MESSAGE
) - sizeof(PORT_MESSAGE
);
103 Request
->Status
= STATUS_INVALID_SYSTEM_SERVICE
;
107 BOOL FASTCALL
CallHardError(void);
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 */
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
,
145 if (!NT_SUCCESS(Status
))
147 DPRINT1("CSR: NtAcceptConnectPort() failed\n");
151 ProcessData
= CsrCreateProcessData(Request
->ClientId
.UniqueProcess
);
152 if (ProcessData
== NULL
)
154 DPRINT1("Unable to allocate or find data for process 0x%x\n",
155 Request
->ClientId
.UniqueProcess
);
156 Status
= STATUS_UNSUCCESSFUL
;
160 ProcessData
->CsrSectionViewBase
= LpcRead
.ViewBase
;
161 ProcessData
->CsrSectionViewSize
= LpcRead
.ViewSize
;
163 Status
= NtCompleteConnectPort(ServerPort
);
164 if (!NT_SUCCESS(Status
))
166 DPRINT1("CSR: NtCompleteConnectPort() failed\n");
170 #if !defined(NTLPC) /* ReactOS LPC */
171 HANDLE ServerThread
= (HANDLE
) 0;
172 Status
= RtlCreateUserThread(NtCurrentProcess(),
178 (PTHREAD_START_ROUTINE
)ClientConnectionThread
,
182 if (!NT_SUCCESS(Status
))
184 DPRINT1("CSR: Unable to create server thread\n");
188 NtClose(ServerThread
);
191 Status
= STATUS_SUCCESS
;
192 DPRINT("CSR: %s done\n", __FUNCTION__
);
198 ClientConnectionThread(HANDLE ServerPort
)
201 BYTE RawRequest
[LPC_MAX_DATA_LENGTH
];
202 PCSR_API_MESSAGE Request
= (PCSR_API_MESSAGE
)RawRequest
;
203 PCSR_API_MESSAGE Reply
;
204 PCSRSS_PROCESS_DATA ProcessData
;
206 DPRINT("CSR: %s called", __FUNCTION__
);
208 /* Reply must be NULL at the first call to NtReplyWaitReceivePort */
211 /* Loop and reply/wait for a new message */
214 /* Send the reply and wait for a new request */
215 Status
= NtReplyWaitReceivePort(ServerPort
,
219 if (!NT_SUCCESS(Status
))
221 DPRINT1("NtReplyWaitReceivePort failed: %lx\n", Status
);
225 /* If the connection was closed, handle that */
226 if (Request
->Header
.u2
.s2
.Type
== LPC_PORT_CLOSED
)
228 DPRINT("Port died, oh well\n");
229 CsrFreeProcessData( Request
->Header
.ClientId
.UniqueProcess
);
236 if (Request
->Header
.u2
.s2
.Type
== LPC_CONNECTION_REQUEST
)
238 CsrpHandleConnectionRequest((PPORT_MESSAGE
)Request
, ServerPort
);
243 if (Request
->Header
.u2
.s2
.Type
== LPC_CLIENT_DIED
)
245 DPRINT("Client died, oh well\n");
250 DPRINT("CSR: Got CSR API: %x [Message Origin: %x]\n",
252 Request
->Header
.ClientId
.UniqueThread
);
254 /* Get the Process Data */
255 ProcessData
= CsrGetProcessData(Request
->Header
.ClientId
.UniqueProcess
);
256 if (ProcessData
== NULL
)
258 DPRINT1("Message %d: Unable to find data for process 0x%x\n",
259 Request
->Header
.u2
.s2
.Type
,
260 Request
->Header
.ClientId
.UniqueProcess
);
263 if (ProcessData
->Terminated
)
265 DPRINT1("Message %d: process %d already terminated\n",
266 Request
->Type
, (ULONG
)Request
->Header
.ClientId
.UniqueProcess
);
270 /* Check if we got a hard error */
271 if (Request
->Header
.u2
.s2
.Type
== LPC_ERROR_EVENT
)
273 /* Call the Handler */
274 CsrHandleHardError(ProcessData
, (PHARDERROR_MSG
)Request
);
278 /* Call the Handler */
279 CsrApiCallHandler(ProcessData
, Request
);
282 /* Send back the reply */
286 /* Close the port and exit the thread */
288 RtlExitUserThread(STATUS_SUCCESS
);
291 /**********************************************************************
293 * ServerApiPortThread/1
296 * Handle connection requests from clients to the port
297 * "\Windows\ApiPort".
300 ServerApiPortThread (HANDLE hApiListenPort
)
302 NTSTATUS Status
= STATUS_SUCCESS
;
303 BYTE RawRequest
[sizeof(PORT_MESSAGE
) + sizeof(CSR_CONNECTION_INFO
)];
304 PPORT_MESSAGE Request
= (PPORT_MESSAGE
)RawRequest
;
306 DPRINT("CSR: %s called", __FUNCTION__
);
310 REMOTE_PORT_VIEW LpcRead
;
311 LpcRead
.Length
= sizeof(LpcRead
);
313 Status
= NtListenPort (hApiListenPort
, Request
);
314 if (!NT_SUCCESS(Status
))
316 DPRINT1("CSR: NtListenPort() failed, status=%x\n", Status
);
320 Status
= CsrpHandleConnectionRequest(Request
, hApiListenPort
);
321 if(!NT_SUCCESS(Status
))
323 DPRINT1("CSR: %s: SmpHandleConnectionRequest failed (Status=0x%08lx)\n",
324 __FUNCTION__
, Status
);
329 NtClose(hApiListenPort
);
330 NtTerminateThread(NtCurrentThread(), Status
);
334 /**********************************************************************
336 * ServerSbApiPortThread/1
339 * Handle connection requests from SM to the port
340 * "\Windows\SbApiPort". We will accept only one
341 * connection request (from the SM).
344 ServerSbApiPortThread (HANDLE hSbApiPortListen
)
346 HANDLE hConnectedPort
= (HANDLE
) 0;
347 PORT_MESSAGE Request
;
348 PVOID Context
= NULL
;
349 NTSTATUS Status
= STATUS_SUCCESS
;
350 PPORT_MESSAGE Reply
= NULL
;
352 DPRINT("CSR: %s called\n", __FUNCTION__
);
354 RtlZeroMemory(&Request
, sizeof(PORT_MESSAGE
));
355 Status
= NtListenPort (hSbApiPortListen
, & Request
);
356 if (!NT_SUCCESS(Status
))
358 DPRINT1("CSR: %s: NtListenPort(SB) failed (Status=0x%08lx)\n",
359 __FUNCTION__
, Status
);
362 Status
= NtAcceptConnectPort (& hConnectedPort
,
373 if(!NT_SUCCESS(Status
))
375 DPRINT1("CSR: %s: NtAcceptConnectPort() failed (Status=0x%08lx)\n",
376 __FUNCTION__
, Status
);
379 Status
= NtCompleteConnectPort (hConnectedPort
);
380 if(!NT_SUCCESS(Status
))
382 DPRINT1("CSR: %s: NtCompleteConnectPort() failed (Status=0x%08lx)\n",
383 __FUNCTION__
, Status
);
388 * Tell the init thread the SM gave the
389 * green light for boostrapping.
391 Status
= NtSetEvent (hBootstrapOk
, NULL
);
392 if(!NT_SUCCESS(Status
))
394 DPRINT1("CSR: %s: NtSetEvent failed (Status=0x%08lx)\n",
395 __FUNCTION__
, Status
);
397 /* Wait for messages from the SM */
401 Status
= NtReplyWaitReceivePort(hConnectedPort
,
405 if(!NT_SUCCESS(Status
))
407 DPRINT1("CSR: %s: NtReplyWaitReceivePort failed (Status=0x%08lx)\n",
408 __FUNCTION__
, Status
);
411 switch (Request
.u2
.s2
.Type
)//fix .h PORT_MESSAGE_TYPE(Request))
415 DPRINT1("CSR: %s received message (type=%d)\n",
416 __FUNCTION__
, Request
.u2
.s2
.Type
);
423 DPRINT("CSR: %s: terminating!\n", __FUNCTION__
);
424 if(hConnectedPort
) NtClose (hConnectedPort
);
425 NtClose (hSbApiPortListen
);
426 NtTerminateThread (NtCurrentThread(), Status
);