Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 02037af

Browse files
committed
Add fallback implementation for setenv()
This fixes the code compilation on Windows with MSVC and Kerberos, as a missing implementation of setenv() causes a compilation failure of the GSSAPI code. This was only reproducible when building the code with Kerberos, something that buildfarm animal hamerkop has fixed recently. This issue only happens on 12 and 13, as this code has been introduced in b0b39f7. HEAD is already able to compile properly thanks to 7ca37fb, and this commit is a minimal cherry-pick of it. Thanks to Tom Lane for the discussion. Discussion: https://postgr.es/m/YLDtm5WGjPxm6ua4@paquier.xyz Backpatch-through: 12
1 parent 6f9e7f2 commit 02037af

File tree

8 files changed

+82
-3
lines changed

8 files changed

+82
-3
lines changed

configure

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16183,15 +16183,32 @@ case $host_os in
1618316183
# Unix sockets.
1618416184
mingw*)
1618516185

16186+
$as_echo "#define HAVE_SETENV 1" >>confdefs.h
16187+
16188+
1618616189
$as_echo "#define HAVE_UNSETENV 1" >>confdefs.h
1618716190

1618816191

1618916192
$as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h
1619016193

16194+
ac_cv_func_setenv=yes
1619116195
ac_cv_func_unsetenv=yes
1619216196
ac_cv_func_getpeereid=yes;;
1619316197
*)
16194-
ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
16198+
ac_fn_c_check_func "$LINENO" "setenv" "ac_cv_func_setenv"
16199+
if test "x$ac_cv_func_setenv" = xyes; then :
16200+
$as_echo "#define HAVE_SETENV 1" >>confdefs.h
16201+
16202+
else
16203+
case " $LIBOBJS " in
16204+
*" setenv.$ac_objext "* ) ;;
16205+
*) LIBOBJS="$LIBOBJS setenv.$ac_objext"
16206+
;;
16207+
esac
16208+
16209+
fi
16210+
16211+
ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
1619516212
if test "x$ac_cv_func_unsetenv" = xyes; then :
1619616213
$as_echo "#define HAVE_UNSETENV 1" >>confdefs.h
1619716214

configure.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,12 +1788,14 @@ case $host_os in
17881788
# and doesn't need a replacement getpeereid because it doesn't use
17891789
# Unix sockets.
17901790
mingw*)
1791+
AC_DEFINE(HAVE_SETENV, 1, [Define to 1 because replacement version used.])
17911792
AC_DEFINE(HAVE_UNSETENV, 1, [Define to 1 because replacement version used.])
17921793
AC_DEFINE(HAVE_GETPEEREID, 1, [Define to 1 because function not required.])
1794+
ac_cv_func_setenv=yes
17931795
ac_cv_func_unsetenv=yes
17941796
ac_cv_func_getpeereid=yes;;
17951797
*)
1796-
AC_REPLACE_FUNCS([unsetenv getpeereid])
1798+
AC_REPLACE_FUNCS([setenv unsetenv getpeereid])
17971799
;;
17981800
esac
17991801

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@
495495
/* Define to 1 if you have the <security/pam_appl.h> header file. */
496496
#undef HAVE_SECURITY_PAM_APPL_H
497497

498+
/* Define to 1 if you have the `setenv' function. */
499+
#undef HAVE_SETENV
500+
498501
/* Define to 1 if you have the `setproctitle' function. */
499502
#undef HAVE_SETPROCTITLE
500503

src/include/pg_config.h.win32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@
371371
/* Define to 1 if you have the <security/pam_appl.h> header file. */
372372
/* #undef HAVE_SECURITY_PAM_APPL_H */
373373

374+
/* Define to 1 if you have the `setenv' function. */
375+
#define HAVE_SETENV 1
376+
374377
/* Define to 1 if you have the `setproctitle' function. */
375378
/* #undef HAVE_SETPROCTITLE */
376379

src/include/port.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ extern size_t strnlen(const char *str, size_t maxlen);
437437
extern long random(void);
438438
#endif
439439

440+
#ifndef HAVE_SETENV
441+
extern int setenv(const char *name, const char *value, int overwrite);
442+
#endif
443+
440444
#ifndef HAVE_UNSETENV
441445
extern void unsetenv(const char *name);
442446
#endif

src/include/port/win32_port.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ extern void _dosmaperr(unsigned long);
471471

472472
/* in port/win32env.c */
473473
extern int pgwin32_putenv(const char *);
474+
extern int pgwin32_setenv(const char *name, const char *value, int overwrite);
474475
extern void pgwin32_unsetenv(const char *);
475476

476477
/* in port/win32security.c */
@@ -481,6 +482,7 @@ extern int pgwin32_is_admin(void);
481482
extern BOOL AddUserToTokenDacl(HANDLE hToken);
482483

483484
#define putenv(x) pgwin32_putenv(x)
485+
#define setenv(x,y,z) pgwin32_setenv(x,y,z)
484486
#define unsetenv(x) pgwin32_unsetenv(x)
485487

486488
/* Things that exist in MinGW headers, but need to be added to MSVC */

src/port/setenv.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* setenv.c
4+
* setenv() emulation for machines without it
5+
*
6+
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
*
10+
* IDENTIFICATION
11+
* src/port/setenv.c
12+
*
13+
*-------------------------------------------------------------------------
14+
*/
15+
16+
#include "c.h"
17+
18+
19+
int
20+
setenv(const char *name, const char *value, int overwrite)
21+
{
22+
char *envstr;
23+
24+
/* Error conditions, per POSIX */
25+
if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL ||
26+
value == NULL)
27+
{
28+
errno = EINVAL;
29+
return -1;
30+
}
31+
32+
/* No work if variable exists and we're not to replace it */
33+
if (overwrite == 0 && getenv(name) != NULL)
34+
return 0;
35+
36+
/*
37+
* Add or replace the value using putenv(). This will leak memory if the
38+
* same variable is repeatedly redefined, but there's little we can do
39+
* about that when sitting atop putenv().
40+
*/
41+
envstr = (char *) malloc(strlen(name) + strlen(value) + 2);
42+
if (!envstr) /* not much we can do if no memory */
43+
return -1;
44+
45+
sprintf(envstr, "%s=%s", name, value);
46+
47+
return putenv(envstr);
48+
}

src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ sub mkvcbuild
100100
dirent.c dlopen.c getopt.c getopt_long.c
101101
pread.c pwrite.c pg_bitutils.c
102102
pg_strong_random.c pgcheckdir.c pgmkdirp.c pgsleep.c pgstrcasecmp.c
103-
pqsignal.c mkdtemp.c qsort.c qsort_arg.c quotes.c system.c
103+
pqsignal.c mkdtemp.c qsort.c qsort_arg.c quotes.c setenv.c system.c
104104
sprompt.c strerror.c tar.c thread.c
105105
win32env.c win32error.c win32security.c win32setlocale.c);
106106

0 commit comments

Comments
 (0)