1 /* $Id: init.c,v 1.23 2001/03/25 02:34:30 dwelch 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
;
243 WCHAR UnicodeBuffer
[MAX_PATH
];
244 PKUSER_SHARED_DATA SharedUserData
=
245 (PKUSER_SHARED_DATA
)USER_SHARED_DATA_BASE
;
247 /* Create the "\SmApiPort" object (LPC) */
248 RtlInitUnicodeString (&UnicodeString
,
250 InitializeObjectAttributes (&ObjectAttributes
,
256 Status
= NtCreatePort (&SmApiPort
,
262 if (!NT_SUCCESS(Status
))
268 DisplayString (L
"SM: \\SmApiPort created...\n");
271 /* Create two threads for "\SmApiPort" */
272 RtlCreateUserThread (NtCurrentProcess (),
278 (PTHREAD_START_ROUTINE
)SmApiThread
,
283 RtlCreateUserThread (NtCurrentProcess (),
289 (PTHREAD_START_ROUTINE
)SmApiThread
,
294 /* Create the system environment */
295 Status
= RtlCreateEnvironment (FALSE
,
296 &SmSystemEnvironment
);
297 if (!NT_SUCCESS(Status
))
302 DisplayString (L
"SM: System Environment created\n");
305 /* FIXME: Define symbolic links to kernel devices (MS-DOS names) */
307 /* FIXME: Run all programs in the boot execution list */
309 /* FIXME: Process the file rename list */
311 /* FIXME: Load the well known DLLs */
313 /* Create paging files */
314 // SmCreatePagingFiles ();
316 /* Load remaining registry hives */
317 NtInitializeRegistry (FALSE
);
319 /* Set environment variables from registry */
320 SmSetEnvironmentVariables ();
322 /* Load the kernel mode driver win32k.sys */
323 RtlInitUnicodeString (&CmdLineW
,
324 L
"\\SystemRoot\\system32\\drivers\\win32k.sys");
325 Status
= NtLoadDriver (&CmdLineW
);
327 if (!NT_SUCCESS(Status
))
333 RtlInitUnicodeString(&UnicodeString
,
335 InitializeObjectAttributes(&ObjectAttributes
,
340 Status
= NtCreateEvent(&CsrssInitEvent
,
345 if (!NT_SUCCESS(Status
))
347 DbgPrint("Failed to create csrss notification event\n");
351 * Start the Win32 subsystem (csrss.exe)
353 DisplayString (L
"SM: Running csrss.exe\n");
355 /* initialize executable path */
356 wcscpy(UnicodeBuffer
, L
"\\??\\");
357 wcscat(UnicodeBuffer
, SharedUserData
->NtSystemRoot
);
358 wcscat(UnicodeBuffer
, L
"\\system32\\csrss.exe");
359 RtlInitUnicodeString (&UnicodeString
,
362 RtlCreateProcessParameters (&ProcessParameters
,
373 Status
= RtlCreateUserProcess (&UnicodeString
,
374 OBJ_CASE_INSENSITIVE
,
384 RtlDestroyProcessParameters (ProcessParameters
);
386 if (!NT_SUCCESS(Status
))
388 DisplayString (L
"SM: Loading csrss.exe failed!\n");
392 DbgPrint("SM: Waiting for csrss\n");
393 NtWaitForSingleObject(CsrssInitEvent
,
396 DbgPrint("SM: Finished waiting for csrss\n");
398 Children
[CHILD_CSRSS
] = ProcessInfo
.ProcessHandle
;
401 * Start the logon process (winlogon.exe)
403 DisplayString(L
"SM: Running winlogon.exe\n");
405 /* initialize executable path */
406 wcscpy(UnicodeBuffer
, L
"\\??\\");
407 wcscat(UnicodeBuffer
, SharedUserData
->NtSystemRoot
);
408 wcscat(UnicodeBuffer
, L
"\\system32\\winlogon.exe");
409 RtlInitUnicodeString (&UnicodeString
,
412 RtlCreateProcessParameters(&ProcessParameters
,
423 Status
= RtlCreateUserProcess(&UnicodeString
,
424 OBJ_CASE_INSENSITIVE
,
434 RtlDestroyProcessParameters(ProcessParameters
);
436 if (!NT_SUCCESS(Status
))
438 DisplayString(L
"SM: Loading winlogon.exe failed!\n");
439 NtTerminateProcess(Children
[CHILD_CSRSS
],
443 Children
[CHILD_WINLOGON
] = ProcessInfo
.ProcessHandle
;
445 /* Create the \DbgSsApiPort object (LPC) */
446 RtlInitUnicodeString (&UnicodeString
,
448 InitializeObjectAttributes (&ObjectAttributes
,
454 Status
= NtCreatePort (&DbgSsApiPort
,
460 if (!NT_SUCCESS(Status
))
465 DisplayString (L
"SM: DbgSsApiPort created...\n");
468 /* Create the \DbgUiApiPort object (LPC) */
469 RtlInitUnicodeString (&UnicodeString
,
471 InitializeObjectAttributes (&ObjectAttributes
,
477 Status
= NtCreatePort (&DbgUiApiPort
,
483 if (!NT_SUCCESS(Status
))
488 DisplayString (L
"SM: DbgUiApiPort created...\n");