Skip to content

Commit 5865e06

Browse files
committed
Portability fixes for sigwait.
Build farm animals running ancient HPUX and Solaris have a non-standard sigwait() from draft versions of POSIX, so they didn't like commit 7c09d27. To avoid the problem in general, only try to use sigwait() if it's declared by <signal.h> and matches the expected declaration. To select the modern declaration on Solaris (even in non-threaded programs), move -D_POSIX_PTHREAD_SEMANTICS into the right place to affect all translation units. Also fix the error checking. Modern sigwait() doesn't set errno. Thanks to Tom Lane for help with this. Discussion: https://postgr.es/m/3187588.1626136248%40sss.pgh.pa.us
1 parent 9aa8268 commit 5865e06

File tree

7 files changed

+122
-21
lines changed

7 files changed

+122
-21
lines changed

config/thread_test.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@
4343
#include <winsock2.h>
4444
#endif
4545

46-
/* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */
47-
#include <signal.h>
48-
int sigwait(const sigset_t *set, int *sig);
49-
5046

5147
#define TEMP_FILENAME_1 "thread_test.1"
5248
#define TEMP_FILENAME_2 "thread_test.2"

configure

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7194,6 +7194,12 @@ $as_echo "#define PROFILE_PID_DIR 1" >>confdefs.h
71947194
fi
71957195
fi
71967196

7197+
# On Solaris, we need this #define to get POSIX-conforming versions
7198+
# of many interfaces (sigwait, getpwuid_r, ...).
7199+
if test "$PORTNAME" = "solaris"; then
7200+
CPPFLAGS="$CPPFLAGS -D_POSIX_PTHREAD_SEMANTICS"
7201+
fi
7202+
71977203
# We already have this in Makefile.win32, but configure needs it too
71987204
if test "$PORTNAME" = "win32"; then
71997205
CPPFLAGS="$CPPFLAGS -I$srcdir/src/include/port/win32"
@@ -11296,9 +11302,8 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
1129611302
# set thread flags
1129711303

1129811304
# Some platforms use these, so just define them. They can't hurt if they
11299-
# are not supported. For example, on Solaris -D_POSIX_PTHREAD_SEMANTICS
11300-
# enables 5-arg getpwuid_r, among other things.
11301-
PTHREAD_CFLAGS="$PTHREAD_CFLAGS -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
11305+
# are not supported.
11306+
PTHREAD_CFLAGS="$PTHREAD_CFLAGS -D_REENTRANT -D_THREAD_SAFE"
1130211307

1130311308
# Check for *_r functions
1130411309
_CFLAGS="$CFLAGS"
@@ -15861,9 +15866,11 @@ $as_echo "#define HAVE_FSEEKO 1" >>confdefs.h
1586115866
fi
1586215867

1586315868

15864-
# posix_fadvise() is a no-op on Solaris, so don't incur function overhead
15865-
# by calling it, 2009-04-02
15866-
# http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/posix_fadvise.c
15869+
# Make sure there's a declaration for sigwait(), then make sure
15870+
# that it conforms to the POSIX standard (there seem to still be
15871+
# some platforms out there with pre-POSIX sigwait()). On Solaris,
15872+
# _POSIX_PTHREAD_SEMANTICS affects the result, but we already
15873+
# added that to CPPFLAGS.
1586715874
# The Clang compiler raises a warning for an undeclared identifier that matches
1586815875
# a compiler builtin function. All extant Clang versions are affected, as of
1586915876
# Clang 3.6.0. Test a builtin known to every version. This problem affects the
@@ -15952,6 +15959,62 @@ case $ac_cv_c_decl_report in
1595215959
*) ac_c_decl_warn_flag= ;;
1595315960
esac
1595415961

15962+
ac_fn_c_check_decl "$LINENO" "sigwait" "ac_cv_have_decl_sigwait" "#include <signal.h>
15963+
"
15964+
if test "x$ac_cv_have_decl_sigwait" = xyes; then :
15965+
ac_have_decl=1
15966+
else
15967+
ac_have_decl=0
15968+
fi
15969+
15970+
cat >>confdefs.h <<_ACEOF
15971+
#define HAVE_DECL_SIGWAIT $ac_have_decl
15972+
_ACEOF
15973+
15974+
if test "x$ac_cv_have_decl_sigwait" = xyes; then
15975+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for POSIX-conforming sigwait declaration" >&5
15976+
$as_echo_n "checking for POSIX-conforming sigwait declaration... " >&6; }
15977+
if ${pgac_cv_have_posix_decl_sigwait+:} false; then :
15978+
$as_echo_n "(cached) " >&6
15979+
else
15980+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
15981+
/* end confdefs.h. */
15982+
15983+
#include <signal.h>
15984+
int sigwait(const sigset_t *set, int *sig);
15985+
15986+
int
15987+
main ()
15988+
{
15989+
15990+
;
15991+
return 0;
15992+
}
15993+
_ACEOF
15994+
if ac_fn_c_try_compile "$LINENO"; then :
15995+
pgac_cv_have_posix_decl_sigwait=yes
15996+
else
15997+
pgac_cv_have_posix_decl_sigwait=no
15998+
fi
15999+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
16000+
fi
16001+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_have_posix_decl_sigwait" >&5
16002+
$as_echo "$pgac_cv_have_posix_decl_sigwait" >&6; }
16003+
fi
16004+
if test "x$pgac_cv_have_posix_decl_sigwait" = xyes; then
16005+
16006+
$as_echo "#define HAVE_POSIX_DECL_SIGWAIT 1" >>confdefs.h
16007+
16008+
else
16009+
# On non-Windows, libpq requires POSIX sigwait() for thread safety.
16010+
if test "$enable_thread_safety" = yes -a "$PORTNAME" != "win32"; then
16011+
as_fn_error $? "POSIX-conforming sigwait is required to enable thread safety." "$LINENO" 5
16012+
fi
16013+
fi
16014+
16015+
# posix_fadvise() is a no-op on Solaris, so don't incur function overhead
16016+
# by calling it, 2009-04-02
16017+
# http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/posix_fadvise.c
1595516018
if test "$PORTNAME" != "solaris"; then :
1595616019

