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 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 21 | int kill_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 23 | int killall, signo = SIGTERM, errors = 0, quiet=0; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 24 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 25 | killall = (ENABLE_KILLALL && bb_applet_name[4]=='a') ? 1 : 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 27 | /* Parse any options */ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 28 | if (argc < 2) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 30 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 31 | if(argv[1][0] != '-'){ |
| 32 | argv++; |
| 33 | argc--; |
| 34 | goto do_it_now; |
| 35 | } |
| 36 | |
| 37 | /* The -l option, which prints out signal names. */ |
| 38 | if(argv[1][1]=='l' && argv[1][2]=='\0'){ |
| 39 | if(argc==2) { |
| 40 | /* Print the whole signal list */ |
| 41 | int col = 0; |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 42 | |
| 43 | for(signo = 0;;) { |
| 44 | char *name = get_signame(++signo); |
| 45 | if (isdigit(*name)) break; |
| 46 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 47 | if (col > 60) { |
| 48 | printf("\n"); |
| 49 | col = 0; |
| 50 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 51 | col += printf("%2d) %-16s", signo, name); |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 52 | } |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 53 | printf("\n"); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 54 | } else { |
| 55 | for(argv++; *argv; argv++) { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 56 | char *name; |
| 57 | |
| 58 | if (isdigit(**argv)) name = get_signame(atoi(*argv)); |
| 59 | else { |
| 60 | int temp = get_signum(*argv); |
| 61 | if (temp<0) |
| 62 | bb_error_msg_and_die("unknown signal %s", *argv); |
| 63 | name = get_signame(temp); |
| 64 | } |
| 65 | puts(name); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | /* If they specified -l, were all done */ |
| 69 | return EXIT_SUCCESS; |
| 70 | } |
| 71 | |
| 72 | /* The -q quiet option */ |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 73 | if(killall && argv[1][1]=='q' && argv[1][2]=='\0'){ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 74 | quiet++; |
| 75 | argv++; |
| 76 | argc--; |
Eric Andersen | 8f4ef15 | 2003-08-06 07:43:58 +0000 | [diff] [blame] | 77 | if(argc<2 || argv[1][0] != '-'){ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 78 | goto do_it_now; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 81 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 82 | if(0>(signo = get_signum(argv[1]+1))) |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 83 | bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1); |
| 84 | argv+=2; |
| 85 | argc-=2; |
| 86 | |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 87 | do_it_now: |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 88 | |
"Vladimir N. Oleynik" | 1e98a07 | 2006-01-25 13:21:08 +0000 | [diff] [blame] | 89 | /* Pid or name required */ |
| 90 | if (argc <= 0) |
| 91 | bb_show_usage(); |
| 92 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 93 | if (!killall) { |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 94 | /* Looks like they want to do a kill. Do that */ |
| 95 | while (--argc >= 0) { |
| 96 | int pid; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | |
Mike Frysinger | b924055 | 2006-01-04 00:13:22 +0000 | [diff] [blame] | 98 | if (!isdigit(**argv) && **argv != '-') |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 99 | bb_error_msg_and_die( "Bad PID '%s'", *argv); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 100 | pid = strtol(*argv, NULL, 0); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 101 | if (kill(pid, signo) != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | bb_perror_msg( "Could not kill pid '%d'", pid); |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 103 | errors++; |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 104 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 105 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | } |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 107 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 108 | } else { |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 109 | pid_t myPid=getpid(); |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 110 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 111 | /* Looks like they want to do a killall. Do that */ |
| 112 | while (--argc >= 0) { |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 113 | long* pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 114 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 115 | pidList = find_pid_by_name(*argv); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 116 | if (!pidList || *pidList<=0) { |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 117 | errors++; |
| 118 | if (quiet==0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | bb_error_msg( "%s: no process killed", *argv); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 120 | } else { |
| 121 | long *pl; |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 122 | |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 123 | for(pl = pidList; *pl !=0 ; pl++) { |
| 124 | if (*pl==myPid) |
| 125 | continue; |
| 126 | if (kill(*pl, signo) != 0) { |
| 127 | errors++; |
| 128 | if (quiet==0) |
| 129 | bb_perror_msg( "Could not kill pid '%ld'", *pl); |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 130 | } |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 131 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 132 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 133 | free(pidList); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 134 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 135 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 136 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 137 | |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 138 | return errors; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 139 | } |