string: Add sigabbrev_np and sigdescr_np

The sigabbrev_np returns the abbreviated signal name (e.g. "HUP" for
SIGHUP) while sigdescr_np returns the string describing the error
number (e.g "Hangup" for SIGHUP).  Different than strsignal,
sigdescr_np does not attempt to translate the return description and
both functions return NULL for an invalid signal number.

They should be used instead of sys_siglist or sys_sigabbrev and they
are both thread and async-signal safe.  They are added as GNU
extensions on string.h header (same as strsignal).

Checked on x86-64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
and s390x-linux-gnu.

Tested-by: Carlos O'Donell <carlos@redhat.com>
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
This commit is contained in:
Adhemerval Zanella 2020-05-18 17:05:05 -03:00
parent 4f92497488
commit bfe05aa289
43 changed files with 236 additions and 10 deletions

10
NEWS
View File

@ -47,6 +47,16 @@ Major new features:
instead of weak references to symbols historically defined in
libpthread.
* The functions sigabbrev_np and sigdescr_np have been added. The
sigabbrev_np returns the abbreviated signal name (e.g. "HUP" for SIGHUP)
while sigdescr_np returns a string describing the signal number (e.g
"Hangup" for SIGHUP). Different than strsignal, sigdescr_np does not
attempt to translate the return description, both functions return
NULL for an invalid signal number.
They should be used instead of sys_siglist or sys_sigabbrev and they
are both thread and async-signal safe. These functions are GNU extensions.
Deprecated and removed features, and other changes affecting compatibility:
* The deprecated <sys/sysctl.h> header and the sysctl function have been

View File

@ -16,7 +16,8 @@ libc_hidden_proto (__libc_current_sigrtmin)
libc_hidden_proto (__libc_current_sigrtmax)
extern const char * const __sys_siglist[_NSIG];
libc_hidden_proto (__sys_siglist)
extern const char * const __sys_sigabbrev[_NSIG];
libc_hidden_proto (__sys_sigabbrev)
/* Now define the internal interfaces. */
extern __sighandler_t __bsd_signal (int __sig, __sighandler_t __handler);

View File

@ -53,6 +53,9 @@ extern char *__strerror_r (int __errnum, char *__buf, size_t __buflen);
extern char *__strerror_l (int __errnum, locale_t __loc);
extern const char *__sigdescr_np (int __errnum);
libc_hidden_proto (__sigdescr_np)
/* Get _STRING_ARCH_unaligned. */
#include <string_private.h>
#endif

View File

@ -880,6 +880,30 @@ to @var{signum}.
This function is a BSD feature, declared in the header file @file{signal.h}.
@end deftypefun
@deftypefun void sigdescr_np (int @var{signum})
@standards{GNU, string.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
This function returns the message describing the signal @var{signum} or
@code{NULL} for invalid signal number (e.g "Hangup" for @code{SIGHUP}).
Different than @code{strsignal} the returned description is not translated.
The message points to a static storage whose lifetime is the whole lifetime
of the program.
@pindex string.h
This function is a GNU extension, declared in the header file @file{string.h}.
@end deftypefun
@deftypefun void sigabbrev_np (int @var{signum})
@standards{GNU, string.h}
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
This function returns the abbreviation describing the signal @var{signum} or
@code{NULL} for invalid signal number. The message points to a static
storage whose lifetime is the whole lifetime of the program.
@pindex string.h
This function is a GNU extension, declared in the header file @file{string.h}.
@end deftypefun
@node Signal Actions
@section Specifying Signal Actions
@cindex signal actions

View File

@ -34,5 +34,6 @@ const char *const __sys_sigabbrev[NSIG] =
#include <siglist.h>
#undef init_sig
};
libc_hidden_def (__sys_sigabbrev)
#include <siglist-compat.c>

View File

@ -44,7 +44,8 @@ routines := strcat strchr strcmp strcoll strcpy strcspn \
addsep replace) \
envz basename \
strcoll_l strxfrm_l string-inlines memrchr \
xpg-strerror strerror_l explicit_bzero
xpg-strerror strerror_l explicit_bzero \
sigdescr_np sigabbrev_np
strop-tests := memchr memcmp memcpy memmove mempcpy memset memccpy \
stpcpy stpncpy strcat strchr strcmp strcpy strcspn \
@ -61,7 +62,7 @@ tests := tester inl-tester noinl-tester testcopy test-ffs \
tst-strtok_r bug-strcoll2 tst-cmp tst-xbzero-opt \
test-endian-types test-endian-file-scope \
test-endian-sign-conversion tst-memmove-overflow \
tst-strsignal tst-strerror
tst-strsignal tst-strerror test-sig_np
# This test allocates a lot of memory and can run for a long time.
xtests = tst-strcoll-overflow

View File

@ -85,4 +85,7 @@ libc {
GLIBC_2.25 {
explicit_bzero;
}
GLIBC_2.32 {
sigdescr_np; sigabbrev_np;
}
}

33
string/sigabbrev_np.c Normal file
View File

