aboutsummaryrefslogtreecommitdiff
path: root/sub/initramfs/kill.c
blob: 9279a778d9ad9e81a76481b30b577383df127adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 /* GPLv2 license. */

#include <skalibs/nonposix.h>
#include <signal.h>
#include <skalibs/types.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr2.h>
#include <skalibs/sig.h>
#include <skalibs/nsig.h>

#define USAGE "kill [ -s SIGNAME ] pids..."
#define dieusage() strerr_dieusage(100, USAGE)

int main (int argc, char const *const *argv)
{
  int sig = 15 ;
  PROG = "kill" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "s:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 's':
        {
          sig = sig_number(l.arg) ;
          if (!sig)
          {
            unsigned int u ;
            if (!uint0_scan(l.arg, &u)) dieusage() ;
            if (u > SKALIBS_NSIG) dieusage() ;
            sig = u ;
          }
          break ;
        }
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;

  {
    pid_t pids[argc] ;
    for (unsigned int i = 0 ; i < argc ; i++)
      if (!pid0_scan(argv[i], pids + i)) dieusage() ;
    for (unsigned int i = 0 ; i < argc ; i++)
      if (kill(pids[i], sig) < 0)
        strerr_warnwu2sys("kill pid ", argv[i]) ;
  }
  return 0 ;
}