From 9d354377a8aea69e19b1785eec32066519052e97 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 11 Jun 2019 14:57:06 -0700 Subject: Move sig_to_num and num_to_sig into portability.c. macOS doesn't have real-time signals, and it has a slightly different set of non-portable signals from Linux. With this, the toybox `kill -l` output matches the default macOS kill(1). --- lib/portability.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'lib/portability.c') diff --git a/lib/portability.c b/lib/portability.c index 088b90e1..d48a3b4d 100644 --- a/lib/portability.c +++ b/lib/portability.c @@ -397,3 +397,104 @@ int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev) return result; } #endif + +// Signals required by POSIX 2008: +// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html + +#define SIGNIFY(x) {SIG##x, #x} + +static const struct signame signames[] = { + // POSIX + SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), + SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL), + SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM), + SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP), + SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ), + // Non-POSIX signals that cause termination + SIGNIFY(PROF), SIGNIFY(IO), +#ifdef __linux__ + SIGNIFY(STKFLT), SIGNIFY(POLL), SIGNIFY(PWR), +#elif defined(__APPLE__) + SIGNIFY(EMT), SIGNIFY(INFO), +#endif + + // Note: sigatexit relies on all the signals with a default disposition that + // terminates the process coming *before* SIGCHLD. + + // POSIX signals that don't cause termination + SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP), + SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG), + // Non-POSIX signals that don't cause termination + SIGNIFY(WINCH), +}; +int signames_len = ARRAY_LEN(signames); + +#undef SIGNIFY + +void xsignal_all_killers(void *handler) +{ + int i; + + for (i=0; signames[i].num != SIGCHLD; i++) + if (signames[i].num != SIGKILL) + xsignal(signames[i].num, handler ? exit_signal : SIG_DFL); +} + +// Convert a string like "9", "KILL", "SIGHUP", or "SIGRTMIN+2" to a number. +int sig_to_num(char *sigstr) +{ + int i, offset; + char *s; + + // Numeric? + i = estrtol(sigstr, &s, 10); + if (!errno && !*s) return i; + + // Skip leading "SIG". + strcasestart(&sigstr, "sig"); + + // Named signal? + for (i=0; i= SIGRTMIN && i <= SIGRTMAX) return i; +#endif + + return -1; +} + +char *num_to_sig(int sig) +{ + int i; + + // A named signal? + for (i=0; i SIGRTMIN && sig < SIGRTMAX) { + if (sig-SIGRTMIN <= SIGRTMAX-sig) sprintf(libbuf, "RTMIN+%d", sig-SIGRTMIN); + else sprintf(libbuf, "RTMAX-%d", SIGRTMAX-sig); + return libbuf; + } +#endif + + return NULL; +} -- cgit v1.2.3