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 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 27 | #include <errno.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | #include <signal.h> |
Eric Andersen | a709317 | 1999-10-23 05:42:08 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 31 | #include <string.h> |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 33 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 35 | #define KILL 0 |
| 36 | #define KILLALL 1 |
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 | extern int kill_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | { |
Eric Andersen | a4fccce | 2004-08-19 18:26:26 +0000 | [diff] [blame] | 40 | int whichApp, signo = SIGTERM; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 41 | const char *name; |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 42 | int errors = 0; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 43 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 44 | #ifdef CONFIG_KILLALL |
Eric Andersen | a4fccce | 2004-08-19 18:26:26 +0000 | [diff] [blame] | 45 | int quiet=0; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 46 | /* Figure out what we are trying to do here */ |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 47 | whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 48 | #else |
| 49 | whichApp = KILL; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 50 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 51 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | /* Parse any options */ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 53 | if (argc < 2) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 56 | if(argv[1][0] != '-'){ |
| 57 | argv++; |
| 58 | argc--; |
| 59 | goto do_it_now; |
| 60 | } |
| 61 | |
| 62 | /* The -l option, which prints out signal names. */ |
| 63 | if(argv[1][1]=='l' && argv[1][2]=='\0'){ |
| 64 | if(argc==2) { |
| 65 | /* Print the whole signal list */ |
| 66 | int col = 0; |
| 67 | for(signo=1; signo < NSIG; signo++) { |
| 68 | name = u_signal_names(0, &signo, 1); |
| 69 | if(name==NULL) /* unnamed */ |
| 70 | continue; |
| 71 | col += printf("%2d) %-16s", signo, name); |
| 72 | if (col > 60) { |
| 73 | printf("\n"); |
| 74 | col = 0; |
| 75 | } |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 76 | } |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 77 | printf("\n"); |
| 78 | |
| 79 | } else { |
| 80 | for(argv++; *argv; argv++) { |
| 81 | name = u_signal_names(*argv, &signo, -1); |
| 82 | if(name!=NULL) |
| 83 | printf("%s\n", name); |
| 84 | } |
| 85 | } |
| 86 | /* If they specified -l, were all done */ |
| 87 | return EXIT_SUCCESS; |
| 88 | } |
| 89 | |
Eric Andersen | a4fccce | 2004-08-19 18:26:26 +0000 | [diff] [blame] | 90 | #ifdef CONFIG_KILLALL |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 91 | /* The -q quiet option */ |
| 92 | if(argv[1][1]=='q' && argv[1][2]=='\0'){ |
| 93 | quiet++; |
| 94 | argv++; |
| 95 | argc--; |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 96 | if(argc<2 || argv[1][0] != '-'){ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 97 | goto do_it_now; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | } |
Eric Andersen | a4fccce | 2004-08-19 18:26:26 +0000 | [diff] [blame] | 100 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 101 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 102 | if(!u_signal_names(argv[1]+1, &signo, 0)) |
| 103 | bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1); |
| 104 | argv+=2; |
| 105 | argc-=2; |
| 106 | |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 107 | do_it_now: |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 108 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 109 | if (whichApp == KILL) { |
| 110 | /* Looks like they want to do a kill. Do that */ |
| 111 | while (--argc >= 0) { |
| 112 | int pid; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | |
Mike Frysinger | b924055 | 2006-01-04 00:13:22 +0000 | [diff] [blame] | 114 | if (!isdigit(**argv) && **argv != '-') |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 115 | bb_error_msg_and_die( "Bad PID '%s'", *argv); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 116 | pid = strtol(*argv, NULL, 0); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 117 | if (kill(pid, signo) != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | bb_perror_msg( "Could not kill pid '%d'", pid); |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 119 | errors++; |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 120 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 121 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 122 | } |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 123 | |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 124 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 125 | #ifdef CONFIG_KILLALL |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 126 | else { |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 127 | pid_t myPid=getpid(); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 128 | /* Looks like they want to do a killall. Do that */ |
| 129 | while (--argc >= 0) { |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 130 | long* pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 131 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 132 | pidList = find_pid_by_name(*argv); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 133 | if (!pidList || *pidList<=0) { |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 134 | errors++; |
| 135 | if (quiet==0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 136 | bb_error_msg( "%s: no process killed", *argv); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 137 | } else { |
| 138 | long *pl; |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 139 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 140 | for(pl = pidList; *pl !=0 ; pl++) { |
| 141 | if (*pl==myPid) |
| 142 | continue; |
| 143 | if (kill(*pl, signo) != 0) { |
| 144 | errors++; |
| 145 | if (quiet==0) |
| 146 | bb_perror_msg( "Could not kill pid '%ld'", *pl); |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 147 | } |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 148 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 149 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 150 | free(pidList); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 151 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 153 | } |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 154 | #endif |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 155 | return errors; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 156 | } |