1 /* $Id: init.c,v 1.25 2001/06/12 17:50:28 chorns Exp $
3 * init.c - Session Manager initialization
5 * ReactOS Operating System
7 * --------------------------------------------------------------------
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.
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.
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,
24 * --------------------------------------------------------------------
26 * 19990530 (Emanuele Aliberti)
27 * Compiled successfully with egcs 1.1.2
30 #include <ntdll/rtl.h>
32 #include <napi/shared_data.h>
38 /* GLOBAL VARIABLES *********************************************************/
40 HANDLE SmApiPort
= INVALID_HANDLE_VALUE
;
41 HANDLE DbgSsApiPort
= INVALID_HANDLE_VALUE
;
42 HANDLE DbgUiApiPort
= INVALID_HANDLE_VALUE
;
44 PWSTR SmSystemEnvironment
= NULL
;
47 /* FUNCTIONS ****************************************************************/
50 SmCreatePagingFiles (VOID
)
52 UNICODE_STRING FileName
;
55 CHAR FileNameBufA
[255];
56 ANSI_STRING FileNameA
;
59 OBJECT_ATTRIBUTES ObjectAttributes
;
62 static CHAR Buffer
[4096];
65 for (i
= 0; i
< 4; i
++)
67 for (j
= 0; j
< 4; j
++)
69 sprintf(FileNameBufA
, "\\Device\\Harddisk%d\\Partition%d", i
, j
);
70 RtlInitAnsiString(&FileNameA
, FileNameBufA
);
71 RtlAnsiStringToUnicodeString(&FileName
, &FileNameA
, TRUE
);
72 InitializeObjectAttributes(&ObjectAttributes
,
78 Status
= ZwOpenFile(&FileHandle
,
84 if (!NT_SUCCESS(Status
))
90 Status
= ZwReadFile(FileHandle
,
99 if (!NT_SUCCESS(Status
))
101 DbgPrint("SM: Failed to read first page of partition\n");
105 if (memcmp(&Buffer
[4096 - 10], "SWAP-SPACE", 10) == 0 ||
106 memcmp(&Buffer
[4096 - 10], "SWAPSPACE2", 10) == 0)
108 DbgPrint("SM: Found swap space at %s\n", FileNameA
);
120 SmCreatePagingFiles (VOID
)
122 UNICODE_STRING FileName
;
125 /* FIXME: Read file names from registry */
127 RtlInitUnicodeString (&FileName
,
128 L
"\\SystemRoot\\pagefile.sys");
130 NtCreatePagingFile (&FileName
,
139 SmSetEnvironmentVariables (VOID
)
141 UNICODE_STRING EnvVariable
;
142 UNICODE_STRING EnvValue
;
143 UNICODE_STRING EnvExpandedValue
;
144 ULONG ExpandedLength
;
145 WCHAR ExpandBuffer
[512];
146 WCHAR ValueBuffer
[MAX_PATH
];
147 PKUSER_SHARED_DATA SharedUserData
=
148 (PKUSER_SHARED_DATA
)USER_SHARED_DATA_BASE
;
151 * The following environment variables are read from the registry.
152 * Because the registry does not work yet, the environment variables
153 * are set one by one, using information from the shared user page.
155 * Variables (example):
156 * SystemRoot = C:\reactos
160 * Path = %SystemRoot%\system32;%SystemRoot%
161 * windir = %SystemRoot%
164 /* copy system root into value buffer */
165 wcscpy (ValueBuffer
, SharedUserData
->NtSystemRoot
);
167 /* set "SystemRoot = C:\reactos" */
168 RtlInitUnicodeString (&EnvVariable
,
170 RtlInitUnicodeString (&EnvValue
,
172 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
176 /* cut off trailing path */
179 /* Set "SystemDrive = C:" */
180 RtlInitUnicodeString (&EnvVariable
,
182 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
187 /* Set "OS = ReactOS" */
188 RtlInitUnicodeString (&EnvVariable
,
190 RtlInitUnicodeString (&EnvValue
,
192 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
197 /* Set "Path = %SystemRoot%\system32;%SystemRoot%" */
198 RtlInitUnicodeString (&EnvVariable
,
200 RtlInitUnicodeString (&EnvValue
,
201 L
"%SystemRoot%\\system32;%SystemRoot%");
202 EnvExpandedValue
.Length
= 0;
203 EnvExpandedValue
.MaximumLength
= 512 * sizeof(WCHAR
);
204 EnvExpandedValue
.Buffer
= ExpandBuffer
;
206 RtlExpandEnvironmentStrings_U (SmSystemEnvironment
,
210 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
214 /* Set "windir = %SystemRoot%" */
215 RtlInitUnicodeString (&EnvVariable
,
217 RtlInitUnicodeString (&EnvValue
,
219 EnvExpandedValue
.Length
= 0;
220 EnvExpandedValue
.MaximumLength
= 512 * sizeof(WCHAR
);
221 EnvExpandedValue
.Buffer
= ExpandBuffer
;
223 RtlExpandEnvironmentStrings_U (SmSystemEnvironment
,
227 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
233 BOOL
InitSessionManager (HANDLE Children
[])
236 UNICODE_STRING UnicodeString
;
237 OBJECT_ATTRIBUTES ObjectAttributes
;
238 UNICODE_STRING CmdLineW
;
239 PRTL_USER_PROCESS_PARAMETERS ProcessParameters
;
240 RTL_PROCESS_INFO ProcessInfo
;
241 HANDLE CsrssInitEvent
;
242 HANDLE WindowsDirectory
;
243 WCHAR UnicodeBuffer
[MAX_PATH
];
244 PKUSER_SHARED_DATA SharedUserData
=
245 (PKUSER_SHARED_DATA
)USER_SHARED_DATA_BASE
;
248 * FIXME: The '\Windows' directory is created by csrss.exe but
249 * win32k.sys needs it at intialization and it is loaded
254 * Create the '\Windows' directory
256 RtlInitUnicodeString(
260 InitializeObjectAttributes(
267 Status
= ZwCreateDirectoryObject(
271 if (!NT_SUCCESS(Status
))
273 DisplayString (L
"SM: Could not create \\Windows directory!\n");
277 /* Create the "\SmApiPort" object (LPC) */
278 RtlInitUnicodeString (&UnicodeString
,
280 InitializeObjectAttributes (&ObjectAttributes
,
286 Status
= NtCreatePort (&SmApiPort
,
292 if (!NT_SUCCESS(Status
))
298 DisplayString (L
"SM: \\SmApiPort created...\n");
301 /* Create two threads for "\SmApiPort" */
302 RtlCreateUserThread (NtCurrentProcess (),
308 (PTHREAD_START_ROUTINE
)SmApiThread
,
313 RtlCreateUserThread (NtCurrentProcess (),
319 (PTHREAD_START_ROUTINE
)SmApiThread
,
324 /* Create the system environment */
325 Status
= RtlCreateEnvironment (FALSE
,
326 &SmSystemEnvironment
);
327 if (!NT_SUCCESS(Status
))
332 DisplayString (L
"SM: System Environment created\n");
335 /* FIXME: Define symbolic links to kernel devices (MS-DOS names) */
337 /* FIXME: Run all programs in the boot execution list */
339 /* FIXME: Process the file rename list */
341 /* FIXME: Load the well known DLLs */
343 /* Create paging files */
344 // SmCreatePagingFiles ();
346 /* Load remaining registry hives */
347 NtInitializeRegistry (FALSE
);
349 /* Set environment variables from registry */
350 SmSetEnvironmentVariables ();
352 /* Load the kernel mode driver win32k.sys */
353 RtlInitUnicodeString (&CmdLineW
,
354 L
"\\SystemRoot\\system32\\drivers\\win32k.sys");
355 Status
= NtLoadDriver (&CmdLineW
);
358 if (!NT_SUCCESS(Status
))
364 RtlInitUnicodeString(&UnicodeString
,
366 InitializeObjectAttributes(&ObjectAttributes
,
371 Status
= NtCreateEvent(&CsrssInitEvent
,
376 if (!NT_SUCCESS(Status
))
378 DbgPrint("Failed to create csrss notification event\n");
382 * Start the Win32 subsystem (csrss.exe)
384 DisplayString (L
"SM: Running csrss.exe\n");
386 /* initialize executable path */
387 wcscpy(UnicodeBuffer
, L
"\\??\\");
388 wcscat(UnicodeBuffer
, SharedUserData
->NtSystemRoot
);
389 wcscat(UnicodeBuffer
, L
"\\system32\\csrss.exe");
390 RtlInitUnicodeString (&UnicodeString
,
393 RtlCreateProcessParameters (&ProcessParameters
,
404 Status
= RtlCreateUserProcess (&UnicodeString
,
405 OBJ_CASE_INSENSITIVE
,
415 RtlDestroyProcessParameters (ProcessParameters
);
417 if (!NT_SUCCESS(Status
))
419 DisplayString (L
"SM: Loading csrss.exe failed!\n");
423 DbgPrint("SM: Waiting for csrss\n");
424 NtWaitForSingleObject(CsrssInitEvent
,
427 DbgPrint("SM: Finished waiting for csrss\n");
429 Children
[CHILD_CSRSS
] = ProcessInfo
.ProcessHandle
;
432 * Start the logon process (winlogon.exe)
434 DisplayString(L
"SM: Running winlogon.exe\n");
436 /* initialize executable path */
437 wcscpy(UnicodeBuffer
, L
"\\??\\");
438 wcscat(UnicodeBuffer
, SharedUserData
->NtSystemRoot
);
439 wcscat(UnicodeBuffer
, L
"\\system32\\winlogon.exe");
440 RtlInitUnicodeString (&UnicodeString
,
443 RtlCreateProcessParameters(&ProcessParameters
,
454 Status
= RtlCreateUserProcess(&UnicodeString
,
455 OBJ_CASE_INSENSITIVE
,
465 RtlDestroyProcessParameters(ProcessParameters
);
467 if (!NT_SUCCESS(Status
))
469 DisplayString(L
"SM: Loading winlogon.exe failed!\n");
470 NtTerminateProcess(Children
[CHILD_CSRSS
],
474 Children
[CHILD_WINLOGON
] = ProcessInfo
.ProcessHandle
;
476 /* Create the \DbgSsApiPort object (LPC) */
477 RtlInitUnicodeString (&UnicodeString
,
479 InitializeObjectAttributes (&ObjectAttributes
,
485 Status
= NtCreatePort (&DbgSsApiPort
,
491 if (!NT_SUCCESS(Status
))
496 DisplayString (L
"SM: DbgSsApiPort created...\n");
499 /* Create the \DbgUiApiPort object (LPC) */
500 RtlInitUnicodeString (&UnicodeString
,
502 InitializeObjectAttributes (&ObjectAttributes
,
508 Status
= NtCreatePort (&DbgUiApiPort
,
514 if (!NT_SUCCESS(Status
))
519 DisplayString (L
"SM: DbgUiApiPort created...\n");