543aaced35f19adbfb0aebfc673a41442801dd9a
[reactos.git] / reactos / subsys / csr / csrsrv / server.c
1 /* $Id$
2 *
3 * subsys/csr/csrsrv/server.c - CSR server - subsystem default server
4 *
5 * ReactOS Operating System
6 *
7 * --------------------------------------------------------------------
8 *
9 * This software is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This software is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this software; see the file COPYING.LIB. If not, write
21 * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
22 * MA 02139, USA.
23 *
24 * --------------------------------------------------------------------
25 */
26 #include "srv.h"
27
28 //#define NDEBUG
29 #include <debug.h>
30
31 typedef struct _CSRSRV_SERVER_DLL
32 {
33 USHORT ServerIndex;
34 USHORT Sequence; // initialization order
35 UNICODE_STRING DllName;
36 UNICODE_STRING DllEntryPoint;
37 CSR_SERVER_THREAD ServerThread; // NULL ==> inactive
38
39 } CSRSRV_SERVER_DLL, *PCSRSRV_SERVER_DLL;
40
41 /*=====================================================================
42 * GLOBALS
43 *===================================================================*/
44
45 CSRSRV_OPTION CsrSrvOption;
46
47 HANDLE CsrSrvApiPortHandle = (HANDLE) 0;
48
49 /*=====================================================================
50 * LOCALS
51 *===================================================================*/
52
53 static HANDLE CsrSrvSbApiPortHandle = (HANDLE) 0;
54
55 static CSRSRV_SERVER_DLL ServerThread [CSR_SERVER_DLL_MAX];
56
57 VOID CALLBACK CsrSrvServerThread (PVOID);
58
59 /**********************************************************************
60 * CsrSrvRegisterServerDll/1
61 */
62 NTSTATUS STDCALL CsrSrvRegisterServerDll (PCSR_SERVER_DLL pServerDll)
63 {
64 static USHORT NextInSequence = 0;
65 USHORT ServerIndex = 0;
66
67 // 1st call?
68 if (0 == NextInSequence)
69 {
70 RtlZeroMemory (ServerThread, sizeof ServerThread);
71 }
72 // We can not register more than CSR_SERVER_DLL_MAX servers.
73 // Note: # servers >= # DLLs (MS Win32 has 3 servers in 2 DLLs).
74 if (NextInSequence >= CSR_SERVER_DLL_MAX)
75 {
76 return STATUS_NO_MEMORY;
77 }
78 // Validate the ServerIndex from the command line:
79 // it may be 0, 1, 2, or 3.
80 ServerIndex = pServerDll->ServerIndex;
81 if (ServerIndex >= CSR_SERVER_DLL_MAX)
82 {
83 return STATUS_INVALID_PARAMETER;
84 }
85 // Register the DLL server.
86 ServerThread [ServerIndex].ServerIndex = ServerIndex;
87 ServerThread [ServerIndex].Sequence = NextInSequence ++;
88 if (0 != ServerIndex)
89 {
90 RtlDuplicateUnicodeString (1, & pServerDll->DllName, & ServerThread [ServerIndex].DllName);
91 RtlDuplicateUnicodeString (1, & pServerDll->DllEntryPoint, & ServerThread [ServerIndex].DllEntryPoint);
92 } else {
93 // CSRSRV.DLL own static server thread
94 ServerThread [ServerIndex].ServerThread = CsrSrvServerThread;
95 }
96 return STATUS_SUCCESS;
97 }
98 /**********************************************************************
99 * CsrpCreateObjectDirectory/1 PRIVATE
100 */
101 NTSTATUS STDCALL CsrpCreateObjectDirectory (PUNICODE_STRING pObjectDirectory)
102 {
103 NTSTATUS Status = STATUS_SUCCESS;
104 OBJECT_ATTRIBUTES DirectoryAttributes;
105
106 DPRINT("CSRSRV:%s(%S) called\n", __FUNCTION__, pObjectDirectory->Buffer);
107
108 InitializeObjectAttributes (& DirectoryAttributes,
109 pObjectDirectory,
110 OBJ_OPENIF,
111 NULL,
112 NULL);
113
114 Status = NtCreateDirectoryObject (& CsrSrvOption.NameSpace.RootHandle,
115 (DIRECTORY_CREATE_OBJECT|DIRECTORY_CREATE_SUBDIRECTORY),
116 & DirectoryAttributes);
117 if (NT_SUCCESS(Status))
118 {
119 Status = RtlDuplicateUnicodeString (0, pObjectDirectory, & CsrSrvOption.NameSpace.Root);
120 if (!NT_SUCCESS(Status))
121 {
122 DPRINT1("CSRSRV:%s: RtlDuplicateUnicodeString failed (Status=0x%08lx)\n",
123 __FUNCTION__, Status);
124 }
125 } else {
126 DPRINT1("CSRSRV:%s: fatal: NtCreateDirectoryObject failed (Status=0x%08lx)\n",
127 __FUNCTION__, Status);
128 }
129 return Status;
130 }
131 /**********************************************************************
132 * CsrSrvBootstrap/0
133 *
134 * DESCRIPTION
135 * This is where a subsystem begins living.
136 */
137 NTSTATUS STDCALL CsrSrvBootstrap (VOID)
138 {
139 NTSTATUS Status = STATUS_SUCCESS;
140 ULONG ServerIndex = 0;
141 ULONG ServerSequence = 0;
142
143 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
144
145 CsrSrvSbApiPortHandle = CsrSrvSbApiPortHandle; //FIXME
146
147 // OBJECT DIRECTORY
148 Status = CsrpCreateObjectDirectory (& CsrSrvOption.NameSpace.Root);
149 if(!NT_SUCCESS(Status))
150 {
151 DPRINT1("CSRSRV:%s: CsrpCreateObjectDirectory failed (Status=%08lx)\n",
152 __FUNCTION__, Status);
153 return Status;
154 }
155 // SESSIONS
156 Status = CsrSrvInitializeSession ();
157 if(!NT_SUCCESS(Status))
158 {
159 DPRINT1("CSRSRV:%s: CsrSrvInitializeSession failed (Status=%08lx)\n",
160 __FUNCTION__, Status);
161 return Status;
162 }
163 // PROCESSES
164 // TODO
165 // THREADS
166 // TODO
167 // WAITS
168 // TODO
169 // Hosted servers
170 for (ServerSequence = 0; ServerSequence < CSR_SERVER_DLL_MAX; ServerSequence ++)
171 {
172 for (ServerIndex = 0; (ServerIndex < CSR_SERVER_DLL_MAX); ++ ServerIndex)
173 {
174 if (ServerSequence == ServerThread [ServerIndex].Sequence)
175 {
176 if (NULL == ServerThread [ServerIndex].ServerThread)
177 {
178 //TODO: load DLL and call ServerDllInitialize
179 }
180 }
181 }
182 }
183 return Status;
184 }
185 /**********************************************************************
186 * CsrSrvServerThread/1
187 *
188 * DESCRIPTION
189 * This is actually a function called by the CsrSrvMainServerThread
190 * when the server index is 0. Other server DLLs register their
191 * function with CsrAddStaticServerThread.
192 */
193 VOID STDCALL CsrSrvServerThread (PVOID x)
194 {
195 NTSTATUS Status = STATUS_SUCCESS;
196 PPORT_MESSAGE Request = (PPORT_MESSAGE) x;
197 PPORT_MESSAGE Reply = NULL;
198 ULONG MessageType = 0;
199
200 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
201
202 MessageType = Request->u2.s2.Type;
203 DPRINT("CSRSRV: %s received a message (Type=%d)\n",
204 __FUNCTION__, MessageType);
205 switch (MessageType)
206 {
207 //TODO
208 default:
209 Reply = Request;
210 Status = NtReplyPort (CsrSrvApiPortHandle, Reply);
211 break;
212 }
213 }
214
215 /**********************************************************************
216 * PUBLIC API
217 *********************************************************************/
218
219 /**********************************************************************
220 * CsrAddStaticServerThread/1
221 */
222 NTSTATUS STDCALL CsrAddStaticServerThread (CSR_SERVER_THREAD ServerThread)
223 {
224 static ULONG StaticServerThreadCount = 0;
225 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
226
227 DPRINT("CSRSRV: %s(%08lx) called\n", __FUNCTION__, ServerThread);
228
229 if (StaticServerThreadCount > CSR_SERVER_DLL_MAX)
230 {
231 DPRINT1("CSRSRV: subsystem tries to add mode than %d static threads!\n",
232 CSR_SERVER_DLL_MAX);
233 return STATUS_NO_MEMORY;
234 }
235 if (NT_SUCCESS(Status))
236 {
237 // FIXME: do we need to make it reentrant?
238 ++ StaticServerThreadCount;
239 }
240 return Status;
241 }
242
243 /**********************************************************************
244 * CsrCallServerFromServer
245 */
246 NTSTATUS STDCALL CsrCallServerFromServer ()
247 {
248 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
249
250 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
251 return Status;
252 }
253
254 /**********************************************************************
255 * CsrExecServerThread
256 */
257 NTSTATUS STDCALL CsrExecServerThread ()
258 {
259 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
260
261 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
262 return Status;
263 }
264
265 /**********************************************************************
266 * CsrImpersonateClient
267 */
268 NTSTATUS STDCALL CsrImpersonateClient ()
269 {
270 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
271
272 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
273 return Status;
274 }
275
276 /**********************************************************************
277 * CsrQueryApiPort/0
278 *
279 * @implemented
280 */
281 HANDLE STDCALL CsrQueryApiPort (VOID)
282 {
283 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
284 return CsrSrvApiPortHandle;
285 }
286
287 /**********************************************************************
288 * CsrRevertToSelf
289 */
290 NTSTATUS STDCALL CsrRevertToSelf ()
291 {
292 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
293
294 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
295 return Status;
296 }
297
298 /**********************************************************************
299 * CsrSetBackgroundPriority
300 */
301 NTSTATUS STDCALL CsrSetBackgroundPriority ()
302 {
303 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
304
305 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
306 return Status;
307 }
308
309 /**********************************************************************
310 * CsrSetCallingSpooler
311 */
312 NTSTATUS STDCALL CsrSetCallingSpooler ()
313 {
314 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
315
316 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
317 return Status;
318 }
319
320 /**********************************************************************
321 * CsrSetForegroundPriority
322 */
323 NTSTATUS STDCALL CsrSetForegroundPriority ()
324 {
325 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
326
327 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
328 return Status;
329 }
330
331 /**********************************************************************
332 * CsrUnhandledExceptionFilter
333 */
334 NTSTATUS STDCALL CsrUnhandledExceptionFilter ()
335 {
336 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
337
338 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
339 return Status;
340 }
341
342 /**********************************************************************
343 * CsrValidateMessageBuffer
344 */
345 NTSTATUS STDCALL CsrValidateMessageBuffer ()
346 {
347 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
348
349 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
350 return Status;
351 }
352
353 /**********************************************************************
354 * CsrValidateMessageString
355 */
356 NTSTATUS STDCALL CsrValidateMessageString ()
357 {
358 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
359
360 DPRINT("CSRSRV: %s called\n", __FUNCTION__);
361 return Status;
362 }
363
364 /* EOF */