blob: 1814e19637ab7ea5169a1a353805c871ef0309e2 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Erik Andersen5661fe02000-04-05 01:00:52 +00003 * Mini kill/killall implementation for busybox
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenc4996011999-10-20 22:08:37 +00009 */
10
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000011#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000012#include <stdio.h>
13#include <stdlib.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000014#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000015#include <unistd.h>
16#include <signal.h>
Eric Andersena7093171999-10-23 05:42:08 +000017#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000018#include <string.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000019#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Rob Landleydfba7412006-03-06 20:47:33 +000021int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000022{
Rob Landleyc9c1a412006-07-12 19:17:55 +000023 int killall, signo = SIGTERM, errors = 0, quiet=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +000024
Rob Landleyc9c1a412006-07-12 19:17:55 +000025 killall = (ENABLE_KILLALL && bb_applet_name[4]=='a') ? 1 : 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000026
Erik Andersene49d5ec2000-02-08 19:58:47 +000027 /* Parse any options */
Eric Andersen7d72e792003-07-26 07:41:56 +000028 if (argc < 2)
Manuel Novoa III cad53642003-03-19 09:13:01 +000029 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000030
Eric Andersen7d72e792003-07-26 07:41:56 +000031 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 Landleyc9c1a412006-07-12 19:17:55 +000042
43 for(signo = 0;;) {
44 char *name = get_signame(++signo);
45 if (isdigit(*name)) break;
46
Eric Andersen7d72e792003-07-26 07:41:56 +000047 if (col > 60) {
48 printf("\n");
49 col = 0;
50 }
Rob Landleyc9c1a412006-07-12 19:17:55 +000051 col += printf("%2d) %-16s", signo, name);
Eric Andersen80cd3cf2002-07-23 23:45:11 +000052 }
Eric Andersen7d72e792003-07-26 07:41:56 +000053 printf("\n");
Eric Andersen7d72e792003-07-26 07:41:56 +000054 } else {
55 for(argv++; *argv; argv++) {
Rob Landleyc9c1a412006-07-12 19:17:55 +000056 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 Andersen7d72e792003-07-26 07:41:56 +000066 }
67 }
68 /* If they specified -l, were all done */
69 return EXIT_SUCCESS;
70 }
71
72 /* The -q quiet option */
Rob Landleyc9c1a412006-07-12 19:17:55 +000073 if(killall && argv[1][1]=='q' && argv[1][2]=='\0'){
Eric Andersen7d72e792003-07-26 07:41:56 +000074 quiet++;
75 argv++;
76 argc--;
Eric Andersen8f4ef152003-08-06 07:43:58 +000077 if(argc<2 || argv[1][0] != '-'){
Eric Andersen7d72e792003-07-26 07:41:56 +000078 goto do_it_now;
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 }
Eric Andersencc8ed391999-10-05 16:24:54 +000080 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000081
Rob Landleyc9c1a412006-07-12 19:17:55 +000082 if(0>(signo = get_signum(argv[1]+1)))
Eric Andersen7d72e792003-07-26 07:41:56 +000083 bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1);
84 argv+=2;
85 argc-=2;
86
Eric Andersen80cd3cf2002-07-23 23:45:11 +000087do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +000088
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +000089 /* Pid or name required */
90 if (argc <= 0)
91 bb_show_usage();
92
Rob Landleyc9c1a412006-07-12 19:17:55 +000093 if (!killall) {
Erik Andersen246cc6d2000-03-07 07:41:42 +000094 /* Looks like they want to do a kill. Do that */
95 while (--argc >= 0) {
96 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +000097
Mike Frysingerb9240552006-01-04 00:13:22 +000098 if (!isdigit(**argv) && **argv != '-')
Eric Andersen7d72e792003-07-26 07:41:56 +000099 bb_error_msg_and_die( "Bad PID '%s'", *argv);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000100 pid = strtol(*argv, NULL, 0);
Eric Andersen7d72e792003-07-26 07:41:56 +0000101 if (kill(pid, signo) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 bb_perror_msg( "Could not kill pid '%d'", pid);
Eric Andersenc38678d2002-09-16 06:22:25 +0000103 errors++;
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000104 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000105 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000107
Rob Landleyc9c1a412006-07-12 19:17:55 +0000108 } else {
Erik Andersen0a64de92000-04-18 20:00:03 +0000109 pid_t myPid=getpid();
Rob Landleyc9c1a412006-07-12 19:17:55 +0000110
Erik Andersen246cc6d2000-03-07 07:41:42 +0000111 /* Looks like they want to do a killall. Do that */
112 while (--argc >= 0) {
Eric Andersenb24d6562001-12-06 14:52:32 +0000113 long* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000114
Eric Andersenc38678d2002-09-16 06:22:25 +0000115 pidList = find_pid_by_name(*argv);
Eric Andersen7d72e792003-07-26 07:41:56 +0000116 if (!pidList || *pidList<=0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000117 errors++;
118 if (quiet==0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 bb_error_msg( "%s: no process killed", *argv);
Eric Andersen7d72e792003-07-26 07:41:56 +0000120 } else {
121 long *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000122
Eric Andersen7d72e792003-07-26 07:41:56 +0000123 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 Andersen80cd3cf2002-07-23 23:45:11 +0000130 }
Eric Andersen7d72e792003-07-26 07:41:56 +0000131 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000132 }
Eric Andersen44608e92002-10-22 12:21:15 +0000133 free(pidList);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000134 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000136 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000137
Eric Andersenc38678d2002-09-16 06:22:25 +0000138 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000139}