Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3MOb-0007sd-Lp for pgsql-committers@arkaria.postgresql.org; Tue, 13 Jul 2021 17:40:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m3MOa-0000i1-FD for pgsql-committers@arkaria.postgresql.org; Tue, 13 Jul 2021 17:40:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3MOa-0000hs-8O for pgsql-committers@lists.postgresql.org; Tue, 13 Jul 2021 17:40:36 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3MOV-0000lJ-MO for pgsql-committers@lists.postgresql.org; Tue, 13 Jul 2021 17:40:35 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 16DHeRZu3278309; Tue, 13 Jul 2021 13:40:27 -0400 From: Tom Lane To: Thomas Munro cc: pgsql-committers Subject: Re: pgsql: Add PSQL_WATCH_PAGER for psql's \watch command. In-reply-to: References: <3187588.1626136248@sss.pgh.pa.us> <3189569.1626138591@sss.pgh.pa.us> <3198323.1626149748@sss.pgh.pa.us> Comments: In-reply-to Thomas Munro message dated "Tue, 13 Jul 2021 23:55:57 +1200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3278297.1626198017.0@sss.pgh.pa.us> Date: Tue, 13 Jul 2021 13:40:27 -0400 Message-ID: <3278308.1626198027@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3278297.1626198017.1@sss.pgh.pa.us> Thomas Munro writes: > Here's a better patch. I didn't like the way you'd done this as two independent tests; the second one falsely reports success if there isn't actually any declaration of sigwait. I see you hacked around that by checking both results in c.h, but the printed result is still very misleading. Plus it's a waste of time to run the second test at all, if the first one fails. Here's a revised patch that I've tested (albeit lightly) on both HPUX and Solaris. I also got rid of the independent check in thread_test.c, since that's always been a hack that delivers a misleading complaint when it fails. Personally, I'd skip adding the stanza in c.h in favor of just making configure set USE_SIGWAIT directly, but perhaps you see it differently. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="0001-Portability-fixes-for-sigwait-2.patch"; charset="us-ascii" Content-ID: <3278297.1626198017.2@sss.pgh.pa.us> Content-Description: 0001-Portability-fixes-for-sigwait-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/config/thread_test.c b/config/thread_test.c index 784f4fe8ce..e2a9e62f49 100644 --- a/config/thread_test.c +++ b/config/thread_test.c @@ -43,10 +43,6 @@ #include #endif = -/* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */ -#include -int sigwait(const sigset_t *set, int *sig); - = #define TEMP_FILENAME_1 "thread_test.1" #define TEMP_FILENAME_2 "thread_test.2" diff --git a/configure b/configure index 1ea28a0d67..111751defa 100755 --- a/configure +++ b/configure @@ -15861,9 +15861,10 @@ $as_echo "#define HAVE_FSEEKO 1" >>confdefs.h fi = = -# posix_fadvise() is a no-op on Solaris, so don't incur function overhead -# by calling it, 2009-04-02 -# http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/= port/gen/posix_fadvise.c +# Make sure there's a declaration for sigwait(), then make sure +# that it conforms to the POSIX standard (there seem to still be +# some platforms out there with pre-POSIX sigwait()). On Solaris, +# PTHREAD_CFLAGS affects the result. # The Clang compiler raises a warning for an undeclared identifier that m= atches # a compiler builtin function. All extant Clang versions are affected, a= s of # Clang 3.6.0. Test a builtin known to every version. This problem affe= cts the @@ -15952,6 +15953,69 @@ case $ac_cv_c_decl_report in *) ac_c_decl_warn_flag=3D ;; esac = +ac_fn_c_check_decl "$LINENO" "sigwait" "ac_cv_have_decl_sigwait" "#includ= e +" +if test "x$ac_cv_have_decl_sigwait" =3D xyes; then : + ac_have_decl=3D1 +else + ac_have_decl=3D0 +fi + +cat >>confdefs.h <<_ACEOF +#define HAVE_DECL_SIGWAIT $ac_have_decl +_ACEOF + +if test "x$ac_cv_have_decl_sigwait" =3D xyes; then + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for POSIX-conforming = sigwait declaration" >&5 +$as_echo_n "checking for POSIX-conforming sigwait declaration... " >&6; } +if ${pgac_cv_have_posix_decl_sigwait+:} false; then : + $as_echo_n "(cached) " >&6 +else + _CFLAGS=3D"$CFLAGS" + _LIBS=3D"$LIBS" + CFLAGS=3D"$CFLAGS $PTHREAD_CFLAGS" + LIBS=3D"$LIBS $PTHREAD_LIBS" + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + #include + int sigwait(const sigset_t *set, int *sig); + +int +main () +{ + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + pgac_cv_have_posix_decl_sigwait=3Dyes +else + pgac_cv_have_posix_decl_sigwait=3Dno +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + CFLAGS=3D"$_CFLAGS" + LIBS=3D"$_LIBS" + +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_have_posix_decl= _sigwait" >&5 +$as_echo "$pgac_cv_have_posix_decl_sigwait" >&6; } +fi +if test "x$pgac_cv_have_posix_decl_sigwait" =3D xyes; then + +$as_echo "#define HAVE_POSIX_DECL_SIGWAIT 1" >>confdefs.h + +else + # On non-Windows, libpq requires POSIX sigwait() for thread safety. + if test "$enable_thread_safety" =3D yes -a "$PORTNAME" !=3D "win32"; th= en + as_fn_error $? "POSIX-conforming sigwait is required to enable thread= safety." "$LINENO" 5 + fi +fi + +# posix_fadvise() is a no-op on Solaris, so don't incur function overhead +# by calling it, 2009-04-02 +# http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/= port/gen/posix_fadvise.c if test "$PORTNAME" !=3D "solaris"; then : = for ac_func in posix_fadvise diff --git a/configure.ac b/configure.ac index 57336e1fb6..1b0e32681a 100644 --- a/configure.ac +++ b/configure.ac @@ -1741,6 +1741,39 @@ PGAC_CHECK_BUILTIN_FUNC([__builtin_popcount], [unsi= gned int x]) # in case it finds that _LARGEFILE_SOURCE has to be #define'd for that. AC_FUNC_FSEEKO = +# Make sure there's a declaration for sigwait(), then make sure +# that it conforms to the POSIX standard (there seem to still be +# some platforms out there with pre-POSIX sigwait()). On Solaris, +# PTHREAD_CFLAGS affects the result. +AC_CHECK_DECLS(sigwait, [], [], [#include ]) +if test "x$ac_cv_have_decl_sigwait" =3D xyes; then + AC_CACHE_CHECK([for POSIX-conforming sigwait declaration], + [pgac_cv_have_posix_decl_sigwait], + [_CFLAGS=3D"$CFLAGS" + _LIBS=3D"$LIBS" + CFLAGS=3D"$CFLAGS $PTHREAD_CFLAGS" + LIBS=3D"$LIBS $PTHREAD_LIBS" + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([ + #include + int sigwait(const sigset_t *set, int *sig); + ], + [])], + [pgac_cv_have_posix_decl_sigwait=3Dyes], + [pgac_cv_have_posix_decl_sigwait=3Dno]) + CFLAGS=3D"$_CFLAGS" + LIBS=3D"$_LIBS" + ]) +fi +if test "x$pgac_cv_have_posix_decl_sigwait" =3D xyes; then + AC_DEFINE(HAVE_POSIX_DECL_SIGWAIT, 1, + [Define to 1 if you have a POSIX-conforming sigwait declarati= on.]) +else + # On non-Windows, libpq requires POSIX sigwait() for thread safety. + if test "$enable_thread_safety" =3D yes -a "$PORTNAME" !=3D "win32"; th= en + AC_MSG_ERROR([POSIX-conforming sigwait is required to enable thread s= afety.]) + fi +fi + # posix_fadvise() is a no-op on Solaris, so don't incur function overhead # by calling it, 2009-04-02 # http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/= port/gen/posix_fadvise.c diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index d704c4220c..376620f68f 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -4899,7 +4899,7 @@ do_watch(PQExpBuffer query_buf, double sleep) FILE *pagerpipe =3D NULL; int title_len; int res =3D 0; -#ifndef WIN32 +#ifdef USE_SIGWAIT sigset_t sigalrm_sigchld_sigint; sigset_t sigalrm_sigchld; sigset_t sigint; @@ -4913,7 +4913,7 @@ do_watch(PQExpBuffer query_buf, double sleep) return false; } = -#ifndef WIN32 +#ifdef USE_SIGWAIT sigemptyset(&sigalrm_sigchld_sigint); sigaddset(&sigalrm_sigchld_sigint, SIGCHLD); sigaddset(&sigalrm_sigchld_sigint, SIGALRM); @@ -4952,7 +4952,7 @@ do_watch(PQExpBuffer query_buf, double sleep) * PAGER environment variables, because traditional pagers probably won'= t * be very useful for showing a stream of results. */ -#ifndef WIN32 +#ifdef USE_SIGWAIT pagerprog =3D getenv("PSQL_WATCH_PAGER"); #endif if (pagerprog && myopt.topt.pager) @@ -5023,7 +5023,7 @@ do_watch(PQExpBuffer query_buf, double sleep) if (pagerpipe && ferror(pagerpipe)) break; = -#ifdef WIN32 +#ifndef USE_SIGWAIT = /* * Set up cancellation of 'watch' via SIGINT. We redo this each time @@ -5059,7 +5059,8 @@ do_watch(PQExpBuffer query_buf, double sleep) { int signal_received; = - if (sigwait(&sigalrm_sigchld_sigint, &signal_received) < 0) + errno =3D sigwait(&sigalrm_sigchld_sigint, &signal_received); + if (errno !=3D 0) { /* Some other signal arrived? */ if (errno =3D=3D EINTR) @@ -5091,7 +5092,7 @@ do_watch(PQExpBuffer query_buf, double sleep) restore_sigpipe_trap(); } = -#ifndef WIN32 +#ifdef USE_SIGWAIT /* Disable the interval timer. */ memset(&interval, 0, sizeof(interval)); setitimer(ITIMER_REAL, &interval, NULL); diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 5f36f0d1c6..f0b46a5efc 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -110,7 +110,7 @@ log_locus_callback(const char **filename, uint64 *line= no) } } = -#ifndef WIN32 +#ifdef USE_SIGWAIT static void empty_signal_handler(SIGNAL_ARGS) { @@ -309,7 +309,7 @@ main(int argc, char *argv[]) = psql_setup_cancel_handler(); = -#ifndef WIN32 +#ifdef USE_SIGWAIT = /* * do_watch() needs signal handlers installed (otherwise sigwait() will diff --git a/src/include/c.h b/src/include/c.h index c8ede08273..4feae5ef57 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -1312,6 +1312,10 @@ extern long long strtoll(const char *str, char **en= dptr, int base); extern unsigned long long strtoull(const char *str, char **endptr, int ba= se); #endif = +#if defined(HAVE_POSIX_DECL_SIGWAIT) +#define USE_SIGWAIT +#endif + /* no special DLL markers on most ports */ #ifndef PGDLLIMPORT #define PGDLLIMPORT diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index d69d461ff2..15ffdd895a 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -158,6 +158,10 @@ don't. */ #undef HAVE_DECL_RTLD_NOW = +/* Define to 1 if you have the declaration of `sigwait', and to 0 if you + don't. */ +#undef HAVE_DECL_SIGWAIT + /* Define to 1 if you have the declaration of `strlcat', and to 0 if you don't. */ #undef HAVE_DECL_STRLCAT @@ -414,6 +418,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_POLL_H = +/* Define to 1 if you have a POSIX-conforming sigwait declaration. */ +#undef HAVE_POSIX_DECL_SIGWAIT + /* Define to 1 if you have the `posix_fadvise' function. */ #undef HAVE_POSIX_FADVISE = diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index 294b968dcd..c967743467 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -249,6 +249,7 @@ sub GenerateFiles HAVE_DECL_PWRITEV =3D> 0, HAVE_DECL_RTLD_GLOBAL =3D> 0, HAVE_DECL_RTLD_NOW =3D> 0, + HAVE_DECL_SIGWAIT =3D> 0, HAVE_DECL_STRLCAT =3D> undef, HAVE_DECL_STRLCPY =3D> undef, HAVE_DECL_STRNLEN =3D> 1, @@ -332,6 +333,7 @@ sub GenerateFiles HAVE_PAM_PAM_APPL_H =3D> undef, HAVE_POLL =3D> undef, HAVE_POLL_H =3D> undef, + HAVE_POSIX_DECL_SIGWAIT =3D> undef, HAVE_POSIX_FADVISE =3D> undef, HAVE_POSIX_FALLOCATE =3D> undef, HAVE_PPC_LWARX_MUTEX_HINT =3D> undef, ------- =_aaaaaaaaaa0--