ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit 4fb9eabf8ef32728a4af0552a92c01e8c430c48b
parent 55fa13016cc2ecc38fca402efeb980ff23013fcb
Author: sin <sin@2f30.org>
Date:   Mon, 17 Mar 2014 14:26:19 +0200

Implement -o for killall5

Diffstat:
Mkillall5.c | 39+++++++++++++++++++++++++++++++++++++--
1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/killall5.c b/killall5.c @@ -4,7 +4,7 @@ #include <signal.h> #include <stdio.h> #include <stdlib.h> -#include <strings.h> +#include <string.h> #include <unistd.h> #include "proc.h" #include "util.h" @@ -23,12 +23,20 @@ struct { static void usage(void) { - eprintf("usage: %s [-s signal]\n", argv0); + eprintf("usage: %s [-o pid1,pid2,..,pidN] [-s signal]\n", argv0); } +static struct omit { + pid_t pid; + struct omit *next; +} *omithead; + int main(int argc, char *argv[]) { + struct omit *onode, *tmp; + int oflag = 0; + char *p, *arg = NULL; DIR *dp; struct dirent *entry; char *end, *v; @@ -51,10 +59,23 @@ main(int argc, char *argv[]) if(i == LEN(sigs)) eprintf("%s: unknown signal\n", v); break; + case 'o': + oflag = 1; + arg = EARGF(usage()); + break; default: usage(); } ARGEND; + for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) { + onode = malloc(sizeof(*onode)); + if (!onode) + eprintf("malloc:"); + onode->pid = estrtol(p, 10); + onode->next = omithead; + omithead = onode; + } + if (sig != SIGSTOP && sig != SIGCONT) kill(-1, SIGSTOP); @@ -67,6 +88,13 @@ main(int argc, char *argv[]) if (pid == 1 || pid == getpid() || getsid(pid) == getsid(0) || getsid(pid) == 0) continue; + if (oflag == 1) { + for (onode = omithead; onode; onode = onode->next) + if (onode->pid == pid) + break; + if (onode) + continue; + } kill(pid, sig); } closedir(dp); @@ -74,5 +102,12 @@ main(int argc, char *argv[]) if (sig != SIGSTOP && sig != SIGCONT) kill(-1, SIGCONT); + onode = omithead; + while (onode) { + tmp = onode->next; + free(onode); + onode = tmp; + } + return EXIT_SUCCESS; }