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
);
231 NtClose(EnvironmentKey
);
234 /* Set the 'PROCESSOR_IDENTIFIER' system environment variable */
235 RtlInitUnicodeString(&Identifier
, NULL
);
236 RtlInitUnicodeString(&VendorIdentifier
, NULL
);
238 RtlZeroMemory(&QueryTable
,
241 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
242 QueryTable
[0].Name
= L
"Identifier";
243 QueryTable
[0].EntryContext
= &Identifier
;
245 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
246 QueryTable
[1].Name
= L
"VendorIdentifier";
247 QueryTable
[1].EntryContext
= &VendorIdentifier
;
249 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
250 L
"\\Registry\\Machine\\Hardware\\Description\\System\\CentralProcessor\\0",
254 if (NT_SUCCESS(Status
))
256 DPRINT("SM: szIdentifier: %wZ\n", &Identifier
);
257 DPRINT("SM: szVendorIdentifier: %wZ\n", &VendorIdentifier
);
259 swprintf(Buffer
, L
"%wZ, %wZ", &Identifier
, &VendorIdentifier
);
261 RtlWriteRegistryValue(RTL_REGISTRY_CONTROL
,
262 L
"Session Manager\\Environment",
263 L
"PROCESSOR_IDENTIFIER",
266 (wcslen(Buffer
) + 1) * sizeof(WCHAR
));
269 RtlFreeUnicodeString(&Identifier
);
270 RtlFreeUnicodeString(&VendorIdentifier
);
272 return STATUS_SUCCESS
;
276 /**********************************************************************
277 * Set environment variables from registry
280 SmUpdateEnvironment(VOID
)
282 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
283 WCHAR ValueBuffer
[MAX_PATH
];
287 * The following environment variables must be set prior to reading
288 * other variables from the registry.
290 * Variables (example):
291 * SystemRoot = "C:\reactos"
295 /* Copy system root into value buffer */
297 SharedUserData
->NtSystemRoot
);
299 /* Set SystemRoot = "C:\reactos" */
300 SmpSetEnvironmentVariable(&SmSystemEnvironment
, L
"SystemRoot", ValueBuffer
);
302 /* Cut off trailing path */
305 /* Set SystemDrive = "C:" */
306 SmpSetEnvironmentVariable(&SmSystemEnvironment
, L
"SystemDrive", ValueBuffer
);
308 /* Read system environment from the registry. */
309 RtlZeroMemory(&QueryTable
,
312 QueryTable
[0].QueryRoutine
= SmpEnvironmentQueryRoutine
;
314 Status
= RtlQueryRegistryValues(RTL_REGISTRY_CONTROL
,
315 L
"Session Manager\\Environment",
317 &SmSystemEnvironment
,
318 SmSystemEnvironment
);