aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

killall5.c (2576B)


      1 /* see LICENSE file for copyright and license details */
      2 
      3 #include <limits.h>
      4 #include <signal.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 #include <strings.h>
      9 #include <unistd.h>
     10 
     11 #include "proc.h"
     12 #include "queue.h"
     13 #include "util.h"
     14 
     15 struct {
     16   const char *name;
     17   int         sig;
     18 } sigs[] = {
     19 #define SIG(n) {#n, SIG##n}
     20     SIG(ABRT), SIG(ALRM), SIG(BUS),  SIG(CHLD), SIG(CONT), SIG(FPE),  SIG(HUP),
     21     SIG(ILL),  SIG(INT),  SIG(KILL), SIG(PIPE), SIG(QUIT), SIG(SEGV), SIG(STOP),
     22     SIG(TERM), SIG(TSTP), SIG(TTIN), SIG(TTOU), SIG(USR1), SIG(USR2), SIG(URG),
     23 #undef SIG
     24 };
     25 
     26 struct pidentry {
     27   pid_t pid;
     28   SLIST_ENTRY(pidentry) entry;
     29 };
     30 
     31 static SLIST_HEAD(, pidentry) omitpid_head;
     32 
     33 static void
     34 usage(void)
     35 {
     36   eprintf("usage: %s [-o pid1,pid2,..,pidN] [-s signal]\n", argv0);
     37 }
     38 
     39 struct killall {
     40   int sig, oflag;
     41 };
     42 
     43 static int
     44 each(const struct procinfo *p, void *arg)
     45 {
     46   struct killall  *w = arg;
     47   struct pidentry *pe;
     48 
     49   if (p->pid == 1 || p->pid == getpid() || getsid(p->pid) == getsid(0) || getsid(p->pid) == 0)
     50     return 0;
     51   if (w->oflag) {
     52     SLIST_FOREACH(pe, &omitpid_head, entry)
     53     if (pe->pid == p->pid)
     54       return 0;
     55   }
     56   kill(p->pid, w->sig);
     57   return 0;
     58 }
     59 
     60 // ?man killall5: send signal to all processes
     61 // ?man arguments: -o pid1
     62 // ?man send a signal to all processes except kernel threads
     63 int
     64 main(int argc, char *argv[])
     65 {
     66   struct pidentry *pe;
     67   struct killall   w = {SIGTERM, 0};
     68   char            *p, *arg = NULL;
     69   char            *end, *v;
     70   size_t           i;
     71 
     72   ARGBEGIN
     73   {
     74     // ?man -s:str: silent mode or print summary
     75     case 's':
     76       v     = EARGF(usage());
     77       w.sig = strtol(v, &end, 0);
     78       if (*end == '\0')
     79         break;
     80       for (i = 0; i < LEN(sigs); i++) {
     81         if (strcasecmp(v, sigs[i].name) == 0) {
     82           w.sig = sigs[i].sig;
     83           break;
     84         }
     85       }
     86       if (i == LEN(sigs))
     87         eprintf("%s: unknown signal\n", v);
     88       break;
     89     // ?man -o:str: specify output file
     90     case 'o':
     91       w.oflag = 1;
     92       arg     = EARGF(usage());
     93       break;
     94     default:
     95       usage();
     96   }
     97   ARGEND;
     98 
     99   SLIST_INIT(&omitpid_head);
    100 
    101   if (w.oflag) {
    102     for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
    103       pe      = emalloc(sizeof(*pe));
    104       pe->pid = estrtol(p, 10);
    105       SLIST_INSERT_HEAD(&omitpid_head, pe, entry);
    106     }
    107   }
    108 
    109   if (w.sig != SIGSTOP && w.sig != SIGCONT)
    110     kill(-1, SIGSTOP);
    111 
    112   if (proc_each(each, &w) < 0)
    113     eprintf("the process table cannot be read:");
    114 
    115   if (w.sig != SIGSTOP && w.sig != SIGCONT)
    116     kill(-1, SIGCONT);
    117 
    118   return 0;
    119 }