aboutsummaryrefslogtreecommitdiff
path: root/toys/lsb/killall.c
blob: 47aea23d30e04dedcaf44e58fb15443f7eabc42a (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* killall.c - Send signal (default: TERM) to all processes with given names.
 *
 * Copyright 2012 Andreas Heck <aheck@gmx.de>
 *
 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html

USE_KILLALL(NEWTOY(killall, "?s:lqvi", TOYFLAG_USR|TOYFLAG_BIN))

config KILLALL
  bool "killall"
  default y
  help
    usage: killall [-l] [-iqv] [-SIGNAL|-s SIGNAL] PROCESS_NAME...

    Send a signal (default: TERM) to all processes with the given names.

    -i	Ask for confirmation before killing
    -l	Print list of all available signals
    -q	Don't print any warnings or error messages
    -s	Send SIGNAL instead of SIGTERM
    -v	Report if the signal was successfully sent
*/

#define FOR_killall
#include "toys.h"

GLOBALS(
  char *s;

  int signum;
  pid_t cur_pid;
  char **names;
  short *err;
)

static int kill_process(pid_t pid, char *name)
{
  int offset = 0;

  if (pid == TT.cur_pid) return 0;

  if (FLAG(i)) {
    fprintf(stderr, "Signal %s(%d)", name, (int)pid);
    if (!yesno(0)) return 0;
  }

  errno = 0;
  kill(pid, TT.signum);
  for (;;) {
    if (TT.names[offset] == name) {
      TT.err[offset] = errno;
      break;
    } else offset++;
  }
  if (errno) {
    if (!FLAG(q)) perror_msg("pid %d", (int)pid);
  } else if (FLAG(v))
    printf("Killed %s(%d) with signal %d\n", name, pid, TT.signum);

  return 0;
}

void killall_main(void)
{
  int i;

  TT.names = toys.optargs;
  TT.signum = SIGTERM;

  if (FLAG(l)) {
    list_signals();
    return;
  }

  if (TT.s || (*TT.names && **TT.names == '-')) {
    if (0 > (TT.signum = sig_to_num(TT.s ? TT.s : (*TT.names)+1))) {
      if (FLAG(q)) exit(1);
      error_exit("Invalid signal");
    }
    if (!TT.s) {
      TT.names++;
      toys.optc--;
    }
  }

  if (!toys.optc) help_exit("no name");

  TT.cur_pid = getpid();

  TT.err = xmalloc(2*toys.optc);
  for (i=0; i<toys.optc; i++) TT.err[i] = ESRCH;
  names_to_pid(TT.names, kill_process);
  for (i=0; i<toys.optc; i++) {
    if (TT.err[i]) {
      toys.exitval = 1;
      errno = TT.err[i];
      perror_msg_raw(TT.names[i]);
    }
  }
  if (CFG_TOYBOX_FREE) free(TT.err);
}