Add the new signalname/signalnext/signalnumber interface to libc.

This as discussed on current-users in the thread
entitled:
  Proposal: new libc/libutil functions to map SIGXXXX <-> "XXXX"
that can be found (starting at):
  http://mail-index.netbsd.org/current-users/2017/04/28/msg031600.html

These functions provide the mechanism to enable applications
to divorce themselves from internal details of the signal
implementation.

Libc minor bumped, prototypes in <signal.h>, sets lists updated (and sorted).

One and all: feel free to improve the sources & man page (etc), but
please do not change the function signatures without discussion.
This commit is contained in:
kre 2017-05-09 11:14:16 +00:00
parent d531aa96de
commit 486691905e
10 changed files with 309 additions and 17 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: shl.mi,v 1.812 2017/05/06 19:54:41 christos Exp $ # $NetBSD: shl.mi,v 1.813 2017/05/09 11:14:16 kre Exp $
# #
# Note: Don't delete entries from here - mark them as "obsolete" instead, # Note: Don't delete entries from here - mark them as "obsolete" instead,
# unless otherwise stated below. # unless otherwise stated below.
@ -18,7 +18,7 @@
./lib/libblacklist.so.0.0 base-sys-shlib dynamicroot ./lib/libblacklist.so.0.0 base-sys-shlib dynamicroot
./lib/libc.so base-sys-shlib dynamicroot ./lib/libc.so base-sys-shlib dynamicroot
./lib/libc.so.12 base-sys-shlib dynamicroot ./lib/libc.so.12 base-sys-shlib dynamicroot
./lib/libc.so.12.206 base-sys-shlib dynamicroot ./lib/libc.so.12.207 base-sys-shlib dynamicroot
./lib/libcrypt.so base-sys-shlib dynamicroot ./lib/libcrypt.so base-sys-shlib dynamicroot
./lib/libcrypt.so.1 base-sys-shlib dynamicroot ./lib/libcrypt.so.1 base-sys-shlib dynamicroot
./lib/libcrypt.so.1.0 base-sys-shlib dynamicroot ./lib/libcrypt.so.1.0 base-sys-shlib dynamicroot
@ -214,7 +214,7 @@
./usr/lib/libc++.so.1.0 base-sys-shlib compatfile,libcxx ./usr/lib/libc++.so.1.0 base-sys-shlib compatfile,libcxx
./usr/lib/libc.so base-sys-shlib compatfile ./usr/lib/libc.so base-sys-shlib compatfile
./usr/lib/libc.so.12 base-sys-shlib compatfile ./usr/lib/libc.so.12 base-sys-shlib compatfile
./usr/lib/libc.so.12.206 base-sys-shlib compatfile ./usr/lib/libc.so.12.207 base-sys-shlib compatfile
./usr/lib/libcdk.so base-obsolete compatfile,obsolete ./usr/lib/libcdk.so base-obsolete compatfile,obsolete
./usr/lib/libcom_err.so base-krb5-shlib compatfile,kerberos ./usr/lib/libcom_err.so base-krb5-shlib compatfile,kerberos
./usr/lib/libcom_err.so.8 base-krb5-shlib compatfile,kerberos ./usr/lib/libcom_err.so.8 base-krb5-shlib compatfile,kerberos

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.2130 2017/05/01 23:33:07 pgoyette Exp $ # $NetBSD: mi,v 1.2131 2017/05/09 11:14:16 kre Exp $
# #
# Note: don't delete entries from here - mark them as "obsolete" instead. # Note: don't delete entries from here - mark them as "obsolete" instead.
./etc/mtree/set.comp comp-sys-root ./etc/mtree/set.comp comp-sys-root
@ -3455,10 +3455,10 @@
./usr/lib/librumpdev_scsitest_p.a comp-obsolete obsolete ./usr/lib/librumpdev_scsitest_p.a comp-obsolete obsolete
./usr/lib/librumpdev_sysmon.a comp-c-lib rump ./usr/lib/librumpdev_sysmon.a comp-c-lib rump
./usr/lib/librumpdev_sysmon_p.a comp-c-proflib rump,profile ./usr/lib/librumpdev_sysmon_p.a comp-c-proflib rump,profile
./usr/lib/librumpdev_ubt.a comp-c-lib rump
./usr/lib/librumpdev_ubt_p.a comp-c-proflib rump,profile
./usr/lib/librumpdev_ualea.a comp-c-lib rump ./usr/lib/librumpdev_ualea.a comp-c-lib rump
./usr/lib/librumpdev_ualea_p.a comp-c-proflib rump,profile ./usr/lib/librumpdev_ualea_p.a comp-c-proflib rump,profile
./usr/lib/librumpdev_ubt.a comp-c-lib rump
./usr/lib/librumpdev_ubt_p.a comp-c-proflib rump,profile
./usr/lib/librumpdev_ucom.a comp-c-lib rump ./usr/lib/librumpdev_ucom.a comp-c-lib rump
./usr/lib/librumpdev_ucom_p.a comp-c-proflib rump,profile ./usr/lib/librumpdev_ucom_p.a comp-c-proflib rump,profile
./usr/lib/librumpdev_ugenhc.a comp-c-lib rump ./usr/lib/librumpdev_ugenhc.a comp-c-lib rump
@ -16633,6 +16633,9 @@
./usr/share/man/html3/signal_initialized.html comp-obsolete obsolete ./usr/share/man/html3/signal_initialized.html comp-obsolete obsolete
./usr/share/man/html3/signal_pending.html comp-obsolete obsolete ./usr/share/man/html3/signal_pending.html comp-obsolete obsolete
./usr/share/man/html3/signal_set.html comp-obsolete obsolete ./usr/share/man/html3/signal_set.html comp-obsolete obsolete
./usr/share/man/html3/signalname.html comp-c-htmlman html
./usr/share/man/html3/signalnext.html comp-c-htmlman html
./usr/share/man/html3/signalnumber.html comp-c-htmlman html
./usr/share/man/html3/signbit.html comp-c-htmlman html ./usr/share/man/html3/signbit.html comp-c-htmlman html
./usr/share/man/html3/significand.html comp-c-htmlman html ./usr/share/man/html3/significand.html comp-c-htmlman html
./usr/share/man/html3/significandf.html comp-c-htmlman html ./usr/share/man/html3/significandf.html comp-c-htmlman html
@ -24159,6 +24162,9 @@
./usr/share/man/man3/signal_initialized.3 comp-obsolete obsolete ./usr/share/man/man3/signal_initialized.3 comp-obsolete obsolete
./usr/share/man/man3/signal_pending.3 comp-obsolete obsolete ./usr/share/man/man3/signal_pending.3 comp-obsolete obsolete
./usr/share/man/man3/signal_set.3 comp-obsolete obsolete ./usr/share/man/man3/signal_set.3 comp-obsolete obsolete
./usr/share/man/man3/signalname.3 comp-c-man .man
./usr/share/man/man3/signalnext.3 comp-c-man .man
./usr/share/man/man3/signalnumber.3 comp-c-man .man
./usr/share/man/man3/signbit.3 comp-c-man .man ./usr/share/man/man3/signbit.3 comp-c-man .man
./usr/share/man/man3/significand.3 comp-c-man .man ./usr/share/man/man3/significand.3 comp-c-man .man
./usr/share/man/man3/significandf.3 comp-c-man .man ./usr/share/man/man3/significandf.3 comp-c-man .man