@ -0,0 +1,33 @@
/* Return string describing signal abbreviation.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <string.h>
#include <signal.h>
#include <array_length.h>
const char *const
sigabbrev_np (int signum)
{
const char *abbrev = NULL;
if (signum >= 0 && signum <= NSIG
&& signum < array_length (__sys_sigabbrev))
abbrev = __sys_sigabbrev[signum];
return abbrev;
}

34
string/sigdescr_np.c Normal file
View File

@ -0,0 +1,34 @@
/* Return string describing signal.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <string.h>
#include <signal.h>
#include <array_length.h>
const char *const
__sigdescr_np (int signum)
{
const char *descr = NULL;
if (signum >= 0 && signum <= NSIG && signum < array_length (__sys_siglist))
descr = __sys_siglist[signum];
return descr;
}
libc_hidden_def (__sigdescr_np)
weak_alias (__sigdescr_np, sigdescr_np)

View File

@ -454,6 +454,14 @@ extern char *strsep (char **__restrict __stringp,
/* Return a string describing the meaning of the signal number in SIG. */
extern char *strsignal (int __sig) __THROW;
# ifdef __USE_GNU
/* Return an abbreviation string for the signal number SIG. */
extern const char *sigabbrev_np (int __sig) __THROW;
/* Return a string describing the meaning of the signal number in SIG,
the result is not translated. */
extern const char *sigdescr_np (int __sig) __THROW;
# endif
/* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
extern char *__stpcpy (char *__restrict __dest, const char *__restrict __src)
__THROW __nonnull ((1, 2));

View File

@ -21,19 +21,14 @@
#include <string.h>
#include <libintl.h>
#include <tls-internal.h>
#include <array_length.h>
/* Return a string describing the meaning of the signal number SIGNUM. */
char *
strsignal (int signum)
{
const char *desc = NULL;
if (signum >= 0 && signum <= NSIG && signum < array_length (__sys_siglist))
desc = __sys_siglist[signum];
const char *desc = __sigdescr_np (signum);
if (desc != NULL)
return (char *) _(desc);
return _(desc);
struct tls_internal_t *tls_internal = __glibc_tls_internal ();
free (tls_internal->strsignal_buf);

51
string/test-sig_np.c Normal file
View File

@ -0,0 +1,51 @@
/* Test and sigabbrev_np and sigdescr_np.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <string.h>
#include <signal.h>
#include <array_length.h>
#include <support/support.h>
#include <support/check.h>
static const struct test_t
{
int errno;
const char *abbrev;
const char *descr;
} tests[] =
{
#define N_(name) name
#define init_sig(sig, abbrev, desc) { sig, abbrev, desc },
#include <siglist.h>
#undef init_sig
};
static int
do_test (void)
{
for (size_t i = 0; i < array_length (tests); i++)
{
TEST_COMPARE_STRING (sigabbrev_np (tests[i].errno), tests[i].abbrev);
TEST_COMPARE_STRING (sigdescr_np (tests[i].errno), tests[i].descr);
}
return 0;
}
#include <support/test-driver.c>

View File

@ -2184,6 +2184,8 @@ GLIBC_2.30 twalk_r F
GLIBC_2.32 __libc_single_threaded D 0x1
GLIBC_2.32 mach_print F
GLIBC_2.32 mremap F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.32 thrd_current F
GLIBC_2.32 thrd_equal F
GLIBC_2.32 thrd_sleep F

View File

@ -2157,3 +2157,5 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F

View File

@ -2239,6 +2239,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -141,6 +141,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
GLIBC_2.4 _IO_2_1_stdin_ D 0xa0

View File

@ -138,6 +138,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
GLIBC_2.4 _IO_2_1_stdin_ D 0xa0

View File

@ -2101,3 +2101,5 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F

View File

@ -2060,6 +2060,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2226,6 +2226,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2092,6 +2092,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -142,6 +142,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0x98
GLIBC_2.4 _IO_2_1_stdin_ D 0x98

View File

@ -2172,6 +2172,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2152,3 +2152,5 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F

View File

@ -2149,3 +2149,5 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F

View File

@ -2143,6 +2143,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2141,6 +2141,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2149,6 +2149,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2143,6 +2143,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2190,3 +2190,5 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F

View File

@ -2199,6 +2199,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2232,6 +2232,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2062,6 +2062,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2352,3 +2352,5 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F

View File

@ -2119,3 +2119,5 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F

View File

@ -2197,6 +2197,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2098,6 +2098,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2067,6 +2067,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2064,6 +2064,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2188,6 +2188,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F

View File

@ -2115,6 +2115,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2073,6 +2073,8 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F

View File

@ -2170,3 +2170,5 @@ GLIBC_2.32 pthread_attr_setsigmask_np F
GLIBC_2.32 pthread_getaffinity_np F
GLIBC_2.32 pthread_getattr_np F
GLIBC_2.32 pthread_sigmask F
GLIBC_2.32 sigabbrev_np F
GLIBC_2.32 sigdescr_np F