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#define FOR_kill
44#include "toys.h"
45
46GLOBALS(
47 char *s;
48 struct arg_list *o;
49)
50
51
52
53#define CLEANUP_kill
54#define FOR_killall5
55#include "generated/flags.h"
56
57void kill_main(void)
58{
59 int signum;
60 char *tmp, **args = toys.optargs;
61 pid_t pid;
62
63
64 if (toys.optflags & FLAG_l) {
65 if (*args) {
66 int signum = sig_to_num(*args);
67 char *s = NULL;
68
69 if (signum>=0) s = num_to_sig(signum&127);
70 puts(s ? s : "UNKNOWN");
71 } else sig_to_num(NULL);
72 return;
73 }
74
75
76
77 if (!TT.s && *args && **args=='-') TT.s = *(args++)+1;
78 if (TT.s) {
79 char *arg;
80 int i = strtol(TT.s, &arg, 10);
81 if (!*arg) arg = num_to_sig(i);
82 else arg = TT.s;
83
84 if (!arg || -1 == (signum = sig_to_num(arg)))
85 error_exit("Unknown signal '%s'", arg);
86 } else signum = SIGTERM;
87
88
89 if (CFG_KILLALL5 && toys.which->name[4]=='a') {
90 DIR *dp;
91 struct dirent *entry;
92 int pid, sid;
93 long *olist = 0, ocount = 0;
94
95
96 if (toys.optflags & FLAG_o) {
97 struct arg_list *ptr;
98
99 for (ptr = TT.o; ptr; ptr = ptr->next) ocount++;
100 olist = xmalloc(ocount*sizeof(long));
101 ocount = 0;
102 for (ptr = TT.o; ptr; ptr=ptr->next) olist[ocount++] = atolx(ptr->arg);
103 }
104
105 sid = getsid(pid = getpid());
106
107 if (!(dp = opendir("/proc"))) perror_exit("/proc");
108 while ((entry = readdir(dp))) {
109 int count, procpid, procsid;
110
111 if (!(procpid = atoi(entry->d_name))) continue;
112
113 snprintf(toybuf, sizeof(toybuf), "/proc/%d/stat", procpid);
114 if (!readfile(toybuf, toybuf, sizeof(toybuf))) continue;
115 if (sscanf(toybuf, "%*d %*s %*c %*d %*d %d", &procsid) != 1) continue;
116 if (pid == procpid || sid == procsid || procpid == 1) continue;
117
118
119 snprintf(toybuf, sizeof(toybuf), "/proc/%d/cmdline", procpid);
120 if (!readfile(toybuf, toybuf, sizeof(toybuf)) || !*toybuf) continue;
121
122
123 for (count = 0; count < ocount; count++)
124 if (procpid == olist[count]) break;
125 if (count != ocount) continue;
126
127 kill(procpid, signum);
128 }
129 if (CFG_TOYBOX_FREE) {
130 closedir(dp);
131 free(olist);
132 }
133
134
135 } else {
136
137
138 if (!*args) help_exit("missing argument");
139
140 while (*args) {
141 char *arg = *(args++);
142
143 pid = strtol(arg, &tmp, 10);
144 if (*tmp || kill(pid, signum) < 0) error_msg("unknown pid '%s'", arg);
145 }
146 }
147}
148
149void killall5_main(void)
150{
151 kill_main();
152}
153