View File

@ -1,8 +1,8 @@
# $NetBSD: shl.mi,v 1.171 2017/05/07 00:43:01 htodd Exp $ # $NetBSD: shl.mi,v 1.172 2017/05/09 11:14:16 kre Exp $
./usr/lib/libbfd_g.a comp-c-debuglib debuglib,compatfile,binutils ./usr/lib/libbfd_g.a comp-c-debuglib debuglib,compatfile,binutils
./usr/libdata/debug/lib base-sys-usr debug,dynamicroot,compatdir ./usr/libdata/debug/lib base-sys-usr debug,dynamicroot,compatdir
./usr/libdata/debug/lib/libblacklist.so.0.0.debug comp-sys-debug debug,dynamicroot ./usr/libdata/debug/lib/libblacklist.so.0.0.debug comp-sys-debug debug,dynamicroot
./usr/libdata/debug/lib/libc.so.12.206.debug comp-sys-debug debug,dynamicroot ./usr/libdata/debug/lib/libc.so.12.207.debug comp-sys-debug debug,dynamicroot
./usr/libdata/debug/lib/libcrypt.so.1.0.debug comp-sys-debug debug,dynamicroot ./usr/libdata/debug/lib/libcrypt.so.1.0.debug comp-sys-debug debug,dynamicroot
./usr/libdata/debug/lib/libcrypto.so.12.0.debug comp-sys-debug debug,dynamicroot,crypto ./usr/libdata/debug/lib/libcrypto.so.12.0.debug comp-sys-debug debug,dynamicroot,crypto
./usr/libdata/debug/lib/libdevmapper.so.1.0.debug comp-sys-debug debug,dynamicroot,lvm ./usr/libdata/debug/lib/libdevmapper.so.1.0.debug comp-sys-debug debug,dynamicroot,lvm
@ -70,7 +70,7 @@
./usr/libdata/debug/usr/lib/libbsdmalloc.so.0.0.debug comp-sys-debug debug,compatfile ./usr/libdata/debug/usr/lib/libbsdmalloc.so.0.0.debug comp-sys-debug debug,compatfile
./usr/libdata/debug/usr/lib/libbz2.so.1.1.debug comp-sys-debug debug,compatfile ./usr/libdata/debug/usr/lib/libbz2.so.1.1.debug comp-sys-debug debug,compatfile
./usr/libdata/debug/usr/lib/libc++.so.1.0.debug comp-sys-debug debug,compatfile,libcxx ./usr/libdata/debug/usr/lib/libc++.so.1.0.debug comp-sys-debug debug,compatfile,libcxx
./usr/libdata/debug/usr/lib/libc.so.12.206.debug comp-sys-debug debug,compatfile ./usr/libdata/debug/usr/lib/libc.so.12.207.debug comp-sys-debug debug,compatfile
./usr/libdata/debug/usr/lib/libcom_err.so.8.0.debug comp-krb5-debug debug,compatfile,kerberos ./usr/libdata/debug/usr/lib/libcom_err.so.8.0.debug comp-krb5-debug debug,compatfile,kerberos
./usr/libdata/debug/usr/lib/libcrypt.so.1.0.debug comp-sys-debug debug,compatfile ./usr/libdata/debug/usr/lib/libcrypt.so.1.0.debug comp-sys-debug debug,compatfile
./usr/libdata/debug/usr/lib/libcrypto.so.12.0.debug comp-crypto-debug debug,compatfile,crypto ./usr/libdata/debug/usr/lib/libcrypto.so.12.0.debug comp-crypto-debug debug,compatfile,crypto

