Implement -o for killall5

This commit is contained in:
sin 2014-03-17 14:26:19 +02:00
parent 55fa13016c
commit 4fb9eabf8e
1 changed files with 37 additions and 2 deletions

View File

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