2 * PROJECT: ReactOS Session Manager
3 * LICENSE: GPL v2 or later - See COPYING in the top level directory
4 * FILE: base/system/smss/initenv.c
5 * PURPOSE: Environment initialization.
6 * PROGRAMMERS: ReactOS Development Team
9 /* INCLUDES ******************************************************************/
17 PWSTR SmSystemEnvironment
= NULL
;
23 SmCreateEnvironment(VOID
)
25 return RtlCreateEnvironment(FALSE
, &SmSystemEnvironment
);
30 SmpSetEnvironmentVariable(IN PVOID Context
,
34 UNICODE_STRING EnvVariable
;
35 UNICODE_STRING EnvValue
;
37 RtlInitUnicodeString(&EnvVariable
,
39 RtlInitUnicodeString(&EnvValue
,
41 return RtlSetEnvironmentVariable(Context
,
48 SmpEnvironmentQueryRoutine(IN PWSTR ValueName
,
53 IN PVOID EntryContext
)
55 DPRINT("ValueName '%S' Type %lu Length %lu\n", ValueName
, ValueType
, ValueLength
);
57 if (ValueType
!= REG_SZ
&& ValueType
!= REG_EXPAND_SZ
)
58 return STATUS_SUCCESS
;
60 DPRINT("ValueData '%S'\n", (PWSTR
)ValueData
);
61 return SmpSetEnvironmentVariable(Context
,ValueName
,(PWSTR
)ValueData
);
66 SmSetEnvironmentVariables(VOID
)
68 SYSTEM_BASIC_INFORMATION BasicInformation
;
69 SYSTEM_PROCESSOR_INFORMATION ProcessorInformation
;
70 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
71 UNICODE_STRING Identifier
;
72 UNICODE_STRING VendorIdentifier
;
74 UNICODE_STRING EnvironmentKeyName
;
75 OBJECT_ATTRIBUTES ObjectAttributes
;
76 HANDLE EnvironmentKey
;
77 UNICODE_STRING VariableName
;
81 Status
= NtQuerySystemInformation(SystemProcessorInformation
,
82 &ProcessorInformation
,
83 sizeof(SYSTEM_PROCESSOR_INFORMATION
),
85 if (!NT_SUCCESS(Status
))
87 DPRINT1("SM: Failed to retrieve system processor information (Status %08lx)", Status
);
91 Status
= NtQuerySystemInformation(SystemBasicInformation
,
93 sizeof(SYSTEM_BASIC_INFORMATION
),
95 if (!NT_SUCCESS(Status
))
97 DPRINT1("SM: Failed to retrieve system basic information (Status %08lx)", Status
);
101 RtlInitUnicodeString(&EnvironmentKeyName
,
102 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\Environment");
103 InitializeObjectAttributes(&ObjectAttributes
,
105 OBJ_CASE_INSENSITIVE
,
109 /* Open the system environment key */
110 Status
= NtOpenKey(&EnvironmentKey
,
113 if (!NT_SUCCESS(Status
))
115 DPRINT1("SM: Failed to open the environment key (Status %08lx)", Status
);
119 /* Set the 'NUMBER_OF_PROCESSORS' system environment variable */
120 RtlInitUnicodeString(&VariableName
,
121 L
"NUMBER_OF_PROCESSORS");
123 swprintf(Buffer
, L
"%lu", BasicInformation
.NumberOfProcessors
);
125 Status
= NtSetValueKey(EnvironmentKey
,
130 (wcslen(Buffer
) + 1) * sizeof(WCHAR
));
131 if (!NT_SUCCESS(Status
))
133 DPRINT1("SM: Failed to set the NUMBER_OF_PROCESSORS environment variable (Status %08lx)", Status
);
137 /* Set the 'OS' system environment variable */
138 RtlInitUnicodeString(&VariableName
,
141 VariableData
= L
"ReactOS";
143 Status
= NtSetValueKey(EnvironmentKey
,
148 (wcslen(VariableData
) + 1) * sizeof(WCHAR
));
149 if (!NT_SUCCESS(Status
))
151 DPRINT1("SM: Failed to set the OS environment variable (Status %08lx)", Status
);
155 /* Set the 'PROCESSOR_ARCHITECTURE' system environment variable */
156 RtlInitUnicodeString(&VariableName
,
157 L
"PROCESSOR_ARCHITECTURE");
159 switch (ProcessorInformation
.ProcessorArchitecture
)
161 case PROCESSOR_ARCHITECTURE_INTEL
:
162 VariableData
= L
"x86";
165 case PROCESSOR_ARCHITECTURE_PPC
:
166 VariableData
= L
"PPC";
169 case PROCESSOR_ARCHITECTURE_ARM
:
170 VariableData
= L
"ARM";
173 case PROCESSOR_ARCHITECTURE_AMD64
:
174 VariableData
= L
"AMD64";
178 VariableData
= L
"Unknown";
182 Status
= NtSetValueKey(EnvironmentKey
,
187 (wcslen(VariableData
) + 1) * sizeof(WCHAR
));
188 if (!NT_SUCCESS(Status
))
190 DPRINT1("SM: Failed to set the PROCESSOR_ARCHITECTURE environment variable (Status %08lx)", Status
);
194 /* Set the 'PROCESSOR_LEVEL' system environment variable */
195 RtlInitUnicodeString(&VariableName
,
198 swprintf(Buffer
, L
"%lu", ProcessorInformation
.ProcessorLevel
);
200 Status
= NtSetValueKey(EnvironmentKey
,
205 (wcslen(Buffer
) + 1) * sizeof(WCHAR
));
206 if (!NT_SUCCESS(Status
))
208 DPRINT1("SM: Failed to set the PROCESSOR_LEVEL environment variable (Status %08lx)", Status
);
212 /* Set the 'PROCESSOR_REVISION' system environment variable */
213 RtlInitUnicodeString(&VariableName
,
214 L
"PROCESSOR_REVISION");
216 swprintf(Buffer
, L
"%04x", ProcessorInformation
.ProcessorRevision
);
218 Status
= NtSetValueKey(EnvironmentKey
,
223 (wcslen(Buffer
) + 1) * sizeof(WCHAR
));
224 if (!NT_SUCCESS(Status
))
226 DPRINT1("SM: Failed to set the PROCESSOR_REVISION environment variable (Status %08lx)", Status
);
230 /* Set the 'PROCESSOR_IDENTIFIER' system environment variable */
231 RtlInitUnicodeString(&Identifier
, NULL
);
232 RtlInitUnicodeString(&VendorIdentifier
, NULL
);
234 RtlZeroMemory(&QueryTable
,
237 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
238 QueryTable
[0].Name
= L
"Identifier";
239 QueryTable
[0].EntryContext
= &Identifier
;
241 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
242 QueryTable
[1].Name
= L
"VendorIdentifier";
243 QueryTable
[1].EntryContext
= &VendorIdentifier
;
245 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
246 L
"\\Registry\\Machine\\Hardware\\Description\\System\\CentralProcessor\\0",
250 if (!NT_SUCCESS(Status
))
252 DPRINT1("SM: Failed to retrieve processor Identifier and/or VendorIdentifier (Status %08lx)", Status
);
256 DPRINT("SM: szIdentifier: %wZ\n" , &Identifier
);
257 DPRINT("SM: szVendorIdentifier: %wZ\n", &VendorIdentifier
);
259 RtlInitUnicodeString(&VariableName
, L
"PROCESSOR_IDENTIFIER");
260 swprintf(Buffer
, L
"%wZ, %wZ", &Identifier
, &VendorIdentifier
);
261 RtlFreeUnicodeString(&VendorIdentifier
);
262 RtlFreeUnicodeString(&Identifier
);
264 Status
= NtSetValueKey(EnvironmentKey
,
269 (wcslen(Buffer
) + 1) * sizeof(WCHAR
));
270 if (!NT_SUCCESS(Status
))
272 DPRINT1("SM: Failed to set the PROCESSOR_IDENTIFIER environment variable (Status %08lx)", Status
);
277 /* Close the handle */
278 NtClose(EnvironmentKey
);
284 /**********************************************************************
285 * Set environment variables from registry
288 SmUpdateEnvironment(VOID
)
290 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
291 WCHAR ValueBuffer
[MAX_PATH
];
299 * The following environment variables must be set prior to reading
300 * other variables from the registry.
302 * Variables (example):
303 * SystemRoot = "C:\reactos"
307 /* Copy system root into value buffer */
309 SharedUserData
->NtSystemRoot
);
311 /* Set SystemRoot = "C:\reactos" */
312 SmpSetEnvironmentVariable(&SmSystemEnvironment
, L
"SystemRoot", ValueBuffer
);
314 /* Cut off trailing path */
317 /* Set SystemDrive = "C:" */
318 SmpSetEnvironmentVariable(&SmSystemEnvironment
, L
"SystemDrive", ValueBuffer
);
320 /* Read system environment from the registry. */
321 RtlZeroMemory(&QueryTable
,
324 QueryTable
[0].QueryRoutine
= SmpEnvironmentQueryRoutine
;
326 Status
= RtlQueryRegistryValues(RTL_REGISTRY_CONTROL
,
327 L
"Session Manager\\Environment",
329 &SmSystemEnvironment
,
330 SmSystemEnvironment
);
333 /* Print all environment varaibles */
335 envp
= SmSystemEnvironment
;
336 DbgPrint("SmUpdateEnvironment:\n");
339 DbgPrint(" %u: %S\n", ii
++, envp
);
340 envp
+= wcslen(envp
) + 1;