2009-01-21 11:30:02 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* win32env.c
|
2016-12-03 21:46:35 +01:00
|
|
|
* putenv() and unsetenv() for win32, which update both process environment
|
|
|
|
* and caches in (potentially multiple) C run-time library (CRT) versions.
|
2009-01-21 11:30:02 +01:00
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
2009-01-21 11:30:02 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/port/win32env.c
|
2009-01-21 11:30:02 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
pgwin32_putenv(const char *envval)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
char *envcpy;
|
|
|
|
char *cp;
|
|
|
|
typedef int (_cdecl * PUTENVPROC) (const char *);
|
Make pgwin32_putenv() follow DLL loading and unloading.
Until now, the first putenv() call of a given postgres.exe process would
cache the set of loaded CRTs. If a CRT unloaded after that call, the
next putenv() would crash. That risk was largely theoretical, because
the first putenv() precedes all PostgreSQL-initiated module loading.
However, this might explain bad interactions with antivirus and other
software that injects threads asynchronously. If an additional CRT
loaded after the first putenv(), pgwin32_putenv() would not discover it.
That CRT would have all environment changes predating its load, but it
would not receive later PostgreSQL-initiated changes. An additional CRT
loading concurrently with the first putenv() might miss that change in
addition to missing later changes. Fix all those problems. This
removes the cache mechanism from pgwin32_putenv(); the cost, less than
100 μs per backend startup, is negligible.
No resulting misbehavior was known to be user-visible given the core
distribution alone, but one can readily construct an affected extension
module. No back-patch given the lack of complaints and the potential
for behavior changes in non-PostgreSQL code running in the backend.
Christian Ullrich, reviewed by Michael Paquier.
2016-12-03 21:46:36 +01:00
|
|
|
static const char *const modulenames[] = {
|
|
|
|
"msvcrt", /* Visual Studio 6.0 / MinGW */
|
|
|
|
"msvcrtd",
|
|
|
|
"msvcr70", /* Visual Studio 2002 */
|
|
|
|
"msvcr70d",
|
|
|
|
"msvcr71", /* Visual Studio 2003 */
|
|
|
|
"msvcr71d",
|
|
|
|
"msvcr80", /* Visual Studio 2005 */
|
|
|
|
"msvcr80d",
|
|
|
|
"msvcr90", /* Visual Studio 2008 */
|
|
|
|
"msvcr90d",
|
|
|
|
"msvcr100", /* Visual Studio 2010 */
|
|
|
|
"msvcr100d",
|
|
|
|
"msvcr110", /* Visual Studio 2012 */
|
|
|
|
"msvcr110d",
|
|
|
|
"msvcr120", /* Visual Studio 2013 */
|
|
|
|
"msvcr120d",
|
|
|
|
"ucrtbase", /* Visual Studio 2015 and later */
|
|
|
|
"ucrtbased",
|
|
|
|
NULL
|
2010-01-01 15:57:16 +01:00
|
|
|
};
|
2010-02-26 03:01:40 +01:00
|
|
|
int i;
|
2009-01-21 11:30:02 +01:00
|
|
|
|
|
|
|
/*
|
2016-12-03 21:46:35 +01:00
|
|
|
* Update process environment, making this change visible to child
|
Make pgwin32_putenv() follow DLL loading and unloading.
Until now, the first putenv() call of a given postgres.exe process would
cache the set of loaded CRTs. If a CRT unloaded after that call, the
next putenv() would crash. That risk was largely theoretical, because
the first putenv() precedes all PostgreSQL-initiated module loading.
However, this might explain bad interactions with antivirus and other
software that injects threads asynchronously. If an additional CRT
loaded after the first putenv(), pgwin32_putenv() would not discover it.
That CRT would have all environment changes predating its load, but it
would not receive later PostgreSQL-initiated changes. An additional CRT
loading concurrently with the first putenv() might miss that change in
addition to missing later changes. Fix all those problems. This
removes the cache mechanism from pgwin32_putenv(); the cost, less than
100 μs per backend startup, is negligible.
No resulting misbehavior was known to be user-visible given the core
distribution alone, but one can readily construct an affected extension
module. No back-patch given the lack of complaints and the potential
for behavior changes in non-PostgreSQL code running in the backend.
Christian Ullrich, reviewed by Michael Paquier.
2016-12-03 21:46:36 +01:00
|
|
|
* processes and to CRTs initializing in the future. Do this before the
|
|
|
|
* _putenv() loop, for the benefit of any CRT that initializes during this
|
|
|
|
* pgwin32_putenv() execution, after the loop checks that CRT.
|
2009-01-21 11:30:02 +01:00
|
|
|
*
|
|
|
|
* Need a copy of the string so we can modify it.
|
|
|
|
*/
|
|
|
|
envcpy = strdup(envval);
|
2010-12-16 16:22:08 +01:00
|
|
|
if (!envcpy)
|
|
|
|
return -1;
|
2009-01-21 11:30:02 +01:00
|
|
|
cp = strchr(envcpy, '=');
|
|
|
|
if (cp == NULL)
|
2010-12-16 16:22:08 +01:00
|
|
|
{
|
|
|
|
free(envcpy);
|
2009-01-21 11:30:02 +01:00
|
|
|
return -1;
|
2010-12-16 16:22:08 +01:00
|
|
|
}
|
2009-01-21 11:30:02 +01:00
|
|
|
*cp = '\0';
|
|
|
|
cp++;
|
2009-02-12 13:53:34 +01:00
|
|
|
if (strlen(cp))
|
2009-01-21 11:30:02 +01:00
|
|
|
{
|
2009-02-12 13:53:34 +01:00
|
|
|
/*
|
|
|
|
* Only call SetEnvironmentVariable() when we are adding a variable,
|
2009-06-11 16:49:15 +02:00
|
|
|
* not when removing it. Calling it on both crashes on at least
|
2016-12-03 21:46:35 +01:00
|
|
|
* certain versions of MinGW.
|
2009-02-12 13:53:34 +01:00
|
|
|
*/
|
|
|
|
if (!SetEnvironmentVariable(envcpy, cp))
|
|
|
|
{
|
|
|
|
free(envcpy);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-21 11:30:02 +01:00
|
|
|
}
|
|
|
|
free(envcpy);
|
|
|
|
|
Make pgwin32_putenv() follow DLL loading and unloading.
Until now, the first putenv() call of a given postgres.exe process would
cache the set of loaded CRTs. If a CRT unloaded after that call, the
next putenv() would crash. That risk was largely theoretical, because
the first putenv() precedes all PostgreSQL-initiated module loading.
However, this might explain bad interactions with antivirus and other
software that injects threads asynchronously. If an additional CRT
loaded after the first putenv(), pgwin32_putenv() would not discover it.
That CRT would have all environment changes predating its load, but it
would not receive later PostgreSQL-initiated changes. An additional CRT
loading concurrently with the first putenv() might miss that change in
addition to missing later changes. Fix all those problems. This
removes the cache mechanism from pgwin32_putenv(); the cost, less than
100 μs per backend startup, is negligible.
No resulting misbehavior was known to be user-visible given the core
distribution alone, but one can readily construct an affected extension
module. No back-patch given the lack of complaints and the potential
for behavior changes in non-PostgreSQL code running in the backend.
Christian Ullrich, reviewed by Michael Paquier.
2016-12-03 21:46:36 +01:00
|
|
|
/*
|
|
|
|
* Each CRT has its own _putenv() symbol and copy of the environment.
|
|
|
|
* Update the environment in each CRT module currently loaded, so every
|
|
|
|
* third-party library sees this change regardless of the CRT it links
|
|
|
|
* against. Addresses within these modules may become invalid the moment
|
|
|
|
* we call FreeLibrary(), so don't cache them.
|
|
|
|
*/
|
|
|
|
for (i = 0; modulenames[i]; i++)
|
|
|
|
{
|
|
|
|
HMODULE hmodule = NULL;
|
|
|
|
BOOL res = GetModuleHandleEx(0, modulenames[i], &hmodule);
|
|
|
|
|
|
|
|
if (res != 0 && hmodule != NULL)
|
|
|
|
{
|
|
|
|
PUTENVPROC putenvFunc;
|
|
|
|
|
|
|
|
putenvFunc = (PUTENVPROC) GetProcAddress(hmodule, "_putenv");
|
|
|
|
if (putenvFunc)
|
|
|
|
putenvFunc(envval);
|
|
|
|
FreeLibrary(hmodule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 06:16:54 +01:00
|
|
|
/*
|
|
|
|
* Finally, update our "own" cache. This is redundant with the loop
|
|
|
|
* above, except when PostgreSQL itself links to a CRT not listed above.
|
|
|
|
* Ideally, the loop does visit all possible CRTs, making this redundant.
|
|
|
|
*/
|
2009-01-21 11:30:02 +01:00
|
|
|
return _putenv(envval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pgwin32_unsetenv(const char *name)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
char *envbuf;
|
2009-01-21 11:30:02 +01:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
envbuf = (char *) malloc(strlen(name) + 2);
|
2009-01-21 11:30:02 +01:00
|
|
|
if (!envbuf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sprintf(envbuf, "%s=", name);
|
|
|
|
pgwin32_putenv(envbuf);
|
|
|
|
free(envbuf);
|
|
|
|
}
|