diff options
Diffstat (limited to 'kill.c')
-rw-r--r-- | kill.c | 299 |
1 files changed, 150 insertions, 149 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini kill implementation for busybox | 3 | * Mini kill implementation for busybox |
3 | * | 4 | * |
@@ -29,188 +30,188 @@ | |||
29 | #include <sys/stat.h> | 30 | #include <sys/stat.h> |
30 | #include <unistd.h> | 31 | #include <unistd.h> |
31 | 32 | ||
32 | static const char* kill_usage = "kill [-signal] process-id [process-id ...]\n\n" | 33 | static const char *kill_usage = |
33 | "Send a signal (default is SIGTERM) to the specified process(es).\n\n" | 34 | "kill [-signal] process-id [process-id ...]\n\n" |
34 | "Options:\n" | 35 | "Send a signal (default is SIGTERM) to the specified process(es).\n\n" |
35 | "\t-l\tList all signal names and numbers.\n\n"; | 36 | "Options:\n" "\t-l\tList all signal names and numbers.\n\n"; |
36 | 37 | ||
37 | 38 | ||
38 | struct signal_name { | 39 | struct signal_name { |
39 | const char *name; | 40 | const char *name; |
40 | int number; | 41 | int number; |
41 | }; | 42 | }; |
42 | 43 | ||
43 | const struct signal_name signames[] = { | 44 | const struct signal_name signames[] = { |
44 | {"HUP", SIGHUP}, | 45 | {"HUP", SIGHUP}, |
45 | {"INT", SIGINT}, | 46 | {"INT", SIGINT}, |
46 | {"QUIT", SIGQUIT}, | 47 | {"QUIT", SIGQUIT}, |
47 | {"ILL", SIGILL}, | 48 | {"ILL", SIGILL}, |
48 | {"TRAP", SIGTRAP}, | 49 | {"TRAP", SIGTRAP}, |
49 | {"ABRT", SIGABRT}, | 50 | {"ABRT", SIGABRT}, |
50 | #ifndef __alpha__ | 51 | #ifndef __alpha__ |
51 | {"IOT", SIGIOT}, | 52 | {"IOT", SIGIOT}, |
52 | #endif | 53 | #endif |
53 | #if defined(__sparc__) || defined(__alpha__) | 54 | #if defined(__sparc__) || defined(__alpha__) |
54 | {"EMT", SIGEMT}, | 55 | {"EMT", SIGEMT}, |
55 | #else | 56 | #else |
56 | {"BUS", SIGBUS}, | 57 | {"BUS", SIGBUS}, |
57 | #endif | 58 | #endif |
58 | {"FPE", SIGFPE}, | 59 | {"FPE", SIGFPE}, |
59 | {"KILL", SIGKILL}, | 60 | {"KILL", SIGKILL}, |
60 | #if defined(__sparc__) || defined(__alpha__) | 61 | #if defined(__sparc__) || defined(__alpha__) |
61 | {"BUS", SIGBUS}, | 62 | {"BUS", SIGBUS}, |
62 | #else | 63 | #else |
63 | {"USR1", SIGUSR1}, | 64 | {"USR1", SIGUSR1}, |
64 | #endif | 65 | #endif |
65 | {"SEGV", SIGSEGV}, | 66 | {"SEGV", SIGSEGV}, |
66 | #if defined(__sparc__) || defined(__alpha__) | 67 | #if defined(__sparc__) || defined(__alpha__) |
67 | {"SYS", SIGSYS}, | 68 | {"SYS", SIGSYS}, |
68 | #else | 69 | #else |
69 | {"USR2", SIGUSR2}, | 70 | {"USR2", SIGUSR2}, |
70 | #endif | 71 | #endif |
71 | {"PIPE", SIGPIPE}, | 72 | {"PIPE", SIGPIPE}, |
72 | {"ALRM", SIGALRM}, | 73 | {"ALRM", SIGALRM}, |
73 | {"TERM", SIGTERM}, | 74 | {"TERM", SIGTERM}, |
74 | #if defined(__sparc__) || defined(__alpha__) | 75 | #if defined(__sparc__) || defined(__alpha__) |
75 | {"URG", SIGURG}, | 76 | {"URG", SIGURG}, |
76 | {"STOP", SIGSTOP}, | 77 | {"STOP", SIGSTOP}, |
77 | {"TSTP", SIGTSTP}, | 78 | {"TSTP", SIGTSTP}, |
78 | {"CONT", SIGCONT}, | 79 | {"CONT", SIGCONT}, |
79 | {"CHLD", SIGCHLD}, | 80 | {"CHLD", SIGCHLD}, |
80 | {"TTIN", SIGTTIN}, | 81 | {"TTIN", SIGTTIN}, |
81 | {"TTOU", SIGTTOU}, | 82 | {"TTOU", SIGTTOU}, |
82 | {"IO", SIGIO}, | 83 | {"IO", SIGIO}, |
83 | # ifndef __alpha__ | 84 | # ifndef __alpha__ |
84 | {"POLL", SIGIO}, | 85 | {"POLL", SIGIO}, |
85 | # endif | 86 | # endif |
86 | {"XCPU", SIGXCPU}, | 87 | {"XCPU", SIGXCPU}, |
87 | {"XFSZ", SIGXFSZ}, | 88 | {"XFSZ", SIGXFSZ}, |
88 | {"VTALRM", SIGVTALRM}, | 89 | {"VTALRM", SIGVTALRM}, |
89 | {"PROF", SIGPROF}, | 90 | {"PROF", SIGPROF}, |
90 | {"WINCH", SIGWINCH}, | 91 | {"WINCH", SIGWINCH}, |
91 | # ifdef __alpha__ | 92 | # ifdef __alpha__ |
92 | {"INFO", SIGINFO}, | 93 | {"INFO", SIGINFO}, |
93 | # else | 94 | # else |
94 | {"LOST", SIGLOST}, | 95 | {"LOST", SIGLOST}, |
95 | # endif | 96 | # endif |
96 | {"USR1", SIGUSR1}, | 97 | {"USR1", SIGUSR1}, |
97 | {"USR2", SIGUSR2}, | 98 | {"USR2", SIGUSR2}, |
98 | #else | 99 | #else |
99 | {"STKFLT", SIGSTKFLT}, | 100 | {"STKFLT", SIGSTKFLT}, |
100 | {"CHLD", SIGCHLD}, | 101 | {"CHLD", SIGCHLD}, |
101 | {"CONT", SIGCONT}, | 102 | {"CONT", SIGCONT}, |
102 | {"STOP", SIGSTOP}, | 103 | {"STOP", SIGSTOP}, |
103 | {"TSTP", SIGTSTP}, | 104 | {"TSTP", SIGTSTP}, |
104 | {"TTIN", SIGTTIN}, | 105 | {"TTIN", SIGTTIN}, |
105 | {"TTOU", SIGTTOU}, | 106 | {"TTOU", SIGTTOU}, |
106 | {"URG", SIGURG}, | 107 | {"URG", SIGURG}, |
107 | {"XCPU", SIGXCPU}, | 108 | {"XCPU", SIGXCPU}, |
108 | {"XFSZ", SIGXFSZ}, | 109 | {"XFSZ", SIGXFSZ}, |
109 | {"VTALRM", SIGVTALRM}, | 110 | {"VTALRM", SIGVTALRM}, |
110 | {"PROF", SIGPROF}, | 111 | {"PROF", SIGPROF}, |
111 | {"WINCH", SIGWINCH}, | 112 | {"WINCH", SIGWINCH}, |
112 | {"IO", SIGIO}, | 113 | {"IO", SIGIO}, |
113 | {"POLL", SIGPOLL}, | 114 | {"POLL", SIGPOLL}, |
114 | {"PWR", SIGPWR}, | 115 | {"PWR", SIGPWR}, |
115 | {"UNUSED", SIGUNUSED}, | 116 | {"UNUSED", SIGUNUSED}, |
116 | #endif | 117 | #endif |
117 | {0, 0} | 118 | {0, 0} |
118 | }; | 119 | }; |
119 | 120 | ||
120 | extern int kill_main (int argc, char **argv) | 121 | extern int kill_main(int argc, char **argv) |
121 | { | 122 | { |
122 | int sig = SIGTERM; | 123 | int sig = SIGTERM; |
123 | 124 | ||
124 | argc--; | 125 | argc--; |
125 | argv++; | 126 | argv++; |
126 | /* Parse any options */ | 127 | /* Parse any options */ |
127 | if (argc < 1) | 128 | if (argc < 1) |
128 | usage(kill_usage); | ||
129 | |||
130 | while (argc > 0 && **argv == '-') { | ||
131 | while (*++(*argv)) { | ||
132 | switch (**argv) { | ||
133 | case 'l': | ||
134 | { | ||
135 | int col=0; | ||
136 | const struct signal_name *s = signames; | ||
137 | |||
138 | while (s->name != 0) { | ||
139 | col+=fprintf(stderr, "%2d) %-8s", s->number, (s++)->name); | ||
140 | if (col>60) { | ||
141 | fprintf(stderr, "\n"); | ||
142 | col=0; | ||
143 | } | ||
144 | } | ||
145 | fprintf(stderr, "\n\n"); | ||
146 | exit( TRUE); | ||
147 | } | ||
148 | break; | ||
149 | case '-': | ||
150 | usage(kill_usage); | 129 | usage(kill_usage); |
151 | default: | 130 | |
152 | { | 131 | while (argc > 0 && **argv == '-') { |
153 | if (isdigit( **argv)) { | 132 | while (*++(*argv)) { |
154 | sig = atoi (*argv); | 133 | switch (**argv) { |
155 | if (sig < 0 || sig >= NSIG) | 134 | case 'l': |
156 | goto end; | 135 | { |
157 | else { | 136 | int col = 0; |
158 | argc--; | 137 | const struct signal_name *s = signames; |
159 | argv++; | 138 | |
160 | goto do_it_now; | 139 | while (s->name != 0) { |
161 | } | 140 | col += |
162 | } | 141 | fprintf(stderr, "%2d) %-8s", s->number, |
163 | else { | 142 | (s++)->name); |
164 | const struct signal_name *s = signames; | 143 | if (col > 60) { |
165 | while (s->name != 0) { | 144 | fprintf(stderr, "\n"); |
166 | if (strcasecmp (s->name, *argv) == 0) { | 145 | col = 0; |
167 | sig = s->number; | 146 | } |
168 | argc--; | 147 | } |
169 | argv++; | 148 | fprintf(stderr, "\n\n"); |
170 | goto do_it_now; | 149 | exit(TRUE); |
171 | } | 150 | } |
172 | s++; | 151 | break; |
152 | case '-': | ||
153 | usage(kill_usage); | ||
154 | default: | ||
155 | { | ||
156 | if (isdigit(**argv)) { | ||
157 | sig = atoi(*argv); | ||
158 | if (sig < 0 || sig >= NSIG) | ||
159 | goto end; | ||
160 | else { | ||
161 | argc--; | ||
162 | argv++; | ||
163 | goto do_it_now; | ||
164 | } | ||
165 | } else { | ||
166 | const struct signal_name *s = signames; | ||
167 | |||
168 | while (s->name != 0) { | ||
169 | if (strcasecmp(s->name, *argv) == 0) { | ||
170 | sig = s->number; | ||
171 | argc--; | ||
172 | argv++; | ||
173 | goto do_it_now; | ||
174 | } | ||
175 | s++; | ||
176 | } | ||
177 | if (s->name == 0) | ||
178 | goto end; | ||
179 | } | ||
180 | } | ||
173 | } | 181 | } |
174 | if (s->name == 0) | 182 | argc--; |
175 | goto end; | 183 | argv++; |
176 | } | ||
177 | } | 184 | } |
178 | } | ||
179 | argc--; | ||
180 | argv++; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | do_it_now: | ||
185 | |||
186 | while (--argc >= 0) { | ||
187 | int pid; | ||
188 | struct stat statbuf; | ||
189 | char pidpath[20]="/proc/"; | ||
190 | |||
191 | if (! isdigit( **argv)) { | ||
192 | fprintf(stderr, "bad PID: %s\n", *argv); | ||
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); | ||
200 | } | 185 | } |
201 | fprintf(stderr, "sig = %d\n", sig); | ||
202 | if (kill (pid, sig) != 0) { | ||
203 | perror (*argv); | ||
204 | exit ( FALSE); | ||
205 | } | ||
206 | argv++; | ||
207 | } | ||
208 | exit ( TRUE); | ||
209 | 186 | ||
187 | do_it_now: | ||
210 | 188 | ||
211 | end: | 189 | while (--argc >= 0) { |
212 | fprintf(stderr, "bad signal name: %s\n", *argv); | 190 | int pid; |
213 | exit (TRUE); | 191 | struct stat statbuf; |
214 | } | 192 | char pidpath[20] = "/proc/"; |
193 | |||
194 | if (!isdigit(**argv)) { | ||
195 | fprintf(stderr, "bad PID: %s\n", *argv); | ||
196 | exit(FALSE); | ||
197 | } | ||
198 | pid = atoi(*argv); | ||
199 | snprintf(pidpath, 20, "/proc/%s/stat", *argv); | ||
200 | if (stat(pidpath, &statbuf) != 0) { | ||
201 | fprintf(stderr, "kill: (%d) - No such pid\n", pid); | ||
202 | exit(FALSE); | ||
203 | } | ||
204 | fprintf(stderr, "sig = %d\n", sig); | ||
205 | if (kill(pid, sig) != 0) { | ||
206 | perror(*argv); | ||
207 | exit(FALSE); | ||
208 | } | ||
209 | argv++; | ||
210 | } | ||
211 | exit(TRUE); | ||
215 | 212 | ||
216 | 213 | ||
214 | end: | ||
215 | fprintf(stderr, "bad signal name: %s\n", *argv); | ||
216 | exit(TRUE); | ||
217 | } | ||