blob: 8b8a9922cbf0c98385a6ac8bc63c58037ee5636d [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>.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include <stdio.h>
25#include <stdlib.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000026#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000027#include <unistd.h>
28#include <signal.h>
Eric Andersena7093171999-10-23 05:42:08 +000029#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000030#include <string.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000031#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Mark Whitley59ab0252001-01-23 22:30:04 +000034static const int KILL = 0;
35static const int KILLALL = 1;
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Eric Andersencc8ed391999-10-05 16:24:54 +000037
Erik Andersene49d5ec2000-02-08 19:58:47 +000038extern int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Erik Andersen246cc6d2000-03-07 07:41:42 +000040 int whichApp, sig = SIGTERM;
Eric Andersen34506362001-08-02 05:02:46 +000041 const char *name;
Erik Andersen246cc6d2000-03-07 07:41:42 +000042
Eric Andersenbdfd0d72001-10-24 05:00:29 +000043#ifdef CONFIG_KILLALL
Erik Andersen246cc6d2000-03-07 07:41:42 +000044 /* Figure out what we are trying to do here */
Matt Kraaie58771e2000-07-12 15:38:49 +000045 whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000046#else
47 whichApp = KILL;
Erik Andersen6273f652000-03-17 01:12:41 +000048#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000049
Eric Andersenabc0f4f1999-12-08 23:19:36 +000050 argc--;
51 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 /* Parse any options */
53 if (argc < 1)
Eric Andersen67991cf2001-02-14 21:23:06 +000054 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000055
56 while (argc > 0 && **argv == '-') {
57 while (*++(*argv)) {
58 switch (**argv) {
59 case 'l':
Eric Andersen34506362001-08-02 05:02:46 +000060 if(argc>1) {
61 for(argv++; *argv; argv++) {
62 name = u_signal_names(*argv, &sig, -1);
63 if(name!=NULL)
64 printf("%s\n", name);
65 }
66 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 int col = 0;
Eric Andersen34506362001-08-02 05:02:46 +000068 for(sig=1; sig < NSIG; sig++) {
69 name = u_signal_names(0, &sig, 1);
70 if(name==NULL) /* unnamed */
71 continue;
72 col += printf("%2d) %-16s", sig, name);
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 if (col > 60) {
Eric Andersen34506362001-08-02 05:02:46 +000074 printf("\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 col = 0;
76 }
77 }
Eric Andersen34506362001-08-02 05:02:46 +000078 printf("\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 }
Eric Andersen34506362001-08-02 05:02:46 +000080 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 case '-':
Eric Andersen67991cf2001-02-14 21:23:06 +000082 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 default:
Eric Andersen34506362001-08-02 05:02:46 +000084 name = u_signal_names(*argv, &sig, 0);
85 if(name==NULL)
86 error_msg_and_die( "bad signal name: %s", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 argc--;
88 argv++;
89 goto do_it_now;
90 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 argc--;
92 argv++;
93 }
Eric Andersencc8ed391999-10-05 16:24:54 +000094 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000095
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +000097
Erik Andersen246cc6d2000-03-07 07:41:42 +000098 if (whichApp == KILL) {
99 /* Looks like they want to do a kill. Do that */
100 while (--argc >= 0) {
101 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102
Erik Andersen246cc6d2000-03-07 07:41:42 +0000103 if (!isdigit(**argv))
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000104 perror_msg_and_die( "Bad PID");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000105 pid = strtol(*argv, NULL, 0);
106 if (kill(pid, sig) != 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000107 perror_msg_and_die( "Could not kill pid '%d'", pid);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000108 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 }
Erik Andersen6273f652000-03-17 01:12:41 +0000110 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000111#ifdef CONFIG_KILLALL
Erik Andersen6273f652000-03-17 01:12:41 +0000112 else {
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000113 int all_found = TRUE;
Erik Andersen0a64de92000-04-18 20:00:03 +0000114 pid_t myPid=getpid();
Erik Andersen246cc6d2000-03-07 07:41:42 +0000115 /* Looks like they want to do a killall. Do that */
116 while (--argc >= 0) {
Erik Andersen825aead2000-04-07 06:00:07 +0000117 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000118
Mark Whitleyf57c9442000-12-07 19:56:48 +0000119 pidList = find_pid_by_name( *argv);
Eric Andersen8e75f6d2001-07-05 16:27:34 +0000120 if (!pidList || *pidList<=0) {
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000121 all_found = FALSE;
Eric Andersen8e75f6d2001-07-05 16:27:34 +0000122 error_msg_and_die( "%s: no process killed", *argv);
Pavel Roskin700a5ae2000-06-06 18:10:17 +0000123 }
Eric Andersen86ab8a32000-06-02 03:21:42 +0000124
Erik Andersen0a64de92000-04-18 20:00:03 +0000125 for(; pidList && *pidList!=0; pidList++) {
126 if (*pidList==myPid)
127 continue;
Erik Andersen825aead2000-04-07 06:00:07 +0000128 if (kill(*pidList, sig) != 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000129 perror_msg_and_die( "Could not kill pid '%d'", *pidList);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000130 }
Erik Andersen825aead2000-04-07 06:00:07 +0000131 /* Note that we don't bother to free the memory
Mark Whitleyf57c9442000-12-07 19:56:48 +0000132 * allocated in find_pid_by_name(). It will be freed
Erik Andersen825aead2000-04-07 06:00:07 +0000133 * upon exit, so we can save a byte or two */
Erik Andersen246cc6d2000-03-07 07:41:42 +0000134 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000136 if (all_found == FALSE)
137 return EXIT_FAILURE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000138 }
Erik Andersen6273f652000-03-17 01:12:41 +0000139#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +0000140
Matt Kraai3e856ce2000-12-01 02:55:13 +0000141 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000142}