diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-12-08 23:19:36 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-12-08 23:19:36 +0000 |
commit | abc0f4f8f97b36f2865986374405d091cefea107 (patch) | |
tree | ee7605752c6323682f0ef0879cc48f95bdf13e37 /kill.c | |
parent | 2285f367e220af9bda9f544945007725a02032dd (diff) | |
download | busybox-w32-abc0f4f8f97b36f2865986374405d091cefea107.tar.gz busybox-w32-abc0f4f8f97b36f2865986374405d091cefea107.tar.bz2 busybox-w32-abc0f4f8f97b36f2865986374405d091cefea107.zip |
Latest and greatest
Diffstat (limited to 'kill.c')
-rw-r--r-- | kill.c | 112 |
1 files changed, 84 insertions, 28 deletions
@@ -26,8 +26,14 @@ | |||
26 | #include <unistd.h> | 26 | #include <unistd.h> |
27 | #include <signal.h> | 27 | #include <signal.h> |
28 | #include <ctype.h> | 28 | #include <ctype.h> |
29 | #include <sys/stat.h> | ||
30 | #include <unistd.h> | ||
31 | |||
32 | static const char* kill_usage = "kill [-signal] process-id [process-id ...]\n\n" | ||
33 | "Send a signal (default is SIGTERM) to the specified process(es).\n\n" | ||
34 | "Options:\n" | ||
35 | "\t-l\tList all signal names and numbers.\n\n"; | ||
29 | 36 | ||
30 | const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n"; | ||
31 | 37 | ||
32 | struct signal_name { | 38 | struct signal_name { |
33 | const char *name; | 39 | const char *name; |
@@ -114,42 +120,92 @@ const struct signal_name signames[] = { | |||
114 | extern int kill_main (int argc, char **argv) | 120 | extern int kill_main (int argc, char **argv) |
115 | { | 121 | { |
116 | int sig = SIGTERM; | 122 | int sig = SIGTERM; |
123 | |||
124 | argc--; | ||
125 | argv++; | ||
126 | /* Parse any options */ | ||
127 | if (argc < 1) | ||
128 | usage(kill_usage); | ||
117 | 129 | ||
118 | if ( argc < 2 ) | 130 | while (argc > 0 && **argv == '-') { |
119 | usage (kill_usage); | 131 | while (*++(*argv)) { |
132 | switch (**argv) { | ||
133 | case 'l': | ||
134 | { | ||
135 | int col=0; | ||
136 | const struct signal_name *s = signames; | ||
120 | 137 | ||
121 | if ( **(argv+1) == '-' ) { | 138 | while (s->name != 0) { |
122 | if (isdigit( *(*(++argv)+1) )) { | 139 | col+=fprintf(stderr, "%2d) %-8s", s->number, (s++)->name); |
123 | sig = atoi (*argv); | 140 | if (col>60) { |
124 | if (sig < 0 || sig >= NSIG) | 141 | fprintf(stderr, "\n"); |
125 | goto end; | 142 | col=0; |
126 | } | 143 | } |
127 | else { | 144 | } |
128 | const struct signal_name *s = signames; | 145 | fprintf(stderr, "\n\n"); |
129 | while (s->name != 0) { | 146 | exit( TRUE); |
130 | if (strcasecmp (s->name, *argv+1) == 0) { | 147 | } |
131 | sig = s->number; | 148 | break; |
132 | break; | 149 | case '-': |
150 | usage(kill_usage); | ||
151 | default: | ||
152 | { | ||
153 | if (isdigit( **argv)) { | ||
154 | sig = atoi (*argv); | ||
155 | if (sig < 0 || sig >= NSIG) | ||
156 | goto end; | ||
157 | else { | ||
158 | argc--; | ||
159 | argv++; | ||
160 | goto do_it_now; | ||
161 | } | ||
162 | } | ||
163 | else { | ||
164 | const struct signal_name *s = signames; | ||
165 | while (s->name != 0) { | ||
166 | if (strcasecmp (s->name, *argv) == 0) { | ||
167 | sig = s->number; | ||
168 | argc--; | ||
169 | argv++; | ||
170 | goto do_it_now; | ||
171 | } | ||
172 | s++; | ||
173 | } | ||
174 | if (s->name == 0) | ||
175 | goto end; | ||
176 | } | ||
133 | } | 177 | } |
134 | s++; | ||
135 | } | 178 | } |
136 | if (s->name == 0) | 179 | argc--; |
137 | goto end; | 180 | argv++; |
138 | } | 181 | } |
139 | } | 182 | } |
140 | 183 | ||
141 | while (--argc > 1) { | 184 | do_it_now: |
142 | int pid; | 185 | |
143 | if (! isdigit( **(++argv))) { | 186 | while (argc >= 1) { |
144 | fprintf(stderr, "bad PID: %s\n", *argv); | 187 | int pid; |
145 | exit( FALSE); | 188 | struct stat statbuf; |
146 | } | 189 | char pidpath[20]="/proc/"; |
147 | pid = atoi (*argv); | 190 | |
148 | if (kill (pid, sig) != 0) { | 191 | if (! isdigit( **argv)) { |
149 | perror (*argv); | 192 | fprintf(stderr, "bad PID: %s\n", *argv); |
150 | exit ( FALSE); | 193 | exit( FALSE); |
194 | } | ||
195 | pid = atoi (*argv); | ||
196 | snprintf(pidpath, 20, "/proc/%s/stat", *argv); | ||
197 | if (stat( pidpath, &statbuf)!=0) { | ||
198 | fprintf(stderr, "kill: (%d) - No such pid\n", pid); | ||
199 | exit( FALSE); | ||
151 | } | 200 | } |
201 | if (kill (pid, sig) != 0) { | ||
202 | perror (*argv); | ||
203 | exit ( FALSE); | ||
204 | } | ||
205 | argv++; | ||
152 | } | 206 | } |
207 | exit ( TRUE); | ||
208 | |||
153 | 209 | ||
154 | end: | 210 | end: |
155 | fprintf(stderr, "bad signal name: %s\n", *argv); | 211 | fprintf(stderr, "bad signal name: %s\n", *argv); |