1595716020
for ac_func in posix_fadvise

configure.ac

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,12 @@ if test "$enable_profiling" = yes && test "$ac_cv_prog_cc_g" = yes; then
610610
fi
611611
fi
612612

613+
# On Solaris, we need this #define to get POSIX-conforming versions
614+
# of many interfaces (sigwait, getpwuid_r, ...).
615+
if test "$PORTNAME" = "solaris"; then
616+
CPPFLAGS="$CPPFLAGS -D_POSIX_PTHREAD_SEMANTICS"
617+
fi
618+
613619
# We already have this in Makefile.win32, but configure needs it too
614620
if test "$PORTNAME" = "win32"; then
615621
CPPFLAGS="$CPPFLAGS -I$srcdir/src/include/port/win32"
@@ -1122,9 +1128,8 @@ AS_IF([test "$enable_thread_safety" = yes -a "$PORTNAME" != "win32"],
11221128
AX_PTHREAD # set thread flags
11231129
11241130
# Some platforms use these, so just define them. They can't hurt if they
1125-
# are not supported. For example, on Solaris -D_POSIX_PTHREAD_SEMANTICS
1126-
# enables 5-arg getpwuid_r, among other things.
1127-
PTHREAD_CFLAGS="$PTHREAD_CFLAGS -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
1131+
# are not supported.
1132+
PTHREAD_CFLAGS="$PTHREAD_CFLAGS -D_REENTRANT -D_THREAD_SAFE"
11281133
11291134
# Check for *_r functions
11301135
_CFLAGS="$CFLAGS"
@@ -1741,6 +1746,33 @@ PGAC_CHECK_BUILTIN_FUNC([__builtin_popcount], [unsigned int x])
17411746
# in case it finds that _LARGEFILE_SOURCE has to be #define'd for that.
17421747
AC_FUNC_FSEEKO
17431748

1749+
# Make sure there's a declaration for sigwait(), then make sure
1750+
# that it conforms to the POSIX standard (there seem to still be
1751+
# some platforms out there with pre-POSIX sigwait()). On Solaris,
1752+
# _POSIX_PTHREAD_SEMANTICS affects the result, but we already
1753+
# added that to CPPFLAGS.
1754+
AC_CHECK_DECLS(sigwait, [], [], [#include <signal.h>])
1755+
if test "x$ac_cv_have_decl_sigwait" = xyes; then
1756+
AC_CACHE_CHECK([for POSIX-conforming sigwait declaration],
1757+
[pgac_cv_have_posix_decl_sigwait],
1758+
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
1759+
#include <signal.h>
1760+
int sigwait(const sigset_t *set, int *sig);
1761+
],
1762+
[])],
1763+
[pgac_cv_have_posix_decl_sigwait=yes],
1764+
[pgac_cv_have_posix_decl_sigwait=no])])
1765+
fi
1766+
if test "x$pgac_cv_have_posix_decl_sigwait" = xyes; then
1767+
AC_DEFINE(HAVE_POSIX_DECL_SIGWAIT, 1,
1768+
[Define to 1 if you have a POSIX-conforming sigwait declaration.])
1769+
else
1770+
# On non-Windows, libpq requires POSIX sigwait() for thread safety.
1771+
if test "$enable_thread_safety" = yes -a "$PORTNAME" != "win32"; then
1772+
AC_MSG_ERROR([POSIX-conforming sigwait is required to enable thread safety.])
1773+
fi
1774+
fi
1775+
17441776
# posix_fadvise() is a no-op on Solaris, so don't incur function overhead
17451777
# by calling it, 2009-04-02
17461778
# http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/posix_fadvise.c

