blob: ca6f4203ad71aba3e0357daab3c87d64d7d15a78 [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
Eric Andersen8f4ef152003-08-06 07:43:58 +000021#define KILL 0
22#define KILLALL 1
Eric Andersencc8ed391999-10-05 16:24:54 +000023
Rob Landleydfba7412006-03-06 20:47:33 +000024int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000025{
Eric Andersena4fccce2004-08-19 18:26:26 +000026 int whichApp, signo = SIGTERM;
Eric Andersen34506362001-08-02 05:02:46 +000027 const char *name;
Eric Andersenc38678d2002-09-16 06:22:25 +000028 int errors = 0;
Erik Andersen246cc6d2000-03-07 07:41:42 +000029
Eric Andersenbdfd0d72001-10-24 05:00:29 +000030#ifdef CONFIG_KILLALL
Eric Andersena4fccce2004-08-19 18:26:26 +000031 int quiet=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +000032 /* Figure out what we are trying to do here */
Eric Andersen8f4ef152003-08-06 07:43:58 +000033 whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000034#else
35 whichApp = KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000036#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000037
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 /* Parse any options */
Eric Andersen7d72e792003-07-26 07:41:56 +000039 if (argc < 2)
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000041
Eric Andersen7d72e792003-07-26 07:41:56 +000042 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 Andersen80cd3cf2002-07-23 23:45:11 +000062 }
Eric Andersen7d72e792003-07-26 07:41:56 +000063 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 Rikerc1ef7bd2006-01-25 00:08:53 +000076#ifdef CONFIG_KILLALL
Eric Andersen7d72e792003-07-26 07:41:56 +000077 /* The -q quiet option */
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +000078 if(whichApp != KILL && argv[1][1]=='q' && argv[1][2]=='\0'){
Eric Andersen7d72e792003-07-26 07:41:56 +000079 quiet++;
80 argv++;
81 argc--;
Eric Andersen8f4ef152003-08-06 07:43:58 +000082 if(argc<2 || argv[1][0] != '-'){
Eric Andersen7d72e792003-07-26 07:41:56 +000083 goto do_it_now;
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 }
Eric Andersencc8ed391999-10-05 16:24:54 +000085 }
Eric Andersena4fccce2004-08-19 18:26:26 +000086#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000087
Eric Andersen7d72e792003-07-26 07:41:56 +000088 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 Andersen80cd3cf2002-07-23 23:45:11 +000093do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +000094
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +000095 /* Pid or name required */
96 if (argc <= 0)
97 bb_show_usage();
98
Erik Andersen246cc6d2000-03-07 07:41:42 +000099 if (whichApp == KILL) {
100 /* Looks like they want to do a kill. Do that */
101 while (--argc >= 0) {
102 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103
Mike Frysingerb9240552006-01-04 00:13:22 +0000104 if (!isdigit(**argv) && **argv != '-')
Eric Andersen7d72e792003-07-26 07:41:56 +0000105 bb_error_msg_and_die( "Bad PID '%s'", *argv);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000106 pid = strtol(*argv, NULL, 0);
Eric Andersen7d72e792003-07-26 07:41:56 +0000107 if (kill(pid, signo) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 bb_perror_msg( "Could not kill pid '%d'", pid);
Eric Andersenc38678d2002-09-16 06:22:25 +0000109 errors++;
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000110 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000111 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000113
Eric Andersen8f4ef152003-08-06 07:43:58 +0000114 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000115#ifdef CONFIG_KILLALL
Erik Andersen6273f652000-03-17 01:12:41 +0000116 else {
Erik Andersen0a64de92000-04-18 20:00:03 +0000117 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000118 /* Looks like they want to do a killall. Do that */
119 while (--argc >= 0) {
Eric Andersenb24d6562001-12-06 14:52:32 +0000120 long* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000121
Eric Andersenc38678d2002-09-16 06:22:25 +0000122 pidList = find_pid_by_name(*argv);
Eric Andersen7d72e792003-07-26 07:41:56 +0000123 if (!pidList || *pidList<=0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000124 errors++;
125 if (quiet==0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 bb_error_msg( "%s: no process killed", *argv);
Eric Andersen7d72e792003-07-26 07:41:56 +0000127 } else {
128 long *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000129
Eric Andersen7d72e792003-07-26 07:41:56 +0000130 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 Andersen80cd3cf2002-07-23 23:45:11 +0000137 }
Eric Andersen7d72e792003-07-26 07:41:56 +0000138 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000139 }
Eric Andersen44608e92002-10-22 12:21:15 +0000140 free(pidList);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000141 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000143 }
Erik Andersen6273f652000-03-17 01:12:41 +0000144#endif
Eric Andersenc38678d2002-09-16 06:22:25 +0000145 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000146}