Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 3 | * Mini kill/killall implementation for busybox |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 11 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 14 | #include <errno.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | #include <signal.h> |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 17 | #include <ctype.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 18 | #include <string.h> |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 21 | #define KILL 0 |
| 22 | #define KILLALL 1 |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 24 | int kill_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | { |
Eric Andersen | a4fccce | 2004-08-19 18:26:26 +0000 | [diff] [blame] | 26 | int whichApp, signo = SIGTERM; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 27 | const char *name; |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 28 | int errors = 0; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 29 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 30 | #ifdef CONFIG_KILLALL |
Eric Andersen | a4fccce | 2004-08-19 18:26:26 +0000 | [diff] [blame] | 31 | int quiet=0; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 32 | /* Figure out what we are trying to do here */ |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 33 | whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 34 | #else |
| 35 | whichApp = KILL; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 36 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | /* Parse any options */ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 39 | if (argc < 2) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 42 | if(argv[1][0] != '-'){ |
| 43 | argv++; |
| 44 | argc--; |
| 45 | goto do_it_now; |
| 46 | } |
| 47 | |
| 48 | /* The -l option, which prints out signal names. */ |
| 49 | if(argv[1][1]=='l' && argv[1][2]=='\0'){ |
| 50 | if(argc==2) { |
| 51 | /* Print the whole signal list */ |
| 52 | int col = 0; |
| 53 | for(signo=1; signo < NSIG; signo++) { |
| 54 | name = u_signal_names(0, &signo, 1); |
| 55 | if(name==NULL) /* unnamed */ |
| 56 | continue; |
| 57 | col += printf("%2d) %-16s", signo, name); |
| 58 | if (col > 60) { |
| 59 | printf("\n"); |
| 60 | col = 0; |
| 61 | } |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 62 | } |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 63 | printf("\n"); |
| 64 | |
| 65 | } else { |
| 66 | for(argv++; *argv; argv++) { |
| 67 | name = u_signal_names(*argv, &signo, -1); |
| 68 | if(name!=NULL) |
| 69 | printf("%s\n", name); |
| 70 | } |
| 71 | } |
| 72 | /* If they specified -l, were all done */ |
| 73 | return EXIT_SUCCESS; |
| 74 | } |
| 75 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 76 | #ifdef CONFIG_KILLALL |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 77 | /* The -q quiet option */ |
"Vladimir N. Oleynik" | 1e98a07 | 2006-01-25 13:21:08 +0000 | [diff] [blame] | 78 | if(whichApp != KILL && argv[1][1]=='q' && argv[1][2]=='\0'){ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 79 | quiet++; |
| 80 | argv++; |
| 81 | argc--; |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 82 | if(argc<2 || argv[1][0] != '-'){ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 83 | goto do_it_now; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | } |
Eric Andersen | a4fccce | 2004-08-19 18:26:26 +0000 | [diff] [blame] | 86 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 87 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 88 | if(!u_signal_names(argv[1]+1, &signo, 0)) |
| 89 | bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1); |
| 90 | argv+=2; |
| 91 | argc-=2; |
| 92 | |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 93 | do_it_now: |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 94 | |
"Vladimir N. Oleynik" | 1e98a07 | 2006-01-25 13:21:08 +0000 | [diff] [blame] | 95 | /* Pid or name required */ |
| 96 | if (argc <= 0) |
| 97 | bb_show_usage(); |
| 98 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 99 | if (whichApp == KILL) { |
| 100 | /* Looks like they want to do a kill. Do that */ |
| 101 | while (--argc >= 0) { |
| 102 | int pid; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | |
Mike Frysinger | b924055 | 2006-01-04 00:13:22 +0000 | [diff] [blame] | 104 | if (!isdigit(**argv) && **argv != '-') |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 105 | bb_error_msg_and_die( "Bad PID '%s'", *argv); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 106 | pid = strtol(*argv, NULL, 0); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 107 | if (kill(pid, signo) != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | bb_perror_msg( "Could not kill pid '%d'", pid); |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 109 | errors++; |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 110 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 111 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | } |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 113 | |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 114 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 115 | #ifdef CONFIG_KILLALL |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 116 | else { |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 117 | pid_t myPid=getpid(); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 118 | /* Looks like they want to do a killall. Do that */ |
| 119 | while (--argc >= 0) { |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 120 | long* pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 121 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 122 | pidList = find_pid_by_name(*argv); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 123 | if (!pidList || *pidList<=0) { |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 124 | errors++; |
| 125 | if (quiet==0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 126 | bb_error_msg( "%s: no process killed", *argv); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 127 | } else { |
| 128 | long *pl; |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 129 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 130 | for(pl = pidList; *pl !=0 ; pl++) { |
| 131 | if (*pl==myPid) |
| 132 | continue; |
| 133 | if (kill(*pl, signo) != 0) { |
| 134 | errors++; |
| 135 | if (quiet==0) |
| 136 | bb_perror_msg( "Could not kill pid '%ld'", *pl); |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 137 | } |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 138 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 139 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 140 | free(pidList); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 141 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 143 | } |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 144 | #endif |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 145 | return errors; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 146 | } |