POSIX specifies that - SIG2STR_MAX "shall be suitable for use in #if preprocessing directives" <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/signal.h.html> - sig2str and str2sig shall be declared in <signal.h>. <https://pubs.opengroup.org/onlinepubs/9799919799/functions/str2sig.html>
This patch implements this, by moving the declarations to <signal.h> and by changing the way SIG2STR_MAX is defined (but without changing its value: 19). 2024-08-09 Bruno Haible <br...@clisp.org> sig2str: Align with POSIX:2024. * lib/signal.in.h (SIG2STR_MAX): New macro. (sig2str, str2sig): New declarations. * lib/sig2str.h: Don't include intprops.h. (SIG2STR_MAX): Remove macro. (sig2str, str2sig): Remove declarations. * lib/sig2str.c: Include <signal.h> as the specification header. * m4/signal_h.m4 (gl_SIGNAL_H): Test whether sig2str, str2sig are declared. (gl_SIGNAL_H_REQUIRE_DEFAULTS): Initialize GNULIB_SIG2STR. (gl_SIGNAL_H_DEFAULTS): Initialize HAVE_SIG2STR, HAVE_STR2SIG. * m4/sig2str.m4 (gl_FUNC_SIG2STR): Require gl_SIGNAL_H_DEFAULTS. Check also for str2sig. Set HAVE_SIG2STR, HAVE_STR2SIG. * modules/signal-h (Makefile.am): Substitute GNULIB_SIG2STR, HAVE_SIG2STR, HAVE_STR2SIG. * modules/sig2str (Depends-on): Add signal-h. Remove intprops. (configure.ac): Test HAVE_SIG2STR, HAVE_STR2SIG. Invoke gl_SIGNAL_MODULE_INDICATOR. (Include): Add <signal.h>. * doc/posix-functions/sig2str.texi: Mention as implemented through the sig2str module. * doc/posix-functions/str2sig.texi: Likewise. diff --git a/doc/posix-functions/sig2str.texi b/doc/posix-functions/sig2str.texi index 80a177a050..f874d9c875 100644 --- a/doc/posix-functions/sig2str.texi +++ b/doc/posix-functions/sig2str.texi @@ -4,18 +4,15 @@ POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9799919799/functions/sig2str.html} -Gnulib module: --- +Gnulib module: sig2str Portability problems fixed by Gnulib: @itemize -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item This function is missing on many platforms: glibc 2.40, macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.3.0, AIX 7.3.1, HP-UX 11.31, Cygwin 3.2.x, mingw, MSVC 14, Android 9.0. @end itemize -Note: Gnulib has a module @code{sig2str} that implements this function, -but declares it in a different header file. +Portability problems not fixed by Gnulib: +@itemize +@end itemize diff --git a/doc/posix-functions/str2sig.texi b/doc/posix-functions/str2sig.texi index 706d2cd0c7..f76b4e331d 100644 --- a/doc/posix-functions/str2sig.texi +++ b/doc/posix-functions/str2sig.texi @@ -4,18 +4,15 @@ POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9799919799/functions/str2sig.html} -Gnulib module: --- +Gnulib module: sig2str Portability problems fixed by Gnulib: @itemize -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item This function is missing on many platforms: glibc 2.40, macOS 14, FreeBSD 14.0, NetBSD 10.0, OpenBSD 7.5, Minix 3.3.0, AIX 7.3.1, HP-UX 11.31, Cygwin 3.2.x, mingw, MSVC 14, Android 9.0. @end itemize -Note: Gnulib has a module @code{sig2str} that implements this function, -but declares it in a different header file. +Portability problems not fixed by Gnulib: +@itemize +@end itemize diff --git a/lib/sig2str.c b/lib/sig2str.c index c6b91e3849..2dad2cd5a3 100644 --- a/lib/sig2str.c +++ b/lib/sig2str.c @@ -19,8 +19,10 @@ #include <config.h> -#include <limits.h> +/* Specification. */ #include <signal.h> + +#include <limits.h> #include <stdlib.h> #include <stdio.h> #include <string.h> diff --git a/lib/sig2str.h b/lib/sig2str.h index 391da4b1d1..1abdb140e5 100644 --- a/lib/sig2str.h +++ b/lib/sig2str.h @@ -19,27 +19,6 @@ #include <signal.h> -/* Don't override system declarations of SIG2STR_MAX, sig2str, str2sig. */ -#ifndef SIG2STR_MAX - -# include "intprops.h" - -/* Size of a buffer needed to hold a signal name like "HUP". */ -# define SIG2STR_MAX (sizeof "SIGRTMAX" + INT_STRLEN_BOUND (int) - 1) - -#ifdef __cplusplus -extern "C" { -#endif - -int sig2str (int, char *); -int str2sig (char const *, int *); - -#ifdef __cplusplus -} -#endif - -#endif - /* An upper bound on signal numbers allowed by the system. */ #if defined _sys_nsig diff --git a/lib/signal.in.h b/lib/signal.in.h index 7ea0242db3..a0effa21ba 100644 --- a/lib/signal.in.h +++ b/lib/signal.in.h @@ -139,6 +139,47 @@ typedef void (*sighandler_t) (int); #endif +/* Maximum size of a signal name returned by sig2str(), including the + terminating NUL byte. */ +#ifndef SIG2STR_MAX +/* The longest one: "RTMAX", then "+" or "-", then up to 10 digits, then NUL. + Add + 2 as a reserve for the future. */ +# define SIG2STR_MAX (5 + 1 + 10 + 1 + 2) +#endif + +#if @GNULIB_SIG2STR@ +# if !@HAVE_SIG2STR@ +_GL_FUNCDECL_SYS (sig2str, int, (int signo, char *str)); +# endif +_GL_CXXALIAS_SYS (sig2str, int, (int signo, char *str)); +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (sig2str); +# endif +#elif defined GNULIB_POSIXCHECK +# undef sig2str +# if HAVE_RAW_DECL_SIG2STR +_GL_WARN_ON_USE (sig2str, "sig2str is not portable - " + "use gnulib module sig2str for portability"); +# endif +#endif + +#if @GNULIB_SIG2STR@ +# if !@HAVE_STR2SIG@ +_GL_FUNCDECL_SYS (str2sig, int, (char const *str, int *signo_p)); +# endif +_GL_CXXALIAS_SYS (str2sig, int, (char const *str, int *signo_p)); +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (str2sig); +# endif +#elif defined GNULIB_POSIXCHECK +# undef str2sig +# if HAVE_RAW_DECL_STR2SIG +_GL_WARN_ON_USE (str2sig, "str2sig is not portable - " + "use gnulib module sig2str for portability"); +# endif +#endif + + #if @GNULIB_PTHREAD_SIGMASK@ # if @REPLACE_PTHREAD_SIGMASK@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) diff --git a/m4/sig2str.m4 b/m4/sig2str.m4 index 096d0253fc..acef58a6a4 100644 --- a/m4/sig2str.m4 +++ b/m4/sig2str.m4 @@ -1,5 +1,5 @@ # sig2str.m4 -# serial 7 +# serial 8 dnl Copyright (C) 2002, 2005-2006, 2009-2024 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -7,7 +7,14 @@ AC_DEFUN([gl_FUNC_SIG2STR], [ - AC_CHECK_FUNCS([sig2str]) + AC_REQUIRE([gl_SIGNAL_H_DEFAULTS]) + AC_CHECK_FUNCS([sig2str str2sig]) + if test $ac_cv_func_sig2str = no; then + HAVE_SIG2STR=0 + fi + if test $ac_cv_func_str2sig = no; then + HAVE_STR2SIG=0 + fi ]) # Prerequisites of lib/sig2str.c. diff --git a/m4/signal_h.m4 b/m4/signal_h.m4 index 65afa2a1de..7a7d2b3c52 100644 --- a/m4/signal_h.m4 +++ b/m4/signal_h.m4 @@ -1,5 +1,5 @@ # signal_h.m4 -# serial 22 +# serial 23 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -32,7 +32,8 @@ AC_DEFUN_ONCE([gl_SIGNAL_H] dnl Check for declarations of anything we want to poison if the dnl corresponding gnulib module is not in use. gl_WARN_ON_USE_PREPARE([[#include <signal.h> - ]], [pthread_sigmask sigaction + ]], [sig2str str2sig + pthread_sigmask sigaction sigaddset sigdelset sigemptyset sigfillset sigismember sigpending sigprocmask]) @@ -74,6 +75,7 @@ AC_DEFUN([gl_SIGNAL_H_REQUIRE_DEFAULTS] m4_defun(GL_MODULE_INDICATOR_PREFIX[_SIGNAL_H_MODULE_INDICATOR_DEFAULTS], [ gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_PTHREAD_SIGMASK]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_RAISE]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SIG2STR]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SIGNAL_H_SIGPIPE]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SIGPROCMASK]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SIGACTION]) @@ -88,9 +90,11 @@ AC_DEFUN([gl_SIGNAL_H_DEFAULTS] HAVE_POSIX_SIGNALBLOCKING=1; AC_SUBST([HAVE_POSIX_SIGNALBLOCKING]) HAVE_PTHREAD_SIGMASK=1; AC_SUBST([HAVE_PTHREAD_SIGMASK]) HAVE_RAISE=1; AC_SUBST([HAVE_RAISE]) + HAVE_SIG2STR=1; AC_SUBST([HAVE_SIG2STR]) HAVE_SIGSET_T=1; AC_SUBST([HAVE_SIGSET_T]) HAVE_SIGINFO_T=1; AC_SUBST([HAVE_SIGINFO_T]) HAVE_SIGACTION=1; AC_SUBST([HAVE_SIGACTION]) + HAVE_STR2SIG=1; AC_SUBST([HAVE_STR2SIG]) HAVE_STRUCT_SIGACTION_SA_SIGACTION=1; AC_SUBST([HAVE_STRUCT_SIGACTION_SA_SIGACTION]) HAVE_TYPE_VOLATILE_SIG_ATOMIC_T=1; diff --git a/modules/sig2str b/modules/sig2str index cb26add2d0..8055a58ff1 100644 --- a/modules/sig2str +++ b/modules/sig2str @@ -7,14 +7,16 @@ lib/sig2str.c m4/sig2str.m4 Depends-on: -intprops +signal-h configure.ac: gl_FUNC_SIG2STR -gl_CONDITIONAL([GL_COND_OBJ_SIG2STR], [test $ac_cv_func_sig2str = no]) +gl_CONDITIONAL([GL_COND_OBJ_SIG2STR], + [test $HAVE_SIG2STR = 0 || test $HAVE_STR2SIG = 0]) AM_COND_IF([GL_COND_OBJ_SIG2STR], [ gl_PREREQ_SIG2STR ]) +gl_SIGNAL_MODULE_INDICATOR([sig2str]) Makefile.am: if GL_COND_OBJ_SIG2STR @@ -22,7 +24,8 @@ lib_SOURCES += sig2str.c endif Include: -"sig2str.h" +<signal.h> +"sig2str.h" /* for SIGNUM_BOUND */ License: GPL diff --git a/modules/signal-h b/modules/signal-h index be4cf1991b..15d8adee72 100644 --- a/modules/signal-h +++ b/modules/signal-h @@ -34,15 +34,18 @@ signal.h: signal.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H -e 's|@''NEXT_SIGNAL_H''@|$(NEXT_SIGNAL_H)|g' \ -e 's/@''GNULIB_PTHREAD_SIGMASK''@/$(GNULIB_PTHREAD_SIGMASK)/g' \ -e 's/@''GNULIB_RAISE''@/$(GNULIB_RAISE)/g' \ + -e 's/@''GNULIB_SIG2STR''@/$(GNULIB_SIG2STR)/g' \ -e 's/@''GNULIB_SIGNAL_H_SIGPIPE''@/$(GNULIB_SIGNAL_H_SIGPIPE)/g' \ -e 's/@''GNULIB_SIGPROCMASK''@/$(GNULIB_SIGPROCMASK)/g' \ -e 's/@''GNULIB_SIGACTION''@/$(GNULIB_SIGACTION)/g' \ -e 's|@''HAVE_POSIX_SIGNALBLOCKING''@|$(HAVE_POSIX_SIGNALBLOCKING)|g' \ -e 's|@''HAVE_PTHREAD_SIGMASK''@|$(HAVE_PTHREAD_SIGMASK)|g' \ -e 's|@''HAVE_RAISE''@|$(HAVE_RAISE)|g' \ + -e 's|@''HAVE_SIG2STR''@|$(HAVE_SIG2STR)|g' \ -e 's|@''HAVE_SIGSET_T''@|$(HAVE_SIGSET_T)|g' \ -e 's|@''HAVE_SIGINFO_T''@|$(HAVE_SIGINFO_T)|g' \ -e 's|@''HAVE_SIGACTION''@|$(HAVE_SIGACTION)|g' \ + -e 's|@''HAVE_STR2SIG''@|$(HAVE_STR2SIG)|g' \ -e 's|@''HAVE_STRUCT_SIGACTION_SA_SIGACTION''@|$(HAVE_STRUCT_SIGACTION_SA_SIGACTION)|g' \ -e 's|@''HAVE_TYPE_VOLATILE_SIG_ATOMIC_T''@|$(HAVE_TYPE_VOLATILE_SIG_ATOMIC_T)|g' \ -e 's|@''HAVE_SIGHANDLER_T''@|$(HAVE_SIGHANDLER_T)|g' \