
Путь: Toys/POSIX, команды версии: Ver.4 Ver.9 Комментарии в файле kill.c : Команд: 2 kill
killall5
Исходный текст в файле kill.c // This has to match the filename:
#define FOR_kill
#define FORCE_FLAGS
#include "toys.h"
GLOBALS(
char *s;
struct arg_list *o;
)
// But kill's flags are a subset of killall5's
#define FOR_killall5
#include "generated/flags.h"
void kill_main(void)
{
int signum;
char *tmp, **args = toys.optargs;
pid_t pid;
// list signal(s)
if (FLAG(l)) {
if (*args) {
int signum = sig_to_num(*args);
char *s = 0;
if (signum>=0) s = num_to_sig(signum&127);
if (isdigit(**args)) puts(s ? s : "UNKNOWN");
else printf("%d\n", signum);
} else list_signals();
return;
}
// signal must come before pids, so "kill -9 -1" isn't confusing.
if (!TT.s && *args && **args=='-') TT.s = *(args++)+1;
if (TT.s) {
char *arg;
int i = strtol(TT.s, &arg, 10);
if (!*arg) arg = num_to_sig(i);
else arg = TT.s;
if (!arg || -1 == (signum = sig_to_num(arg)))
error_exit("Unknown signal '%s'", arg);
} else signum = SIGTERM;
// is it killall5?
if (CFG_KILLALL5 && toys.which->name[4]=='a') {
DIR *dp;
struct dirent *entry;
int pid, sid;
long *olist = 0, ocount = 0;
// parse omit list
if (FLAG(o)) {
struct arg_list *ptr;
for (ptr = TT.o; ptr; ptr = ptr->next) ocount++;
olist = xmalloc(ocount*sizeof(long));
ocount = 0;
for (ptr = TT.o; ptr; ptr=ptr->next) olist[ocount++] = atolx(ptr->arg);
}
sid = getsid(pid = getpid());
if (!(dp = opendir("/proc"))) {
free(olist);
perror_exit("/proc");
}
while ((entry = readdir(dp))) {
int count, procpid, procsid;
if (!(procpid = atoi(entry->d_name))) continue;
snprintf(toybuf, sizeof(toybuf), "/proc/%d/stat", procpid);
if (!readfile(toybuf, toybuf, sizeof(toybuf))) continue;
if (sscanf(toybuf, "%*d %*s %*c %*d %*d %d", &procsid) != 1) continue;
if (pid == procpid || sid == procsid || procpid == 1) continue;
// Check for kernel threads.
snprintf(toybuf, sizeof(toybuf), "/proc/%d/cmdline", procpid);
if (!readfile(toybuf, toybuf, sizeof(toybuf)) || !*toybuf) continue;
// Check with omit list.
for (count = 0; count < ocount; count++)
if (procpid == olist[count]) break;
if (count != ocount) continue;
kill(procpid, signum);
}
closedir(dp);
free(olist);
// is it kill?
} else {
// "<1" in optstr wouldn't cover this because "-SIGNAL"
if (!*args) help_exit("missing argument");
while (*args) {
char *arg = *(args++);
pid = estrtol(arg, &tmp, 10);
if (!errno && *tmp) errno = ESRCH;
if (errno || kill(pid, signum)<0) perror_msg("bad pid '%s'", arg);
}
}
}
void killall5_main(void)
{
kill_main();
} |
![]() |