utils: add a SYS5 killall
authorAlan Cox <alan@linux.intel.com>
Sun, 3 Dec 2017 20:27:15 +0000 (20:27 +0000)
committerAlan Cox <alan@linux.intel.com>
Sun, 3 Dec 2017 20:27:15 +0000 (20:27 +0000)
Not yet tested

Applications/util/Makefile.6502
Applications/util/Makefile.68000
Applications/util/Makefile.6809
Applications/util/Makefile.z80
Applications/util/killall.c [new file with mode: 0644]

index 830a7ac..585738a 100644 (file)
@@ -25,6 +25,7 @@ SRCSNS = \
        head.c \
        init.c \
        kill.c \
+       killall.c \
        line.c \
        ln.c \
        logname.c \
index 35ae170..186f5dd 100644 (file)
@@ -29,6 +29,7 @@ SRCSNS = \
        head.c \
        init.c \
        kill.c \
+       killall.c \
        ln.c \
        logname.c \
        mkdir.c \
index 264606a..4620d2a 100644 (file)
@@ -29,6 +29,7 @@ SRCSNS = \
        head.c \
        init.c \
        kill.c \
+       killall.c \
        ln.c \
        logname.c \
        mkdir.c \
index fac4131..b3e0fcc 100644 (file)
@@ -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 (file)
index 0000000..5a41322
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *     SYS5 style killall
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <proc.h>
+#include <fcntl.h>
+
+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);
+}