src/bin/psql/command.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4899,7 +4899,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
48994899
FILE *pagerpipe = NULL;
49004900
int title_len;
49014901
int res = 0;
4902-
#ifndef WIN32
4902+
#ifdef HAVE_POSIX_DECL_SIGWAIT
49034903
sigset_t sigalrm_sigchld_sigint;
49044904
sigset_t sigalrm_sigchld;
49054905
sigset_t sigint;
@@ -4913,7 +4913,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
49134913
return false;
49144914
}
49154915

4916-
#ifndef WIN32
4916+
#ifdef HAVE_POSIX_DECL_SIGWAIT
49174917
sigemptyset(&sigalrm_sigchld_sigint);
49184918
sigaddset(&sigalrm_sigchld_sigint, SIGCHLD);
49194919
sigaddset(&sigalrm_sigchld_sigint, SIGALRM);
@@ -4952,7 +4952,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
49524952
* PAGER environment variables, because traditional pagers probably won't
49534953
* be very useful for showing a stream of results.
49544954
*/
4955-
#ifndef WIN32
4955+
#ifdef HAVE_POSIX_DECL_SIGWAIT
49564956
pagerprog = getenv("PSQL_WATCH_PAGER");
49574957
#endif
49584958
if (pagerprog && myopt.topt.pager)
@@ -5023,7 +5023,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
50235023
if (pagerpipe && ferror(pagerpipe))
50245024
break;
50255025

5026-
#ifdef WIN32
5026+
#ifndef HAVE_POSIX_DECL_SIGWAIT
50275027

50285028
/*
50295029
* Set up cancellation of 'watch' via SIGINT. We redo this each time
@@ -5059,7 +5059,8 @@ do_watch(PQExpBuffer query_buf, double sleep)
50595059
{
50605060
int signal_received;
50615061

5062-
if (sigwait(&sigalrm_sigchld_sigint, &signal_received) < 0)
5062+
errno = sigwait(&sigalrm_sigchld_sigint, &signal_received);
5063+
if (errno != 0)
50635064
{
50645065
/* Some other signal arrived? */
50655066
if (errno == EINTR)
@@ -5091,7 +5092,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
50915092
restore_sigpipe_trap();
50925093
}
50935094

5094-
#ifndef WIN32
5095+
#ifdef HAVE_POSIX_DECL_SIGWAIT
50955096
/* Disable the interval timer. */
50965097
memset(&interval, 0, sizeof(interval));
50975098
setitimer(ITIMER_REAL, &interval, NULL);

src/bin/psql/startup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ log_locus_callback(const char **filename, uint64 *lineno)
110110
}
111111
}
112112

113-
#ifndef WIN32
113+
#ifdef HAVE_POSIX_DECL_SIGWAIT
114114
static void
115115
empty_signal_handler(SIGNAL_ARGS)
116116
{
@@ -309,7 +309,7 @@ main(int argc, char *argv[])
309309

310310
psql_setup_cancel_handler();
311311

312-
#ifndef WIN32
312+
#ifdef HAVE_POSIX_DECL_SIGWAIT
313313

314314
/*
315315
* do_watch() needs signal handlers installed (otherwise sigwait() will

src/include/pg_config.h.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@
158158
don't. */
159159
#undef HAVE_DECL_RTLD_NOW
160160

161+
/* Define to 1 if you have the declaration of `sigwait', and to 0 if you
162+
don't. */
163+
#undef HAVE_DECL_SIGWAIT
164+
161165
/* Define to 1 if you have the declaration of `strlcat', and to 0 if you
162166
don't. */
163167
#undef HAVE_DECL_STRLCAT
@@ -414,6 +418,9 @@
414418
/* Define to 1 if you have the <poll.h> header file. */
415419
#undef HAVE_POLL_H
416420

421+
/* Define to 1 if you have a POSIX-conforming sigwait declaration. */
422+
#undef HAVE_POSIX_DECL_SIGWAIT
423+
417424
/* Define to 1 if you have the `posix_fadvise' function. */
418425
#undef HAVE_POSIX_FADVISE
419426

src/tools/msvc/Solution.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ sub GenerateFiles
249249
HAVE_DECL_PWRITEV => 0,
250250
HAVE_DECL_RTLD_GLOBAL => 0,
251251
HAVE_DECL_RTLD_NOW => 0,
252+
HAVE_DECL_SIGWAIT => 0,
252253
HAVE_DECL_STRLCAT => undef,
253254
HAVE_DECL_STRLCPY => undef,
254255
HAVE_DECL_STRNLEN => 1,
@@ -332,6 +333,7 @@ sub GenerateFiles
332333
HAVE_PAM_PAM_APPL_H => undef,
333334
HAVE_POLL => undef,
334335
HAVE_POLL_H => undef,
336+
HAVE_POSIX_DECL_SIGWAIT => undef,
335337
HAVE_POSIX_FADVISE => undef,
336338
HAVE_POSIX_FALLOCATE => undef,
337339
HAVE_PPC_LWARX_MUTEX_HINT => undef,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy