Rework kill / killall so it behaves itself, even when subjected

to abuse.
 -Erik
This commit is contained in:
Eric Andersen 2003-07-26 07:41:56 +00:00
parent d5868c1692
commit 7d72e796d6

View File

@ -35,10 +35,9 @@
static const int KILL = 0; static const int KILL = 0;
static const int KILLALL = 1; static const int KILLALL = 1;
extern int kill_main(int argc, char **argv) extern int kill_main(int argc, char **argv)
{ {
int whichApp, sig = SIGTERM, quiet; int whichApp, signo = SIGTERM, quiet = 0;
const char *name; const char *name;
int errors = 0; int errors = 0;
@ -49,58 +48,59 @@ extern int kill_main(int argc, char **argv)
whichApp = KILL; whichApp = KILL;
#endif #endif
quiet=0;
argc--;
argv++;
/* Parse any options */ /* Parse any options */
if (argc < 1) if (argc < 2)
bb_show_usage(); bb_show_usage();
while (argc > 0 && **argv == '-') { if(argv[1][0] != '-'){
while (*++(*argv)) { argv++;
switch (**argv) { argc--;
#ifdef CONFIG_KILLALL goto do_it_now;
case 'q': }
quiet++;
break; /* The -l option, which prints out signal names. */
#endif if(argv[1][1]=='l' && argv[1][2]=='\0'){
case 'l': if(argc==2) {
if(argc>1) { /* Print the whole signal list */
for(argv++; *argv; argv++) { int col = 0;
name = u_signal_names(*argv, &sig, -1); for(signo=1; signo < NSIG; signo++) {
if(name!=NULL) name = u_signal_names(0, &signo, 1);
printf("%s\n", name); if(name==NULL) /* unnamed */
} continue;
} else { col += printf("%2d) %-16s", signo, name);
int col = 0; if (col > 60) {
for(sig=1; sig < NSIG; sig++) { printf("\n");
name = u_signal_names(0, &sig, 1); col = 0;
if(name==NULL) /* unnamed */ }
continue;
col += printf("%2d) %-16s", sig, name);
if (col > 60) {
printf("\n");
col = 0;
}
}
printf("\n");
}
return EXIT_SUCCESS;
case '-':
bb_show_usage();
default:
name = u_signal_names(*argv, &sig, 0);
if(name==NULL)
bb_error_msg_and_die( "bad signal name: %s", *argv);
argc--;
argv++;
goto do_it_now;
} }
argc--; printf("\n");
argv++;
} else {
for(argv++; *argv; argv++) {
name = u_signal_names(*argv, &signo, -1);
if(name!=NULL)
printf("%s\n", name);
}
}
/* If they specified -l, were all done */
return EXIT_SUCCESS;
}
/* The -q quiet option */
if(argv[1][1]=='q' && argv[1][2]=='\0'){
quiet++;
argv++;
argc--;
if(argv[1][0] != '-'){
goto do_it_now;
} }
} }
if(!u_signal_names(argv[1]+1, &signo, 0))
bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1);
argv+=2;
argc-=2;
do_it_now: do_it_now:
if (whichApp == KILL) { if (whichApp == KILL) {
@ -109,9 +109,9 @@ do_it_now:
int pid; int pid;
if (!isdigit(**argv)) if (!isdigit(**argv))
bb_perror_msg_and_die( "Bad PID"); bb_error_msg_and_die( "Bad PID '%s'", *argv);
pid = strtol(*argv, NULL, 0); pid = strtol(*argv, NULL, 0);
if (kill(pid, sig) != 0) { if (kill(pid, signo) != 0) {
bb_perror_msg( "Could not kill pid '%d'", pid); bb_perror_msg( "Could not kill pid '%d'", pid);
errors++; errors++;
} }
@ -127,22 +127,22 @@ do_it_now:
long* pidList; long* pidList;
pidList = find_pid_by_name(*argv); pidList = find_pid_by_name(*argv);
if (*pidList <= 0) { if (!pidList || *pidList<=0) {
errors++; errors++;
if (quiet==0) if (quiet==0)
bb_error_msg( "%s: no process killed", *argv); bb_error_msg( "%s: no process killed", *argv);
} else { } else {
long *pl; long *pl;
for(pl = pidList; *pl !=0 ; pl++) { for(pl = pidList; *pl !=0 ; pl++) {
if (*pl==myPid) if (*pl==myPid)
continue; continue;
if (kill(*pl, sig) != 0) { if (kill(*pl, signo) != 0) {
errors++; errors++;
if (quiet==0) if (quiet==0)
bb_perror_msg( "Could not kill pid '%ld'", *pl); bb_perror_msg( "Could not kill pid '%ld'", *pl);
}
} }
}
} }
free(pidList); free(pidList);
argv++; argv++;