blob: 5f2f6618fb77d59a7729fac63aa0838d37020cff [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 Andersen1d1d2f92002-04-13 08:31:59 +00006 * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.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;
Erik Andersen246cc6d2000-03-07 07:41:42 +000043
Eric Andersenbdfd0d72001-10-24 05:00:29 +000044#ifdef CONFIG_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +000045 /* Figure out what we are trying to do here */
Matt Kraaie58771e2000-07-12 15:38:49 +000046 whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000047#else
48 whichApp = KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000049#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Eric Andersen80cd3cf2002-07-23 23:45:11 +000051 quiet=0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000052 argc--;
53 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 /* Parse any options */
55 if (argc < 1)
Eric Andersen67991cf2001-02-14 21:23:06 +000056 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000057
58 while (argc > 0 && **argv == '-') {
59 while (*++(*argv)) {
60 switch (**argv) {
Eric Andersen80cd3cf2002-07-23 23:45:11 +000061#ifdef CONFIG_KILLALL
62 case 'q':
63 quiet++;
64 break;
65#endif
66 case 'l':
67 if(argc>1) {
68 for(argv++; *argv; argv++) {
69 name = u_signal_names(*argv, &sig, -1);
70 if(name!=NULL)
71 printf("%s\n", name);
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 }
Eric Andersen80cd3cf2002-07-23 23:45:11 +000073 } else {
74 int col = 0;
75 for(sig=1; sig < NSIG; sig++) {
76 name = u_signal_names(0, &sig, 1);
77 if(name==NULL) /* unnamed */
78 continue;
79 col += printf("%2d) %-16s", sig, name);
80 if (col > 60) {
81 printf("\n");
82 col = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 }
Eric Andersen80cd3cf2002-07-23 23:45:11 +000084 }
85 printf("\n");
86 }
87 return EXIT_SUCCESS;
88 case '-':
89 show_usage();
90 default:
91 name = u_signal_names(*argv, &sig, 0);
92 if(name==NULL)
93 error_msg_and_die( "bad signal name: %s", *argv);
94 argc--;
95 argv++;
96 goto do_it_now;
97 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 argc--;
99 argv++;
100 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000101 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000102
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000103do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000104
Erik Andersen246cc6d2000-03-07 07:41:42 +0000105 if (whichApp == KILL) {
106 /* Looks like they want to do a kill. Do that */
107 while (--argc >= 0) {
108 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109
Erik Andersen246cc6d2000-03-07 07:41:42 +0000110 if (!isdigit(**argv))
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000111 perror_msg_and_die( "Bad PID");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000112 pid = strtol(*argv, NULL, 0);
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000113 if (kill(pid, sig) != 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000114 perror_msg_and_die( "Could not kill pid '%d'", pid);
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000115 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000116 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 }
Erik Andersen6273f652000-03-17 01:12:41 +0000118 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000119#ifdef CONFIG_KILLALL
Erik Andersen6273f652000-03-17 01:12:41 +0000120 else {
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000121 int all_found = TRUE;
Erik Andersen0a64de92000-04-18 20:00:03 +0000122 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000123 /* Looks like they want to do a killall. Do that */
124 while (--argc >= 0) {
Eric Andersenb24d6562001-12-06 14:52:32 +0000125 long* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000126
Mark Whitleyf57c9442000-12-07 19:56:48 +0000127 pidList = find_pid_by_name( *argv);
Eric Andersen8e75f6d2001-07-05 16:27:34 +0000128 if (!pidList || *pidList<=0) {
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000129 all_found = FALSE;
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000130 if (quiet) {
131 exit(EXIT_FAILURE);
132 } else {
133 error_msg_and_die( "%s: no process killed", *argv);
134 }
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000135 }
Eric Andersen86ab8a32000-06-02 03:21:42 +0000136
Erik Andersen0a64de92000-04-18 20:00:03 +0000137 for(; pidList && *pidList!=0; pidList++) {
138 if (*pidList==myPid)
139 continue;
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000140 if (kill(*pidList, sig) != 0) {
141 if (quiet) {
142 exit(EXIT_FAILURE);
143 } else {
144 perror_msg_and_die( "Could not kill pid '%d'", *pidList);
145 }
146 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000147 }
Erik Andersen825aead2000-04-07 06:00:07 +0000148 /* Note that we don't bother to free the memory
Mark Whitleyf57c9442000-12-07 19:56:48 +0000149 * allocated in find_pid_by_name(). It will be freed
Erik Andersen825aead2000-04-07 06:00:07 +0000150 * upon exit, so we can save a byte or two */
Erik Andersen246cc6d2000-03-07 07:41:42 +0000151 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 }
Matt Kraai1f0c4362001-12-20 23:13:26 +0000153 if (! all_found)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000154 return EXIT_FAILURE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000155 }
Erik Andersen6273f652000-03-17 01:12:41 +0000156#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +0000157
Matt Kraai3e856ce2000-12-01 02:55:13 +0000158 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000159}