ubase/killall5.c

114 lines
2.2 KiB
C
Raw Normal View History

2014-03-06 06:13:47 -05:00
/* See LICENSE file for copyright and license details. */
#include <dirent.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
2014-03-17 08:26:19 -04:00
#include <string.h>
2014-03-06 06:13:47 -05:00
#include <unistd.h>
#include "proc.h"
#include "queue.h"
2014-03-06 06:13:47 -05:00
#include "util.h"
struct {
const char *name;
int sig;
} sigs[] = {
#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(TSTP), SIG(TTIN), SIG(TTOU), SIG(USR1), SIG(USR2), SIG(URG),
#undef SIG
};
static void
usage(void)
{
2014-03-17 08:26:19 -04:00
eprintf("usage: %s [-o pid1,pid2,..,pidN] [-s signal]\n", argv0);
2014-03-06 06:13:47 -05:00
}
struct pidentry {
2014-03-17 08:26:19 -04:00
pid_t pid;
TAILQ_ENTRY(pidentry) entry;
};
static TAILQ_HEAD(omitpid_head, pidentry) omitpid_head;
2014-03-17 08:26:19 -04:00
2014-03-06 06:13:47 -05:00
int
main(int argc, char *argv[])
{
struct pidentry *pe, *tmp;
2014-03-17 08:26:19 -04:00
int oflag = 0;
char *p, *arg = NULL;
2014-03-06 06:13:47 -05:00
DIR *dp;
struct dirent *entry;
char *end, *v;
int sig = SIGTERM;
pid_t pid;
size_t i;
ARGBEGIN {
case 's':
v = EARGF(usage());
sig = strtol(v, &end, 0);
if(*end == '\0')
break;
for(i = 0; i < LEN(sigs); i++) {
if(strcasecmp(v, sigs[i].name) == 0) {
sig = sigs[i].sig;
break;
}
}
if(i == LEN(sigs))
eprintf("%s: unknown signal\n", v);
break;
2014-03-17 08:26:19 -04:00
case 'o':
oflag = 1;
arg = EARGF(usage());
break;
2014-03-06 06:13:47 -05:00
default:
usage();
} ARGEND;
TAILQ_INIT(&omitpid_head);
2014-03-17 08:26:19 -04:00
for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
pe = emalloc(sizeof(*pe));
pe->pid = estrtol(p, 10);
TAILQ_INSERT_TAIL(&omitpid_head, pe, entry);
2014-03-17 08:26:19 -04:00
}
2014-03-06 06:13:47 -05:00
if (sig != SIGSTOP && sig != SIGCONT)
kill(-1, SIGSTOP);
if (!(dp = opendir("/proc")))
eprintf("opendir /proc:");
while ((entry = readdir(dp))) {
2014-03-06 10:06:47 -05:00
if (pidfile(entry->d_name) == 0)
2014-03-06 06:13:47 -05:00
continue;
pid = estrtol(entry->d_name, 10);
if (pid == 1 || pid == getpid() ||
getsid(pid) == getsid(0) || getsid(pid) == 0)
continue;
2014-03-17 08:26:19 -04:00
if (oflag == 1) {
TAILQ_FOREACH(pe, &omitpid_head, entry)
if (pe->pid == pid)
2014-03-17 08:26:19 -04:00
break;
if (pe)
2014-03-17 08:26:19 -04:00
continue;
}
2014-03-06 06:13:47 -05:00
kill(pid, sig);
}
closedir(dp);
if (sig != SIGSTOP && sig != SIGCONT)
kill(-1, SIGCONT);
for (pe = TAILQ_FIRST(&omitpid_head); pe; pe = tmp) {
tmp = TAILQ_NEXT(pe, entry);
free(pe);
2014-03-17 08:26:19 -04:00
}
2014-03-06 06:13:47 -05:00
return EXIT_SUCCESS;
}