On Tue, Jul 13, 2021 at 4:15 PM Tom Lane <t...@sss.pgh.pa.us> wrote:
> Huh ... gaur did this:
>
> /usr/ccs/bin/ld: Unsatisfied symbols:
>    sigwait (code)
>
> which is not what I was expecting, because there is a definition for
> sigwait in /usr/include (though not in a header file we use).  It must
> be in some add-on library instead of libc.

Could be part of "DCE threads" (= "draft 4"), from last time I googled
this topic when gaur blew up.

> However, wrasse did this:
>
> "/export/home/nm/farm/studio64v12_6/HEAD/pgsql.build/../pgsql/src/bin/psql/command.c",
>  line 5062: prototype mismatch: 2 args passed, 1 expected
> cc: acomp failed for 
> /export/home/nm/farm/studio64v12_6/HEAD/pgsql.build/../pgsql/src/bin/psql/command.c
>
> which I was expecting even less.  Evidently, prehistoric HPUX is not the
> only platform still using the pre-POSIX API for this function.  So we
> really do need the full configure check.

Huh, that is surprising.  Apparently it has "draft 6" and also final,
depending on a macro:

https://illumos.org/man/2/sigwait
https://docs.oracle.com/cd/E36784_01/html/E36872/sigwait-2.html

Here's a first swing at a configure check.  Could probably be improved
a bit, as I haven't yet tried to share the check with thread_test.c (I
was going to ask if it might be entirely removable since draft 4
systems like gaur surely can't compile the rest of thread_test.c due
to other differences we know about, but now that I know that there are
also draft 6 systems out there...).  Note also the errno change, which
I (ironically) did the pre-standard way by mistake.
From 6f03a0f82fa842e5d6f2b32807343907923961a4 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.mu...@gmail.com>
Date: Tue, 13 Jul 2021 12:47:03 +1200
Subject: [PATCH] Add checks around sigwait.

Build farm animal gaur has a pre-standard sigwait so would fail
on recent commit 7c09d279.  Add a configure check to avoid that.

Also fix the error checking.  Modern sigwait returns errors, it
doesn't set errno.

Reported-by: Tom Lane <t...@sss.pgh.pa.us>
Discussion: https://postgr.es/m/3187588.1626136248%40sss.pgh.pa.us
---
 configure                  | 36 ++++++++++++++++++++++++++++++++++++
 configure.ac               | 15 +++++++++++++++
 src/bin/psql/command.c     | 13 +++++++------
 src/bin/psql/startup.c     |  4 ++--
 src/include/pg_config.h.in |  3 +++
 src/tools/msvc/Solution.pm |  1 +
 6 files changed, 64 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 1ea28a0d67..1a9ca5a59c 100755
--- a/configure
+++ b/configure
@@ -15861,6 +15861,42 @@ $as_echo "#define HAVE_FSEEKO 1" >>confdefs.h
 fi
 
 
+# Check if there is a declaration for sigwait() that matches POSIX.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for POSIX-conforming sigwait" >&5
+$as_echo_n "checking for POSIX-conforming sigwait... " >&6; }
+if ${pgac_cv_have_posix_sigwait+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */
+#include <signal.h>
+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_sigwait=yes
+else
+  pgac_cv_have_posix_sigwait=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_have_posix_sigwait" >&5
+$as_echo "$pgac_cv_have_posix_sigwait" >&6; }
+if test x"$pgac_cv_have_posix_sigwait" = xyes ; then
+
+$as_echo "#define HAVE_POSIX_SIGWAIT 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
diff --git a/configure.ac b/configure.ac
index 57336e1fb6..5eb8d6de88 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1741,6 +1741,21 @@ PGAC_CHECK_BUILTIN_FUNC([__builtin_popcount], [unsigned int x])
 # in case it finds that _LARGEFILE_SOURCE has to be #define'd for that.
 AC_FUNC_FSEEKO
 
+# Check if there is a declaration for sigwait() that matches POSIX.
+AC_CACHE_CHECK([for POSIX-conforming sigwait],
+			   [pgac_cv_have_posix_sigwait],
+[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+/* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */
+#include <signal.h>
+int		sigwait(const sigset_t *set, int *sig);
+],
+[])],
+[pgac_cv_have_posix_sigwait=yes],
+[pgac_cv_have_posix_sigwait=no])])
+if test x"$pgac_cv_have_posix_sigwait" = xyes ; then
+AC_DEFINE(HAVE_POSIX_SIGWAIT, 1, [Define to 1 if you have POSIX-conforming sigwait.])
+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..6c9c93ee86 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 = NULL;
 	int			title_len;
 	int			res = 0;
-#ifndef WIN32
+#ifdef HAVE_POSIX_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 HAVE_POSIX_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 HAVE_POSIX_SIGWAIT
 	pagerprog = 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 HAVE_POSIX_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 = sigwait(&sigalrm_sigchld_sigint, &signal_received);
+			if (errno != 0)
 			{
 				/* Some other signal arrived? */
 				if (errno == EINTR)
@@ -5091,7 +5092,7 @@ do_watch(PQExpBuffer query_buf, double sleep)
 		restore_sigpipe_trap();
 	}
 
-#ifndef WIN32
+#ifdef HAVE_POSIX_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..4576125891 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -110,7 +110,7 @@ log_locus_callback(const char **filename, uint64 *lineno)
 	}
 }
 
-#ifndef WIN32
+#ifdef HAVE_POSIX_SIGWAIT
 static void
 empty_signal_handler(SIGNAL_ARGS)
 {
@@ -309,7 +309,7 @@ main(int argc, char *argv[])
 
 	psql_setup_cancel_handler();
 
-#ifndef WIN32
+#ifdef HAVE_POSIX_SIGWAIT
 
 	/*
 	 * do_watch() needs signal handlers installed (otherwise sigwait() will
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index d69d461ff2..cbf518dd26 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -420,6 +420,9 @@
 /* Define to 1 if you have the `posix_fallocate' function. */
 #undef HAVE_POSIX_FALLOCATE
 
+/* Define to 1 if you have POSIX-conforming sigwait. */
+#undef HAVE_POSIX_SIGWAIT
+
 /* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
 #undef HAVE_PPC_LWARX_MUTEX_HINT
 
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index 294b968dcd..86424a5d1d 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -334,6 +334,7 @@ sub GenerateFiles
 		HAVE_POLL_H                 => undef,
 		HAVE_POSIX_FADVISE          => undef,
 		HAVE_POSIX_FALLOCATE        => undef,
+		HAVE_POSIX_SIGWAIT          => undef,
 		HAVE_PPC_LWARX_MUTEX_HINT   => undef,
 		HAVE_PPOLL                  => undef,
 		HAVE_PREAD                  => undef,
-- 
2.30.2

Reply via email to