1 /*
2 * Ntdll environment functions
3 *
4 * Copyright 1996, 1998 Alexandre Julliard
5 * Copyright 2003 Eric Pouech
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21 #include "config.h"
22
23 #include <assert.h>
24 #include <stdarg.h>
25
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
33 #include "winnt.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(environ);
36
37 /******************************************************************************
38 * NtQuerySystemEnvironmentValue [NTDLL.@]
39 */
40 NTSYSAPI NTSTATUS WINAPI NtQuerySystemEnvironmentValue(PUNICODE_STRING VariableName,
41 PWCHAR Value,
42 ULONG ValueBufferLength,
43 PULONG RequiredLength)
44 {
45 FIXME("stub!\n");
46 return STATUS_NOT_IMPLEMENTED;
47 }
48
49 /******************************************************************************
50 * RtlCreateEnvironment [NTDLL.@]
51 */
52 NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
53 {
54 NTSTATUS nts;
55
56 TRACE("(%u,%p)!\n", inherit, env);
57
58 if (inherit)
59 {
60 MEMORY_BASIC_INFORMATION mbi;
61
62 RtlAcquirePebLock();
63
64 nts = NtQueryVirtualMemory(NtCurrentProcess(),
65 NtCurrentTeb()->Peb->ProcessParameters->Environment,
66 0, &mbi, sizeof(mbi), NULL);
67 if (nts == STATUS_SUCCESS)
68 {
69 *env = NULL;
70 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &mbi.RegionSize,
71 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
72 if (nts == STATUS_SUCCESS)
73 memcpy(*env, NtCurrentTeb()->Peb->ProcessParameters->Environment, mbi.RegionSize);
74 else *env = NULL;
75 }
76 RtlReleasePebLock();
77 }
78 else
79 {
80 SIZE_T size = 1;
81 PVOID addr = NULL;
82 nts = NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
83 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
84 if (nts == STATUS_SUCCESS) *env = addr;
85 }
86
87 return nts;
88 }
89
90 /******************************************************************************
91 * RtlDestroyEnvironment [NTDLL.@]
92 */
93 NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
94 {
95 SIZE_T size = 0;
96
97 TRACE("(%p)!\n", env);
98
99 return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env, &size, MEM_RELEASE);
100 }
101
102 static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
103 {
104 for (; *var; var += strlenW(var) + 1)
105 {
106 /* match var names, but avoid setting a var with a name including a '='
107 * (a starting '=' is valid though)
108 */
109 if (strncmpiW(var, name, namelen) == 0 && var[namelen] == '=' &&
110 strchrW(var + 1, '=') == var + namelen)
111 {
112 return var + namelen + 1;
113 }
114 }
115 return NULL;
116 }
117
118 /******************************************************************
119 * RtlQueryEnvironmentVariable_U [NTDLL.@]
120 *
121 * NOTES: when the buffer is too small, the string is not written, but if the
122 * terminating null char is the only char that cannot be written, then
123 * all chars (except the null) are written and success is returned
124 * (behavior of Win2k at least)
125 */
126 NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
127 PUNICODE_STRING name,
128 PUNICODE_STRING value)
129 {
130 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
131 PCWSTR var;
132 unsigned namelen;
133
134 TRACE("%p %s %p\n", env, debugstr_us(name), value);
135
136 value->Length = 0;
137 namelen = name->Length / sizeof(WCHAR);
138 if (!namelen) return nts;
139
140 if (!env)
141 {
142 RtlAcquirePebLock();
143 var = NtCurrentTeb()->Peb->ProcessParameters->Environment;
144 }
145 else var = env;
146
147 var = ENV_FindVariable(var, name->Buffer, namelen);
148 if (var != NULL)
149 {
150 value->Length = strlenW(var) * sizeof(WCHAR);
151
152 if (value->Length <= value->MaximumLength)
153 {
154 memmove(value->Buffer, var, min(value->Length + sizeof(WCHAR), value->MaximumLength));
155 nts = STATUS_SUCCESS;
156 }
157 else nts = STATUS_BUFFER_TOO_SMALL;
158 }
159
160 if (!env) RtlReleasePebLock();
161
162 return nts;
163 }
164
165 /******************************************************************
166 * RtlSetCurrentEnvironment [NTDLL.@]
167 *
168 */
169 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
170 {
171 TRACE("(%p %p)\n", new_env, old_env);
172
173 RtlAcquirePebLock();
174
175 if (old_env) *old_env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
176 NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
177
178 RtlReleasePebLock();
179 }
180
181
182 /******************************************************************************
183 * RtlSetEnvironmentVariable [NTDLL.@]
184 */
185 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
186 PUNICODE_STRING value)
187 {
188 INT len, old_size;
189 LPWSTR p, env;
190 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
191 MEMORY_BASIC_INFORMATION mbi;
192
193 TRACE("(%p, %s, %s)\n", penv, debugstr_us(name), debugstr_us(value));
194
195 if (!name || !name->Buffer || !name->Length)
196 return STATUS_INVALID_PARAMETER_1;
197
198 len = name->Length / sizeof(WCHAR);
199
200 /* variable names can't contain a '=' except as a first character */
201 for (p = name->Buffer + 1; p < name->Buffer + len; p++)
202 if (*p == '=') return STATUS_INVALID_PARAMETER;
203
204 if (!penv)
205 {
206 RtlAcquirePebLock();
207 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
208 } else env = *penv;
209
210 /* compute current size of environment */
211 for (p = env; *p; p += strlenW(p) + 1);
212 old_size = p + 1 - env;
213
214 /* Find a place to insert the string */
215 for (p = env; *p; p += strlenW(p) + 1)
216 {
217 if (!strncmpiW(name->Buffer, p, len) && (p[len] == '=')) break;
218 }
219 if (!value && !*p) goto done; /* Value to remove doesn't exist */
220
221 /* Realloc the buffer */
222 len = value ? len + value->Length / sizeof(WCHAR) + 2 : 0;
223 if (*p) len -= strlenW(p) + 1; /* The name already exists */
224
225 if (len < 0)
226 {
227 LPWSTR next = p + strlenW(p) + 1; /* We know there is a next one */
228 memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
229 }
230
231 nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0,
232 &mbi, sizeof(mbi), NULL);
233 if (nts != STATUS_SUCCESS) goto done;
234
235 if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
236 {
237 LPWSTR new_env;
238 SIZE_T new_size = (old_size + len) * sizeof(WCHAR);
239
240 new_env = NULL;
241 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
242 &new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
243 if (nts != STATUS_SUCCESS) goto done;
244
245 memmove(new_env, env, (p - env) * sizeof(WCHAR));
246 assert(len > 0);
247 memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
248 p = new_env + (p - env);
249
250 RtlDestroyEnvironment(env);
251 if (!penv) NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
252 else *penv = new_env;
253 }
254 else
255 {
256 if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
257 }
258
259 /* Set the new string */
260 if (value)
261 {
262 memcpy( p, name->Buffer, name->Length );
263 p += name->Length / sizeof(WCHAR);
264 *p++ = '=';
265 memcpy( p, value->Buffer, value->Length );
266 p[value->Length / sizeof(WCHAR)] = 0;
267 }
268 done:
269 if (!penv) RtlReleasePebLock();
270
271 return nts;
272 }
273
274 /******************************************************************
275 * RtlExpandEnvironmentStrings_U (NTDLL.@)
276 *
277 */
278 NTSTATUS WINAPI RtlExpandEnvironmentStrings_U(PCWSTR renv, const UNICODE_STRING* us_src,
279 PUNICODE_STRING us_dst, PULONG plen)
280 {
281 DWORD src_len, len, count, total_size = 1; /* 1 for terminating '\0' */
282 LPCWSTR env, src, p, var;
283 LPWSTR dst;
284
285 src = us_src->Buffer;
286 src_len = us_src->Length / sizeof(WCHAR);
287 count = us_dst->MaximumLength / sizeof(WCHAR);
288 dst = count ? us_dst->Buffer : NULL;
289
290 if (!renv)
291 {
292 RtlAcquirePebLock();
293 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
294 }
295 else env = renv;
296
297 while (src_len)
298 {
299 if (*src != '%')
300 {
301 if ((p = memchrW( src, '%', src_len ))) len = p - src;
302 else len = src_len;
303 var = src;
304 src += len;
305 src_len -= len;
306 }
307 else /* we are at the start of a variable */
308 {
309 if ((p = memchrW( src + 1, '%', src_len - 1 )))
310 {
311 len = p - src - 1; /* Length of the variable name */
312 if ((var = ENV_FindVariable( env, src + 1, len )))
313 {
314 src += len + 2; /* Skip the variable name */
315 src_len -= len + 2;
316 len = strlenW(var);
317 }
318 else
319 {
320 var = src; /* Copy original name instead */
321 len += 2;
322 src += len;
323 src_len -= len;
324 }
325 }
326 else /* unfinished variable name, ignore it */
327 {
328 var = src;
329 len = src_len; /* Copy whole string */
330 src += len;
331 src_len = 0;
332 }
333 }
334 total_size += len;
335 if (dst)
336 {
337 if (count < len) len = count;
338 memcpy(dst, var, len * sizeof(WCHAR));
339 count -= len;
340 dst += len;
341 }
342 }
343
344 if (!renv) RtlReleasePebLock();
345
346 /* Null-terminate the string */
347 if (dst && count) *dst = '\0';
348
349 us_dst->Length = (dst) ? (dst - us_dst->Buffer) * sizeof(WCHAR) : 0;
350 if (plen) *plen = total_size * sizeof(WCHAR);
351
352 return (count) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
353 }
354
355
356 static inline void normalize( void *base, WCHAR **ptr )
357 {
358 if (*ptr) *ptr = (WCHAR *)((char *)base + (UINT_PTR)*ptr);
359 }
360
361 /******************************************************************************
362 * RtlNormalizeProcessParams [NTDLL.@]
363 */
364 PRTL_USER_PROCESS_PARAMETERS WINAPI RtlNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
365 {
366 if (params && !(params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
367 {
368 normalize( params, ¶ms->CurrentDirectory.DosPath.Buffer );
369 normalize( params, ¶ms->DllPath.Buffer );
370 normalize( params, ¶ms->ImagePathName.Buffer );
371 normalize( params, ¶ms->CommandLine.Buffer );
372 normalize( params, ¶ms->WindowTitle.Buffer );
373 normalize( params, ¶ms->Desktop.Buffer );
374 normalize( params, ¶ms->ShellInfo.Buffer );
375 normalize( params, ¶ms->RuntimeInfo.Buffer );
376 params->Flags |= PROCESS_PARAMS_FLAG_NORMALIZED;
377 }
378 return params;
379 }
380
381
382 static inline void denormalize( const void *base, WCHAR **ptr )
383 {
384 if (*ptr) *ptr = (WCHAR *)(UINT_PTR)((char *)*ptr - (const char *)base);
385 }
386
387 /******************************************************************************
388 * RtlDeNormalizeProcessParams [NTDLL.@]
389 */
390 PRTL_USER_PROCESS_PARAMETERS WINAPI RtlDeNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
391 {
392 if (params && (params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
393 {
394 denormalize( params, ¶ms->CurrentDirectory.DosPath.Buffer );
395 denormalize( params, ¶ms->DllPath.Buffer );
396 denormalize( params, ¶ms->ImagePathName.Buffer );
397 denormalize( params, ¶ms->CommandLine.Buffer );
398 denormalize( params, ¶ms->WindowTitle.Buffer );
399 denormalize( params, ¶ms->Desktop.Buffer );
400 denormalize( params, ¶ms->ShellInfo.Buffer );
401 denormalize( params, ¶ms->RuntimeInfo.Buffer );
402 params->Flags &= ~PROCESS_PARAMS_FLAG_NORMALIZED;
403 }
404 return params;
405 }
406
407
408 /* append a unicode string to the process params data; helper for RtlCreateProcessParameters */
409 static void append_unicode_string( void **data, const UNICODE_STRING *src,
410 UNICODE_STRING *dst )
411 {
412 dst->Length = src->Length;
413 dst->MaximumLength = src->MaximumLength;
414 dst->Buffer = *data;
415 memcpy( dst->Buffer, src->Buffer, dst->MaximumLength );
416 *data = (char *)dst->Buffer + dst->MaximumLength;
417 }
418
419
420 /******************************************************************************
421 * RtlCreateProcessParameters [NTDLL.@]
422 */
423 NTSTATUS WINAPI RtlCreateProcessParameters( RTL_USER_PROCESS_PARAMETERS **result,
424 const UNICODE_STRING *ImagePathName,
425 const UNICODE_STRING *DllPath,
426 const UNICODE_STRING *CurrentDirectoryName,
427 const UNICODE_STRING *CommandLine,
428 PWSTR Environment,
429 const UNICODE_STRING *WindowTitle,
430 const UNICODE_STRING *Desktop,
431 const UNICODE_STRING *ShellInfo,
432 const UNICODE_STRING *RuntimeInfo )
433 {
434 static WCHAR empty[] = {0};
435 static const UNICODE_STRING empty_str = { 0, sizeof(empty), empty };
436 static const UNICODE_STRING null_str = { 0, 0, NULL };
437
438 const RTL_USER_PROCESS_PARAMETERS *cur_params;
439 SIZE_T size, total_size;
440 void *ptr;
441 NTSTATUS status;
442
443 RtlAcquirePebLock();
444 cur_params = NtCurrentTeb()->Peb->ProcessParameters;
445 if (!DllPath) DllPath = &cur_params->DllPath;
446 if (!CurrentDirectoryName)
447 {
448 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
449 CurrentDirectoryName = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
450 else
451 CurrentDirectoryName = &cur_params->CurrentDirectory.DosPath;
452 }
453 if (!CommandLine) CommandLine = ImagePathName;
454 if (!Environment) Environment = cur_params->Environment;
455 if (!WindowTitle) WindowTitle = &empty_str;
456 if (!Desktop) Desktop = &empty_str;
457 if (!ShellInfo) ShellInfo = &empty_str;
458 if (!RuntimeInfo) RuntimeInfo = &null_str;
459
460 size = (sizeof(RTL_USER_PROCESS_PARAMETERS)
461 + ImagePathName->MaximumLength
462 + DllPath->MaximumLength
463 + CurrentDirectoryName->MaximumLength
464 + CommandLine->MaximumLength
465 + WindowTitle->MaximumLength
466 + Desktop->MaximumLength
467 + ShellInfo->MaximumLength
468 + RuntimeInfo->MaximumLength);
469
470 total_size = size;
471 ptr = NULL;
472 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &total_size,
473 MEM_COMMIT, PAGE_READWRITE )) == STATUS_SUCCESS)
474 {
475 RTL_USER_PROCESS_PARAMETERS *params = ptr;
476 params->AllocationSize = total_size;
477 params->Size = size;
478 params->Flags = PROCESS_PARAMS_FLAG_NORMALIZED;
479 params->ConsoleFlags = cur_params->ConsoleFlags;
480 params->Environment = Environment;
481 /* all other fields are zero */
482
483 ptr = params + 1;
484 append_unicode_string( &ptr, CurrentDirectoryName, ¶ms->CurrentDirectory.DosPath );
485 append_unicode_string( &ptr, DllPath, ¶ms->DllPath );
486 append_unicode_string( &ptr, ImagePathName, ¶ms->ImagePathName );
487 append_unicode_string( &ptr, CommandLine, ¶ms->CommandLine );
488 append_unicode_string( &ptr, WindowTitle, ¶ms->WindowTitle );
489 append_unicode_string( &ptr, Desktop, ¶ms->Desktop );
490 append_unicode_string( &ptr, ShellInfo, ¶ms->ShellInfo );
491 append_unicode_string( &ptr, RuntimeInfo, ¶ms->RuntimeInfo );
492 *result = RtlDeNormalizeProcessParams( params );
493 }
494 RtlReleasePebLock();
495 return status;
496 }
497
498
499 /******************************************************************************
500 * RtlDestroyProcessParameters [NTDLL.@]
501 */
502 void WINAPI RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS *params )
503 {
504 void *ptr = params;
505 SIZE_T size = 0;
506 NtFreeVirtualMemory( NtCurrentProcess(), &ptr, &size, MEM_RELEASE );
507 }
508
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.