View File

@ -1,4 +1,4 @@
/* $NetBSD: signal.h,v 1.55 2015/07/31 12:51:32 kamil Exp $ */ /* $NetBSD: signal.h,v 1.56 2017/05/09 11:14:16 kre Exp $ */
/*- /*-
* Copyright (c) 1991, 1993 * Copyright (c) 1991, 1993
@ -56,6 +56,13 @@ extern const int sys_nsig __RENAME(__sys_nsig14);
__BEGIN_DECLS __BEGIN_DECLS
int raise(int); int raise(int);
#if defined(_NETBSD_SOURCE)
const char *signalname(int);
int signalnext(int);
int signalnumber(const char *);
#endif
#if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) || \ #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) || \
defined(_NETBSD_SOURCE) defined(_NETBSD_SOURCE)
int kill(pid_t, int); int kill(pid_t, int);

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile.inc,v 1.198 2017/02/07 19:29:40 kamil Exp $ # $NetBSD: Makefile.inc,v 1.199 2017/05/09 11:14:16 kre Exp $
# from: @(#)Makefile.inc 8.6 (Berkeley) 5/4/95 # from: @(#)Makefile.inc 8.6 (Berkeley) 5/4/95
# gen sources # gen sources
@ -29,8 +29,8 @@ SRCS+= alarm.c alphasort.c arc4random.c assert.c asysctl.c \
rewinddir.c scandir.c seekdir.c setdomainname.c \ rewinddir.c scandir.c seekdir.c setdomainname.c \
sethostname.c setjmperr.c setmode.c setproctitle.c setprogname.c \ sethostname.c setjmperr.c setmode.c setproctitle.c setprogname.c \
shquote.c shquotev.c sighold.c sigignore.c siginterrupt.c \ shquote.c shquotev.c sighold.c sigignore.c siginterrupt.c \
siglist.c signal.c signame.c sigrelse.c \ siglist.c signal.c signalname.c signalnext.c signalnumber.c \
sigset.c sigsetops.c sleep.c \ signame.c sigrelse.c sigset.c sigsetops.c sleep.c \
stringlist.c sysconf.c sysctl.c sysctlbyname.c sysctlgetmibinfo.c \ stringlist.c sysconf.c sysctl.c sysctlbyname.c sysctlgetmibinfo.c \
sysctlnametomib.c syslog.c syslog_ss.c telldir.c time.c timespec_get.c \ sysctlnametomib.c syslog.c syslog_ss.c telldir.c time.c timespec_get.c \
times.c toascii.c tolower_.c ttyname.c ttyslot.c toupper_.c ualarm.c \ times.c toascii.c tolower_.c ttyname.c ttyslot.c toupper_.c ualarm.c \
@ -79,8 +79,8 @@ MAN+= alarm.3 arc4random.3 basename.3 bsd_signal.3 bswap.3 \
psignal.3 pwcache.3 pthread_atfork.3 \ psignal.3 pwcache.3 pthread_atfork.3 \
raise.3 randomid.3 realpath.3 scandir.3 setjmp.3 setmode.3 \ raise.3 randomid.3 realpath.3 scandir.3 setjmp.3 setmode.3 \
setproctitle.3 shquote.3 sighold.3 sigignore.3 siginterrupt.3 \ setproctitle.3 shquote.3 sighold.3 sigignore.3 siginterrupt.3 \
signal.3 signbit.3 sigrelse.3 sigset.3 sigsetops.3 sleep.3 \ signal.3 signalname.3 signbit.3 sigrelse.3 sigset.3 sigsetops.3 \
stringlist.3 sysconf.3 sysctl.3 syslog.3 \ sleep.3 stringlist.3 sysconf.3 sysctl.3 syslog.3 \
time.3 timespec_get.3 times.3 timezone.3 toascii.3 tolower.3 \ time.3 timespec_get.3 times.3 timezone.3 toascii.3 tolower.3 \
toupper.3 ttyname.3 \ toupper.3 ttyname.3 \
ualarm.3 ulimit.3 uname.3 unvis.3 usleep.3 utime.3 valloc.3 vis.3 \ ualarm.3 ulimit.3 uname.3 unvis.3 usleep.3 utime.3 valloc.3 vis.3 \
@ -171,6 +171,7 @@ MLINKS+=setjmp.3 _longjmp.3 setjmp.3 _setjmp.3 setjmp.3 longjmp.3 \
setjmp.3 longjmperror.3 setjmp.3 sigsetjmp.3 setjmp.3 siglongjmp.3 setjmp.3 longjmperror.3 setjmp.3 sigsetjmp.3 setjmp.3 siglongjmp.3
MLINKS+=setmode.3 getmode.3 MLINKS+=setmode.3 getmode.3
MLINKS+=shquote.3 shquotev.3 MLINKS+=shquote.3 shquotev.3
MLINKS+=signalname.3 signalnext.3 signalname.3 signalnumber.3
MLINKS+=sigsetops.3 sigemptyset.3 sigsetops.3 sigfillset.3 \ MLINKS+=sigsetops.3 sigemptyset.3 sigsetops.3 sigfillset.3 \
sigsetops.3 sigaddset.3 sigsetops.3 sigdelset.3 \ sigsetops.3 sigaddset.3 sigsetops.3 sigdelset.3 \
sigsetops.3 sigismember.3 sigsetops.3 sigismember.3

133
lib/libc/gen/signalname.3 Normal file
View File

@ -0,0 +1,133 @@
.\" $NetBSD: signalname.3,v 1.1 2017/05/09 11:14:16 kre Exp $
.\"
.\" Available to all and sundry, without restriction on use, or other
.\" limitations, and without fee. Also without any warranty of fitness
.\" for any purpose whatever.
.\"
.\" Licensed for any use, including redistribution in source
.\" and binary forms, with or without modifications, subject
.\" the following agreement:
.\"
.\" Licensee agrees to indemnify licensor, and distributor, for
.\" the full amount of any any claim made by the licensee against
.\" the licensor or distributor, for any action that results from
.\" any use or redistribution of this software, plus any costs
.\" incurred by licensor or distributor resulting from that claim.
.\"
.\" This licence must be retained with the software.
.\"
.Dd April 28, 2017
.Dt SIGNALNAME 3
.Os
.Sh NAME
.Nm signalname
.Nm signalnumber
.Nm signalnext
.Nd convert between signal numbers and names
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In signal.h
.Ft const char *
.Fn signalname "int sig"
.Ft int
.Fn signalnumber "const char *name"
.Ft int
.Fn signalnext "int sig"
.Sh DESCRIPTION
The
.Fn signalname
function takes a signal number
.Fa sig ,
and returns the name of that signal.
The name returned is locale independent,
and can be the string representation of one of the
signal names from
.In signal.h
such as SIGHUP, SIGSTOP, SIGKILL,
or some similar name,
but does not contain the leading
.Dq SIG
prefix.
.Pp
The return value of
.Fn signalname
is NULL if
.Fa sig
does not represent a valid signal number,
or if the signal number given has no name.
.Pp
The
.Fn signalnumber
function converts the signal name
.Fa name
to the number corresponding to that signal.
The
.Fa name
is handled in a case-insensitive manner.
Any leading
.Dq SIG
prefix in
.Fa name
is ignored.
.Pp
The
.Fn signalnumber
function returns the signal number,
or zero (0) if the name given does not represent a valid signal.
.Pp
The
.Fn signalnext
function takes a signal number, and returns the number of the
next available bigger signal number.
When no higher signal numbers remain, it returns zero (0).
The parameter
.Fa sig
can be given as zero (0), to obtain the smallest implemented
signal number.
.Pp
The
.Fn signalnext
function returns minus one (\-1) on error, if the given signal
.Fa sig
is neither a valid signal number, nor zero.
It returns zero when the input signal number,
.Fa sig ,
is the biggest available signal number.
Otherwise it returns the signal number of an implemented
signal that is larger than
.Fa sig
and such that there are no implemented signals with values
between
.Fa sig
and the value returned.
.Pp
The
.Fn signalnext
function can also be used to determine if a non-zero signal
number is valid or not (0 is always invalid, but cannot be
detected as such this way.)
Given the non-zero signal number to check as
.Fa sig ,
if
.Fn signalnext
returns anything other than minus one (\-1)
then
.Fa sig
represents a valid signal number.
If the return value is \-1 then
.Fa sig
is invalid.
.Sh SEE ALSO
.Xr kill 1 ,
.Xr intro 2 ,
.Xr psignal 3 ,
.Xr strsignal 3
.Sh HISTORY
The
.Fn signalname ,
.Fn signalnext
and
.Fn signalnumber
functions first appeared in
.Nx 8.0 .

44
lib/libc/gen/signalname.c Normal file
View File

@ -0,0 +1,44 @@
/* $NetBSD: signalname.c,v 1.1 2017/05/09 11:14:16 kre Exp $ */
/*
* Software available to all and sundry without limitations
* but without warranty of fitness for any purpose whatever.
*
* Licensed for any use, including redistribution in source
* and binary forms, with or without modifications, subject
* the following agreement:
*
* Licensee agrees to indemnify licensor, and distributor, for
* the full amount of any any claim made by the licensee against
* the licensor or distributor, for any action that results from
* any use or redistribution of this software, plus any costs
* incurred by licensor or distributor resulting from that claim.
*
* This licence must be retained with the software.
*/
#include <sys/types.h>
#include <signal.h>
#include <string.h>
/*
* signalname()
*
* Converts the signal number "sig" to its
* signal name (without the "SIG" prefix).
*
* Returns:
* NULL on error (invalid signal number)
* otherwise the (abbreviated) signal name (no "SIG").
*/
const char *
signalname(int sig)
{
if (sig <= 0 || sig >= NSIG)
return NULL;
return sys_signame[sig];
}

