Made header file usage more consistent
[reactos.git] / reactos / subsys / smss / init.c
1 /* $Id: init.c,v 1.18 2000/06/29 23:35:51 dwelch Exp $
2 *
3 * init.c - Session Manager initialization
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 * 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 * 19990530 (Emanuele Aliberti)
27 * Compiled successfully with egcs 1.1.2
28 */
29 #include <ntos.h>
30 #include <ntdll/rtl.h>
31 #include <napi/lpc.h>
32
33 #include "smss.h"
34
35 #define NDEBUG
36
37 /* uncomment to run csrss.exe */
38 #define RUN_CSRSS
39
40 /* GLOBAL VARIABLES *********************************************************/
41
42 HANDLE SmApiPort = INVALID_HANDLE_VALUE;
43 HANDLE DbgSsApiPort = INVALID_HANDLE_VALUE;
44 HANDLE DbgUiApiPort = INVALID_HANDLE_VALUE;
45
46 PVOID SmSystemEnvironment = NULL;
47
48
49 /* FUNCTIONS ****************************************************************/
50
51 #if 0
52 static VOID
53 SmCreatePagingFiles (VOID)
54 {
55 UNICODE_STRING FileName;
56 ULONG ulCurrentSize;
57
58 /* FIXME: Read file names from registry */
59
60 RtlInitUnicodeString (&FileName,
61 L"\\??\\C:\\reactos\\pagefile.sys");
62
63 NtCreatePagingFile (&FileName,
64 50,
65 80,
66 &ulCurrentSize);
67 }
68 #endif
69
70
71 static VOID
72 SmSetEnvironmentVariables (VOID)
73 {
74 UNICODE_STRING EnvVariable;
75 UNICODE_STRING EnvValue;
76 UNICODE_STRING EnvExpandedValue;
77 ULONG ExpandedLength;
78 WCHAR ExpandBuffer[512];
79
80 /*
81 * The following environment variables are read from the registry.
82 * Since the registry does not work yet, the environment variables
83 * are set one by one, using hard-coded default values.
84 *
85 * Variables:
86 * SystemRoot = C:\reactos
87 * SystemDrive = C:
88 *
89 * OS = ReactOS
90 * Path = %SystemRoot%\system32;%SystemRoot%
91 * windir = %SystemRoot%
92 */
93
94 /* Set "SystemRoot = C:\reactos" */
95 RtlInitUnicodeString (&EnvVariable,
96 L"SystemRoot");
97 RtlInitUnicodeString (&EnvValue,
98 L"C:\\reactos");
99 RtlSetEnvironmentVariable (&SmSystemEnvironment,
100 &EnvVariable,
101 &EnvValue);
102
103 /* Set "SystemDrive = C:" */
104 RtlInitUnicodeString (&EnvVariable,
105 L"SystemDrive");
106 RtlInitUnicodeString (&EnvValue,
107 L"C:");
108 RtlSetEnvironmentVariable (&SmSystemEnvironment,
109 &EnvVariable,
110 &EnvValue);
111
112
113 /* Set "OS = ReactOS" */
114 RtlInitUnicodeString (&EnvVariable,
115 L"OS");
116 RtlInitUnicodeString (&EnvValue,
117 L"ReactOS");
118 RtlSetEnvironmentVariable (&SmSystemEnvironment,
119 &EnvVariable,
120 &EnvValue);
121
122
123 /* Set "Path = %SystemRoot%\system32;%SystemRoot%" */
124 RtlInitUnicodeString (&EnvVariable,
125 L"Path");
126 RtlInitUnicodeString (&EnvValue,
127 L"%SystemRoot%\\system32;%SystemRoot%");
128 EnvExpandedValue.Length = 0;
129 EnvExpandedValue.MaximumLength = 512 * sizeof(WCHAR);
130 EnvExpandedValue.Buffer = ExpandBuffer;
131 *ExpandBuffer = 0;
132 RtlExpandEnvironmentStrings_U (SmSystemEnvironment,
133 &EnvValue,
134 &EnvExpandedValue,
135 &ExpandedLength);
136 RtlSetEnvironmentVariable (&SmSystemEnvironment,
137 &EnvVariable,
138 &EnvExpandedValue);
139
140 /* Set "windir = %SystemRoot%" */
141 RtlInitUnicodeString (&EnvVariable,
142 L"windir");
143 RtlInitUnicodeString (&EnvValue,
144 L"%SystemRoot%");
145 EnvExpandedValue.Length = 0;
146 EnvExpandedValue.MaximumLength = 512 * sizeof(WCHAR);
147 EnvExpandedValue.Buffer = ExpandBuffer;
148 *ExpandBuffer = 0;
149 RtlExpandEnvironmentStrings_U (SmSystemEnvironment,
150 &EnvValue,
151 &EnvExpandedValue,
152 &ExpandedLength);
153 RtlSetEnvironmentVariable (&SmSystemEnvironment,
154 &EnvVariable,
155 &EnvExpandedValue);
156 }
157
158
159 BOOL InitSessionManager (HANDLE Children[])
160 {
161 NTSTATUS Status;
162 UNICODE_STRING UnicodeString;
163 OBJECT_ATTRIBUTES ObjectAttributes;
164 UNICODE_STRING CmdLineW;
165 UNICODE_STRING CurrentDirectoryW;
166 PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
167 RTL_USER_PROCESS_INFO ProcessInfo;
168 HANDLE CsrssInitEvent;
169
170 /* Create the "\SmApiPort" object (LPC) */
171 RtlInitUnicodeString (&UnicodeString,
172 L"\\SmApiPort");
173 InitializeObjectAttributes (&ObjectAttributes,
174 &UnicodeString,
175 0xff,
176 NULL,
177 NULL);
178
179 Status = NtCreatePort (&SmApiPort,
180 &ObjectAttributes,
181 0,
182 0,
183 0);
184
185 if (!NT_SUCCESS(Status))
186 {
187 return FALSE;
188 }
189
190 #ifndef NDEBUG
191 DisplayString (L"SM: \\SmApiPort created...\n");
192 #endif
193
194 /* Create two threads for "\SmApiPort" */
195 RtlCreateUserThread (NtCurrentProcess (),
196 NULL,
197 FALSE,
198 0,
199 NULL,
200 NULL,
201 (PTHREAD_START_ROUTINE)SmApiThread,
202 (PVOID)SmApiPort,
203 NULL,
204 NULL);
205
206 RtlCreateUserThread (NtCurrentProcess (),
207 NULL,
208 FALSE,
209 0,
210 NULL,
211 NULL,
212 (PTHREAD_START_ROUTINE)SmApiThread,
213 (PVOID)SmApiPort,
214 NULL,
215 NULL);
216
217 /* Create the system environment */
218 Status = RtlCreateEnvironment (TRUE,
219 &SmSystemEnvironment);
220 if (!NT_SUCCESS(Status))
221 return FALSE;
222 #ifndef NDEBUG
223 DisplayString (L"SM: System Environment created\n");
224 #endif
225
226 /* FIXME: Define symbolic links to kernel devices (MS-DOS names) */
227
228 /* FIXME: Run all programs in the boot execution list */
229
230 /* FIXME: Process the file rename list */
231
232 /* FIXME: Load the well known DLLs */
233
234 #if 0
235 /* Create paging files */
236 SmCreatePagingFiles ();
237 #endif
238
239 #if 0
240 /* Load missing registry hives */
241 NtInitializeRegistry (FALSE);
242 #endif
243
244 /* Set environment variables from registry */
245 SmSetEnvironmentVariables ();
246
247 /* Load the kernel mode driver win32k.sys */
248 RtlInitUnicodeString (&CmdLineW,
249 L"\\??\\C:\\reactos\\system32\\drivers\\win32k.sys");
250 Status = NtLoadDriver (&CmdLineW);
251
252 if (!NT_SUCCESS(Status))
253 {
254 return FALSE;
255 }
256
257 #ifdef RUN_CSRSS
258 RtlInitUnicodeString(&UnicodeString,
259 L"\\CsrssInitDone");
260 InitializeObjectAttributes(&ObjectAttributes,
261 &UnicodeString,
262 EVENT_ALL_ACCESS,
263 0,
264 NULL);
265 Status = NtCreateEvent(&CsrssInitEvent,
266 EVENT_ALL_ACCESS,
267 &ObjectAttributes,
268 TRUE,
269 FALSE);
270 if (!NT_SUCCESS(Status))
271 {
272 DbgPrint("Failed to create csrss notification event\n");
273 }
274
275 /* Start the Win32 subsystem (csrss.exe) */
276 DisplayString (L"SM: Executing csrss.exe\n");
277
278 RtlInitUnicodeString (&UnicodeString,
279 L"\\??\\C:\\reactos\\system32\\csrss.exe");
280
281 /* initialize current directory */
282 RtlInitUnicodeString (&CurrentDirectoryW,
283 L"C:\\reactos\\system32\\");
284
285 RtlCreateProcessParameters (&ProcessParameters,
286 &UnicodeString,
287 NULL,
288 &CurrentDirectoryW,
289 NULL,
290 SmSystemEnvironment,
291 NULL,
292 NULL,
293 NULL,
294 NULL);
295
296 Status = RtlCreateUserProcess (&UnicodeString,
297 0,
298 ProcessParameters,
299 NULL,
300 NULL,
301 FALSE,
302 0,
303 0,
304 0,
305 &ProcessInfo);
306
307 RtlDestroyProcessParameters (ProcessParameters);
308
309 if (!NT_SUCCESS(Status))
310 {
311 DisplayString (L"SM: Loading csrss.exe failed!\n");
312 return FALSE;
313 }
314
315 DbgPrint("SM: Waiting for csrss\n");
316 NtWaitForSingleObject(CsrssInitEvent,
317 FALSE,
318 NULL);
319 DbgPrint("SM: Finished waiting for csrss\n");
320
321 Children[CHILD_CSRSS] = ProcessInfo.ProcessHandle;
322 #endif /* RUN_CSRSS */
323
324
325 /* Start the simple shell (shell.exe) */
326 DisplayString (L"SM: Executing shell\n");
327 RtlInitUnicodeString (&UnicodeString,
328 L"\\??\\C:\\reactos\\system32\\shell.exe");
329 #if 0
330 /* Start the logon process (winlogon.exe) */
331 DisplayString (L"SM: Running winlogon\n");
332 RtlInitUnicodeString (&UnicodeString,
333 L"\\??\\C:\\reactos\\system32\\winlogon.exe");
334 #endif
335
336 /* initialize current directory (trailing backslash!!)*/
337 RtlInitUnicodeString (&CurrentDirectoryW,
338 L"C:\\reactos\\");
339
340 RtlCreateProcessParameters (&ProcessParameters,
341 &UnicodeString,
342 NULL,
343 &CurrentDirectoryW,
344 NULL,
345 SmSystemEnvironment,
346 NULL,
347 NULL,
348 NULL,
349 NULL);
350
351
352 Status = RtlCreateUserProcess (&UnicodeString,
353 0,
354 ProcessParameters,
355 NULL,
356 NULL,
357 FALSE,
358 0,
359 0,
360 0,
361 &ProcessInfo);
362
363 RtlDestroyProcessParameters (ProcessParameters);
364
365 if (!NT_SUCCESS(Status))
366 {
367 DisplayString (L"SM: Loading shell.exe failed!\n");
368 #if 0
369 NtTerminateProcess (Children[CHILD_CSRSS],
370 0);
371 #endif
372 return FALSE;
373 }
374 Children[CHILD_WINLOGON] = ProcessInfo.ProcessHandle;
375
376 /* Create the \DbgSsApiPort object (LPC) */
377 RtlInitUnicodeString (&UnicodeString,
378 L"\\DbgSsApiPort");
379 InitializeObjectAttributes (&ObjectAttributes,
380 &UnicodeString,
381 0xff,
382 NULL,
383 NULL);
384
385 Status = NtCreatePort (&DbgSsApiPort,
386 &ObjectAttributes,
387 0,
388 0,
389 0);
390
391 if (!NT_SUCCESS(Status))
392 {
393 return FALSE;
394 }
395 #ifndef NDEBUG
396 DisplayString (L"SM: DbgSsApiPort created...\n");
397 #endif
398
399 /* Create the \DbgUiApiPort object (LPC) */
400 RtlInitUnicodeString (&UnicodeString,
401 L"\\DbgUiApiPort");
402 InitializeObjectAttributes (&ObjectAttributes,
403 &UnicodeString,
404 0xff,
405 NULL,
406 NULL);
407
408 Status = NtCreatePort (&DbgUiApiPort,
409 &ObjectAttributes,
410 0,
411 0,
412 0);
413
414 if (!NT_SUCCESS(Status))
415 {
416 return FALSE;
417 }
418 #ifndef NDEBUG
419 DisplayString (L"SM: DbgUiApiPort created...\n");
420 #endif
421
422 return TRUE;
423 }
424
425 /* EOF */