From 7d72e796d6362872fe0075f936589bba23124344 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Sat, 26 Jul 2003 07:41:56 +0000 Subject: [PATCH] Rework kill / killall so it behaves itself, even when subjected to abuse. -Erik --- procps/kill.c | 122 +++++++++++++++++++++++++------------------------- 1 file 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 @@ static const int KILL = 0; static const int KILLALL = 1; - extern int kill_main(int argc, char **argv) { - int whichApp, sig = SIGTERM, quiet; + int whichApp, signo = SIGTERM, quiet = 0; const char *name; int errors = 0; @@ -49,58 +48,59 @@ extern int kill_main(int argc, char **argv) whichApp = KILL; #endif - quiet=0; - argc--; - argv++; /* Parse any options */ - if (argc < 1) + if (argc < 2) bb_show_usage(); - while (argc > 0 && **argv == '-') { - while (*++(*argv)) { - switch (**argv) { -#ifdef CONFIG_KILLALL - case 'q': - quiet++; - break; -#endif - case 'l': - if(argc>1) { - for(argv++; *argv; argv++) { - name = u_signal_names(*argv, &sig, -1); - if(name!=NULL) - printf("%s\n", name); - } - } else { - int col = 0; - for(sig=1; sig < NSIG; sig++) { - name = u_signal_names(0, &sig, 1); - 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; + if(argv[1][0] != '-'){ + argv++; + argc--; + goto do_it_now; + } + + /* The -l option, which prints out signal names. */ + if(argv[1][1]=='l' && argv[1][2]=='\0'){ + if(argc==2) { + /* Print the whole signal list */ + int col = 0; + for(signo=1; signo < NSIG; signo++) { + name = u_signal_names(0, &signo, 1); + if(name==NULL) /* unnamed */ + continue; + col += printf("%2d) %-16s", signo, name); + if (col > 60) { + printf("\n"); + col = 0; + } + } + printf("\n"); + + } else { + for(argv++; *argv; argv++) { + name = u_signal_names(*argv, &signo, -1); + if(name!=NULL) + printf("%s\n", name); } - argc--; - argv++; } + /* 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: if (whichApp == KILL) { @@ -109,9 +109,9 @@ do_it_now: int pid; if (!isdigit(**argv)) - bb_perror_msg_and_die( "Bad PID"); + bb_error_msg_and_die( "Bad PID '%s'", *argv); pid = strtol(*argv, NULL, 0); - if (kill(pid, sig) != 0) { + if (kill(pid, signo) != 0) { bb_perror_msg( "Could not kill pid '%d'", pid); errors++; } @@ -127,22 +127,22 @@ do_it_now: long* pidList; pidList = find_pid_by_name(*argv); - if (*pidList <= 0) { + if (!pidList || *pidList<=0) { errors++; if (quiet==0) bb_error_msg( "%s: no process killed", *argv); - } else { - long *pl; - - for(pl = pidList; *pl !=0 ; pl++) { - if (*pl==myPid) - continue; - if (kill(*pl, sig) != 0) { - errors++; - if (quiet==0) - bb_perror_msg( "Could not kill pid '%ld'", *pl); - } + } else { + long *pl; + + for(pl = pidList; *pl !=0 ; pl++) { + if (*pl==myPid) + continue; + if (kill(*pl, signo) != 0) { + errors++; + if (quiet==0) + bb_perror_msg( "Could not kill pid '%ld'", *pl); } + } } free(pidList); argv++; -- 2.25.1