aboutsummaryrefslogtreecommitdiff
path: root/src/port/pgstrsignal.c
blob: ec989229baaa151acab3a7ef076384bc5c8039e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;
}