2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Client/Server Runtime SubSystem
4 * FILE: subsystems/win32/csrsrv/server.c
5 * PURPOSE: CSR Server DLL Server Functions
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
9 /* INCLUDES *******************************************************************/
13 #include <ndk/mmfuncs.h>
18 /* DATA ***********************************************************************/
20 PCSR_SERVER_DLL CsrLoadedServerDll
[CSR_SERVER_DLL_MAX
];
21 PVOID CsrSrvSharedSectionHeap
= NULL
;
22 PVOID CsrSrvSharedSectionBase
= NULL
;
23 PVOID
*CsrSrvSharedStaticServerData
= NULL
;
24 ULONG CsrSrvSharedSectionSize
= 0;
25 HANDLE CsrSrvSharedSection
= NULL
;
27 PCSR_API_ROUTINE CsrServerApiDispatchTable
[CsrpMaxApiNumber
] =
32 CsrSrvIdentifyAlertableThread
,
33 CsrSrvSetPriorityClass
36 BOOLEAN CsrServerApiServerValidTable
[CsrpMaxApiNumber
] =
46 * On Windows Server 2003, CSR Servers contain
47 * the API Names Table only in Debug Builds.
50 PCHAR CsrServerApiNameTable
[CsrpMaxApiNumber
] =
55 "IdentifyAlertableThread",
60 /* PRIVATE FUNCTIONS **********************************************************/
63 * @name CsrServerDllInitialization
66 * The CsrServerDllInitialization is the initialization routine
67 * for this Server DLL.
69 * @param LoadedServerDll
70 * Pointer to the CSR Server DLL structure representing this Server DLL.
72 * @return STATUS_SUCCESS.
77 CSR_SERVER_DLL_INIT(CsrServerDllInitialization
)
79 /* Setup the DLL Object */
80 LoadedServerDll
->ApiBase
= CSRSRV_FIRST_API_NUMBER
;
81 LoadedServerDll
->HighestApiSupported
= CsrpMaxApiNumber
;
82 LoadedServerDll
->DispatchTable
= CsrServerApiDispatchTable
;
83 LoadedServerDll
->ValidTable
= CsrServerApiServerValidTable
;
85 LoadedServerDll
->NameTable
= CsrServerApiNameTable
;
87 LoadedServerDll
->SizeOfProcessData
= 0;
88 LoadedServerDll
->ConnectCallback
= NULL
;
89 LoadedServerDll
->DisconnectCallback
= NULL
;
92 return STATUS_SUCCESS
;
96 * @name CsrLoadServerDll
99 * The CsrLoadServerDll routine loads a CSR Server DLL and calls its entrypoint.
102 * Pointer to the CSR Server DLL to load and call.
105 * Pointer to the name of the server's initialization function.
106 * If this parameter is NULL, the default ServerDllInitialize
109 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
116 CsrLoadServerDll(IN PCHAR DllString
,
117 IN PCHAR EntryPoint OPTIONAL
,
122 UNICODE_STRING TempString
, ErrorString
;
123 ULONG_PTR Parameters
[2];
124 HANDLE hServerDll
= NULL
;
126 PCSR_SERVER_DLL ServerDll
;
127 STRING EntryPointString
;
128 PCSR_SERVER_DLL_INIT_CALLBACK ServerDllInitProcedure
;
131 /* Check if it's beyond the maximum we support */
132 if (ServerId
>= CSR_SERVER_DLL_MAX
) return STATUS_TOO_MANY_NAMES
;
134 /* Check if it's already been loaded */
135 if (CsrLoadedServerDll
[ServerId
]) return STATUS_INVALID_PARAMETER
;
137 /* Convert the name to Unicode */
138 ASSERT(DllString
!= NULL
);
139 RtlInitAnsiString(&DllName
, DllString
);
140 Status
= RtlAnsiStringToUnicodeString(&TempString
, &DllName
, TRUE
);
141 if (!NT_SUCCESS(Status
)) return Status
;
143 /* If we are loading ourselves, don't actually load us */
144 if (ServerId
!= CSRSRV_SERVERDLL_INDEX
)
147 Status
= LdrLoadDll(NULL
, 0, &TempString
, &hServerDll
);
148 if (!NT_SUCCESS(Status
))
150 /* Setup error parameters */
151 Parameters
[0] = (ULONG_PTR
)&TempString
;
152 Parameters
[1] = (ULONG_PTR
)&ErrorString
;
153 RtlInitUnicodeString(&ErrorString
, L
"Default Load Path");
155 /* Send a hard error */
156 NtRaiseHardError(Status
,
164 /* Get rid of the string */
165 RtlFreeUnicodeString(&TempString
);
166 if (!NT_SUCCESS(Status
)) return Status
;
169 /* Allocate a CSR DLL Object */
170 Size
= sizeof(CSR_SERVER_DLL
) + DllName
.MaximumLength
;
171 ServerDll
= RtlAllocateHeap(CsrHeap
, HEAP_ZERO_MEMORY
, Size
);
174 if (hServerDll
) LdrUnloadDll(hServerDll
);
175 return STATUS_NO_MEMORY
;
178 /* Set up the Object */
179 ServerDll
->Length
= Size
;
180 ServerDll
->SizeOfProcessData
= 0;
181 ServerDll
->SharedSection
= CsrSrvSharedSectionHeap
; // Send to the server dll our shared heap pointer.
182 ServerDll
->Name
.Length
= DllName
.Length
;
183 ServerDll
->Name
.MaximumLength
= DllName
.MaximumLength
;
184 ServerDll
->Name
.Buffer
= (PCHAR
)(ServerDll
+ 1);
187 strncpy(ServerDll
->Name
.Buffer
, DllName
.Buffer
, DllName
.Length
);
189 ServerDll
->ServerId
= ServerId
;
190 ServerDll
->ServerHandle
= hServerDll
;
192 /* Now get the entrypoint */
195 /* Initialize a string for the entrypoint, or use the default */
196 RtlInitAnsiString(&EntryPointString
,
197 EntryPoint
? EntryPoint
: "ServerDllInitialization");
199 /* Get a pointer to it */
200 Status
= LdrGetProcedureAddress(hServerDll
,
203 (PVOID
)&ServerDllInitProcedure
);
207 /* No handle, so we are loading ourselves */
208 ServerDllInitProcedure
= CsrServerDllInitialization
;
209 Status
= STATUS_SUCCESS
;
212 /* Check if we got the pointer, and call it */
213 if (NT_SUCCESS(Status
))
215 /* Get the result from the Server DLL */
216 Status
= ServerDllInitProcedure(ServerDll
);
217 if (NT_SUCCESS(Status
))
220 * Add this Server's Per-Process Data Size to the total that each
223 CsrTotalPerProcessDataLength
+= ServerDll
->SizeOfProcessData
;
225 /* Save the pointer in our list */
226 CsrLoadedServerDll
[ServerDll
->ServerId
] = ServerDll
;
228 /* Does it use our generic heap? */
229 if (ServerDll
->SharedSection
!= CsrSrvSharedSectionHeap
)
231 /* No, save the pointer to its shared section in our list */
232 CsrSrvSharedStaticServerData
[ServerDll
->ServerId
] = ServerDll
->SharedSection
;
237 if (!NT_SUCCESS(Status
))
239 /* Server Init failed, unload it */
240 if (hServerDll
) LdrUnloadDll(hServerDll
);
242 /* Delete the Object */
243 RtlFreeHeap(CsrHeap
, 0, ServerDll
);
246 /* Return to caller */
251 * @name CsrSrvClientConnect
253 * The CsrSrvClientConnect CSR API handles a new connection to a server DLL.
256 * Pointer to the CSR API Message for this request.
259 * Optional reply to this request.
261 * @return STATUS_SUCCESS in case of success, STATUS_INVALID_PARAMETER
262 * or STATUS_TOO_MANY_NAMES in case of failure.
267 CSR_API(CsrSrvClientConnect
)
270 PCSR_CLIENT_CONNECT ClientConnect
= &ApiMessage
->Data
.CsrClientConnect
;
271 PCSR_SERVER_DLL ServerDll
;
272 PCSR_PROCESS CurrentProcess
= CsrGetClientThread()->Process
;
274 /* Set default reply */
275 *ReplyCode
= CsrReplyImmediately
;
277 /* Validate the ServerID */
278 if (ClientConnect
->ServerId
>= CSR_SERVER_DLL_MAX
)
280 return STATUS_TOO_MANY_NAMES
;
282 else if (!CsrLoadedServerDll
[ClientConnect
->ServerId
])
284 return STATUS_INVALID_PARAMETER
;
287 /* Validate the Message Buffer */
288 if (!(CsrValidateMessageBuffer(ApiMessage
,
289 &ClientConnect
->ConnectionInfo
,
290 ClientConnect
->ConnectionInfoSize
,
293 /* Fail due to buffer overflow or other invalid buffer */
294 return STATUS_INVALID_PARAMETER
;
297 /* Load the Server DLL */
298 ServerDll
= CsrLoadedServerDll
[ClientConnect
->ServerId
];
300 /* Check if it has a Connect Callback */
301 if (ServerDll
->ConnectCallback
)
303 /* Call the callback */
304 Status
= ServerDll
->ConnectCallback(CurrentProcess
,
305 ClientConnect
->ConnectionInfo
,
306 &ClientConnect
->ConnectionInfoSize
);
311 Status
= STATUS_SUCCESS
;
319 * @name CsrSrvCreateSharedSection
321 * The CsrSrvCreateSharedSection creates the Shared Section that all
322 * CSR Server DLLs and Clients can use to share data.
324 * @param ParameterValue
325 * Specially formatted string from our registry command-line which
326 * specifies various arguments for the shared section.
328 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
335 CsrSrvCreateSharedSection(IN PCHAR ParameterValue
)
337 PCHAR SizeValue
= ParameterValue
;
340 LARGE_INTEGER SectionSize
;
342 PPEB Peb
= NtCurrentPeb();
344 /* If there's no parameter, fail */
345 if (!ParameterValue
) return STATUS_INVALID_PARAMETER
;
347 /* Find the first comma, and null terminate */
350 if (*SizeValue
== ',')
352 *SizeValue
++ = ANSI_NULL
;
361 /* Make sure it's valid */
362 if (!*SizeValue
) return STATUS_INVALID_PARAMETER
;
364 /* Convert it to an integer */
365 Status
= RtlCharToInteger(SizeValue
, 0, &Size
);
366 if (!NT_SUCCESS(Status
)) return Status
;
368 /* Multiply by 1024 entries and round to page size */
369 CsrSrvSharedSectionSize
= ROUND_UP(Size
* 1024, CsrNtSysInfo
.PageSize
);
371 /* Create the Secion */
372 SectionSize
.LowPart
= CsrSrvSharedSectionSize
;
373 SectionSize
.HighPart
= 0;
374 Status
= NtCreateSection(&CsrSrvSharedSection
,
378 PAGE_EXECUTE_READWRITE
,
379 SEC_BASED
| SEC_RESERVE
,
381 if (!NT_SUCCESS(Status
)) return Status
;
383 /* Map the section */
384 Status
= NtMapViewOfSection(CsrSrvSharedSection
,
386 &CsrSrvSharedSectionBase
,
393 PAGE_EXECUTE_READWRITE
);
394 if (!NT_SUCCESS(Status
))
397 NtClose(CsrSrvSharedSection
);
401 /* FIXME: Write the value to registry */
403 /* The Heap is the same place as the Base */
404 CsrSrvSharedSectionHeap
= CsrSrvSharedSectionBase
;
406 /* Create the heap */
407 if (!(RtlCreateHeap(HEAP_ZERO_MEMORY
| HEAP_CLASS_7
,
408 CsrSrvSharedSectionHeap
,
409 CsrSrvSharedSectionSize
,
414 /* Failure, unmap section and return */
415 NtUnmapViewOfSection(NtCurrentProcess(), CsrSrvSharedSectionBase
);
416 NtClose(CsrSrvSharedSection
);
417 return STATUS_NO_MEMORY
;
420 /* Now allocate space from the heap for the Shared Data */
421 CsrSrvSharedStaticServerData
= RtlAllocateHeap(CsrSrvSharedSectionHeap
,
423 CSR_SERVER_DLL_MAX
* sizeof(PVOID
));
424 if (!CsrSrvSharedStaticServerData
) return STATUS_NO_MEMORY
;
426 /* Write the values to the PEB */
427 Peb
->ReadOnlySharedMemoryBase
= CsrSrvSharedSectionBase
;
428 Peb
->ReadOnlySharedMemoryHeap
= CsrSrvSharedSectionHeap
;
429 Peb
->ReadOnlyStaticServerData
= CsrSrvSharedStaticServerData
;
432 return STATUS_SUCCESS
;
436 * @name CsrSrvAttachSharedSection
438 * The CsrSrvAttachSharedSection maps the CSR Shared Section into a new
439 * CSR Process' address space, and returns the pointers to the section
440 * through the Connection Info structure.
443 * Pointer to the CSR Process that is attempting a connection.
446 * Pointer to the CSR Connection Info structure for the incoming
449 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
456 CsrSrvAttachSharedSection(IN PCSR_PROCESS CsrProcess OPTIONAL
,
457 OUT PCSR_API_CONNECTINFO ConnectInfo
)
462 /* Check if we have a process */
465 /* Map the section into this process */
466 Status
= NtMapViewOfSection(CsrSrvSharedSection
,
467 CsrProcess
->ProcessHandle
,
468 &CsrSrvSharedSectionBase
,
476 if (!NT_SUCCESS(Status
)) return Status
;
479 /* Write the values in the Connection Info structure */
480 ConnectInfo
->SharedSectionBase
= CsrSrvSharedSectionBase
;
481 ConnectInfo
->SharedSectionHeap
= CsrSrvSharedSectionHeap
;
482 ConnectInfo
->SharedStaticServerData
= CsrSrvSharedStaticServerData
;
485 return STATUS_SUCCESS
;
489 * @name CsrSrvIdentifyAlertableThread
492 * The CsrSrvIdentifyAlertableThread CSR API marks a CSR Thread as alertable.
495 * Pointer to the CSR API Message for this request.
498 * Pointer to an optional reply to this request.
500 * @return STATUS_SUCCESS.
505 CSR_API(CsrSrvIdentifyAlertableThread
)
507 PCSR_THREAD CsrThread
= CsrGetClientThread();
509 /* Set the alertable flag */
510 CsrThread
->Flags
|= CsrThreadAlertable
;
513 return STATUS_SUCCESS
;
517 * @name CsrSrvSetPriorityClass
520 * The CsrSrvSetPriorityClass CSR API is deprecated.
523 * Pointer to the CSR API Message for this request.
526 * Pointer to an optional reply to this request.
528 * @return STATUS_SUCCESS.
533 CSR_API(CsrSrvSetPriorityClass
)
536 return STATUS_SUCCESS
;
540 * @name CsrSrvUnusedFunction
543 * The CsrSrvUnusedFunction CSR API is a stub for deprecated APIs.
545 * The CsrSrvSetPriorityClass CSR API is deprecated.
548 * Pointer to the CSR API Message for this request.
551 * Pointer to an optional reply to this request.
553 * @return STATUS_INVALID_PARAMETER.
555 * @remarks CsrSrvSetPriorityClass does not use this stub because
556 * it must return success.
559 CSR_API(CsrSrvUnusedFunction
)
562 return STATUS_INVALID_PARAMETER
;
565 /* PUBLIC FUNCTIONS ***********************************************************/
568 * @name CsrSetCallingSpooler
571 * the CsrSetCallingSpooler routine is deprecated.
578 * @remarks This routine was used in archaic versions of NT for Printer Drivers.
583 CsrSetCallingSpooler(ULONG Reserved
)
590 * @name CsrUnhandledExceptionFilter
593 * The CsrUnhandledExceptionFilter routine handles all exceptions
594 * within SEH-protected blocks.
596 * @param ExceptionPointers
597 * System-defined Argument.
599 * @return EXCEPTION_EXECUTE_HANDLER.
604 EXCEPTION_DISPOSITION
606 CsrUnhandledExceptionFilter(IN PEXCEPTION_POINTERS ExceptionInfo
)
608 SYSTEM_KERNEL_DEBUGGER_INFORMATION DebuggerInfo
;
609 EXCEPTION_DISPOSITION Result
= EXCEPTION_EXECUTE_HANDLER
;
612 UNICODE_STRING ErrorSource
;
613 ULONG_PTR ErrorParameters
[4];
616 DPRINT1("CsrUnhandledExceptionFilter called\n");
618 /* Check if a debugger is installed */
619 Status
= NtQuerySystemInformation(SystemKernelDebuggerInformation
,
621 sizeof(DebuggerInfo
),
624 /* Check if this is Session 0, and the Debugger is Enabled */
625 if ((NtCurrentPeb()->SessionId
!= 0) && (NT_SUCCESS(Status
)) &&
626 (DebuggerInfo
.KernelDebuggerEnabled
))
628 /* Call the Unhandled Exception Filter */
629 Result
= RtlUnhandledExceptionFilter(ExceptionInfo
);
630 if (Result
!= EXCEPTION_CONTINUE_EXECUTION
)
632 /* We're going to raise an error. Get Shutdown Privilege first */
633 Status
= RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE
,
638 /* Use the Process token if that failed */
639 if (Status
== STATUS_NO_TOKEN
)
641 Status
= RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE
,
647 /* Initialize our Name String */
648 RtlInitUnicodeString(&ErrorSource
, L
"Windows SubSystem");
650 /* Set the parameters */
651 ErrorParameters
[0] = (ULONG_PTR
)&ErrorSource
;
652 ErrorParameters
[1] = ExceptionInfo
->ExceptionRecord
->ExceptionCode
;
653 ErrorParameters
[2] = (ULONG_PTR
)ExceptionInfo
->ExceptionRecord
->ExceptionAddress
;
654 ErrorParameters
[3] = (ULONG_PTR
)ExceptionInfo
->ContextRecord
;
657 Status
= NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
,
661 OptionShutdownSystem
,
665 /* Just terminate us */
666 NtTerminateProcess(NtCurrentProcess(),
667 ExceptionInfo
->ExceptionRecord
->ExceptionCode
);