aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/kill.c
blob: f8e86b6720f4b78f07de37360aabba32fef5e120 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* kill.c - a program to send signals to processes
 *
 * Copyright 2012 Daniel Walter <d.walter@0x90.at>
 *
 * See http://opengroup.org/onlinepubs/9699919799/utilities/kill.html
 *
 * killall5.c - Send signal to all processes outside current session.
 *
 * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com>
 * Copyright 2014 Kyungwan Han <asura321@gamil.com>
 *
 * No Standard

USE_KILL(NEWTOY(kill, "?ls: ", TOYFLAG_BIN))
USE_KILLALL5(NEWTOY(killall5, "?o*ls: [!lo][!ls]", TOYFLAG_SBIN))

config KILL
  bool "kill"
  default y
  help
    usage: kill [-l [SIGNAL] | -s SIGNAL | -SIGNAL] pid...

    Send signal to process(es).

    -l	List signal name(s) and number(s)
    -s	Send SIGNAL (default SIGTERM)

config KILLALL5
  bool "killall5"
  default y
  depends on KILL
  help
    usage: killall5 [-l [SIGNAL]] [-SIGNAL|-s SIGNAL] [-o PID]...

    Send a signal to all processes outside current session.

    -l	List signal name(s) and number(s)
    -o PID	Omit PID
    -s	Send SIGNAL (default SIGTERM)
*/

// This has to match the filename:
#define FOR_kill
#include "toys.h"

GLOBALS(
  char *s;
  struct arg_list *o;
)

// But kill's flags are a subset of killall5's

#define CLEANUP_kill
#define FOR_killall5
#include "generated/flags.h"

void kill_main(void)
{
  int signum;
  char *tmp, **args = toys.optargs;
  pid_t pid;

  // list signal(s)
  if (toys.optflags & FLAG_l) {
    if (*args) {
      int signum = sig_to_num(*args);
      char *s = NULL;

      if (signum>=0) s = num_to_sig(signum&127);
      puts(s ? s : "UNKNOWN");
    } else sig_to_num(NULL);
    return;
  }

  // signal must come before pids, so "kill -9 -1" isn't confusing.

  if (!TT.s && *args && **args=='-') TT.s = *(args++)+1;
  if (TT.s) {
    char *arg;
    int i = strtol(TT.s, &arg, 10);
    if (!*arg) arg = num_to_sig(i);
    else arg = TT.s;

    if (!arg || -1 == (signum = sig_to_num(arg)))
      error_exit("Unknown signal '%s'", arg);
  } else signum = SIGTERM;

  // is it killall5?
  if (CFG_KILLALL5 && toys.which->name[4]=='a') {
    DIR *dp;
    struct dirent *entry;
    int pid, sid;
    long *olist = 0, ocount = 0;

    // parse omit list
    if (toys.optflags & FLAG_o) {
      struct arg_list *ptr;

      for (ptr = TT.o; ptr; ptr = ptr->next) ocount++;
      olist = xmalloc(ocount*sizeof(long));
      ocount = 0;
      for (ptr = TT.o; ptr; ptr=ptr->next) olist[ocount++] = atolx(ptr->arg);
    }

    sid = getsid(pid = getpid());

    if (!(dp = opendir("/proc"))) perror_exit("/proc");
    while ((entry = readdir(dp))) {
      int count, procpid, procsid;

      if (!(procpid = atoi(entry->d_name))) continue;

      snprintf(toybuf, sizeof(toybuf), "/proc/%d/stat", procpid);
      if (!readfile(toybuf, toybuf, sizeof(toybuf))) continue;
      if (sscanf(toybuf, "%*d %*s %*c %*d %*d %d", &procsid) != 1) continue;
      if (pid == procpid || sid == procsid || procpid == 1) continue;

      // Check for kernel threads.
      snprintf(toybuf, sizeof(toybuf), "/proc/%d/cmdline", procpid);
      if (!readfile(toybuf, toybuf, sizeof(toybuf)) || !*toybuf) continue;

      // Check with omit list.
      for (count = 0; count < ocount; count++) 
        if (procpid == olist[count]) break;
      if (count != ocount) continue;

      kill(procpid, signum);
    }
    if (CFG_TOYBOX_FREE) {
      closedir(dp);
      free(olist);
    }

  // is it kill?
  } else {

    // "<1" in optstr wouldn't cover this because "-SIGNAL"
    if (!*args) help_exit("missing argument");

    while (*args) {
      char *arg = *(args++);

      pid = strtol(arg, &tmp, 10);
      if (*tmp || kill(pid, signum) < 0) error_msg("unknown pid '%s'", arg);
    }
  }
}

void killall5_main(void)
{
  kill_main();
}