aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
Diffstat (limited to 'procps')
-rw-r--r--procps/Config.in5
-rw-r--r--procps/kill.c76
2 files changed, 53 insertions, 28 deletions
diff --git a/procps/Config.in b/procps/Config.in
index 2183fab9a..2d4ad3bd4 100644
--- a/procps/Config.in
+++ b/procps/Config.in
@@ -38,6 +38,11 @@ config CONFIG_KILLALL
38 specified commands. If no signal name is specified, SIGTERM is 38 specified commands. If no signal name is specified, SIGTERM is
39 sent. 39 sent.
40 40
41config CONFIG_KILLALL5
42 bool "killall5"
43 default n
44 depends on CONFIG_KILL
45
41config CONFIG_PIDOF 46config CONFIG_PIDOF
42 bool "pidof" 47 bool "pidof"
43 default n 48 default n
diff --git a/procps/kill.c b/procps/kill.c
index 654467ac1..fe9b24200 100644
--- a/procps/kill.c
+++ b/procps/kill.c
@@ -13,17 +13,18 @@
13int kill_main(int argc, char **argv) 13int kill_main(int argc, char **argv)
14{ 14{
15 char *arg; 15 char *arg;
16 int killall, signo = SIGTERM, errors = 0, quiet = 0; 16 pid_t pid;
17 17 int signo = SIGTERM, errors = 0, quiet = 0;
18 killall = (ENABLE_KILLALL && bb_applet_name[4]=='a') ? 1 : 0; 18 const int killall = (ENABLE_KILLALL && bb_applet_name[4]=='a'
19 && (!ENABLE_KILLALL5 || bb_applet_name[7]!='5'));
20 const int killall5 = (ENABLE_KILLALL5 && bb_applet_name[4]=='a'
21 && (!ENABLE_KILLALL || bb_applet_name[7]=='5'));
19 22
20 /* Parse any options */ 23 /* Parse any options */
21 argc--; 24 argc--;
22 arg = *++argv; 25 arg = *++argv;
23 if (argc<1)
24 bb_show_usage();
25 26
26 if (arg[0]!='-') { 27 if (argc<1 || arg[0]!='-') {
27 goto do_it_now; 28 goto do_it_now;
28 } 29 }
29 30
@@ -79,42 +80,48 @@ int kill_main(int argc, char **argv)
79 80
80do_it_now: 81do_it_now:
81 82
82 /* Pid or name required */ 83 if (killall5) {
83 if (argc<1) 84 pid_t sid;
84 bb_show_usage(); 85 procps_status_t* p;
85 86
86 if (!killall) { 87 /* kill(-1, sig) on Linux (at least 2.1.x)
87 /* Looks like they want to do a kill. Do that */ 88 * might send signal to the calling process too */
88 while (arg) { 89 signal(SIGTERM, SIG_IGN);
89 int pid; 90 /* Now stop all processes */
90 91 kill(-1, SIGSTOP);
91 if (!isdigit(arg[0]) && arg[0]!='-') 92 /* Find out our own session id */
92 bb_error_msg_and_die("bad pid '%s'", arg); 93 pid = getpid();
93 pid = strtol(arg, NULL, 0); 94 sid = getsid(pid);
94 if (kill(pid, signo)!=0) { 95 /* Now kill all processes except our session */
95 bb_perror_msg("cannot kill pid %d", pid); 96 while ((p = procps_scan(0))!=0) {
96 errors++; 97 if (getsid(p->pid)!=sid && p->pid!=pid && p->pid!=1)
97 } 98 kill(p->pid, signo);
98 arg = *++argv;
99 } 99 }
100 /* And let them continue */
101 kill(-1, SIGCONT);
102 return 0;
103 }
100 104
101 } else { 105 /* Pid or name required for kill/killall */
102 pid_t myPid = getpid(); 106 if (argc<1)
107 bb_show_usage();
103 108
109 if (killall) {
104 /* Looks like they want to do a killall. Do that */ 110 /* Looks like they want to do a killall. Do that */
111 pid = getpid();
105 while (arg) { 112 while (arg) {
106 long* pidList; 113 long* pidList;
107 114
108 pidList = find_pid_by_name(arg); 115 pidList = find_pid_by_name(arg);
109 if (!pidList || *pidList<=0) { 116 if (!pidList || *pidList<=0) {
110 errors++; 117 errors++;
111 if (quiet==0) 118 if (!quiet)
112 bb_error_msg("%s: no process killed", arg); 119 bb_error_msg("%s: no process killed", arg);
113 } else { 120 } else {
114 long *pl; 121 long *pl;
115 122
116 for (pl = pidList; *pl!=0 ; pl++) { 123 for (pl = pidList; *pl!=0; pl++) {
117 if (*pl==myPid) 124 if (*pl==pid)
118 continue; 125 continue;
119 if (kill(*pl, signo)!=0) { 126 if (kill(*pl, signo)!=0) {
120 errors++; 127 errors++;
@@ -126,7 +133,20 @@ do_it_now:
126 free(pidList); 133 free(pidList);
127 arg = *++argv; 134 arg = *++argv;
128 } 135 }
136 return errors;
129 } 137 }
130 138
139 /* Looks like they want to do a kill. Do that */
140 while (arg) {
141 if (!isdigit(arg[0]) && arg[0]!='-')
142 bb_error_msg_and_die("bad pid '%s'", arg);
143 pid = strtol(arg, NULL, 0);
144 /* FIXME: better overflow check? */
145 if (kill(pid, signo)!=0) {
146 bb_perror_msg("cannot kill pid %ld", (long)pid);
147 errors++;
148 }
149 arg = *++argv;
150 }
131 return errors; 151 return errors;
132} 152}