diff options
author | Leah Rowe <leah@libreboot.org> | 2025-10-04 09:14:33 +0100 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2025-10-04 09:20:12 +0100 |
commit | e9a910b33c7837b4b868e3abda18eb4810df7f02 (patch) | |
tree | 749e1830cb0607952df1a1afc0ae09ec1db54140 /util/sbase/kill.c | |
parent | 2cfaba181b3c68761871fa47b32725c934423c14 (diff) |
config/git: import suckless sbase
i currently use the output of sha512sum in several
places of xbmk, which is a bit unreliable in case
output changes.
other cases where i use util outputs in variables
are probably reliable, because i'm using mostly
posix utilities in those.
to mitigate this, i now import suckless sbase, which
has a reasonable sha512sum implementation.
*every* binary it builds is being placed in build.list,
because i'll probably start using more of them.
for example, i may start modifying the "date"
implementation, adding the GNU-specific options that
i need as mentioned on init.sh
i'm importing it in util/ because the sha512sum
util is needed for verifying project sources, so
if sbase itself is a "project source", that means
we can into a chicken and egg bootstrapping problem.
this is sbase at revision:
055cc1ae1b3a13c3d8f25af0a4a3316590efcd48
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/sbase/kill.c')
-rw-r--r-- | util/sbase/kill.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/util/sbase/kill.c b/util/sbase/kill.c new file mode 100644 index 00000000..b09f55cb --- /dev/null +++ b/util/sbase/kill.c @@ -0,0 +1,131 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/wait.h> + +#include <ctype.h> +#include <limits.h> +#include <signal.h> +#include <stdio.h> +#include <string.h> +#include <strings.h> + +#include "util.h" + +struct { + const char *name; + const int sig; +} sigs[] = { + { "0", 0 }, +#define SIG(n) { #n, SIG##n } + SIG(ABRT), SIG(ALRM), SIG(BUS), SIG(CHLD), SIG(CONT), SIG(FPE), SIG(HUP), + SIG(ILL), SIG(INT), SIG(KILL), SIG(PIPE), SIG(QUIT), SIG(SEGV), SIG(STOP), + SIG(TERM), SIG(TRAP), SIG(TSTP), SIG(TTIN), SIG(TTOU), SIG(USR1), SIG(USR2), + SIG(URG), +#undef SIG +}; + +const char * +sig2name(const int sig) +{ + size_t i; + + for (i = 0; i < LEN(sigs); i++) + if (sigs[i].sig == sig) + return sigs[i].name; + eprintf("%d: bad signal number\n", sig); + + return NULL; /* not reached */ +} + +int +name2sig(const char *name) +{ + size_t i; + + for (i = 0; i < LEN(sigs); i++) + if (!strcasecmp(sigs[i].name, name)) + return sigs[i].sig; + eprintf("%s: bad signal name\n", name); + + return -1; /* not reached */ +} + +static void +usage(void) +{ + eprintf("usage: %s [-s signame | -num | -signame] pid ...\n" + " %s -l [num]\n", argv0, argv0); +} + +int +main(int argc, char *argv[]) +{ + pid_t pid; + size_t i; + int ret = 0, sig = SIGTERM; + + argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + + if (!argc) + usage(); + + if ((*argv)[0] == '-') { + switch ((*argv)[1]) { + case 'l': + if ((*argv)[2]) + goto longopt; + argc--, argv++; + if (!argc) { + for (i = 0; i < LEN(sigs); i++) + puts(sigs[i].name); + } else if (argc == 1) { + sig = estrtonum(*argv, 0, INT_MAX); + if (sig > 128) + sig = WTERMSIG(sig); + puts(sig2name(sig)); + } else { + usage(); + } + return fshut(stdout, "<stdout>"); + case 's': + if ((*argv)[2]) + goto longopt; + argc--, argv++; + if (!argc) + usage(); + sig = name2sig(*argv); + argc--, argv++; + break; + case '-': + if ((*argv)[2]) + goto longopt; + argc--, argv++; + break; + default: + longopt: + /* XSI-extensions -argnum and -argname*/ + if (isdigit((*argv)[1])) { + sig = estrtonum((*argv) + 1, 0, INT_MAX); + sig2name(sig); + } else { + sig = name2sig((*argv) + 1); + } + argc--, argv++; + } + } + + if (argc && !strcmp(*argv, "--")) + argc--, argv++; + + if (!argc) + usage(); + + for (; *argv; argc--, argv++) { + pid = estrtonum(*argv, INT_MIN, INT_MAX); + if (kill(pid, sig) < 0) { + weprintf("kill %d:", pid); + ret = 1; + } + } + + return ret; +} |