2003-05-08 05:47:00 +02:00
|
|
|
/*
|
|
|
|
* Ntdll environment functions
|
|
|
|
*
|
|
|
|
* Copyright 1996, 1998 Alexandre Julliard
|
|
|
|
* Copyright 2003 Eric Pouech
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-05-08 05:47:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2019-10-22 09:58:32 +02:00
|
|
|
#include <sys/types.h>
|
2003-05-08 05:47:00 +02:00
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2003-05-08 05:47:00 +02:00
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "ntdll_misc.h"
|
2009-10-03 22:41:03 +02:00
|
|
|
#include "winnt.h"
|
2003-05-08 05:47:00 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(environ);
|
|
|
|
|
2020-12-04 00:36:32 +01:00
|
|
|
static WCHAR empty[] = L"";
|
2019-02-13 12:48:36 +01:00
|
|
|
static const UNICODE_STRING empty_str = { 0, sizeof(empty), empty };
|
|
|
|
static const UNICODE_STRING null_str = { 0, 0, NULL };
|
|
|
|
|
2019-10-22 10:15:28 +02:00
|
|
|
static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
|
|
|
|
|
2019-09-09 12:54:43 +02:00
|
|
|
static inline SIZE_T get_env_length( const WCHAR *env )
|
|
|
|
{
|
|
|
|
const WCHAR *end = env;
|
2020-03-31 11:50:16 +02:00
|
|
|
while (*end) end += wcslen(end) + 1;
|
2019-09-09 12:54:43 +02:00
|
|
|
return end + 1 - env;
|
|
|
|
}
|
|
|
|
|
2019-10-22 10:04:51 +02:00
|
|
|
|
2019-11-05 11:34:55 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* set_wow64_environment
|
|
|
|
*
|
|
|
|
* Set the environment variables that change across 32/64/Wow64.
|
|
|
|
*/
|
|
|
|
static void set_wow64_environment( WCHAR **env )
|
|
|
|
{
|
2020-12-04 00:36:32 +01:00
|
|
|
static WCHAR archW[] = L"PROCESSOR_ARCHITECTURE";
|
|
|
|
static WCHAR arch6432W[] = L"PROCESSOR_ARCHITEW6432";
|
2019-11-05 11:34:55 +01:00
|
|
|
|
2020-04-16 15:15:07 +02:00
|
|
|
WCHAR buf[256];
|
2019-11-05 11:34:55 +01:00
|
|
|
UNICODE_STRING arch_strW = { sizeof(archW) - sizeof(WCHAR), sizeof(archW), archW };
|
|
|
|
UNICODE_STRING arch6432_strW = { sizeof(arch6432W) - sizeof(WCHAR), sizeof(arch6432W), arch6432W };
|
|
|
|
UNICODE_STRING valW = { 0, sizeof(buf), buf };
|
|
|
|
UNICODE_STRING nameW;
|
2020-04-16 15:15:07 +02:00
|
|
|
|
2019-11-05 11:34:55 +01:00
|
|
|
/* set the PROCESSOR_ARCHITECTURE variable */
|
|
|
|
|
|
|
|
if (!RtlQueryEnvironmentVariable_U( *env, &arch6432_strW, &valW ))
|
|
|
|
{
|
|
|
|
if (is_win64)
|
|
|
|
{
|
|
|
|
RtlSetEnvironmentVariable( env, &arch_strW, &valW );
|
|
|
|
RtlSetEnvironmentVariable( env, &arch6432_strW, NULL );
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 12:25:21 +02:00
|
|
|
else if (NtCurrentTeb64() && !RtlQueryEnvironmentVariable_U( *env, &arch_strW, &valW ))
|
2019-11-05 11:34:55 +01:00
|
|
|
{
|
2021-04-01 12:25:21 +02:00
|
|
|
RtlSetEnvironmentVariable( env, &arch6432_strW, &valW );
|
|
|
|
RtlInitUnicodeString( &nameW, L"x86" );
|
|
|
|
RtlSetEnvironmentVariable( env, &arch_strW, &nameW );
|
2019-11-05 11:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the ProgramFiles variables */
|
|
|
|
|
2021-03-24 10:43:31 +01:00
|
|
|
RtlInitUnicodeString( &nameW, is_win64 ? L"ProgramW6432" : L"ProgramFiles(x86)" );
|
|
|
|
if (!RtlQueryEnvironmentVariable_U( *env, &nameW, &valW ))
|
2019-11-05 11:34:55 +01:00
|
|
|
{
|
2021-03-24 10:43:31 +01:00
|
|
|
RtlInitUnicodeString( &nameW, L"ProgramFiles" );
|
|
|
|
RtlSetEnvironmentVariable( env, &nameW, &valW );
|
2019-11-05 11:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the CommonProgramFiles variables */
|
|
|
|
|
2021-03-24 10:43:31 +01:00
|
|
|
RtlInitUnicodeString( &nameW, is_win64 ? L"CommonProgramW6432" : L"CommonProgramFiles(x86)" );
|
|
|
|
if (!RtlQueryEnvironmentVariable_U( *env, &nameW, &valW ))
|
2019-11-05 11:34:55 +01:00
|
|
|
{
|
2021-03-24 10:43:31 +01:00
|
|
|
RtlInitUnicodeString( &nameW, L"CommonProgramFiles" );
|
|
|
|
RtlSetEnvironmentVariable( env, &nameW, &valW );
|
2019-11-05 11:34:55 +01:00
|
|
|
}
|
2021-03-24 10:43:31 +01:00
|
|
|
|
|
|
|
RtlReAllocateHeap( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, *env,
|
|
|
|
get_env_length(*env) * sizeof(WCHAR) );
|
2019-11-05 11:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-08 05:47:00 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* RtlCreateEnvironment [NTDLL.@]
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
|
|
|
|
{
|
2019-09-09 12:54:43 +02:00
|
|
|
SIZE_T size;
|
2003-05-08 05:47:00 +02:00
|
|
|
|
|
|
|
TRACE("(%u,%p)!\n", inherit, env);
|
|
|
|
|
|
|
|
if (inherit)
|
|
|
|
{
|
|
|
|
RtlAcquirePebLock();
|
2019-09-09 12:54:43 +02:00
|
|
|
size = get_env_length( NtCurrentTeb()->Peb->ProcessParameters->Environment ) * sizeof(WCHAR);
|
|
|
|
if ((*env = RtlAllocateHeap( GetProcessHeap(), 0, size )))
|
|
|
|
memcpy( *env, NtCurrentTeb()->Peb->ProcessParameters->Environment, size );
|
2003-05-08 05:47:00 +02:00
|
|
|
RtlReleasePebLock();
|
|
|
|
}
|
2019-09-09 12:54:43 +02:00
|
|
|
else *env = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR) );
|
2003-05-08 05:47:00 +02:00
|
|
|
|
2019-09-09 12:54:43 +02:00
|
|
|
return *env ? STATUS_SUCCESS : STATUS_NO_MEMORY;
|
2003-05-08 05:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* RtlDestroyEnvironment [NTDLL.@]
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
|
|
|
|
{
|
2019-09-09 12:54:43 +02:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, env );
|
|
|
|
return STATUS_SUCCESS;
|
2003-05-08 05:47:00 +02:00
|
|
|
}
|
|
|
|
|
2003-05-13 06:45:50 +02:00
|
|
|
static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
|
|
|
|
{
|
2020-03-27 13:39:44 +01:00
|
|
|
while (*var)
|
2003-05-13 06:45:50 +02:00
|
|
|
{
|
|
|
|
/* match var names, but avoid setting a var with a name including a '='
|
|
|
|
* (a starting '=' is valid though)
|
|
|
|
*/
|
2020-03-31 11:50:16 +02:00
|
|
|
unsigned int len = wcslen( var );
|
2020-03-27 13:39:44 +01:00
|
|
|
if (len > namelen &&
|
|
|
|
var[namelen] == '=' &&
|
|
|
|
!RtlCompareUnicodeStrings( var, namelen, name, namelen, TRUE ) &&
|
2020-03-31 11:43:50 +02:00
|
|
|
wcschr(var + 1, '=') == var + namelen)
|
2003-05-13 06:45:50 +02:00
|
|
|
{
|
|
|
|
return var + namelen + 1;
|
|
|
|
}
|
2020-03-27 13:39:44 +01:00
|
|
|
var += len + 1;
|
2003-05-13 06:45:50 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-05-08 05:47:00 +02:00
|
|
|
/******************************************************************
|
|
|
|
* RtlQueryEnvironmentVariable_U [NTDLL.@]
|
|
|
|
*
|
2003-05-13 06:45:50 +02:00
|
|
|
* NOTES: when the buffer is too small, the string is not written, but if the
|
|
|
|
* terminating null char is the only char that cannot be written, then
|
|
|
|
* all chars (except the null) are written and success is returned
|
|
|
|
* (behavior of Win2k at least)
|
2003-05-08 05:47:00 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
|
|
|
|
PUNICODE_STRING name,
|
|
|
|
PUNICODE_STRING value)
|
|
|
|
{
|
|
|
|
NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
|
2003-05-13 06:45:50 +02:00
|
|
|
PCWSTR var;
|
|
|
|
unsigned namelen;
|
2003-05-08 05:47:00 +02:00
|
|
|
|
2005-07-10 19:44:37 +02:00
|
|
|
TRACE("%p %s %p\n", env, debugstr_us(name), value);
|
2003-05-08 05:47:00 +02:00
|
|
|
|
|
|
|
value->Length = 0;
|
|
|
|
namelen = name->Length / sizeof(WCHAR);
|
|
|
|
if (!namelen) return nts;
|
|
|
|
|
|
|
|
if (!env)
|
|
|
|
{
|
|
|
|
RtlAcquirePebLock();
|
2004-03-12 02:59:35 +01:00
|
|
|
var = NtCurrentTeb()->Peb->ProcessParameters->Environment;
|
2003-05-08 05:47:00 +02:00
|
|
|
}
|
|
|
|
else var = env;
|
|
|
|
|
2003-05-13 06:45:50 +02:00
|
|
|
var = ENV_FindVariable(var, name->Buffer, namelen);
|
|
|
|
if (var != NULL)
|
2003-05-08 05:47:00 +02:00
|
|
|
{
|
2020-03-31 11:50:16 +02:00
|
|
|
value->Length = wcslen(var) * sizeof(WCHAR);
|
2003-06-18 05:23:22 +02:00
|
|
|
|
2003-05-13 06:45:50 +02:00
|
|
|
if (value->Length <= value->MaximumLength)
|
2003-05-08 05:47:00 +02:00
|
|
|
{
|
2003-05-13 06:45:50 +02:00
|
|
|
memmove(value->Buffer, var, min(value->Length + sizeof(WCHAR), value->MaximumLength));
|
|
|
|
nts = STATUS_SUCCESS;
|
2003-05-08 05:47:00 +02:00
|
|
|
}
|
2003-05-13 06:45:50 +02:00
|
|
|
else nts = STATUS_BUFFER_TOO_SMALL;
|
2003-05-08 05:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!env) RtlReleasePebLock();
|
|
|
|
|
|
|
|
return nts;
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:59:53 +02:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* RtlQueryEnvironmentVariable [NTDLL.@]
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlQueryEnvironmentVariable( WCHAR *env, const WCHAR *name, SIZE_T namelen,
|
|
|
|
WCHAR *value, SIZE_T value_length, SIZE_T *return_length )
|
|
|
|
{
|
|
|
|
NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
|
|
|
|
SIZE_T len = 0;
|
|
|
|
const WCHAR *var;
|
|
|
|
|
|
|
|
if (!namelen) return nts;
|
|
|
|
|
|
|
|
if (!env)
|
|
|
|
{
|
|
|
|
RtlAcquirePebLock();
|
|
|
|
var = NtCurrentTeb()->Peb->ProcessParameters->Environment;
|
|
|
|
}
|
|
|
|
else var = env;
|
|
|
|
|
|
|
|
var = ENV_FindVariable(var, name, namelen);
|
|
|
|
if (var != NULL)
|
|
|
|
{
|
|
|
|
len = wcslen(var);
|
|
|
|
if (len <= value_length)
|
|
|
|
{
|
|
|
|
memcpy(value, var, min(len + 1, value_length) * sizeof(WCHAR));
|
|
|
|
nts = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
nts = STATUS_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*return_length = len;
|
|
|
|
|
|
|
|
if (!env) RtlReleasePebLock();
|
|
|
|
|
|
|
|
return nts;
|
|
|
|
}
|
|
|
|
|
2003-05-08 05:47:00 +02:00
|
|
|
/******************************************************************
|
|
|
|
* RtlSetCurrentEnvironment [NTDLL.@]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
|
|
|
|
{
|
2020-05-06 16:40:27 +02:00
|
|
|
WCHAR *prev;
|
|
|
|
|
2003-05-08 05:47:00 +02:00
|
|
|
TRACE("(%p %p)\n", new_env, old_env);
|
|
|
|
|
|
|
|
RtlAcquirePebLock();
|
|
|
|
|
2020-05-06 16:40:27 +02:00
|
|
|
prev = NtCurrentTeb()->Peb->ProcessParameters->Environment;
|
2004-03-12 02:59:35 +01:00
|
|
|
NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
|
2021-01-13 15:23:28 +01:00
|
|
|
NtCurrentTeb()->Peb->ProcessParameters->EnvironmentSize = RtlSizeHeap( GetProcessHeap(), 0, new_env );
|
2003-05-08 05:47:00 +02:00
|
|
|
|
|
|
|
RtlReleasePebLock();
|
2020-05-06 16:40:27 +02:00
|
|
|
|
|
|
|
if (old_env)
|
|
|
|
*old_env = prev;
|
|
|
|
else
|
|
|
|
RtlDestroyEnvironment( prev );
|
2003-05-08 05:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* RtlSetEnvironmentVariable [NTDLL.@]
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
|
|
|
|
PUNICODE_STRING value)
|
|
|
|
{
|
2020-03-27 13:39:44 +01:00
|
|
|
INT varlen, len, old_size;
|
2003-05-08 05:47:00 +02:00
|
|
|
LPWSTR p, env;
|
2021-04-21 20:21:36 +02:00
|
|
|
NTSTATUS nts = STATUS_SUCCESS;
|
2003-05-08 05:47:00 +02:00
|
|
|
|
2005-07-10 19:44:37 +02:00
|
|
|
TRACE("(%p, %s, %s)\n", penv, debugstr_us(name), debugstr_us(value));
|
2003-05-08 05:47:00 +02:00
|
|
|
|
2004-05-05 03:39:12 +02:00
|
|
|
if (!name || !name->Buffer || !name->Length)
|
2003-05-08 05:47:00 +02:00
|
|
|
return STATUS_INVALID_PARAMETER_1;
|
2004-05-05 03:39:12 +02:00
|
|
|
|
|
|
|
len = name->Length / sizeof(WCHAR);
|
|
|
|
|
2003-05-08 05:47:00 +02:00
|
|
|
/* variable names can't contain a '=' except as a first character */
|
2004-05-05 03:39:12 +02:00
|
|
|
for (p = name->Buffer + 1; p < name->Buffer + len; p++)
|
|
|
|
if (*p == '=') return STATUS_INVALID_PARAMETER;
|
2003-05-08 05:47:00 +02:00
|
|
|
|
|
|
|
if (!penv)
|
|
|
|
{
|
|
|
|
RtlAcquirePebLock();
|
2004-03-12 02:59:35 +01:00
|
|
|
env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
|
2003-05-08 05:47:00 +02:00
|
|
|
} else env = *penv;
|
|
|
|
|
2019-09-09 12:54:43 +02:00
|
|
|
old_size = get_env_length( env );
|
2003-05-08 05:47:00 +02:00
|
|
|
|
|
|
|
/* Find a place to insert the string */
|
2020-03-27 13:39:44 +01:00
|
|
|
for (p = env; *p; p += varlen + 1)
|
2003-05-08 05:47:00 +02:00
|
|
|
{
|
2020-03-31 11:50:16 +02:00
|
|
|
varlen = wcslen(p);
|
2020-03-27 13:39:44 +01:00
|
|
|
if (varlen > len && p[len] == '=' &&
|
|
|
|
!RtlCompareUnicodeStrings( name->Buffer, len, p, len, TRUE )) break;
|
2003-05-08 05:47:00 +02:00
|
|
|
}
|
|
|
|
if (!value && !*p) goto done; /* Value to remove doesn't exist */
|
|
|
|
|
|
|
|
/* Realloc the buffer */
|
|
|
|
len = value ? len + value->Length / sizeof(WCHAR) + 2 : 0;
|
2020-03-31 11:50:16 +02:00
|
|
|
if (*p) len -= wcslen(p) + 1; /* The name already exists */
|
2003-05-08 05:47:00 +02:00
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
{
|
2020-03-31 11:50:16 +02:00
|
|
|
LPWSTR next = p + wcslen(p) + 1; /* We know there is a next one */
|
2003-05-08 05:47:00 +02:00
|
|
|
memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
|
|
|
|
}
|
|
|
|
|
2019-09-09 12:54:43 +02:00
|
|
|
if ((old_size + len) * sizeof(WCHAR) > RtlSizeHeap( GetProcessHeap(), 0, env ))
|
2003-05-08 05:47:00 +02:00
|
|
|
{
|
2021-01-13 15:23:28 +01:00
|
|
|
SIZE_T new_size = (old_size + len) * sizeof(WCHAR);
|
2019-09-09 12:54:43 +02:00
|
|
|
LPWSTR new_env = RtlAllocateHeap( GetProcessHeap(), 0, new_size );
|
2003-05-08 05:47:00 +02:00
|
|
|
|
2019-09-09 12:54:43 +02:00
|
|
|
if (!new_env)
|
|
|
|
{
|
|
|
|
nts = STATUS_NO_MEMORY;
|
|
|
|
goto done;
|
|
|
|
}
|
2021-01-13 15:23:28 +01:00
|
|
|
memcpy(new_env, env, (p - env) * sizeof(WCHAR));
|
|
|
|
memcpy(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
|
2003-05-08 05:47:00 +02:00
|
|
|
p = new_env + (p - env);
|
|
|
|
|
|
|
|
RtlDestroyEnvironment(env);
|
2021-01-13 15:23:28 +01:00
|
|
|
if (!penv)
|
|
|
|
{
|
|
|
|
NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
|
|
|
|
NtCurrentTeb()->Peb->ProcessParameters->EnvironmentSize = new_size;
|
|
|
|
}
|
2003-05-08 05:47:00 +02:00
|
|
|
else *penv = new_env;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the new string */
|
|
|
|
if (value)
|
|
|
|
{
|
2004-05-05 03:39:12 +02:00
|
|
|
memcpy( p, name->Buffer, name->Length );
|
|
|
|
p += name->Length / sizeof(WCHAR);
|
|
|
|
*p++ = '=';
|
|
|
|
memcpy( p, value->Buffer, value->Length );
|
|
|
|
p[value->Length / sizeof(WCHAR)] = 0;
|
2003-05-08 05:47:00 +02:00
|
|
|
}
|
2019-09-09 12:54:43 +02:00
|
|
|
|
2003-05-08 05:47:00 +02:00
|
|
|
done:
|
|
|
|
if (!penv) RtlReleasePebLock();
|
|
|
|
|
|
|
|
return nts;
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:05:31 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* RtlExpandEnvironmentStrings (NTDLL.@)
|
2003-05-13 06:45:50 +02:00
|
|
|
*/
|
2018-06-21 12:05:31 +02:00
|
|
|
NTSTATUS WINAPI RtlExpandEnvironmentStrings( const WCHAR *renv, WCHAR *src, SIZE_T src_len,
|
|
|
|
WCHAR *dst, SIZE_T count, SIZE_T *plen )
|
2003-05-13 06:45:50 +02:00
|
|
|
{
|
2018-06-21 12:05:31 +02:00
|
|
|
SIZE_T len, total_size = 1; /* 1 for terminating '\0' */
|
2020-03-30 12:09:44 +02:00
|
|
|
LPCWSTR env, var;
|
2003-05-13 06:45:50 +02:00
|
|
|
|
2003-06-18 05:23:22 +02:00
|
|
|
if (!renv)
|
|
|
|
{
|
|
|
|
RtlAcquirePebLock();
|
2004-03-12 02:59:35 +01:00
|
|
|
env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
|
2003-06-18 05:23:22 +02:00
|
|
|
}
|
|
|
|
else env = renv;
|
2003-05-13 06:45:50 +02:00
|
|
|
|
2004-11-29 19:24:08 +01:00
|
|
|
while (src_len)
|
2003-05-13 06:45:50 +02:00
|
|
|
{
|
|
|
|
if (*src != '%')
|
|
|
|
{
|
2020-03-30 12:09:44 +02:00
|
|
|
for (len = 0; len < src_len; len++) if (src[len] == '%') break;
|
2003-05-13 06:45:50 +02:00
|
|
|
var = src;
|
|
|
|
src += len;
|
2004-11-29 19:24:08 +01:00
|
|
|
src_len -= len;
|
2003-05-13 06:45:50 +02:00
|
|
|
}
|
|
|
|
else /* we are at the start of a variable */
|
|
|
|
{
|
2020-03-30 12:09:44 +02:00
|
|
|
for (len = 1; len < src_len; len++) if (src[len] == '%') break;
|
|
|
|
if (len < src_len)
|
2003-05-13 06:45:50 +02:00
|
|
|
{
|
2020-03-30 12:09:44 +02:00
|
|
|
if ((var = ENV_FindVariable( env, src + 1, len - 1 )))
|
2003-05-13 06:45:50 +02:00
|
|
|
{
|
2020-03-30 12:09:44 +02:00
|
|
|
src += len + 1; /* Skip the variable name */
|
|
|
|
src_len -= len + 1;
|
2020-03-31 11:50:16 +02:00
|
|
|
len = wcslen(var);
|
2003-05-13 06:45:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var = src; /* Copy original name instead */
|
2020-03-30 12:09:44 +02:00
|
|
|
len++;
|
2003-05-13 06:45:50 +02:00
|
|
|
src += len;
|
2004-11-29 19:24:08 +01:00
|
|
|
src_len -= len;
|
2003-05-13 06:45:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* unfinished variable name, ignore it */
|
|
|
|
{
|
|
|
|
var = src;
|
|
|
|
src += len;
|
2004-11-29 19:24:08 +01:00
|
|
|
src_len = 0;
|
2003-05-13 06:45:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
total_size += len;
|
|
|
|
if (dst)
|
|
|
|
{
|
|
|
|
if (count < len) len = count;
|
|
|
|
memcpy(dst, var, len * sizeof(WCHAR));
|
|
|
|
count -= len;
|
|
|
|
dst += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-18 05:23:22 +02:00
|
|
|
if (!renv) RtlReleasePebLock();
|
2003-05-13 06:45:50 +02:00
|
|
|
|
|
|
|
if (dst && count) *dst = '\0';
|
2018-06-21 12:05:31 +02:00
|
|
|
if (plen) *plen = total_size;
|
2003-05-13 06:45:50 +02:00
|
|
|
|
|
|
|
return (count) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:05:31 +02:00
|
|
|
/******************************************************************
|
|
|
|
* RtlExpandEnvironmentStrings_U (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlExpandEnvironmentStrings_U( const WCHAR *env, const UNICODE_STRING *src,
|
|
|
|
UNICODE_STRING *dst, ULONG *plen )
|
|
|
|
{
|
|
|
|
SIZE_T len;
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
ret = RtlExpandEnvironmentStrings( env, src->Buffer, src->Length / sizeof(WCHAR),
|
|
|
|
dst->Buffer, dst->MaximumLength / sizeof(WCHAR), &len );
|
|
|
|
if (plen) *plen = len * sizeof(WCHAR); /* FIXME: check for overflow? */
|
|
|
|
if (len > UNICODE_STRING_MAX_CHARS) ret = STATUS_BUFFER_TOO_SMALL;
|
|
|
|
if (!ret) dst->Length = (len - 1) * sizeof(WCHAR);
|
|
|
|
return ret;
|
|
|
|
}
|
2003-10-04 05:11:57 +02:00
|
|
|
|
|
|
|
static inline void normalize( void *base, WCHAR **ptr )
|
|
|
|
{
|
|
|
|
if (*ptr) *ptr = (WCHAR *)((char *)base + (UINT_PTR)*ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* RtlNormalizeProcessParams [NTDLL.@]
|
|
|
|
*/
|
|
|
|
PRTL_USER_PROCESS_PARAMETERS WINAPI RtlNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
|
|
|
|
{
|
|
|
|
if (params && !(params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
|
|
|
|
{
|
2004-04-16 07:00:46 +02:00
|
|
|
normalize( params, ¶ms->CurrentDirectory.DosPath.Buffer );
|
2003-10-04 05:11:57 +02:00
|
|
|
normalize( params, ¶ms->DllPath.Buffer );
|
|
|
|
normalize( params, ¶ms->ImagePathName.Buffer );
|
|
|
|
normalize( params, ¶ms->CommandLine.Buffer );
|
|
|
|
normalize( params, ¶ms->WindowTitle.Buffer );
|
|
|
|
normalize( params, ¶ms->Desktop.Buffer );
|
|
|
|
normalize( params, ¶ms->ShellInfo.Buffer );
|
|
|
|
normalize( params, ¶ms->RuntimeInfo.Buffer );
|
|
|
|
params->Flags |= PROCESS_PARAMS_FLAG_NORMALIZED;
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-22 20:48:12 +02:00
|
|
|
static inline void denormalize( const void *base, WCHAR **ptr )
|
2003-10-04 05:11:57 +02:00
|
|
|
{
|
2007-06-22 20:48:12 +02:00
|
|
|
if (*ptr) *ptr = (WCHAR *)(UINT_PTR)((char *)*ptr - (const char *)base);
|
2003-10-04 05:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* RtlDeNormalizeProcessParams [NTDLL.@]
|
|
|
|
*/
|
|
|
|
PRTL_USER_PROCESS_PARAMETERS WINAPI RtlDeNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
|
|
|
|
{
|
|
|
|
if (params && (params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
|
|
|
|
{
|
2004-04-16 07:00:46 +02:00
|
|
|
denormalize( params, ¶ms->CurrentDirectory.DosPath.Buffer );
|
2003-10-04 05:11:57 +02:00
|
|
|
denormalize( params, ¶ms->DllPath.Buffer );
|
|
|
|
denormalize( params, ¶ms->ImagePathName.Buffer );
|
|
|
|
denormalize( params, ¶ms->CommandLine.Buffer );
|
|
|
|
denormalize( params, ¶ms->WindowTitle.Buffer );
|
|
|
|
denormalize( params, ¶ms->Desktop.Buffer );
|
|
|
|
denormalize( params, ¶ms->ShellInfo.Buffer );
|
|
|
|
denormalize( params, ¶ms->RuntimeInfo.Buffer );
|
|
|
|
params->Flags &= ~PROCESS_PARAMS_FLAG_NORMALIZED;
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-16 20:45:10 +02:00
|
|
|
#define ROUND_SIZE(size) (((size) + sizeof(void *) - 1) & ~(sizeof(void *) - 1))
|
|
|
|
|
2003-10-04 05:11:57 +02:00
|
|
|
/* append a unicode string to the process params data; helper for RtlCreateProcessParameters */
|
|
|
|
static void append_unicode_string( void **data, const UNICODE_STRING *src,
|
|
|
|
UNICODE_STRING *dst )
|
|
|
|
{
|
|
|
|
dst->Length = src->Length;
|
|
|
|
dst->MaximumLength = src->MaximumLength;
|
2018-10-16 20:45:10 +02:00
|
|
|
if (dst->MaximumLength)
|
|
|
|
{
|
|
|
|
dst->Buffer = *data;
|
|
|
|
memcpy( dst->Buffer, src->Buffer, dst->Length );
|
|
|
|
*data = (char *)dst->Buffer + ROUND_SIZE( dst->MaximumLength );
|
|
|
|
}
|
|
|
|
else dst->Buffer = NULL;
|
2003-10-04 05:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2018-10-08 18:41:50 +02:00
|
|
|
* RtlCreateProcessParametersEx [NTDLL.@]
|
2003-10-04 05:11:57 +02:00
|
|
|
*/
|
2018-10-08 18:41:50 +02:00
|
|
|
NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **result,
|
|
|
|
const UNICODE_STRING *ImagePathName,
|
|
|
|
const UNICODE_STRING *DllPath,
|
|
|
|
const UNICODE_STRING *CurrentDirectoryName,
|
|
|
|
const UNICODE_STRING *CommandLine,
|
|
|
|
PWSTR Environment,
|
|
|
|
const UNICODE_STRING *WindowTitle,
|
|
|
|
const UNICODE_STRING *Desktop,
|
|
|
|
const UNICODE_STRING *ShellInfo,
|
|
|
|
const UNICODE_STRING *RuntimeInfo,
|
|
|
|
ULONG flags )
|
2003-10-04 05:11:57 +02:00
|
|
|
{
|
2018-10-16 20:43:42 +02:00
|
|
|
UNICODE_STRING curdir;
|
2003-10-04 05:11:57 +02:00
|
|
|
const RTL_USER_PROCESS_PARAMETERS *cur_params;
|
2019-02-12 11:13:57 +01:00
|
|
|
SIZE_T size, env_size = 0;
|
2003-10-04 05:11:57 +02:00
|
|
|
void *ptr;
|
2018-10-16 20:45:31 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2003-10-04 05:11:57 +02:00
|
|
|
|
|
|
|
RtlAcquirePebLock();
|
|
|
|
cur_params = NtCurrentTeb()->Peb->ProcessParameters;
|
2021-03-24 10:54:05 +01:00
|
|
|
if (!DllPath) DllPath = &null_str;
|
2007-07-12 07:27:25 +02:00
|
|
|
if (!CurrentDirectoryName)
|
|
|
|
{
|
|
|
|
if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
|
2018-10-16 20:43:42 +02:00
|
|
|
curdir = ((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
|
2007-07-12 07:27:25 +02:00
|
|
|
else
|
2018-10-16 20:43:42 +02:00
|
|
|
curdir = cur_params->CurrentDirectory.DosPath;
|
2007-07-12 07:27:25 +02:00
|
|
|
}
|
2018-10-16 20:43:42 +02:00
|
|
|
else curdir = *CurrentDirectoryName;
|
|
|
|
curdir.MaximumLength = MAX_PATH * sizeof(WCHAR);
|
|
|
|
|
2003-10-04 05:11:57 +02:00
|
|
|
if (!CommandLine) CommandLine = ImagePathName;
|
2019-02-13 12:48:36 +01:00
|
|
|
if (!Environment && cur_params) Environment = cur_params->Environment;
|
2003-10-04 05:11:57 +02:00
|
|
|
if (!WindowTitle) WindowTitle = &empty_str;
|
|
|
|
if (!Desktop) Desktop = &empty_str;
|
|
|
|
if (!ShellInfo) ShellInfo = &empty_str;
|
2004-10-25 23:47:23 +02:00
|
|
|
if (!RuntimeInfo) RuntimeInfo = &null_str;
|
2003-10-04 05:11:57 +02:00
|
|
|
|
2019-09-09 12:54:43 +02:00
|
|
|
if (Environment) env_size = get_env_length( Environment ) * sizeof(WCHAR);
|
2018-10-16 15:15:27 +02:00
|
|
|
|
2003-10-04 05:11:57 +02:00
|
|
|
size = (sizeof(RTL_USER_PROCESS_PARAMETERS)
|
2018-10-16 20:45:10 +02:00
|
|
|
+ ROUND_SIZE( ImagePathName->MaximumLength )
|
|
|
|
+ ROUND_SIZE( DllPath->MaximumLength )
|
|
|
|
+ ROUND_SIZE( curdir.MaximumLength )
|
|
|
|
+ ROUND_SIZE( CommandLine->MaximumLength )
|
|
|
|
+ ROUND_SIZE( WindowTitle->MaximumLength )
|
|
|
|
+ ROUND_SIZE( Desktop->MaximumLength )
|
|
|
|
+ ROUND_SIZE( ShellInfo->MaximumLength )
|
|
|
|
+ ROUND_SIZE( RuntimeInfo->MaximumLength ));
|
2003-10-04 05:11:57 +02:00
|
|
|
|
2021-03-08 09:14:19 +01:00
|
|
|
if ((ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, size + ROUND_SIZE( env_size ) )))
|
2003-10-04 05:11:57 +02:00
|
|
|
{
|
|
|
|
RTL_USER_PROCESS_PARAMETERS *params = ptr;
|
2021-01-13 15:23:28 +01:00
|
|
|
params->AllocationSize = size;
|
|
|
|
params->Size = size;
|
|
|
|
params->Flags = PROCESS_PARAMS_FLAG_NORMALIZED;
|
2021-03-08 09:14:19 +01:00
|
|
|
params->EnvironmentSize = ROUND_SIZE( env_size );
|
2019-02-13 12:48:36 +01:00
|
|
|
if (cur_params) params->ConsoleFlags = cur_params->ConsoleFlags;
|
2003-10-04 05:11:57 +02:00
|
|
|
/* all other fields are zero */
|
|
|
|
|
|
|
|
ptr = params + 1;
|
2018-10-16 20:43:42 +02:00
|
|
|
append_unicode_string( &ptr, &curdir, ¶ms->CurrentDirectory.DosPath );
|
2003-10-04 05:11:57 +02:00
|
|
|
append_unicode_string( &ptr, DllPath, ¶ms->DllPath );
|
|
|
|
append_unicode_string( &ptr, ImagePathName, ¶ms->ImagePathName );
|
|
|
|
append_unicode_string( &ptr, CommandLine, ¶ms->CommandLine );
|
|
|
|
append_unicode_string( &ptr, WindowTitle, ¶ms->WindowTitle );
|
|
|
|
append_unicode_string( &ptr, Desktop, ¶ms->Desktop );
|
|
|
|
append_unicode_string( &ptr, ShellInfo, ¶ms->ShellInfo );
|
|
|
|
append_unicode_string( &ptr, RuntimeInfo, ¶ms->RuntimeInfo );
|
2019-09-09 12:54:43 +02:00
|
|
|
if (Environment) params->Environment = memcpy( ptr, Environment, env_size );
|
2018-10-08 18:41:50 +02:00
|
|
|
*result = params;
|
|
|
|
if (!(flags & PROCESS_PARAMS_FLAG_NORMALIZED)) RtlDeNormalizeProcessParams( params );
|
2003-10-04 05:11:57 +02:00
|
|
|
}
|
2018-10-16 20:45:31 +02:00
|
|
|
else status = STATUS_NO_MEMORY;
|
|
|
|
|
2003-10-04 05:11:57 +02:00
|
|
|
RtlReleasePebLock();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-08 18:41:50 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* RtlCreateProcessParameters [NTDLL.@]
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlCreateProcessParameters( RTL_USER_PROCESS_PARAMETERS **result,
|
|
|
|
const UNICODE_STRING *image,
|
|
|
|
const UNICODE_STRING *dllpath,
|
|
|
|
const UNICODE_STRING *curdir,
|
|
|
|
const UNICODE_STRING *cmdline,
|
|
|
|
PWSTR env,
|
|
|
|
const UNICODE_STRING *title,
|
|
|
|
const UNICODE_STRING *desktop,
|
|
|
|
const UNICODE_STRING *shellinfo,
|
|
|
|
const UNICODE_STRING *runtime )
|
|
|
|
{
|
|
|
|
return RtlCreateProcessParametersEx( result, image, dllpath, curdir, cmdline,
|
|
|
|
env, title, desktop, shellinfo, runtime, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-04 05:11:57 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* RtlDestroyProcessParameters [NTDLL.@]
|
|
|
|
*/
|
|
|
|
void WINAPI RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS *params )
|
|
|
|
{
|
2018-10-16 20:45:31 +02:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, params );
|
2003-10-04 05:11:57 +02:00
|
|
|
}
|
2019-02-12 11:13:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* init_user_process_params
|
|
|
|
*
|
|
|
|
* Fill the initial RTL_USER_PROCESS_PARAMETERS structure from the server.
|
|
|
|
*/
|
2020-06-29 12:10:58 +02:00
|
|
|
void init_user_process_params(void)
|
2019-02-12 11:13:57 +01:00
|
|
|
{
|
2021-03-24 10:54:05 +01:00
|
|
|
WCHAR *env;
|
2021-05-25 17:16:30 +02:00
|
|
|
SIZE_T size = 0, env_size;
|
2021-02-15 12:30:28 +01:00
|
|
|
RTL_USER_PROCESS_PARAMETERS *new_params, *params = NtCurrentTeb()->Peb->ProcessParameters;
|
2021-03-24 10:54:05 +01:00
|
|
|
UNICODE_STRING curdir;
|
2021-02-15 12:43:56 +01:00
|
|
|
|
|
|
|
/* environment needs to be a separate memory block */
|
|
|
|
env_size = params->EnvironmentSize;
|
|
|
|
if ((env = RtlAllocateHeap( GetProcessHeap(), 0, max( env_size, sizeof(WCHAR) ))))
|
|
|
|
{
|
|
|
|
if (env_size) memcpy( env, params->Environment, env_size );
|
|
|
|
else env[0] = 0;
|
|
|
|
}
|
2019-02-12 11:13:57 +01:00
|
|
|
|
2021-03-24 10:54:05 +01:00
|
|
|
params->Environment = NULL; /* avoid copying it */
|
|
|
|
if (RtlCreateProcessParametersEx( &new_params, ¶ms->ImagePathName, ¶ms->DllPath,
|
|
|
|
¶ms->CurrentDirectory.DosPath,
|
|
|
|
¶ms->CommandLine, NULL, ¶ms->WindowTitle, ¶ms->Desktop,
|
|
|
|
¶ms->ShellInfo, ¶ms->RuntimeInfo,
|
|
|
|
PROCESS_PARAMS_FLAG_NORMALIZED ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
new_params->Environment = env;
|
|
|
|
new_params->DebugFlags = params->DebugFlags;
|
|
|
|
new_params->ConsoleHandle = params->ConsoleHandle;
|
|
|
|
new_params->ConsoleFlags = params->ConsoleFlags;
|
|
|
|
new_params->hStdInput = params->hStdInput;
|
|
|
|
new_params->hStdOutput = params->hStdOutput;
|
|
|
|
new_params->hStdError = params->hStdError;
|
|
|
|
new_params->dwX = params->dwX;
|
|
|
|
new_params->dwY = params->dwY;
|
|
|
|
new_params->dwXSize = params->dwXSize;
|
|
|
|
new_params->dwYSize = params->dwYSize;
|
|
|
|
new_params->dwXCountChars = params->dwXCountChars;
|
|
|
|
new_params->dwYCountChars = params->dwYCountChars;
|
|
|
|
new_params->dwFillAttribute = params->dwFillAttribute;
|
|
|
|
new_params->dwFlags = params->dwFlags;
|
|
|
|
new_params->wShowWindow = params->wShowWindow;
|
|
|
|
|
2021-05-25 17:16:30 +02:00
|
|
|
NtCurrentTeb()->Peb->ProcessParameters = new_params;
|
|
|
|
NtFreeVirtualMemory( GetCurrentProcess(), (void **)¶ms, &size, MEM_RELEASE );
|
2019-02-13 12:48:36 +01:00
|
|
|
|
2021-05-25 17:16:30 +02:00
|
|
|
if (RtlSetCurrentDirectory_U( &new_params->CurrentDirectory.DosPath ))
|
2019-10-22 09:58:32 +02:00
|
|
|
{
|
|
|
|
MESSAGE("wine: could not open working directory %s, starting in the Windows directory.\n",
|
2021-05-25 17:16:30 +02:00
|
|
|
debugstr_w( new_params->CurrentDirectory.DosPath.Buffer ));
|
2019-10-22 09:58:32 +02:00
|
|
|
RtlInitUnicodeString( &curdir, windows_dir );
|
|
|
|
RtlSetCurrentDirectory_U( &curdir );
|
|
|
|
}
|
2021-05-25 17:16:30 +02:00
|
|
|
set_wow64_environment( &new_params->Environment );
|
|
|
|
new_params->EnvironmentSize = RtlSizeHeap( GetProcessHeap(), 0, new_params->Environment );
|
2019-02-12 11:13:57 +01:00
|
|
|
}
|