49
lib/libc/gen/signalnext.c Normal file
View File

@ -0,0 +1,49 @@
/* $NetBSD: signalnext.c,v 1.1 2017/05/09 11:14:16 kre Exp $ */
/*
* Software available to all and sundry without limitations
* but without warranty of fitness for any purpose whatever.
*
* Licensed for any use, including redistribution in source
* and binary forms, with or without modifications, subject
* the following agreement:
*
* Licensee agrees to indemnify licensor, and distributor, for
* the full amount of any any claim made by the licensee against
* the licensor or distributor, for any action that results from
* any use or redistribution of this software, plus any costs
* incurred by licensor or distributor resulting from that claim.
*
* This licence must be retained with the software.
*/
#include <sys/types.h>
#include <signal.h>
#include <string.h>
/*
* signalnext()
*
* Returns the next signal number after the one given.
* Giving 0 as 'sig' requests the lowest available signal number.
*
* Returns:
* -1 on error (invalid 'sig' arg)
* 0 when there is no next.
* otherwise the next greater implemented signal number after 'sig'
*/
int
signalnext(int sig)
{
if (sig < 0 || sig >= NSIG || (sig > 0 && sys_signame[sig] == NULL))
return -1;
while (++sig < NSIG)
if (sys_signame[sig] != NULL)
return sig;
return 0;
}

