[patch] killall5 -o (v2)
Maxime Bizon
mbizon at freebox.fr
Fri Feb 6 21:36:01 UTC 2009
On Fri, 2009-02-06 at 21:55 +0100, Maxime Bizon wrote:
> The list of pid to omit has a static size, we could scan argv when
> looping over the process list, but the current argument parsing code
> does not make it easy to do so.
Actually the omit list has to be given at the end, as for kill pid list,
so this one is correct and much better. I changed the usage.h file too.
--- busybox/procps/kill.c.old 2009-02-06 21:23:51.000000000 +0100
+++ busybox/procps/kill.c 2009-02-06 22:33:34.000000000 +0100
@@ -97,6 +97,12 @@
}
arg++; /* skip '-' */
+
+ if (killall5 && arg[0] == 'o') {
+ /* pid omit list starts here */
+ goto do_it_now;
+ }
+
if (argc > 1 && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
argc--;
arg = *++argv;
@@ -115,6 +121,11 @@
if (killall5) {
pid_t sid;
procps_status_t* p = NULL;
+ int ret = 0;
+
+ /* remaining args should be -o pid [-o pid ...] */
+ if (argc & 1)
+ bb_show_usage();
/* Find out our own session id */
sid = getsid(pid);
@@ -122,12 +133,47 @@
kill(-1, SIGSTOP);
/* Now kill all processes except our session */
while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
- if (p->sid != (unsigned)sid && p->pid != (unsigned)pid && p->pid != 1)
+ int i, found;
+
+ if (p->sid == (unsigned)sid ||
+ p->pid == (unsigned)pid ||
+ p->pid == 1)
+ continue;
+
+ /* check all "-o omitpid" */
+ found = 0;
+ for (i = 0; i < argc - 1; i += 2) {
+ pid_t omit;
+
+ arg = argv[i];
+ if (arg[0] != '-' || arg[1] != 'o') {
+ bb_error_msg("bad option '%s'", arg);
+ ret = 1;
+ goto resume;
+ }
+
+ arg = argv[i + 1];
+ omit = bb_strtoi(arg, NULL, 10);
+ if (errno) {
+ bb_error_msg("bad pid '%s'", arg);
+ ret = 1;
+ goto resume;
+ }
+
+ if (p->pid == omit) {
+ found = 1;
+ break;
+ }
+ }
+
+ /* kill unless found */
+ if (!found)
kill(p->pid, signo);
}
+resume:
/* And let them continue */
kill(-1, SIGCONT);
- return 0;
+ return ret;
}
/* Pid or name is required for kill/killall */
--- busybox/include/usage.h.old 2009-02-06 22:05:07.000000000 +0100
+++ busybox/include/usage.h 2009-02-06 22:04:48.000000000 +0100
@@ -2123,11 +2123,12 @@
"$ killall apache\n"
#define killall5_trivial_usage \
- "[-l] [-SIG]"
+ "[-l] [-SIG] [-o omitpid] [-o omitpid...] "
#define killall5_full_usage "\n\n" \
"Send a signal (default is TERM) to all processes outside current session\n" \
"\nOptions:" \
- "\n -l List all signal names and numbers" \
+ "\n -l List all signal names and numbers" \
+ "\n -o omitpid Tells killall5 to omit processes with that process id." \
/* "\n -s SIG Yet another way of specifying SIG" */ \
#define klogd_trivial_usage \
--
Maxime
More information about the busybox
mailing list