From: Alan Cox Date: Sun, 3 Dec 2017 20:27:15 +0000 (+0000) Subject: utils: add a SYS5 killall X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=315e86bd2524d74f8b33ca8ff21d721c8803fd75;p=FUZIX.git utils: add a SYS5 killall Not yet tested --- diff --git a/Applications/util/Makefile.6502 b/Applications/util/Makefile.6502 index 830a7ac8..585738aa 100644 --- a/Applications/util/Makefile.6502 +++ b/Applications/util/Makefile.6502 @@ -25,6 +25,7 @@ SRCSNS = \ head.c \ init.c \ kill.c \ + killall.c \ line.c \ ln.c \ logname.c \ diff --git a/Applications/util/Makefile.68000 b/Applications/util/Makefile.68000 index 35ae1700..186f5dd6 100644 --- a/Applications/util/Makefile.68000 +++ b/Applications/util/Makefile.68000 @@ -29,6 +29,7 @@ SRCSNS = \ head.c \ init.c \ kill.c \ + killall.c \ ln.c \ logname.c \ mkdir.c \ diff --git a/Applications/util/Makefile.6809 b/Applications/util/Makefile.6809 index 264606a3..4620d2ae 100644 --- a/Applications/util/Makefile.6809 +++ b/Applications/util/Makefile.6809 @@ -29,6 +29,7 @@ SRCSNS = \ head.c \ init.c \ kill.c \ + killall.c \ ln.c \ logname.c \ mkdir.c \ diff --git a/Applications/util/Makefile.z80 b/Applications/util/Makefile.z80 index fac4131f..b3e0fcc3 100644 --- a/Applications/util/Makefile.z80 +++ b/Applications/util/Makefile.z80 @@ -26,6 +26,7 @@ SRCSNS = \ head.c \ init.c \ kill.c \ + killall.c \ line.c \ ln.c \ logname.c \ diff --git a/Applications/util/killall.c b/Applications/util/killall.c new file mode 100644 index 00000000..5a41322a --- /dev/null +++ b/Applications/util/killall.c @@ -0,0 +1,71 @@ +/* + * SYS5 style killall + */ + +#include +#include +#include +#include +#include +#include + +static pid_t pid, ppid; +static struct p_tab_buffer buf; + +static void writes(int fd, const char *p) +{ + write(fd, p, strlen(p)); +} + +static int kill_pids(int sig) +{ + int fd = open("/dev/proc", O_RDONLY); + int nodesize; + int procs; + int ct = 0; + int i; + + if (fd == -1) { + perror("/dev/proc"); + return 255; + } + if (ioctl(fd, 2, (char *)&nodesize) != 0) { + perror("ioctl"); + return 255; + } + if (nodesize > sizeof(buf)) { + writes(2, "kilall: mismatch with kernel.\n"); + exit(1); + } + if (ioctl(fd, 1, (char *)&procs) != 0) { + perror("ioctl"); + return 255; + } + for (i = 0; i < procs; i++) { + if (read(fd, (char *)&buf, nodesize) != nodesize) { + perror("read"); + return 255; + } + if (buf.p_tab.p_pid != ppid && buf.p_tab.p_pid != pid && buf.p_tab.p_pid != 1) { + kill(buf.p_tab.p_pid, sig); + ct++; + } + } + close(fd); + return ct; +} + +int main(int argc, char *argv[]) +{ + int sig = SIGTERM; + + if (argc == 2) + sig = atoi(argv[1]); + else if (argc > 2) { + writes(1, "killall [signal]\n"); + exit(1); + } + pid = getpid(); + ppid = getppid(); + return kill_pids(sig); +}