mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Modernize our code for looking up descriptive strings for Unix signals.
At least as far back as the 2008 spec, POSIX has defined strsignal(3) for looking up descriptive strings for signal numbers. We hadn't gotten the word though, and were still using the crufty old sys_siglist array, which is in no standard even though most Unixen provide it. Aside from not being formally standards-compliant, this was just plain ugly because it involved #ifdef's at every place using the code. To eliminate the #ifdef's, create a portability function pg_strsignal, which wraps strsignal(3) if available and otherwise falls back to sys_siglist[] if available. The set of Unixen with neither API is probably empty these days, but on any platform with neither, you'll just get "unrecognized signal". All extant callers print the numeric signal number too, so no need to work harder than that. Along the way, upgrade pg_basebackup's child-error-exit reporting to match the rest of the system. Discussion: https://postgr.es/m/25758.1544983503@sss.pgh.pa.us
This commit is contained in:
67
src/port/pgstrsignal.c
Normal file
67
src/port/pgstrsignal.c
Normal file
@ -0,0 +1,67 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* pgstrsignal.c
|
||||
* Identify a Unix signal number
|
||||
*
|
||||
* On platforms compliant with modern POSIX, this just wraps strsignal(3).
|
||||
* Elsewhere, we do the best we can.
|
||||
*
|
||||
* This file is not currently built in MSVC builds, since it's useless
|
||||
* on non-Unix platforms.
|
||||
*
|
||||
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* src/port/pgstrsignal.c
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include "c.h"
|
||||
|
||||
|
||||
/*
|
||||
* pg_strsignal
|
||||
*
|
||||
* Return a string identifying the given Unix signal number.
|
||||
*
|
||||
* The result is declared "const char *" because callers should not
|
||||
* modify the string. Note, however, that POSIX does not promise that
|
||||
* the string will remain valid across later calls to strsignal().
|
||||
*
|
||||
* This version guarantees to return a non-NULL pointer, although
|
||||
* some platforms' versions of strsignal() do not.
|
||||
*/
|
||||
const char *
|
||||
pg_strsignal(int signum)
|
||||
{
|
||||
const char *result;
|
||||
|
||||
/*
|
||||
* If we have strsignal(3), use that --- but check its result for NULL.
|
||||
* Otherwise, if we have sys_siglist[], use that; just out of paranoia,
|
||||
* check for NULL there too. (We assume there is no point in trying both
|
||||
* APIs.)
|
||||
*/
|
||||
#if defined(HAVE_STRSIGNAL)
|
||||
result = strsignal(signum);
|
||||
if (result)
|
||||
return result;
|
||||
#elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST
|
||||
if (signum > 0 && signum < NSIG)
|
||||
{
|
||||
result = sys_siglist[signum];
|
||||
if (result)
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Fallback case: just return "unrecognized signal". Project style is for
|
||||
* callers to print the numeric signal value along with the result of this
|
||||
* function, so there's no need to work harder than this.
|
||||
*/
|
||||
result = "unrecognized signal";
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user