aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-07-26 07:41:56 +0000
committerEric Andersen <andersen@codepoet.org>2003-07-26 07:41:56 +0000
commit7d72e796d6362872fe0075f936589bba23124344 (patch)
treea7c306e39d321a451093c5a6c114c7fa7bcfb7f1
parentd5868c169214b2be273c8a5ecb0a30ff1368faba (diff)
downloadbusybox-w32-7d72e796d6362872fe0075f936589bba23124344.tar.gz
busybox-w32-7d72e796d6362872fe0075f936589bba23124344.tar.bz2
busybox-w32-7d72e796d6362872fe0075f936589bba23124344.zip
Rework kill / killall so it behaves itself, even when subjected
to abuse. -Erik
-rw-r--r--procps/kill.c122
1 files changed, 61 insertions, 61 deletions
diff --git a/procps/kill.c b/procps/kill.c
index 717d8c69e..f11623e01 100644
--- a/procps/kill.c
+++ b/procps/kill.c
@@ -35,10 +35,9 @@
35static const int KILL = 0; 35static const int KILL = 0;
36static const int KILLALL = 1; 36static const int KILLALL = 1;
37 37
38
39extern int kill_main(int argc, char **argv) 38extern int kill_main(int argc, char **argv)
40{ 39{
41 int whichApp, sig = SIGTERM, quiet; 40 int whichApp, signo = SIGTERM, quiet = 0;
42 const char *name; 41 const char *name;
43 int errors = 0; 42 int errors = 0;
44 43
@@ -49,58 +48,59 @@ extern int kill_main(int argc, char **argv)
49 whichApp = KILL; 48 whichApp = KILL;
50#endif 49#endif
51 50
52 quiet=0;
53 argc--;
54 argv++;
55 /* Parse any options */ 51 /* Parse any options */
56 if (argc < 1) 52 if (argc < 2)
57 bb_show_usage(); 53 bb_show_usage();
58 54
59 while (argc > 0 && **argv == '-') { 55 if(argv[1][0] != '-'){
60 while (*++(*argv)) { 56 argv++;
61 switch (**argv) { 57 argc--;
62#ifdef CONFIG_KILLALL 58 goto do_it_now;
63 case 'q': 59 }
64 quiet++; 60
65 break; 61 /* The -l option, which prints out signal names. */
66#endif 62 if(argv[1][1]=='l' && argv[1][2]=='\0'){
67 case 'l': 63 if(argc==2) {
68 if(argc>1) { 64 /* Print the whole signal list */
69 for(argv++; *argv; argv++) { 65 int col = 0;
70 name = u_signal_names(*argv, &sig, -1); 66 for(signo=1; signo < NSIG; signo++) {
71 if(name!=NULL) 67 name = u_signal_names(0, &signo, 1);
72 printf("%s\n", name); 68 if(name==NULL) /* unnamed */
73 } 69 continue;
74 } else { 70 col += printf("%2d) %-16s", signo, name);
75 int col = 0; 71 if (col > 60) {
76 for(sig=1; sig < NSIG; sig++) { 72 printf("\n");
77 name = u_signal_names(0, &sig, 1); 73 col = 0;
78 if(name==NULL) /* unnamed */ 74 }
79 continue; 75 }
80 col += printf("%2d) %-16s", sig, name); 76 printf("\n");
81 if (col > 60) { 77
82 printf("\n"); 78 } else {
83 col = 0; 79 for(argv++; *argv; argv++) {
84 } 80 name = u_signal_names(*argv, &signo, -1);
85 } 81 if(name!=NULL)
86 printf("\n"); 82 printf("%s\n", name);
87 }
88 return EXIT_SUCCESS;
89 case '-':
90 bb_show_usage();
91 default:
92 name = u_signal_names(*argv, &sig, 0);
93 if(name==NULL)
94 bb_error_msg_and_die( "bad signal name: %s", *argv);
95 argc--;
96 argv++;
97 goto do_it_now;
98 } 83 }
99 argc--;
100 argv++;
101 } 84 }
85 /* If they specified -l, were all done */
86 return EXIT_SUCCESS;
102 } 87 }
103 88
89 /* The -q quiet option */
90 if(argv[1][1]=='q' && argv[1][2]=='\0'){
91 quiet++;
92 argv++;
93 argc--;
94 if(argv[1][0] != '-'){
95 goto do_it_now;
96 }
97 }
98
99 if(!u_signal_names(argv[1]+1, &signo, 0))
100 bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1);
101 argv+=2;
102 argc-=2;
103
104do_it_now: 104do_it_now:
105 105
106 if (whichApp == KILL) { 106 if (whichApp == KILL) {
@@ -109,9 +109,9 @@ do_it_now:
109 int pid; 109 int pid;
110 110
111 if (!isdigit(**argv)) 111 if (!isdigit(**argv))
112 bb_perror_msg_and_die( "Bad PID"); 112 bb_error_msg_and_die( "Bad PID '%s'", *argv);
113 pid = strtol(*argv, NULL, 0); 113 pid = strtol(*argv, NULL, 0);
114 if (kill(pid, sig) != 0) { 114 if (kill(pid, signo) != 0) {
115 bb_perror_msg( "Could not kill pid '%d'", pid); 115 bb_perror_msg( "Could not kill pid '%d'", pid);
116 errors++; 116 errors++;
117 } 117 }
@@ -127,22 +127,22 @@ do_it_now:
127 long* pidList; 127 long* pidList;
128 128
129 pidList = find_pid_by_name(*argv); 129 pidList = find_pid_by_name(*argv);
130 if (*pidList <= 0) { 130 if (!pidList || *pidList<=0) {
131 errors++; 131 errors++;
132 if (quiet==0) 132 if (quiet==0)
133 bb_error_msg( "%s: no process killed", *argv); 133 bb_error_msg( "%s: no process killed", *argv);
134 } else { 134 } else {
135 long *pl; 135 long *pl;
136 136
137 for(pl = pidList; *pl !=0 ; pl++) { 137 for(pl = pidList; *pl !=0 ; pl++) {
138 if (*pl==myPid) 138 if (*pl==myPid)
139 continue; 139 continue;
140 if (kill(*pl, sig) != 0) { 140 if (kill(*pl, signo) != 0) {
141 errors++; 141 errors++;
142 if (quiet==0) 142 if (quiet==0)
143 bb_perror_msg( "Could not kill pid '%ld'", *pl); 143 bb_perror_msg( "Could not kill pid '%ld'", *pl);
144 }
145 } 144 }
145 }
146 } 146 }
147 free(pidList); 147 free(pidList);
148 argv++; 148 argv++;