|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pgstrsignal.c |
| 4 | + * Identify a Unix signal number |
| 5 | + * |
| 6 | + * On platforms compliant with modern POSIX, this just wraps strsignal(3). |
| 7 | + * Elsewhere, we do the best we can. |
| 8 | + * |
| 9 | + * This file is not currently built in MSVC builds, since it's useless |
| 10 | + * on non-Unix platforms. |
| 11 | + * |
| 12 | + * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group |
| 13 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 14 | + * |
| 15 | + * IDENTIFICATION |
| 16 | + * src/port/pgstrsignal.c |
| 17 | + * |
| 18 | + *------------------------------------------------------------------------- |
| 19 | + */ |
| 20 | + |
| 21 | +#include "c.h" |
| 22 | + |
| 23 | + |
| 24 | +/* |
| 25 | + * pg_strsignal |
| 26 | + * |
| 27 | + * Return a string identifying the given Unix signal number. |
| 28 | + * |
| 29 | + * The result is declared "const char *" because callers should not |
| 30 | + * modify the string. Note, however, that POSIX does not promise that |
| 31 | + * the string will remain valid across later calls to strsignal(). |
| 32 | + * |
| 33 | + * This version guarantees to return a non-NULL pointer, although |
| 34 | + * some platforms' versions of strsignal() do not. |
| 35 | + */ |
| 36 | +const char * |
| 37 | +pg_strsignal(int signum) |
| 38 | +{ |
| 39 | + const char *result; |
| 40 | + |
| 41 | + /* |
| 42 | + * If we have strsignal(3), use that --- but check its result for NULL. |
| 43 | + * Otherwise, if we have sys_siglist[], use that; just out of paranoia, |
| 44 | + * check for NULL there too. (We assume there is no point in trying both |
| 45 | + * APIs.) |
| 46 | + */ |
| 47 | +#if defined(HAVE_STRSIGNAL) |
| 48 | + result = strsignal(signum); |
| 49 | + if (result) |
| 50 | + return result; |
| 51 | +#elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST |
| 52 | + if (signum > 0 && signum < NSIG) |
| 53 | + { |
| 54 | + result = sys_siglist[signum]; |
| 55 | + if (result) |
| 56 | + return result; |
| 57 | + } |
| 58 | +#endif |
| 59 | + |
| 60 | + /* |
| 61 | + * Fallback case: just return "unrecognized signal". Project style is for |
| 62 | + * callers to print the numeric signal value along with the result of this |
| 63 | + * function, so there's no need to work harder than this. |
| 64 | + */ |
| 65 | + result = "unrecognized signal"; |
| 66 | + return result; |
| 67 | +} |
0 commit comments