xref: /reactos/sdk/include/reactos/subsys/csr/csrsrv.h (revision d2aeaba5)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Client/Server Runtime SubSystem
4  * FILE:            include/reactos/subsys/csr/csrsrv.h
5  * PURPOSE:         Public definitions for CSR Servers
6  * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
7  *                  Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8  */
9 
10 #ifndef _CSRSRV_H
11 #define _CSRSRV_H
12 
13 /*
14  * The CSR_DBG macro is defined for building CSR Servers
15  * with extended debugging information.
16  */
17 #if DBG
18 #define CSR_DBG
19 #endif
20 
21 #include "csrmsg.h"
22 
23 
24 /* TYPES **********************************************************************/
25 
26 // Used in csr/connect.c
27 #define CSR_CSRSS_SECTION_SIZE  65536
28 
29 typedef struct _CSR_NT_SESSION
30 {
31     ULONG ReferenceCount;
32     LIST_ENTRY SessionLink;
33     ULONG SessionId;
34 } CSR_NT_SESSION, *PCSR_NT_SESSION;
35 
36 typedef struct _CSR_PROCESS
37 {
38     CLIENT_ID ClientId;
39     LIST_ENTRY ListLink;
40     LIST_ENTRY ThreadList;
41     PCSR_NT_SESSION NtSession;
42     ULONG ExpectedVersion;
43     HANDLE ClientPort;
44     ULONG_PTR ClientViewBase;
45     ULONG_PTR ClientViewBounds;
46     HANDLE ProcessHandle;
47     ULONG SequenceNumber;
48     ULONG Flags;
49     ULONG DebugFlags;
50     CLIENT_ID DebugCid;
51     ULONG ReferenceCount;
52     ULONG ProcessGroupId;
53     ULONG ProcessGroupSequence;
54     ULONG fVDM;
55     ULONG ThreadCount;
56     ULONG PriorityClass;
57     ULONG Reserved;
58     ULONG ShutdownLevel;
59     ULONG ShutdownFlags;
60     PVOID ServerData[ANYSIZE_ARRAY];    // One structure per CSR server.
61 } CSR_PROCESS, *PCSR_PROCESS;
62 
63 typedef struct _CSR_THREAD
64 {
65     LARGE_INTEGER CreateTime;
66     LIST_ENTRY Link;
67     LIST_ENTRY HashLinks;
68     CLIENT_ID ClientId;
69     PCSR_PROCESS Process;
70     struct _CSR_WAIT_BLOCK *WaitBlock;
71     HANDLE ThreadHandle;
72     ULONG Flags;
73     ULONG ReferenceCount;
74     ULONG ImpersonationCount;
75 } CSR_THREAD, *PCSR_THREAD;
76 
77 #define CsrGetClientThread() \
78     ((PCSR_THREAD)(NtCurrentTeb()->CsrClientThread))
79 
80 
81 /* ENUMERATIONS ***************************************************************/
82 
83 typedef enum _CSR_PROCESS_FLAGS
84 {
85     CsrProcessTerminating          = 0x1,
86     CsrProcessSkipShutdown         = 0x2,
87     CsrProcessNormalPriority       = 0x10,
88     CsrProcessIdlePriority         = 0x20,
89     CsrProcessHighPriority         = 0x40,
90     CsrProcessRealtimePriority     = 0x80,
91     CsrProcessCreateNewGroup       = 0x100,
92     CsrProcessTerminated           = 0x200,
93     CsrProcessLastThreadTerminated = 0x400,
94     CsrProcessIsConsoleApp         = 0x800
95 } CSR_PROCESS_FLAGS, *PCSR_PROCESS_FLAGS;
96 
97 #define CsrProcessPriorityFlags (CsrProcessNormalPriority | \
98                                  CsrProcessIdlePriority   | \
99                                  CsrProcessHighPriority   | \
100                                  CsrProcessRealtimePriority)
101 
102 typedef enum _CSR_THREAD_FLAGS
103 {
104     CsrThreadAlertable      = 0x1,
105     CsrThreadInTermination  = 0x2,
106     CsrThreadTerminated     = 0x4,
107     CsrThreadIsServerThread = 0x10
108 } CSR_THREAD_FLAGS, *PCSR_THREAD_FLAGS;
109 
110 typedef enum _SHUTDOWN_RESULT
111 {
112     CsrShutdownCsrProcess = 1,
113     CsrShutdownNonCsrProcess,
114     CsrShutdownCancelled
115 } SHUTDOWN_RESULT, *PSHUTDOWN_RESULT;
116 
117 typedef enum _CSR_SHUTDOWN_FLAGS
118 {
119     CsrShutdownSystem = 4,
120     CsrShutdownOther  = 8
121 } CSR_SHUTDOWN_FLAGS, *PCSR_SHUTDOWN_FLAGS;
122 
123 typedef enum _CSR_DEBUG_FLAGS
124 {
125     CsrDebugOnlyThisProcess = 1,
126     CsrDebugProcessChildren = 2
127 } CSR_PROCESS_DEBUG_FLAGS, *PCSR_PROCESS_DEBUG_FLAGS;
128 
129 typedef enum _CSR_REPLY_CODE
130 {
131     CsrReplyImmediately = 0,
132     CsrReplyPending     = 1,
133     CsrReplyDeadClient  = 2,
134     CsrReplyAlreadySent = 3
135 } CSR_REPLY_CODE, *PCSR_REPLY_CODE;
136 
137 
138 /* FUNCTION TYPES AND STRUCTURES **********************************************/
139 
140 /*
141  * Wait block
142  */
143 typedef
144 BOOLEAN
145 (NTAPI *CSR_WAIT_FUNCTION)(
146     IN PLIST_ENTRY WaitList,
147     IN PCSR_THREAD WaitThread,
148     IN PCSR_API_MESSAGE WaitApiMessage,
149     IN PVOID WaitContext,
150     IN PVOID WaitArgument1,
151     IN PVOID WaitArgument2,
152     IN ULONG WaitFlags
153 );
154 
155 typedef struct _CSR_WAIT_BLOCK
156 {
157     ULONG Size;                     // Size of the wait block (variable-sized)
158     LIST_ENTRY WaitList;
159     PVOID WaitContext;
160     PCSR_THREAD WaitThread;
161     CSR_WAIT_FUNCTION WaitFunction;
162     CSR_API_MESSAGE WaitApiMessage; // Variable-sized CSR API message
163 } CSR_WAIT_BLOCK, *PCSR_WAIT_BLOCK;
164 
165 
166 /*
167  * Server DLL structure
168  */
169 typedef
170 NTSTATUS
171 (NTAPI *PCSR_API_ROUTINE)(
172     IN OUT PCSR_API_MESSAGE ApiMessage,
173     IN OUT PCSR_REPLY_CODE  ReplyCode OPTIONAL
174 );
175 
176 #define CSR_API(n)                                          \
177     NTSTATUS NTAPI n(IN OUT PCSR_API_MESSAGE ApiMessage,    \
178                      IN OUT PCSR_REPLY_CODE  ReplyCode OPTIONAL)
179 
180 typedef
181 NTSTATUS
182 (NTAPI *PCSR_CONNECT_CALLBACK)(
183     IN PCSR_PROCESS CsrProcess,
184     IN OUT PVOID ConnectionInfo,
185     IN OUT PULONG ConnectionInfoLength
186 );
187 
188 typedef
189 VOID
190 (NTAPI *PCSR_DISCONNECT_CALLBACK)(IN PCSR_PROCESS CsrProcess);
191 
192 typedef
193 NTSTATUS
194 (NTAPI *PCSR_NEWPROCESS_CALLBACK)(
195     IN PCSR_PROCESS Parent,
196     IN PCSR_PROCESS CsrProcess
197 );
198 
199 typedef
200 VOID
201 (NTAPI *PCSR_HARDERROR_CALLBACK)(
202     IN PCSR_THREAD CsrThread,
203     IN PHARDERROR_MSG HardErrorMessage
204 );
205 
206 typedef
207 ULONG
208 (NTAPI *PCSR_SHUTDOWNPROCESS_CALLBACK)(
209     IN PCSR_PROCESS CsrProcess,
210     IN ULONG Flags,
211     IN BOOLEAN FirstPhase
212 );
213 
214 // See http://redplait.blogspot.fr/2011/07/csrserverdll.html
215 typedef struct _CSR_SERVER_DLL
216 {
217     ULONG Length;
218     ANSI_STRING Name;
219     HANDLE ServerHandle;
220     ULONG ServerId;
221     ULONG Unknown;
222     ULONG ApiBase;
223     ULONG HighestApiSupported;
224     PCSR_API_ROUTINE *DispatchTable;
225     PBOOLEAN ValidTable; // Table of booleans which describe whether or not a server function call is valid when it is called via CsrCallServerFromServer.
226 /*
227  * On Windows Server 2003, CSR Servers contain
228  * the API Names Table only in Debug Builds.
229  */
230 #ifdef CSR_DBG
231     PCHAR *NameTable;
232 #endif
233 
234     ULONG SizeOfProcessData;
235     PCSR_CONNECT_CALLBACK ConnectCallback;
236     PCSR_DISCONNECT_CALLBACK DisconnectCallback;
237     PCSR_HARDERROR_CALLBACK HardErrorCallback;
238     PVOID SharedSection;
239     PCSR_NEWPROCESS_CALLBACK NewProcessCallback;
240     PCSR_SHUTDOWNPROCESS_CALLBACK ShutdownProcessCallback;
241     ULONG Unknown2[3];
242 } CSR_SERVER_DLL, *PCSR_SERVER_DLL;
243 #ifndef _WIN64
244     #ifdef CSR_DBG
245         C_ASSERT(FIELD_OFFSET(CSR_SERVER_DLL, SharedSection) == 0x3C);
246     #else
247         C_ASSERT(FIELD_OFFSET(CSR_SERVER_DLL, SharedSection) == 0x38);
248     #endif
249 #endif
250 
251 typedef
252 NTSTATUS
253 (NTAPI *PCSR_SERVER_DLL_INIT_CALLBACK)(IN PCSR_SERVER_DLL LoadedServerDll);
254 
255 #define CSR_SERVER_DLL_INIT(n)  \
256     NTSTATUS NTAPI n(IN PCSR_SERVER_DLL LoadedServerDll)
257 
258 
259 /* PROTOTYPES ****************************************************************/
260 
261 NTSTATUS
262 NTAPI
263 CsrServerInitialization(IN ULONG ArgumentCount,
264                         IN PCHAR Arguments[]);
265 
266 PCSR_THREAD
267 NTAPI
268 CsrAddStaticServerThread(IN HANDLE hThread,
269                          IN PCLIENT_ID ClientId,
270                          IN ULONG ThreadFlags);
271 
272 NTSTATUS
273 NTAPI
274 CsrCallServerFromServer(IN PCSR_API_MESSAGE ReceiveMsg,
275                         IN OUT PCSR_API_MESSAGE ReplyMsg);
276 
277 PCSR_THREAD
278 NTAPI
279 CsrConnectToUser(VOID);
280 
281 NTSTATUS
282 NTAPI
283 CsrCreateProcess(IN HANDLE hProcess,
284                  IN HANDLE hThread,
285                  IN PCLIENT_ID ClientId,
286                  IN PCSR_NT_SESSION NtSession,
287                  IN ULONG Flags,
288                  IN PCLIENT_ID DebugCid);
289 
290 NTSTATUS
291 NTAPI
292 CsrCreateRemoteThread(IN HANDLE hThread,
293                       IN PCLIENT_ID ClientId);
294 
295 NTSTATUS
296 NTAPI
297 CsrCreateThread(IN PCSR_PROCESS CsrProcess,
298                 IN HANDLE hThread,
299                 IN PCLIENT_ID ClientId,
300                 IN BOOLEAN HaveClient);
301 
302 BOOLEAN
303 NTAPI
304 CsrCreateWait(IN PLIST_ENTRY WaitList,
305               IN CSR_WAIT_FUNCTION WaitFunction,
306               IN PCSR_THREAD CsrWaitThread,
307               IN OUT PCSR_API_MESSAGE WaitApiMessage,
308               IN PVOID WaitContext);
309 
310 NTSTATUS
311 NTAPI
312 CsrDebugProcess(IN PCSR_PROCESS CsrProcess);
313 
314 NTSTATUS
315 NTAPI
316 CsrDebugProcessStop(IN PCSR_PROCESS CsrProcess);
317 
318 VOID
319 NTAPI
320 CsrDereferenceProcess(IN PCSR_PROCESS CsrProcess);
321 
322 VOID
323 NTAPI
324 CsrDereferenceThread(IN PCSR_THREAD CsrThread);
325 
326 VOID
327 NTAPI
328 CsrDereferenceWait(IN PLIST_ENTRY WaitList);
329 
330 NTSTATUS
331 NTAPI
332 CsrDestroyProcess(IN PCLIENT_ID Cid,
333                   IN NTSTATUS ExitStatus);
334 
335 NTSTATUS
336 NTAPI
337 CsrDestroyThread(IN PCLIENT_ID Cid);
338 
339 NTSTATUS
340 NTAPI
341 CsrExecServerThread(IN PVOID ThreadHandler,
342                     IN ULONG Flags);
343 
344 NTSTATUS
345 NTAPI
346 CsrGetProcessLuid(IN HANDLE hProcess OPTIONAL,
347                   OUT PLUID Luid);
348 
349 BOOLEAN
350 NTAPI
351 CsrImpersonateClient(IN PCSR_THREAD CsrThread);
352 
353 NTSTATUS
354 NTAPI
355 CsrLockProcessByClientId(IN HANDLE Pid,
356                          OUT PCSR_PROCESS *CsrProcess OPTIONAL);
357 
358 NTSTATUS
359 NTAPI
360 CsrLockThreadByClientId(IN HANDLE Tid,
361                         OUT PCSR_THREAD *CsrThread);
362 
363 VOID
364 NTAPI
365 CsrMoveSatisfiedWait(IN PLIST_ENTRY DestinationList,
366                      IN PLIST_ENTRY WaitList);
367 
368 BOOLEAN
369 NTAPI
370 CsrNotifyWait(IN PLIST_ENTRY WaitList,
371               IN BOOLEAN NotifyAll,
372               IN PVOID WaitArgument1,
373               IN PVOID WaitArgument2);
374 
375 VOID
376 NTAPI
377 CsrPopulateDosDevices(VOID);
378 
379 HANDLE
380 NTAPI
381 CsrQueryApiPort(VOID);
382 
383 VOID
384 NTAPI
385 CsrReferenceThread(IN PCSR_THREAD CsrThread);
386 
387 BOOLEAN
388 NTAPI
389 CsrRevertToSelf(VOID);
390 
391 VOID
392 NTAPI
393 CsrSetBackgroundPriority(IN PCSR_PROCESS CsrProcess);
394 
395 VOID
396 NTAPI
397 CsrSetCallingSpooler(ULONG Reserved);
398 
399 VOID
400 NTAPI
401 CsrSetForegroundPriority(IN PCSR_PROCESS CsrProcess);
402 
403 NTSTATUS
404 NTAPI
405 CsrShutdownProcesses(IN PLUID CallerLuid,
406                      IN ULONG Flags);
407 
408 EXCEPTION_DISPOSITION
409 NTAPI
410 CsrUnhandledExceptionFilter(IN PEXCEPTION_POINTERS ExceptionInfo);
411 
412 NTSTATUS
413 NTAPI
414 CsrUnlockProcess(IN PCSR_PROCESS CsrProcess);
415 
416 NTSTATUS
417 NTAPI
418 CsrUnlockThread(IN PCSR_THREAD CsrThread);
419 
420 BOOLEAN
421 NTAPI
422 CsrValidateMessageBuffer(IN PCSR_API_MESSAGE ApiMessage,
423                          IN PVOID *Buffer,
424                          IN ULONG ElementCount,
425                          IN ULONG ElementSize);
426 
427 BOOLEAN
428 NTAPI
429 CsrValidateMessageString(IN PCSR_API_MESSAGE ApiMessage,
430                          IN PWSTR *MessageString);
431 
432 #endif // _CSRSRV_H
433 
434 /* EOF */
435