Update internal.h to conditionally include asm/string.h
[oweals/busybox.git] / kill.c
diff --git a/kill.c b/kill.c
index 8a99e0f9e99d016044a8c85974a91d3a787ee5e2..75277d9626c42b073a65421ac84ed08503ca8ce7 100644 (file)
--- a/kill.c
+++ b/kill.c
@@ -1,6 +1,6 @@
 /* vi: set sw=4 ts=4: */
 /*
- * Mini kill implementation for busybox
+ * Mini kill/killall implementation for busybox
  *
  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
  *
 #include <unistd.h>
 
 static const char *kill_usage =
-       "kill [-signal] process-id [process-id ...]\n\n"
-       "Send a signal (default is SIGTERM) to the specified process(es).\n\n"
-       "Options:\n" "\t-l\tList all signal names and numbers.\n\n";
+       "kill [-signal] process-id [process-id ...]\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+       "\nSend a signal (default is SIGTERM) to the specified process(es).\n\n"
+       "Options:\n" "\t-l\tList all signal names and numbers.\n\n"
+#endif
+       ;
 
+#ifdef BB_KILLALL
 static const char *killall_usage =
-       "killall [-signal] process-name [process-name ...]\n\n"
-       "Send a signal (default is SIGTERM) to the specified process(es).\n\n"
-       "Options:\n" "\t-l\tList all signal names and numbers.\n\n";
-
+       "killall [-signal] process-name [process-name ...]\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+       "\nSend a signal (default is SIGTERM) to the specified process(es).\n\n"
+       "Options:\n" "\t-l\tList all signal names and numbers.\n\n"
+#endif
+#endif
+       ;
 
 #define KILL   0
 #define KILLALL        1
@@ -132,10 +139,15 @@ extern int kill_main(int argc, char **argv)
        int whichApp, sig = SIGTERM;
        const char *appUsage;
 
+#ifdef BB_KILLALL
        /* Figure out what we are trying to do here */
        whichApp = (strcmp(*argv, "killall") == 0)? 
                KILLALL : KILL; 
        appUsage = (whichApp == KILLALL)?  killall_usage : kill_usage;
+#else
+       whichApp = KILL;
+       appUsage = kill_usage;
+#endif
 
        argc--;
        argv++;
@@ -213,18 +225,35 @@ extern int kill_main(int argc, char **argv)
                                fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno));
                        argv++;
                }
-       } else {
+       } 
+#ifdef BB_KILLALL
+       else {
+               int all_found = TRUE;
+               pid_t myPid=getpid();
                /* Looks like they want to do a killall.  Do that */
                while (--argc >= 0) {
-                       int pid;
+                       pid_t* pidList;
 
-                       while((pid = findPidByName( *argv))) {
-                               if (kill(pid, sig) != 0) 
-                                       fatalError( "Could not kill pid '%d': %s\n", pid, strerror(errno));
+                       pidList = findPidByName( *argv);
+                       if (!pidList) {
+                               all_found = FALSE;
+                               errorMsg( "%s: no process killed\n", *argv);
                        }
+
+                       for(; pidList && *pidList!=0; pidList++) {
+                               if (*pidList==myPid)
+                                       continue;
+                               if (kill(*pidList, sig) != 0) 
+                                       fatalError( "Could not kill pid '%d': %s\n", *pidList, strerror(errno));
+                       }
+                       /* Note that we don't bother to free the memory
+                        * allocated in findPidByName().  It will be freed
+                        * upon exit, so we can save a byte or two */
                        argv++;
                }
+               exit (all_found);
        }
+#endif
 
        exit(TRUE);