blob: 717d8c69e3326ca7783ff3046c254bd9b4a9e411 [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 Andersencb81e642003-07-14 21:21:08 +00006 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
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 Andersencc8ed391999-10-05 16:24:54 +000025#include <stdio.h>
26#include <stdlib.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000027#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <unistd.h>
29#include <signal.h>
Eric Andersena7093171999-10-23 05:42:08 +000030#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000031#include <string.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000032#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000033#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Mark Whitley59ab0252001-01-23 22:30:04 +000035static const int KILL = 0;
36static const int KILLALL = 1;
Eric Andersencc8ed391999-10-05 16:24:54 +000037
Eric Andersencc8ed391999-10-05 16:24:54 +000038
Erik Andersene49d5ec2000-02-08 19:58:47 +000039extern int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000040{
Eric Andersen80cd3cf2002-07-23 23:45:11 +000041 int whichApp, sig = SIGTERM, quiet;
Eric Andersen34506362001-08-02 05:02:46 +000042 const char *name;
Eric Andersenc38678d2002-09-16 06:22:25 +000043 int errors = 0;
Erik Andersen246cc6d2000-03-07 07:41:42 +000044
Eric Andersenbdfd0d72001-10-24 05:00:29 +000045#ifdef CONFIG_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +000046 /* Figure out what we are trying to do here */
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000048#else
49 whichApp = KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000050#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Eric Andersen80cd3cf2002-07-23 23:45:11 +000052 quiet=0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000053 argc--;
54 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 /* Parse any options */
56 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000058
59 while (argc > 0 && **argv == '-') {
60 while (*++(*argv)) {
61 switch (**argv) {
Eric Andersen80cd3cf2002-07-23 23:45:11 +000062#ifdef CONFIG_KILLALL
63 case 'q':
64 quiet++;
65 break;
66#endif
67 case 'l':
68 if(argc>1) {
69 for(argv++; *argv; argv++) {
70 name = u_signal_names(*argv, &sig, -1);
71 if(name!=NULL)
72 printf("%s\n", name);
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 }
Eric Andersen80cd3cf2002-07-23 23:45:11 +000074 } else {
75 int col = 0;
76 for(sig=1; sig < NSIG; sig++) {
77 name = u_signal_names(0, &sig, 1);
78 if(name==NULL) /* unnamed */
79 continue;
80 col += printf("%2d) %-16s", sig, name);
81 if (col > 60) {
82 printf("\n");
83 col = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 }
Eric Andersen80cd3cf2002-07-23 23:45:11 +000085 }
86 printf("\n");
87 }
88 return EXIT_SUCCESS;
89 case '-':
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 bb_show_usage();
Eric Andersen80cd3cf2002-07-23 23:45:11 +000091 default:
92 name = u_signal_names(*argv, &sig, 0);
93 if(name==NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 bb_error_msg_and_die( "bad signal name: %s", *argv);
Eric Andersen80cd3cf2002-07-23 23:45:11 +000095 argc--;
96 argv++;
97 goto do_it_now;
98 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 argc--;
100 argv++;
101 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000102 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000103
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000104do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000105
Erik Andersen246cc6d2000-03-07 07:41:42 +0000106 if (whichApp == KILL) {
107 /* Looks like they want to do a kill. Do that */
108 while (--argc >= 0) {
109 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110
Erik Andersen246cc6d2000-03-07 07:41:42 +0000111 if (!isdigit(**argv))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 bb_perror_msg_and_die( "Bad PID");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000113 pid = strtol(*argv, NULL, 0);
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000114 if (kill(pid, sig) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 bb_perror_msg( "Could not kill pid '%d'", pid);
Eric Andersenc38678d2002-09-16 06:22:25 +0000116 errors++;
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000117 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000118 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000120
Erik Andersen6273f652000-03-17 01:12:41 +0000121 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000122#ifdef CONFIG_KILLALL
Erik Andersen6273f652000-03-17 01:12:41 +0000123 else {
Erik Andersen0a64de92000-04-18 20:00:03 +0000124 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000125 /* Looks like they want to do a killall. Do that */
126 while (--argc >= 0) {
Eric Andersenb24d6562001-12-06 14:52:32 +0000127 long* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000128
Eric Andersenc38678d2002-09-16 06:22:25 +0000129 pidList = find_pid_by_name(*argv);
Eric Andersen44608e92002-10-22 12:21:15 +0000130 if (*pidList <= 0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000131 errors++;
132 if (quiet==0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 bb_error_msg( "%s: no process killed", *argv);
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000134 } else {
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000135 long *pl;
136
137 for(pl = pidList; *pl !=0 ; pl++) {
138 if (*pl==myPid)
139 continue;
140 if (kill(*pl, sig) != 0) {
141 errors++;
142 if (quiet==0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_perror_msg( "Could not kill pid '%ld'", *pl);
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000144 }
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000145 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000146 }
Eric Andersen44608e92002-10-22 12:21:15 +0000147 free(pidList);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000148 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000150 }
Erik Andersen6273f652000-03-17 01:12:41 +0000151#endif
Eric Andersenc38678d2002-09-16 06:22:25 +0000152 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000153}