Hello all,

I've extended killall to support the -v (verbose) and -i (interactive) option as described in http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html sample output:

Suppose 5 yes'es are running:

# Kill 3 of them
edb@lapedb:~/edb-stuff/toybox/toybox$ ./toybox killall -vi -9 yes
Signal yes(11452) ? (y/N):
Signal yes(11453) ? (y/N):N
Signal yes(11454) ? (y/N):y
Killed yes(11454) with signal 9
Signal yes(11455) ? (y/N):y
Killed yes(11455) with signal 9
Signal yes(11456) ? (y/N):y
Killed yes(11456) with signal 9
# Kill none of the remaining
edb@lapedb:~/edb-stuff/toybox/toybox$ ./toybox killall -vi -9 yes
Signal yes(11452) ? (y/N):
Signal yes(11453) ? (y/N):
killall: No such process
# Kill all remaining
edb@lapedb:~/edb-stuff/toybox/toybox$ ./toybox killall -vi -9 yes
Signal yes(11452) ? (y/N):y
Killed yes(11452) with signal 9
Signal yes(11453) ? (y/N):y
Killed yes(11453) with signal 9
# None left
edb@lapedb:~/edb-stuff/toybox/toybox$ ./toybox killall -vi -9 yes
killall: No such process

my 2 cents
E.


--
Elie De Brauwer

# HG changeset patch
# User Elie De Brauwer <[email protected]>
# Date 1355661816 -3600
# Node ID ad5653b98b4d03b1c2c156a8906f2e6c57fdfe56
# Parent  0305ce03173f4e077cc35e84e25d18f90cd3554c
Extend killall with support for -v and -i

diff -r 0305ce03173f -r ad5653b98b4d lib/lib.c
--- a/lib/lib.c	Sun Dec 16 12:31:15 2012 +0100
+++ b/lib/lib.c	Sun Dec 16 13:43:36 2012 +0100
@@ -975,7 +975,7 @@
 }
 
 // Execute a callback for each PID that matches a process name from a list.
-void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid))
+void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name))
 {
   DIR *dp;
   struct dirent *entry;
@@ -999,7 +999,7 @@
 
     for (curname = names; *curname; curname++)
       if (!strcmp(basename(cmd), *curname)) 
-          if (!callback(atol(entry->d_name))) goto done;
+          if (!callback(atol(entry->d_name), *curname)) goto done;
   }
 done:
   closedir(dp);
diff -r 0305ce03173f -r ad5653b98b4d lib/lib.h
--- a/lib/lib.h	Sun Dec 16 12:31:15 2012 +0100
+++ b/lib/lib.h	Sun Dec 16 13:43:36 2012 +0100
@@ -144,7 +144,7 @@
 void crc_init(unsigned int *crc_table, int little_endian);
 void terminal_size(unsigned *x, unsigned *y);
 int yesno(char *prompt, int def);
-void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid));
+void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name));
 unsigned long xstrtoul(const char *nptr, char **endptr, int base);
 
 // getmountlist.c
diff -r 0305ce03173f -r ad5653b98b4d toys/lsb/killall.c
--- a/toys/lsb/killall.c	Sun Dec 16 12:31:15 2012 +0100
+++ b/toys/lsb/killall.c	Sun Dec 16 13:43:36 2012 +0100
@@ -1,20 +1,23 @@
+
 /* killall.c - Send signal (default: TERM) to all processes with given names.
  *
  * Copyright 2012 Andreas Heck <[email protected]>
  *
  * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html
 
-USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN))
+USE_KILLALL(NEWTOY(killall, "<1?lqvi", TOYFLAG_USR|TOYFLAG_BIN))
 
 config KILLALL
   bool "killall"
   default y
   help
-    usage: killall [-l] [-q] [-SIG] PROCESS_NAME...
+    usage: killall [-l] [-qv] [-SIG] PROCESS_NAME...
 
     Send a signal (default: TERM) to all processes with the given names.
 
     -l	print list of all available signals
+    -i	ask for confirmation before killing
+    -v	report if the signal was successfully sent
     -q	don't print any warnings or error messages
 */
 
@@ -25,12 +28,20 @@
   int signum;
 )
 
-static int kill_process(pid_t pid)
+static int kill_process(pid_t pid, char *name)
 {
   int ret;
 
+  if(toys.optflags & FLAG_i) {
+    snprintf(toybuf, sizeof(toybuf), "Signal %s(%d) ?", name, pid);
+    if (yesno(toybuf, 0) == 0) return 1;
+  }
+
   toys.exitval = 0;
+
   ret = kill(pid, TT.signum);
+  if (toys.optflags & FLAG_v)
+    printf("Killed %s(%d) with signal %d\n", name, pid, TT.signum);
 
   if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill");
   return 1;
diff -r 0305ce03173f -r ad5653b98b4d toys/lsb/pidof.c
--- a/toys/lsb/pidof.c	Sun Dec 16 12:31:15 2012 +0100
+++ b/toys/lsb/pidof.c	Sun Dec 16 13:43:36 2012 +0100
@@ -25,7 +25,7 @@
   char *omit;
 )
 
-static int print_pid(pid_t pid)
+static int print_pid(pid_t pid, char * name)
 {
   char * res;
   int len;
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to