View File

@ -0,0 +1,52 @@
/* $NetBSD: signalnumber.c,v 1.1 2017/05/09 11:14:16 kre Exp $ */
/*
* Software available to all and sundry without limitations
* but without warranty of fitness for any purpose whatever.
*
* Licensed for any use, including redistribution in source
* and binary forms, with or without modifications, subject
* the following agreement:
*
* Licensee agrees to indemnify licensor, and distributor, for
* the full amount of any any claim made by the licensee against
* the licensor or distributor, for any action that results from
* any use or redistribution of this software, plus any costs
* incurred by licensor or distributor resulting from that claim.
*
* This licence must be retained with the software.
*/
#include <signal.h>
#include <string.h>
/*
* signalnumber()
*
* Converts the signal named "name" to its
* signal number.
*
* "name" can be the signal name with or without
* the leading "SIG" prefix, and character comparisons
* are done in a case independent manner.
* (ie: SIGINT == INT == int == Int == SiGiNt == (etc).)
*
* Returns:
* 0 on error (invalid signal name)
* otherwise the signal number that corresponds to "name"
*/
int
signalnumber(const char *name)
{
int i;
if (strncasecmp(name, "sig", 3) == 0)
name += 3;
for (i = 1; i < NSIG; ++i)
if (sys_signame[i] != NULL &&
strcasecmp(name, sys_signame[i]) == 0)
return i;
return 0;
}

View File

@ -1,4 +1,4 @@
# $NetBSD: shlib_version,v 1.272 2017/03/15 23:23:15 dholland Exp $ # $NetBSD: shlib_version,v 1.273 2017/05/09 11:14:16 kre Exp $
# Remember to update distrib/sets/lists/base/shl.* when changing # Remember to update distrib/sets/lists/base/shl.* when changing
# #
# things we wish to do on next major version bump: # things we wish to do on next major version bump:
@ -48,4 +48,4 @@
# - remove exect(3) API - tracing functionality has been moved to the kernel # - remove exect(3) API - tracing functionality has been moved to the kernel
# - punt gethostid/sethostid(3) to libcompat or remove entirely # - punt gethostid/sethostid(3) to libcompat or remove entirely
major=12 major=12
minor=206 minor=207