[patch] killall5 -o

Maxime Bizon mbizon at freebox.fr
Fri Feb 6 20:55:27 UTC 2009


Hi guys,

killall5 currently lacks "-o" flag to specify a list of pid to omit, the
following patch implements it.

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.



--- busybox/procps/kill.c.old	2009-02-06 21:23:51.000000000 +0100
+++ busybox/procps/kill.c	2009-02-06 21:46:16.000000000 +0100
@@ -23,12 +23,14 @@
  * kill %n gets translated into kill ' -<process group>' by shell (note space!)
  * This is needed to avoid collision with kill -9 ... syntax
  */
+#define MAX_KILLALL5_OMIT	8
 
 int kill_main(int argc, char **argv)
 {
 	char *arg;
 	pid_t pid;
 	int signo = SIGTERM, errors = 0, quiet = 0;
+	int omit[MAX_KILLALL5_OMIT + 1];
 #if !ENABLE_KILLALL && !ENABLE_KILLALL5
 #define killall 0
 #define killall5 0
@@ -42,6 +44,9 @@
 #define killall5 (ENABLE_KILLALL5 && char3 == 'l')
 #endif
 
+	if (killall5)
+		omit[0] = -1;
+
 	/* Parse any options */
 	argc--;
 	arg = *++argv;
@@ -96,6 +101,29 @@
 		if (arg[0] != '-') goto do_it_now;
 	}
 
+	/* the killall5 -o option */
+	if (killall5) {
+		int i;
+
+		/* fetch all "-o omitpid" */
+		i = 0;
+		while (argc >= 2 && arg[0] == '-' && arg[1] == 'o') {
+			arg = *++argv;
+			/* warn user if we can't take any more */
+			if (i == MAX_KILLALL5_OMIT - 1)
+				bb_error_msg_and_die("too many -o");
+			omit[i++] = bb_strtoi(arg, NULL, 10);
+			if (errno)
+				bb_error_msg_and_die("bad pid '%s'", arg);
+			arg = *++argv;
+			argc -= 2;
+		}
+
+		/* terminate pid list */
+		omit[i] = -1;
+		if (argc < 1) goto do_it_now;
+	}
+
 	arg++; /* skip '-' */
 	if (argc > 1 && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
 		argc--;
@@ -122,7 +150,22 @@
 		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;
+
+			/* skip init process too */
+			if (p->sid == (unsigned)sid ||
+			    p->pid == (unsigned)pid ||
+			    p->pid == 1)
+				continue;
+
+			/* check if pid is in to-omit list */
+			for (i = 0; omit[i] != -1; i++) {
+				if (p->pid == omit[i])
+					break;
+			}
+
+			/* kill unless found */
+			if (omit[i] == -1)
 				kill(p->pid, signo);
 		}
 		/* And let them continue */


-- 
Maxime




More information about the busybox mailing list