diff options
Diffstat (limited to 'kill.c')
-rw-r--r-- | kill.c | 63 |
1 files changed, 40 insertions, 23 deletions
@@ -24,6 +24,7 @@ | |||
24 | #include "internal.h" | 24 | #include "internal.h" |
25 | #include <stdio.h> | 25 | #include <stdio.h> |
26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
27 | #include <errno.h> | ||
27 | #include <unistd.h> | 28 | #include <unistd.h> |
28 | #include <signal.h> | 29 | #include <signal.h> |
29 | #include <ctype.h> | 30 | #include <ctype.h> |
@@ -35,6 +36,14 @@ static const char *kill_usage = | |||
35 | "Send a signal (default is SIGTERM) to the specified process(es).\n\n" | 36 | "Send a signal (default is SIGTERM) to the specified process(es).\n\n" |
36 | "Options:\n" "\t-l\tList all signal names and numbers.\n\n"; | 37 | "Options:\n" "\t-l\tList all signal names and numbers.\n\n"; |
37 | 38 | ||
39 | static const char *killall_usage = | ||
40 | "killall [-signal] process-name [process-name ...]\n\n" | ||
41 | "Send a signal (default is SIGTERM) to the specified process(es).\n\n" | ||
42 | "Options:\n" "\t-l\tList all signal names and numbers.\n\n"; | ||
43 | |||
44 | |||
45 | #define KILL 0 | ||
46 | #define KILLALL 1 | ||
38 | 47 | ||
39 | struct signal_name { | 48 | struct signal_name { |
40 | const char *name; | 49 | const char *name; |
@@ -120,13 +129,19 @@ const struct signal_name signames[] = { | |||
120 | 129 | ||
121 | extern int kill_main(int argc, char **argv) | 130 | extern int kill_main(int argc, char **argv) |
122 | { | 131 | { |
123 | int sig = SIGTERM; | 132 | int whichApp, sig = SIGTERM; |
133 | const char *appUsage; | ||
134 | |||
135 | /* Figure out what we are trying to do here */ | ||
136 | whichApp = (strcmp(*argv, "killall") == 0)? | ||
137 | KILLALL : KILL; | ||
138 | appUsage = (whichApp == KILLALL)? killall_usage : kill_usage; | ||
124 | 139 | ||
125 | argc--; | 140 | argc--; |
126 | argv++; | 141 | argv++; |
127 | /* Parse any options */ | 142 | /* Parse any options */ |
128 | if (argc < 1) | 143 | if (argc < 1) |
129 | usage(kill_usage); | 144 | usage(appUsage); |
130 | 145 | ||
131 | while (argc > 0 && **argv == '-') { | 146 | while (argc > 0 && **argv == '-') { |
132 | while (*++(*argv)) { | 147 | while (*++(*argv)) { |
@@ -150,7 +165,7 @@ extern int kill_main(int argc, char **argv) | |||
150 | } | 165 | } |
151 | break; | 166 | break; |
152 | case '-': | 167 | case '-': |
153 | usage(kill_usage); | 168 | usage(appUsage); |
154 | default: | 169 | default: |
155 | { | 170 | { |
156 | if (isdigit(**argv)) { | 171 | if (isdigit(**argv)) { |
@@ -186,32 +201,34 @@ extern int kill_main(int argc, char **argv) | |||
186 | 201 | ||
187 | do_it_now: | 202 | do_it_now: |
188 | 203 | ||
189 | while (--argc >= 0) { | 204 | if (whichApp == KILL) { |
190 | int pid; | 205 | /* Looks like they want to do a kill. Do that */ |
191 | struct stat statbuf; | 206 | while (--argc >= 0) { |
192 | char pidpath[20] = "/proc/"; | 207 | int pid; |
193 | 208 | ||
194 | if (!isdigit(**argv)) { | 209 | if (!isdigit(**argv)) |
195 | fprintf(stderr, "bad PID: %s\n", *argv); | 210 | fatalError( "Bad PID: %s\n", strerror(errno)); |
196 | exit(FALSE); | 211 | pid = strtol(*argv, NULL, 0); |
197 | } | 212 | if (kill(pid, sig) != 0) |
198 | pid = atoi(*argv); | 213 | fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno)); |
199 | snprintf(pidpath, 20, "/proc/%s/stat", *argv); | 214 | argv++; |
200 | if (stat(pidpath, &statbuf) != 0) { | ||
201 | fprintf(stderr, "kill: (%d) - No such pid\n", pid); | ||
202 | exit(FALSE); | ||
203 | } | 215 | } |
204 | fprintf(stderr, "sig = %d\n", sig); | 216 | } else { |
205 | if (kill(pid, sig) != 0) { | 217 | /* Looks like they want to do a killall. Do that */ |
206 | perror(*argv); | 218 | while (--argc >= 0) { |
207 | exit(FALSE); | 219 | int pid; |
220 | |||
221 | while((pid = findPidByName( *argv))) { | ||
222 | if (kill(pid, sig) != 0) | ||
223 | fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno)); | ||
224 | } | ||
225 | argv++; | ||
208 | } | 226 | } |
209 | argv++; | ||
210 | } | 227 | } |
228 | |||
211 | exit(TRUE); | 229 | exit(TRUE); |
212 | 230 | ||
213 | 231 | ||
214 | end: | 232 | end: |
215 | fprintf(stderr, "bad signal name: %s\n", *argv); | 233 | fatalError( "bad signal name: %s\n", *argv); |
216 | exit(TRUE); | ||
217 | } | 234 | } |