Skip to content

Commit e2f21ff

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 fe6f632 commit e2f21ff

File tree

8 files changed

+80
-3
lines changed

8 files changed

+80
-3
lines changed

configure

+18-1
Original file line numberDiff line numberDiff line change
@@ -15858,12 +15858,29 @@ case $host_os in
1585815858
# Windows uses a specialised env handler
1585915859
mingw*)
1586015860

15861+
$as_echo "#define HAVE_SETENV 1" >>confdefs.h
15862+
15863+
1586115864
$as_echo "#define HAVE_UNSETENV 1" >>confdefs.h
1586215865

15866+
ac_cv_func_setenv=yes
1586315867
ac_cv_func_unsetenv=yes
1586415868
;;
1586515869
*)
15866-
ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
15870+
ac_fn_c_check_func "$LINENO" "setenv" "ac_cv_func_setenv"
15871+
if test "x$ac_cv_func_setenv" = xyes; then :
15872+
$as_echo "#define HAVE_SETENV 1" >>confdefs.h
15873+
15874+
else
15875+
case " $LIBOBJS " in
15876+
*" setenv.$ac_objext "* ) ;;
15877+
*) LIBOBJS="$LIBOBJS setenv.$ac_objext"
15878+
;;
15879+
esac
15880+
15881+
fi
15882+
15883+
ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
1586715884
if test "x$ac_cv_func_unsetenv" = xyes; then :
1586815885
$as_echo "#define HAVE_UNSETENV 1" >>confdefs.h
1586915886

configure.in

+3-1
Original file line numberDiff line numberDiff line change
@@ -1747,11 +1747,13 @@ fi
17471747
case $host_os in
17481748
# Windows uses a specialised env handler
17491749
mingw*)
1750+
AC_DEFINE(HAVE_SETENV, 1, [Define to 1 because replacement version used.])
17501751
AC_DEFINE(HAVE_UNSETENV, 1, [Define to 1 because replacement version used.])
1752+
ac_cv_func_setenv=yes
17511753
ac_cv_func_unsetenv=yes
17521754
;;
17531755
*)
1754-
AC_REPLACE_FUNCS([unsetenv])
1756+
AC_REPLACE_FUNCS([setenv unsetenv])
17551757
;;
17561758
esac
17571759

src/include/pg_config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@
473473
/* Define to 1 if you have the <security/pam_appl.h> header file. */
474474
#undef HAVE_SECURITY_PAM_APPL_H
475475

476+
/* Define to 1 if you have the `setenv' function. */
477+
#undef HAVE_SETENV
478+
476479
/* Define to 1 if you have the `setproctitle' function. */
477480
#undef HAVE_SETPROCTITLE
478481

src/include/port.h

+4
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ extern size_t strnlen(const char *str, size_t maxlen);
429429
extern long random(void);
430430
#endif
431431

432+
#ifndef HAVE_SETENV
433+
extern int setenv(const char *name, const char *value, int overwrite);
434+
#endif
435+
432436
#ifndef HAVE_UNSETENV
433437
extern void unsetenv(const char *name);
434438
#endif

src/include/port/win32_port.h

+2
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ extern void _dosmaperr(unsigned long);
462462

463463
/* in port/win32env.c */
464464
extern int pgwin32_putenv(const char *);
465+
extern int pgwin32_setenv(const char *name, const char *value, int overwrite);
465466
extern void pgwin32_unsetenv(const char *);
466467

467468
/* in port/win32security.c */
@@ -472,6 +473,7 @@ extern int pgwin32_is_admin(void);
472473
extern BOOL AddUserToTokenDacl(HANDLE hToken);
473474

474475
#define putenv(x) pgwin32_putenv(x)
476+
#define setenv(x,y,z) pgwin32_setenv(x,y,z)
475477
#define unsetenv(x) pgwin32_unsetenv(x)
476478

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

src/port/setenv.c

+48
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ sub mkvcbuild
101101
dirent.c dlopen.c getopt.c getopt_long.c link.c
102102
pread.c pwrite.c pg_bitutils.c
103103
pg_strong_random.c pgcheckdir.c pgmkdirp.c pgsleep.c pgstrcasecmp.c
104-
pqsignal.c mkdtemp.c qsort.c qsort_arg.c quotes.c system.c
104+
pqsignal.c mkdtemp.c qsort.c qsort_arg.c quotes.c setenv.c system.c
105105
sprompt.c strerror.c tar.c thread.c
106106
win32env.c win32error.c win32security.c win32setlocale.c);
107107

src/tools/msvc/Solution.pm

+1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ sub GenerateFiles
341341
HAVE_RL_FILENAME_QUOTING_FUNCTION => undef,
342342
HAVE_RL_RESET_SCREEN_SIZE => undef,
343343
HAVE_SECURITY_PAM_APPL_H => undef,
344+
HAVE_SETENV => undef,
344345
HAVE_SETPROCTITLE => undef,
345346
HAVE_SETPROCTITLE_FAST => undef,
346347
HAVE_SETSID => undef,

0 commit comments

Comments
 (0)