From 1d2a6bcf8be054a3e791035d7fc50181e26ed415 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Sun, 7 Apr 2024 06:43:30 +1200 Subject: [PATCH] process: fix inconsistent signal detection Previously strtosig() detected the signal count at runtime, mutating the value of a static global variable. The signals were detected using \#ifdef directives and were taken from on my OpenBSD install. Extract the common signals that should be found in every POSIX implementation [1] and remove any vendor-specific signals with possibly differing names across implementations. Do not detect the signal count at runtime anymore. Keep it in a static const variable which holds the signal count known _at compile time_. Fix the logic error in the for loop in strtosig(). [1]: --- lprocess.c | 128 ++++++++++++++--------------------------------------- 1 file changed, 33 insertions(+), 95 deletions(-) diff --git a/lprocess.c b/lprocess.c index f4b028f..52bd381 100644 --- a/lprocess.c +++ b/lprocess.c @@ -22,115 +22,58 @@ #define PID_MAX 8 /* rounded to the nearest even number */ #define PROCESS_MAX 256 -static int sigc = -1; +static const int sigc = 22; /* clang-format off */ static const int signals[] = { - SIGHUP, - SIGINT, - SIGQUIT, - SIGILL, - SIGTRAP, SIGABRT, - SIGIOT, - SIGFPE, - SIGKILL, - SIGBUS, - SIGSEGV, - SIGSYS, - SIGPIPE, SIGALRM, - SIGTERM, - SIGURG, - SIGSTOP, - SIGTSTP, - SIGCONT, + SIGBUS, SIGCHLD, + SIGCONT, + SIGFPE, + SIGHUP, + SIGILL, + SIGINT, + SIGKILL, + SIGPIPE, + SIGQUIT, + SIGSEGV, + SIGSTOP, + SIGTERM, + SIGTSTP, SIGTTIN, SIGTTOU, -#ifdef SIGSTKFL - SIGSTKFL, -#endif - SIGIO, - SIGXCPU, - SIGXFSZ, -#ifdef SIGVTALR - SIGVTALR, -#elif defined(SIGVTALRM) - SIGVTALRM, -#endif - SIGPROF, -#ifdef SIGWINC - SIGWINC, -#elif defined(SIGWINCH) - SIGWINCH, -#endif -#ifdef SIGINFO - SIGINFO, -#endif -#ifdef SIGPOLL - SIGPOLL, -#endif -#ifdef SIGPWR - SIGPWR, -#endif SIGUSR1, SIGUSR2, - -1 /* end */ + SIGTRAP, + SIGURG }; static const char *sigstrs[] = { - [SIGHUP] = "SIGHUP", - [SIGINT] = "SIGINT", - [SIGQUIT] = "SIGQUIT", - [SIGILL] = "SIGILL", - [SIGTRAP] = "SIGTRAP", [SIGABRT] = "SIGABRT", - [SIGIOT] = "SIGIOT", - [SIGFPE] = "SIGFPE", - [SIGKILL] = "SIGKILL", - [SIGBUS] = "SIGBUS", - [SIGSEGV] = "SIGSEGV", - [SIGSYS] = "SIGSYS", - [SIGPIPE] = "SIGPIPE", [SIGALRM] = "SIGALRM", - [SIGTERM] = "SIGTERM", - [SIGURG] = "SIGURG", - [SIGSTOP] = "SIGSTOP", - [SIGTSTP] = "SIGTSTP", - [SIGCONT] = "SIGCONT", + [SIGBUS] = "SIGBUS", [SIGCHLD] = "SIGCHLD", + [SIGCONT] = "SIGCONT", + [SIGFPE] = "SIGFPE", + [SIGHUP] = "SIGHUP", + [SIGILL] = "SIGILL", + [SIGINT] = "SIGINT", + [SIGKILL] = "SIGKILL", + [SIGPIPE] = "SIGPIPE", + [SIGQUIT] = "SIGQUIT", + [SIGSEGV] = "SIGSEGV", + [SIGSTOP] = "SIGSTOP", + [SIGTERM] = "SIGTERM", + [SIGTSTP] = "SIGTSTP", [SIGTTIN] = "SIGTTIN", [SIGTTOU] = "SIGTTOU", -#ifdef SIGSTKFL - [SIGSTKFL] = "SIGSTKFL", -#endif - [SIGIO] = "SIGIO", - [SIGXCPU] = "SIGXCPU", - [SIGXFSZ] = "SIGXFSZ", -#ifdef SIGVTALR - [SIGVTALR] = "SIGVTALR", -#elif defined(SIGVTALRM) - [SIGVTALRM] = "SIGVTALRM", -#endif - [SIGPROF] = "SIGPROF", -#ifdef SIGWINC - [SIGWINC] = "SIGWINC", -#elif defined(SIGWINCH) - [SIGWINCH] = "SIGWINCH", -#endif -#ifdef SIGINFO - [SIGINFO] = "SIGINFO", -#endif -#ifdef SIGPOLL - [SIGPOLL] = "SIGPOLL", -#endif -#ifdef SIGPWR - [SIGPWR] = "SIGPWR", -#endif [SIGUSR1] = "SIGUSR1", - [SIGUSR2] = "SIGUSR2" + [SIGUSR2] = "SIGUSR2", + [SIGTRAP] = "SIGTRAP", + [SIGURG] = "SIGURG" }; /* clang-format on */ @@ -187,13 +130,8 @@ strtosig(const char *sig) { int i, j; - sigc = 0; - while (signals[sigc] != -1) - sigc++; - sigc--; - j = 0; - for (i = signals[j]; i < sigc; j++) { + for (i = signals[j]; j < sigc; j++) { i = signals[j]; if (strcasecmp(sigstrs[i], sig) == 0) return i; /* signal found */