sig: Move runtime signal count check to compile time

Since the value of number_of_signals is known at compile time, we can
use a compile-time check instead. This also adds SIGLOST for the Hurd,
uses the correct signal counts for the Hurd and FreeBSD, and only gives
a compile-time warning when compiled on an unknown platform that it does
not know whether the number of signals is correct.
This commit is contained in:
James Clarke 2017-10-13 17:09:47 +01:00 committed by Craig Small
parent 3e6a208ae5
commit bd72ba3a4b

View File

@ -34,8 +34,10 @@
* You get SIGSTKFLT and SIGUNUSED instead on i386, m68k, ppc, and arm. * You get SIGSTKFLT and SIGUNUSED instead on i386, m68k, ppc, and arm.
* (this is a Linux & libc bug -- both must be fixed) * (this is a Linux & libc bug -- both must be fixed)
* *
* Total garbage: SIGIO SIGINFO SIGIOT SIGLOST SIGCLD * Total garbage: SIGIO SIGINFO SIGIOT SIGCLD
* (popular ones are handled as aliases) * (popular ones are handled as aliases)
* SIGLOST
* (except on the Hurd; reused to mean a server died)
* Nearly garbage: SIGSTKFLT SIGUNUSED (nothing else to fill slots) * Nearly garbage: SIGSTKFLT SIGUNUSED (nothing else to fill slots)
*/ */
@ -50,6 +52,10 @@
# undef SIGSTKFLT # undef SIGSTKFLT
#endif #endif
#if !defined(__GNU__) && defined(SIGLOST)
# undef SIGLOST
#endif
#ifndef SIGRTMIN #ifndef SIGRTMIN
# warning Standards require that <signal.h> define SIGRTMIN; assuming 32 # warning Standards require that <signal.h> define SIGRTMIN; assuming 32
# define SIGRTMIN 32 # define SIGRTMIN 32
@ -81,6 +87,9 @@ static const mapstruct sigtable[] = {
{"ILL", SIGILL}, {"ILL", SIGILL},
{"INT", SIGINT}, {"INT", SIGINT},
{"KILL", SIGKILL}, {"KILL", SIGKILL},
#ifdef SIGLOST
{"LOST", SIGLOST}, /* Hurd-specific */
#endif
{"PIPE", SIGPIPE}, {"PIPE", SIGPIPE},
{"POLL", SIGPOLL}, /* IO */ {"POLL", SIGPOLL}, /* IO */
{"PROF", SIGPROF}, {"PROF", SIGPROF},
@ -108,7 +117,24 @@ static const mapstruct sigtable[] = {
{"XFSZ", SIGXFSZ} {"XFSZ", SIGXFSZ}
}; };
static const int number_of_signals = sizeof(sigtable)/sizeof(mapstruct); #define number_of_signals (sizeof(sigtable)/sizeof(mapstruct))
#define XJOIN(a, b) JOIN(a, b)
#define JOIN(a, b) a##b
#define STATIC_ASSERT(x) typedef int XJOIN(static_assert_on_line_,__LINE__)[(x) ? 1 : -1]
/* sanity check */
#if defined(__linux__)
STATIC_ASSERT(number_of_signals == 31);
#elif defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
STATIC_ASSERT(number_of_signals == 30);
#elif defined(__GNU__)
STATIC_ASSERT(number_of_signals == 31);
#elif defined(__CYGWIN__)
STATIC_ASSERT(number_of_signals == 31);
#else
# warning Unknown operating system; assuming number_of_signals is correct
#endif
static int compare_signal_names(const void *a, const void *b){ static int compare_signal_names(const void *a, const void *b){
return strcasecmp( ((const mapstruct*)a)->name, ((const mapstruct*)b)->name ); return strcasecmp( ((const mapstruct*)a)->name, ((const mapstruct*)b)->name );
@ -285,12 +311,3 @@ void unix_print_signals(void){
} }
printf("\n"); printf("\n");
} }
/* sanity check */
static int init_signal_list(void) __attribute__((constructor));
static int init_signal_list(void){
if(number_of_signals != 31){
fprintf(stderr, "WARNING: %d signals -- adjust and recompile.\n", number_of_signals);
}
return 0;
}